def test_obj_with_datetime_without_milliseconds(self): """test date encoding decoding when part of a list like object""" d = [ datetime.datetime(2020, 1, 1, 2, 2, 2), dict(status='this is a test') ] dj = dumps(d) d2 = loads(dj) self.assertListEqual(d, d2)
def get(self, *a, **k): """get a response from the remote site""" values = urllib.urlencode(k) url = '/'.join([self.site_url]+list(a))+'?'+values response = self.opener.open(url).read() try: result = loads(response) except Exception: print 'JSON Conversion Failed: response was %s' % repr(response) result = response return result
def get(self, *a, **k): """get a response from the remote site""" values = urllib.urlencode(k) url = '/'.join([self.site_url] + list(a)) + '?' + values response = self.opener.open(url).read() try: result = loads(response) except Exception: print 'JSON Conversion Failed: response was %s' % repr(response) result = response return result
def post(self, *a, **data): """post data to the remote site""" url = '/'.join([self.site_url]+list(a)) encoded = urllib.urlencode(data) response = self.opener.open(url, encoded).read() if response.lower() in ['ok', 'error']: result = dict(status=response.lower()) else: try: result = loads(response) except Exception: msg = 'JSON Conversion Failed: response was %s' print msg % repr(response) result = response return result
def post(self, *a, **data): """post data to the remote site""" url = '/'.join([self.site_url] + list(a)) encoded = urllib.urlencode(data) response = self.opener.open(url, encoded).read() if response.lower() in ['ok', 'error']: result = dict(status=response.lower()) else: try: result = loads(response) except Exception: msg = 'JSON Conversion Failed: response was %s' print msg % repr(response) result = response return result
def _peek(self, newest=None): def decoded(value): if type(value) is bytes: return value.decode('utf8') return value top_one = newest is not None and newest or self.newest or 0 db = self.db if self.last() > self.newest: if self.name: cmd = """ select min(row_id) as row_id from attributes where kind=%s and attribute="topic" and value=%s and row_id > %s """ rec = db(cmd, self.messages.kind, self.name, top_one) else: cmd = """ select min(row_id) as row_id from attributes where kind=%s and row_id > %s """ rec = db(cmd, self.messages.kind, top_one) if type(rec) == int: row_id = 0 else: row_id = rec.first()[0] if row_id: message = self.messages.get(row_id) if message and message.body is not None and message.topic is not None: # Checking for message body because finding an id does not guarantee # that a message is ready to go depending on isolation level of # database. Rather than require a specific isolation level we # just check to see if the message body and topic are present and if # not, we ignore the message. return row_id, decoded(message.topic), json.loads( decoded(message.body)) raise EmptyException
def _peek(self, newest=None): def decoded(value): if type(value) is bytes: return value.decode('utf8') return value top_one = newest is not None and newest or self.newest or 0 db = self.db if self.last() > self.newest: if self.name: cmd = """ select min(row_id) as row_id from attributes where kind=%s and attribute="topic" and value=%s and row_id > %s """ rec = db(cmd, self.messages.kind, self.name, top_one) else: cmd = """ select min(row_id) as row_id from attributes where kind=%s and row_id > %s """ rec = db(cmd, self.messages.kind, top_one) if type(rec) == int: row_id = 0 else: row_id = rec.first()[0] if row_id: message = self.messages.get(row_id) if message: return row_id, decoded(message.topic), json.loads( decoded(message.body)) raise EmptyException
def test_float(self): d = [22.32, '22.32'] dj = dumps(d) d2 = loads(dj) self.assertEqual(d, d2)
def test_decimal(self): d = [Decimal('22.32'), Decimal(22.32)] dj = dumps(d) d2 = loads(dj) self.assertEqual(d, d2)
def test_float(self): d = [22.32, u"22.32"] dj = dumps(d) d2 = loads(dj) self.assertEqual(d, d2)
def test_date(self): d = datetime.date.today() dj = dumps(d) d2 = loads(dj) self.assertEqual(d,d2)
def test_string(self): t = 'This is a test' tj = dumps(t) t2 = loads(tj) self.assertEqual(t,t2)
def test_bytes(self): d = [b'123', Decimal(22.32)] dj = dumps(d) d2 = loads(dj) self.assertEqual(d, d2)
def test_obj(self): """test object literal decode - pass/fallthrough of object_hook of loads""" d = [Decimal('22.32'), dict(status='this is a test')] dj = dumps(d) d2 = loads(dj) self.assertListEqual(d, d2)
def test_date(self): d = datetime.date.today() dj = dumps(d) d2 = loads(dj) self.assertEqual(d, d2)
def test_string(self): t = 'This is a test' tj = dumps(t) t2 = loads(tj) self.assertEqual(t, t2)