Пример #1
0
 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"""
     from numpy import ndarray
     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, ndarray):
         self.write_array(val)
     elif val is None:
         self.write_none()
     elif isinstance(val, (list, tuple)):
         self.write_sequence(val)
     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)
Пример #2
0
 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"""
     from numpy import ndarray
     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, ndarray):
         self.write_array(val)
     elif val is None:
         self.write_none()
     elif isinstance(val, (list, tuple)):
         self.write_sequence(val)
     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)
Пример #3
0
 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")
Пример #4
0
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)
Пример #5
0
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
Пример #6
0
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
Пример #7
0
 def read_unicode(self):
     val = self.read_any()
     if is_unicode(val) or val is None:
         return val
     else:
         return self.read_str()
Пример #8
0
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
Пример #9
0
 def translate_dumb(x):
     if not is_unicode(x):
         return to_text_string(x, "utf-8")
     return x
Пример #10
0
 def read_unicode(self):
     val = self.read_any()
     if is_unicode(val) or val is None:
         return val
     else:
         return self.read_str()
Пример #11
0
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