def test_save_cache(self): self.fb.updates = { 'post_publics': { '3': None, newstr('4'): None, 'True': None, newstr('False'): None, }, } self.fb.put()
def ensure_new_type(obj): from future.types.newbytes import newbytes from future.types.newstr import newstr from future.types.newint import newint from future.types.newdict import newdict native_type = type(native(obj)) # Upcast only if the type is already a native (non-future) type if issubclass(native_type, type(obj)): # Upcast if native_type == str: # i.e. Py2 8-bit str return newbytes(obj) elif native_type == unicode: return newstr(obj) elif native_type == int: return newint(obj) elif native_type == long: return newint(obj) elif native_type == dict: return newdict(obj) else: return obj else: # Already a new type assert type(obj) in [newbytes, newstr] return obj
def ensure_new_type(obj): from future.types.newbytes import newbytes from future.types.newstr import newstr from future.types.newint import newint from future.types.newdict import newdict native_type = type(native(obj)) # Upcast only if the type is already a native (non-future) type if issubclass(native_type, type(obj)): # Upcast if native_type == str: # i.e. Py2 8-bit str return newbytes(obj) elif native_type == unicode: return newstr(obj) elif native_type == int: return newint(obj) elif native_type == long: return newint(obj) elif native_type == dict: return newdict(obj) else: return NotImplementedError('type %s not supported' % type(obj)) else: # Already a new type assert type(obj) in [newbytes, newstr] return obj
def test_strip_newtypes(): from future.types.newbytes import newbytes from future.types.newstr import newstr def check(x, y): assert x == y assert type(x).__name__ == type(y).__name__ check(strip_newtypes(newstr("abc")), "abc") check(strip_newtypes(newbytes(b"abc")), b"abc")
def decode(self, encoding='utf-8', errors='strict'): """ Returns a newstr (i.e. unicode subclass) Decode B using the codec registered for encoding. Default encoding is 'utf-8'. errors may be given to set a different error handling scheme. Default is 'strict' meaning that encoding errors raise a UnicodeDecodeError. Other possible values are 'ignore' and 'replace' as well as any other name registered with codecs.register_error that is able to handle UnicodeDecodeErrors. """ # Py2 str.encode() takes encoding and errors as optional parameter, # not keyword arguments as in Python 3 str. from future.types.newstr import newstr return newstr(super(newbytes, self).decode(encoding, errors))
def flush(cls, task=None): """Flush all in memory key/values to database.""" log.debug('Flushing simple persistence for task %s to db.' % task) with Session() as session: for pluginname in cls.class_store[task]: for key, value in cls.class_store[task][pluginname].items(): query = (session.query(SimpleKeyValue). filter(SimpleKeyValue.task == task). filter(SimpleKeyValue.plugin == pluginname). filter(SimpleKeyValue.key == key)) if value == DELETE: query.delete() else: updated = query.update( {'value': newstr(json.dumps(value, encode_datetime=True))}, synchronize_session=False ) if not updated: session.add(SimpleKeyValue(task, pluginname, key, value))
def flush(cls, task=None): """Flush all in memory key/values to database.""" log.debug('Flushing simple persistence for task %s to db.' % task) with Session() as session: for pluginname in cls.class_store[task]: for key, value in cls.class_store[task][pluginname].items(): query = (session.query(SimpleKeyValue).filter( SimpleKeyValue.task == task).filter( SimpleKeyValue.plugin == pluginname).filter( SimpleKeyValue.key == key)) if value == DELETE: query.delete() else: updated = query.update( { 'value': newstr(json.dumps(value, encode_datetime=True)) }, synchronize_session=False) if not updated: session.add( SimpleKeyValue(task, pluginname, key, value))
def GetUtf8String_NewStr_test(): eq_( b'fo\xc3\xb8', ycm_core.GetUtf8String( newstr( 'foø', 'utf8' ) ) )
def pretty(self, value, htchar='\t', lfchar='\n', indent=0): """ Prints pretty json :param value: :param htchar: :param lfchar: :param indent: :return: pretty json """ if self.debug: print('### type: ' + str(type(value)) + lineno() + lineno()) if type(value) == type(OrderedDict()): if self.debug: print('found it' + lineno()) print('value: ' + str(value) + lineno()) nlch = lfchar + htchar * (indent + 1) if type(value) == type(dict()) or type(value) == type(OrderedDict()): if (self.debug): print("##############################\n") print('is dict' + lineno()) print("##############################\n") items = [ nlch + repr(key) + ': ' + self.pretty(value[key], htchar, lfchar, indent + 1) for key in value ] return str( '{%s}' % (','.join(items) + lfchar + htchar * indent)).replace( '\'', '"').replace(']"', ']').replace('"[', '[').replace( '"{"', '{"').replace('"}"', '"}').replace( '"[\'', '[\'').replace('\']"', '\']') elif type(value) == type(list()): if (self.debug): print("##############################\n") print('is list' + lineno()) print("##############################\n") items = [ nlch + self.pretty(item, htchar, lfchar, indent + 1) for item in value ] if self.debug: print('items: ' + str(items) + lineno()) print('items type: ' + str(type(items)) + lineno()) if items: items = sorted(items) [str(item) for item in sorted(items)] return str( '[%s]' % (','.join(sorted(items)) + lfchar + htchar * indent)).replace( '\'', '"').replace(']"', ']').replace('"[', '[').replace( '"{"', '{"').replace('"}"', '"}').replace( '"[\'', '[\'').replace('\']"', '\']') elif type(value) is tuple: if (self.debug): print("##############################\n") print('is tuple' + lineno()) print("##############################\n") items = [ nlch + self.pretty(item, htchar, lfchar, indent + 1) for item in sorted(value) ] if self.debug: print('returning: ' + str('(%s)' % (','.join(items) + lfchar + htchar * indent)) + lineno()) return str( '(%s)' % (','.join(items) + lfchar + htchar * indent)).replace( '\'', '"').replace(']"', ']').replace('"[', '[').replace( '"{"', '{"').replace('"}"', '"}').replace( '"[\'', '[\'').replace('\']"', '\']') elif type(value) == type(newstr()): if self.debug: print('is a new string' + lineno()) print('value: ' + str(value) + lineno()) if value.startswith('['): if self.debug: print('starts with bracket' + lineno()) my_json = eval((value.replace('\'', '"'))) my_new_list = [] for my_it in sorted(my_json): my_new_list.append(str(my_it)) return str('"' + str(my_new_list) + '"').replace( ']"', ']').replace('"[', '[').replace('"{"', '{"').replace( '"}"', '"}').replace('"[\'', '[\'').replace('\']"', '\']') #value = self.pretty(my_json, htchar, lfchar, indent + 1) return repr(str(value)) else: if (self.debug): print("##############################\n") print('is other: ' + str(type(value)) + lineno()) print('class name: ' + str(value.__class__.__name__)) print('returning: ' + str(value)) print("##############################\n") return str(repr(str(value))).replace('\'', '"').replace( ']"', ']').replace('"[', '[').replace('"{"', '{"').replace( '"}"', '"}').replace('"[\'', '[\'').replace('\']"', '\']')
def pretty(value, htchar='\t', lfchar='\n', indent=0): """ Prints pretty json :param value: :param htchar: :param lfchar: :param indent: :return: pretty json """ nlch = lfchar + htchar * (indent + 1) if type(value) == type(dict()) or type(value) == type(OrderedDict()): items = [ nlch + repr(key) + ': ' + pretty(value[key], htchar, lfchar, indent + 1) for key in value ] return str( '{%s}' % (','.join(items) + lfchar + htchar * indent)).replace( '\'', '"').replace(']"', ']').replace('"[', '[').replace( '"{"', '{"').replace('"}"', '"}').replace('"[\'', '[\'').replace('\']"', '\']') elif type(value) == type(list()): items = [ nlch + pretty(item, htchar, lfchar, indent + 1) for item in value ] if items: items = sorted(items) [str(item) for item in sorted(items)] return str( '[%s]' % (','.join(sorted(items)) + lfchar + htchar * indent)).replace( '\'', '"').replace(']"', ']').replace('"[', '[').replace( '"{"', '{"').replace('"}"', '"}').replace('"[\'', '[\'').replace('\']"', '\']') elif type(value) is tuple: items = [ nlch + pretty(item, htchar, lfchar, indent + 1) for item in sorted(value) ] return str( '(%s)' % (','.join(items) + lfchar + htchar * indent)).replace( '\'', '"').replace(']"', ']').replace('"[', '[').replace( '"{"', '{"').replace('"}"', '"}').replace('"[\'', '[\'').replace('\']"', '\']') elif type(value) == type(newstr()): if value.startswith('['): my_json = eval((value.replace('\'', '"'))) my_new_list = [] for my_it in sorted(my_json): my_new_list.append(str(my_it)) return str('"' + str(my_new_list) + '"').replace( ']"', ']').replace('"[', '[').replace('"{"', '{"').replace( '"}"', '"}').replace('"[\'', '[\'').replace('\']"', '\']') return repr(str(value)) else: return str(repr(str(value))).replace('\'', '"').replace( ']"', ']').replace('"[', '[').replace('"{"', '{"').replace( '"}"', '"}').replace('"[\'', '[\'').replace('\']"', '\']')
def GetUtf8String_NewStr_test(): eq_(b'fo\xc3\xb8', ycm_core.GetUtf8String(newstr('foø', 'utf8')))
def test_press(lircd): logfile = open(lircd.logfile) # newbytes doesn't play well with parameterize here, so we use a for loop: for key in [b'KEY_OK', u'KEY_OK', newbytes(b'KEY_OK'), newstr(u'KEY_OK')]: print("key = %r (%s)" % (key, type(key))) # pylint: disable=superfluous-parens control = uri_to_control("lirc:%s:Apple_TV" % lircd.socket) control.press(key) lircd_output = logfile.read() expected = dedent("""\ pulse 9000 space 4500 pulse 527 space 527 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 527 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 527 pulse 527 space 527 pulse 527 space 527 pulse 527 space 527 pulse 527 space 1703 pulse 527 space 527 pulse 527 space 527 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 527 pulse 527 space 1703 pulse 527 space 527 pulse 527 space 527 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 527 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 1703 pulse 527 space 527 pulse 527 space 38000 """) assert expected == lircd_output