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
os
os.chdir('/opt/vidClassifier/classifier/')
# sys.path.append('/opt/vidClassifier/classifier')
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))
typing
-
typing.Union
- python>3.10 :
str|bytes
rather than Union[str|bytes]
-
typing.Optional
- python>3.10 : Use
str|None
rather than Optional[str, None]
-
typing.List
, typing.Tuple
, typing.Dict
- python>3.9 : Use
list[str]
, tuple[int, int]
, dict[str, str]
-
typing.TypedDict
: for dict with multiple type, you can use typedDict
- python>3.7 : Use dataclass
-
typing.Generator
, typing.Iterable
, typing.Iterator
, typing.Callable
- python>3.9 : Use
collections.abc.Generator
, collections.abc.Iterator
, collections.abc.Iterable
, collections.abc.Callable
-
typing.TypeVar
: for generic
from typing import TypedDict
class Person(TypedDict):
name: str
age: int
def calc_cost(person: Person) -> float:
pass
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
def calc_cost(person: Person) -> float:
pass