Python
Contents
Project Setup - venv
- Why to use :
- to install modules independently
- to manage modules independently
- How
-
setup
virtual environmentpython -m venv venv
-
activate
virtual environment# In Windows venv\Scripts\activate # In Mac or Linux source venv/bin/activate
-
manage
modules# install pip install {module_name} # check the list of modules pip list # get the list within txt file pip freeze > requirements.txt # install module list using txt file pip install -r requirements.txt
-
deactivate
virtunal environmentdeactivate
-
Docstring Convention
-
function
def function_name(param1: int, param2: str) -> bool: """ 한 줄 요약: 함수가 수행하는 작업을 간단히 설명합니다. 여러 줄 설명: 함수의 동작, 사용법, 특별한 조건 등을 설명합니다. 예를 들어, param1은 X를 의미하며, param2는 Y의 역할을 합니다. Args: param1 (int): 설명1. param2 (str): 설명2. Returns: bool: 반환값에 대한 설명. """ return True
-
class
class ClassName: """ 클래스 요약: 클래스의 역할이나 목적을 간단히 설명합니다. 클래스의 동작, 속성, 주요 메서드 등을 상세히 기술합니다. 예를 들어, 이 클래스는 X 작업을 수행하며 Y 속성을 관리합니다. Attributes: attr1 (type): 속성1의 설명. attr2 (type): 속성2의 설명. """ def __init__(self, param1: int, param2: str) -> None: """ 클래스의 생성자: 인스턴스를 초기화합니다. Args: param1 (int): 설명1. param2 (str): 설명2. """ self.param1 = param1 self.param2 = param2
Absolute Import vs Relative Import
- Two terms are for where does code start to track
- Absolute Import
- start from
root directory
of project
- start from
- Relative Import
- start from
current script file
- start from
In python2
, there was a confliction issue between original module name and created module name. So for the sake of avoiding the confliction, need to usefrom __future__ import absolute_import
In python3
, this condition or setup become default, so this isnot necessary
Module
__future__
Deferred evaluation by storing module as string
-
This is for preventing the codes from circular import
from typing import TYPE_CHECKING if TYPE_CHECKING: from mod1 import School class Student: def register_school(school: 'School'): pass
typing
Union
- This represents that parameter type and return type can be one of several specific types
-
|
could be used instead of Union from3.10
onwardsfrom typing import Union def parse_data(data: Union[str, list]) -> list: if isinstance(data, str): return data.split(",") # 문자열을 리스트로 변환 return data def get_data() -> Union[str, dict]: # 조건에 따라 반환 타입이 달라질 수 있음 if some_condition: return "data as string" return {"key": "value"}
pandas
- CRUD
- C : create
- create empty dataframe
- create dataframe from list of dict
- R : read
- search row by condition ( column name cell value )
- selecting and indexing from Dataframe to sub dataframe ( filtering )
- read and access exact cell
- U : update
- add row with dictionary
- D : delete
- C : create
- Utility
- DataFrame.copy()
- DataFrame.to_dict(“index”)
- DataFrame.to_dict(orient=”records”)
Create
- Create table
def create_table(default_info :dict) -> pd.DataFrame: return pd.DataFrame(default_info) default_header = {"task_name":[], "task_id":[], "assignee":[], "entity_id":[]} table = create_table(default_header) print(table) # ============= # Print results # ============= # Empty DataFrame # Columns: [task_name, task_id, assignee, entity_id] # Index: []
Update
- Add rows
def add_row_to_table(tar_table :pd.DataFrame, input_info :dict) -> None: tar_table.loc[len(tar_table)] = input_info assignee = [{"name":"송태영"}] assignee_str = dumps(assignee) input_infos = [ {"task_name":"fx01", "task_id":123, "assignee":assignee_str, "entity_id":456}, {"task_name":"fx02", "task_id":111, "assignee":assignee_str, "entity_id":333}, {"task_name":"fx03", "task_id":122, "assignee":assignee_str, "entity_id":555}, ] for _info in input_infos: add_row_to_table(table, _info) print(table) # ============= # Print results # ============= # task_name task_id assignee entity_id # 0 fx01 123 [{"name": "\uc1a1\ud0dc\uc601"}] 456 # 1 fx02 111 [{"name": "\uc1a1\ud0dc\uc601"}] 333 # 2 fx03 122 [{"name": "\uc1a1\ud0dc\uc601"}] 555
Read
-
Search row by condition ( filtering )
def search_in_table(tar_table :pd.DataFrame, col_name :str, input_info :str) -> pd.DataFrame: return tar_table.loc[tar_table[col_name] == input_info] res = search_in_table(table, "task_id", 123) print(res)
-
Select and Index from Dataframe to sub dataframe ( filtering )
# 한개의 column print(table["task_name"] ) # 선택한 column 들 print(table[["task_name", "entity_id"]] ) # 한개의 row print(table.loc[[1]] ) # 선택한 row들 print(table.loc[[0,2]])
-
Access exact cell
print(table.iloc[0]["task_name"]) print(loads(table.iloc[0]["assignee"])) print(table.to_dict("index")) print(table.to_dict(orient='records')) # ============= # Print results # ============= # fx01 # [{'name': '송태영'}] # {0: {'task_name': 'fx01', 'task_id': 123, 'assignee': '[{"name": "\\uc1a1\\ud0dc\\uc601"}]', 'entity_id': 456}} # [{'task_name': 'fx01', 'task_id': 123, 'assignee': '[{"name": "\\uc1a1\\ud0dc\\uc601"}]', 'entity_id': 456}]