파이썬 - python 2.7 print function usage from __future__ import print_function # above should be in the very beginning of the program # print(*objects, sep=' ', end='\n', file=sys.stdout) print("Pink", "dfd", sep='>>', end=' ') print("Octopus") exit() Coding/Python Matlab 2012.10.10
파이썬 - 웹 링크 다운로드 (버퍼) import urllib2 def ReportError(msg, screenprinttoo = True): if screenprinttoo: print msg f = open("Error.log", "a") f.write(time.ctime() + " : " + msg + "\n") f.close() def WebLinkSave(url, savedfilename): try: f = urllib2.urlopen(url) except: ReportError("Cannot save url: " + url) return None output = open(savedfilename,'wb',1024*1024*10) while True: binaryfile = f.read() if len(binaryfile)==0:.. Coding/Python Matlab 2012.03.13
파이썬 - multi-column data array (list) sorting >>> dist = [[25, 1], [12, 2], [11, 3]] ... ... >>> >>> dist.sort(key=lambda a: a[0]) # key function only looks at first element in row ie column 0 >>> dist [[11, 3], [12, 2], [25, 1]] >>> dist = dist[0:2] # take only the first 2 rows >>> dist [[11, 3], [12, 2]] Coding/Python Matlab 2012.02.24
파이썬 - SciPy, NumPy 파이썬 확장기능들 찾다보니 과학기술 계산용 확장라이브러리가 있음을 알게 됨. Numpy + Scipy + Matplotlib... 윈도우판은 인스톨도 무척 쉽다. 그냥 맞는 버전의 것 다운로드해서 실행파일 실행하면 끝... (현재 Python 2.7 사용중) 간단한 예제 따라해 보니 Matlab 부럽지 않다. 게다가 Matlab과 달리 모두 공짜 (아~ 앞머리가 벗겨지기 시작하는 듯한 기분이...) from scipy import optimize, special from numpy import * from pylab import * x = arange(0,10,0.01) for k in arange(0.5,5.5): y = special.jv(k,x) plot(x,y) f = lambda x: -spec.. Coding/Python Matlab 2012.02.23
파이썬 - 만화 다운로드 영어로 된 만화 다운로드하는 코드. 다운로드 후 꿀뷰 등으로 보는 것이 편해서... ㅋㅋ from twill.commands import * import re,urllib2,os def WebLinkSave(url, savedfilename): try: f = urllib2.urlopen(url) except: print "Cannot save url: " + url exit() output = open(savedfilename,'wb') binaryfile = f.read() output.write(binaryfile) output.close() if __name__ == "__main__": title=raw_input('input manga title: ') datafolder = '.\\' + t.. Coding/Python Matlab 2012.02.14
파이썬 - wxPython Python에서 GUI를 구현하기 위한 라이브러리들은 상당히 다양하다. 기본으로 제공되는 Tkinter 도 있지만 다른 것들도 각각 장단점이 있어 보인다. 블로깅을 하다보니 다음의 블로그에 wxPython 에 대한 연습위주의 단계별 정리가 있기에 따라해 보려 했다. http://pythondev.egloos.com/category/wxPython 윈도우의 경우에는 wxPython이 기본으로 인스톨되지 않는데 비해 Ubuntu에서 Python을 인스톨하고 테스트해보니 기본으로 딸려들어와 있는것 같다. 따로 인스톨 한 기억이 없는데도 잘 돌아간다. 며칠 연습삼아 공부해 보자 !! Coding/Python Matlab 2012.01.29
파이썬 - lambda 다른 언어에서의 lambda에 대해서는 아는 바 없고... Python에서의 lambda에 대해 조금 더 이해하고자 여기저기 뒤진 결과를 이곳에 정리함. 대략 이해한 바로는 def ... 로 정의하는 함수와 같은 일을 하되 함수로 별도로 정의하기보다는 일회성으로 사용할 경우 유용한 방법임. lambda 변수들 : 변수들을 이용한 expression 예를 들어 g = lambda x : x**2 는 def g(x): return x**2 와 동일한 역할 filter(), map(), reduce() 등과 함께 사용하는 사례들 http://www.secnetix.de/olli/Python/lambda_functions.hawk 다음의 사이트에서는 어떤 경우에 lambda 사용이 더 적절한가와 왜 python.. Coding/Python Matlab 2012.01.27
파이썬 - how to sort a Python dict http://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/ sort by key for key in sorted(mydict.iterkeys()): print "%s: %s" % (key, mydict[key]) sort by value for key, value in sorted(mydict.iteritems(), key=lambda (k,v): (v,k)): print "%s: %s" % (key, value) Coding/Python Matlab 2012.01.26