使用config可以将配置从对象初始化的文件中分离出来 更好的结构。
This file contains hidden or 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
| import contextlib | |
| @contextlib.contextmanager | |
| def my_open(file_name): | |
| # __enter__ | |
| print('open file: ', file_name, 'in_enter') | |
| file_handler = open(file_name) | |
| yield file_handler |
This file contains hidden or 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
| 在分割出配置文件后,将任务函数也抽离出来 | |
| $ tree | |
| . | |
| ├── README.md | |
| ├── __pycache__ | |
| ├── app.py | |
| └── modules | |
| ├── __init__.py | |
| ├── __pycache__ |
This file contains hidden or 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
| from typing import TypeVar, Sequence | |
| from typing import Generic | |
| T = TypeVar('T') | |
| def first(l: Sequence[T]) -> T: | |
| return l[0] | |
This file contains hidden or 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
| from typing import Callable | |
| def i2s(x: int) -> str: | |
| return str(x) | |
| def trans(n, method: Callable[[int], str]) -> str: | |
| return method(n) |
This file contains hidden or 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
| from typing import Callable | |
| def i2s(x: int) -> str: | |
| return str(x) | |
| def trans(n, method: Callable[[int], str]) -> str: | |
| return method(n) |
This file contains hidden or 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
| class B: | |
| pass | |
| class A: | |
| def __init__(self,bs): | |
| self.bs=bs | |
| def __contains__(self, item): | |
| if isinstance(item,B): | |
| return item in self.bs |
This file contains hidden or 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
| class Bar: | |
| def __init__(self,node:Bar): # Unresolved reference 'Bar' | |
| pass | |
| class Foo: | |
| def __init__(self, node: 'Foo'): | |
| pass |
This file contains hidden or 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
| from collections import ChainMap | |
| d1 = {'a': 'a', 'b': 'b'} | |
| d2 = {'c': 'c', 'd': 'd'} | |
| m = ChainMap(d1, d2) | |
| if __name__ == '__main__': | |
| print(m) | |
| n = m.new_child({"e": "e"}) |