Last active
April 21, 2017 22:49
-
-
Save lennax/5691368 to your computer and use it in GitHub Desktop.
Minimal class
This file contains hidden or 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
#!/usr/bin/env python | |
from __future__ import division | |
import argparse | |
import glob | |
import inspect | |
import logging | |
import os | |
script_dir = os.path.abspath(os.path.dirname(inspect.getfile(inspect.currentframe()))) | |
class MYCLASSError(RuntimeError): | |
"""Exception for class MYCLASS""" | |
def __init__(self, message): | |
super(MYCLASSError, self).__init__(message) | |
class MYCLASS(object): | |
def __init__(self, directory=None, input=None, pattern=None): | |
"""CONSTRUCTOR""" | |
if pattern is None: | |
pattern = "FILEPATTERN" | |
filenames = list() | |
if input is not None: | |
if isinstance(input, str): | |
filenames.append(input) | |
else: | |
filenames.extend(input) | |
if directory is not None: | |
filenames.extend(glob.glob(os.path.join(directory, pattern))) | |
if not filenames: | |
raise MYCLASSError("No files were found") | |
@classmethod | |
def commandline(cls, module_args=None): | |
desc = """HELPDESCRIPTION""" | |
a = argparse.ArgumentParser(description=desc) | |
a.add_argument("-i", "--input", nargs="*", | |
help="Input file(s)") | |
a.add_argument("-d", "--directory", | |
help="Directory of input files") | |
a.add_argument("-p", "--pattern", | |
help="File pattern in directory (default FILEPATTERN)") | |
a.add_argument("-v", "--verbose", action="store_true", | |
help="Show debug statements") | |
args = a.parse_args(module_args) | |
kwargs = vars(args) | |
level = logging.INFO | |
if kwargs.pop("verbose"): | |
level = logging.DEBUG | |
logging.basicConfig(level=level) | |
c = cls(**kwargs) | |
return c | |
if __name__ == "__main__": | |
MYCLASS.commandline() |
This file contains hidden or 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
#!/usr/bin/env python | |
from __future__ import division | |
import argparse | |
import inspect | |
import logging | |
import os | |
import matplotlib | |
matplotlib.use("Agg") | |
matplotlib.rcParams['savefig.dpi'] = 150 | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
pd.set_option("max_colwidth", 100) | |
import seaborn as sns | |
sns.set(context="paper") | |
logging.basicConfig(level=logging.DEBUG) | |
script_dir = os.path.abspath(os.path.dirname(inspect.getfile(inspect.currentframe()))) | |
class MYCLASSError(RuntimeError): | |
"""Exception for class MYCLASS""" | |
def __init__(self, message): | |
super(MYCLASSError, self).__init__(message) | |
class MYCLASS(object): | |
def __init__(self): | |
"""CONSTRUCTOR""" | |
@classmethod | |
def commandline(cls, module_args=None): | |
desc = """HELPDESCRIPTION""" | |
a = argparse.ArgumentParser(description=desc) | |
args = a.parse_args(module_args) | |
c = cls(**vars(args)) | |
return c | |
if __name__ == "__main__": | |
MYCLASS.commandline() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment