~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/examples/row_util.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from twisted.enterprise import row
 
2
 
 
3
 
 
4
##################################################
 
5
########## Definitions of Row Classes ############
 
6
##################################################
 
7
 
 
8
class KeyFactory:
 
9
    """This is a lame, but simple way to generate keys.
 
10
       For real code, use the database instead."""
 
11
    def __init__(self, minimum, pool):
 
12
        self.min = minimum
 
13
        self.pool = minimum + pool
 
14
        self.current = self.min
 
15
 
 
16
    def getNextKey(self):
 
17
        next = self.current + 1
 
18
        self.current = next
 
19
        if self.current >= self.pool:
 
20
            raise ValueError("Key factory key pool exceeded.")
 
21
        return next
 
22
 
 
23
def myRowFactory(rowClass, data, kw):
 
24
    newRow = rowClass()
 
25
    newRow.__dict__.update(kw)
 
26
    return newRow
 
27
 
 
28
class RoomRow(row.RowObject):
 
29
    rowColumns = [
 
30
        ("roomId",  "int"),
 
31
        ("town_id", "int"),
 
32
        ("name",    "varchar"),
 
33
        ("owner",   "varchar"),
 
34
        ("posx",    "int"),
 
35
        ("posy",    "int"),
 
36
        ("width",   "int"),
 
37
        ("height",  "int")
 
38
        ]
 
39
    rowKeyColumns    = [("roomId","int")]
 
40
    rowTableName     = "testrooms"
 
41
    rowFactoryMethod = [myRowFactory]
 
42
 
 
43
    def __init__(self):
 
44
        self.furniture = []
 
45
 
 
46
    def addStuff(self, stuff):
 
47
        self.furniture.append(stuff)
 
48
        
 
49
    def moveTo(self, x, y):
 
50
        self.posx = x
 
51
        self.posy = y
 
52
        
 
53
    def __repr__(self):
 
54
        return "<Room #%s: %s (%s) (%s,%s)>" % (self.roomId, self.name, self.owner, self.posx, self.posy)
 
55
 
 
56
class FurnitureRow(row.RowObject):
 
57
    rowColumns      = [
 
58
        ("furnId", "int"),
 
59
        ("roomId", "int"),
 
60
        ("name",   "varchar"),
 
61
        ("posx",   "int"),
 
62
        ("posy",   "int")
 
63
        ]
 
64
    rowKeyColumns   = [("furnId","int")]
 
65
    rowTableName    = "furniture"
 
66
    rowForeignKeys  = [("testrooms", [("roomId","int")], [("roomId","int")], "addStuff", 1) ]
 
67
    
 
68
    def __repr__(self):
 
69
        return "Furniture #%s: room #%s (%s) (%s,%s)" % (self.furnId, self.roomId, self.name, self.posx, self.posy)
 
70
 
 
71
class RugRow(row.RowObject):
 
72
    rowColumns       = [
 
73
        ("rugId",  "int"),
 
74
        ("roomId", "int"),
 
75
        ("name",   "varchar")
 
76
        ]
 
77
    rowKeyColumns    = [("rugId","int")]
 
78
    rowTableName     = "rugs"
 
79
    rowFactoryMethod = [myRowFactory]
 
80
    rowForeignKeys   = [( "testrooms", [("roomId","int")],[("roomId","int")], "addStuff", 1) ]
 
81
    
 
82
    def __repr__(self):
 
83
        return "Rug %#s: room #%s, (%s)" % (self.rugId, self.roomId, self.name)
 
84
 
 
85
class LampRow(row.RowObject):
 
86
    rowColumns      = [
 
87
        ("lampId",   "int"),
 
88
        ("furnId",   "int"),
 
89
        ("furnName", "varchar"),
 
90
        ("lampName", "varchar")
 
91
        ]
 
92
    rowKeyColumns   = [("lampId","int")]
 
93
    rowTableName    = "lamps"
 
94
    rowForeignKeys  = [("furniture",
 
95
                        [("furnId","int"),("furnName", "varchar")],  # child table columns (this table)
 
96
                        [("furnId","int"),("name", "varchar")],      # parent table columns (the other table)
 
97
                        None,
 
98
                        1)
 
99
                       ]
 
100
                      # NOTE: this has no containerMethod so children will be added to "childRows"
 
101
    
 
102
    def __repr__(self):
 
103
        return "Lamp #%s" % self.lampId