Ejemplo n.º 1
0
 def initialize_keys(self, arg, split_chr='|'):
     """
     Initializes keys from string or sequence.
     From string:
          1) String is converted to list, split by 'split_chr' argument
     From list:
          2) If list item has two subitems, it will be treated as a key-value pair
             E.g: [['key', 'value'], ['key2', 'value2']] will set keys with corresponding values
          3) Otherwise, it will be treated as a list of keys
             E.g.: [['key1', 'key2', 'key3']] will instantiate keys as new Dicts
          4) If list item is not a sequence, it will be converted to a list as per Example 1
             E.g.: ['key1', 'key2', 'key3'] will act similarly to Example 3
          5) Exception: If list item is a dict, it will be handled via update.update_dict
     """
     if not is_seq_type(arg):
         arg = item_to_list(arg, split_chr=split_chr)
     for items in arg:
         if is_dict_type(items):
             self.update(items)
             continue
         if not is_seq_type(items):
             items = item_to_list(items, split_chr=split_chr)
         if len(items) is 1:
             self[items[0]]
         elif len(items) is 2:
             self[items[0]] = items[1]
         else:
             self.update_seq(items)
Ejemplo n.º 2
0
 def update_dict(d):
     """ Recursively merge d into self. """
     for k, v in d.items():
         if k in self and is_dict_type(self[k], v):
             self[k].update(v)
         else:
             self[k] = v
Ejemplo n.º 3
0
 def execute(self, sql, a=None, kw=None, auto=None, **kwargs):
     if is_dict_type(a):
         kw, a = a, kw
     if not is_seq_type(a):
         a = item_to_list(a)
     if is_dict_type(sql):
         auto = sql
         sql = ' AND '.join(["`{0}` = :{0}".format(key) for key in auto.keys()])
     if kw is None:
         kw = {}
     kwargs.update(kw)
     sql = self._create_query_(sql, **kwargs)
     if auto:
         kw = auto
     log_sql(sql, a, kw, self=self)
     self.ankdb_lastquery = sql
     if self._is_stmt_(sql):
         self.mod = True
     t = time.time()
     try:
         if a:
             # execute("...where id = ?", 5)
             res = self._db.execute(sql, a)
         elif kw:
             # execute("...where id = :id", id=5)
             res = self._db.execute(sql, kw)
         else:
             res = self._db.execute(sql)
     except (sqlite.OperationalError, sqlite.ProgrammingError, sqlite.Error, Exception) as e:
         log_sql(sql, a, kw, self=self, filter_disabled=False)
         import traceback
         log_error('Error with ankDB().execute(): %s\n Query: %s\n Trace: %s' %
                   (str(e), sql, traceback.format_exc()))
         raise
     if self.echo:
         # print a, ka
         print sql, "%0.3fms" % ((time.time() - t) * 1000)
         if self.echo == "2":
             print a, kw
     return res
Ejemplo n.º 4
0
 def __get_args_and_kwargs(self, func_args=None, func_kwargs=None, name=None, allow_cls_override=True):
     if not func_args and not func_kwargs:
         return self.args or [], self.kwargs or DictCaseInsensitive()
     func_args = func_args or allow_cls_override and self.args or []
     func_kwargs = func_kwargs or allow_cls_override and self.kwargs or DictCaseInsensitive()
     if is_seq_type(func_kwargs) and is_dict_type(func_args):
         func_args, func_kwargs = func_kwargs, func_args
     func_args = self.__args_to_list(func_args)
     if isinstance(func_kwargs, dict):
         func_kwargs = DictCaseInsensitive(func_kwargs, key=name, parent_key='kwargs')
     if not isinstance(func_args, list):
         func_args = []
     if not isinstance(func_kwargs, DictCaseInsensitive):
         func_kwargs = DictCaseInsensitive(key=name)
     return func_args, func_kwargs