Utilities¶
Overview¶
From time to time, I usually read internal codebase of public repository, then I come up with some best practice overthere that help me a lot in the time of coding.
Code¶
To avoid de-duplicated on search, using set for list¶
Pattern
pool = set() # Define empty set
for element in basket:
...
if <condition>: # Defile condition matching
pool.update(element)
...
# Then we can convert to list or yield from set
for runner in pool:
# ... do something
# ...
# or convert back to list
pool = list(pool)
Example:
def _list_folders(self, bkt, prefix, artifact_path):
results = bkt.list_blobs(prefix=prefix, delimiter="/")
dir_paths = set()
for page in results.pages:
dir_paths.update(page.prefixes)
Validate python version¶
Valiudate specific version in application. The snippet can put in __init__.py or entrypoint script.
```py indent=4 title="init.py"
!/bin/python3¶
Gloabal¶
import sys
Declare¶
required_major, required_minor, _ = "3.9.x".split(".")
Extract¶
major, minor, micro, releaselevel, serial = sys.version_info
Required¶
if not all([major >= int(required_major), minor >= int(required_minor)]): raise RuntimeError( f"Python version required to >= {required_major}.{required_minor}.x release. " f"Got Python version {'.'.join([str(x) for x in sys.version_info])}" )
### Detect the system platform identifier
From `sys` library, enumuration of system platforms:
Table 1: System Platform Encoding
| Platform | Identifier |
| -------------- | ---------- |
| AIX | 'aix' |
| Linux | 'linux' |
| Windows | 'win32' |
| Windows/Cygwin | 'cygwin' |
| macOS | 'darwin' |
```py
import sys
if sys.platform == "linux":
...
elif sys.platform.starswith("cy"):
...
One Line Punch¶
One line punch is a 1 line command in python to do various tasks.
Print the python version¶
python -c "import sys; print(sys.version)"
# 3.12.8 (main, Jan 14 2025, 22:49:36) [MSC v.1942 64 bit (AMD64)]
Print the system timestamp¶
Hash an target IP¶
python -c "print('.'.join([val if ind != 2 else '___' for ind, val in enumerate('196.168.5.3'.split('.'))]))"
# yield '196.168.___.3'
Lambda with default argument¶
To declare lambda with default argument, using following syntax
lambda arg1=value1, arg2=value2, ..., argN=valueN : expression
# with:
# arg1, arg2, argN – the names of the arguments that the lambda expression uses;
# value1, value2, valueN – values that are assigned respectively to the arguments arg1, arg2, argN;
# expression – the lambda expression itself.
For example:
summ = lambda a=1, b=2, c=3: a+b+c
print("summ() = ", summ()) # 1+2+3 = 6
print("summ(10) = ", summ(10)) # 10+2+3 = 15
print("summ(10, 20) = ", summ(10,20)) # 10+20+3 = 33
print("summ(10, 20, 30) = ", summ(10,20,30)) # 10+20+30 = 60
If you do not specify the values of the arguments when calling the lambda expression, then they will be assigned the default values value1, …, valueN. If you specify only part of the arguments, then this part will replace the default value starting from the leftmost argument (arg1).
If you declare a lambda expression that takes 3 arguments by default, and call the lambda expression with 1 argument, then the changes will affect the leftmost argument. The following example demonstrates the rule for overriding default arguments when invoking a lambda expression.
Reference: https://www.bestprog.net/en/2021/06/17/python-default-arguments-in-lambda-expressions/