def set_image_url(image_url = WEBSERVER.ROBOTER_IMAGE_URL): print(image_url) WEBSERVER.ROBOTER_IMAGE_URL = image_url print(WEBSERVER.ROBOTER_IMAGE_URL) print(configuration.configuration["WEBSERVER"]["ROBOTER_IMAGE_URL"]) configuration.dump() configuration.load() return WEBSERVER.ROBOTER_IMAGE_URL
def test_warn_on_missing_options(self, mock_logging): """Tests that loading a config file missing options causes warnings.""" config = { 'boilerplate_dir': '♨ 📂 directory', } with open(self.config_path, 'w') as config_file: json.dump(config, config_file) configuration.load(self.config_path) mock_logging.warning.assert_called_with( 'Configuration file `%s` missing options: %s', self.config_path, 'report_dir, start_url')
def main(): try: logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s") config = configuration.load() ser = init_serial(config) server = modbus_rtu.RtuServer(ser) server.start() slaves.create(server, logger) while True: try: time.sleep(1) except Exception as e: print(e) break except Exception as e: print(e) finally: ser.close() server.stop() logger.error("Slave stopped.")
def test_warn_on_unknown_options(self, mock_logging): """Tests that loading a config file with unknown options causes warnings.""" config = { 'boilerplate_dir': '♨ 📂 directory', 'report_dir': '✒ 📂 directory', 'start_url': '🚧 my 📄 website 🚧.html', 'year': '2007', '📄': ' 📄 📄 📄 📄', } with open(self.config_path, 'w') as config_file: json.dump(config, config_file) configuration.load(self.config_path) mock_logging.warning.assert_called_with( 'Configuration file `%s` has unexpected options: %s', self.config_path, 'year, 📄')
def test_load_valid(self): """Tests loading a valid config file returns the correct result.""" config = { 'boilerplate_dir': '♨ 📂 directory', 'report_dir': '✒ 📂 directory', 'start_url': '🚧 my 📄 website 🚧.html', } with open(self.config_path, 'w') as config_file: json.dump(config, config_file) loaded_config = configuration.load(self.config_path) self.assertEqual(loaded_config, config)
def __init__(self, config_path="./config/"): super(Publisher, self).__init__() self.config = configuration.load(config_path) self.mapping = mapper.Mapper(config_path) self.register_logger() self.channels = self.mapping.get_channels() self.register_zmq_server() self.register_channels() self.alerts = alerts.Alerts(config_path) self.log.info('The server is ready!')
def main(): """Executes the script and handles command line arguments.""" # Set up parsers, then parse the command line arguments. desc = 'Semi-automatically convert Chrome Apps into progressive web apps.' parser = argparse.ArgumentParser(description=desc) parser.add_argument('-v', '--verbose', help='Verbose logging', action='store_true') subparsers = parser.add_subparsers(dest='mode') parser_convert = subparsers.add_parser( 'convert', help='Convert a Chrome App into a progressive web app.') parser_convert.add_argument( 'input', help='Chrome App input directory', type=unicode_arg) parser_convert.add_argument( 'output', help='Progressive web app output directory', type=unicode_arg) parser_convert.add_argument('-c', '--config', help='Configuration file', required=True, metavar='config', type=unicode_arg) parser_convert.add_argument('-f', '--force', help='Force output overwrite', action='store_true') parser_config = subparsers.add_parser( 'config', help='Print a default configuration file to stdout.') parser_config.add_argument('output', help='Output config file path', type=unicode_arg) parser_config.add_argument('-i', '--interactive', help='Whether to interactively generate the config file', action='store_true') args = parser.parse_args() # Set up logging. logging_level = logging.DEBUG if args.verbose else logging.INFO logging.root.setLevel(logging_level) colorama.init(autoreset=True) logging_format = ':%(levelname)s: \t%(message)s' formatter = Formatter(logging_format) handler = WarningStoreStreamHandler(sys.stdout) handler.setFormatter(formatter) logging.root.addHandler(handler) # Main program. if args.mode == 'config': configuration.generate_and_save(args.output, args.interactive) elif args.mode == 'convert': config = configuration.load(args.config) convert_app(args.input, args.output, config, handler.captured_warnings, args.force)
def main(): try: logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s") master = modbus_tcp.TcpMaster(host=os.environ.get('SLAVE_IP', '0.0.0.0'), port=int(os.environ.get('SLAVE_PORT', '5022'))) master.set_timeout(5.0) logger.info('Master connected.') config = configuration.load() while True: try: slaves.read(master, config) time.sleep(1) except Exception as e: print(e) break finally: logger.error('Master stopped.')
def create(server, logger=None): for s in configuration.load(): slave = server.add_slave(s['unit']) for x in s['coils']: block_name = 'unit_%i_coil_%i' % (s['unit'], x) slave.add_block(block_name, cst.COILS, x, 1) slave.set_values(block_name, x, True) for x in s['discrete_inputs']: block_name = 'unit_%i_discrete_input_%i' % (s['unit'], x) slave.add_block(block_name, cst.DISCRETE_INPUTS, x, 1) slave.set_values(block_name, x, True) for x in s['input_registers']: block_name = 'unit_%i_input_register_%i' % (s['unit'], x) slave.add_block(block_name, cst.HOLDING_REGISTERS, x, 1) slave.set_values(block_name, x, x) for x in s['holding_registers']: block_name = 'unit_%i_holding_register_%i' % (s['unit'], x) slave.add_block(block_name, cst.HOLDING_REGISTERS, x, 1) slave.set_values(block_name, x, x) if logger is not None: logger.info('Slave unit %i added.' % (s['unit']))
def main(): try: logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s") ser = init_serial() master = modbus_rtu.RtuMaster(ser) master.set_timeout(5.0) logger.info('Master connected.') config = configuration.load() while True: try: slaves.read(master, config) time.sleep(1) except Exception as e: print(e) break except Exception as e: print(e) finally: ser.close() logger.error('Master stopped.')
def test_sms(): config = configuration.load() s = alerts.AlertSMS(config['alerts']['AlertSMS']) s.send_notification('This is a test.')
def test_pushbullet(): config = configuration.load() p = alerts.AlertPushBullet(config['alerts']['AlertPushBullet']) p.send_notification('This is a test.')
try: date.isoformat() except Exception: date = datetime.datetime(2017, 1, 1) return collection.find( {"created_at.date": {"$exists": True}, "$and": [{"created_at.date": {"$gte": date.strftime("%Y-%m-%d %H:%M:%S")}}]}) \ .sort([("created_at.date", 1)]) \ .skip(0) \ .limit(limit) if __name__ == "__main__": conf = configuration.load() destination = Destination() for i in range(30): for row in Source.fetch(destination.fetch_max_date(), 100): try: data = [] try: res = destination.insert_rules(data) print('%s: %s' % (row['reference'], res)) pass except Exception as e: print("%s: Writing ERROR: %s" % (row['reference'], e)) except Exception: print('%s: Error' % row['reference'])
# the ZMQ topology. self.ctrl_socket.send_multipart([worker_id, srl.dumps(payload)]) self.logging.info('Sending Bytecode to %s' % worker_id) else: self.logging.debug('Worker asking for code again?') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('path', help='Verbose logging') parser.add_argument('config', help='Configuration') parser.add_argument('--verbose', help='Verbose logging') args = parser.parse_args() config = configuration.load(args.config) # Python code if '.py' in args.path: path = args.path.replace('.py', '') fp, pathname, description = imp.find_module(path) mod = imp.load_module(args.path, fp, pathname, description) # BLIR code elif '.bl' in args.path: from blir import compile, Context path = args.path ast, env = compile(open(path).read()) ctx = Context(env) mod = ctx.mod
def __init__(self, config_path="./config/"): self.config = configuration.load(config_path) self.register()
#!/usr/bin/env python # -*- encoding: utf-8 -*- import os import json import configuration os.environ['CONFIGURATION'] = ''' [ { "unit": 1, "coils": [1, 2, 74, 89], "holding_registers": [801, 802, 803, 804] } ] ''' print(configuration.load()) print(json.dumps(configuration.load()).replace(' ', ''))
def __init__(self, config_path="./config/"): super(Subscriber, self).__init__() self.config = configuration.load(config_path) self.log = logger.create('CLIENT') self.connect()
def main(): """Executes the script and handles command line arguments.""" # Set up parsers, then parse the command line arguments. desc = 'Semi-automatically convert Chrome Apps into progressive web apps.' parser = argparse.ArgumentParser(description=desc) parser.add_argument('-v', '--verbose', help='Verbose logging', action='store_true') subparsers = parser.add_subparsers(dest='mode') parser_convert = subparsers.add_parser( 'convert', help='Convert a Chrome App into a progressive web app.') parser_convert.add_argument('input', help='Chrome App input directory', type=unicode_arg) parser_convert.add_argument('output', help='Progressive web app output directory', type=unicode_arg) parser_convert.add_argument('-c', '--config', help='Configuration file', required=True, metavar='config', type=unicode_arg) parser_convert.add_argument('-f', '--force', help='Force output overwrite', action='store_true') parser_config = subparsers.add_parser( 'config', help='Print a default configuration file to stdout.') parser_config.add_argument('output', help='Output config file path', type=unicode_arg) parser_config.add_argument( '-i', '--interactive', help='Whether to interactively generate the config file', action='store_true') args = parser.parse_args() # Set up logging. logging_level = logging.DEBUG if args.verbose else logging.INFO logging.root.setLevel(logging_level) colorama.init(autoreset=True) logging_format = ':%(levelname)s: \t%(message)s' formatter = Formatter(logging_format) handler = WarningStoreStreamHandler(sys.stdout) handler.setFormatter(formatter) logging.root.addHandler(handler) # Main program. if args.mode == 'config': configuration.generate_and_save(args.output, args.interactive) elif args.mode == 'convert': config = configuration.load(args.config) convert_app(args.input, args.output, config, handler.captured_warnings, args.force)
#!/usr/bin/env python # -*- encoding: utf-8 -*- import sys import configuration if __name__ == '__main__': configuration = configuration.load() mode = configuration.get('mode', '') if mode not in ['TCP_SLAVE', 'TCP_MASTER', 'RTU_SLAVE', 'RTU_MASTER']: sys.exit('Please set mode to either SLAVE or MASTER') if mode == 'TCP_SLAVE': from tcp_slave import main elif mode == 'TCP_MASTER': from tcp_master import main elif mode == 'RTU_SLAVE': from rtu_slave import main else: from rtu_master import main main()
def test_loads_from_env(self): self.put_config_to_env() configuration.load(NOT_EXISTING_CONFIG_FILE) self.assert_configuration_correct()
def setUp(self): self.clean_env() configuration.load(NOT_EXISTING_CONFIG_FILE)
def run(config_path='./config/', debug=False): app.extraconfig = configuration.load(config_path) host=app.extraconfig['host_http'] port=int(app.extraconfig['port_http']) app.run(host=host, port=port, debug=debug)
def test_email(): config = configuration.load() e = alerts.AlertEmail(config['alerts']['AlertEmail']) e.send_notification('This is a test.')
def main2(): path = 'problems/example' problem = configuration.load(path) solve(problem, save=True)
def run(*configs, group=None): config = configuration.load(*configs) if config.group: config.group = config.data_source + '-' + config.group else: config.group = config.data_source if group: config.group = config.group + "-" + str(group) if config.from_scratch: config.group = 'scratch-' + config.group config.name = 'scratch-' + config.name if config.log: wandb.init(project='explainable-asag', group=config.group, name=config.name, config=config) config = wandb.config model = transformers.AutoModelForSequenceClassification.from_pretrained( config.model_name, num_labels=config.num_labels) if config.token_types: embedding_size = model.config.__dict__.get('embedding_size', model.config.hidden_size) update_token_type_embeddings(model, embedding_size, model.config.initializer_range) if config.from_scratch: model.init_weights() cuda = torch.cuda.is_available() if cuda: model.cuda() train_dataloader = dataset.dataloader(val_mode=False, data_file=config.train_data, data_source=config.data_source, vocab_file=config.model_name, num_labels=config.num_labels, train_percent=config.train_percent, batch_size=config.batch_size, drop_last=config.drop_last, num_workers=config.num_workers) val_dataloader = dataset.dataloader(val_mode=True, data_file=config.val_data, data_source=config.data_source, vocab_file=config.model_name, num_labels=config.num_labels, train_percent=config.val_percent, batch_size=config.batch_size, drop_last=config.drop_last, num_workers=config.num_workers) optimizer = torch.optim.__dict__[config.optimizer]( model.parameters(), lr=config.learn_rate, **config.optimizer_kwargs) # Hack to get any scheduler we want. transformers.get_scheduler does not implement e.g. linear_with_warmup. get_scheduler = { 'linear_with_warmup': transformers.get_linear_schedule_with_warmup, 'cosine_with_warmup': transformers.get_cosine_schedule_with_warmup, 'constant_with_warmup': transformers.get_constant_schedule_with_warmup, 'cosine_with_hard_restarts_with_warmup': transformers.get_cosine_with_hard_restarts_schedule_with_warmup } lr_scheduler = get_scheduler[config.scheduler](optimizer, *config.scheduler_args, **config.scheduler_kwargs) best_f1 = 0.0 patience = 0 epoch = 0 log_line = '' try: #while lr_scheduler.last_epoch <= total_steps: while epoch < config.max_epochs: epoch += 1 av_epoch_loss = training.train_epoch( train_dataloader, model, optimizer, lr_scheduler, config.num_labels, cuda, log=config.log, token_types=config.token_types) #tidy stuff up every epoch gc.collect() torch.cuda.empty_cache() metrics_weighted, metrics_macro = training.val_loop( model, val_dataloader, cuda, token_types=config.token_types) p, r, f1, val_acc = metrics_weighted p_m, r_m, f1_m, val_acc_m = metrics_macro log_line = f'model: {config.model_name} | epoch: {epoch} | av_epoch_loss {av_epoch_loss:.5f} | f1: {f1:.5f} | accuracy: {val_acc:.5f} \n' print(log_line[:-1]) if config.log: wandb.log({ 'precision': p, 'recall': r, 'f1': f1, 'accuracy': val_acc, 'av_epoch_loss': av_epoch_loss }) wandb.log({ 'precision-macro': p_m, 'recall-macro': r_m, 'f1-macro': f1_m, 'accuracy-macro': val_acc_m }) if f1 > best_f1: if config.log: this_model = os.path.join(wandb.run.dir, 'best_f1.pt') print("saving to: ", this_model) torch.save([model.state_dict(), config.__dict__], this_model) wandb.save('*.pt') best_f1 = f1 patience = 0 #max((0, patience-1)) elif config.max_patience: patience += 1 if patience >= config.max_patience: break # Move stuff off the gpu model.cpu() #This is for sure a kinda dumb way of doing it, but the least mentally taxing right now optimizer = torch.optim.__dict__[config.optimizer]( model.parameters(), lr=config.learn_rate) gc.collect() torch.cuda.empty_cache() #return model #Gives Error except KeyboardInterrupt: if config.log: wandb.save('*.pt') #Move stuff off the gpu model.cpu() optimizer = torch.optim.__dict__[config.optimizer]( model.parameters(), lr=config.learn_rate) gc.collect() torch.cuda.empty_cache()
"code": code }) return http_client.request("https://slack.com/api/oauth.access" + "?" + answer_args) def parse_auth_response(auth_response, response_content): if auth_response.status == 200: response = json.loads(response_content.decode()) if response.get("ok"): access_token = response.get("access_token") team_id = response.get("team_id") user_id = response.get("user_id") team_name = response.get("team_name") print("Authenticated user %s with token: ***%s in team: %s/%s" % (user_id, access_token[:6], team_id, team_name)) else: print("Auth request error. Get: ", response_content) else: print("Auth request error. Bad response: ", auth_response, response_content) if __name__ == '__main__': configuration.load() port = int(os.getenv('PORT', 5000)) slack_utils.init(configuration.slack_app_api_key) message_processor.start_processing() app.run(debug=False, port=port, host='0.0.0.0')