Python
목차
- 1 Multiple input stream
- 2 file position
- 3 Interactive session detection
- 4 Temporary file or directory
- 5 Check syntax without running
- 6 Time
- 7 String replace
- 8 is and ==
- 9 Access index in for loop
- 10 Max integer
- 11 Argument parsing
- 12 range by step
- 13 File existence
- 14 Define source code encoding
- 15 randint
- 16 groupby example
- 17 Suppress scientific notation
- 18 a
Multiple input stream
import fileinput for line in fileinput.input(): process(line)
https://docs.python.org/2/library/fileinput.html#module-fileinput
file position
def skip_comments(f): while True: pos = f.tell() line = f.readline().strip() if not line.startswith('#'): f.seek(pos) #rewind break
tell로 현재 위치를 얻고, seek로 set.
>>> f = open('workfile', 'r+') >>> f.write('0123456789abcdef') >>> f.seek(5) # Go to the 6th byte in the file >>> f.read(1) '5' >>> f.seek(-3, 2) # Go to the 3rd byte before the end >>> f.read(1) 'd'
https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
Interactive session detection
sys.stdin.isatty()
http://stackoverflow.com/a/7576567/766330
Temporary file or directory
tempfile.TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=[, prefix='tmp'[, dir=None]]]]]) [1]
임시파일을 만든다. 아래를 쓸것.
tempfile.NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=[, prefix='tmp'[, dir=None[, delete=True]]]]]]) [2]
위와 동일하나 visible name의 파일을 만든다. 이름은 반환값의 name속성으로 받을 수 있다.
주의할점은, delete속성의 기본값이 True여서 쓰여진 후 곧바로 지워진다. 계속 쓰려면 해당속성을 False로 해야 한다.
tempfile.mkdtemp([suffix=[, prefix='tmp'[, dir=None]]]) [3]
임시디렉토리를 만들고, 자동으로 지우지 않는다.
Check syntax without running
python -m py_compile script.py
http://stackoverflow.com/a/8437597/766330
Time
current time
>>> import time >>> time.time() 1491792653.410371
>>> import datetime >>> datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') '2017-04-10 11:51:17' >>> datetime.datetime.utcnow() datetime.datetime(2017, 4, 10, 2, 51, 34, 356682) >>> datetime.datetime.now() datetime.datetime(2017, 4, 10, 11, 51, 36, 572681) >>> datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p") 'Monday, 10. April 2017 11:51AM'
yesterday
>>> from datetime import date, timedelta >>> yesterday = date.today() - timedelta(1) >>> print yesterday.strftime('%m%d%y') '110909'
String replace
str.replace
str.replace(old, new[, max]) [6]
re.sub
>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):', ... r'static PyObject*\npy_\1(void)\n{', ... 'def myfunc():') 'static PyObject*\npy_myfunc(void)\n{' [7]
commented version example
line = re.sub(r""" (?x) # Use free-spacing mode. < # Match a literal '<' /? # Optionally match a '/' \[ # Match a literal '[' \d+ # Match one or more digits > # Match a literal '>' """, "", line) [8]
is and ==
is is identity testing, == is equality testing. what happens in your code would be emulated in the interpreter like this:
>>> a = 'pub' >>> b = ''.join(['p', 'u', 'b']) >>> a == b True >>> a is b False
Access index in for loop
enumerate
for idx, val in enumerate(ints): print(idx, val)
Max integer
sys.maxint
Argument parsing
use argparse
range by step
>>> step = .1 >>> N = 10 # number of data points >>> [ x / pow(step, -1) for x in range(0, N + 1) ] [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
File existence
os.path.exists(file_path)
Define source code encoding
첫째나 둘째줄에,
# coding=<encoding name>
보통 에디터들은 다음 형식을 이해함.
#!/usr/bin/python # -*- coding: <encoding name> -*-
혹은
#!/usr/bin/python # vim: set fileencoding=<encoding name> :
정확히는, 첫째나 둘째줄이 아래 정규식에 맞아야 함.
^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)
https://www.python.org/dev/peps/pep-0263/
randint
from random import randint print(randint(0,9))
0~9 inclusive
https://docs.python.org/2/library/random.html#random.randint
groupby example
from itertools import groupby things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"), ("vehicle", "speed boat"), ("vehicle", "school bus")] for key, group in groupby(things, lambda x: x[0]): for thing in group: print "A %s is a %s." % (thing[1], key) print " "
output
A bear is a animal. A duck is a animal. A cactus is a plant. A speed boat is a vehicle. A school bus is a vehicle.
http://stackoverflow.com/a/7286/766330
Suppress scientific notation
use %f
'%f' % (x/y)