개발/Python(4)
-
20200924 - Python Thread pool, Cache, Mocking snowflake cursor
class SFExecutionPool: def __init__(self): self.func_li = [] def add(self, func, **kwargs): self.func_li.append((func, kwargs)) def execute(self): with ThreadPool(len(self.func_li)) as pool: multiple_results = [ pool.apply_async(func=func, kwds=kwargs) for func, kwargs in self.func_li ] results = [res.get() for res in multiple_results] return results 1. Python Thread pool 유명한 사실이지만 python은 싱글 스레..
2020.09.24 -
20200728 python 관련 사소한 팁
1.@property python을 해본 사람은 누구나 알듯이 public, private같은 접근 제어자가 없다. 그래서 다른 언어에서는 private으로 쓸 것을 변수 앞에 밑줄 두개(__)를 써서, protected는 밑줄 하나(_)를 써서 표현한다. 보통 다른 언어에서 이들을 가져오고 싶거나 세팅하고 싶을 때 get, set 함수를 쓴다. python에서 이를 똑같이 하면 다음과 같다. class Everything: def __init__(self): self.__name = "any name" def get_name(self): return self.__name def set_name(self, new_name:str): self.__name = new_name 하지만 이런 식으로 하면 상당히..
2020.07.28 -
20200312 함수 내 import, exception
1. 함수 내 import 별건 아니고, 회사에서 쓰던 코드 보면 가끔 씩 함수 내에서 library를 import하는 경우가 있어 의아해서 찾아봤다. 그랬더니 https://hashcode.co.kr/questions/1054/import%EB%8A%94-%EA%BC%AD-%EC%BD%94%EB%93%9C-%EB%A7%A8-%EC%9C%97%EC%A4%84%EC%97%90-%EC%8D%A8%EC%95%BC-%ED%95%98%EB%82%98%EC%9A%94 import는 꼭 코드 맨 윗줄에 써야 하나요? 보통 모듈을 import할 때는 소스코드1보다는 소스코드2같이 맨 윗줄에 넣는 것 같던데이 클래스안에서만 쓰이는 모듈이라면 굳이 맨 윗줄에 import해 줄 필요는 없을 것 같은데 다들 맨 윗줄에서 im..
2020.03.12 -
재미로 Python으로 Linked list 구현해보기
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = Node("head") self.last = self.head self.numData = 0 self.sorted = False def append(self, data): newNode = Node(data) self.last.next = newNode self.last = newNode self.numData += 1 self.sorted = False def slice(self, idx1, idx2): assert idx1 >= 1 and idx2 = idx1 - 1 and i < id..
2020.02.01