def translate_gettext(x): if not PY3 and is_unicode(x): x = x.encode("utf-8") y = lgettext(x) if is_text_string(y) and PY3: return y else: return to_text_string(y, "utf-8")
def add_image_path(path, subfolders=True): """Append image path (opt. with its subfolders) to global list IMG_PATH""" if not is_unicode(path): path = decode_fs_string(path) global IMG_PATH IMG_PATH.append(path) if subfolders: for fileobj in os.listdir(path): pth = osp.join(path, fileobj) if osp.isdir(pth): IMG_PATH.append(pth)
def utf8_to_unicode(string): """Convert UTF-8 string to Unicode""" if not is_text_string(string): string = to_text_string(string) if not is_unicode(string): try: string = to_text_string(string, "utf-8") except UnicodeDecodeError: # This is border line... but we admit here string which has been # erroneously encoded in file system charset instead of UTF-8 string = decode_fs_string(string) return string
def write(self, val, group_name=None): """Write value using the appropriate routine depending on value type group_name: if None, writing the value in current group""" import numpy as np if group_name: self.begin(group_name) if isinstance(val, bool): self.write_bool(val) elif isinstance(val, int): self.write_int(val) elif isinstance(val, float): self.write_float(val) elif is_unicode(val): self.write_unicode(val) elif isinstance(val, str): self.write_any(val) elif isinstance(val, np.ndarray): self.write_array(val) elif np.isscalar(val): self.write_any(val) elif val is None: self.write_none() elif isinstance(val, (list, tuple)): self.write_sequence(val) elif isinstance(val, datetime.datetime): self.write_float(val.timestamp()) elif isinstance(val, datetime.date): self.write_int(val.toordinal()) elif hasattr(val, 'serialize') and isinstance(val.serialize, collections.Callable): # The object has a DataSet-like `serialize` method val.serialize(self) else: raise NotImplementedError("cannot serialize %r of type %r" % (val, type(val))) if group_name: self.end(group_name)
def encode_to_utf8(x): """Encode unicode string in UTF-8 -- but only with Python 2""" if PY2 and is_unicode(x): return x.encode("utf-8") else: return x
def read_unicode(self): val = self.read_any() if is_unicode(val) or val is None: return val else: return self.read_str()
def translate_dumb(x): if not is_unicode(x): return to_text_string(x, "utf-8") return x