def __init__(self, server_adress, verbose=False): QThread.__init__(self) Client.__init__(self, server_adress) if verbose: print("Connecting to {}".format(server_adress)) self.connect() self.connect_status() self.request.connect(self.send_request)
def fillFields(self): k = 0 self.items = [] while (True): try: courier = Courier(k) print(type(courier.name)) self.items.append( str(courier.name) + " " + str(courier.surename)) k = k + 1 except KeyError: break self.Courier.addItems(self.items) dfn = pd.read_csv('data/products.csv', encoding='utf-8') del dfn['Unnamed: 0'] self.items = [] for i in range(len(dfn)): self.items.append(dfn.iloc[i, 0]) self.Product.addItems(self.items) k = 0 self.items = [] self.itemsAddress = [] while (True): try: client = Client(k) self.items.append( str(client.name) + " " + str(client.phone_number)) self.itemsAddress.append( str(client.street) + " " + str(client.house)) k = k + 1 except KeyError: break self.Client.addItems(self.items) self.Address.addItems(self.itemsAddress)
def test_remove(self): dfn = pd.read_csv('data/clients.csv', encoding='utf-8') bef = dfn.shape[0] Client(0).removeClient() dfn = pd.read_csv('data/clients.csv', encoding='utf-8') ath = dfn.shape[0] self.assertEqual(bef - 1, ath, "Should False")
def client_get_details(self, client_id, email=None): self.data['action'] = 'getclientsdetails' self.data['clientid'] = client_id if email is not None: self.data['email'] = email data = self.send_call() return Client(data=data)
def __init__(self, host_list = ['0.0.0.0:5000']): """初始化服务端/客户端服务 """ self.logger = Logger(logname='log/log.txt', loglevel = 3, callfile = __file__).get_logger() try: self.server = Admin(host_list) self.client = Client(host_list) except: print "Gearman server host port is error!" self.logger.error("Dispatch a task name %s, %r" %(task_name, json_data)) sys.exit()
def changeClient(self): self.name = self.textName.toPlainText() self.surname = self.textSurname.toPlainText() self.patronymic = self.textPatr.toPlainText() self.street = self.textStreet.toPlainText() self.house = self.textHouse.toPlainText() self.phone = self.textNumber.toPlainText() Client(self.index).changeData(self.name, self.surname, self.patronymic, self.street, self.house, self.phone) self.setWindowTitle("done") self.hide()
def get_clients(): """ Funcion para obtener los clientes de un csv. Lo retorna en una lista. """ clients = [] csv_data = get_data_from_csv('files/clientes.csv') clients_data = csv_data['data'] for client in clients_data: clients.append(Client(*client)) return clients
def simulate_test(): """ Simulate behavior of one client Returns ------- current_time : int How many se """ #Creating queues, a client, and enqueueing the client print('Utworzono kolejki!') q1 = Queue(queue_type=1) q2 = Queue(queue_type=2) q3 = Queue(queue_type=3) print('Utworzono klienta!') c1 = Client(1) print('Dołączam do kolejki') q1.enqueue(c1) # max time equals 9 hours max_time = 9 * 3600 # time spent on registering current_time = c1.register_time while current_time < max_time: if current_time + c1.selection_time + 1800 > max_time: print("I won't manage to play another game, quitting...") c1.set_type(3) # find best queue q3.enqueue(c1) # wait for your turn current_time += c1.signoff_time q3.dequeue() break selection_time = c1.selection_time print("I've chosen a game in {}!".format(manage_time(selection_time))) game_time = int(np.random.normal(1800, 900)) print("I played for {}!".format(manage_time(game_time))) current_time += selection_time + game_time print("Ending within {}".format(manage_time(current_time))) return current_time
def test_change(self): Client(0).changeData("Дмитрий", "Дуплий", "Олегович", "Пушкниа", "Колотушкина", "69") self.assertEqual([ Client(0).name, Client(0).surename, Client(0).patronymic, Client(0).street, Client(0).house, Client(0).phone_number ], ["Дмитрий", "Дуплий", "Олегович", "Пушкниа", "Колотушкина", "69"], "Should False")
def __init__(self, config_file = 'config.yaml'): """初始化配置文件""" global PLUGIN_NAME try: self.open_config = open(config_file) except: print 'not find cofig, please add in catalog!' logger.error('not find config, please add in catalog!') sys.exit() load_config = yaml.load(self.open_config) try: self.hosts_list = load_config['HOSTS_LIST'].split(' ') self.client = Client(self.hosts_list) except: print 'HOSTS_LIST in config.yaml init error,please checking!' logger.error('HOSTS_LIST in config.yaml init error, please checking!') sys.exit() try: PLUGIN_NAME = load_config['PLUGIN_NAME'] except: print 'PLUGIN_NAME in config.yaml init error,please checking!' logger.error('PLUGIN_NAME in config.yaml init error, please checking!') sys.exit() try: self.cycle = load_config['CYCLE'] except: print 'CYCLE in config.yaml init error,please checking!' logger.error('CYCLE in config.yaml init error,please checking!') sys.exit() try: self.update = load_config['UPDATE'] except: print 'UPDATE in config.yaml init error,please checking!' logger.error('UPDATE in config.yaml init error,please checking!') sys.exit() try: self.debug = load_config['DEBUG'] except: print 'DEBUG in config.yaml init error,please checking!' logger.error('DEBUG in config.yaml init error,please checking!') sys.exit() try: self.log = load_config['LOG'] except: print 'LOG in config.yaml init error,please checking!' logger.error('LOG in config.yaml init error,please checking!') sys.exit() self.open_config.close()
def client_get_set(self, limitstart=0, limitnum=25, search=None): self.data['action'] = 'getclients' self.data['limitnum'] = limitnum self.data['limitstart'] = limitstart if search is not None: self.data['search'] = search data = self.send_call() clients = data['clients']['client'] client_list = [] for client in clients: client_list.append(Client(data=client)) return client_list
def add_people(queue, quantity): """ Enqueue given quantity of clients to a queue Parameters ---------- queue : Queue choice of queue to add clients quantity : int number of clients to be enqueued Returns ------- list a list of consequetive enqueues to given queue """ return [queue.enqueue(Client()) for _ in range(quantity)]
from make_seq import MakeSeq from make_seq import MakeSeqEx from clients import Client from writer import Writer if __name__ == '__main__': client = Client('sqlfiles') mse = MakeSeqEx() seqlist = mse.make_seq_extend(client.sqllist) wt = Writer('output') wt.todo(seqlist) exit(0)
def connect_clients(self): self.clients = Client(self.config) self.client_index = self.clients.clients_to_server() self.clients.get_model(self.model) self.clients.load_data()
class Server: def __init__(self, config): self.config = config self.model = self.load_model() # print(self.model) self.clients = None self.client_index = [] self.target_round = -1 def run(self): self.connect_clients() # communication rounds for round in (range(1, self.config.fl.rounds + 1)): logging.info("-" * 22 + "round {}".format(round) + "-" * 30) # select clients which participate training selected = self.clients_selection() logging.info("selected clients:{}".format(selected)) info = self.clients.train(selected) logging.info("aggregate weights") # update glob model glob_weights = self.fed_avg(info) self.model.load_state_dict(glob_weights) train_acc = self.getacc(info) test_acc, test_loss = self.test() logging.info( "training acc: {:.4f},test acc: {:.4f}, test_loss: {:.4f}\n". format(train_acc, test_acc, test_loss)) if test_acc > self.config.fl.target_accuracy: self.target_round = round logging.info("target achieved") break # broadcast glob weights self.clients.update(glob_weights) def fed_avg(self, info): weights = info["weights"] length = info["len"] w_avg = copy.deepcopy(weights[0]) for k in w_avg.keys(): w_avg[k] *= length[0] for i in range(1, len(weights)): w_avg[k] += weights[i][k] * length[i] w_avg[k] = w_avg[k] / (sum(length)) return w_avg def clients_selection(self): # randomly selection frac = self.config.clients.fraction n_clients = max(1, int(self.config.clients.total * frac)) training_clients = np.random.choice(self.client_index, n_clients, replace=False) return training_clients def load_model(self): model_path = self.config.paths.model dataset = self.config.dataset logging.info('dataset: {}'.format(dataset)) # Set up global model model = models.get_model(dataset) logging.debug(model) return model def connect_clients(self): self.clients = Client(self.config) self.client_index = self.clients.clients_to_server() self.clients.get_model(self.model) self.clients.load_data() def test(self): return self.clients.test() def getacc(self, info): corrects = sum(info["corrects"]) total_samples = sum(info["len"]) return corrects / total_samples
def client(self): return Client.get_by_id(self.client_id)
def __init__(self, server_adress, connect): Client.__init__(self, server_adress, connect) self.steering_log = []
class Send_Job_Client(object): def __init__(self, config_file = 'config.yaml'): """初始化配置文件""" global PLUGIN_NAME try: self.open_config = open(config_file) except: print 'not find cofig, please add in catalog!' logger.error('not find config, please add in catalog!') sys.exit() load_config = yaml.load(self.open_config) try: self.hosts_list = load_config['HOSTS_LIST'].split(' ') self.client = Client(self.hosts_list) except: print 'HOSTS_LIST in config.yaml init error,please checking!' logger.error('HOSTS_LIST in config.yaml init error, please checking!') sys.exit() try: PLUGIN_NAME = load_config['PLUGIN_NAME'] except: print 'PLUGIN_NAME in config.yaml init error,please checking!' logger.error('PLUGIN_NAME in config.yaml init error, please checking!') sys.exit() try: self.cycle = load_config['CYCLE'] except: print 'CYCLE in config.yaml init error,please checking!' logger.error('CYCLE in config.yaml init error,please checking!') sys.exit() try: self.update = load_config['UPDATE'] except: print 'UPDATE in config.yaml init error,please checking!' logger.error('UPDATE in config.yaml init error,please checking!') sys.exit() try: self.debug = load_config['DEBUG'] except: print 'DEBUG in config.yaml init error,please checking!' logger.error('DEBUG in config.yaml init error,please checking!') sys.exit() try: self.log = load_config['LOG'] except: print 'LOG in config.yaml init error,please checking!' logger.error('LOG in config.yaml init error,please checking!') sys.exit() self.open_config.close() def check_request_status(self, job_request): """ 可以增加或是使用多种方法,对出现有问题(延时过高/失败等)任务再次请求链接/放弃等处理(同步单线程长链接). 提示:当使用多线程异步并发的时候,注意,此时只能现实任务的提交状况,而不能获取任务之情后的反馈,此时需要在worker端进行任务完成情况 """ if job_request.complete: info = "Job %s finished! Result: %s - %s" % (job_request.gearman_job.unique, job_request.state, job_request.result) DEBUG_MODE(self.debug, info) if self.log: logger.info(info) elif job_request.timed_out: """例如在此可以设置,如果有任务超时,则可以选择重试任务并直到成功. client.wait_until_jobs_accepted(submitted_requests,wait_until_complete = True,poll_timeout = None) """ info = "Job %s timed out!" % job_request.gearman_job.unique DEBUG_MODE(self.debug, info) if self.log: logger.info(info) elif job_request.state == JOB_UNKNOWN: info = "Job %s connection failed!" % job_request.gearman_job.unique DEBUG_MODE(self.debug, info) if self.log: logger.info(info) def Transfer_Mode(self, data_for_process = [], back_ground = 'async'): jobs_infos = {} list_of_jobs = [] if back_ground[1] == 'async': back_ground = True wait = False elif back_ground[1] == 'sync': back_ground = False wait = True elif len(back_ground) == 3 and back_ground[1] == 'stop': data_for_process = [{ 'task_name':back_ground[2], 'data_pack':{'SHUTDOWN': True} }] back_ground = False else: print 'transfer mode error !' logger.error('transfer mode error!') return sys.exit() if not data_for_process: print "the jobs list is null!" logger.error('the jobs list is null!') return -1 for tasks in data_for_process: assert isinstance(tasks, dict) DEBUG_MODE(self.debug, tasks) list_of_jobs.append(dict(task=tasks['task_name'], data=json.dumps(tasks['data_pack']), priority = PRIORITY_LOW )) submitted_requests = self.client.send_jobs( list_of_jobs, wait_until_complete=wait, background=back_ground) if self.log: logger.info("the total job have: %d" % len(submitted_requests)) """对任务请求后状态的监管方法如下. submit_multiple_requests(jobs_requests, wait_until_complete, poll_timeout) wait_until_jobs_accepted(job_requests, poll_timeout=None) wait_until_jobs_completed(job_requests, poll_timeout=None) 以第一种方法为事例 """ if self.debug or wait: completed_requests = self.client.wait_until_jobs_accepted(submitted_requests,poll_timeout = 2) for completed_job_request in completed_requests: self.check_request_status(completed_job_request) return 0
class Gearman_Manage(object): def __init__(self, host_list = ['0.0.0.0:5000']): """初始化服务端/客户端服务 """ self.logger = Logger(logname='log/log.txt', loglevel = 3, callfile = __file__).get_logger() try: self.server = Admin(host_list) self.client = Client(host_list) except: print "Gearman server host port is error!" self.logger.error("Dispatch a task name %s, %r" %(task_name, json_data)) sys.exit() def show_status(self): """查看server状态信息""" current_status = self.server.get_status() num = 0 for status in current_status: print status def get_worker(self, task_name = None): """查看worker端状态信息""" workers = [] for w in self.server.get_workers(): if w['tasks']: workers.append( w ) print "totla workers: %d" % (len(workers)) if not task_name: for i in workers: print "the IP:[%s]---Worker_name:[%s]---Task_name:[%s]"%(i['ip'],i['client_id'],i['tasks']) else: for i in workers: if task_name and i['tasks'][0] == task_name: print "the IP:[%s]---Worker_name:[%s]---Task_name:[%s]"%(i['ip'],i['client_id'],i['tasks']) return workers def send_task(self, task_name, json_data, priority=PRIORITY_NONE): """发送控制指令""" self.client.send_job(name=task_name, data=json.dumps(json_data), wait_until_complete=False, background=True, priority=priority) print ("Dispatch a task name %s, %r" %(task_name, json_data)) self.logger.info("Dispatch a task name %s, %r" %(task_name, json_data)) def clear_workers(self, task_name = None,priority = PRIORITY_HIGH): """关闭worker""" current_status = self.server.get_status() num = 0 if not task_name: print "I don't know which worker will be clear!" return if task_name == 'all': for status in current_status: num = 0 num = int(status['workers']) for i in range(num): self.send_task(status['task'], {'SHUTDOWN': True},priority) print "stop worker total:%d" % num else: for status in current_status: if status['task'] == task_name: num = int(status['workers']) print status for i in range(num): self.send_task(task_name,{'SHUTDOWN': True},priority) print "stop worker total:%d" % num if num == 0: print "Task list no have name is '%s' task!" % task_name return None def clear_server_list(self, task_name = None): """清理server job 队列""" current_status = self.server.get_status() if not task_name: print "I don't know clear which data list!" return if task_name == 'all': pass else: num = [i['queued'] for i in current_status if task_name == i['task']] print "the list len:%d" % num[0] self.server.empty_task(str(task_name)) def start_server(self, prot = 5000): """启动服务器""" self.server.start_server(prot) self.logger.info("start server.") def stop_server(self): """停止服务器""" try: self.server.send_shutdown() self.logger.info("stop server.") except: print "server is not run!" def ping_server(self): """查看服务器连通状况""" try: print self.server.get_response_time() except: print "server is not run!"
def test_remove_negativ(self): dfn = pd.read_csv('data/clients.csv', encoding='utf-8') bef = dfn.shape[0] + 1 Client(bef).removeClient()
from cat import Cat from clients import Client if __name__ == '__main__': cat1 = Cat("Барон", "Мальчик", "2") cat2 = Cat("Сэм", "Мальчик", "2") cat1.printCat() cat2.printCat() print() client_1 = Client("Вася", "Пупкин") print(client_1) client_1.add_money(30) print(client_1.get_account()) client_1.add_money(30.1) client_1.spend_money(10) print() print(client_1) print("Транзакции клиента: ", ", ".join(map(str, client_1.get_transactions())))
def __del__(self): Client.__del__(self)
from clients import Client from accounts import Account, SpecialAccount from banks import Bank joao = Client('Joao da Silva', '777-1234') maria = Client('Maria da Silva', '555-4321') contam = Account(maria, 99) contaj = Account(joao, 100) tatu = Bank("Tatú") tatu.abre_conta(contaj) tatu.abre_conta(contam) tatu.lista_contas() contaJoao = SpecialAccount(joao, 2, 500, 1000) contaJoao.deposito(1000) contaJoao.deposito(3000) contaJoao.deposito(95.15) contaJoao.saque(1500) contaJoao.extrato()
def delClient(self): index = self.tableWidget.row(self.tableWidget.currentItem()) if index == -1: return Client(index).removeClient() self.fillTableClients()
from services.passgen import PassGenerator from services.repo import FileRepository from services.passwordcheker import PasswordChecker from conf.config import Settings from clients import Client pass_service = PassGenerator(Settings.pass_generator_pattern) file_service = FileRepository(Settings.data_folder) chek_service = PasswordChecker() client = Client(file_service,pass_service,chek_service) client.load() client.execute()
threadList = [] clientList = [] companyList = [] agencyList = [] #Flight Companies latam = Company('LATAM') ryanair = Company('Ryanair') airFrance = Company('AirFrance') panAm = Company('PanAm') companyList = [latam, ryanair, airFrance, panAm] #Travel Agencies decolar = Agency(companyList, 'Decolar.Com') skyScanner = Agency(companyList, 'Sky Scanner') agencyList = [decolar, skyScanner] for agency in agencyList: agency.run() sleep(2) # Creating Clients for i in range(10): clientList.append(Client(agencyList)) threadList.append(Thread(target=clientList[i].run).start())