Skip to content

Instantly share code, notes, and snippets.

@osisdie
osisdie / 01_invalid_json_serialization.py
Created October 20, 2022 14:10
[python3] invalid json serialization with custom hooks
"""
Reference: https://pydantic-docs.helpmanual.io/usage/exporting_models/#advanced-include-and-exclude
Purpose: workaround for not able to deserialize/serialize the include_keys/exclude_keys object from the example above
"""
import datetime
import json
import re
from typing import List
from pydantic import BaseModel, SecretStr
@osisdie
osisdie / 01_jq_pre-process.sh
Created August 21, 2022 14:29
[bash] jq for json file process
#!/usr/bin/bash
# usage
# export JSON_DIR=tests/data
# export RULES=rule1,rule2,rule3
# ./jq_pre-process.sh
JSON_DIR=${JSON_DIR:-tests/data}
if [ -z "$JSON_DIR" ]; then
echo "ERROR: missing env varible JSON_DIR"
@osisdie
osisdie / 01_dirname_vs_basename.sh
Created June 27, 2022 03:57
[bash] grep folder name
$ python -c "import sys; print(sys.prefix)"
C:\Users\kevin\writable\.pyenv\pyenv-win\versions\3.9.12
# Wrong folder name due to backslash
$ dirname $(python -c "import sys; print(sys.prefix)")
C:\Users\kevin\writable\.pyenv\pyenv-win\versions
# Correct by fix slash on the path
$ basename $(python -c "import sys; print(sys.prefix)")
3.9.12
@osisdie
osisdie / 01_grep_foreach_json_fie.sh
Last active June 20, 2022 12:10
[bash] foreach json file check content
#!/usr/bin/sh
# GOTO for bash, based upon https://stackoverflow.com/a/31269848/5353461
goto() {
label=$1
cmd=$(sed -En "/^[[:space:]]*#[[:space:]]*$label:[[:space:]]*#/{:a;n;p;ba};" "$0")
eval "$cmd"
exit
}

Tested only on Ubuntu 20.04, KDE Neon User Edition (based on Ubuntu 20.04) and OSX Mojave.

will probably work on other newer versions, with no changes, or with few changes in non-python dependencies (apt-get packages)

NOTE: Don't create a .sh file and run it all at once. It will not work. Copy, paste, and execute each command below manually. :-)

Ubuntu

# DO NOT RUN THIS AS A ROOT USER
@osisdie
osisdie / 01_play_with_decoration_order.py
Last active June 17, 2022 02:22
[python3] python decoration
if __name__ == '__main__':
demo = Demo()
try:
demo.call_alice()
except Exception:
print('Error') # Hello3 -> Hello2 -> Hello1 -> Alice
# Error
try:
hello1_attribute(hello2_attribute(demo.call_alice))()
@osisdie
osisdie / 01_invoice.py
Created May 26, 2022 11:24
[python3] regex to named group
import re
pattern = re.compile(r"^(?P<year>\d{3})(?P<month>\d{2})(?P<invoice>[a-zA-Z]{2}\d{8})(?P<code>.{4})$")
matching = list(re.finditer(pattern, '11106AY616994809730'))
matching[0]
# <re.Match object; span=(0, 19), match='11106AY616994809730'>
matching[0].re.groupindex
# mappingproxy({'year': 1, 'month': 2, 'invoice': 3, 'code': 4})
@osisdie
osisdie / 01_hello_pyscript.html
Last active May 12, 2022 13:38
[pyscript] JavaScript + Python Script
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>PyScript</title>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- numpy
@osisdie
osisdie / 01_none_safe_in_python.md
Last active January 26, 2024 13:41
[python3] Null safe expressions