Skip to content

Commit 721ead2

Browse files
committed
Decorators @classmethod and @staticmethod are used everywhere.
git-svn-id: http://svn.colorstudy.com/SQLObject/trunk@4385 95a46c32-92d2-0310-94a5-8d71aeb3d4b3
1 parent 4699423 commit 721ead2

21 files changed

Lines changed: 89 additions & 126 deletions

docs/News.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Features & Interface
1515

1616
* SelectResults (returned from .select()) is allowed in IN(column, list).
1717

18+
Source code
19+
-----------
20+
21+
* Decorators @classmethod and @staticmethod are used everywhere.
22+
1823
Internals
1924
---------
2025

docs/TODO.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
TODO
22
----
33

4-
* Decorators @classmethod and @staticmethod.
5-
64
* Declare one encoding for all UnicodeCol's per table or even per connection.
75
Don't forget about fromDatabase.
86

sqlobject/boundattributes.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __instanceinit__(self, new_attrs):
6868
declarative.Declarative.__instanceinit__(self, new_attrs)
6969
self.__dict__['_all_attrs'] = self._add_attrs(self, new_attrs)
7070

71+
@staticmethod
7172
def _add_attrs(this_object, new_attrs):
7273
private = this_object._private_variables
7374
all_attrs = list(this_object._all_attrs)
@@ -77,8 +78,8 @@ def _add_attrs(this_object, new_attrs):
7778
if key not in all_attrs:
7879
all_attrs.append(key)
7980
return tuple(all_attrs)
80-
_add_attrs = staticmethod(_add_attrs)
8181

82+
@declarative.classinstancemethod
8283
def __addtoclass__(self, cls, added_class, attr_name):
8384
me = self or cls
8485
attrs = {}
@@ -101,18 +102,14 @@ def rebind(new_class):
101102

102103
me.set_object(added_class, attr_name, obj)
103104

104-
__addtoclass__ = declarative.classinstancemethod(__addtoclass__)
105-
105+
@classmethod
106106
def set_object(cls, added_class, attr_name, obj):
107107
setattr(added_class, attr_name, obj)
108108

109-
set_object = classmethod(set_object)
110-
109+
@classmethod
111110
def make_object(cls, added_class, attr_name, *args, **attrs):
112111
raise NotImplementedError
113112

114-
make_object = classmethod(make_object)
115-
116113
def __setattr__(self, name, value):
117114
self.__dict__['_all_attrs'] = self._add_attrs(self, {name: value})
118115
self.__dict__[name] = value

sqlobject/col.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ def _maxdbType(self):
11361136

11371137
class DateTimeCol(Col):
11381138
baseClass = SODateTimeCol
1139+
@staticmethod
11391140
def now():
11401141
if default_datetime_implementation == DATETIME_IMPLEMENTATION:
11411142
return datetime.datetime.now()
@@ -1145,7 +1146,6 @@ def now():
11451146
assert 0, ("No datetime implementation available "
11461147
"(DATETIME_IMPLEMENTATION=%r)"
11471148
% DATETIME_IMPLEMENTATION)
1148-
now = staticmethod(now)
11491149

11501150

11511151
class DateValidator(DateTimeValidator):

sqlobject/dbconnection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,15 @@ def uri(self):
129129
db = db[1:]
130130
return uri + urllib.quote(db)
131131

132+
@classmethod
132133
def connectionFromOldURI(cls, uri):
133134
return cls._connectionFromParams(*cls._parseOldURI(uri))
134-
connectionFromOldURI = classmethod(connectionFromOldURI)
135135

136+
@classmethod
136137
def connectionFromURI(cls, uri):
137138
return cls._connectionFromParams(*cls._parseURI(uri))
138-
connectionFromURI = classmethod(connectionFromURI)
139139

140+
@staticmethod
140141
def _parseOldURI(uri):
141142
schema, rest = uri.split(':', 1)
142143
assert rest.startswith('/'), "URIs must start with scheme:/ -- you did not include a / (in %r)" % rest
@@ -185,8 +186,8 @@ def _parseOldURI(uri):
185186
argvalue = urllib.unquote(argvalue)
186187
args[argname] = argvalue
187188
return user, password, host, port, path, args
188-
_parseOldURI = staticmethod(_parseOldURI)
189189

190+
@staticmethod
190191
def _parseURI(uri):
191192
protocol, request = urllib.splittype(uri)
192193
user, password, port = None, None, None
@@ -224,7 +225,6 @@ def _parseURI(uri):
224225
args[name] = value
225226

226227
return user, password, host, port, path, args
227-
_parseURI = staticmethod(_parseURI)
228228

229229
def soClassAdded(self, soClass):
230230
"""

sqlobject/declarative.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,16 @@ def __call__(self, *args, **kw):
158158
kw['__alsocopy'] = self.__dict__
159159
return self.__class__(*args, **kw)
160160

161+
@classinstancemethod
161162
def singleton(self, cls):
162163
if self:
163164
return self
164165
name = '_%s__singleton' % cls.__name__
165166
if not hasattr(cls, name):
166167
setattr(cls, name, cls(declarative_count=cls.declarative_count))
167168
return getattr(cls, name)
168-
singleton = classinstancemethod(singleton)
169169

170+
@classinstancemethod
170171
def __repr__(self, cls):
171172
if self:
172173
name = '%s object' % self.__class__.__name__
@@ -188,15 +189,13 @@ def __repr__(self, cls):
188189
else:
189190
return '<%s %s>' % (name, ' '.join(args))
190191

192+
@staticmethod
191193
def _repr_vars(dictNames):
192194
names = [n for n in dictNames
193195
if not n.startswith('_')
194196
and n != 'declarative_count']
195197
names.sort()
196198
return names
197-
_repr_vars = staticmethod(_repr_vars)
198-
199-
__repr__ = classinstancemethod(__repr__)
200199

201200
def setup_attributes(cls, new_attrs):
202201
for name, value in new_attrs.items():

sqlobject/firebird/firebirdconnection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, host, port, db, user='sysdba',
3131

3232
DBAPI.__init__(self, **kw)
3333

34+
@classmethod
3435
def _connectionFromParams(cls, auth, password, host, port, path, args):
3536
if not password:
3637
password = 'masterkey'
@@ -41,7 +42,6 @@ def _connectionFromParams(cls, auth, password, host, port, path, args):
4142
path = path[1:]
4243
path = path.replace('/', os.sep)
4344
return cls(host, port, db=path, user=auth, password=password, **args)
44-
_connectionFromParams = classmethod(_connectionFromParams)
4545

4646
def _runWithConnection(self, meth, *args):
4747
if not self.autoCommit:
@@ -104,6 +104,7 @@ def _queryInsertID(self, conn, soInstance, id, names, values):
104104
self.printDebug(conn, id, 'QueryIns', 'result')
105105
return id
106106

107+
@classmethod
107108
def _queryAddLimitOffset(cls, query, start, end):
108109
"""Firebird slaps the limit and offset (actually 'first' and
109110
'skip', respectively) statement right after the select."""
@@ -119,7 +120,6 @@ def _queryAddLimitOffset(cls, query, start, end):
119120
return ' '.join([limit_str, match.group(2)])
120121
else:
121122
return query
122-
_queryAddLimitOffset = classmethod(_queryAddLimitOffset)
123123

124124
def createTable(self, soClass):
125125
self.query('CREATE TABLE %s (\n%s\n)' % \

sqlobject/inheritance/__init__.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def accumulateMany(self, *attributes, **kw):
9393
return clone.accumulateMany(skipInherited=True, *attributes)
9494

9595
class InheritableSQLMeta(sqlmeta):
96+
@classmethod
9697
def addColumn(sqlmeta, columnDef, changeSchema=False, connection=None, childUpdate=False):
9798
soClass = sqlmeta.soClass
9899
#DSM: Try to add parent properties to the current class
@@ -133,8 +134,7 @@ def setfunc(self, val):
133134
c.sqlmeta.addColumn(columnDef, connection=connection, childUpdate=True)
134135
if q: setattr(c.q, columnDef.name, q)
135136

136-
addColumn = classmethod(addColumn)
137-
137+
@classmethod
138138
def delColumn(sqlmeta, column, changeSchema=False, connection=None, childUpdate=False):
139139
if childUpdate:
140140
soClass = sqlmeta.soClass
@@ -157,8 +157,7 @@ def delColumn(sqlmeta, column, changeSchema=False, connection=None, childUpdate=
157157
c.sqlmeta.delColumn(column, changeSchema=changeSchema,
158158
connection=connection, childUpdate=True)
159159

160-
delColumn = classmethod(delColumn)
161-
160+
@classmethod
162161
def addJoin(sqlmeta, joinDef, childUpdate=False):
163162
soClass = sqlmeta.soClass
164163
#DSM: Try to add parent properties to the current class
@@ -188,8 +187,7 @@ def addJoin(sqlmeta, joinDef, childUpdate=False):
188187
for c in sqlmeta.childClasses.values():
189188
c.sqlmeta.addJoin(joinDef, childUpdate=True)
190189

191-
addJoin = classmethod(addJoin)
192-
190+
@classmethod
193191
def delJoin(sqlmeta, joinDef, childUpdate=False):
194192
if childUpdate:
195193
soClass = sqlmeta.soClass
@@ -204,23 +202,21 @@ def delJoin(sqlmeta, joinDef, childUpdate=False):
204202
for c in sqlmeta.childClasses.values():
205203
c.sqlmeta.delJoin(joinDef, childUpdate=True)
206204

207-
delJoin = classmethod(delJoin)
208-
205+
@classmethod
209206
def getAllColumns(sqlmeta):
210207
columns = sqlmeta.columns.copy()
211208
sm = sqlmeta
212209
while sm.parentClass:
213210
columns.update(sm.parentClass.sqlmeta.columns)
214211
sm = sm.parentClass.sqlmeta
215212
return columns
216-
getAllColumns = classmethod(getAllColumns)
217213

214+
@classmethod
218215
def getColumns(sqlmeta):
219216
columns = sqlmeta.getAllColumns()
220217
if columns.has_key('childName'):
221218
del columns['childName']
222219
return columns
223-
getColumns = classmethod(getColumns)
224220

225221

226222
class InheritableSQLObject(SQLObject):
@@ -249,7 +245,7 @@ def __classinit__(cls, new_attrs):
249245
getattr(currentClass.q, column.name))
250246
currentClass = currentClass.sqlmeta.parentClass
251247

252-
# @classmethod
248+
@classmethod
253249
def _SO_setupSqlmeta(cls, new_attrs, is_base):
254250
# Note: cannot use super(InheritableSQLObject, cls)._SO_setupSqlmeta -
255251
# InheritableSQLObject is not defined when it's __classinit__
@@ -284,8 +280,7 @@ def _SO_setupSqlmeta(cls, new_attrs, is_base):
284280
if not sqlmeta.childName:
285281
sqlmeta.childName = cls.__name__
286282

287-
_SO_setupSqlmeta = classmethod(_SO_setupSqlmeta)
288-
283+
@classmethod
289284
def get(cls, id, connection=None, selectResults=None, childResults=None, childUpdate=False):
290285

291286
val = super(InheritableSQLObject, cls).get(id, connection, selectResults)
@@ -318,8 +313,7 @@ def get(cls, id, connection=None, selectResults=None, childResults=None, childUp
318313
#DSM: We can now return ourself
319314
return val
320315

321-
get = classmethod(get)
322-
316+
@classmethod
323317
def _notifyFinishClassCreation(cls):
324318
sqlmeta = cls.sqlmeta
325319
# verify names of added columns
@@ -347,7 +341,6 @@ def _notifyFinishClassCreation(cls):
347341
# There are no joins - call addJoin to propagate joins
348342
# from parent classes to children
349343
sqlmeta.addJoin(None)
350-
_notifyFinishClassCreation = classmethod(_notifyFinishClassCreation)
351344

352345
def _create(self, id, **kw):
353346

@@ -400,14 +393,15 @@ def _create(self, id, **kw):
400393
# TC: Reraise the original exception
401394
raise
402395

396+
@classmethod
403397
def _findAlternateID(cls, name, dbName, value, connection=None):
404398
result = list(cls.selectBy(connection, **{name: value}))
405399
if not result:
406400
return result, None
407401
obj = result[0]
408402
return [obj.id], obj
409-
_findAlternateID = classmethod(_findAlternateID)
410403

404+
@classmethod
411405
def select(cls, clause=None, *args, **kwargs):
412406
parentClass = cls.sqlmeta.parentClass
413407
childUpdate = kwargs.pop('childUpdate', None)
@@ -469,8 +463,8 @@ def _patch_id_clause(clause):
469463
else:
470464
return super(InheritableSQLObject, cls).select(
471465
clause, *args, **kwargs)
472-
select = classmethod(select)
473466

467+
@classmethod
474468
def selectBy(cls, connection=None, **kw):
475469
clause = []
476470
foreignColumns = {}
@@ -504,8 +498,6 @@ def selectBy(cls, connection=None, **kw):
504498
conn = connection or cls._connection
505499
return cls.SelectResultsClass(cls, clause, connection=conn)
506500

507-
selectBy = classmethod(selectBy)
508-
509501
def destroySelf(self):
510502
#DSM: If this object has parents, recursivly kill them
511503
if hasattr(self, '_parent') and self._parent:

0 commit comments

Comments
 (0)