Skip to content

Commit

Permalink
Merge c561f88 into 176bb38
Browse files Browse the repository at this point in the history
  • Loading branch information
mindjun committed Apr 2, 2019
2 parents 176bb38 + c561f88 commit 9448c2c
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 16 deletions.
6 changes: 6 additions & 0 deletions docs/change_log.rst
@@ -1,5 +1,11 @@
更新记录
===========================
2019.04.01 v1.1.8
---------------------------
* `#218 <https://github.com/chinapnr/fishbase/issues/218>`_, file, edit function :meth:`fish_file.get_abs_filename_with_sub_path`, :meth:`fish_file.check_sub_path_create`, optimize
* `#215 <https://github.com/chinapnr/fishbase/issues/215>`_, common, add function :meth:`fish_common.DeserializeInstance, doc and unittest;
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
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Expand Up @@ -58,9 +58,9 @@
# built documents.
#
# The short X.Y version.
version = '1.1.7'
version = '1.1.8'
# The full version, including alpha/beta/rc tags.
release = '1.1.7'
release = '1.1.8'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion docs/fish_common.rst
Expand Up @@ -12,7 +12,6 @@
fish_common.has_special_char
fish_common.find_files
fish_common.get_random_str
fish_common.if_any_elements_is_space
fish_common.get_distinct_elements
fish_common.sort_objs_by_attr
fish_common.get_query_param_from_url
Expand All @@ -22,6 +21,7 @@
fish_common.find_same_between_dicts
fish_common.yaml_conf_as_dict
fish_common.serialize_instance
fish_common.DeserializeInstance

.. automodule:: fish_common
:members:
2 changes: 1 addition & 1 deletion docs/fish_data.rst
@@ -1,5 +1,5 @@
``fish_data`` 数据信息处理函数包,含银行卡、身份证等
===================================================
=====================================================

.. autosummary::
fish_data.CardBin.get_checkcode
Expand Down
2 changes: 1 addition & 1 deletion docs/fish_date.rst
@@ -1,5 +1,5 @@
``fish_date`` 日期处理增强函数包
===============================
=================================

.. autosummary::
fish_date.get_date_range
Expand Down
2 changes: 1 addition & 1 deletion fishbase/__init__.py
Expand Up @@ -27,4 +27,4 @@
from .fish_project import *
from .fish_random import *

__version__ = '1.1.7' # type: str
__version__ = '1.1.8' # type: str
38 changes: 38 additions & 0 deletions fishbase/fish_common.py
Expand Up @@ -244,6 +244,44 @@ def __init__(self, x, y):
return obj_dict


# 2019.03.28 v1.1.8 edit by Hu Jun, edit from Jia Chunying,#215
class DeserializeInstance(object):
"""
字典对象反序列化
:param:
* obj_dict: (dict) 对象序列化字典
:return:
* obj: (object) 对象
举例如下::
print('--- DeserializeInstance demo ---')
temp_dict = {'user': {'name': {'last_name': 'zhang', 'first_name': 'san'}, 'address': 'Beijing'}}
new_obj = DeserializeInstance(temp_dict)
print('last_name is: ', new_obj.user.name.last_name)
print('first_name is: ', new_obj.user.name.first_name)
print('address is: ', new_obj.user.address)
print('---')
执行结果::
--- DeserializeInstance demo ---
last_name is: zhang
first_name is: san
address is: Beijing
---
"""
def __init__(self, obj_dict):
for key, value in obj_dict.items():
if isinstance(value, dict):
setattr(self, key, DeserializeInstance(value) if isinstance(value, dict) else value)
else:
setattr(self, key, value)


# 2018.5.26 v1.0.13 edit by David Yi,#19038
def get_uuid(kind):
"""
Expand Down
23 changes: 13 additions & 10 deletions fishbase/fish_file.py
Expand Up @@ -9,7 +9,7 @@

# 2017.1.8 v1.0.9 created

import os
import pathlib


# 生成当前路径下一级路径某文件的完整文件名
Expand All @@ -19,6 +19,7 @@
# 2018.1.30 1.31 v1.0.10 代码优化, #11004
# 2018.4.24 v1.0.11 加入 docstring
# 2018.5.28 v1.0.13 edit, #19040
# 2019.4.1 v1.1.8 edit by Hu Jun, #218
def get_abs_filename_with_sub_path(sub_path, filename):

"""
Expand Down Expand Up @@ -61,12 +62,12 @@ def get_abs_filename_with_sub_path(sub_path, filename):
"""

try:
cur_path = os.getcwd()
abs_filename = os.path.join(cur_path, sub_path, filename)
cur_path = pathlib.Path.cwd()
abs_filename = cur_path / pathlib.Path(sub_path) / filename
flag = pathlib.Path.is_file(abs_filename)

flag = os.path.isfile(abs_filename)

return flag, abs_filename
# 将 path 对象转换成字符串
return flag, str(abs_filename)

except:

Expand All @@ -89,6 +90,7 @@ def get_abs_filename_with_sub_path(sub_path, filename):
# 检查当前路径下的某个子路径是否存在, 不存在则创建
# 2016.10.4 v1.0.9 #19001, edit by David Yi
# 2018.5.28 v1.0.13 #19042, edit by David Yi
# 2019.4.1 v1.1.8 edit by Hu Jun, #218
def check_sub_path_create(sub_path):

"""
Expand Down Expand Up @@ -122,17 +124,18 @@ def check_sub_path_create(sub_path):
"""

# 获得当前路径
cur_path = os.path.abspath('')
temp_path = pathlib.Path()
cur_path = temp_path.resolve()

# 生成 带有 sub_path_name 的路径
path = os.path.join(cur_path, sub_path)
path = cur_path / pathlib.Path(sub_path)

# 判断是否存在带有 sub_path 路径
if os.path.exists(path):
if path.exists():
# 返回 True: 路径存在, False: 不需要创建
return True, False
else:
os.makedirs(path)
path.mkdir(parents=True)
# 返回 False: 路径不存在 True: 路径已经创建
return False, True

Expand Down
8 changes: 8 additions & 0 deletions test/test_common.py
Expand Up @@ -426,3 +426,11 @@ def __init__(self, x, y):
assert '__classname__' in obj_attr_dict
assert obj_attr_dict.get('__classname__') == 'ObjA'
assert isinstance(obj_attr_dict.get('b'), dict)

def test_deserialize_instance(self):
temp_dict = {'user': {'name': {'last_name': 'zhang', 'first_name': 'san'}, 'address': 'Beijing'}}
new_obj = DeserializeInstance(temp_dict)
assert hasattr(new_obj, 'user')
assert hasattr(new_obj.user, 'name')
assert new_obj.user.name.last_name == 'zhang'
assert new_obj.user.address == 'Beijing'
1 change: 1 addition & 0 deletions test/test_file.py
Expand Up @@ -2,6 +2,7 @@
# fish_file.py 单元测试
# 2018.5.28 create by David Yi

import os
from fishbase.fish_file import *

# 定义当前路径
Expand Down

0 comments on commit 9448c2c

Please sign in to comment.