Last active
January 26, 2021 11:26
-
-
Save alexandru-dinu/3c307817f3a7692144bbde97f4958d40 to your computer and use it in GitHub Desktop.
Misc
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
## IMPORTING | |
# add top-dirs to path | |
BASE_PATH = Path(__file__).resolve().parents[1] | |
sys.path[:0] = [str(BASE_PATH / 'src'), str(BASE_PATH)] | |
## LOGGING | |
def setup_logger(name, level, path=None): | |
if path is None: | |
handler = logging.StreamHandler() | |
else: | |
handler = logging.FileHandler(path) | |
formatter = logging.Formatter( | |
fmt='[%(process)d][%(asctime)s.%(msecs)03d @ %(funcName)s][%(levelname)s] %(message)s', | |
datefmt='%y-%m-%d %H:%M:%S') | |
handler.setFormatter(formatter) | |
logger = logging.getLogger(name) | |
logger.setLevel(level) | |
logger.addHandler(handler) | |
return logger | |
## ARGPARSE | |
parser = argparse.ArgumentParser() | |
# --foo OR --bar, not both | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('--foo', nargs='+', type=int) | |
group.add_argument('--bar', nargs='+', type=str) | |
args = parser.parse_args() | |
## YAML | |
# string concatenation | |
import yaml, os | |
class Loader(yaml.SafeLoader): | |
def __init__(self, stream): | |
super().__init__(stream) | |
self.add_constructor(tag='!join', constructor=self.join) | |
@staticmethod | |
def join(loader, node): | |
seq = loader.construct_sequence(node) | |
# return os.path.join(*[str(x) for x in seq]) | |
return ''.join(map(str, seq)) | |
xs = yaml.load(open('config.yaml'), Loader) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment