smartdict.py (6997:5af4976c17e2) smartdict.py (13697:8d4afe1c365e)
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

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

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):
55 return toLong(str(self))
56 def __float__(self):
57 return toFloat(str(self))
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

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

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):
55 return toLong(str(self))
56 def __float__(self):
57 return toFloat(str(self))
58 def __nonzero__(self):
58 def __bool__(self):
59 return toBool(str(self))
59 return toBool(str(self))
60 # Python 2.7 uses __nonzero__ instead of __bool__
61 __nonzero__ = __bool__
60 def convert(self, other):
61 t = type(other)
62 if t == bool:
63 return bool(self)
64 if t == int:
65 return int(self)
66 if t == long:
67 return long(self)

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

102 return other / self.convert(other)
103 def __rtruediv__(self, other):
104 return other / self.convert(other)
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"""
62 def convert(self, other):
63 t = type(other)
64 if t == bool:
65 return bool(self)
66 if t == int:
67 return int(self)
68 if t == long:
69 return long(self)

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

104 return other / self.convert(other)
105 def __rtruediv__(self, other):
106 return other / self.convert(other)
107
108class UndefinedVariable(object):
109 """Placeholder class to represent undefined variables. Will
110 generally cause an exception whenever it is used, but evaluates to
111 zero for boolean truth testing such as in an if statement"""
110 def __nonzero__(self):
112 def __bool__(self):
111 return False
112
113 return False
114
115 # Python 2.7 uses __nonzero__ instead of __bool__
116 __nonzero__ = __bool__
117
113class SmartDict(attrdict):
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

--- 35 unchanged lines hidden ---
118class SmartDict(attrdict):
119 """Dictionary class that holds strings, but intelligently converts
120 those strings to other types depending on their usage"""
121
122 def __getitem__(self, key):
123 """returns a Variable proxy if the values exists in the database and
124 returns an UndefinedVariable otherwise"""
125

--- 35 unchanged lines hidden ---