def convert_value(self, val): if not utils.is_str(val): return val if not val.startswith('var/'): return val var_name = val.split('var/', 1)[1] return self.variables[var_name]
def plcNeutered(self, neutered): if utils.is_str(neutered): if neutered.find("payed") != -1 or neutered.find("eutered") != -1: return "y" elif utils.is_numeric(neutered): if neutered == 1: return "y" return "n" return "u"
def validate_package_list(packages, strict=False): valid_keys = { "author", "name", "release", "short_description", "thumbnail", "title", "type" } for package in packages: assert set(package.keys()).issubset(valid_keys) assert is_str(package.get("author")) assert is_str(package.get("name")) if strict: assert is_int(package.get("release")) else: assert is_optional(int, package.get("release")) assert is_str(package.get("short_description")) assert is_optional(str, package.get("thumbnail")) assert is_str(package.get("title")) assert is_str(package.get("type"))
def sql_value(self, v): """ Given a value v, writes it as an SQL parameter value """ if v is None: return "null" elif utils.is_unicode(v) or utils.is_str(v): return "'%s'" % v.replace("'", "`") elif type(v) == datetime.datetime: return self.sql_date(v) else: return str(v)
def convert_padding(padding, kernel_size): if utils.is_str(padding): if padding == 'same': return get_same_padding(kernel_size) elif padding == 'valid': return (0, 0) else: ValueError('Unknown padding type') return tuple(padding)
def escape(self, s): """ Makes a string value safe for database queries """ if s is None: return "" if utils.is_str(s): s = MySQLdb.escape_string(s) elif utils.is_unicode(s): # Encode the string as UTF-8 for MySQL escape_string # then decode it back into unicode before continuing s = s.encode("utf-8") s = MySQLdb.escape_string(s) s = s.decode("utf-8") # This is historic - ASM2 switched backticks for apostrophes so we do for compatibility s = s.replace("'", "`") return s
def __init__(self, model, batch_processor, optimizer=None, work_dir=None, log_level=logging.INFO, logger=None): assert callable(batch_processor) self.model = model if optimizer is not None: self.optimizer = self.init_optimizer(optimizer) else: self.optimizer = None self.batch_processor = batch_processor # create work_dir if is_str(work_dir): self.work_dir = osp.abspath(work_dir) mkdir_or_exist(self.work_dir) elif work_dir is None: self.work_dir = None else: raise TypeError('"work_dir" must be a str or None') # get model name from the model class if hasattr(self.model, 'module'): self._model_name = self.model.module.__class__.__name__ else: self._model_name = self.model.__class__.__name__ self._rank, self._world_size = get_dist_info() if logger is None: self.logger = self.init_logger(work_dir, log_level) else: self.logger = logger self.log_buffer = LogBuffer() # self.tensorboardX_buffer = TensorboardXBuffer() self.mode = None self._hooks = [] self._epoch = 0 self._iter = 0 self._inner_iter = 0 self._max_epochs = 0 self._max_iters = 0
def to_tensor(data): """Convert objects of various python types to :obj:`torch.Tensor`. Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`, :class:`Sequence`, :class:`int` and :class:`float`. """ if isinstance(data, torch.Tensor): return data elif isinstance(data, np.ndarray): return torch.from_numpy(data) elif isinstance(data, Sequence) and not utils.is_str(data): return torch.tensor(data) elif isinstance(data, int): return torch.LongTensor([data]) elif isinstance(data, float): return torch.FloatTensor([data]) else: raise TypeError('type {} cannot be converted to tensor.'.format( type(data)))
def encode_str_after_read(self, v): """ Encodes any string values returned by a query result. If v is unicode, encodes it as an ascii str with HTML entities If v is already a str, removes any non-ascii chars If it is any other type, returns v untouched """ try: if v is None: return v elif utils.is_unicode(v): v = self.unescape(v) return v.encode("ascii", "xmlcharrefreplace") elif utils.is_str(v): v = self.unescape(v) return v.decode("ascii", "ignore").encode("ascii", "ignore") else: return v except Exception as err: al.error(str(err), "Database.encode_str_after_read", self, sys.exc_info()) raise err
def encode_str_before_write(self, values): """ Fix and encode/decode any string values before storing them in the database. string column names with an asterisk will not do XSS escaping. """ for k, v in values.copy().iteritems(): # Work from a copy to prevent iterator problems if utils.is_str(v) or utils.is_unicode(v): if not DB_DECODE_HTML_ENTITIES: # Store HTML entities as is v = utils.encode_html(v) # Turn any unicode chars into HTML entities else: v = utils.decode_html(v) # Turn HTML entities into unicode chars if k.find("*") != -1: # If there's an asterisk in the name, remove it so that the # value is stored again below, but without XSS escaping del values[k] k = k.replace("*", "") else: # Otherwise, do XSS escaping v = self.escape_xss(v) v = self.escape_apos(v) values[k] = u"%s" % v return values
def test_is_str__valid(self): """with a string""" result = utils.is_str('TEST') self.assertEqual(result, 'TEST') result = utils.is_str(u'TEST') self.assertEqual(result, u'TEST')