Programming/Python(17)
-
Click: the Python CLI library
click ? command line interface를 쉽게 만드는데 사용되는 library 입니다. installation 간단히 pip를 통해 설치 가능합니다. pip install click 사용법   click.command click은 decorator를 통해 command를 정의합니다. CLI를 통해 실행하고 싶은 함수 위에 @click.command()를 추가하여 click framework을 통해 실행 되도록 지정합니다. ex. # ... # main.py import click @click.command() @click.option('--name', default='', help='name to be echoed') def echo(na..
2022.04.25 -
파이썬 윤년 (Python leap year)
윤년이란? 윤년(閏年, leap year)은 태음력이나 태양력에서의 흐림에 의해 생길 수 있는 오차를 보정하기 위해 추가하는 날이나 주, 달이 들어가는 해 입니다. 한국에서의 윤년은 그레고리력에서 하루를 2월 29일에 추가하여 1년간의 날짜 수가 366일이 되는 해를 의미합니다. 윤의 의미 윤년의 "윤"의 의미는 "잉여"를 의미하기에 365일에 추가적 즉, 잉여로 추가된 날이 포함된 년을 의미합니다. 한국은 예로부터 음력을 주로 사용해 왔으나, 1896년부터 태양력인 양력을 사용하게 되었습니다. 양력은 그레고리력이라고 합니다. 이 그레고리력은 0.2422일이 적은 1년을 채우고자 이러한 치윤법을 시행했다고 합니다. 윤년 계산 100으로 나눠 떨어지지 않는 4년에 한 해는 윤년이 됩니다. 혹은 400으로 ..
2021.12.24 -
Python: 원소의 중복 제거
중복 제거 Python에서 중복을 제거하는 방법은 매우 간단하다. 단순하게 list를 set으로 변환하면 된다. alist = list('abcba') # ['a', 'b', 'c', 'b', 'a'] 위 list를 set으로 변환한다. aset = set(alist) # {'c', 'b', 'a'} auniq = list(aset) # ['c', 'b', 'a'] 그런데, 위 aset이 지니고 있는 각 원소들의 순서를 원본 list 내 원소들의 순서와 동일하게 하고 싶다면? 이 때는 다음과 같이 list 초기화 exppression을..
2021.12.15 -
Python instance check
Like any other programminmg languages, Python also provides a way to check what the type of the instance is. Simple, by using 'isinstance' or 'type' function, it is possible to check instance's type. the following code is about using 'isinstance and type' val = 7 ovals = [1, 2] vals = [] if isinstance(val, int): vals += val, if type(ovals) == list: vals += ovals
2021.12.13 -
[Python] Lambda expression (람다 표현식)
lambda param : body >>> add_10 = lambda x : x + 10 >>> >>> add_10(1) 11 >>> (lambda arg : arg + 10)(1) 11 >>> (lambda : 1)() 1 Python lambda expression 내에서는 변수 선인이 불가능 >>> y = 10 >>> (lambda arg : arg + y)(1) 11 >>> def plus(x): ... return x + 10 >>> list(map(plus, [1])) [11] >>> >>> list(map(plus, [1, 2])) [11, 12] >>> list(map(lambda arg : arg + 10, [1, 2])) [11, 12] map, filter, reduce 함수 사용 ..
2021.12.13 -
Python access member as string
You from time to time meet a case that you need to set member varialbe. In such a case, if you get a class with lots of member varialbe, then code to set/get is somewhat burden. In such case, there are very simple way to do. I'll let you know in this article. class definition Assume that there is a class with three member attributes as follow. class Item: def __init__(self): self.key = None ..
2021.12.10