-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
156 changed files
with
1,783 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 고차함수와 데코레이터 | ||
# https://soooprmx.com/archives/3999#more-3999 | ||
|
||
# asyncio API : 코루틴과 태스크 | ||
# https://docs.python.org/ko/3/library/asyncio-task.html | ||
|
||
|
||
# static & class method | ||
|
||
# regular expression | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import os | ||
import sys | ||
|
||
#-------------- | ||
# * iterable | ||
# * iterator | ||
# * iteration | ||
# * generator | ||
#-------------- | ||
|
||
# * iterable : 반복가능한 객체 | ||
# __iter__ or __getitem__ method가 정의된 파이썬의 모든 객체. | ||
|
||
# * iterator : 반복자 | ||
# next() or __next__() method가 정의된 모든 객체. | ||
|
||
# * iteration : 반복 | ||
# 리스트 같은 저장소에서 아이템을 가져오는 과정. | ||
|
||
# * generator | ||
# generator는 iterator이지만 단 한 번만 반복한다. | ||
# 메모리에 모든 값을 저장하지 않기 때문에 값을 사용할 때 즉시 생성한다. | ||
# for loop를 사용하거나 반복하는 함수나 구조에 생성된 값들을 전달하여 반복을 사용한다. | ||
# 대부분 generator는 함수로 구현된다. 그러나 값을 return하지 않고, yield(산출)될 뿐이다. | ||
|
||
|
||
# generator 모든 결과물들을 메모리에 저장하지 않으면서 동시에, 많은 양의 결과 셋을 계산해야할 때 좋다. | ||
# 특히 루프 그 자체를 포함한 계산을 할때. | ||
|
||
# fibonacci generator version | ||
def fibonacci(n): | ||
a = b = 1 | ||
for i in range(n): | ||
yield a | ||
a, b = b, a+b | ||
|
||
for x in fibonacci(100): | ||
print(x) | ||
|
||
gen = fibonacci(100) | ||
print(next(gen)) | ||
print(next(gen)) | ||
print(next(gen)) | ||
print(next(gen)) | ||
print(next(gen)) | ||
|
||
# str type은 iterable 이지만 iterator는 아니다. | ||
# next()는 못쓰지만, iter 를 사용하면 된다. | ||
myStr = "abcdefg" | ||
myIter = iter(myStr) | ||
print(next(myIter)) | ||
print(next(myIter)) | ||
print(next(myIter)) | ||
print(next(myIter)) | ||
print(next(myIter)) | ||
print(next(myIter)) | ||
print(next(myIter)) | ||
#print(next(myIter)) # error | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import os | ||
import sys | ||
|
||
#* map | ||
#* filter | ||
|
||
#----------- | ||
# * map | ||
#----------- | ||
# map 은 입력 리스트의 모든 항목에 함수를 적용한다. | ||
# | ||
# map(function_to_apply, list_of_inputs) | ||
|
||
# without map | ||
itemList = [1,2,3,4,5] | ||
resultList = [] | ||
for each in itemList: | ||
resultList.append(each**2) | ||
print(resultList) | ||
|
||
# use map | ||
itemList = [1,2,3,4,5] | ||
resultList = map(lambda x: x**2, itemList) | ||
print(list(resultList)) | ||
|
||
# new example | ||
def mul(x): | ||
return x*x | ||
|
||
def add(x): | ||
return x+x | ||
|
||
funcs = [mul, add] | ||
for i in range(5): | ||
value = map(lambda x:x(i), funcs) | ||
print(list(value)) | ||
|
||
#------------ | ||
# * filter | ||
#------------ | ||
# 함수에서 true를 반환하는 요소들로 구성되는 리스트를 생성한다. | ||
|
||
inList = range(-5, 5) | ||
lessThanZero = list(filter(lambda x: x<0, inList)) | ||
print(lessThanZero) | ||
|
||
|
||
#------------ | ||
# * reduce | ||
#------------ | ||
# reduce는 리스트로 몇가지 계산을 수행하고 반환하는 매우 유용한 함수다. | ||
# 예를 들어 리스트의 곱을 계산하려고 하면, | ||
from functools import reduce | ||
output = reduce( (lambda x, y: x*y), [1,2,3,4] ) | ||
print(output) | ||
|
||
|
||
|
||
|
||
# 정리 | ||
# map | ||
inList = [1,2,3,4,5] | ||
outList = list(map( lambda x: x*x, inList)) | ||
print(outList) # [1,4,9, 16, 25] | ||
|
||
# filter | ||
inList = [1,2,3,4,5] | ||
outList = list(filter(lambda x: x>3, inList)) | ||
print(outList) # [4,5] | ||
|
||
# reduce | ||
from functools import reduce | ||
inList = [1,2,3,4,5] | ||
outList = reduce(lambda x,y: x*y, inList) | ||
print(outList) # 120 | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
import sys | ||
|
||
# set datastructure | ||
#aSet = {''} | ||
#print(type(aSet)) # set | ||
|
||
someList = ['a', 'a', 'b', 'c', 'd', 'e', 'e'] | ||
someSet = set(someList) | ||
print(someSet) | ||
|
||
# brute force version | ||
duplicateList = [] | ||
for value in someList: | ||
if someList.count(value) > 1: | ||
if value not in duplicateList: | ||
duplicateList.append(value) | ||
print(duplicateList) | ||
|
||
# more nice code by using set | ||
someList = ['a', 'a', 'b', 'c', 'd', 'e', 'e'] | ||
duplicateSet = set([x for x in someList if someList.count(x) > 1]) | ||
print(list(duplicateSet)) | ||
|
||
#------------- | ||
# set methods | ||
#------------- | ||
#>>> intersection | ||
valid = set(['yellow', 'red', 'blue', 'green', 'black']) | ||
inSet = set(['red', 'brown']) | ||
print(inSet.intersection(valid)) | ||
|
||
#>>> difference | ||
valid = set(['yellow', 'red', 'blue', 'green', 'black']) | ||
inSet = set(['red', 'brown']) | ||
print(inSet.difference(valid)) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
import sys | ||
|
||
# (if_testIsTrue, if_testIsFalse) [test] | ||
|
||
isFat = True | ||
state = ('skinny', 'fat')[isFat] | ||
print(state) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import os | ||
import sys | ||
|
||
def new_decorator(func): | ||
def wrapTheFunction(): | ||
print("before run func") | ||
func() | ||
print("after run func") | ||
return wrapTheFunction | ||
|
||
def function_requiring_decoration(): | ||
print("in function_requireing_decoration()") | ||
|
||
function_requiring_decoration() | ||
func_with_deco = new_decorator( function_requiring_decoration ) | ||
func_with_deco() | ||
|
||
@new_decorator | ||
def anotherFunction(): | ||
print("anotherFunction") | ||
anotherFunction() | ||
|
||
|
||
# >>> use functools.wraps | ||
|
||
from functools import wraps | ||
|
||
def newDecorator(inFunc): | ||
@wraps(inFunc) | ||
def wrapTheFunction(): | ||
print("before run inFunc") | ||
inFunc() | ||
print("after run inFunc") | ||
return wrapTheFunction | ||
|
||
@newDecorator | ||
def function_requiring_decorator(): | ||
print("in function_requiring_decorator") | ||
return 'a' | ||
|
||
print(function_requiring_decorator.__name__) | ||
|
||
|
||
# >>> | ||
from functools import wraps | ||
canRun = True | ||
def decoratorName(f): | ||
@wraps(f) | ||
def decorated(*args, **kwargs): | ||
if not canRun: | ||
return "Will not run" | ||
return f(*args, **kwargs) | ||
return decorated | ||
|
||
@decoratorName | ||
def func(): | ||
return("in func") | ||
|
||
canRun = True | ||
print(func()) | ||
canRun = False | ||
print(func()) | ||
|
||
|
||
# example 1. : 인증 | ||
from functools import wraps | ||
def requires_auth(f): | ||
@wraps(f) | ||
def decorated(*args, **kwargs): | ||
auth = request.authorization | ||
if not auth or not check_auth(auth.username, auth.password): | ||
return authenticate() | ||
return f(*args, **kwargs) | ||
return decorated | ||
|
||
|
||
# example 2. : Logging | ||
from functools import wraps | ||
def logit(func): | ||
@wraps(func) | ||
def with_logging(*args, **kwargs): | ||
print(func.__name__ + " was called") | ||
return func(*args, **kwargs) | ||
return with_logging | ||
|
||
@logit | ||
def addFunc(x): | ||
return x+x | ||
result = addFunc(10) | ||
|
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
|
||
# bad example | ||
def add_to(num, target=[]): | ||
target.append(num) | ||
return target | ||
|
||
print(add_to(1)) #[1] | ||
print(add_to(2)) #[1,2] | ||
print(add_to(3)) #[1,2,3] | ||
|
||
# if you want to | ||
# [1] | ||
# [2] | ||
# [3] | ||
|
||
# correct | ||
def add_to2(element, target=None): | ||
if target == None: | ||
target = [] | ||
target.append(element) | ||
return target | ||
|
||
print(add_to2(1)) #[1] | ||
print(add_to2(2)) #[2] | ||
print(add_to2(3)) #[3] | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# install | ||
#> pip install virtualenv | ||
|
||
# set virtualenv | ||
#> virtualenv myenv | ||
#> source myenv/bin/activate | ||
|
||
# virtualenv를 생성하는 동안 virtialenv가 시스템의 site-packages에 있는 패키지들을 사용하거나, | ||
# virtualenv의 site-packages에 있는 패키지들을 설치할지를 결정해야한다. | ||
# 기본적으로는 virtualenv는 전역 사이트 패키지에 대한 엑세스 권한을 부여하지 않는다. | ||
# virtualenv가 시스템 site-packages에 엑세스하게 하려면 | ||
# 다음과 같이 virtualenv를 만들때 --system-site-packages 스위치를 사용해야한다. | ||
# | ||
#> virtualenv --system-site-packages mycoolvenv | ||
|
||
# deactivate command | ||
#> deactivate | ||
|
Oops, something went wrong.