일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Substring with Concatenation of All Words
- concurrency
- Python Code
- Protocol
- 파이썬
- Convert Sorted List to Binary Search Tree
- Regular Expression
- 109. Convert Sorted List to Binary Search Tree
- Python
- kaggle
- DWG
- LeetCode
- iterator
- 프로그래머스
- 밴픽
- 컴퓨터의 구조
- 715. Range Module
- attribute
- 315. Count of Smaller Numbers After Self
- 30. Substring with Concatenation of All Words
- Generator
- shiba
- Python Implementation
- data science
- t1
- 43. Multiply Strings
- 시바견
- Class
- 운영체제
- Decorator
- Today
- Total
목록Python (33)
Scribbling
Thread vs Asyncio 스레드나 코루틴을 통해 콘솔 에니메이션을 구현할 수 있다. 먼저 스레드를 이용하는 코드이다. Console animation can be implemented with either threads or coroutines. First, with thread. import threading import itertools import time import sys class Signal: go = True def spin(msg, signal): write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/-\\'): status = char + ' ' + msg write(status) flus..
Web downloads in three different styles - sequential download - concurrent.futures - asyncio With heavy input/output workload, we can make the best out of concurrency. Below is the code that downloads flag gif files sequentially. import os import time import sys import requests POP20_CC = ('CN IN US ID BR PK NK BD RU JP' 'MX PH VN ET EG DE IR TR CD FR').split() BASE_URL = 'http://flupy.org/data/..
Basic behavior of Coroutines def coroutine(): print('started') x = yield print('received: ', x) c = coroutine() next(c) c.send(3) 1) "next(c)" or "c.send(None)" primes the coroutine -> coroutine now waits at 'yield' expression 2) c.send(3) sets x to 3, re-executing the coroutine 3) At the end, coroutine raises StopIteration Example: coroutine to compute a running average def averager(): total, c..
Else block For-Else Else block is executed when the 'for loop' was not stopped by any break. for i in range(1, 1): else: print('Else block is executed') for i in range(1, 10): if i == 8: break else: print('Else block is executed') While-Else Works the same for 'for-else'. while False: break else: print('Else block executed') Try-Else try: dangerous_call() after_call() except OSError: log('OSErro..
Sequence Protocol To make a custom data type have Sequence Protocol, you need to implement "__iter__" method. Even though "__getitem__" method is enough for now, you should implement "__iter__" method as well for later compatibility. A classic iterator Below is a classic implementation - not a rolemodel - of an iterator. import re import reprlib class Sentence: def __init__(self, text): self.tex..
In this post, we add operator overloading code to custom 'Vector' class in the previous post. https://focalpoint.tistory.com/300 Python: Sequence Protocol To learn sequence protocol in Python, we create a custom vector class. from array import array import math import reprlib class Vector: typecode = 'd' def __init__(self, components): self._component.. focalpoint.tistory.com Vector Class is as ..
Risks of subclassing built-in types - Generally, overriden methods in sub-classes are not called by other built-in methods. class StrangeDict(dict): def __setitem__(self, key, value): super().__setitem__(key, value*2) dic = StrangeDict(one=1) dic['1'] = 1 dic.update(three=3) print(dic) - This is because some methods are written with C-language. - So, use UserDict, UserList, UserString if you nee..
What is interface? Interface is a set of public methods. In python, 'X object', 'X protocol' and 'X interface' have the same meaning. In Python, protocol is pretty dynamic. In below example, FrenchDeck class does not inherit any class (other than default 'object' class). However, the class has sequential protocol as it has two magic methods (__len__, __getitem__). Likewise, in Python, object's d..
The main goals we use 'Abstract Base Class' in python are as follows: - to provide a standerdized way to test whether an object adheres to a certain specification - to prevent any attempt to initiate a subclass that does not override methods of the super class 1> To provide a standerdized way to test whether an object adheres to a certain specification import abc class TypeGroup1(metaclass=abc.A..
To learn sequence protocol in Python, we create a custom vector class. from array import array import math import reprlib class Vector: typecode = 'd' def __init__(self, components): self._components = array(self.typecode, components) def __iter__(self): return iter(self._components) def __repr__(self): components = reprlib.repr(self._components) components = components[components.find('['):-1] ..