dynamic_dict.py :  » Database » SQLAlchemy » SQLAlchemy-0.6.0 » examples » dynamic_dict » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLAlchemy 
SQLAlchemy » SQLAlchemy 0.6.0 » examples » dynamic_dict » dynamic_dict.py
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()

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.