Skip to content

Latest commit

 

History

History
918 lines (652 loc) · 16.9 KB

File metadata and controls

918 lines (652 loc) · 16.9 KB

Major Function

Exception

Basic Example

i=1
try:ß
    print(1/(3-i)^10)
except  ZeroDivisionError as err:
    print("0으로 나누지 마세요.")
    print(err)
    print(err.args)
except TypeError:
    print("type err")
except  Exception as err:
    print("--------------------")
    logging.exception('')
    print("--------------------")
    print(err.args)
else:
    print("None Error")
finally:
    print("--------------------")

tuples as function arguments

def f1(x,y):
    return (x,y)
def f2(x,y):
    print(x+y)

f2(*f1(2,3) )

Raise

try:
    raise Exception('spam', 'eggs')
except Exception as inst:
    print(type(inst))    # the exception instance
    print(inst.args)     # arguments stored in .args
    print(inst)          # __str__ allows args to be printed directly,
                         # but may be overridden in exception subclasses
    x, y = inst.args     # unpack args
    print('x =', x)
    print('y =', y)

Lambda

Lamdba Operator

f=lambda x: x**2
f(3)
def inc(n):
 return lambda x: x + n

f=inc(2)

print(f(12))

print(inc(2)(12))

# 출처: https://offbyone.tistory.com/73 [쉬고 싶은 개발자]

Map

a=[1, 2, 3, 4]
b=[10,20,30,40]
list(map(lambda x, y:x+y, a,b))

Filter

foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
list( filter(lambda x: x % 3 == 0, foo))

Reduce

from functools import reduce
reduce(lambda x,y: x+y, [1,2,3,4,5])

python2

# map(lambda x: x**2, [0,1,2,3])
# reduce(lambda x,y: x+y, [0,1,2,3])
# filter(lambda x: x < 5, range(10))

Builtin Function

eval

  • 문자열을 코드로 실행 가능하게 함.
x=3
str='3+x'
print(eval(str))
  • 단, 변수를 정의할때는 안되므로, local()['변수명']으로 사용
#fail : eval('y=1')
locals()['y'] = 1

zip

a=[1,2,3]
b=[4,5,6]
[i for  i in zip(a,b)]

struct

  • Python 문자열로 표현된 C structs와 Python Value 간의 변환
  • Link : http://hextracker.tistory.com/141
  • 특징
    • 파일에 Binary Data를 저장하거나 Network Connection 시 사용
  • 주요 Fuction
    • struct.pack(fmt,v1,v2,...) 지정된 Format에 따라 v1,v2,..의 Values를 Pack을 수행하며 그 결과는 String으로 리턴

    • struct.unpack(fmt,string) 지정된 Format에 따라 String을 Unpack을 수행하며 그 결과는 Tuple로 리턴

import struct

def getDword(s, offset) :
    return struct.unpack("<L", s[offset:offset+4])[0]

str = b"123456789"
tmp = getDword(str,0)
print(hex(tmp))

Byte Order

Character Byte Order Size Alignment
@ Native Native Native
= Native Standard None
< Little-Endian Standard None
> Big-Endian Standard None
! network(=Big-Endian) Standard None

Format Characters

Format C Types Python Type Standard Size
x Pad Byte No Value
c char String of length 1 1
b signed char integer 1
B unsigned char integer 1
? _Bool bool 1
h short integer 2
H unsigned short integer 2
i int integer 4
I unsigned int integer 4
l long integer 4
L unsigned long integer 4
q long long integer 8
Q unsigned long long integer 8
f float float 4
d double float 8
s char[] string
p char[] string
P void * integer

Builtin Library

OS명령

import os
print(os.system('touch /tmp/x123') )
!ls /tmp/x123

Pickle

  • 파이썬객체를 저장하고 읽음.
import pickle

thePath="/tmp/tmp"
obj=[1,2,3,4]
with open(thePath, "wb") as f:
  pickle.dump(obj,f)


with open(thePath, "rb") as f:
    obj2 = pickle.load(f)

obj2

os.path

import os
import sys
from pathlib import Path

basePath=os.path.dirname(sys.argv[0])

childPath=os.path.join(basePath,"test")

os.makedirs(childPath)
print(os.listdir(basePath))
parentPath=Path(basePath).parent
# join(os.path.abspath( join(basePath,"..") )
executedPath=os.getcwd()

os.path.isdir(basePath)

os.path.isfile(basePath)

Default Dict

  • 없는 키값을 리턴해도 에러를 발생시키기 않는 Dictionary
from collections import defaultdict
d=defaultdict(lambda: None)
d["a"]       # no error, return None
d["b"]=3
d.keys()
print(d["c"])
d.keys()

Regular Expression

import re

re.escape

re.findall

re.findall('a','apache')
# ['a', 'a']

MultiLine Examples

import re
s='''
aa
bbb cc
dd
'''
re.findall('bb.*$',s,re.DOTALL)

re.finditer

re.fullmatch

re.functools

re.match

  • 주의 정확히 일치 해야 함.
p=re.compile('[a-z]+')
if p.match("python"):
  print('Match')


if not re.match('a$',"ca"):
     print("Match")
else:
     print("NoMatch") # <==요게 나옴 정확히 않맞으니까.

re.search/compile/group

  • match Object의 메서드들
    • Method, Descrption
    • group(), 일치된 문자열을 반환한다.
    • start(), 일치된 문자열의 시작 위치를 반환한다.
    • end(), 일치된 문자열의 끝 위치를 반환한다.
    • span(), 일치된 문자열의 (시작 위치, 끝 위치) 튜플을 반환한다.
  • Example Simple
regex = re.compile('(a+).*(c+)')
mo = regex.search('abc')
mo.group(2)
# 'c'
  • Example Full
import re

matchObj = re.search('s+', "a a ss a ssss")
print(matchObj)

regex=re.compile('(a)(b)(.*)')
mo=regex.search('abca')
print(mo.group(1))

print(matchObj.group())
# ss
print(matchObj.start())
# 4
print(matchObj.end())
# 6
print(matchObj.span())
# (4,6)

re.split

re.sub

re.sub(r"18|28",r"xx","She is 18, her sister is 28.")
### re.subn
re.subn(r"18|28",r"xx","She is 18, her sister is 28.")
# ('She is xx, her sister is xx.', 2)

re.subn(r"18",r"xx","She is 18, her sister is 28.")
# ('She is xx, her sister is 28.', 1)

Examples

import re
operater = '[\+|\-|\/|\*|\(|\)]+'
e="(3+4)*5"
operaters=re.findall(operater,e)
variables=re.split(operater, e)

json

!echo '{"aa":1,"bb":[1,2,3]}' > "/tmp/xx.json"
import json
json_data=open('/tmp/xx.json').read()
d=json.loads(json_data)
print(d)
print(d['bb'])

dateutil

  • String to Date
from dateutil.parser import parse
parse('2000-01-01')
  • Date add
import datetime
from dateutil.parser import parse
d=parse('2010-01-01')-parse('2000-01-01')
print(d)
print(parse('2010-01-01') + datetime.timedelta(days=10) )

pdb

import pdb; pdb.set_trace()

PDB Syntax

PDB 명령어 실행내용
help 도움말
next 다음 문장으로 이동
print 변수값 화면에 표시
list 소스코드 리스트 출력. 현재 위치 화살표로 표시됨
where 콜스택 출력
continue 계속 실행. 다음 중단점에 멈추거나 중단점 없으면 끝까지 실행
step Step Into 하여 함수 내부로 들어감
return 현재 함수의 리턴 직전까지 실행
!변수명 = 값 변수에 값 재설정

subprocess

import subprocess
p = subprocess.Popen('df -h', shell=True, stdout=subprocess.PIPE)
p.communicate()[0].decode('utf-8')
# python2 :  print(str(p.communicate()[0], 'utf-8'))

Requests

Request Example

import requests
r = requests.get('https://api.github.com/events')
r = requests.post('https://httpbin.org/post', data = {'key':'value'})
r = requests.put('https://httpbin.org/put', data = {'key':'value'})
r = requests.delete('https://httpbin.org/delete')
r = requests.head('https://httpbin.org/get')
r = requests.options('https://httpbin.org/get')

Request with Header

import requests
url = 'http://www.ichangtou.com/#company:data_000008.html'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = requests.get(url, headers=headers)
print(response.content)

Request with Post(file)

import requests
url = 'https://httpbin.org/post'
files = {'file': open('f.png', 'rb')}
r = requests.post(url, files=files)

Counter

import collections
l=['a','b','b','b','b','b','b','b','c','c','c','d','d']
collections.Counter(l).most_common()

argparse

Example

  • Simple
import argparse
parser = argparse.ArgumentParser(description='---------')
parser.add_argument('filePath', metavar='path', type=str, help='Input FilePath')
parser.add_argument("-d", "--debug", action="store_true", help="Debug Mode On")
args = parser.parse_args()

print(args.filePath)
print(args.debug)
  • Some Complex
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',const=sum, default=max,help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))
print(dir(args))
  • cf) no argparse
import sys
print('sys.argv 길이 : ', len(sys.argv))
for arg in sys.argv:
    print('arg value = ', arg)

closure : Dynamic Function

def f(a=1,b=2):
    print(a,b)
      return a+b

def mf(a,b):
    def f1():
        return f(a,b)
    return f1

fn1=mf(1,1)
fn2=mf(1,2)
fn1()
fn2()

json

import json
json_data=open('xx.json').read()
d=json.loads(json_data)

dateutil

  • String to Date
from dateutil.parser import parse
parse('2000-01-01')
  • Date add
import datetime
from dateutil.parser import parse
d=parse('2010-01-01')-parse('2000-01-01')
print(d)
print(parse('2010-01-01') + datetime.timedelta(days=10) )

pdb

import pdb; pdb.set_trace()

PDB Syntax

PDB 명령어 실행내용
help 도움말
next 다음 문장으로 이동
print 변수값 화면에 표시
list 소스코드 리스트 출력. 현재 위치 화살표로 표시됨
where 콜스택 출력
continue 계속 실행. 다음 중단점에 멈추거나 중단점 없으면 끝까지 실행
step Step Into 하여 함수 내부로 들어감
return 현재 함수의 리턴 직전까지 실행
!변수명 = 값 변수에 값 재설정

pathlib.Path

from pathlib import Path
parentPath=Path(basePath).parent
# join(os.path.abspath( join(basePath,"..") )

subprocess

import subprocess
p = subprocess.Popen('df -h', shell=True, stdout=subprocess.PIPE)
print(str(p.communicate()[0], 'utf-8'))

etc

String Format

% 기반

print('-- %d --'%3)
print('-- %d -- %s --'%(3,'hi'))
for i in range(1,1000,77):
    print("%10.2f , %10d"%(i/3,i))

str.format

print( "Hi, {}".format('steve') )
print( "{0}, {1}, {0}".format('2','steve') )
print( "{id}, {name}".format(id='2',name='steve') )

f-string

  • from Python3.6
name="Steve"
print(f'Hello {name}')

closure : Dynamic Function

def f(a=1,b=2):
    print(a,b)
    return a+b

def mf(a,b):
    def f1():
        return f(a,b)
    return f1

fn1=mf(1,1)
fn2=mf(1,2)
fn1()
fn2()

Shell에서 Python구현

%%bash
echo '
print("   Column1, Column2")
for i in range(1,1000,40):
       print("%10.2f , %10d"%(i/3,i))
' |python > /tmp/a.csv
cat /tmp/a.csv

freeze and -r

%%bash
pip freeze > /tmp/requirements.txt


# requirements.txt를 새로운 시스템으로 파일 이동후 사용하거나 ==를 >=로 바꿔서 사용

# pip install -r requirements.txt
!cat /tmp/requirements.txt

Windows 환경에서 주의사항

  1. windows10에 git-scm 사용시 : python을 호출 못하는 경우 있음.
  2. "#!/usr/bin/env python" 문을 사용하면 안됨.
  3. 경로에 xx.py를 넣어두고 실행하는 것 안됨(xx.bat에서 호출해서 사용할 것)

CodeReversor

data=['aaa','bbb','ccc']
enc={ r[1]:r[0]   for r in  enumerate(data)}
def corderReversor(corder):
  k=enc.keys()
  return { corder[i]:i for i in k}

dec=corderReversor(enc)

print("Encoder = " , enc)
print("Decoder = " , dec)

list to 2D-Matrix

a=[0.0,8.04,10.0,9.14,10.0,7.46,8.0,6.58,8.0,6.95,8.0,8.14,8.0,6.77,8.0,5.76,13.0,7.58,13.0,8.74,13.0,12.74,8.0,7.71,9.0,8.81,9.0,8.77,9.0,7.11,8.0,8.84,11.0,8.33,11.0,9.26,11.0,7.81,8.0,8.47,14.0,9.96,14.0,8.10,14.0,8.84,8.0,7.04,6.0,7.24,6.0,6.13,6.0,6.08,8.0,5.25,4.0,4.26,4.0,3.10,4.0,5.39,19.0,12.50,12.0,10.84,12.0,9.13,12.0,8.15,8.0,5.56,7.0,4.82,7.0,7.26,7.0,6.42,8.0,7.91,5.0,5.68,5.0,4.74,5.0,5.73,8.0,6.89]
it=iter(a)
rowCnt=8
x = [[i for i in range(rowCnt)] for j in range(int(len(a)/rowCnt))]
print(x)

날짜 범위를 리스트로 만들기

import datetime
start = datetime.datetime.strptime("20130129","%Y%m%d")
end = datetime.datetime.strptime("20130203", "%Y%m%d")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
for date in date_generated:
    print(date.strftime("%Y%m%d"))

대화형 입력 : expect

import pexpect
child = pexpect.spawn ('login :')
child.sendline('admin')
child.sendline('xxxx')

설치

    https://pypi.python.org/pypi/pexpect/ 에서 받거나
    easy_install pip
    pip pexpect
  • 수동 설치
    tar xzvf pexpect-4.0.1.tar.gz
    cd pexpect-4.0.1
    python setup.py install

list에서 Matching 해서 list인덱스 배열로 리턴하기

x=[11,22,33,44]
y=[11,33,22,44,44]
[x.index(i) for i in y if i in y]

list → Dictionary → onehot encoding (enumerate)

  • enumerate로 encoder만들기
a=["a","b","c"]
l={ k:v for k,v in enumerate(a)}
  • list → dictionary → onehot
import numpy as np
dictionary_data=["aa","bb","cc","dd"]

encoder_dic={ k:v for v,k in enumerate(dictionary_data)}
encoder_dic
decoder_dic={ k:v for k,v in enumerate(dictionary_data)}
decoder_dic

org_data=["aa","bb","bb","cc","cc","cc","dd","dd","dd","dd"]
encoded_data=[encoder_dic[i] for i in org_data]
encoded_data

decoded_data=[decoder_dic[i] for i in encoded_data]
decoded_data


one_hot=np.identity( len(encoder_dic.keys()) )

File Read

  • basic Example
#@title
!echo "abcdefg">/tmp/a.txt
with open('/tmp/a.txt','r') as f:
    print(f.readline())
  • 여러줄 읽기
#@title
with open("/tmp/a.txt", 'r') as f:
  while True:
    line = f.readline()
    if not line: break
    print(line)
  f.close()

itertools

효율적인 루핑을 위한 이터레이터를 만드는 함수

import itertools
iterables =[[1,2,3],[88,99],['a','b']]
result = list()
# for i in itertools.product(*iterables):
#   print(list(i))
#   result.append(list(i))
result = [list(i) for i in itertools.product(*iterables)]

두개의 리스트 정렬

import random

a = ['a', 'b', 'c']
b = [1, 2, 3]

c = list(zip(a, b))

random.shuffle(c)

a, b = zip(*c)

print(a)
print(b)