-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature-refactor' into develop
- Loading branch information
Showing
8 changed files
with
326 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
History | ||
====== | ||
0.0.6 | ||
|
||
0.0.5 | ||
-------------- | ||
1. refactor objson module | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__author__ = 'benjamin.c.yan' | ||
__version__ = "0.0.5" | ||
__version__ = "0.0.6" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import json | ||
import functools | ||
import re | ||
from keyword import iskeyword | ||
|
||
__author__ = 'benjamin.c.yan' | ||
|
||
_re_encode = re.compile('[^a-zA-Z0-9]', re.MULTILINE) | ||
|
||
|
||
def object2dict(obj): | ||
d = {} | ||
for key in obj: | ||
d[key] = obj[key] | ||
return d | ||
|
||
|
||
def object_hook(obj): | ||
return Dolphin(obj) | ||
|
||
|
||
class Dolphin(object): | ||
def __init__(self, other=None): | ||
if isinstance(other, (dict, Dolphin)): | ||
for key in other: | ||
self[key] = other[key] | ||
|
||
def __iter__(self): | ||
return iter(self.__dict__) | ||
|
||
def __getitem__(self, key): | ||
if not key.startswith('_'): | ||
return self.__dict__.get(key) | ||
|
||
def __setitem__(self, key, value): | ||
if not key.startswith('_'): | ||
self.__dict__[key] = value | ||
|
||
def __getattr__(self, name): | ||
if not name.startswith('_'): | ||
if name in self: | ||
return self[name] | ||
elif name.startswith('m') and (iskeyword(name[1:]) or len(name) > 1 and name[1].isdigit()): | ||
return self[name[1:]] | ||
elif '_' in name: | ||
return self[_re_encode.sub('-', name)] | ||
|
||
|
||
def __str__(self): | ||
return '%s (%s)' % (self.__class__.__name__, repr(self)) | ||
|
||
|
||
def __repr__(self): | ||
keys = sorted(self.__dict__.keys()) | ||
text = ', '.join('%s=%r' % (key, self[key]) for key in keys) | ||
return '{%s}' % text | ||
|
||
|
||
def dumps(obj, *args, **kwargs): | ||
"""Serialize a object to string | ||
Basic Usage: | ||
>>> import simplekit.objson | ||
>>> obj = {'name':'wendy'} | ||
>>> print simplekit.objson.dumps(obj) | ||
:param obj: a object which need to dump | ||
:param args: Optional arguments that :func:`json.dumps` takes. | ||
:param kwargs: Keys arguments that :py:func:`json.dumps` takes. | ||
:return: string | ||
""" | ||
kwargs['default'] = object2dict | ||
|
||
return json.dumps(obj, *args, **kwargs) | ||
|
||
|
||
def dump(obj, fp, *args, **kwargs): | ||
"""Serialize a object to a file object. | ||
Basic Usage: | ||
>>> import simplekit.objson | ||
>>> from cStringIO import StringIO | ||
>>> obj = {'name': 'wendy'} | ||
>>> io = StringIO() | ||
>>> simplekit.objson.dump(obj, io) | ||
>>> print io.getvalue() | ||
:param obj: a object which need to dump | ||
:param fp: a instance of file object | ||
:param args: Optional arguments that :func:`json.dump` takes. | ||
:param kwargs: Keys arguments that :func:`json.dump` takes. | ||
:return: None | ||
""" | ||
kwargs['default'] = object2dict | ||
|
||
json.dump(obj, fp, *args, **kwargs) | ||
|
||
|
||
|
||
def _load(fn): | ||
@functools.wraps(fn) | ||
def tmp(src, *args, **kwargs): | ||
"""Deserialize json string to a object | ||
Provide a brief way to represent a object, Can use ``.`` operate access | ||
Json object property | ||
Basic Usage: | ||
>>> from simplekit import objson | ||
>>> text = r'{"Name":"wendy"}' | ||
>>> obj = objson.loads(text) | ||
>>> assert obj.Name == 'wendy' | ||
:param src: string or file object | ||
:param args: Optional arguments that :func:`json.load` takes. | ||
:param kwargs: Keys arguments that :func:`json.loads` takes. | ||
:return: :class:`object` or :class:`list` | ||
""" | ||
try: | ||
kwargs['object_hook'] = object_hook | ||
return fn(src, *args, **kwargs) | ||
except ValueError: | ||
return None | ||
|
||
return tmp | ||
|
||
|
||
load = _load(json.load) | ||
loads = _load(json.loads) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import time | ||
import json | ||
from simplekit import objson | ||
|
||
__author__ = 'benjamin.c.yan' | ||
|
||
|
||
def benchmark(fn): | ||
def mark(*args, **kwargs): | ||
now = time.time() | ||
result = fn(*args, **kwargs) | ||
print 'time, ', time.time() - now | ||
return result | ||
|
||
return mark | ||
|
||
|
||
@benchmark | ||
def benchmark_objson(text, times=100000): | ||
for _ in xrange(times): | ||
objson.loads2(text) | ||
|
||
|
||
@benchmark | ||
def benchmark_json(text, times=100000): | ||
for _ in xrange(times): | ||
json.loads(text) | ||
|
||
|
||
@benchmark | ||
def benchmark_objson_get(text, times=1000000): | ||
obj = objson.loads2(text) | ||
for _ in xrange(times): | ||
x = obj.name | ||
|
||
|
||
@benchmark | ||
def benchmark_json_get(text, times=1000000): | ||
obj = json.loads(text) | ||
for _ in xrange(times): | ||
x = obj['name'] | ||
|
||
|
||
@benchmark | ||
def benchmark_objson_set(text, times=1000000): | ||
obj = objson.loads2(text) | ||
for _ in xrange(times): | ||
obj.name = 'benjamin2' | ||
|
||
|
||
@benchmark | ||
def benchmark_json_set(text, times=1000000): | ||
obj = json.loads(text) | ||
for _ in xrange(times): | ||
obj['name'] = 'benjamin2' | ||
|
||
|
||
@benchmark | ||
def benchmark_objson_set(text, times=1000000): | ||
obj = objson.loads2(text) | ||
for _ in xrange(times): | ||
obj.name = 'benjamin2' | ||
|
||
|
||
@benchmark | ||
def benchmark_json_set(text, times=1000000): | ||
obj = json.loads(text) | ||
for _ in xrange(times): | ||
obj['name'] = 'benjamin2' | ||
|
||
|
||
@benchmark | ||
def benchmark_objson_dumps(text, times=100000): | ||
obj = objson.loads2(text) | ||
for _ in xrange(times): | ||
objson.dumps2(obj) | ||
|
||
|
||
@benchmark | ||
def benchmark_json_dumps(text, times=100000): | ||
obj = json.loads(text) | ||
for _ in xrange(times): | ||
json.dumps(obj) | ||
|
||
|
||
if __name__ == '__main__': | ||
text = '{"name": "benjamin.c.yan", "name2": "benjamin.c.yan",' \ | ||
'"name3": "benjamin.c.yan", "name4": "benjamin.c.yan", ' \ | ||
'"name5": "benjamin.c.yan", "name6": "benjamin.c.yan", ' \ | ||
'"name7": "benjamin.c.yan", "name8": "benjamin.c.yan", ' \ | ||
'"name9": "benjamin.c.yan", "name0": "benjamin.c.yan"}' | ||
benchmark_objson(text) | ||
benchmark_json(text) | ||
benchmark_objson_get(text) | ||
benchmark_json_get(text) | ||
benchmark_objson_set(text) | ||
benchmark_json_set(text) | ||
benchmark_objson_dumps(text) | ||
benchmark_json_dumps(text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters