python.module

Haks.·2024년 12월 15일
0

How to use

목록 보기
5/32

👍 자주 사용되는 모듈 정리

📌 re


정규 표현식의 기초, 메타 문자

메타 문자란 원래 그 문자가 가진 뜻이 아니라 특별한 의미를 가진 문자를 의미,
정규 표현식에 다음과 같은 메타 문자를 사용하면 특별한 의미를 가지게 된다

  • . ^ $ * + ? { } [ ] \ | ( )
* [] : 문자클래스를 만드는 메타 문자, 어떤 문자도 들어갈 수 있다.
ex) [abc] 라면 a,b,c 중 한개의 문자와 매치를 의미한다 
before 은 b가 있으므로 매치
dude 는 abc 중 어느 하나도 없으므로 매치 x
[] 안에 - 하이픈을 사용하면 범위를 나타낸다 a-c = abc 이다

* ^와 $ → 문자열의 시작과 끝
def is_english(text):
    pattern = r"^[A-Za-z]+$"  # 영어만 허용
    return bool(re.match(pattern, text))
    
* 공백을 포함해도 되려면 \s 추가
def is_english_with_space(text):
    pattern = r"^[A-Za-z\s]+$"  # 영어 + 공백 허용
    return bool(re.match(pattern, text))

* .(dot) \n 을 제외한 모든 문자와 매치
ex) a.b 는 a + 모든문자 + b 를 의미한다. 즉 a와 b 사이에 어떤 글자가 들어가도 매치
pattern = r"a.b"  # a와 b 사이에 어떤 문자든 하나만 있으면 매치
print(bool(re.search(pattern, "acb")))  # True ('c'가 사이에 있음)
print(bool(re.search(pattern, "ab")))   # False (중간에 아무것도 없음)

* {} 반복 문자 고정
ex) 
ca{2}t c + a를 반드시 2번 반복 + t
cat 만 매치
pattern = r"ca{2}t"  # 'c' + 'a'가 2번 + 't'
print(bool(re.search(pattern, "caat")))   # True
print(bool(re.search(pattern, "cat")))    # False ('a'가 1번만 등장)

* ca{2,5}t 2~5회 반복시 매치
pattern = r"ca{2,5}t"  # 'a'가 2~5번 반복
print(bool(re.search(pattern, "caaat")))  # True ('a'가 3번)
print(bool(re.search(pattern, "cat")))    # False ('a'가 1번만 등장)


* \d : 숫자를 의미하며,[0-9]와 동일
pattern = r"\d"  # 숫자가 포함되어 있으면 매치
print(bool(re.search(pattern, "hello123")))  # True (숫자 포함)
print(bool(re.search(pattern, "hello")))     # False (숫자 없음)

* + : 바로앞의 패턴이 하나 이상 연속으로 반복됨을 의미
pattern = r"a+"  # 'a'가 1개 이상 반복되면 매치
print(bool(re.search(pattern, "aaaa")))  # True
print(bool(re.search(pattern, "b")))     # False ('a' 없음)

* * : 무한대로 반복될수 있다는 의미
pattern = r"ab*c"  # 'a' + ('b'가 0개 이상) + 'c'
print(bool(re.search(pattern, "ac")))     # True ('b' 없이 매치 가능)
print(bool(re.search(pattern, "abbc")))   # True ('b' 여러 개 가능)
print(bool(re.search(pattern, "adc")))    # False ('b'가 아닌 다른 문자가 중간에 있음)
  • match() : 문자열의 처음부터 시작해서 작성한 패턴이 일치하는지 확인
import re
print(re.match('a','ab'))  # 매치
print(re.match('a','bba')) # b로시작하여 매치 실패
  • serch() : 처음부터 일지하지 않아도 매치
import re
print(re.search('a','ab'))  # 매치
print(re.search('a','bba')) # 매치
    def validate_password(cls, value):
    if len(value) < 8:
        raise ValueError("Password must be at least 8 characters long")
    if not re.search(r"[A-Z]", value):
        raise ValueError("Password must contain at least one uppercase letter")
    if not re.search(r"\d", value):
        raise ValueError("Password must contain at least one number")
    if not re.search(r"[!@#$%^&*(),.?\":{}|<>]", value):
        raise ValueError("Password must contain at least one special character")
    return value
  • findall() : 문자열 안에 패턴에 맞는 케이스를 전부 찾아 리스트로 반환
print(re.findall('a','a')) # ['a']
print(re.findall('aaa','aaaaa')) # ['aaa']
print(re.findall('aaa','aaaaaa')) # ['aaa','aaa']
print(re.findall('a','baa')) # ['a','a']
print(re.findall('\d','숫자 123이 이렇게 56 있다8'))
# ['1','2','3','5','6','8']
print(re.findall('\d+', 숫자 123이 이렇게 56 있다8'))
# ['123','56','8']
  • finditer() : 패턴에 맞는 문자열으 ㅣ리스트가아닌 iterator 형식으로 반환
  • fullmatch() : 처음부터 패턴에 맞으면 반환 처음 과 끝이 일치해야함
  • compile(패턴,플래그) : 패턴과 플래그가 동일한 정규식을 여러번 사용하려면 사용

📌 collections


자료형을 도와주는 모듈

  • Counter : count = collections.Counter(iterable), 딕셔너리에 특화된 클래스
from collections import Counter
lst = ['aa', 'cc', 'dd', 'aa', 'bb', 'ee']
print(collections.Counter(lst)) # 쓰려면 변수로 선언 
# Counter({'aa': 2, 'cc': 1, 'dd': 1, 'bb': 1, 'ee': 1}) 출력
  • deque : dq = deque('asdf'), 각문자가 요소로 된 문자열로 반환, 리스트 메소드 사용가능
  • defaultdict : 값을 안넣으면 해당 고정 타입으로 넣어준다,정수면 0
from collections import defaultdict
int_dict = defaultdict(int)
int_dict['key1'] # key1 이라는 key 갑생성 벨류는 안넣음 = 0
int_dict['key2'] = 'test' # key2 : test
  • OrderedDict : 중복제거
from collections OrderedDict 
ex1 = OrderedDict.fromkeys(x) # 중복 제거
  • namedtuple : 튜플 형식의 key,value 변수 선언
from collections import namedtuple
Point = namedtuple(Point,'x y')
pt = Point(1.0,3.0) x = 1.0, y = 3.0
  • ChainMap

📌 heapq(힙큐)


  • heapq.heappush(heap, item) : item을 heap에 추가
  • heapq.heappop(heap) : heap에서 가장 작은 원소를 pop & 리턴. 비어 있는 경우 IndexError가 호출됨.
  • heapq.heapify(x) : 리스트 x를 즉각적으로 heap으로 변환함 (in linear time, O(N) )
import heapq

heap = []
heapq.heappush(heap, 50) # 
heap2 = [50 ,10, 20]
heapq.heapify(heap2) # 이미 존재하는 리스트 힙화
result = heapq.heappop(heap) # 삭제 후 반환

print(heap)

heap_items = [1,3,5,7,9]

max_heap = []
for item in heap_items:
  heapq.heappush(max_heap, (-item, item))

print(max_heap)

📌 fucntools


코드 재사용 및 최적화를 위해 사용하는 모듈

  • parital : 함수의 인수를 고정하여 새로운 함수를 생성. 이를통해 매번 동일한 인수를 반복해서 입력할 필요 없이 함수 호출을 단순화 가능
from functools import partial

def multiply(x, y) :
	return x * y
# y 값을 2로 고정한 새로운 함수 생성
double = partial(multiply, y=2)
print(double(5)) # 10
print(double(7)) # 14
  • lru_cache : Least Recently Used(LRU), 동일한 입력값에 대해 계산된 결과를 캐시하여 함수의 실행 속도 상승 가능
from functools import lru_cache
import time
@lru_cache(maxsize=None)
def slow_function(n) :
	time.sleep(2) # 계산이 오래 걸리는 함수 시뮬레이션
    return n * n
    
start = time.time()
print(slow_function(4)) # 처음 호출 , 2초지연
print(time.time() - start)

stat = time.time()
print(slow_function(4)) # 캐시된 결과 사용 , 지연 없음 !!,전결과 저장했다 사용하는것
print(time.time() - start)
slow_function.cache_clear() # 캐싱 저장 삭제
  • cmp_to_key : 키 함수로 전환
def keyfunc():
	pass
key_func = cmp_to_key(keyfunc)
sorted_x = sorted(x, key = key_func)
  • reduce : 함수와 iterable 을 인수로 받아, iterable 요소들을 누적적으로 적용하여 단일 결과를 반환
from functools import reduce
numbers = [1,2,3,4,5]

# 누적 합계 계산
total = reduce(lambda x, y : x+y, numbers) 
print(total) # 15
  • total_ordering(데코레이터) : 클래스에 비교 연산자 메소드를 최소한으로 구현하여 나머지 비교 연산자를 자동으로 추가해 주는 데코레이터 , 중복을 줄이고 클래스 정의를 간결하게 만들 수 있음.
from functools import total_ordering
@total_ordering
class Person :
	def __init__(self,name,age) :
    	self.name = name
        slef.age = age
    def __eq__(self, other) :
    	if isinstance(other, Person) :
        	return self.age == other.age
        return NotImplemented
    def __lt__(self, other) :
    	if isinstance(other, Person) :
        	return self.age < other.age
        return NotImplemented
    def __repr__(self) :
    	return f'Person(name = {self.name}, age = {self.age}'
person1 = Person('Chill', 30)
person2 = Person('Bob', 25)
person3 = Person('Hoon', 30)

print(person1 == person2) # False
print(person1 == person3) # True
print(person1 < person2)  # False
print(person2 < person1)  # True
print(person1 > person2)  # True
  • wraps(데코레이터) : 원래 함수의 메타데이터(이름,문서 문자열) 을 유지하기 위해 사용됨

📌 itertools


효율적인 루핑을 위한 이터레이터를 만드는 함수

  • combinations(iterable, r) : 원소개수가 r 개인 조합 뽑기 ,조합
from itertools import combinations
list = [1, 2, 3]
for i in combinations(list, 2) : # 3C2
	print(i)
print(list(map(''.join, combinations(list,2))))
(1, 2)
(1, 3)
(2, 3)
[(1, 2), (1, 3), (2, 3)]
  • permutations (순열)
from itertools import permutations

list = ['A', 'B', 'C']

for i in permutations(list): #r을 지정하지 않거나 r=None으로 하면 최대 길이의 순열이 리턴된다.
	print(i)
print(list(map(''.join, itertools.permutations(list))))
print(list(map(''.join, itertools.permutations(list, 2)))) #2개의 원소로 수열 만들기
('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')
['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']

['AB', 'AC', 'BA', 'BC', 'CA', 'CB']

📌 sys


파이썬 인터프리터를 제어하는데 사용되는 기본 모듈

📌 math


  • math.perm(x,y) :xPy
  • math.comb(x,y) :xCy
  • math.ceil(3.2) : 올림함수 # 4
  • math.floor() : 내림함수
  • math.fabs(-3.14) : 절대값 반환 # 3.14
  • math.factorial(5) : # 120
  • math.gcd(6,8) : 최대 공약수 # 2
  • math.lcm(3,4) : 최소 공배수 # 13
  • math.trunc(x) : 0으로 내리는 함수 음수면 + , 양수면 -
  • math.log(10,10)
  • math.log2(x) : e를 밑으로 하는 x+1 로그
  • math.log10(x) : 10을 밑으로 하는 x 로그
  • math.pow(x,y) : x에 y승
  • math.sqrt(25) : 제곱근 # 5.0
  • math.sin,cos,tan(x)
  • math.degrees(x)
  • math.radians(x)
  • math.pi # 원주율
  • math.e # 자연상수 2.7~~~
  • math.tau # 타우 6.2~~

📌 datetime


  • 현재 시간 가져오기, UTC 기준
from datetime import datetime, timezone
now = datetime.now()
utc_now = datetime.utcnow()
utc_now = datetime.now(timezone.utc)  # 타임존 정보 포함
  • 날짜 생성 (특정 날짜 만들기)
custom_date = datetime(2025, 2, 17, 15, 30, 45)
# 2025-02-17 15:30:45
  • 날짜 포맷 변환 (strftime)
now = datetime.now()
format_date = now.strftime("%Y-%m-%d %H:%M:%S")
# "2025-02-17 15:30:00"
# %Y: 연도(4자리), %y: 연도(2자리)
# %H: 시(24시간제), %I: 시(12시간제)
# %p: AM/PM
  • 문자열 -> 날자 변환(strptime)
date_str = "2025-02-17 15:30:00"
date_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
# 2025-02-17 15:30:00
  • 날짜 차이 계산(timedelta)
from datetime import timedelta

today = datetime.now()

one_month_later = today  + timedelta(months=1)
# 현재 날짜 + 1달 후

three_days_later = today + timedelta(days=3)
# 현재 날짜 + 3일 후

one_week_ago = today - timedelta(weeks=1)
# 현재 날짜 - 1주 전
  • 두 날짜의 차이 계산
date1 = datetime(2025, 2, 17)
date2 = datetime(2025, 3, 1)

diff = date2 - date1
print(diff.days)
  • 날짜만 가져오기(date)
today = date.today()
print(today)  # 2025-02-17
  • 타임스탬프 변환(timestamp)
now = datetime.now()
timestamp = now.timestamp()  # 현재 시간을 Unix Timestamp로 변환
print(timestamp)  # 1739784600.123456

# 타임스탬프 -> 날짜
date_from_timestamp = datetime.fromtimestamp(timestamp)
print(date_from_timestamp)  # 2025-02-17 15:30:00
  • 요일 확인(weekday())
today = datetime.today()
print(today.weekday())  # 0 (월요일: 0, 일요일: 6)
  • UTC -> 로컬 시간 변환(astimezone)
from datetime import timezone

utc_now = datetime.now(timezone.utc)  # UTC 기준 시간
local_now = utc_now.astimezone()  # 시스템 로컬 시간으로 변환
print(local_now)

📌 UUID


  • uuid4() -> 랜덤값 기반 (가장 많이 사용), 충돌 확률 낮음, 1은 네트워크 기반
import uuid
uuid_4 = uuid.uuid4()
print(uuid_4)  # 예: 2a737d89-f74b-4c45-9e41-5d369e1f5e56
  • uuid3()/5() -> 고정된 네임스페이스 기반
uuid_3 = uuid.uuid3(uuid.NAMESPACE_DNS, "example.com")
uuid_5 = uuid.uuid5(uuid.NAMESPACE_DNS, "example.com")

print(uuid_3)  # 예: 5df41881-3aed-3515-88a7-2f4a814cf09e
print(uuid_5)  # 예: 2ed6657d-e927-568b-95e1-2665a8aea6a2
  • UUID객체를 문자열로 변환 str()
uuid_str = str(uuid.uuid4())
print(uuid_str)  # 예: '2a737d89-f74b-4c45-9e41-5d369e1f5e56'
  • 바이트로 저장, 공간절약 가능(데이터베이스에 유용)
uuid_bytes = uuid.uuid4().bytes
print(uuid_bytes)  # 예: b'\x9e\x18\xd7\xcdK\x94J\xcf\x98\xcf\xf3\xc0\x10\xe8\xb1\x12'

📌 random


  • 정수 난수 생성(randint, randrange)
import random

print(random.randint(1, 10))  # 1~10 사이의 랜덤 정수 (포함)
print(random.randrange(1, 10, 2))  # 1~9 사이에서 2 간격으로 선택 (예: 1, 3, 5, 7, 9 중 하나)
  • 실수 난수 생성(random, uniform)
print(random.random())  # 0.0 ~ 1.0 사이의 랜덤 실수
print(random.uniform(1.5, 6.5))  # 1.5 ~ 6.5 사이의 랜덤 실수
  • 리스트에서 랜덤 선택(choice, choices, sample)
colors = ["red", "blue", "green", "yellow"]

print(random.choice(colors))  # 리스트에서 랜덤 요소 1개 선택
print(random.choices(colors, k=2))  # 리스트에서 중복 가능하게 2개 선택
print(random.sample(colors, k=2))  # 리스트에서 중복 없이 2개 선택
  • 리스트 섞기(shuffle)
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)  # 리스트 순서 랜덤 변경
  • 시드값 고정(seed), 매 실행마다 랜덤 결과를 얻고 싶을때 사용
random.seed(42)  # 랜덤 시드 고정
print(random.randint(1, 10))  # 항상 같은 결과가 나옴
print(random.random())  # 실행할 때마다 동일한 랜덤 값

📌 os


  • 운영체제와 상호작용하는 기능
  • 파일 시스템 조작, 환경 변수 접근, 프로세스 관리 등에 사용..
  • 파일 및 디렉토리 조작
import os

# 현재 작업 디렉토리 확인 및 변경
print(os.getcwd())  # 현재 작업 디렉토리 출력
os.chdir('/path/to/directory')  # 작업 디렉토리 변경

# 파일 및 폴더 생성, 삭제
os.mkdir('new_folder')  # 새로운 디렉토리 생성
os.makedirs('parent_folder/child_folder')  # 부모 폴더 포함하여 디렉토리 생성
os.rmdir('new_folder')  # 빈 디렉토리 삭제
os.removedirs('parent_folder/child_folder')  # 상위 폴더까지 삭제
os.remove('file.txt') 

# 파일 및 폴더 존재 여부 확인
os.path.exists('file.txt')  # 파일 또는 폴더가 존재하는지 확인 (True/False)
os.path.isfile('file.txt')  # 파일인지 확인
os.path.isdir('my_folder')  # 폴더인지 확인

# 디렉토리 내 파일 리스트 가져오기
print(os.listdir('.'))  # 현재 디렉토리의 파일 목록 출력
  • 경로 관련 함수(os.path)
# 경로 합치기
path = os.path.join('folder', 'subfolder', 'file.txt')
print(path)  # 'folder/subfolder/file.txt' (운영체제에 따라 다름

# 절대 경로 변환
print(os.path.abspath('file.txt'))  # 절대 경로 출력

# 경로 분리
path = '/home/user/docs/file.txt'
print(os.path.dirname(path))  # '/home/user/docs'
print(os.path.basename(path))  # 'file.txt'

# 파일 확장자 확인
filename, extension = os.path.splitext('document.pdf')
print(filename)   # 'document'
print(extension)  # '.pdf'
  • 환경 변수 다루기
print(os.environ['HOME'])  # 사용자 홈 디렉토리
os.environ['MY_VAR'] = '123'  # 환경 변수 설정
print(os.getenv('MY_VAR'))  # 환경 변수 가져오기 (없으면 None 반환)
  • 시스템 명령어 실행
# 터미널 명령어 실행(os.system)
os.system('ls -l')  # UNIX: 파일 목록 출력
os.system('dir')  # Windows: 파일 목록 출력

# 명령 실행 결과 가져오기 (os.popen)
output = os.popen('ls').read()
print(output)  # 파일 목록 출력
  • 프로세스 관련 함수
# 현재 프로세스 ID 확인
print(os.getpid())  # 현재 실행 중인 프로세스 ID 출력

# 부모 프로세스 ID 확인
print(os.getppid())  # 부모 프로세스 ID 출력

# 사용자 ID 확인
print(os.getlogin())  # 현재 로그인한 사용자 이름 출력
  • 운영체제 정보확인
print(os.name)  # 현재 OS 종류 ('posix' (Linux/macOS) 또는 'nt' (Windows))
print(os.uname())  # OS 상세 정보 (Linux/macOS 전용)

✨Tip


  • insert(x,y) : x번째 y 추가 x번째 가 없으면 젤끝에 추가

0개의 댓글

관련 채용 정보