This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import asdict, dataclass, fields, is_dataclass | |
@dataclass | |
class BaseDataClass: | |
def __post_init__(self): | |
""" | |
Convert all fields of type `dataclass` into an instance of the | |
specified data class if the current value is of type dict. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export PATH="$DOTFILES/zsh/scripts/bin:$PATH" | |
fpath=( $DOTFILES/zsh/scripts/completions $fpath ) | |
autoload -U compinit && compinit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import contextlib | |
import io | |
import pstats | |
@contextlib.contextmanager | |
def profile(max_results=-1, pattern="*"): | |
""" | |
Profile a code block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import traceback | |
from typing import Optional, Union | |
def format_exception(exception: Optional[Union[Exception, BaseException]]) -> str: | |
""" Format a prettier exception trace """ | |
if exception: | |
return f"\n{type(exception).__name__} Exception: {exception}" f"\n{''.join(traceback.format_tb(exception.__traceback__))}" | |
return " Exception." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import traceback | |
from typing import Optional, Union | |
def format_exception(exception: Optional[Union[Exception, BaseException]]) -> str: | |
""" Format a prettier exception trace """ | |
if exception: | |
return f"\n{type(exception).__name__} Exception: {exception}" f"\n{''.join(traceback.format_tb(exception.__traceback__))}" | |
return " Exception." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from alembic import config | |
from alembic import script | |
from alembic.autogenerate import compare_metadata | |
from alembic.runtime import migration | |
from alembic.runtime.environment import EnvironmentContext | |
# other imports ... | |
def assert_database_is_up_to_date(): | |
""" Database is up to date with migration head version """ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_all(session: Session, offset: int, limit: int, order_by: str = "", filters: Optional[list] = None, joins: Optional[list] = None): | |
if not filters: | |
filters = [] | |
if not joins: | |
joins = [] | |
query = session.query(self._model_type) | |
if joins: | |
query = query.join(*joins) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parser = argparse.ArgumentParser(description="input `a=b&c=d` becomes `dict({'a':'b', 'c':'d'})`") | |
parser.add_argument( | |
"-p", "--params", | |
help="Convert parameters in the form `--params a=b&c=d` to a dictionary", | |
action=type('', (argparse.Action,), dict( | |
__call__=lambda a, _, n, v, __: setattr(n, a.dest, dict([kv.split('=') for kv in v.split('&')])) | |
)), | |
default={}, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any, Dict | |
class PropertyTypeMixin: | |
""" | |
Enforce type checking of attributes at runtime | |
""" | |
_properties_type: Dict[str, Any] = {} | |
def __setattr__(self, key, value): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip list --outdated | sed 's/ (.*Latest: /==/g;s/ \[.*//g' |
NewerOlder