Skip to content

Commit 15e5f79

Browse files
committed
New dynoptimdict,examples,LICENSE,pyproject.toml; Update README.md
1 parent 10602df commit 15e5f79

File tree

6 files changed

+144
-2
lines changed

6 files changed

+144
-2
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023-present, leoweyr
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# Python-DynOptimDict
2-
1+
# Dynoptimdict
2+
[![PyPI Latest Release](https://img.shields.io/pypi/v/dynoptimdict.svg)](https://pypi.org/project/dynoptimdict/)
3+
[![Package Status](https://img.shields.io/pypi/status/dynoptimdict.svg)](https://pypi.org/project/dynoptimdict/)
4+
[![License](https://img.shields.io/pypi/l/dynoptimdict.svg)](https://github.com/leoweyr/Python-Dynoptimdict/blob/main/LICENSE)
5+
[![Downloads](https://static.pepy.tech/personalized-badge/dynoptimdict?period=total&units=international_system&left_color=grey&right_color=green&left_text=pypi%20downloads)](https://pepy.tech/project/dynoptimdict)
6+
7+
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.
8+
9+
## ⚖️License
10+
11+
[MIT](https://github.com/leoweyr/Python-Dynoptimdict/blob/main/LICENSE)
12+
13+
## 📗Documentation
14+
15+
The basic usage is similar to that of Python built-in `dict`, please refer to the code example for specific differences: https://github.com/leoweyr/Python-Dynoptimdict/tree/main/examples

dynoptimdict/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .dynamic_data_dict import (
2+
DynamicDataDict
3+
)
4+
5+
__all__ = [
6+
"DynamicDataDict"
7+
]

dynoptimdict/dynamic_data_dict.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class DynamicDataDict(dict):
2+
def __init__(self):
3+
self.__get_value_operate_queue = []
4+
self.__get_value_operate_arg_list = []
5+
6+
def __repr__(self): # Called when the object does not use the dictionary access operator, which is distinguished from calling method:special<__getitem__>.
7+
self.__get_value(None) # Get all dynamic data.
8+
return super().__repr__()
9+
10+
def __getitem__(self, key):
11+
self.__get_value(key) # Get the specified dynamic data.
12+
return super().__getitem__(key)
13+
14+
def __setitem__(self, dynamic_data_name, get_dynamic_data_func: callable):
15+
self.__get_value_operate_queue.append(self.__get_value_operate)
16+
self.__get_value_operate_arg_list.append((dynamic_data_name, get_dynamic_data_func))
17+
18+
def keys(self):
19+
self.__get_value(None)
20+
return super().keys()
21+
22+
def values(self):
23+
self.__get_value(None)
24+
return super().values()
25+
26+
def items(self):
27+
self.__get_value(None)
28+
return super().items()
29+
30+
def __get_value_operate(self, key, dynamic_data_name, get_dynamic_data_func: callable):
31+
if key == dynamic_data_name or key is None:
32+
super().__setitem__(dynamic_data_name, get_dynamic_data_func())
33+
34+
def __get_value(self, key):
35+
for get_value_operate, get_value_operate_arg in zip(self.__get_value_operate_queue, self.__get_value_operate_arg_list):
36+
get_value_operate(key, get_value_operate_arg[0], get_value_operate_arg[1])
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import time
2+
from dynoptimdict import DynamicDataDict
3+
4+
5+
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.
7+
return "data_1_example" # Must have a return value.
8+
9+
10+
def get_status_data_2():
11+
time.sleep(10) # In the same way, here is just a hypothesis, for the convenience of comparison later.
12+
return "data_2_example"
13+
14+
15+
def get_status_data_3():
16+
time.sleep(100) # The same is just an assumption.
17+
return "data_3_example"
18+
19+
20+
class MyClass:
21+
@property
22+
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+
'''
30+
status["data_1"] = get_status_data_1
31+
status["data_2"] = get_status_data_2
32+
status["data_3"] = get_status_data_3
33+
34+
return status
35+
36+
37+
def main():
38+
obj = MyClass()
39+
obj.status["data_1"] # Only get the specified data, it takes 1 second, and achieve the optimized effect.
40+
obj.status # Get all data, it takes about 100 + 10 + 1 seconds.
41+
42+
43+
if __name__ == "__main__":
44+
main()

pyproject.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[tool.poetry]
2+
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."
5+
license = "MIT"
6+
authors = ["leoweyr <leoweyr@foxmail.com>"]
7+
readme = "README.md"
8+
repository = "https://github.com/leoweyr/Python-Dynoptimdict"
9+
classifiers = [
10+
"Development Status :: 3 - Alpha"
11+
]
12+
packages = [{include = "dynoptimdict"}]
13+
14+
15+
[tool.poetry.dependencies]
16+
python = "^3.8"
17+
18+
19+
[build-system]
20+
requires = ["poetry-core"]
21+
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)