def configure_logging(cl_args): """ Configure root Logger for Infinium. Args cl_args: A namespace created by ``argparse``. Returns None """ configuration = get_config() log_level = logging.DEBUG if cl_args.debug or configuration.debug else logging.INFO formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') root = logging.getLogger() root.setLevel(log_level) file_handler = logging.FileHandler(str(configuration.log_path)) file_handler.setFormatter(formatter) root.addHandler(file_handler) if cl_args.verbose or configuration.verbose: stderr_handler = logging.StreamHandler(sys.stderr) stderr_handler.setFormatter(formatter) root.addHandler(stderr_handler)
def main(): """ Parse command line, read configuration file, and launch Infinium's user interface. Return None Raises NotImplementedError, SelectionError """ # Parse command line arguments. cl_args = parse_command_line() # Get configuration options. try: configuration = get_config() except (ConfigurationError, OSError) as error: print(error, sys.stderr) sys.exit(data.ExitCode.config_file_not_found.value) # Configure root Logger. configure_logging(cl_args) # Launch user interface. if cl_args.graphical: # Use graphical user interface. raise NotImplementedError('GUI under construction. Please use CLI.') else: # Use command line interface. launch_cli()
def connect_database(): """ Connect to the Infinium database and create a ``Session`` class which can be instantiated to interact with the database. Returns: SQLAlchemy ``Session`` class. """ configuration = get_config() url = '{dialect}+{driver}://{username}:{password}@{host}:{port}/{database}' url = url.format(dialect=configuration.db_dialect, driver=configuration.db_driver, username=configuration.db_username, password=configuration.db_password, host=configuration.db_host, port=configuration.db_port, database=configuration.db_database) engine = create_engine(url, echo=configuration.db_echo) _Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) return Session
def construct_model(): configuration = get_config() classifier = create_classifier() training_data = extract_training_data(database) train_classifier(classifier, training_data) return classifier
def launch_cli(): """ Launch command line interface event loop. Return None; does not return. Terminates program upon completion. """ configuration = get_config() _show_welcome() # Connect to database. while True: try: Session = db.connect_database() break except OperationalError: logging.warning('Database credentials not valid.') username, password = _prompt_db_credentials( configuration.db_host, configuration.db_port, configuration.db_database) configuration.db_username = username configuration.db_password = password msg = 'Connected to database at {}:{}/{}' msg = msg.format(configuration.db_host, configuration.db_port, configuration.db_database) logging.info(msg) # Enter CLI event loop. while True: # Decide whether to analyze a stock, add a new entry to the database, # or construct a new valuation model. main_operation = _main_prompt() if main_operation is _MainOperation.construct_model: construct_model() elif main_operation is _MainOperation.add_database_entry: _add_database_entry(Session) elif main_operation is _MainOperation.parse_annual_report: raise NotImplementedError() elif main_operation is _MainOperation.analyze_stock: raise NotImplementedError() elif main_operation is _MainOperation.exit: sys.exit(ExitCode.success.value)
def create_classifier(): configuration = get_config() return SGDClassifier(loss=configuration.sgd_loss, penalty=configuration.sgd_penalty, alpha=configuration.sgd_alpha, l1_ratio=configuration.sgd_l1_ratio, fit_intercept=configuration.sgd_fit_intercept, n_iter=configuration.sgd_n_iter, shuffle=configuration.sgd_shuffle, verbose=configuration.sgd_verbose, n_jobs=configuration.sgd_n_jobs, learning_rate=configuration.sgd_learning_rate, eta0=configuration.sgd_eta0, power_t=configuration.sgd_power_t)