def mark_windows_with_ns(self, n_mark): if self._mixed_windows is None: raise Error("Mixed windows have not been computed") counter = 0 for window in self._mixed_windows: wga_w = window.get_window(wtype=WindowType.WGA) n_wga_w = window.get_window(wtype=WindowType.NO_WGA) if wga_w.has_base("N") == True and\ n_wga_w.has_base("N") == False: raise Error("WGA Window {0} has N " "but Non WGA Window {1} does not".format( wga_w.idx, n_wga_w.idx)) elif wga_w.has_base("N") == False and\ n_wga_w.has_base("N") == True: raise Error("WGA Window {0} does not have N " "but Non WGA Window {1} does".format( wga_w.idx, n_wga_w.idx)) ## Add error if one has N and the other not if wga_w.has_base("N") or n_wga_w.has_base("N"): wga_w.set_window_rd_mark(mark=n_mark) wga_w.state = WindowType.N_WIN n_wga_w.set_window_rd_mark(mark=n_mark) n_wga_w.state = WindowType.N_WIN counter += 1 return counter
def _mov(_): if len(parms) != 2: raise Error('Syntax error', line_no) op = OP2 dreg = parse_reg(parms[0]) if parms[1][0] == '#': value = parms[1][1:] if value[0].isalpha(): if value in symbols: value = symbols[value] else: raise Error('Symbol not defined: ' + value, line_no) else: value = parse_int(value) if op == OP2 and value >= 0 and value < 64: opcode = 0o61_00_00 | dreg | (value << 6) return [OP1, line_no, code_adrs, opcode] else: opcode = 0o00_07_00 | dreg return [op, line_no, code_adrs, opcode, value] else: sreg = parse_reg(parms[1]) opcode = 0o42_00_00 | dreg | (sreg << 6) return [OP1, line_no, code_adrs, opcode]
def process_line(sline): global parts, parms, code_adrs, const_adrs parts = parse_line(sline) if len(parts) == 0: return if parts[0] == 'INCLUDE': file_name = parts[2][1:-1] read_file(file_name) return if parts[0] != None: if parts[0] in symbols: raise Error('Duplicate label: ' + parts[0], line_no) symbols[parts[0]] = code_adrs if len(parts[1]) == 0: return if parts[1] not in op_table: raise Error('Invalid opcode: ' + parts[1], line_no) parms = parts[2].split(',') op = op_table[parts[1]] bin_line = op[0](op[1]) if bin_line != None: bin_code.append(bin_line) opc = bin_line[0] if opc == OP1 or opc == ADR: code_adrs += 1 elif opc == OP2 or opc == IMM: code_adrs += 2
def __init__(self, msg, attribute): Error.__init__(self, f"Node missing attribute '{attribute}': {msg}") self.msg = msg self.attribute = attribute self.args = ( msg, attribute, )
def _level(_): if len(parms) != 1: raise Error('Syntax error', line_no) N = parse_int(parts[2]) if N > 15 or N < 0: raise Error('LEVEL value out of range', line_no) return [OP1, line_no, code_adrs, 0o00_05_00 | N]
def _sxx(opcode): if len(parms) != 1: raise Error('Syntax error', line_no) dst = parse_int(parms[0]) if dst < 0 or dst > 63: raise Error('Invalid bit number', line_no) return [OP1, line_no, code_adrs, opcode | dst]
def _timer(_): if len(parms) != 1: raise Error('Syntax error', line_no) N = parse_int(parts[2]) if N > 0x0FFF or N < 0: raise Error('TIMER value out of range', line_no) return [OP1, line_no, code_adrs, 0o05_00_00 | N]
def _bit(opcode): if len(parms) != 1: raise Error('Syntax error', line_no) value = parse_int(parms[0]) if value < 0 or value > 63: raise Error('Invalid bit number', line_no) opcode = opcode | value | (value << 6) return [OP1, line_no, code_adrs, opcode | value]
def handler(*args, **kwargs): try: return func(*args, **kwargs) except NoResultFound: logging.error("NoResultFound caught from SQLAlchemy") raise ResourceNotFound("Resource not found") except IntegrityError as e: logging.debug("Integrity error caught from SQLAlchemy (%s)" % str(e)) raise Error("Error data may be malformed") except SQLAlchemyError as e: raise Error("An error occurred (%s)" % str(e))
def _cmp(_): if len(parms) != 2 or len(parms[0]) == 0 or len(parms[1]) == 0: raise Error('Syntax error', line_no) dreg = parse_reg(parms[0]) sreg = parse_reg(parms[1]) if sreg == 63: raise Error('Syntax error', line_no) opcode = 0o04_00_00 | dreg | (sreg << 6) return [OP1, line_no, code_adrs, opcode]
def _simple2(opcode): if len(parms) != 2: raise Error('Syntax error', line_no) dst = parse_int(parms[0]) src = parse_int(parms[1]) if src < 0 or src > 63 or dst < 0 or dst > 63: raise Error('Range error', line_no) opcode |= dst opcode |= src << 6 return [OP1, line_no, code_adrs, opcode]
def parse_reg(str): if str[0].isalpha(): if str not in symbols: raise Error('Invalid register: ' + str, line_no) reg = symbols[str] else: reg = int(str) if reg < 0 or reg > 63: raise Error('Invalid register: ' + str, line_no) return reg
def _ds(_): global data_adrs if parts[0] != None: symbols[parts[0]] = data_adrs else: raise Error('DS requires a label', line_no) if len(parms) != 1: raise Error('Syntax error', line_no) line = [DSL, line_no, data_adrs] data_adrs += parse_int(parms[0]) return line
def _ldr(opcode): if len(parms) != 2: raise Error('Syntax error', line_no) if len(parms[1]) < 3 or \ parms[1][0] != '(' or \ parms[1][-1] != ')': raise Error('Syntax error', line_no) dreg = parse_reg(parms[0]) sreg = parse_reg(parms[1][1:-1]) opcode |= sreg << 6 opcode |= dreg return [OP1, line_no, code_adrs, opcode]
def get_value(self, port_number): """Return value read from port. It can be used both for input and output ports. The port value is read in the same manner it is written to. The whole 8-bit registry is read, converted to binary string, and value for given port is read from it. """ self._validate_port_number(port_number) value = self._check_no_hardware_port_value(port_number) if value is not None: return value else: value = get_bus().read_byte_data( self._address, self._get_read_register(port_number)) # Decode. Incoming binary value represent the whole 8 bit register. # So format the number to be binary representation without # '0b' prefix, revert it and get the port value. # logging.debug( # 'Reading value for port %s -> %s', # self.get_port(port_number), # format(value, '#010b')) index = (port_number - 1) % 8 value = format(value, '08b')[::-1][index] if value == '0': return self.LOW elif value == '1': return self.HIGH else: # This actually should never happen. raise Error('Could not read value for port %s', self.get_port(port_number))
def save_mixed_windows_statistic(self, statistic, tips): if self._mixed_windows is None: raise Error("Mixed windows have not been computed") save_windows_statistic(windows=self._mixed_windows, statistic="mean", region_id=self._idx, tips=tips)
def update(self, coach: Coach, data: dict): if 'firstname' in data: coach.firstname = data['firstname'] if 'lastname' in data: coach.lastname = data['lastname'] if 'email' in data: coach.email = data['email'] if 'contract' in data: coach.contract = data['contract'] if 'degree' in data: coach.degree = data['degree'] if 'address' in data: if 'street' in data['address']: coach.address.street = data['address']['street'] if 'postal_code' in data['address']: coach.address.postal_code = data['address']['postal_code'] if 'city' in data['address']: coach.address.city = data['address']['city'] if 'country' in data['address']: coach.address.country = data['address']['country'] if 'user' in data: if 'username' in data['user']: coach.user.username = data['user']['username'] if 'password' in data['user']: coach.user.hash_password(data['user']['password']) try: self._database_session.merge(coach) self._database_session.flush() except IntegrityError: raise Error("Error data may be malformed") return coach
def check_windows_sanity(self): # check if the rest of the windows # are aligned self.get_mixed_windows() if len(self._windows[WindowType.NO_WGA]) > len( self._windows[WindowType.WGA]): print("{0} Windows size mismatch" " WGA {1} NON_WGA {2}".format( WARNING, len(self._windows[WindowType.WGA]), len(self._windows[WindowType.NO_WGA]))) elif len(self._windows[WindowType.NO_WGA]) < len( self._windows[WindowType.WGA]): print("{0} Windows size mismatch" " WGA {1} NON_WGA {2}".format( WARNING, len(self._windows[WindowType.WGA]), len(self._windows[WindowType.NO_WGA]))) for window in self._mixed_windows: start_wga, end_wga = window.get_window( wtype=WindowType.WGA).get_start_end_pos() start_no_wga, end_no_wga = window.get_window( wtype=WindowType.NO_WGA).get_start_end_pos() if (start_wga, end_wga) != (start_no_wga, end_no_wga): raise Error("Invalid window matching " "window WGA at {0}, {1} " "matched with NO WGA window at {2}, {3}".format( start_wga, end_wga, start_no_wga, end_no_wga))
def ajout_stream(self, music: Music): music.stream += 1 try: self._database_session.merge(music) self._database_session.flush() except IntegrityError: raise Error("Erreur stream")
def update(self, machine: Machine, data: dict): if 'name' in data: machine.name = data['name'] if 'brand' in data: machine.brand = data['brand'] if 'provider' in data: machine.provider = data['provider'] if 'muscular_group' in data: machine.muscular_group = data['muscular_group'] try: self._database_session.merge(machine) self._database_session.flush() logging.debug( "MachineDAO:update() on name : {}, brand : {}, provider : {}". format(data['name'], data['brand'], data['provider'])) except IntegrityError: logging.error( "MachineDAO:update() on name : {}, brand : {}, provider : {}". format(data['name'], data['brand'], data['provider'])) raise Error("ERROR (during update) : Machine doesn't exist.") return machine
def remove_outliers(windows, removemethod, config): if removemethod == "zscore": return zscore_outlier_removal(windows=windows, config=config) elif removemethod == "means_cutoff": return means_cutoff_outlier_removal(windows=windows, config=config) raise Error("Unknown outlier removal method: {0}".format(removemethod))
def delete(self, entity): try: self._database_session.delete(entity) logging.debug("CoachDAO:delete()") except SQLAlchemyError as error: logging.error("CoachDAO:delete()") raise Error(str(error))
def get_rd_mean_sequence(self, size, window_type, exclude_gaps): if self._mixed_windows is None: raise Error("Mixed windows have not been computed") sequence = [] if size < len(self._mixed_windows): counter = 0 for window in self._mixed_windows: if exclude_gaps and window.is_gap_window(): continue sequence.append(window.get_rd_statistic(statistics="mean", name=window_type)) counter += 1 if counter == size: break else: print("{0} Region size is less than {1}".format(WARNING, size)) for window in self._mixed_windows: if exclude_gaps and window.is_gap_window(): continue sequence.append(window.get_rd_statistic(statistics="mean", name=window_type)) return sequence
def remove_outliers(self, configuration): if self._mixed_windows is None: raise Error("Mixed windows have not been computed") # compute the statistics wga_means = array.array('d') no_wga_means = array.array('d') for window in self._mixed_windows: if not window.is_gap_window(): wga_means.append(window.get_rd_statistic(statistics="mean", name=WindowType.WGA)) no_wga_means.append(window.get_rd_statistic(statistics="mean", name=WindowType.NO_WGA)) if len(wga_means) == 0 or len(no_wga_means) == 0: print("{0} Cannot remove outliers for region. " "Empty RD list detected".format(WARNING)) return wga_statistics = compute_statistic(data=wga_means, statistics="all") no_wga_statistics = compute_statistic(data=no_wga_means, statistics="all") config = configuration["outlier_remove"]["config"] config["statistics"] = {WindowType.NO_WGA: no_wga_statistics, WindowType.WGA: wga_statistics} self._mixed_windows = \ remove_outliers(windows=self._mixed_windows, removemethod=configuration["outlier_remove"]["name"], config=config)
def find_name_from_state(state, **kwargs): for clst in kwargs['clusters']: if kwargs['clusters'][clst]['state'] == state: return clst raise Error("No cluster with state {0} found".format(state))
def _cpi(_): if len(parms) != 2 or len(parms[0]) == 0 or len(parms[1]) == 0: raise Error('Syntax error', line_no) dreg = parse_reg(parms[0]) value = parms[1] if value[0].isalpha(): if value not in symbols: raise Error('Symbol not defined: ' + value, line_no) value = symbols[value] else: value = parse_int(value) opcode = 0o04_77_00 | dreg return [OP2, line_no, code_adrs, opcode, value]
def remove_outliers(self, configuration): if self._mixed_windows is None: raise Error("Mixed windows have not been computed") # compute the statistis wga_rds = [] no_wga_rds = [] for window in self._mixed_windows: if not window.is_n_window(): wga_rds.extend(window.get_rd_counts(name=WindowType.WGA)) no_wga_rds.extend(window.get_rd_counts(name=WindowType.NO_WGA)) wga_statistics = compute_statistic(data=wga_rds, statistics="all") no_wga_statistics = compute_statistic(data=no_wga_rds, statistics="all") config = configuration["outlier_remove"]["config"] config["statistics"] = { WindowType.NO_WGA: no_wga_statistics, WindowType.WGA: wga_statistics } self._mixed_windows = \ remove_outliers(windows=self._mixed_windows, removemethod=configuration["outlier_remove"]["name"], config=config)
def log(class_name='Error', message=''): """ Syslog error handler :param str class_name: log name :param str message: log message :raise Error :return: None """ try: filesystem.makedir(Config.logdir) try: logging.config.dictConfig(Config.exceptions) except AttributeError: logging.config.fileConfig(Config.legacy_config) logger = logging.getLogger('exceptions') func = inspect.currentframe().f_back.f_code message = "{class_name}: {message} in {file} -> {func}() line {line}".format( class_name=class_name, message=message, file=func.co_filename, func=func.co_name, line=func.co_firstlineno) logger.error(message) except (Error, ValueError) as e: raise Error(e)
def __init__(self, driver, encoding=None, disabled=False, defaultsymbol='?', encoder=None): """ :param driver: :param encoding: If you know the current encoding of the printer when initializing this class, set it here. If the current encoding is unknown, the first character emitted will be a codepage switch. :param disabled: :param defaultsymbol: :param encoder: """ if disabled and not encoding: raise Error( 'If you disable magic encode, you need to define an encoding!') self.driver = driver self.encoder = encoder or Encoder(driver.profile.get_code_pages()) self.encoding = self.encoder.get_encoding_name( encoding) if encoding else None self.defaultsymbol = defaultsymbol self.disabled = disabled
def update(self, coach: Coach, data: dict): if 'firstname' in data: coach.firstname = data['firstname'] if 'lastname' in data: coach.lastname = data['lastname'] if 'email' in data: coach.email = data['email'] if 'phone_number' in data: coach.phone_number = data['phone_number'] if 'degree' in data: coach.degree = data['degree'] if 'specialties' in data: coach.specialties = data['specialties'] try: self._database_session.merge(coach) self._database_session.flush() logging.debug( "CoachDAO:update() on firstname : {}, lastname : {}".format( data['firstname'], data['lastname'])) except IntegrityError: logging.error( "CoachDAO:update() on firstname : {}, lastname : {}".format( data['firstname'], data['lastname'])) raise Error("ERROR (during update) : Coach doesn't exist.") return coach