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


itertools

from itertools import chain, product, combinations, permutations, groupby, accumulate, islice, cycle

# chain: flatten iterables
list(chain([1,2], [3,4]))                 # [1, 2, 3, 4]

# product: cartesian product
list(product([1,2], ['a','b']))           # [(1,'a'), (1,'b'), (2,'a'), (2,'b')]

# combinations / permutations
list(combinations([1,2,3], 2))            # [(1,2), (1,3), (2,3)]
list(permutations([1,2,3], 2))            # [(1,2), (1,3), (2,1), (2,3), (3,1), (3,2)]

# groupby: consecutive runs (sort first if you want global groups)
data = sorted([('a',1), ('b',2), ('a',3)], key=lambda x: x[0])
{k: [v for _, v in g] for k, g in groupby(data, key=lambda x: x[0])}
# {'a': [1, 3], 'b': [2]}

# accumulate: running totals (or any binary op)
list(accumulate([1,2,3,4]))               # [1, 3, 6, 10]

# islice: slice a generator without materializing
list(islice((x*x for x in range(10)), 2, 6))   # [4, 9, 16, 25]

# cycle: infinite repetition (combine with islice or zip)
list(islice(cycle(['A','B']), 5))         # ['A', 'B', 'A', 'B', 'A']


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