Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class _caller:
- def __init__(self, sql, name):
- self.sql = sql
- self.name = name
- def __call__(self, **kwargs):
- if self.name in self.sql:
- try:
- print self.sql[self.name] % kwargs
- except KeyError, e:
- raise Exception("Missing argument %s" % e)
- else:
- raise Exception("Invalid method '%s'" % self.name)
- class test:
- def __init__(self, sql, **kwargs):
- self.sql = sql
- self.kwargs = kwargs
- def __getattr__(self, name):
- return _caller(self.sql, name, **self.kwargs)
- if __name__ == "__main__":
- _SQLS = {
- "add": "INSERT INTO `tabulka` (name, age, email) VALUES ('%(name)s', '%(age)s', '%(email)s');",
- "edit": "UPDATE `tabulka` SET age = '%(age)s', email = '%(email)s' WHERE name = '%(name)s';",
- "del": "REMOVE FROM `tabulka` WHERE name = '%(name)s';",
- }
- t = test(_SQLS)
- t.add(name="John", age="30", email="john@example.com")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement