Skip to content

Instantly share code, notes, and snippets.

@reydx
reydx / update_dict.py
Last active October 15, 2021 02:04
python skills
if key not in my_dict:
my_dict[key] = []
my_dict[key].append(new_value)
# 等价于
my_dict.setdefault(key, []).append(new_value)
# 等价于
import collections
my_dict = collections.defaultdict(list)
@reydx
reydx / composite.py
Created May 21, 2021 01:25
design-pattern
# -*- coding: UTF-8 -*-
# 组合模式
from abc import ABCMeta, abstractmethod
class Graphic(metaclass=ABCMeta):
@abstractmethod
def draw(self):
pass
@reydx
reydx / bridge.py
Created May 21, 2021 01:13
design-pattern
# -*- coding: UTF-8 -*-
# 桥模式
from abc import ABCMeta, abstractmethod
class Shape(metaclass=ABCMeta):
def __init__(self, color):
self.color = color
@reydx
reydx / adapter.py
Created May 20, 2021 01:24
design-pattern
# -*- coding: UTF-8 -*-
# 适配器模式
from abc import ABCMeta, abstractmethod
class Payment(metaclass=ABCMeta):
@abstractmethod
def pay(self, money):
pass
@reydx
reydx / singleton.py
Created May 20, 2021 01:14
design-pattern
# -*- coding: UTF-8 -*-
# 单例模式
class Singleton:
def __new__(cls, *args, **kwargs):
if not hasattr(cls, "_instance"):
cls._instance = super(Singleton, cls).__new__(cls)
return cls._instance
@reydx
reydx / builder.py
Created May 20, 2021 01:06
design-pattern
# -*- coding: UTF-8 -*-
# 建造者模式
from abc import ABCMeta, abstractmethod
class Player:
def __init__(self, face=None, body=None, arm=None, leg=None):
self.face = face
self.body = body
@reydx
reydx / abstract_factory.py
Created May 20, 2021 00:53
design-pattern
# -*- coding: UTF-8 -*-
# 抽象工厂模式
from abc import ABCMeta, abstractmethod
class PhoneShell(metaclass=ABCMeta):
@abstractmethod
def show_shell(self):
pass
@reydx
reydx / factory_method.py
Last active May 20, 2021 00:54
design-pattern
# -*- coding: UTF-8 -*-
# 工厂方法模式
from abc import ABCMeta, abstractmethod
class Payment(metaclass=ABCMeta):
@abstractmethod
def pay(self, money):
pass
@reydx
reydx / factory.py
Last active May 20, 2021 00:54
design-pattern
# -*- coding: UTF-8 -*-
# 简单工厂模式
from abc import ABCMeta, abstractmethod
class Payment(metaclass=ABCMeta):
@abstractmethod
def pay(self, money):
pass

1 表头加单位, 数字不加单位

2 注意数字字体, 保证单个数字的宽度相同

3 数字右对齐