Forwarded from oleg_log (Oleg Kovalov)
"filter(P, S) is almost always written clearer as [x for x in S if P(x)]"
(с) Guido van Rossum on Python
(с) Guido van Rossum on Python
A tool (and pre-commit hook) to automatically add trailing commas to calls and literals.
tl;dr:
https://github.com/asottile/add-trailing-comma
tl;dr:
# with no trailing commas
x(
- arg
+ arg,
+ arg2
)
# with trailing commas
x(
arg,
+ arg2,
)
https://github.com/asottile/add-trailing-comma
GitHub
GitHub - asottile/add-trailing-comma: A tool (and pre-commit hook) to automatically add trailing commas to calls and literals.
A tool (and pre-commit hook) to automatically add trailing commas to calls and literals. - asottile/add-trailing-comma
После моего доклада на python fwdays обещал рассказать как пользоваться tcpdump:
Andrii Soldatenko, [18.05.20 13:15]
tcpdump example:
1. Я запустил postgresql в докере и пробросли порт в систему (5432)
f9284723a810 postgres "docker-entrypoint.s…" 2 days ago Up 2 days 0.0.0.0:5432->5432
2. запускаем tcpdump:
-X print data of each packet in hex and ascii
3. Логинимся
у меня это md5 - Require the client to supply an MD5-encrypted password for authentication. See Section 19.3.2 for details.
Andrii Soldatenko, [18.05.20 13:15]
tcpdump example:
1. Я запустил postgresql в докере и пробросли порт в систему (5432)
f9284723a810 postgres "docker-entrypoint.s…" 2 days ago Up 2 days 0.0.0.0:5432->5432
2. запускаем tcpdump:
sudo tcpdump -i lo0 -nX dst port 5432важный параметр:
-X print data of each packet in hex and ascii
3. Логинимся
PGPASSWORD=postgres psql -U postgres -h localhost4. То что видим в tcpdump output (некоторые пакеты я пропустил):
13:09:25.020387 IP6 ::1.62337 > ::1.5432: Flags [P.], seq 8:92, ack 2, win 6371, options [nop,nop,TS val 962074756 ecr 962074756], length 84Далее все завист от того как настроен ваш сервер БД (https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html)
0x0000: 6004 334d 0074 0640 0000 0000 0000 0000 `.3M.t.@........
0x0010: 0000 0000 0000 0001 0000 0000 0000 0000 ................
0x0020: 0000 0000 0000 0001 f381 1538 3b74 c631 ...........8;t.1
0x0030: b72a e9e7 8018 18e3 007c 0000 0101 080a .*.......|......
0x0040: 3958 1884 3958 1884 0000 0054 0003 0000 9X..9X.....T....
0x0050: 7573 6572 0070 6f73 7467 7265 7300 6461 user.postgres.da
0x0060: 7461 6261 7365 0070 6f73 7467 7265 7300 tabase.postgres.
0x0070: 6170 706c 6963 6174 696f 6e5f 6e61 6d65 application_name
0x0080: 0070 7371 6c00 636c 6965 6e74 5f65 6e63 .psql.client_enc
0x0090: 6f64 696e 6700 5554 4638 0000 oding.UTF8..
1
13:09:25.022436 IP6 ::1.62337 > ::1.5432: Flags [P.], seq 92:133, ack 15, win 6371, options [nop,nop,TS val 962074758 ecr 962074758], length 41
0x0000: 6004 334d 0049 0640 0000 0000 0000 0000 `.3M.I.@........
0x0010: 0000 0000 0000 0001 0000 0000 0000 0000 ................
0x0020: 0000 0000 0000 0001 f381 1538 3b74 c685 ...........8;t..
0x0030: b72a e9f4 8018 18e3 0051 0000 0101 080a .*.......Q......
0x0040: 3958 1886 3958 1886 7000 0000 286d 6435 9X..9X..p...(md5
0x0050: 6234 3339 6532 3464 3065 6231 3861 3635 b439e24d0eb18a65
0x0060: 6431 6563 3039 6438 3231 3333 3665 6565 d1ec09d821336eee
0x0070: 00 .
1
у меня это md5 - Require the client to supply an MD5-encrypted password for authentication. See Section 19.3.2 for details.
13:09:25.022436 IP6 ::1.62337 > ::1.5432: Flags [P.], seq 92:133, ack 15, win 6371, options [nop,nop,TS val 962074758 ecr 962074758], length 41но бывает там стоит
0x0000: 6004 334d 0049 0640 0000 0000 0000 0000 `.3M.I.@........
0x0010: 0000 0000 0000 0001 0000 0000 0000 0000 ................
0x0020: 0000 0000 0000 0001 f381 1538 3b74 c685 ...........8;t..
0x0030: b72a e9f4 8018 18e3 0051 0000 0101 080a .*.......Q......
0x0040: 3958 1886 3958 1886 7000 0000 286d 6435 9X..9X..p...(md5
0x0050: 6234 3339 6532 3464 3065 6231 3861 3635 b439e24d0eb18a65
0x0060: 6431 6563 3039 6438 3231 3333 3665 6565 d1ec09d821336eee
0x0070: 00 .
1
password - Require the client to supply an unencrypted password for authentication
=)Import Linter allows you to define and enforce rules for the imports within and between Python packages.
Example output:
https://import-linter.readthedocs.io/en/stable/readme.html
[importlinter]
root_package = myproject
[importlinter:contract:1]
name=Foo doesn't import bar or baz
type=forbidden
source_modules=
myproject.foo
forbidden_modules=
myproject.bar
myproject.baz
Example output:
=============
Import Linter
=============
---------
Contracts
---------
Analyzed 23 files, 44 dependencies.
-----------------------------------
Foo doesn't import bar or baz BROKEN
Contracts: 1 broken.
https://import-linter.readthedocs.io/en/stable/readme.html
beta preview of Python 3.9 is out:
- PEP 584, Union Operators in dict
- PEP 585, Type Hinting Generics In Standard Collections
- PEP 593, Flexible function and variable annotations
- PEP 602, Python adopts a stable annual release cadence
- PEP 615, Support for the IANA Time Zone Database in the Standard Library
- PEP 616, String methods to remove prefixes and suffixes
- PEP 617, New PEG parser for CPython
https://www.python.org/downloads/release/python-390b2/
- PEP 584, Union Operators in dict
- PEP 585, Type Hinting Generics In Standard Collections
- PEP 593, Flexible function and variable annotations
- PEP 602, Python adopts a stable annual release cadence
- PEP 615, Support for the IANA Time Zone Database in the Standard Library
- PEP 616, String methods to remove prefixes and suffixes
- PEP 617, New PEG parser for CPython
https://www.python.org/downloads/release/python-390b2/
Python.org
Python Release Python 3.9.0b2
The official home of the Python Programming Language
Restructuring data, the Python way.
happy glomming! 😋
https://glom.readthedocs.io/en/latest/
>>> import glom
>>> glom.assign({}, "a.b.c.d", 42, missing=dict)
{'a': {'b': {'c': {'d': 42}}}}
# Path-based access for nested structures
>>> target = {'a': {'b': {'c': 'd'}}}
>>> glom(target, 'a.b.c')
'd'
happy glomming! 😋
https://glom.readthedocs.io/en/latest/
Forwarded from Sysadmin Tools 🇺🇦
YouTube
Dive into BPF performance tools using python [ru] / Андрей Солдатенко
Видео с онлайн-конференции Python fwdays'20, которая прошла 16 мая 2020 года.
Описание доклада:
Этот доклад — быстрое ознакомления с BPF и различными инструментами python. Это сессия live coding, где Андрей демонстрирует, как реализовать, использовать…
Описание доклада:
Этот доклад — быстрое ознакомления с BPF и различными инструментами python. Это сессия live coding, где Андрей демонстрирует, как реализовать, использовать…
Forwarded from 🇺🇦 Go for two :)
Note #68: The Rules of Optimization Club
1. You do not optimize.
2. You do not optimize, without measuring first.
3. When the performance is not bound by the code, but by external factors, the optimization is over.
4. Only optimize code that already has full unit test coverage.
5. One factor at a time.
6. No unresolved bugs, no schedule pressure.
7. Testing will go on as long as it has to.
8. If this is your first night at Optimization Club, you have to write a test case.
from https://wiki.c2.com/?RulesOfOptimizationClub
1. You do not optimize.
2. You do not optimize, without measuring first.
3. When the performance is not bound by the code, but by external factors, the optimization is over.
4. Only optimize code that already has full unit test coverage.
5. One factor at a time.
6. No unresolved bugs, no schedule pressure.
7. Testing will go on as long as it has to.
8. If this is your first night at Optimization Club, you have to write a test case.
from https://wiki.c2.com/?RulesOfOptimizationClub
PEP 622 -- Structural Pattern Matching
Interesting proposal for python 3.10.
The proposed indentation structure is as following:
and example from pep:
https://www.python.org/dev/peps/pep-0622/
Interesting proposal for python 3.10.
The proposed indentation structure is as following:
match some_expression:
case pattern_1:
...
case pattern_2:
...
and example from pep:
match number:
case 0:
print("Nothing")
case 1:
print("Just one")
case 2:
print("A couple")
case -1:
print("One less than nothing")
case 1-1j:
print("Good luck with that...")
https://www.python.org/dev/peps/pep-0622/
Python Enhancement Proposals (PEPs)
PEP 622 – Structural Pattern Matching | peps.python.org
This PEP proposes to add a pattern matching statement to Python, inspired by similar syntax found in Scala, Erlang, and other languages.
Asynchronous IO Support for Core and ORM
SQLAlchemy 1.4 now supports Python asyncio-compatible database drivers using an all-new asyncio front-end interface to Connection for Core usage as well as Session for ORM use, using the AsyncConnection and AsyncSession objects.
https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#asynchronous-io-support-for-core-and-orm
SQLAlchemy 1.4 now supports Python asyncio-compatible database drivers using an all-new asyncio front-end interface to Connection for Core usage as well as Session for ORM use, using the AsyncConnection and AsyncSession objects.
https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#asynchronous-io-support-for-core-and-orm
Forwarded from 🇺🇦 Go for two :)
Note #71 The seven rules of a great Git commit message
1. Separate subject from body with a blank line
2. Limit the subject line to 50 characters
3. Capitalize the subject line
4. Do not end the subject line with a period
5. Use the imperative mood in the subject line
6. Wrap the body at 72 characters
7. Use the body to explain what and why vs. how
https://chris.beams.io/posts/git-commit/
1. Separate subject from body with a blank line
2. Limit the subject line to 50 characters
3. Capitalize the subject line
4. Do not end the subject line with a period
5. Use the imperative mood in the subject line
6. Wrap the body at 72 characters
7. Use the body to explain what and why vs. how
https://chris.beams.io/posts/git-commit/
Hypermodern Python 👩🚀 🐍
Chapter 1: Setup
Chapter 2: Testing
Chapter 3: Linting
Chapter 4: Typing
Chapter 5: Documentation
Chapter 6: CI/CD
https://cjolowicz.github.io/posts/hypermodern-python-01-setup/
Chapter 1: Setup
Chapter 2: Testing
Chapter 3: Linting
Chapter 4: Typing
Chapter 5: Documentation
Chapter 6: CI/CD
https://cjolowicz.github.io/posts/hypermodern-python-01-setup/
Releasing pip 20.3, featuring new dependency resolver
https://pyfound.blogspot.com/2020/11/pip-20-3-new-resolver.html
https://pyfound.blogspot.com/2020/11/pip-20-3-new-resolver.html
Python Software Foundation Blog
Releasing pip 20.3, featuring new dependency resolver
On behalf of the Python Packaging Authority and the pip team, I am pleased to announce that we have just released pip 20.3 , a new versio...
Interesting thread how to solve interview problem https://twitter.com/al_grigor/status/1357028887209902088?s=21
Twitter
Alexey Grigorev
Most candidates cannot solve this interview problem: 🔸 Input: "aaaabbbcca" 🔸 Output: [("a", 4), ("b", 3), ("c", 2), ("a", 1)] Write a function that converts the input to the output I ask it in the screening interview and give it 25 minutes How would you solve…
Armin dislikes new PEP regarding structural pattern matching https://twitter.com/mitsuhiko/status/1359263135304187910
btw PEP has been approved!
https://www.python.org/dev/peps/pep-0636/
btw PEP has been approved!
https://www.python.org/dev/peps/pep-0636/
Twitter
Armin Ronacher
I really, really, really don't want to be dismissive of all the new python language features but it's hard not to see faults in PEPs like this: https://t.co/fIvSTSEcxl — pattern matching is awesome, but only when the language is built for it.
I can't recall where I saw this originally, but you can use the spaceship operator "--0--" to turn floor division into ceiling division:
>>> 12//5
2
>>> --0-- 12//5
3
There's also the ++0++ operator when you want to emphasize that you really *do* mean floor division:
>>> ++0++ 12//5
2
from https://bugs.python.org/issue43255#msg387248
But yes, -(-n // d) is the easy (if a little bit cryptic) way to get the ceiling of n / d.
>>> 12//5
2
>>> --0-- 12//5
3
There's also the ++0++ operator when you want to emphasize that you really *do* mean floor division:
>>> ++0++ 12//5
2
from https://bugs.python.org/issue43255#msg387248
But yes, -(-n // d) is the easy (if a little bit cryptic) way to get the ceiling of n / d.
🇺🇦 Python Programming Сhallenges
I can't recall where I saw this originally, but you can use the spaceship operator "--0--" to turn floor division into ceiling division: >>> 12//5 2 >>> --0-- 12//5 3 There's also the ++0++ operator when you want to emphasize that you really *do* mean floor…
It's just a trick
It's because unary minus is right-associative and binds tighter than binary minus which is left-associative
-12 // 5 => - 3
- (-12 // 5) => +3
The rest is just putting 0 at the start:
--0 == 0
and --0--12//5 is parsed as
-(-(0)) - (- (12 // 5))
from Twitter https://twitter.com/ericpromislow/status/1362475189125095425?s=21
It's because unary minus is right-associative and binds tighter than binary minus which is left-associative
-12 // 5 => - 3
- (-12 // 5) => +3
The rest is just putting 0 at the start:
--0 == 0
and --0--12//5 is parsed as
-(-(0)) - (- (12 // 5))
from Twitter https://twitter.com/ericpromislow/status/1362475189125095425?s=21
Twitter
ericpromislow
@professorcraven @VictorStinner @nedbat It's just a trick It's because unary minus is right-associative and binds tighter than binary minus which is left-associative -12 // 5 => - 3 - (-12 // 5) => +3 The rest is just putting 0 at the start: --0 == 0 and…
Forwarded from 🇺🇦 Go for two :)
Performance comparison: counting words in Python, Go, C++, C, AWK, Forth, and Rust
https://benhoyt.com/writings/count-words/
https://benhoyt.com/writings/count-words/
Benhoyt
Performance comparison: counting words in Python, Go, C++, C, AWK, Forth, and Rust
Performance comparison of counting and sorting word frequencies in various languages (Python, Go, C++, C, AWK, Forth, and Rust)