def renew_cert_and_emit_event(self) -> bool: """ Renew a certificate and emit an event whether it succeeded or not. Return: True if succeeded, False otherwise. """ try: # First we need to revoke the certificate if not self.revoke_cert_and_emit_event(): raise CertRevogationError() self.new_cert.renew_cert() CertUtils.create_cert_files(self.new_cert, self.device_cert_dir) except Exception as exception: Utils.fire_locust_failure( request_type=REQUEST_TYPE, name=MESSAGE_TYPE_RENEW, response_time=0, response_length=0, exception=CertRenovationError("failed to renew")) self.logger.error( "An error occurred while trying to renew the certificate") self.logger.error(str(exception)) return False else: Utils.fire_locust_success(request_type=REQUEST_TYPE, name=MESSAGE_TYPE_RENEW, response_time=0, response_length=0) self.is_renewed = True return True
def revoke_cert_and_emit_event(self) -> bool: """ Revoke a certificate and emit an event whether it succeeded or not. Return: True if succeeded, False otherwise. """ try: if CertUtils.has_been_revoked(self.new_cert): self.logger.debug("Already revoked, skipping step...") else: CertUtils.revoke_cert(self.new_cert) Utils.fire_locust_success(request_type=REQUEST_TYPE, name=MESSAGE_TYPE_REVOKE, response_time=0, response_length=0) self.is_revoked = True return True except Exception as exception: self.logger.error( "An error occurred while trying to revoke the certificate") self.logger.error(str(exception)) Utils.fire_locust_failure( request_type=REQUEST_TYPE, name=MESSAGE_TYPE_REVOKE, response_time=0, response_length=0, exception=CertRevogationError("certificate not revoked")) return False
def subscribe(self) -> None: """ Handles the subscription in MQTT topics. """ start_time = Utils.seconds_to_milliseconds(time.time()) try: err, mid = self.mqttc.subscribe( (self.sub_topic, CONFIG['mqtt']['qos'])) if err: raise ValueError(err) self.submmap[mid] = { 'name': MESSAGE_TYPE_SUB, 'start_time': start_time } except Exception as exception: error = Utils.error_message(int(str(exception))) self.logger.error("Error while subscribing: %s", error) Utils.fire_locust_failure( request_type=REQUEST_TYPE, name=MESSAGE_TYPE_SUB, response_time=Utils.seconds_to_milliseconds(time.time()) - start_time, exception=error)
def publish(self) -> None: """ Handles the publishing of messages to MQTT host. """ start_time = Utils.seconds_to_milliseconds(time.time()) payload = {"timestamp": start_time} try: err, mid = self.mqttc.publish(topic=self.topic, payload=json.dumps(payload), qos=CONFIG['mqtt']['qos']) if err: raise ValueError(err) self.pubmmap[mid] = { 'name': MESSAGE_TYPE_PUB, 'payload': payload, 'start_time': start_time } except Exception as exception: error = Utils.error_message(int(str(exception))) Utils.fire_locust_failure( request_type=REQUEST_TYPE, name=MESSAGE_TYPE_PUB, response_time=Utils.seconds_to_milliseconds(time.time()) - start_time, exception=error)
def process_files(): """ process files with rebuild engine """ Utils.truncate_folder(Config.input_path) Utils.truncate_folder(Config.output_path) file = request.files.get("file") if not file: return jsonify({"message": "please supply a file"}) file.save(os.path.join(Config.input_path, file.filename)) os.system( "glasswallCLI -config={0}/Config.ini -xmlconfig={0}/Config.xml". format(Config.config_path)) logger.info( os.system("ls -alh /usr/src/app/rebuild_files/output/Managed")) logger.info( os.system( "cat /usr/src/app/rebuild_files/output/glasswallCLIProcess.log" )) try: return send_file( Config.output_path + "/" + file.filename, attachment_filename=file.filename, as_attachment=True, ) except Exception: return jsonify({"message": "failed"})
def download_dataset(self, folder_path): url = "http://cs231n.stanford.edu/tiny-imagenet-200.zip" from src.utils import Utils folder_path = Utils.download_file(folder_path, url=url) return Utils.extract_zip_file(file_path=folder_path, extract_path='data')
def dump_to_dict(self, with_children=False, with_files=True, child_number=None): dumped = { 'id': self.id, 'board_id': self.board_id, 'parent_id': self.parent_id, 'children_ids': list(map(lambda child : child.id, self.children)), 'head': self.head, 'body': parse_to_markup(self.body), 'created': Utils.dump_time(self.created) } if with_children: children = Utils.get_children(self)[:child_number] children = list(map( lambda post: post.dump_to_dict(), children )) dumped.update({ 'children': children }) if with_files: files = list(map( lambda file: file.filetracker.dump_to_dict(full=True), self.files )) dumped.update({ 'files': files }) return dumped
def subscribing(self) -> None: """ Handles the subscription in MQTT topics. """ start_time = time.time() try: err, mid = self.mqttc.subscribe( (self.sub_topic, CONFIG['mqtt']['qos'])) if err: raise ValueError(err) self.submmap[mid] = { 'name': MESSAGE_TYPE_SUB, 'qos': CONFIG['mqtt']['qos'], 'topic': self.sub_topic, 'payload': "", 'start_time': start_time, 'messages': 'messages' } except Exception as exception: error = Utils.error_message(int(str(exception))) logging.error("Error while subscribing: %s", error) Utils.fire_locust_failure(request_type=REQUEST_TYPE, name=MESSAGE_TYPE_SUB, response_time=Utils.time_delta( start_time, time.time()), exception=error)
def gerar(self, formulario): nome, quantidade = formulario['nome'], int(formulario['quantidade']) alunos = self.model.aluno.alunos.copy() grupos_gerados = [] quantidade_grupos = len(alunos) // quantidade for i in range(quantidade_grupos): grupo = {} grupo['nome'] = '' grupo['integrantes'] = [] grupos_gerados.append(grupo) for i, grupo in enumerate(grupos_gerados): grupo['nome'] = f'{nome}-{i + 1}' for j in range(quantidade): fim = len(alunos) - 1 index = Utils.inteiro_aleatorio(inicio=0, fim=fim) grupo['integrantes'].append(alunos[index]) del alunos[index] # alunos sem grupo fim = quantidade_grupos - 1 for aluno in alunos: index = Utils.inteiro_aleatorio(inicio=0, fim=fim) grupos_gerados[index]['integrantes'].append(aluno) return grupos_gerados
def calculate_year_hsebs(self, year): Utils.run_log(u'calculate year %d hsebs...' % year) result = self.calculate_day_hseb(year, 1, 1) first_hs = result[0] first_eb = result[1] hseb_position_result = HSEB.calculate_hseb_position(first_hs, first_eb) first_hs_position = hseb_position_result[0] first_eb_position = hseb_position_result[1] begin = datetime.date(year, 1, 1) end = datetime.date(year, 12, 31) contents = [] for days_diff in range((end - begin).days + 1): day = begin + datetime.timedelta(days=days_diff) today = int(day.strftime("%w")) day_hs_position = HSEB.calculate_hs_position_by_days_diff( first_hs_position, days_diff) day_eb_position = HSEB.calculate_eb_position_by_days_diff( first_eb_position, days_diff) day_hseb = HSEB.get_hseb_by_position(day_hs_position, day_eb_position) day_hs = day_hseb[0] day_eb = day_hseb[1] line = u'%s\t%d\t%s%s' % (day, today, day_hs, day_eb) contents.append(line) return contents
def locust_on_publish(self, _client: mqtt.Client, _userdata, mid) -> None: """ Publishing callback function. """ end_time = time.time() message = self.pubmmap.pop(mid, None) if message is None: self.log.append_log("publish message is none\n") Utils.fire_locust_failure( request_type=REQUEST_TYPE, name=MESSAGE_TYPE_PUB, response_time=0, exception=ValueError("Published message could not be found"), ) return total_time = Utils.time_delta(message['start_time'], end_time) Utils.fire_locust_success( request_type=REQUEST_TYPE, name=message['name'], response_time=total_time, response_length=len(message['payload']), ) self.recvmqueue.put({ # The end_time for publish is the start_time for the subscribed clients 'start_time': end_time, })
class Item(Base): def __init__(self, item): super().__init__() self.c.execute( '''SELECT id, name, emoji, description, price_min, price_max FROM wars_items WHERE id=?''', (item, )) item_check = self.c.fetchone() if not item_check: raise Exception("no item by that id") self.id, self.name, self.emoji, self.description, self.price_min, self.price_max = item_check self.utils = Utils() def edit(self, field, value): self.c.execute( '''UPDATE wars_items SET ''' + field + '''=? WHERE id=?''', (value, self.id)) self.conn.commit() self.utils.update_prices(self.id) self.__init__(self.id) def get_price(self): try: return random.randrange(self.price_min, self.price_max) except ValueError: return self.price_min
def save_image(file, id, ext): files_local_path, files_route = Utils.mark_resource("uploads/%s.%s" % (id, ext)) file.save(files_local_path) info = "Image - %sb, %spx" % (Utils.get_file_size( files_local_path), Utils.get_file_resolution(files_local_path)) return files_route, files_route, info
def test_fire_locust_success(mock_events): """ fire_locust_success() should fire a Locust event on success """ Utils.fire_locust_success() mock_events.request_success.fire.assert_called_once() mock_events.reset_mock()
def save_music(file, id, ext): files_local_path, files_route = Utils.mark_resource("uploads/%s.%s" % (id, ext)) file.save(files_local_path) info = "Video - %sb, %ss" % (Utils.get_file_size(files_local_path), Utils.get_media_length(files_local_path)) return files_route, Config.AUDIO_FILES_PREVIEWS_ROUTE, info
def save_model(self, model, epochs, optimizer_state_dict, lr_data, class_correct, class_total, path="savedmodels/finalmodelwithdata.pt"): ''' :param model: model whose data wi;; be saved :param epochs: no of epochs model was trained for :param optimizer_state_dict: optimizer state dict to be saved :param lr_data: lr data to be saved :param class_correct: class_correct to be saved :param class_total: class_total to be saved :param path: path where model is to be saved :return: None ''' train_losses, train_acc = self.gettraindata() test_losses, test_acc = self.gettestdata() Utils.savemodel(model=model, epoch=epochs, path=path, optimizer_state_dict=optimizer_state_dict, train_losses=train_losses, train_acc=train_acc, test_acc=test_acc, test_losses=test_losses, lr_data=lr_data, class_correct=class_correct, class_total=class_total)
def test_fire_locust_failure(mock_events): """ fire_locust_failure() should fire a Locust event on failure """ Utils.fire_locust_failure() mock_events.request_failure.fire.assert_called_once() mock_events.reset_mock()
def __init__(self, data, surrogate): print("Initializing FlexiBO class") self.df = data with open("config.yaml", "r") as fp: config = yaml.load(fp) cfg = ConfigSpaceReal("hardware", "os", config["config"]["network"]["net"]) (self.E, self.O, self.measurement) = cfg.set_design_space() self.network = config["config"]["network"]["net"] self.NUM_ITER = 200 self.NUM_OBJ = 2 self.O1_IND = config["config"]["index"]["O1"] self.O2_IND = config["config"]["index"]["O2"] self.m1 = config["config"]["objective"]["O1"] self.m2 = config["config"]["objective"]["O2"] self.O1_COST = config["config"]["evaluation_cost"]["O1"] self.O2_COST = config["config"]["evaluation_cost"]["O2"] self.sampling = Sampling(self.O1_IND, self.O2_IND, self.O1_COST, self.O2_COST) self.utils = Utils(self.O1_IND, self.O2_IND) self.surrogate = surrogate if self.surrogate == "GP": from src.surrogate_model import GPSurrogateModel self.SM = GPSurrogateModel() else: print("[ERROR]: Surrogate model not supported") (self.X, self.Y1, self.Y2) = self.prepare_training_data() self.perform_bo_loop()
def inicializar_integrantes(self, elemento): if not elemento.subelemento.integrantes.ativo: elemento.subelemento.integrantes.iniciar(master=elemento) for integrante in elemento.subelemento.integrantes.lista: integrante.destroy() elemento.subelemento.integrantes.lista = [] fim = elemento.dados['duracao'] inicio = elemento.dados['data'] for _elemento in self.elementos: data = _elemento.dados['data'] if (_elemento.defs.tipo != 'apresentacao' or Utils.comparar_(data1=fim, data2=data) in [-1] or Utils.comparar_(data1=inicio, data2=data) in [1]): continue integrante = self.criar_label() integrante.defs.cnf['text'] = _elemento.dados['titulo'] integrante.defs.cnf['width'] = 102 integrante.defs.cnf['height'] = 1 integrante.defs.cnf['bg'] = 'orange' integrante.defs.pack['side'] = 'top' integrante.iniciar(master=elemento.subelemento.integrantes) elemento.subelemento.integrantes.lista.append(integrante)
def main(): window_width = 800 window_height = 925 button_height = 25 pg.init() pg.display.set_caption("Maze") screen = pg.display.set_mode((window_width, window_height)) utility = Utils(window_height, window_width, button_height, screen) utility.create_buttons() utility.draw_window() closed = False event_key = None # Continue running until the user closes the window while not closed: utility.handle_event(event_key) event_key = None utility.draw_window() event = pg.event.poll() if event.type == pg.QUIT: closed = True elif event.type == pg.MOUSEBUTTONUP: for key, button in utility.buttons.items(): if button.select(event.pos): button.pressed += 1 event_key = key pg.quit()
def choose_best_variable(self, state, current_unsat_clause): """ Find the best variable to flip :param state: The current state :param current_unsat_clause: The current total unsatisfied clauses :return: The best count of unsatisfied variables """ best_flip = current_unsat_clause best = state.copy() # Loop through all variables for i in range(len(self.variables)): # Check if variable is in tabu list, if it is then continue to next iteration if self.tabu.is_item_in_queue(self.variables[i]): continue tmp_state = state.copy() # Flip the variable Utils.flip_variable(tmp_state, self.variables[i]) # Compute the solution _, unsat_clause = self.solution_status(self.formula, tmp_state) # If this solution is better than the current best, then make this solution the new best if unsat_clause < best_flip: best_flip = unsat_clause best = tmp_state # Add new best solution flip variable to tabu list self.tabu.add_to_queue(self.variables[i]) return best
def load_gdp(phase): """ Parameters: =========== phase (string) : {'late', 'early'} Returns ======= pd.DataFrame (object) """ if phase is 'early': raw = pd.read_csv('datafolder//raw//gdp//gdp_early_period.csv', skiprows=4) elif phase is 'late': raw = pd.read_csv('datafolder//raw//gdp//gdp_late_period.csv', skiprows=4) else: raise ValueError('Phase must be either "late" or "early"') raw.rename(columns={'Area': 'time'}, inplace=True) raw.set_index('time', inplace=True) raw.drop('Fips', axis=1, inplace=True) raw = raw.transpose() raw.dropna(axis=1, inplace=True) raw = Utils.rename(raw) return Utils.drop_cols(raw)
class DataBase: def __init__(self): self.name = None self.working_dir = os.getcwd() self.db_dir = os.path.join(self.working_dir, 'db') self.utils = Utils() def add_person(self, name, frame): try: log.info("Adding {} to database...".format(name)) file_name = os.path.join( self.db_dir, '{0}-{1}.jpg'.format(name, str(uuid.uuid4()))) cv2.imwrite(file_name, frame) log.info("{0} was added to database to {1} file.".format( name, file_name)) return file_name except Exception as ex: log.error("Failed to add person to database: {}".format(ex)) @staticmethod def get_formatted_person_name(file_path): return file_path.split('/')[-1].split('-')[0] def remove_person(self, name): log.info("Removing all {}-* images from database...".format(name)) files = self.utils.get_files_from_folder_recursively( self.get_db_dir(), pattern='^{}-.*$'.format(name)) if files: for file in files: if os.path.isfile(file): os.remove(file) log.info( "Person with name {} was successfully removed from database." .format(name)) else: log.warning( "Person with name {} is not in database. Nothing to remove.". format(name)) def set_db_dir(self, db_dir): if db_dir: self.db_dir = db_dir try: if not os.path.exists(self.db_dir): os.makedirs(self.db_dir) log.info("Database folder was created in {}.".format( self.db_dir)) else: log.info("Using database from {} folder.".format(self.db_dir)) except Exception as ex: raise Exception( 'Unable to initialize database folder: {}'.format(ex)) def get_db_dir(self): return self.db_dir def get_all_persons(self): return self.utils.get_files_from_folder_recursively( self.db_dir, pattern=r'^.*\.(jpg|png|tiff|bmp)$')
def __init__(self, bot): self.bot = bot self.conn = sqlite3.connect('mybdb.db') self.c = self.conn.cursor() self.items = ItemManager() self.stores = StoreManager() self.locations = LocationManager() self.utils = Utils()
def __init__(self, o1_ind, o2_ind, o1_cost, o2_cost): print("[STATUS]: Initializing Sample Class") self.O1_IND = o1_ind self.O2_IND = o2_ind self.NUM_OBJ = 2 self.O1_COST = o1_cost self.O2_COST = o2_cost self.utils = Utils(o1_ind, o2_ind)
def print_head(self): help = "\n" help += Utils.pbdiv() help += Utils.pbn() help += Utils.pb("wtstamp - a flexible timestamp tool") help += Utils.pbn() help += Utils.pbdiv() print(help, end='')
def new_cert(tenant: str, device_id: str) -> Thing: """ Creates/renovates the certificate for a device. """ Utils.validate_tenant(tenant) Utils.validate_device_id(device_id) thing = Thing(tenant, device_id) return thing
def Init_data(self): # 加载权重 Utils.Load_W(self) # 计算每个权重Wi的T个邻居 Utils.cpt_W_Bi_T(self) # 创建种群 self.GA_DE_Utils.Creat_Pop(self) # 初始化Z集,最小问题0,0 Utils.cpt_Z(self)
def __init__(self): self.utils = Utils(os.path.join("resources")) self.config = self.utils.get_config() host = self.config["elastic"]['host'] port = self.config["elastic"]['port'] username = self.config["elastic"]['username'] password = self.config["elastic"]['password'] self.elastic_search = Elasticsearch("http://{}:{}@{}:{}".format( username, password, host, port))
def col_names(cls): df = cls.col_selector() new_cols = [ df.columns[i].split('_')[0] for i in range(len(df.columns)) ] df.columns = [new_cols] df = Utils.rename(df) df = Utils.drop_cols(df) return df
print 'not grabbed' break try: pars = zip(frames, paths) pool.map(getRep, pars) except Exception as e: print e def run_face_clustering(facelink): files = [filename for filename in os.listdir(facelink)] id = [file[:24] for file in files] reps = [] for file in files: img = cv2.imread(facelink+file) rep = parse_img(img) reps.append(rep) return reps if __name__ == '__main__': mongo_ip = args.mongo_ip mongo_port = args.mongo_port db = Utils.init_mongo_client(mongo_ip, mongo_port) collection_name = 'door_data' iterable = db[collection_name].find(no_cursor_timeout=True) video_link = '/home/cuda/workspace/experiment_data/videos/' face_link = '/home/cuda/workspace/experiment_data/faces_v2/' #run_face_extraction(iterable, face_link, video_link) reps = run_face_clustering(face_link) pickle.dump(reps, open(face_link+"save.p", "wb")) X = np.array(reps) kmeans = KMeans(n_clusters=70, random_state=0).fit(X) print kmeans.labels_