smartdict.py (6654:4c84e771cca7) smartdict.py (6997:5af4976c17e2)
1# Copyright (c) 2005 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

--- 29 unchanged lines hidden (view full) ---

38# proxy class for values and overriding __nonzero__ on the proxy.
39# Everything else is just to (a) make proxies behave like normal
40# values otherwise, (b) make sure any dict operation returns a proxy
41# rather than a normal value, and (c) coerce values written to the
42# dict to be strings.
43
44
45from convert import *
1# Copyright (c) 2005 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

--- 29 unchanged lines hidden (view full) ---

38# proxy class for values and overriding __nonzero__ on the proxy.
39# Everything else is just to (a) make proxies behave like normal
40# values otherwise, (b) make sure any dict operation returns a proxy
41# rather than a normal value, and (c) coerce values written to the
42# dict to be strings.
43
44
45from convert import *
46from attrdict import attrdict
46
47class Variable(str):
48 """Intelligent proxy class for SmartDict. Variable will use the
49 various convert functions to attempt to convert values to useable
50 types"""
51 def __int__(self):
52 return toInteger(str(self))
53 def __long__(self):

--- 50 unchanged lines hidden (view full) ---

104
105class UndefinedVariable(object):
106 """Placeholder class to represent undefined variables. Will
107 generally cause an exception whenever it is used, but evaluates to
108 zero for boolean truth testing such as in an if statement"""
109 def __nonzero__(self):
110 return False
111
47
48class Variable(str):
49 """Intelligent proxy class for SmartDict. Variable will use the
50 various convert functions to attempt to convert values to useable
51 types"""
52 def __int__(self):
53 return toInteger(str(self))
54 def __long__(self):

--- 50 unchanged lines hidden (view full) ---

105
106class UndefinedVariable(object):
107 """Placeholder class to represent undefined variables. Will
108 generally cause an exception whenever it is used, but evaluates to
109 zero for boolean truth testing such as in an if statement"""
110 def __nonzero__(self):
111 return False
112
112class SmartDict(dict):
113class SmartDict(attrdict):
113 """Dictionary class that holds strings, but intelligently converts
114 those strings to other types depending on their usage"""
115
116 def __getitem__(self, key):
117 """returns a Variable proxy if the values exists in the database and
118 returns an UndefinedVariable otherwise"""
119
120 if key in self:

--- 34 unchanged lines hidden ---
114 """Dictionary class that holds strings, but intelligently converts
115 those strings to other types depending on their usage"""
116
117 def __getitem__(self, key):
118 """returns a Variable proxy if the values exists in the database and
119 returns an UndefinedVariable otherwise"""
120
121 if key in self:

--- 34 unchanged lines hidden ---