전체(177)
-
[Python] map, filter, reduce
파이썬에서 제공하는 map, filter, reduce에 대해서 알아보자. 1. map > map의 첫번째 파라미터는 함수, 2번째 파라미터는 list가 들어가야한다. > map은 List를 리턴한다. def add(number):return number + 5 def minus(number):return number - 5 arr = [1, 3, 6, 12] result1 = map(add, arr) print result1 result2 = map(minus, result1) print result2 result [6, 8, 11, 17] [1, 3, 6, 12] > 아래의 예제는 람다와 함께 map을 사용한 예제이다. arr = [30, 50, 100, 120] arr2 = [3, 5, 10, 12..
2016.10.04 -
[dJango] compressor
1. compressor js , css 파일등을 압축할 때 사용되며, js나 css 파일들이 간혹 브라우저 캐시로 인해서 갱신 되지 않는 경우가 있는데 compressor를 사용하면 이러한 부분들도 해결된다. 2. 설치 1) 아래 명령어 실행 $ pip install django_compressor 2) setting.py 수정 INSTALLED_APPS = [ ..., ..., 'compressor', ] STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'compressor.finders.Compresso..
2016.10.03 -
[Python] with 구문
1. 파이썬에서 with 구문에 대해서 알아보자. with는 블록단위의 프로세스의 시작과 끝에 대한 처리를 해준다. 예를들어 file 은 open이 되면 반드시 닫아야 하기때문에 아래와 같이 f.open, 후 finally에서 f.close를 수행해줘야한다. try: a.open('a.txt') except: print 'error' finally: a.close() 위의 소스에서 open 중 에러가 발생할 경우 close 처리를 안해줬기 때문에 문제가 발생한다. 때문에 except 쪽에도 close 처리를 해줘야하는데, 이러한 불편한 부분들을 with문을 사용해 간단하게 해결할 수 있다. ex) with open(a.txt) as file : #do something
2016.09.30 -
[django] template tag "with" 사용법
1. django with 사용법 ex) a.html 을 include 함과 동시에 B.name 값을 넘기고자 한다. {% include 'a.html' with name=B.name %} 위와 같이 작성하면 손쉽게 데이터를 보낼 수 있으며, 만약 여러개의 데이터를 보내려면 아래와 같이 작성하면 된다. {% include 'a.html' with name=B.name age=B.age %}
2016.09.30 -
[Python] 문자열 자르기 (slice)
1. 파이썬에서는 문자열을 쉽게 자를 수 있다. 2. 사용법 [n:] > n 부터 끝까지 자름 [n:nn] > n 부터 nn-1까지 자름 [:n] > 처음부터 nn-1까지 자름 ex) alp = 'HelloBrotherWorld' > Hello slice alp[:5] > brother slice alp[5:12] > World slice alp[12:]
2016.09.28 -
[Python] 축약 함수 lambda 사용법
1. 간단한 Temp식 함수가 필요할 때 Lambda 를 사용하면 된다. 2. 사용법 func = lambda a, b: a*b func = (2, 3) - 결과result : 6
2016.09.28