Posts

Showing posts with the label python

public, private, protected in python

 https://www.geeksforgeeks.org/access-modifiers-in-python-public-private-and-protected/

Minimum layout for a library should look like this

  ├── Makefile  ├── README.rst  ├── setup.py  ├── src      └── apptool      ├── common.py │       ├── __init__.py │       └── parse.py  └── tests      ├── integration       └── unit

Format time in python

 https://www.journaldev.com/23365/python-string-to-datetime-strptime#:~:text=We%20can%20convert%20a%20string,datetime%20and%20time%20objects%20respectively. Directive Description Example Output %a Weekday as locale’s abbreviated name. Sun, Mon, …, Sat (en_US) So, Mo, …, Sa (de_DE) %A Weekday as locale’s full name. Sunday, Monday, …, Saturday (en_US) Sonntag, Montag, …, Samstag (de_DE) %w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, 2, 3, 4, 5, 6 %d Day of the month as a zero-padded decimal number. 01, 02, …, 31 %b Month as locale’s abbreviated name. Jan, Feb, …, Dec (en_US) Jan, Feb, …, Dez (de_DE) %B Month as locale’s full name. January, February, …, December (en_US) Januar, Februar, …, Dezember (de_DE) %m Month as a zero-padded decimal number. 01, 02 … 12 %y Year without century as a zero-padded decimal number. 01, 02, … 99 %Y Year with century as a decimal number. 0001, 0002, … , 9999 %H Hour (24-hour clock) as a zero-padded decimal number. 01, 02, … ,...

ThreadPool in Python

  from concurrent . futures import ThreadPoolExecutor import asyncio import time import threading x = 1 def run_forever1 ( lock ): global x while x < 1000 : print ( f'----start1 acc------ { threading . current_thread (). name } -' ) lock . acquire () print ( f'----start1 acced------ { threading . current_thread (). name } -' ) if x < 1000 : print ( f'----run 1------ { threading . current_thread (). name } -' ) x = x + 1 lock . release () print ( f'----start1 release------ { threading . current_thread (). name } -' ) def run_forever2 ( lock ): global x while x < 1000 : print ( f'----start2 acc------ { threading . current_thread (). name } -' ) lock . acquire () print ( f'----start2 acced------ { threading . current_thread (). name } -' ) if x < 1000 : print ( f'----run 2------ { threading . cur...

Decorator in python

def distributed_rate_limit (calls: int , period_in_sec: int , counter: Counter): def outer_wrapper (func): @wraps (func) def inner_wrapper (*args , **kwargs): counter.lock.acquire() now = int (datetime.now().timestamp()) start_time = get_cache_int_value(counter.timer_key , now) range_time = now - start_time # checking range time and reset request count if range_time >= period_in_sec or range_time == 0 : redis.set(counter.counter_key , 0 ) redis.set(counter.timer_key , now) count = get_cache_int_value(counter.counter_key , 0 ) # checking limit calls if count < calls: redis.set(counter.counter_key , count + 1 ) counter.lock.release() return func(*args , **kwargs) counter.lock.release() raise TooManyRequestException( "Too many requests exception...

How to understand and use @property in Python

 https://www.machinelearningplus.com/python/python-property/

How to identity False value in Python

  constants defined to be false:  None  and  False . zero of any numeric type:  0 ,  0.0 ,  0j ,  Decimal(0) ,  Fraction(0,   1) empty sequences and collections:  '' ,  () ,  [] ,  {} ,  set() ,  range(0)

Install Pyenv and virtualenv

Install pyenv in Ubuntu https://www.codegrepper.com/code-examples/shell/install+brew+ubuntu+20.04 https://github.com/pyenv/pyenv/wiki/Common-build-problems Tại sao cần Install Pyenv? Vì pyenv là một phần mềm cho phép bạn có thể cài nhiều version của python đơn giản là  pyenv install 3.7.3 hoặc uninstall bằng cách  pyemv uninstall 3.7.3 sau khi có python version 3.7.3 rồi thì bằng cách setup nó và dùng nó như là python defaut  pyenv global 3.7.3 để kiểm tra version python dùng  which python python -V  để kiểm tra pip which pip  pip -V Oki vậy là đã có python cho máy, sau này cần nâng cấp python version 3.8.2 thì chỉ cần instal 3.8.2 và dùng pyenv global để apply it ------------------------DONE install python-------------- Tiếp theo để dùng python trong dự án thì chúng ta cần thiết lập một môi trường ảo cho nó cài virtualenv pip install --upgrade virtualenv virtualenv .venv vào project và dùng lện  virtualenv  .venv để tạo thư mục .venv cho dự án s...

if x: #x is treated True except for all empty data types [],{},(),'',0 False, and None

  if x: #x is treated True except for all empty data types [],{},(),'',0 False, and None https://stackoverflow.com/a/7825137

First steps for learning python

https://realpython.com/intro-to-pyenv/  for ubuntu  https://opensource.com/article/19/5/python-3-default-mac  for macos You should install python, and pip by pyenv , it is easy to install several versions of python on your machine and it is so easy to switch each version -------------------- install RUST and Cargo  and apply  Rust has multiple compilers and nightly contains some of the more experimental features. I was able to upgrade from stable Rust to nightly following the advice on the  Rust-Lang-Nursery GitHub Page : Install nightly toolchain: $ rustup install nightly Switch to nightly toolchain as default toolchain: $ rustup default nightly