Python #2 | Built In Modules


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)


counter

from collections import Counter
Counter(['apple','red','apple','red','red','pear'])
>>> Counter({'red': 3, 'apple': 2, 'pear': 1})


datetime

from datetime import datetime
datetime.today().strftime("%Y%m%d%H%M%S")    # YYYYmmddHHMMSS 형태의 시간 출력


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


pandas

df = pd.read_pickle('PICKLED_PATH')

df.drop(i) # remove i-th row
df.sort_values(by, ascending=True) # sort 

# Filtering 
df.iloc[[0,1,2,3,4,5]] # get rows by indices # not iloc() => iloc[]
df_new = df.loc[df['Column'].str.contains("sub_str1|sub_str2", case=False)] # Filtering rows that contain either sub_str1 or sub_str2

# Groupby
agg_functions = {'col1':'first', 
                 'col2' : 'sum', 
                 'col3' : lambda col: ' && '.join(col), }
df_new = df.groupby(df['id']).aggregate(agg_functions)


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)


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))


os

os.chdir('/opt/vidClassifier/classifier/')
# sys.path.append('/opt/vidClassifier/classifier')