1. Pickle
import pickle
# load or save object using pickle
try:
with open(path_pkl, 'rb') as f: obj_pkl = pickle.load(f)
except:
obj_pkl = []
with open(path_pkl, 'wb') as f: pickle.dump(obj_pkl, f)
2. Requests
import requests
# GET
url = 'http://localhost/test'
params = {'arg1':'1', 'arg2':'2'}
response = requests.get(url=url, params=params).json()
# POST
response = requests.post(url=url, data=json.dumps(params))
3. argparse
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--name", type=str, required=True, help="help")
args = parser.parse_args()
print(args.name)
4. Datetime
from datetime import datetime
datetime.today().strftime("%Y%m%d%H%M%S") # YYYYmmddHHMMSS 형태의 시간 출력
5. Flask
from flask import jsonify, make_response
@application.route('/inference', methods=["GET"])
def infer():
summary = {'class' : 'cat', 'score':'0.92'} # make response data
res = make_response(jsonify(summary), 200) # make Response object
res.headers.add("Access-Control-Allow-Origin", "*") # CORS ERROR 대응
return res
6. Path
# Root path
# Current Working Directory
import os
print(os.getcwd())
7. Counter
from collections import Counter
Counter(['apple','red','apple','red','red','pear'])
>>> Counter({'red': 3, 'apple': 2, 'pear': 1})
8. Limiting floats to N demicmal points
# 1. round
round(1.23456, 4)
>> 1.2346
# 2. Python format
"{:.2f} / {:.3f}".format(1.2345, 1.2345)
>> "1.23 / 1.235"
# 3. Python f-string
num1 = 1.23456
f"{num1:.2f}"
>> "1.23"