python3 -m venv my-test-env
- package install
- my-test-env> ํ๊ฒฝ์์ pip install๋ก ์ค์น
- pip list๋ก ํ์ธ
- pip๋ก ๊ด๋ฆฌ
pip list
pip freeze > requirements.txt
pip install -r requirements.txt
source ./my-test-env/bin/activate
- [ expression for item in iterable if ์กฐ๊ฑด ]
a_list = [number for number in range(1, 6) if number % 2 == 1]
>>>> a_list
[1, 3, 5]
- closure๋ func.closure ์ tuple๋ก ์ ์ฅ
- constructor (initializer)
- ์์ class์์ __init__ํธ์ถ์ ์๋์ผ๋ก ๋ถ๋ชจ __init__์ด ํธ์ถ ์๋จ
class Employee(object):
def __init__(self, first, last, pay):
- namespace
- instance namespace -> class namespace -> super namespace
- class variable ์ ๋ชจ๋ instance๊ฐ ๋์ผ ๊ฐ์ ๊ณต์
- reference
- instance method - self ๋ฅผ ์ฒซ ์ธ์๋ก(convention)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)
- class method - cls๋ฅผ ์ฒซ์ธ์๋ก(convention), @classmethod decorator๋ฅผ ์ด์ฉ
@classmethod
def ssn_constructor(cls, ssn):
front, back = ssn.split('-')
sex = back[0]
if sex == '1' or sex == '2':
year = '19' + front[:2]
else:
year = '20' + front[:2]
if (int(sex) % 2) == 0:
sex = '์ฌ์ฑ'
else:
sex = '๋จ์ฑ'
month = front[2:4]
day = front[4:6]
return cls(year, month, day, sex)
- static method - class์์ ์กด์ฌํ๋ ์ผ๋ฐ ํจ์ ์ญํ
@staticmethod
def is_work_day(day):
# weekday() ํจ์์ ๋ฆฌํด๊ฐ์
# ์: 0, ํ: 1, ์: 2, ๋ชฉ: 3, ๊ธ: 4, ํ : 5, ์ผ: 6
if day.weekday() == 5 or day.weekday() == 6:
return False
return True
- super() ๋ฉ์๋
- ์์ class์ ํจ์ ํธ์ถ ๊ฐ๋ฅ
def show_status(self):
super(Goblin, self).show_status()
print('๊ณต๊ฒฉ ํ์
: {}'.format(self.attack_type))
- time.perf_counter
import functools
import time
def timer(func):
"""Print the runtime of the decorated function"""
@functools.wraps(func)
def wrapper_timer(*args, **kwargs):
start_time = time.perf_counter() # 1
value = func(*args, **kwargs)
end_time = time.perf_counter() # 2
run_time = end_time - start_time # 3
print(f"Finished {func.__name__!r} in {run_time:.4f} secs")
return value
return wrapper_timer
@timer
def waste_some_time(num_times):
for _ in range(num_times):
sum([i**2 for i in range(10000)])
print(waste_some_time)
waste_some_time(1000)
- ๋์๋ฐํ ์ธก์
- argument๊ฐ ์๋ ๊ธฐ๋ณธ decorator
def decorator_repeat(func):
@functools.wraps(func)
def wrapper_repeat(*args, **kwargs):
...
return wrapper_repeat
- argument๊ฐ ์๋ decorator pattern
def repeat(num_times):
def decorator_repeat(func):
def wrapper_repeat(*args, **kwargs):
...
return wrapper_repeat
return decorator_repeat
...
์ค์ ์
def repeat(num_times):
def decorator_repeat(func):
@functools.wraps(func)
def wrapper_repeat(*args, **kwargs):
for _ in range(num_times):
value = func(*args, **kwargs)
return value
return wrapper_repeat
return decorator_repeat
import functools
def singleton(cls):
"""Make a class a Singleton class (only one instance)"""
@functools.wraps(cls)
def wrapper_singleton(*args, **kwargs):
if not wrapper_singleton.instance:
wrapper_singleton.instance = cls(*args, **kwargs)
return wrapper_singleton.instance
wrapper_singleton.instance = None
return wrapper_singleton
@singleton
class TheOne:
pass
import functools
from decorators import count_calls
def cache(func):
"""Keep a cache of previous function calls"""
@functools.wraps(func)
def wrapper_cache(*args, **kwargs):
cache_key = args + tuple(kwargs.items())
if cache_key not in wrapper_cache.cache:
wrapper_cache.cache[cache_key] = func(*args, **kwargs)
return wrapper_cache.cache[cache_key]
wrapper_cache.cache = dict()
return wrapper_cache
@cache
def fibonacci(num):
if num < 2:
return num
return fibonacci(num - 1) + fibonacci(num - 2)
- tab = 4 spaces
- max = 79 , comments์ docstring = 72
- blacklines
- function, class = 2 line
- method = 1 link
- ์์คํ์ผ ์ธ์ฝ๋ฉ
- UTF-8 (or ASCII in Python 2)
- ์ด ๊ฒฝ์ฐ encoding ์ ์ธ๋ฌธ์ ๋์ง ์๋๋ค.
- import
- ํญ์ file top
- ํ ๋ผ์ธ์๋ ํ๋์ import
- package์์ ์ฌ๋ฌ module์ ํ line์ import ํ์ฉ
- Absolute import๋ฅผ ์ถ์ฒ
- ์์ผ๋์นด๋ import๋ ํผํ๋ค
- '__'๋ก ์์ํ๋ Dunder ์ ์ธ์ ๋ชจ๋ import์์ ์จ๋ค.
- ํ์ดํธ์คํ์ด์ค
- ๋๋๋ก ํผํ๋ค
- ','์์๋ ์์ด๋ค.
- ์คํธ๋ง[์:๋ค] ์ธ ๊ฒฝ์ฐ ':' ์๋ค๋ก ๋ค ๋ถ์ด๋๊ฐ ๋ค ๋์ด๋ค.
- ๋ณ์ ์ ์ธ์ align ๋ง์ถ๊ธฐ ์ํด ์ธ๋ฐ์๋ ์คํ์ด์ค ์์ด๋ค.
- a = 12 + 34 ์ฒ๋ผ ์ฐ์ ์์๊ฐ ๋ฎ์ผ๋ฉด ์คํ์ด์ค๋ฅผ ์ด๋ค.
- ํจ์
def munge(input: AnyStr): ...
def munge() -> PosInt: ...
- keyword๋ ๋ํดํธ ์ง์ ์ ์คํ์ด์ค ์์
- trailing ์ปด๋ง
FILES = [
'setup.cfg',
'tox.ini',
]
initialize(FILES,
error=True,
)
- comment
- multiline์ผ๋ 2 space๋ฅผ ์ด๋ค.
- docstring
- ๋ชจ๋ public modules, functions, classes, and methods์ ์ด๋ค.
- package and module
- ์งง๊ฒ, ์๋ฌธ์ + _ ๋ก ์ฌ์ฉ
- class
- Type
- camel case + _ ์๋ฌธ์๋ ๊ฐ๋ฅ
- Function , varialbe
- ์๋ฌธ์ + _ ๋ก๋ง ์ฌ์ฉ
- Private method, variable
- ์์