Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(2)  |  Call(1)  |  Derive(0)  |  Import(1)
An authenticated dictionary, based on a Merkle Trie, and stored on disk.

Nodes are identified by pointers (really strings).

The whole thing is backed by an SQLite database, but has additional
transaction support. You can make a shallow copy of an
AuthenticatedDictionary, and insert, find, and delete on it without
affecting the original or other shallow copies. Copying a shallow copy with
n changes made is O(N). When you want to save your changes to disk, run
commit(). After one shallow copy (or the updated original) has been
comitted, no accesses are allowed to anything that isn't it or a descendant
of it created after the commit.

Each chain of an AuthenticatedDictionary and its shallow copy descendants
must have a unique database filename not shared with any other
AuthenticatedDictonary chain. It's fine to share it with other things, as
long as they never sync the database without calling commit on an
AuthenticatedDictionary first.

This is how you use it:
>>> a = AuthenticatedDictionary(":memory:")
>>> for x in map(str, xrange(100)):
...     a.insert(x, x)
...     a.commit()
>>> a.clear()
>>> a.insert("stuff", "wombats")
>>> components, root = a.dump_state_components()
>>> a.get_hash() == root
True
>>> a.commit()
>>> a.clear()
>>> b = a.copy()
>>> b.import_from_state_components(components, root)
>>> b.get_hash() == root
True
>>> b.find("stuff")
'wombats'
>>> c = a.copy()
>>> c.update_from_state_components(components, root)
>>> c.get_hash() == b.get_hash()
True
>>> c.find("stuff")
'wombats'
>>> a.insert("thing", "other thing")
>>> a.commit()
>>> for x in map(str, xrange(100)):
...     a.insert(x, x)
...     a.commit()
>>> a.commit()
>>> util.bytes2hex(a.get_hash())
'3cf8a21949088a41f405e3f37c90af54e3cc33dce1c7b8226bd4e2450ddf2aff2d8dd089f3d78cd8a6dc5aea757a941868fd369daca22efd87b00d882b6e667d'
>>> a.find("thing")
'other thing'
>>> print a.find("some third thing")
None
>>> len(set(a.iterkeys()))
101
>>> a.insert("some third thing", "foo")
>>> a.remove("thing")
>>> a.remove("some third thing")
>>> a.insert("thing", "other thing")
>>> util.bytes2hex(a.get_hash())
'3cf8a21949088a41f405e3f37c90af54e3cc33dce1c7b8226bd4e2450ddf2aff2d8dd089f3d78cd8a6dc5aea757a941868fd369daca22efd87b00d882b6e667d'
>>> a.get_node_by_hash(a.get_hash())
'root'
>>> a.commit()
>>> a.get_node_by_hash(a.get_hash())
'root'
>>> a.insert("thing", "a different thing")
>>> util.bytes2hex(a.get_hash())
'af8e3873a0eb172a1aefe78661630f908266e4edbcaae9752ca0fc6a441bad65448543240d17f7ced11d5dad2f72a12ffab1afd7ad600ee4cfb89cdb4a5c64c8'

This next test depends explicitly on internal pointer values.
>>> a.node_to_state_component("100") # doctest: +NORMALIZE_WHITESPACE
StateComponent gDoDgLKgh+RqaXBf6kdnQ67qkSy4wXwcQ/jdNFApA/rGZFssb87C+4ygOid5l3yNr1m9i9inTKl+pWhOQC0rRg==
            <13 bytes of data>


We also get cool order independence
>>> import random
>>> observed_hashes = set()
>>> for i in xrange(10):
...     items = [str(n) for n in xrange(10)]
...     random.shuffle(items)
...     d = AuthenticatedDictionary(":memory:", table="test{}".format(i))
...     for item in items:
...         d.insert(item, item)
...         d.commit()
...     random.shuffle(items)
...     for item in items:
...         d.remove(item)
...         d.commit()
...     random.shuffle(items)
...     for item in items:
...         d.insert(item, item)
...         d.commit()
...     random.shuffle(items)
...     for item in items:
...         d.insert(item, "{} updated".format(item))
...         d.commit()
...     observed_hashes.add(d.get_hash())
>>> len(observed_hashes)
1

A demo of the shallow copy semantics:
>>> c0 = AuthenticatedDictionary(":memory:", table="copies")
>>> c0.insert("thing", "other thing")
>>> digest = c0.get_hash()
>>> c0.get_hash() == digest
True

Note that we can copy without committing. This is O(number of updates made
since last commit)
>>> c1 = c0.copy()
>>> c0.get_hash() == digest
True
>>> c1.find("thing")
'other thing'
>>> c1.insert("thing", "not a thing")
>>> c0.get_hash() == digest
True
>>> c1.find("thing")
'not a thing'
>>> c0.find("thing")
'other thing'
>>> c0.get_hash() == digest
True
>>> c1.get_hash() == digest
False
>>> print c1.get_node_by_hash(digest)
None
>>> c0.remove("thing")
>>> print c0.find("thing")
None
>>> c1.find("thing")
'not a thing'
>>> c1.commit()

src/p/y/pybc-0.6.8/pybc/coin.py   pybc(Download)
import pybc
import transactions, util, sqliteshelf
from AuthenticatedDictionary import AuthenticatedDictionary
from AuthenticatedDictionary import AuthenticatedDictionaryStateComponent
from State import State
            # We need to make a new AuthenticatedDictionary pointing to the
            # appropriate database and table.
            self.unused_outputs = AuthenticatedDictionary(filename=filename, 
                table=table)
        else: