File: dynamic_dict.py

package info (click to toggle)
sqlalchemy 0.6.3-3%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,744 kB
  • ctags: 15,132
  • sloc: python: 93,431; ansic: 787; makefile: 137; xml: 17
file content (87 lines) | stat: -rw-r--r-- 2,510 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class ProxyDict(object):
    def __init__(self, parent, collection_name, childclass, keyname):
        self.parent = parent
        self.collection_name = collection_name
        self.childclass = childclass
        self.keyname = keyname
    
    @property
    def collection(self):
        return getattr(self.parent, self.collection_name)
    
    def keys(self):
        descriptor = getattr(self.childclass, self.keyname)
        return [x[0] for x in self.collection.values(descriptor)]
        
    def __getitem__(self, key):
        x = self.collection.filter_by(**{self.keyname:key}).first()
        if x:
            return x
        else:
            raise KeyError(key)

    def __setitem__(self, key, value):
        try:
            existing = self[key]
            self.collection.remove(existing)
        except KeyError:
            pass
        self.collection.append(value)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship

engine=create_engine('sqlite://', echo=True)
Base = declarative_base(engine)

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    _collection = relationship("Child", lazy="dynamic", cascade="all, delete-orphan")
    
    @property
    def child_map(self):
        return ProxyDict(self, '_collection', Child, 'key')
    
class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    key = Column(String(50))
    parent_id = Column(Integer, ForeignKey('parent.id'))

    def __repr__(self):
        return "Child(key=%r)" % self.key
    
Base.metadata.create_all()

sess = sessionmaker()()

p1 = Parent(name='p1')
sess.add(p1)

print "\n---------begin setting nodes, autoflush occurs\n"
p1.child_map['k1'] = Child(key='k1')
p1.child_map['k2'] = Child(key='k2')

# this will autoflush the current map.
# ['k1', 'k2']
print "\n---------print keys - flushes first\n"
print p1.child_map.keys()

# k1
print "\n---------print 'k1' node\n"
print p1.child_map['k1']

print "\n---------update 'k2' node - must find existing, and replace\n"
p1.child_map['k2'] = Child(key='k2')

print "\n---------print 'k2' key - flushes first\n"
# k2
print p1.child_map['k2']

print "\n---------print all child nodes\n"
# [k1, k2b]
print sess.query(Child).all()