Advertisement
Guest User

test

a guest
Apr 25th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. class _caller:
  2.     def __init__(self, sql, name):
  3.         self.sql = sql
  4.         self.name = name
  5.  
  6.     def __call__(self, **kwargs):
  7.         if self.name in self.sql:
  8.             try:
  9.                 print self.sql[self.name] % kwargs
  10.             except KeyError, e:
  11.                 raise Exception("Missing argument %s" % e)
  12.         else:
  13.             raise Exception("Invalid method '%s'" % self.name)
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. class test:
  21.     def __init__(self, sql, **kwargs):
  22.         self.sql = sql
  23.         self.kwargs = kwargs
  24.  
  25.     def __getattr__(self, name):
  26.         return _caller(self.sql, name, **self.kwargs)
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. if __name__ == "__main__":
  34.     _SQLS = {
  35.         "add": "INSERT INTO `tabulka` (name, age, email) VALUES ('%(name)s', '%(age)s', '%(email)s');",
  36.         "edit": "UPDATE `tabulka` SET age = '%(age)s', email = '%(email)s' WHERE name = '%(name)s';",
  37.         "del": "REMOVE FROM `tabulka` WHERE name = '%(name)s';",
  38.     }
  39.  
  40.     t = test(_SQLS)
  41.     t.add(name="John", age="30", email="john@example.com")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement