Skip to content

Instantly share code, notes, and snippets.

View wonderbeyond's full-sized avatar
🎯
Focusing

Wonder wonderbeyond

🎯
Focusing
View GitHub Profile
@wonderbeyond
wonderbeyond / password.py
Last active February 17, 2023 07:37
password validation
import re
PASSWORD_MIN_LENGTH = 8
PASSWORD_MAX_LENGTH = 128
PASSWORD_MIN_UNIQUE_CHARS = 5
PASSWORD_MAX_REPEATED_CHARS = 3
MAX_CONTINUOUS_INCREASING_OR_DECREASING_SEQUENCES = 3
class PasswordValidationError(Exception):
@wonderbeyond
wonderbeyond / sed-replace-apt-source-list.sh
Created January 17, 2023 01:56
Replace apt source mirror using sed
sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
@wonderbeyond
wonderbeyond / install-swagger-codegen.sh
Created December 3, 2022 11:47
Install the swagger-codegen-cli
#!/usr/bin/env bash
set -euo pipefail
PREFIX=${PREFIX:-~/.local}
mkdir -p $PREFIX/bin $PREFIX/lib
curl -L https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.36/swagger-codegen-cli-3.0.36.jar \
-o $PREFIX/lib/swagger-codegen-cli.jar
echo '#!/usr/bin/env bash' > $PREFIX/bin/swagger-codegen-cli
@wonderbeyond
wonderbeyond / asyncio.py
Created November 28, 2022 06:39
[asyncio] async 2 sync
from functools import wraps
import asyncio
def get_event_loop():
try:
return asyncio.get_event_loop()
except RuntimeError as e:
if "There is no current event loop in thread" in str(e):
loop = asyncio.new_event_loop()
@wonderbeyond
wonderbeyond / camel_case_to_snake_case.py
Created November 23, 2022 11:03
[Python] camel case to (fat) snake case
def camel_case_to_snake_case(s: str, use_fat: bool = False) -> str:
"""
>>> camel_case_to_snake_case("ProcedureContextStatus")
"procedure_context_status"
>>> camel_case_to_snake_case("ProcedureContextStatus", use_fat=True)
"PROCEDURE_CONTEXT_STATUS"
"""
chars = [s[0].upper() if use_fat else s[0].lower()]
for c in s[1:]:
if c in ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
@wonderbeyond
wonderbeyond / async.py
Last active November 23, 2022 01:04 — forked from phizaz/async.py
Python turn sync functions to async (and async to sync)
from functools import wraps
import asyncio
def get_event_loop():
try:
return asyncio.get_event_loop()
except RuntimeError as e:
if "There is no current event loop in thread" in str(e):
loop = asyncio.new_event_loop()
@wonderbeyond
wonderbeyond / poetry-inst.sh
Last active February 1, 2023 03:18
Install poetry with CI/CD environment
#!/usr/bin/env bash
# Mainly used with GitLab CI/CD
# Original source link of this file:
# https://gist.github.com/wonderbeyond/434d3622c2db3d5ff00b4ba4a2a667bc
set -euo pipefail
export POETRY_HOME=~/poetry
command -v curl > /dev/null || apt-get install -y curl
@wonderbeyond
wonderbeyond / wait_port_ready.py
Last active November 11, 2022 01:42
Waiting for a TCP port ready to accept connections
"""Thanks to https://gist.github.com/butla/2d9a4c0f35ea47b7452156c96a4e7b12"""
import socket
import time
def wait_port_ready(port: int, host: str = 'localhost', timeout: float = 5.0):
"""
Wait until a port starts accepting TCP connections.
@wonderbeyond
wonderbeyond / deep_data.py
Last active September 30, 2022 10:34
[Python][deep data] Handle (get/set) nested python dicts and lists.
from typing import Union, Any, List, Dict, Iterable
import functools
def _get(data, key: Union[str, int], default=None) -> Any:
"""
Get value from dict or list by key (integer or string).
"""
if isinstance(data, dict):
return data.get(key, default)
@wonderbeyond
wonderbeyond / gstate.py
Created September 22, 2022 02:45
[KV][global state storage] Key/Vale store implemented with SQLAlchemy + PostgreSQL
from typing import Any
import copy
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSONB
from my_proj.db import db
class GState(db.Model):