class Device(GenericDevice): """ This is the base Device class that supports instantiation from a dictionarry. the __init__ takes the dictionary as its argument, together with a list of valid fields to recognize and with a mapping for dictionary keys into valid field names for self.data The fields are required to know what fields we have in the table. The mapping allows transformation from whatever comes in to valid fields in the table Looks complicated but it isn't -- gafton """ def __init__(self, fields, dict=None, mapping=None): GenericDevice.__init__(self) x = {} for k in fields: x[k] = None self.data = UserDictCase(x) if not dict: return # make sure we get a UserDictCase to work with if type(dict) == type({}): dict = UserDictCase(dict) if mapping is None or type(mapping) == type({}): mapping = UserDictCase(mapping) if not isinstance(dict, UserDictCase) or not isinstance(mapping, UserDictCase): log_error("Argument passed is not a dictionary", dict, mapping) raise TypeError("Argument passed is not a dictionary", dict, mapping) # make sure we have a platform for k in dict.keys(): if dict[k] == "": dict[k] = None if self.data.has_key(k): self.data[k] = dict[k] continue if mapping.has_key(k): # the mapping dict might tell us to lose some fields if mapping[k] is not None: self.data[mapping[k]] = dict[k] else: log_error("Unknown HW key =`%s'" % k, dict.dict(), mapping.dict()) # The try-except is added just so that we can send e-mails try: raise KeyError("Don't know how to parse key `%s''" % k, dict.dict()) except: Traceback(mail=1) # Ignore this key continue # clean up this data try: for k in self.data.keys(): if type(self.data[k]) == type("") and len(self.data[k]): self.data[k] = string.strip(self.data[k]) if not len(self.data[k]): continue if self.data[k][0] == '"' and self.data[k][-1] == '"': self.data[k] = self.data[k][1:-1] except IndexError: raise_with_tb(IndexError("Can not process data = %s, key = %s" % (repr(self.data), k)), sys.exc_info()[2])
def __init__(self, fields, dict=None, mapping=None): GenericDevice.__init__(self) x = {} for k in fields: x[k] = None self.data = UserDictCase(x) if not dict: return # make sure we get a UserDictCase to work with if type(dict) == type({}): dict = UserDictCase(dict) if mapping is None or type(mapping) == type({}): mapping = UserDictCase(mapping) if not isinstance(dict, UserDictCase) or \ not isinstance(mapping, UserDictCase): log_error("Argument passed is not a dictionary", dict, mapping) raise TypeError("Argument passed is not a dictionary", dict, mapping) # make sure we have a platform for k in list(dict.keys()): if dict[k] == '': dict[k] = None if self.data.has_key(k): self.data[k] = dict[k] continue if mapping.has_key(k): # the mapping dict might tell us to lose some fields if mapping[k] is not None: self.data[mapping[k]] = dict[k] else: log_error("Unknown HW key =`%s'" % k, dict.dict(), mapping.dict()) # The try-except is added just so that we can send e-mails try: raise KeyError("Don't know how to parse key `%s''" % k, dict.dict()) except: Traceback(mail=1) # Ignore this key continue # clean up this data try: for k in list(self.data.keys()): if type(self.data[k]) == type("") and len(self.data[k]): self.data[k] = self.data[k].strip() if not len(self.data[k]): continue if self.data[k][0] == '"' and self.data[k][-1] == '"': self.data[k] = self.data[k][1:-1] except IndexError: raise_with_tb( IndexError("Can not process data = %s, key = %s" % (repr(self.data), k)), sys.exc_info()[2])
def __init__(self, fields, dict=None, mapping=None): GenericDevice.__init__(self) x = {} for k in fields: x[k] = None self.data = UserDictCase(x) if not dict: return # make sure we get a UserDictCase to work with if type(dict) == type({}): dict = UserDictCase(dict) if mapping is None or type(mapping) == type({}): mapping = UserDictCase(mapping) if not isinstance(dict, UserDictCase) or \ not isinstance(mapping, UserDictCase): log_error("Argument passed is not a dictionary", dict, mapping) raise TypeError("Argument passed is not a dictionary", dict, mapping) # make sure we have a platform for k in dict.keys(): if dict[k] == '': dict[k] = None if self.data.has_key(k): self.data[k] = dict[k] continue if mapping.has_key(k): # the mapping dict might tell us to lose some fields if mapping[k] is not None: self.data[mapping[k]] = dict[k] else: log_error("Unknown HW key =`%s'" % k, dict.dict(), mapping.dict()) # The try-except is added just so that we can send e-mails try: raise KeyError("Don't know how to parse key `%s''" % k, dict.dict()) except: Traceback(mail=1) # Ignore this key continue # clean up this data try: for k in self.data.keys(): if type(self.data[k]) == type("") and len(self.data[k]): self.data[k] = string.strip(self.data[k]) if not len(self.data[k]): continue if self.data[k][0] == '"' and self.data[k][-1] == '"': self.data[k] = self.data[k][1:-1] except IndexError: raise IndexError, "Can not process data = %s, key = %s" % ( repr(self.data), k), sys.exc_info()[2]
def _executemany(self, *args, **kwargs): if not kwargs: return 0 params = UserDictCase(kwargs) # First break all the incoming keyword arg lists into individual # hashes: all_kwargs = [] for key in list(params.keys()): if len(all_kwargs) < len(params[key]): for i in range(len(params[key])): all_kwargs.append({}) i = 0 for val in params[key]: all_kwargs[i][key] = val i = i + 1 psycopg2.extras.execute_batch(self._real_cursor, self.sql, all_kwargs) self.description = self._real_cursor.description
def _executemany(self, *args, **kwargs): if not kwargs: return 0 params = UserDictCase(kwargs) # First break all the incoming keyword arg lists into individual # hashes: all_kwargs = [] for key in params.keys(): if len(all_kwargs) < len(params[key]): for i in range(len(params[key])): all_kwargs.append({}) i = 0 for val in params[key]: all_kwargs[i][key] = val i = i + 1 self._real_cursor.executemany(self.sql, all_kwargs) self.description = self._real_cursor.description rowcount = self._real_cursor.rowcount return rowcount
class Device(GenericDevice): """ This is the base Device class that supports instantiation from a dictionarry. the __init__ takes the dictionary as its argument, together with a list of valid fields to recognize and with a mapping for dictionary keys into valid field names for self.data The fields are required to know what fields we have in the table. The mapping allows transformation from whatever comes in to valid fields in the table Looks complicated but it isn't -- gafton """ def __init__(self, fields, dict=None, mapping=None): GenericDevice.__init__(self) x = {} for k in fields: x[k] = None self.data = UserDictCase(x) if not dict: return # make sure we get a UserDictCase to work with if type(dict) == type({}): dict = UserDictCase(dict) if mapping is None or type(mapping) == type({}): mapping = UserDictCase(mapping) if not isinstance(dict, UserDictCase) or \ not isinstance(mapping, UserDictCase): log_error("Argument passed is not a dictionary", dict, mapping) raise TypeError("Argument passed is not a dictionary", dict, mapping) # make sure we have a platform for k in list(dict.keys()): if dict[k] == '': dict[k] = None if self.data.has_key(k): self.data[k] = dict[k] continue if mapping.has_key(k): # the mapping dict might tell us to lose some fields if mapping[k] is not None: self.data[mapping[k]] = dict[k] else: log_error("Unknown HW key =`%s'" % k, dict.dict(), mapping.dict()) # The try-except is added just so that we can send e-mails try: raise KeyError("Don't know how to parse key `%s''" % k, dict.dict()) except: Traceback(mail=1) # Ignore this key continue # clean up this data try: for k in list(self.data.keys()): if type(self.data[k]) == type("") and len(self.data[k]): self.data[k] = self.data[k].strip() if not len(self.data[k]): continue if self.data[k][0] == '"' and self.data[k][-1] == '"': self.data[k] = self.data[k][1:-1] except IndexError: raise_with_tb( IndexError("Can not process data = %s, key = %s" % (repr(self.data), k)), sys.exc_info()[2])