Property Bag Style Database
Project description
Description
The propbd package is a simple database module. It is basically a persisting Python dictionary.
Property item name’s are dictionary keys and the dictionary values are property items. The keys are enforced to be of basestring type whereas the values can be anything, almost. The property bag is saved to the file system by serializing the dictionary as JSON; therefore, the property item values must be able to be seriailized by the json module.
Property bag name and location form the path where the bag is saved. If no location is set then the current working directory is used. Property bags are created by instantiating a propbag. Property items are created by calling add from the bag instance.
If autosave is true then the bag will automatically save when property items are added, updated, or dropped. If multiple items need to be added, updated, or dropped then suspend autosave before operations by calling suspend_autosave(). Resume autosaving once operations are completed by calling resume_autosave(). If autosave is false then the bag can manually be saved by calling the save() method. Checking the is_dirty property on the bag indicates if the bag needs to be saved.
Property items in a property bag can be iterated directly on the instance of the bag (e.g. for item in bag).
Installation
pip install propdb
Basic Usage
from propdb import propbag as bag from propdb import propbag as item bag1 = bag('bag1', autosave = True) bag1.add('item1') bag1.add(('item2', 'Value as a string', 'item3', 3333)) item1 = bag1['item1'] item1.value = [1, 2, 3, 4] bag1.set(item1) bag1.set(('item2', 'A new string value')) bag1.set({'item3':12345})
Imports
from propdb import propbag from propdb import propitem
Create a new property bag
propbag('name', 'location', autosave = True)
Adding property items
Use the propbag.add(items) method to add property item(s) to the bag.
Returns a list of all the added property items.
Adding property items is very flexible. It is acceptable to add one or many with one call. It can be just a name, a collection, or a property item instance. Adding by list can be name/value pairs or a collection of property items. Adding by dictionary uses the key for the name and the value for the property item’s value.
# adds a property item with the value set to None propbag.add('item1') # by list of name/value pairs propbag.add(('item1', 1, 'item2', 2, ...)) # by dictionary propbag.add({'item1':1, 'item2':2, ...}) # by list or propitems propbag.add((propitem, propitem, ...)) # by propitem propbag.add(propitem)
Adding property items with the + and += operators can be done in the same exact way as the add method (e.g. propbag + propitem, etc). The only difference is that there is no return value when adding by operator.
For example:
propbag + 'item1' propbag += ('item1', 1, 'item2', 2, ...)
Updating property items
Use propbag.set to update property item(s) in the bag.
Returns a list of all the updated property items.
Updating property items is very flexible. It is acceptable to update one or many with one call. It can be a collection or an instance of a property item. Updating by list must be by name/value pairs or a collection of property items. Updating by dictionary assumes the key to be the name and the dictionary value the value of the property item.
# by list of name/value pairs propbag.set(('item1', 1, 'item2', 2, ...)) # by dictionary propbag.set({'item1':1, 'item2':2, ...}) # by list or propitems propbag.set((propitem, propitem, ...)) # by propitem propbag.set(propitem)
Updating property items with the [] operator can be done by indexing the property bag with the name of the property item and passing in a new value or a property item.
For example:
propbag['item1'] = value propbag[propitem.name] = propitem
Deleting property items
Use propbag.drop to delete property item(s) from the bag.
Returns the number of property items dropped.
Dropping property items is very flexible. It is acceptable to drop one or many with one call. It can be a single string, property item, or list.
Delete one property item by passing the name of the item to delete. Passing a list can either be a list of names or property items, mixed is acceptable as well (e.g. (‘item1’, propitem)).
# drops one property by name propbag.drop('item1') # drops one property item by name propbag.drop(propitem) # by list of names propbag.drop(('item1', 'item2', 'item3', ...)) # by list of propitems propbag.drop((propitem, propitem, ...))
Dropping property items with the - and -= operators can be done in the same exact way as the drop method (e.g. propbag - propitem, etc). The only difference is that there is no return value when dropping by operator.
For example:
propbag - 'item1' # drops one property item by name propbag -= ('item1', 'item2', 'item3', ...) # via list of names
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.