Souper

Generic Indexed Storage based on ZODB
Download

Souper Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • BlueDynamics Alliance
  • Publisher web site:
  • http://bluedynamics.com

Souper Tags


Souper Description

Souper is a Python tool for programmers. It offers an integrated storage tied together with indexes in a catalog. The records in the storage are generic. It is possible to store any data on a record if it is persistent pickable in ZODB.Souper can be used used in any python application using the ZODB, such as Pyramid or Plone.Using SouperProviding a LocatorSoups are looked up by adapting souper.interfaces.IStorageLocator to some context. Souper does not provide any default locator. So first one need to be provided. Let's assume context is some persistent dict-like instance:>>> from zope.interface import implementer>>> from zope.interface import Interface>>> from zope.component import provideAdapter>>> from souper.interfaces import IStorageLocator>>> from souper.soup import SoupData>>> @implementer(IStorageLocator)... class StorageLocator(object):...... def __init__(self, context):... self.context = context...... def storage(self, soup_name):... if soup_name not in self.context:... self.context = SoupData()... return self.context>>> provideAdapter(StorageLocator, adapts=)So we have locator creating soups by name on the fly. Now its easy to get a soup by name:>>> from souper.soup import get_soup>>> soup = get_soup('mysoup', context)>>> soup< souper.soup.Soup object at 0x... >Providing a Catalog FactoryDepending on your needs the catalog and its indexes may look different from use-case to use-case. The catalog factory is responsible to create a catalog for a soup. The factory is a named utility implementing souper.interfaces.ICatalogFactory. The name of the utility has to the the same as the soup have.Here repoze.catalog is used and to let the indexes access the data on the records by key the NodeAttributeIndexer is used. For special cases one may write its custom indexers, but the default one is fine most of the time:>>> from souper.interfaces import ICatalogFactory>>> from souper.soup import NodeAttributeIndexer>>> from zope.component import provideUtility>>> from repoze.catalog.catalog import Catalog>>> from repoze.catalog.indexes.field import CatalogFieldIndex>>> from repoze.catalog.indexes.text import CatalogTextIndex>>> from repoze.catalog.indexes.keyword import CatalogKeywordIndex>>> @implementer(ICatalogFactory)... class MySoupCatalogFactory(object):...... def __call__(self, context=None):... catalog = Catalog()... userindexer = NodeAttributeIndexer('user')... catalog = CatalogFieldIndex(userindexer)... textindexer = NodeAttributeIndexer('text')... catalog = CatalogTextIndex(textindexer)... keywordindexer = NodeAttributeIndexer('keywords')... catalog = CatalogKeywordIndex(keywordindexer)... return catalog>>> provideUtility(MySoupCatalogFactory(), name="mysoup")The catalog factory is used soup-internal only but one may want to check if it works fine:>>> catalogfactory = getUtility(ICatalogFactory, name='mysoup')>>> catalogfactory< MySoupCatalogFactory object at 0x... >>>> catalog = catalogfactory()>>> sorted(catalog.items())Adding recordsAs mentioned above the souper.soup.Record is the one and only kind of data added to the soup. A record has attributes containing the data:>>> from souper.soup import get_soup>>> from souper.soup import Record>>> soup = get_soup('mysoup', context)>>> record = Record()>>> record.attrs = 'user1'>>> record.attrs = u'foo bar baz'>>> record.attrs = >>> record_id = soup.add(record)A record may contains other records. But to index them one would need a custom indexer, so usally conatined records are valuable for later display, not for searching:>>> record = Record()>>> record.attrs = '6020'>>> record.attrs = 'Innsbruck'>>> record.attrs = 'Austria'Access dataEven without any query a record can be fetched by id:>>> from souper.soup import get_soup>>> soup = get_soup('mysoup', context)>>> record = soup.get(record_id)All records can be accessed using utilizing the container BTree:>>> soup.data.keys() == record_idTrueQuery dataHow to query a repoze catalog is documented well. Sorting works the same too. Queries are passed to soups query method (which uses then repoze catalog). It returns a generator:>>> from repoze.catalog.query import Eq>>> >>> []To also get the size of the result set pass a with_size=True to the query. The first item returned by the generator is the size:>>> To optimize handling of large result sets one may not to fetch the record but a generator returning light weight objects. Records are fetched on call:>>> lazy = >>> lazy()< Record object 'None' at ... >Here the size is passed as first value of the geneartor too if with_size=True is passed.Delete a recordTo remove a record from the soup python del is used like one would do on any dict:>>> del soupReindexAfter a records data changed it needs a reindex:>>> record.attrs = 'user1'>>> soup.reindex(records=)Sometimes one may want to reindex all data. Then reindex has to be called without parameters. It may take a while:>>> soup.reindex()Rebuild catalogUsally after a change of catalog factory was made - i.e. some index was added - a rebuild of the catalog i needed. It replaces the current catalog with a new one created by the catalog factory and reindexes all data. It may take while:>>> soup.rebuild()Reset (or clear) the soupTo remove all data from the soup and empty and rebuild the catalog call clear. Attention: all data is lost!>>> soup.clear()Installation NotesIn order to use souper node.ext.zodb is needed. As this beta was released there was no release (will be done soon). Please check pypi if it is there, otherwise please fetch node.ext.zodb from github.Product's homepage


Souper Related Software