HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Model for web applications

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
applicationsmodelforweb

Problem

I'm developing a model for web applications. The database that I use is Redis.

I created special DbField objects like HashDbField, SortedSetDbField, ScalarDbField and so on, for each data type in Redis. This objects provides a convenient way to work with redis keys. Each of it takes target key name in constructor

The second type of objects are DbObjects that represents objects in database. DbObjects (UserDbObject, PostDbObject) consist of DbFields. There are class fields, for some global registers and instance fields for values of certain objects.

Each dbObject has a DbObjectName class field which contains the current object name ('user', 'post', 'comment', and so on...)

DbObject class looks like:

```
class DbObjectType(type):
def __init__(cls, name, bases, dct):
super(DbObjectType, cls).__init__(name, bases, dct)
cls._TypeInit()

class DbObject(metaclass=DbObjectType):
DbObjectName = DB_OBJECT_NAME

@classmethod
def _TypeInit(cls):
cls._SelfDbObjectSerializator = DbObjectSerializator(cls)

cls.LastId = ScalarDbField(cls.DbObjectName, None, R_LAST_ID)

cls.Reg = SortedSetDbField(cls.DbObjectName, None, R_REG, cls._SelfDbObjectSerializator, DateTimeSerializator)
cls._Reg_raw = SortedSetDbField(cls.DbObjectName, None, R_REG)

@classmethod
def Create(cls):
newDbObjectId = cls.LastId.increment()

dbObject = cls(newDbObjectId)
dbObject.createDate.set(datetime.now())

return dbObject

@classmethod
def Get(cls, dbObjectId):
assert dbObjectId

if not cls._Reg_raw.contains(dbObjectId): return None

return cls(dbObjectId)

def delete(self):
for value in self.__dict__.values():
if isinstance(value, DbField):
dbField = value
dbField.destroy()

def __init__(self, dbObjectId):
assert dbObjectId

self._dbObjectId = str(dbObjectId)

se

Solution

Woah, that's a lot of code.

In Python 3, you can do replace super(DbObjectType, cls) by super().

Instead of

if not cls._Reg_raw.contains(dbObjectId): return None

return cls(dbObjectId)


You could do that

if cls._Reg_raw.contains(dbObjectId):
    return cls(dbObjectId)


Because Python automatically return None if nothing is returned.
(Or return None can be changed to return but it's not important)

Instead of using old %-formatting style for strings, you should start using str.format which is preferred since 2.6, like I stated it here.

Comparing types is bad so this if type(self) != type(other): is bad.
At least you should do if not isinstance(self, type(other)): or something along these lines.

Just FYI (just in case you or someone else didn't know), property can be used as a decorator too: property in Python doc .

Also check PEP-8 for proper naming conventions (how you should name your variables, methods, etc) as raylu said in comment of your question.

I must admit I didn't really analyze how your code works or what does it do: TL;DR.
I just looked for some piece of code I know could have some minor amelioration to make it more Pythonic.

And I don't know Redis.

Code Snippets

if not cls._Reg_raw.contains(dbObjectId): return None

return cls(dbObjectId)
if cls._Reg_raw.contains(dbObjectId):
    return cls(dbObjectId)

Context

StackExchange Code Review Q#20175, answer score: 3

Revisions (0)

No revisions yet.