Skip to content

Instantly share code, notes, and snippets.

@kemingy
Last active February 12, 2019 07:31
Show Gist options
  • Select an option

  • Save kemingy/96b8da1f7cf846131ff319fb5407d0a4 to your computer and use it in GitHub Desktop.

Select an option

Save kemingy/96b8da1f7cf846131ff319fb5407d0a4 to your computer and use it in GitHub Desktop.
Python project config template for deep learning
# run this with bash: `EPOCH=20 python config.py`
import os
class Config:
def __init__(self):
self.EMBEDDING_DIM = 256
self.EPOCH = 10
self.SAVE_PATH = os.path.join(os.path.curdir, 'checkpoint')
self.FILTER_SIZE = [2, 3, 4]
def update(self, **kwargs):
for key in kwargs:
if not hasattr(self, key):
print('[✗] Ignore unknown attribute "{}"'.format(key))
else:
setattr(self, key, kwargs[key])
print('[✓] Attribute "{}" has been updated to "{}"'.format(
key, kwargs[key]))
def update_from_env(self):
for key in os.environ:
if hasattr(self, key):
setattr(self, key, os.environ[key])
print('[✓] Attribute "{}" has been updated to "{}"'.format(
key, os.environ[key]))
def __repr__(self):
display = '{:=^80}\n'.format(self.__class__.__name__)
for k, v in vars(self).items():
if not k.startswith('__'):
display += '| {:<30} {}\n'.format(k, v)
return display + '=' * 80
if __name__ == '__main__':
config = Config()
config.update_from_env()
config.update(EMBEDDING_DIM=128, FILTER_SIZE=[1, 2, 3, 4], MODEL='CNN')
print(config)
@kemingy

kemingy commented Feb 11, 2019

Copy link
Copy Markdown
Author

Variables updated from env or cmd are str, be careful.

@kemingy

kemingy commented Feb 12, 2019

Copy link
Copy Markdown
Author

Variables updated from env or cmd are str, be careful.

Or use the code below replace line No.25 to handle some type problems. (not suitable for list, dict)

# ... 
            if hasattr(self, key):
                _type = type(getattr(self, key))
                setattr(self, key, _type(os.environ[key]))
# ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment