2021/07/13
That pickle.loads(pickle.dumps(obj)) is faster than copy.deepcopy(obj)
Learned from Simon Willison’s twitter
That pickle.loads(pickle.dumps(obj)) is faster than copy.deepcopy(obj)
Learned from Simon Willison’s twitter
that running python with the -O flag disables assert statements
about a new source of standard-ish python functionality: https://github.com/mahmoud/boltons
you can import from a parent-module using from ... import .
That python f-strings can do
f"{today:%Y-%m-%d}"f"{x = }, {y = }"__repr__ rather than __str__: f"{user!r}"f"output: {value:{width}.{precision}}"
https://martinheinz.dev/blog/70Of the window.prompt() method:
https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt
To prefer async context managers or asyncio.run() instead of creating+managing event loops natively.
That sql has assertions:
CREATE ASSERTION <Constraint name>
CHECK (search condition)
[ <constraint attributes> ]
see https://crate.io/docs/sql-99/en/latest/chapters/20.html#create-assertion-statement.
h/t https://www.scattered-thoughts.net/log/0024/ for bringing that to my attention.
Also, from the weekend: in postgres, at least, you can call TABLE :table_name; directly to SELECT * FROM :table_name;
Also: how to look up a domain name from an ip:
reverse_ip_lookup() {
ip_address="$1"
dig -x $ip_address +noall +answer
}
Also: how to audit AWS VPC flow logs from CloudWatch Log Insights: use the example queries in the right sidebar.
Also: python can execute .zip files directly, like so:
:; echo 'print("Hello, World!")' > __main__.py
:; zip hello-world.zip __main__.py
:; python3 ./hello-world.zip
## Hello, World!
h/t https://pradeepchhetri.xyz/til/pythonzip/ for pointing that out.
that you can write raise SomeException() from prev_exception: see
https://docs.python.org/3/reference/simple_stmts.html#raise
that you can easily send asyncio tasks to threads:
#!/usr/bin/env python3
import asyncio
from asyncio.threads import to_thread
async def foo(bar: str):
asyncio.sleep
print(bar)
asyncio.run(to_thread(foo, "baz"))
that you can set PYTHONDONTWRITEBYTECODE=1 to prevent python from writing pycs (see https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE)
Also: that AWS SSO does SCIM actions using the sso-directory:* IAM prefix rather than the sso:* prefix.
That in Rust, assert! runs in both debug and release builds. For debug-only assertions, use debug_assert!. See https://doc.rust-lang.org/stable/std/macro.debug_assert.html.
That in Python, you can call isinstance(thing, (tuple, OfMany, DifferentClasses)) to check if a variable is an instance of many possible classes.
See https://docs.python.org/3/library/functions.html?highlight=isinstance#isinstance.
That properties of a NamedTuple can have docstrings:
class FooBar(NamedTuple):
foo: str
"""Foo"""
bar: int
"""Bar"""
As seen in https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/sql/compiler.py#L348
How to type a decorator function or decorator factory in Python: see https://mypy.readthedocs.io/en/stable/generics.html##declaring-decorators
That Python mangles class attributes named like __name:
Python mangles these names with the class name: if class
Foohas an attribute named__a, it cannot be accessed byFoo.__a. (An insistent user could still gain access by callingFoo._Foo__a.) Generally, double leading underscores should be used only to avoid name conflicts with attributes in classes designed to be subclassed.Note: there is some controversy about the use of
__names(see below).– https://peps.python.org/pep-0008/#method-names-and-instance-variables
Designing for Inheritance
Always decide whether a class’s methods and instance variables (collectively: “attributes”) should be public or non-public. If in doubt, choose non-public; it’s easier to make it public later than to make a public attribute non-public.
Public attributes are those that you expect unrelated clients of your class to use, with your commitment to avoid backwards incompatible changes. Non-public attributes are those that are not intended to be used by third parties; you make no guarantees that non-public attributes won’t change or even be removed.
We don’t use the term “private” here, since no attribute is really private in Python (without a generally unnecessary amount of work).
Another category of attributes are those that are part of the “subclass API” (often called “protected” in other languages). Some classes are designed to be inherited from, either to extend or modify aspects of the class’s behavior. When designing such a class, take care to make explicit decisions about which attributes are public, which are part of the subclass API, and which are truly only to be used by your base class.
With this in mind, here are the Pythonic guidelines:
Public attributes should have no leading underscores.
If your public attribute name collides with a reserved keyword, append a single trailing underscore to your attribute name. This is preferable to an abbreviation or corrupted spelling. (However, notwithstanding this rule, ‘cls’ is the preferred spelling for any variable or argument which is known to be a class, especially the first argument to a class method.)
Note 1: See the argument name recommendation above for class methods.
For simple public data attributes, it is best to expose just the attribute name, without complicated accessor/mutator methods. Keep in mind that Python provides an easy path to future enhancement, should you find that a simple data attribute needs to grow functional behavior. In that case, use properties to hide functional implementation behind simple data attribute access syntax.
Note 1: Try to keep the functional behavior side-effect free, although side-effects such as caching are generally fine.
Note 2: Avoid using properties for computationally expensive operations; the attribute notation makes the caller believe that access is (relatively) cheap.
If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores. This invokes Python’s name mangling algorithm, where the name of the class is mangled into the attribute name. This helps avoid attribute name collisions should subclasses inadvertently contain attributes with the same name.
Note 1: Note that only the simple class name is used in the mangled name, so if a subclass chooses both the same class name and attribute name, you can still get name collisions.
Note 2: Name mangling can make certain uses, such as debugging and
__getattr__(), less convenient. However the name mangling algorithm is well documented and easy to perform manually.Note 3: Not everyone likes name mangling. Try to balance the need to avoid accidental name clashes with potential use by advanced callers.
– https://peps.python.org/pep-0008/#designing-for-inheritance
That if you specify an Entry-points key in python .dist-info/METADATA, python will create command-line wrappers for your scripts.
See https://packaging.python.org/en/latest/specifications/entry-points/
That virtualenvs may contain two files that alter the sys.path outside of the $PYTHONPATH variable:
.venv/pyvenv.cfg (see https://docs.python.org/3/library/sys_path_init.html#virtual-environments).venv/python3.*/site-packages/*.pth (see https://docs.python.org/3/library/sys_path_init.html#pth-files)That python dicts have an upsert method: https://docs.python.org/3/library/stdtypes.html#dict.setdefault
That on windows, creating a virtualenv:
$VIRTUAL_ENV/Scripts rather than $VIRTUAL_ENV/binpython.exe symlink, not a python3.exe symlink.See https://docs.python.org/3/library/venv.html#creating-virtual-environments
Less exciting, but I also (re-)learned that setting a PYTHONPATH on windows requires using ; rather than : as the path-separator.
About python’s __debug__ constant: https://docs.python.org/3/library/constants.html#debug__
It’s true unless python is run in -O mode, stripping out assertions and other unoptimized code.
that pypi packages all have