Skip to content

Commit

Permalink
Merge pull request #217 from chinapnr/v1.1.7_develop
Browse files Browse the repository at this point in the history
V1.1.7 develop
  • Loading branch information
itaa committed Mar 21, 2019
2 parents fb7a38f + 3a082b4 commit 176bb38
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -42,8 +42,8 @@ fishbase 是由我们自主开发和整理的一套 Python 基础函数库。



怎么用?
========
安装
=====

.. code:: shell
Expand Down
4 changes: 2 additions & 2 deletions docs/change_log.rst
@@ -1,9 +1,9 @@
更新记录
===========================

2019.02.27 v1.1.7
2019.03.19 v1.1.7
---------------------------
* `#212 <https://github.com/chinapnr/fishbase/issues/212>`_, common, edit function :meth:`fish_common.conf_as_dict`, :meth:`fish_common.find_files`, :meth:`fish_common.yaml_conf_as_dict`, optimize
* `#215 <https://github.com/chinapnr/fishbase/issues/215>`_, common, edit function :meth:`fish_common.serialize_instance`, optimize doc and add unittest


2019.01.22 v1.1.6
Expand Down
1 change: 1 addition & 0 deletions docs/fish_common.rst
Expand Up @@ -21,6 +21,7 @@
fish_common.camelcase_to_underline
fish_common.find_same_between_dicts
fish_common.yaml_conf_as_dict
fish_common.serialize_instance

.. automodule:: fish_common
:members:
4 changes: 2 additions & 2 deletions docs/index.rst
Expand Up @@ -49,8 +49,8 @@ fishbase 是由我们自主开发和整理的一套 Python 基础函数库。



怎么用?
========
安装
=====

.. code:: shell
Expand Down
54 changes: 50 additions & 4 deletions fishbase/fish_common.py
Expand Up @@ -44,6 +44,9 @@
charChinese = 10021
charNum = 10022

# common data type
commonDataType = tuple([int, float, bool, complex, str, set, list, tuple, dict])


# v1.1.6 edit by Hu Jun
def show_deprecation_warn(old_fun, suggest_fun):
Expand Down Expand Up @@ -190,12 +193,55 @@ def __new__(cls, *args, **kwargs):
return ob


# 对象序列化
# 2015.6.14 edit by david.yi
# 2019.03.19 v1.1.7 edit by Hu Jun, edit from Jia Chunying,#215
def serialize_instance(obj):
d = {'__classname__': type(obj).__name__}
d.update(vars(obj))
return d
"""
对象序列化
:param:
* obj: (object) 对象实例
:return:
* obj_dict: (dict) 对象序列化字典
举例如下::
print('--- serialize_instance demo ---')
# 定义两个对象
class Obj(object):
def __init__(self, a, b):
self.a = a
self.b = b
class ObjB(object):
def __init__(self, x, y):
self.x = x
self.y = y
# 对象序列化
b = ObjB('string', [item for item in range(10)])
obj_ = Obj(1, b)
print(serialize_instance(obj_))
print('---')
执行结果::
--- serialize_instance demo ---
{'__classname__': 'Obj', 'a': 1,
'b': {'__classname__': 'ObjB', 'x': 'string', 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}}
---
"""
obj_dict = {'__classname__': type(obj).__name__}
obj_dict.update(obj.__dict__)
for key, value in obj_dict.items():
if not isinstance(value, commonDataType):
sub_dict = serialize_instance(value)
obj_dict.update({key: sub_dict})
else:
continue
return obj_dict


# 2018.5.26 v1.0.13 edit by David Yi,#19038
Expand Down
21 changes: 21 additions & 0 deletions test/test_common.py
Expand Up @@ -405,3 +405,24 @@ def test_fish_isalpha(self):
assert fish_isalpha(letter_str)
assert not fish_isalpha(mix_str)
assert not if_any_elements_is_letter(mix_str)

# 测试 serialize_instance() tc
def test_serialize_instance(self):
# 定义两个对象
class ObjA(object):
def __init__(self, a, b):
self.a = a
self.b = b

class ObjB(object):
def __init__(self, x, y):
self.x = x
self.y = y

obj_b = ObjB('string', [item for item in range(10)])
obj_a = ObjA(1, obj_b)
obj_attr_dict = serialize_instance(obj_a)

assert '__classname__' in obj_attr_dict
assert obj_attr_dict.get('__classname__') == 'ObjA'
assert isinstance(obj_attr_dict.get('b'), dict)

0 comments on commit 176bb38

Please sign in to comment.