본문 바로가기

분류 전체보기

(667)
[Python] Magic Method(2) 클래스 샘플2 class Vector(object): def __init__(self, *args): # function Doctring '''(init)Create a vector, example : v = Vector(5,10)''' if len(args) == 0: self._x, self._y = 0, 0 else: self._x, self._y = args # un-packing def __repr__(self): '''(repr)Returns the vector infomations''' return '(repr)Vector(%r, %r)' % (self._x, self._y) def __add__(self, other): '''(add)Returns the vector addition of ..
[Python] Magic Method(1) 클래스 예제 class Fruit: def __init__(self, name, price): self._name = name self._price = price def __str__(self): return '(str)Fruit Class Info : {} , {}'.format(self._name, self._price) def __ge__(self, x): print('Called >> __ge__ Method.') if self._price >= x._price: return True else: return False def __le__(self, x): print('Called >> __le__ Method.') if self._price = s2) print(s1 > __ge__ Method...
[Python] Class(3) - Class Method, Static Method Instance method, Class method, Static method # 클래스 선언 class Car(object): ''' Car Class Author : Me Date : Description : Class, Static Method, Class Method ''' # Class Variable(모든 인스턴스가 공유 가능) price_per_raise = 1.0 def __init__(self, company, details): self._company = company self._details = details def __str__(self): return 'str : {} - {}'.format(self._company, self._details) def __repr__(self):..
[Python] Class(2) - self, 매직 매소드 # 클래스 재 선언 class Car(): """ Car Class """ # 클래스 변수 car_count = 0 def __init__(self, company, details): self._company = company # 인스턴스 변수 self._details = details Car.car_count += 1 # 클래스 변수 def __str__(self): return 'str : {} - {}'.format(self._company, self._details) def __repr__(self): return 'repr : {} - {}'.format(self._company, self._details) def detail_info(self): print('Current Id : {}'.fo..
[Python] Class(1) - Basic print(car1) 과 print(car_list) 의 차이에 주의할 것 print(car1) - str 로 출력 print(car_list) - repr 로 출력 class Car(): def __init__(self, company, details): self._company = company self._details = details def __str__(self): # 매직 메소드, 간단한 사용자 레벨의 출력 return 'str : {} - {}'.format(self._company, self._details) def __repr__(self): # 매직 메소드, 엄격한 개발자 레벨의 출력 return 'repr : {} - {}'.format(self._company, self._detai..
CPU Bound(2) - Multi-process CPU 연산 - 멀티 프로세스 from multiprocessing import current_process, Array, freeze_support, Process, Manager import time import os def cpu_bound(number, total_list): process_id = os.getpid() process_name = current_process().name # Process 정보 출력 print(f"Process ID: {process_id}, Process Name: {process_name}") total_list.append(sum(i * i for i in range(number))) def main(): numbers = [3_000_000 + x for x..
CPU Bound(1) - Single process CPU 연산 - 단일 프로세스 import time def cpu_bound(number): return sum(i * i for i in range(number)) def find_sums(numbers): result = [] for number in numbers: result.append(cpu_bound(number)) return result def main(): numbers = [3_000_000 + x for x in range(30)] # 확인 # print(numbers) start_time = time.time() total = find_sums(numbers) print() print(f"Total list : {total}") print(f"Sum : {sum(total)}") ..
[Python] I/O Bound(2) - Asyncio 통신관련 비동기 처리 import asyncio import aiohttp # import requests # 패키지는 동기방식이라서 사용불가. import time async def request_site(session, url): # 세션 확인 # print(session) # print(session.headers) async with session.get(url) as response: # status_code 동기화 문제 print("Read Contents {}, from {}, stauts:{}".format(response.content_length, url, response.status)) # print("*****Read Contents {0}, from {1}".format(respo..