Skip to content

Commit 5704257

Browse files
committed
Support storing static data directly.
1 parent 15e5f79 commit 5704257

File tree

5 files changed

+78
-53
lines changed

5 files changed

+78
-53
lines changed

dynoptimdict/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .dynamic_data_dict import (
2-
DynamicDataDict
1+
from .dynamic_dict import (
2+
DynamicDict
33
)
44

55
__all__ = [
6-
"DynamicDataDict"
6+
"DynamicDict"
77
]

dynoptimdict/dynamic_data_dict.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

dynoptimdict/dynamic_dict.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import Callable
2+
3+
4+
class DynamicDict(dict):
5+
def __init__(self):
6+
self.__get_value_operate_queue = []
7+
self.__get_value_operate_arg_list = []
8+
9+
def __repr__(self):
10+
"""
11+
Called when the object does not use the dictionary access operator,
12+
which is distinguished from calling method:special<__getitem__>.
13+
"""
14+
self.__get_value(None) # Get all dynamic or static data.
15+
return super().__repr__()
16+
17+
def __getitem__(self, key):
18+
self.__get_value(key) # Get the specified dynamic or static data.
19+
return super().__getitem__(key)
20+
21+
def __setitem__(self, data_name, get_dynamic_data_func_or_static_data_value):
22+
self.__get_value_operate_queue.append(self.__get_value_operate)
23+
self.__get_value_operate_arg_list.append((data_name, get_dynamic_data_func_or_static_data_value))
24+
25+
def keys(self):
26+
self.__get_value(None)
27+
return super().keys()
28+
29+
def values(self):
30+
self.__get_value(None)
31+
return super().values()
32+
33+
def items(self):
34+
self.__get_value(None)
35+
return super().items()
36+
37+
def __get_value_operate(self, key, data_name, get_dynamic_data_func_or_static_data_value):
38+
if key == data_name or key is None:
39+
if isinstance(get_dynamic_data_func_or_static_data_value, Callable): # Store dynamic data.
40+
super().__setitem__(data_name, get_dynamic_data_func_or_static_data_value())
41+
else: # Store static data.
42+
super().__setitem__(data_name, get_dynamic_data_func_or_static_data_value)
43+
44+
def __get_value(self, key):
45+
for get_value_operate, get_value_operate_arg in zip(self.__get_value_operate_queue, self.__get_value_operate_arg_list):
46+
get_value_operate(key, get_value_operate_arg[0], get_value_operate_arg[1])

examples/whether_to_use_the_dictionary_access_operator.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,51 @@
11
import time
2-
from dynoptimdict import DynamicDataDict
2+
from dynoptimdict import DynamicDict
3+
4+
5+
'''
6+
The following steps are necessary. If you want to obtain dynamic data from the dynamic dict in real time, you must
7+
define function which is used to get related data, and then pass in its function pointer through the method of adding
8+
key-value pairs to dict.
9+
'''
310

411

512
def get_status_data_1():
6-
time.sleep(1) # Here is just assuming this function takes 1 second to perform the calculation before returning the result.
13+
"""
14+
Here is just assuming this function takes 1 second to perform the calculation before returning the result.
15+
"""
16+
time.sleep(1)
717
return "data_1_example" # Must have a return value.
818

919

1020
def get_status_data_2():
11-
time.sleep(10) # In the same way, here is just a hypothesis, for the convenience of comparison later.
21+
"""
22+
In the same way, here is just a hypothesis, for the convenience of comparison later.
23+
"""
24+
time.sleep(10)
1225
return "data_2_example"
1326

1427

1528
def get_status_data_3():
16-
time.sleep(100) # The same is just an assumption.
29+
"""
30+
The same is just an assumption.
31+
"""
32+
time.sleep(100)
1733
return "data_3_example"
1834

1935

2036
class MyClass:
2137
@property
2238
def status(self):
23-
status = DynamicDataDict()
24-
'''
25-
The following steps are necessary. If you want to obtain data from the dynamic data dict in real time, you must
26-
define function which is used to get related data, and then pass in its function pointer through the method of
27-
adding key-value pairs to dict.
28-
29-
'''
39+
status = DynamicDict()
40+
41+
# Store the acquisition way of each dynamic data.
3042
status["data_1"] = get_status_data_1
3143
status["data_2"] = get_status_data_2
3244
status["data_3"] = get_status_data_3
3345

46+
# Store static data directly.
47+
status["data_4"] = "data_4_example"
48+
3449
return status
3550

3651

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[tool.poetry]
22
name = "dynoptimdict"
3-
version = "1.0.0"
4-
description = "A dynamic data dict class that inherits and overrides the built-in dict class for special purposes. That provides real-time access to dynamic data, while still allowing the option to get only the specified data without calculating all. It adheres to the concept of program optimization which avoids loading if not used, saving both memory and time."
3+
version = "2.0.0"
4+
description = "A dynamic dict class that inherits and overrides the built-in dict class for special purposes. That provides real-time access to dynamic data, while still allowing the option to get only the specified data without calculating all. It adheres to the concept of program optimization which avoids loading if not used, saving both memory and time."
55
license = "MIT"
66
authors = ["leoweyr <leoweyr@foxmail.com>"]
77
readme = "README.md"
88
repository = "https://github.com/leoweyr/Python-Dynoptimdict"
99
classifiers = [
10-
"Development Status :: 3 - Alpha"
10+
"Development Status :: 4 - Beta"
1111
]
1212
packages = [{include = "dynoptimdict"}]
1313

0 commit comments

Comments
 (0)