User:Wagnerrp/Bindings/Programming Guidelines
After a significant berating in #python for copious use of isinstance(), all 38 instances of said command have been removed, and a bit of restructuring has necessitated a set of guidelines for further programming.
Contents |
Database Access
All classes and functions which require access to the database must provide a 'db' keyword input. Furthermore, all uses of said functions and classes must provide a database object through said input. This remains even for classes/functions which themselves do not directly access the database. This is to allow users to specify alternate database access credentials in the place of those in config.xml, and have them remain consistent throughout.
This is simple to accomplish, as all MythDBConn object and subclasses will accept an existing object as the first input. They also use connection caching, so only one connection to the database will be set up for a single set of credentials.
class Example( object ):
def __init__(self, db=None):
db = MythDBConn(db)
db = MythDB()
ex = Example(db=db)
DictData and DBData Classes
Aside from the above db keyword, the first argument to any class should be a list or tuple containing the full set of data, of the same length as field_order. This is not mandatory, and does not have to follow if the subclass deviates significantly from the original function.
DBDataWrite
The first argument should be a database key, used to set the WHERE argument in the SQL query. This can be multiple arguments if using the '*args' style input. This argument should be capable of being omitted, in which case the object would be left in an uninitialized state, allowing '.create()' to be run. Additionally, the class should provide a 'raw' input, accepting the full set of data as list or tuple, to allow an object to be filled without having to hit the database.
create()
This should return a copy of the object to allow syntax such as:
newobj = Example().create(<dictionary data>)