Giter Club home page Giter Club logo

ftw.dictstorage's Introduction

Introduction

This package provides a layer for storing key/value pairs. The storage can be configured dynamically by providing a IConfig adapter of the context on which the dict storage is used.

Examples

The adapter defaults to using a non-persistent dict:

>>> from ftw.dictstorage.interfaces import IDictStorage

>>> context = layer['example_context']
>>> print context
<ftw.dictstorage.testing.ExampleContext object at ...>

>>> storage = IDictStorage(context)
>>> storage['key'] = 'value'
>>> print storage['key']
value

>>> print storage.storage
{'key': 'value'}

>>> print IDictStorage(context).storage
{}

For configuring a custom storage, implement your own IConfig which uses custom IDictStorage:

>>> from ftw.dictstorage.interfaces import IConfig
>>> from ftw.dictstorage.base import DictStorage
>>> from zope.component import provideAdapter, adapts
>>> from zope.interface import Interface, alsoProvides, implements

>>> context = layer['example_context']
>>> class IMyContext(Interface):
...     pass
>>> alsoProvides(context, IMyContext)

>>> class ContextStorageConfig(object):
...     implements(IConfig)
...     adapts(IMyContext)
...
...     def __init__(self, context):
...         self.context = context
...
...     def get_storage(self):
...         if not hasattr(self.context, '_dictstorage'):
...             self.context._dictstorage = {}
...         return self.context._dictstorage
>>> provideAdapter(ContextStorageConfig)

>>> class ContextDictStorage(DictStorage):
...     implements(IDictStorage)
...     adapts(IMyContext, IConfig)
...
...     def __init__(self, context, config):
...         self.context = context
...         self.config = config
...         self._storage = config.get_storage()
...
...     @property
...     def storage(self):
...         return self._storage
...
>>> provideAdapter(ContextDictStorage)

>>> storage = IDictStorage(context)
>>> storage['foo'] = 'bar'
>>> print storage['foo']
bar

>>> print context._dictstorage
{'foo': 'bar'}

>>> print IDictStorage(context)['foo']
bar

Compatibility

Runs with Plone 4.2, 4.3, 5.1.

Links

Copyright

This package is copyright by 4teamwork.

ftw.dictstorage is licensed under GNU General Public License, version 2.

ftw.dictstorage's People

Contributors

jone avatar phgross avatar garbas avatar nachtalb avatar elioschmutz avatar lukasgraf avatar phabegger avatar maethu avatar

Watchers

Thomas Buchberger avatar Timon Tschanz avatar  avatar  avatar  avatar  avatar  avatar 4teamwork Jenkins avatar James Cloos avatar Bernhard Bühlmann avatar Murat Tokmak avatar Linus Luginbühl avatar Furkan Kalınsaz avatar Ursula Bühlmann avatar Fabian Guyer avatar Nicola Lorenz avatar  avatar Marco Baumgartner avatar  avatar Daniel Jowett avatar

ftw.dictstorage's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.