-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
108 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
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,16 +1,14 @@ | ||
# -*- coding: utf-8 -*- | ||
# Intro: 配置模块 | ||
# Intro: 配置注册模块 | ||
# Author: Ztj | ||
# Email: [email protected] | ||
# Version: 0.0.4 | ||
# Date: 2020-09-08 | ||
# Version: 1.0.0 | ||
# Date: 2021-06-02 | ||
|
||
import time | ||
|
||
__version__ = '0.0.4' | ||
|
||
|
||
class Registry(object): | ||
class ConfigRegistry(object): | ||
""" 配置模块""" | ||
|
||
def __init__(self, options=None): | ||
|
@@ -125,7 +123,7 @@ def load(self, options): | |
else: | ||
return False | ||
|
||
def set_hook(self, key, overtime, callback): | ||
def setting_hook(self, key, overtime, callback): | ||
"""设置钩子""" | ||
self.hooks[key] = dict() | ||
self.hooks[key]['last_time'] = int(time.time()) | ||
|
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,86 +1,86 @@ | ||
# Python Registry Package | ||
# Python Config Registry Package | ||
|
||
### 说明 | ||
这是一个 Python 配置快速调用模块,主要解决 Json or Yaml 深层次配置调用问题。 | ||
这是一个 Python 配置快速注册调用模块,主要解决多维数组深层次配置调用问题。 | ||
|
||
### 链接 | ||
- [GitHub](https://github.com/ztj-package/py-registry) | ||
- [PyPI](https://pypi.org/project/py-ztj-registry) | ||
- [GitHub](https://github.com/ztj1993/py-config-registry) | ||
- [PyPI](https://pypi.org/project/config-registry) | ||
|
||
### 安装 | ||
``` | ||
pip install py-ztj-registry | ||
pip install config-registry | ||
``` | ||
|
||
### 设置获取数据 | ||
``` | ||
from registry import Registry | ||
from ConfigRegistry import ConfigRegistry | ||
registry = Registry() | ||
setting = Registry() | ||
registry.set('a', 'a') | ||
registry.set('b', {'bb': 'bbb'}) | ||
registry.set('c.h', 'h') | ||
setting.set('a', 'a') | ||
setting.set('b', {'bb': 'bbb'}) | ||
setting.set('c.h', 'h') | ||
print(registry.get()) | ||
print(registry.get('b.bb')) | ||
print(setting.get()) | ||
print(setting.get('b.bb')) | ||
``` | ||
|
||
### 加载字典 | ||
``` | ||
from registry import Registry | ||
from ConfigRegistry import ConfigRegistry | ||
registry = Registry() | ||
setting = ConfigRegistry() | ||
registry.load({'a': {'aa': 'aaa'}}) | ||
print(registry.get('a.aa')) | ||
setting.load({'a': {'aa': 'aaa'}}) | ||
print(setting.get('a.aa')) | ||
``` | ||
|
||
### 合并字典 | ||
``` | ||
from registry import Registry | ||
from ConfigRegistry import ConfigRegistry | ||
registry = Registry() | ||
setting = ConfigRegistry() | ||
registry.load({'a': {'a1': 'aaa1'}}) | ||
registry.merge('a', {'a2': 'aaa2' }) | ||
print(registry.get('a')) | ||
setting.load({'a': {'a1': 'aaa1'}}) | ||
setting.merge('a', {'a2': 'aaa2' }) | ||
print(setting.get('a')) | ||
``` | ||
|
||
### 设置默认值 | ||
``` | ||
from registry import Registry | ||
from ConfigRegistry import ConfigRegistry | ||
registry = Registry() | ||
setting = ConfigRegistry() | ||
registry.set('a', 'aaa') | ||
registry.default('a', 'bbb') | ||
registry.default('c', 'ccc') | ||
print(registry.get('a')) | ||
print(registry.get('c')) | ||
setting.set('a', 'aaa') | ||
setting.default('a', 'bbb') | ||
setting.default('c', 'ccc') | ||
print(setting.get('a')) | ||
print(setting.get('c')) | ||
``` | ||
|
||
### 钩子调用 | ||
``` | ||
import time | ||
from registry import Registry | ||
from ConfigRegistry import ConfigRegistry | ||
registry = Registry() | ||
setting = ConfigRegistry() | ||
def callback(): | ||
print('callback') | ||
registry.set_hook('hook', 3, callback) | ||
setting.set_hook('hook', 3, callback) | ||
time.sleep(1) | ||
registry.refresh_hook('hook') | ||
setting.refresh_hook('hook') | ||
time.sleep(3) | ||
registry.refresh_hook('hook') | ||
setting.refresh_hook('hook') | ||
``` | ||
|
||
### 扁平化数据 | ||
``` | ||
from registry import Registry | ||
from ConfigRegistry import ConfigRegistry | ||
registry = Registry({'a': {'aa': 'aaa'}, 'b': {'bb': 'bbb'}}) | ||
print(registry.flat()) | ||
setting = ConfigRegistry({'a': {'aa': 'aaa'}, 'b': {'bb': 'bbb'}}) | ||
print(setting.flat()) | ||
``` |
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,30 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
# Intro: 配置模块安装文件 | ||
# Author: Ztj | ||
# Email: [email protected] | ||
# Date: 2019-09-04 | ||
|
||
import os.path | ||
import re | ||
|
||
from setuptools import setup | ||
|
||
f = open(os.path.join(os.path.dirname(__file__), 'README.md'), encoding='utf8') | ||
readme = f.read() | ||
f.close() | ||
|
||
f = open(os.path.join(os.path.dirname(__file__), 'ZtjRegistry.py'), encoding='utf8') | ||
version = re.search(r'__version__ = \'(.*?)\'', f.read()).group(1) | ||
f.close() | ||
|
||
setup( | ||
name='py-ztj-registry', | ||
version=version, | ||
description='python registry package', | ||
name='config-registry', | ||
version='1.0.0', | ||
description='python config registry package', | ||
long_description=readme, | ||
long_description_content_type='text/markdown', | ||
py_modules=['ZtjRegistry'], | ||
url='https://github.com/ztj-package/py-registry', | ||
py_modules=['ConfigRegistry'], | ||
url='https://pypi.org/project/config-registry', | ||
author='ZhangTianJie', | ||
author_email='[email protected]', | ||
keywords='registry config json yaml', | ||
|
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,31 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# Intro: 配置模块单元测试 | ||
# Author: Ztj | ||
# Email: [email protected] | ||
# Version: 0.0.1 | ||
# Date: 2019-09-02 | ||
|
||
import time | ||
import unittest | ||
|
||
from ZtjRegistry import Registry | ||
from ConfigRegistry import ConfigRegistry | ||
|
||
|
||
class TestRegistry(unittest.TestCase): | ||
class TestConfigRegistry(unittest.TestCase): | ||
|
||
def test_init(self): | ||
"""测试初始化""" | ||
registry = Registry({'a': {'aa': 'aaa'}}) | ||
self.assertEqual(registry.get(), {'a': {'aa': 'aaa'}}) | ||
self.assertEqual(registry.get('a.aa'), 'aaa') | ||
setting = ConfigRegistry({'a': {'aa': 'aaa'}}) | ||
self.assertEqual(setting.get(), {'a': {'aa': 'aaa'}}) | ||
self.assertEqual(setting.get('a.aa'), 'aaa') | ||
|
||
def test_set(self): | ||
"""测试设置配置项""" | ||
registry = Registry() | ||
registry.set('a', 'a') | ||
registry.set('b', [1, 2]) | ||
registry.set('c.h', 'h') | ||
self.assertEqual(registry.get(), { | ||
setting = ConfigRegistry() | ||
setting.set('a', 'a') | ||
setting.set('b', [1, 2]) | ||
setting.set('c.h', 'h') | ||
self.assertEqual(setting.get(), { | ||
'a': 'a', | ||
'b': [1, 2], | ||
'c': { | ||
|
@@ -35,13 +31,13 @@ def test_set(self): | |
|
||
def test_unset(self): | ||
"""测试删除配置项""" | ||
registry = Registry() | ||
registry.set('a', 'a') | ||
registry.set('b', [1, 2]) | ||
registry.set('c.i.h', 'h') | ||
registry.set('c.i.j', 'j') | ||
registry.unset('c.i.h') | ||
self.assertEqual(registry.get(), { | ||
setting = ConfigRegistry() | ||
setting.set('a', 'a') | ||
setting.set('b', [1, 2]) | ||
setting.set('c.i.h', 'h') | ||
setting.set('c.i.j', 'j') | ||
setting.unset('c.i.h') | ||
self.assertEqual(setting.get(), { | ||
'a': 'a', | ||
'b': [1, 2], | ||
'c': { | ||
|
@@ -50,39 +46,39 @@ def test_unset(self): | |
} | ||
} | ||
}) | ||
registry.unset('c.i.j', clear=True) | ||
self.assertEqual(registry.get(), { | ||
setting.unset('c.i.j', clear=True) | ||
self.assertEqual(setting.get(), { | ||
'a': 'a', | ||
'b': [1, 2] | ||
}) | ||
|
||
def test_flat(self): | ||
"""测试扁平处理""" | ||
registry = Registry({ | ||
setting = ConfigRegistry({ | ||
'a': { | ||
'h': 'h', | ||
'i': 'i', | ||
} | ||
}) | ||
self.assertEqual(registry.flat(), { | ||
self.assertEqual(setting.flat(), { | ||
'a.h': 'h', | ||
'a.i': 'i', | ||
}) | ||
|
||
def test_merge(self): | ||
"""测试合并配置""" | ||
registry = Registry({ | ||
setting = ConfigRegistry({ | ||
'a': { | ||
'h': 'h', | ||
'i': 'i', | ||
} | ||
}) | ||
registry.merge({ | ||
setting.merge({ | ||
'a': { | ||
'g': 'g', | ||
} | ||
}) | ||
self.assertEqual(registry.get(), { | ||
self.assertEqual(setting.get(), { | ||
'a': { | ||
'h': 'h', | ||
'i': 'i', | ||
|
@@ -92,25 +88,25 @@ def test_merge(self): | |
|
||
def test_get(self): | ||
"""测试获取配置项""" | ||
registry = Registry({'a': {'aa': 'aaa'}}) | ||
self.assertEqual(registry.get(), {'a': {'aa': 'aaa'}}) | ||
self.assertEqual(registry.get('a'), {'aa': 'aaa'}) | ||
self.assertEqual(registry.get('a.aa'), 'aaa') | ||
self.assertEqual(registry.get('b', ['b']), ['b']) | ||
setting = ConfigRegistry({'a': {'aa': 'aaa'}}) | ||
self.assertEqual(setting.get(), {'a': {'aa': 'aaa'}}) | ||
self.assertEqual(setting.get('a'), {'aa': 'aaa'}) | ||
self.assertEqual(setting.get('a.aa'), 'aaa') | ||
self.assertEqual(setting.get('b', ['b']), ['b']) | ||
|
||
def test_append(self): | ||
"""测试列表追加值""" | ||
registry = Registry({'a': {'b': ['c', 'd']}}) | ||
registry.append('a.b', 'e') | ||
self.assertEqual(registry.get(), {'a': {'b': ['c', 'd', 'e']}}) | ||
setting = ConfigRegistry({'a': {'b': ['c', 'd']}}) | ||
setting.append('a.b', 'e') | ||
self.assertEqual(setting.get(), {'a': {'b': ['c', 'd', 'e']}}) | ||
|
||
def test_default(self): | ||
"""测试设置默认值""" | ||
registry = Registry({'a': 'aaa'}) | ||
self.assertEqual(registry.default('a', 'bbb'), 'aaa') | ||
self.assertEqual(registry.get('a'), 'aaa') | ||
self.assertEqual(registry.default('c', 'ccc'), 'ccc') | ||
self.assertEqual(registry.get('c', 'ccc'), 'ccc') | ||
setting = ConfigRegistry({'a': 'aaa'}) | ||
self.assertEqual(setting.default('a', 'bbb'), 'aaa') | ||
self.assertEqual(setting.get('a'), 'aaa') | ||
self.assertEqual(setting.default('c', 'ccc'), 'ccc') | ||
self.assertEqual(setting.get('c', 'ccc'), 'ccc') | ||
|
||
def test_load(self): | ||
"""测试加载配置项""" | ||
|
@@ -124,25 +120,25 @@ def test_load(self): | |
'e': True, | ||
'f': 1.1, | ||
} | ||
registry = Registry() | ||
registry.load(d) | ||
self.assertEqual(registry.get(), d) | ||
setting = ConfigRegistry() | ||
setting.load(d) | ||
self.assertEqual(setting.get(), d) | ||
|
||
def test_hook(self): | ||
"""测试钩子""" | ||
registry = Registry() | ||
setting = ConfigRegistry() | ||
|
||
def callback(): | ||
registry.set('a', 'aaa') | ||
setting.set('a', 'aaa') | ||
return True | ||
|
||
registry.set_hook('hook', 3, callback) | ||
setting.setting_hook('hook', 3, callback) | ||
time.sleep(1) | ||
registry.refresh_hook('hook') | ||
self.assertEqual(registry.get('a'), None) | ||
setting.refresh_hook('hook') | ||
self.assertEqual(setting.get('a'), None) | ||
time.sleep(3) | ||
registry.refresh_hook('hook') | ||
self.assertEqual(registry.get('a'), 'aaa') | ||
setting.refresh_hook('hook') | ||
self.assertEqual(setting.get('a'), 'aaa') | ||
|
||
|
||
if __name__ == '__main__': | ||
|