category: prog, direction: python, group: django
Python
python
источники:
unittest — Unit testing framework
Категория: Программирование. Направление: Python. Группа: Django.
Добавлено: 02-02-2025. Обновлено: 07-03-2025
Ключи: #python
только важные выдержки
python
Тезисы
2
postfix admin
argon2-cffi: Argon2 for Python
pip install argon2-cffi
from argon2 import PasswordHasher
ph = PasswordHasher()
hash = ph.hash("correct horse battery staple")
hash # doctest: +SKIP
# '$argon2id$v=19$m=65536,t=3,p=4$MIIRqgvgQbgj220jfp0MPA$YfwJSVjtjSU0zzV/P3S9nnQ/USre2wvJMjfCIjrTQbg'
ph.verify(hash, "correct horse battery staple")
# True
ph.check_needs_rehash(hash)
# False
ph.verify(hash, "Tr0ub4dor&3")
# Traceback (most recent call last):
# ...
# argon2.exceptions.VerifyMismatchError: The password does not match the supplied hash
ph = argon2.PasswordHasher()
def login(db, user, password):
hash = db.get_password_hash_for_user(user)
# Verify password, raises exception if wrong.
ph.verify(hash, password)
# Now that we have the cleartext password,
# check the hash's parameters and if outdated,
# rehash the user's password in the database.
if ph.check_needs_rehash(hash):
db.set_password_hash_for_user(user, ph.hash(password))
Beautiful Soup
Beautiful Soup Documentation
Документация Beautiful Soup
Beautiful Soup Documentation
pip install beautifulsoup4
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())
soup.p
# <p class="title"><b>The Dormouse's story</b></p>
soup.p['class']
soup.find_all('a')
pypandoc
pip install pypandoc
pip install panflute
import io
import pypandoc
import panflute
def action(elem, doc):
if isinstance(elem, panflute.Image):
doc.images.append(elem)
elif isinstance(elem, panflute.Link):
doc.links.append(elem)
if __name__ == '__main__':
data = pypandoc.convert_file('example.md', 'json')
doc = panflute.load(io.StringIO(data))
doc.images = []
doc.links = []
doc = panflute.run_filter(action, prepare=prepare, doc=doc)
print("\nList of image URLs:")
for image in doc.images:
print(image.url)
NamedTuple
dataclass
TypedDict
Enum
Literal
Union, Optional
Iterable, Sequence
Callable
TypeVar
type hinting
>>> a,b,c = input.tream().split()
>>> c = ' 1 3 4 5 '
>>> c = map(int, c.split())
>>> import functools as f
>>> z = list( map( print, dir(f) ) )
>>> c = {
... "d":lambda x: print('d:',x),
... "e":lambda x: print('e:',x),
... "f":lambda x: print('f:',x),
... }
>>> c['d']('z');c['e']('z');c['f']('z')
члены НАТО, но и "крупные союзники" вне блока, такие как Аргентина, Австралия, Колумбия, Израиль, Япония, Кения, Марокко, Новая Зеландия, Катар, Южная Корея и Тунис
https://otus.ru/lessons/ml-specialization/?utm_source=tgads&utm_medium=cpc&utm_campaign=spec_ml&utm_term=pythonbooksru&utm_content=lesson_13-01-2025_usp-universal_spec_ml_text_no-headline_text_short_aibanner_banner-hl1_blue_standart#event-5203
MYSQLAlchemy Alembic
json.dumps
arr = []
json.dumps(arr, indent=4, ensure_ascii=False)
indent (int | str | None) – If a positive integer or string, JSON array elements and object members will be pretty-printed with that indent level. A positive integer indents that many spaces per level; a string (such as "\t") is used to indent each level. If zero, negative, or "" (the empty string), only newlines are inserted. If None (the default), the most compact representation is used.
ensure_ascii (bool) – If True (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If False, these characters will be outputted as-is.
json — JSON encoder and decoder
dict update
dict.update(dict)
# 3.9+
dict = dict | dict2
dict |= dict2
Некоторые встроенные функции словарей
Метод | Описание |
---|---|
all() | Returns True if all the items in an iterable are truthy and False otherwise. |
any() | Returns True if at least one element in the iterable is truthy and False otherwise. |
len() | Returns an integer representing the number of items in the input object. |
max() | Returns the largest value in an iterable or series of arguments. |
min() | Returns the smallest value in an iterable or series of arguments. |
sorted() | Returns a new sorted list of the elements in the iterable. |
sum() | Returns the sum of a start value and the values in the input iterable from left to right. |
Merging two Dictionaries using the Union Operator [Python 3.9+]
Python functions
String methods
Метод | Описание |
---|---|
capitalize() | Converts the first character to upper case |
casefold() | Converts string into lower case |
center() | Returns a centered string |
count() | Returns the number of times a specified value occurs in a string |
encode() | Returns an encoded version of the string |
endswith() | Returns true if the string ends with the specified value |
expandtabs() | Sets the tab size of the string |
find() | Searches the string for a specified value and returns the position of where it was found |
format() | Formats specified values in a string |
format_map() | Formats specified values in a string |
index() | Searches the string for a specified value and returns the position of where it was found |
isalnum() | Returns True if all characters in the string are alphanumeric |
isalpha() | Returns True if all characters in the string are in the alphabet |
isascii() | Returns True if all characters in the string are ascii characters |
isdecimal() | Returns True if all characters in the string are decimals |
isdigit() | Returns True if all characters in the string are digits |
isidentifier() | Returns True if the string is an identifier |
islower() | Returns True if all characters in the string are lower case |
isnumeric() | Returns True if all characters in the string are numeric |
isprintable() | Returns True if all characters in the string are printable |
isspace() | Returns True if all characters in the string are whitespaces |
istitle() | Returns True if the string follows the rules of a title |
isupper() | Returns True if all characters in the string are upper case |
join() | Converts the elements of an iterable into a string |
ljust() | Returns a left justified version of the string |
lower() | Converts a string into lower case |
lstrip() | Returns a left trim version of the string |
maketrans() | Returns a translation table to be used in translations |
partition() | Returns a tuple where the string is parted into three parts |
replace() | Returns a string where a specified value is replaced with a specified value |
rfind() | Searches the string for a specified value and returns the last position of where it was found |
rindex() | Searches the string for a specified value and returns the last position of where it was found |
rjust() | Returns a right justified version of the string |
rpartition() | Returns a tuple where the string is parted into three parts |
rsplit() | Splits the string at the specified separator, and returns a list |
rstrip() | Returns a right trim version of the string |
split() | Splits the string at the specified separator, and returns a list |
splitlines() | Splits the string at line breaks and returns a list |
startswith() | Returns true if the string starts with the specified value |
strip() | Returns a trimmed version of the string |
swapcase() | Swaps cases, lower case becomes upper case and vice versa |
title() | Converts the first character of each word to upper case |
translate() | Returns a translated string |
upper() | Converts a string into upper case |
zfill() | Fills the string with a specified number of 0 values at the beginning |
Заметка: Все методы строк возвращают новое значение. Они не изменяют исходную строку.
Note: All string methods returns new values. They do not change the original string.
w3schools: Python String Methods
List/Array Methods
Python has a set of built-in methods that you can use on lists/arrays.
Python имеет набор встроенных методов, которые вы можете использовать списками/массивами.
Метод | Описание |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Заметка: Python не имеет встроенных функций для поддержки массиво, но Python Lists может быть использован вместо них.
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
w3schools: Python List/Array Methods
REGEXP
import re
p = re.compile('\([A-Z]+\)')
s = 'some (msk) (d) (D) (DST)'
r = p.search(s)
r.group()
m = re.match(r'(?P<first>\w+) (?P<last>\w+)', 'Jane Doe')
m.groupdict() # {'first': 'Jane', 'last': 'Doe'}
r = p.search(s)
Медоты поиска: match, search, findall, finditer, groupdict()
Method/Attribute | Purpose |
---|---|
match() | Determine if the RE matches at the beginning of the string. |
search() | Scan through a string, looking for any location where this RE matches. |
findall() | Find all substrings where the RE matches, and returns them as a list. |
finditer() | Find all substrings where the RE matches, and returns them as an iterator. |
Методы результата:
Method/Attribute | Purpose |
---|---|
group() | Return the string matched by the RE |
start() | Return the starting position of the match |
end() | Return the ending position of the match |
span() | Return a tuple containing the (start, end) positions of the match |
groups() | Returns a tuple containing the strings for all the subgroups, from 1 up to however many there are. |
Modifying Strings Up to this point, we’ve simply performed searches against a static string. Regular expressions are also commonly used to modify strings in various ways, using the following pattern methods:
Method/Attribute | Purpose |
---|---|
split() | Split the string into a list, splitting it wherever the RE matches |
sub() | Find all substrings where the RE matches, and replace them with a different string |
subn() | Does the same thing as sub(), but returns the new string and the number of replacements |
print(re.search('^From', 'From Here to Eternity'))
print(re.search('^From', 'Reciting From Memory'))
docs.python.org: Regular Expression HOWTO
DateTime – strptime()
Конвертация даты в определённый формат с коррекцией на 3 часа.
Подходит под любой часовой пояс.
import re
from datetime import datetime, timedelta
time_data = 'Wed, 23 Feb 2022 01:52:43 +0000'
time_data2 = 'Wed, 19 Feb 2025 11:17:36 +0300 (MSK)'
def date_fix(time_data):
# убираем лишний суфикс
res = re.search(r' \([a-zA-Z]+\)', time_data )
if res:
time_data = time_data.replace(res.group(), '')
# парсим дату
format_data = "%a, %d %b %Y %H:%M:%S %z"
date = datetime.strptime(time_data, format_data)
print('date:', date, time_data, format_data, sep=' | ')
# 2025-02-19 10:33:02+03:00
# корректируем сдвиг времени
date = date - date.utcoffset()
date = (date + timedelta(0, (3 * 3600)) )
# приводим к нужному формату
# день недели, число месяц, время
date = date.strftime("%w %d-%m %H:%M")
return date
date_fix(time_data) # 3 23-02 04:52
date_fix(time_data2) # 3 19-02 11:17
geeksforgeeks: Python DateTime – strptime() Function
geeksforgeeks: Python datetime.utcoffset() Method with Example
FastAPI стриминг файлов
Работа с файлами в FastAPI — Загрузка и умное скачивание
получить номер строки
#python3
from inspect import currentframe, getframeinfo
frameinfo = getframeinfo(currentframe())
print(frameinfo.filename, frameinfo.lineno)
декодировать формат b''
str = b'some'
pritn(str) # b'some'
print(str.decode()) # some
print(str.decode('UTF-8')) # some
GIL
Global Interpreter Lock — это способ синхронизации потоков, который используется в некоторых интерпретируемых языках программирования, например в Python и Ruby.
WIKI: Глобальная блокировка интерпретатора
GIL — это глобальный блокировщик интерпретатора Python (Global Interpreter Lock). Он позволяет только одному потоку выполнять Python-код в любой момент, чтобы не дать двум и более потокам одновременно получить доступ к важным объектам. Проще говоря — не допустить состояние гонки. Если попытаться принудительно разделить выполнение программы на потоки и запустить их одновременно, GIL не позволит это сделать.
Чтобы было понятнее, что происходит дальше, поясним один термин: «поток захватывает GIL». Это значит, что поток получает добро на своё выполнение, а GIL блокирует все остальные потоки и запрещает им работать, пока тот поток не освободит GIL. Когда GIL свободен, его может захватить любой поток — как именно и в каком порядке, зависит от логики.
bash in python
# fast call
import os
s.system('pwd; echo "=============="')
# fast call with output
out = os.popen(cmd).read()
print(out)
# with catch error output
from subprocess import PIPE, Popen
p = Popen("foo", shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
print "stdout: '%s'" % stdout
print "stderr: '%s'" % stderr
Двойной коммит
Хабр: Двухфазный коммит и будущее распределённых систем
Распределённые транзакции
Calculator
def calc(_s=[]):
'''
get arifmetical string
return calculated number
15:00 - 15:59
'''
if not len(_s): return f'no todo';
s2 = [s for s in list(_s)if not s.isspace()]
a=[];o=[];ap=0;op=0;
#print('init:',a,o,ap,op)
#print('s2:',s2)
isOp = False
for s in s2:
#print('s2[]:',s)
if s.isalnum():
if len(a) < (ap+1):
a.append([])
a[ap].append(s)
isOp=False
else:
if len(o) < (op+1):
o.append([])
if isOp:
o[(op-1)].append(s)
else:
o[op].append(s)
op=op+1
if not isOp: ap=ap+1
isOp=True
#print('fill:',a,o,ap,op)
r=[]
for s in a:
if isinstance(s,list):
s = int(''.join(s))
r.append(s)
a=r
r=[]
for s in o:
s = (''.join(s))
r.append(s)
o=r
#print('full:',a,o,ap,op)
res = 0
__o={
'+':lambda a,b: a+b,
'-':lambda a,b: a-b,
'*':lambda a,b: a*b,
'/':lambda a,b: a/b,
'**':lambda a,b: a**b,
'%':lambda a,b: a%b,
'//':lambda a,b: a//b,
'+':lambda a,b: a+b,
};
for i in range(len(a)-1):
_o=o[i]
#print('_o',_o)
if _o not in __o:
return f'phrase "{_s}" containst un calculated operator(s)'
if i==0:
res=a[i]
#print(a[i])
#print(_o)
#print(a[i+1])
res = __o[_o](res, a[i+1])
return res
# calc('1+1')
calc('2**23')
calc('9%2')
assert calc('20+20') == 40
functools
functools — Higher-order functions and operations on callable objects
debug: get current function name
import inspect
def foo():
# will give the caller of foos name, if something called foo
print(inspect.stack()[0][3])
print(inspect.stack()[1][3])
# file:func:line
frameinfo = getframeinfo(currentframe())
print(frameinfo.filename,inspect.stack()[0][3], \
frameinfo.lineno,sep=':')
# Номер строки вызова функции
def line():
frame = inspect.stack()[1]
fn = frame.filename.split('/')
fn = '/'+fn[-2]+'/'+fn[-1]
print("\n","\r"+fn,frame[3], frame.lineno,sep=':', end='\n\n')
# выделяем строку от остального текста
def _print(*args,**kwargs):
print("\nd\r",*args,**kwargs, end="\n\n")
requirements
# export modules
pip freeze > requirements.txt
# import modules
pip install -r requirements.txt
time
# 2025-02-19T20:20:27.707950+00:00
datetime(2002, 12, 25, tzinfo=TZ()).isoformat()