Skip to content

Instantly share code, notes, and snippets.

View AntiKnot's full-sized avatar
🎯
Focusing

Yan.xxx AntiKnot

🎯
Focusing
View GitHub Profile
@AntiKnot
AntiKnot / my_open.py
Created August 10, 2020 09:11
实现上下文管理 with
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
@AntiKnot
AntiKnot / README.txt
Last active August 9, 2020 15:52
celery demo 3
在分割出配置文件后,将任务函数也抽离出来
$ tree
.
├── README.md
├── __pycache__
├── app.py
└── modules
├── __init__.py
├── __pycache__
@AntiKnot
AntiKnot / README.md
Last active August 9, 2020 15:34
celery demo 2

使用config可以将配置从对象初始化的文件中分离出来 更好的结构。

@AntiKnot
AntiKnot / README.md
Created August 9, 2020 15:15
celery demo 1

#celery demo

1

choice a broker, here use RabbitMQ

docker run -d -p 5672:5672 rabbitmq

2

install celery

@AntiKnot
AntiKnot / foo.py
Created June 2, 2020 06:53
python typing Generic
from typing import TypeVar, Sequence
from typing import Generic
T = TypeVar('T')
def first(l: Sequence[T]) -> T:
return l[0]
@AntiKnot
AntiKnot / foo.py
Last active June 2, 2020 06:45
python typing Callable
from typing import Callable
def i2s(x: int) -> str:
return str(x)
def trans(n, method: Callable[[int], str]) -> str:
return method(n)
@AntiKnot
AntiKnot / foo.py
Created June 2, 2020 06:42
python typing Callable
from typing import Callable
def i2s(x: int) -> str:
return str(x)
def trans(n, method: Callable[[int], str]) -> str:
return method(n)
@AntiKnot
AntiKnot / foo.py
Created June 2, 2020 03:13
python magic methods
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
@AntiKnot
AntiKnot / foo.py
Last active June 2, 2020 02:58
python type hint, use self
class Bar:
def __init__(self,node:Bar): # Unresolved reference 'Bar'
pass
class Foo:
def __init__(self, node: 'Foo'):
pass
@AntiKnot
AntiKnot / cm.py
Last active June 1, 2020 10:17
python ChainMap new_child parents
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"})