Python re.fullmatch() Examples
The following are 30
code examples of re.fullmatch().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
re
, or try the search function
.
Example #1
Source File: configtypes.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
def to_py(self, value: _StrUnset) -> _StrUnsetNone: self._basic_py_validation(value, str) if isinstance(value, usertypes.Unset): return value elif not value: return None if not self.font_regex.fullmatch(value): # pragma: no cover # This should never happen, as the regex always matches everything # as family. raise configexc.ValidationError(value, "must be a valid font") if (value.endswith(' default_family') and self.default_family is not None): value = value.replace('default_family', self.default_family) if 'default_size ' in value and self.default_size is not None: value = value.replace('default_size', self.default_size) return value
Example #2
Source File: env.py From gpu-utils with GNU General Public License v3.0 | 6 votes |
def read_amd_driver_version_debian(self) -> bool: """ Read the AMD driver version and store in GutConst object. :return: True if successful """ for pkgname in ['amdgpu', 'amdgpu-core', 'amdgpu-pro', 'rocm-utils']: try: dpkg_out = subprocess.check_output(shlex.split('{} -l {}'.format(self.cmd_dpkg, pkgname)), shell=False, stderr=subprocess.DEVNULL).decode().split('\n') except (subprocess.CalledProcessError, OSError): continue for dpkg_line in dpkg_out: for driverpkg in ['amdgpu', 'rocm']: if re.search(driverpkg, dpkg_line): LOGGER.debug(dpkg_line) dpkg_items = dpkg_line.split() if len(dpkg_items) > 2: if re.fullmatch(r'.*none.*', dpkg_items[2]): continue print('AMD: {} version: {}'.format(driverpkg, dpkg_items[2])) return True print('amdgpu/rocm version: UNKNOWN') return False
Example #3
Source File: GPUmodule.py From gpu-utils with GNU General Public License v3.0 | 6 votes |
def get_plot_data(self) -> dict: """ Return a dictionary of dynamic gpu parameters used by gpu-plot to populate a df. :return: Dictionary of GPU state info for plot data. """ gpu_state = {'Time': str(self.energy['tn'].strftime(env.GUT_CONST.TIME_FORMAT)), 'Card#': int(self.prm.card_num)} for table_item in self.table_parameters: gpu_state_str = str(re.sub(PATTERNS['MHz'], '', str(self.get_params_value(table_item)))).strip() if gpu_state_str == 'nan': gpu_state[table_item] = np_nan elif gpu_state_str.isnumeric(): gpu_state[table_item] = int(gpu_state_str) elif re.fullmatch(PATTERNS['IS_FLOAT'], gpu_state_str): gpu_state[table_item] = float(gpu_state_str) elif gpu_state_str == '' or gpu_state_str == '-1' or gpu_state_str == 'NA' or gpu_state_str is None: gpu_state[table_item] = 'NA' else: gpu_state[table_item] = gpu_state_str return gpu_state
Example #4
Source File: simpletable.py From TheCannon with MIT License | 6 votes |
def get(self, v, full_match=False): """ returns a table from columns given as v this function is equivalent to :func:`__getitem__` but preserve the Table format and associated properties (units, description, header) Parameters ---------- v: str pattern to filter the keys with full_match: bool if set, use :func:`re.fullmatch` instead of :func:`re.match` """ new_keys = self.keys(v) t = self.__class__(self[new_keys]) t.header.update(**self.header) t._aliases.update((k, v) for (k, v) in self._aliases.items() if v in new_keys) t._units.update((k, v) for (k, v) in self._units.items() if v in new_keys) t._desc.update((k, v) for (k, v) in self._desc.items() if v in new_keys) return t
Example #5
Source File: utils.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
def expand_windows_drive(path: str) -> str: r"""Expand a drive-path like E: into E:\. Does nothing for other paths. Args: path: The path to expand. """ # Usually, "E:" on Windows refers to the current working directory on drive # E:\. The correct way to specifify drive E: is "E:\", but most users # probably don't use the "multiple working directories" feature and expect # "E:" and "E:\" to be equal. if re.fullmatch(r'[A-Z]:', path, re.IGNORECASE): return path + "\\" else: return path
Example #6
Source File: urlutils.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
def qurl_from_user_input(urlstr: str) -> QUrl: """Get a QUrl based on a user input. Additionally handles IPv6 addresses. QUrl.fromUserInput handles something like '::1' as a file URL instead of an IPv6, so we first try to handle it as a valid IPv6, and if that fails we use QUrl.fromUserInput. WORKAROUND - https://bugreports.qt.io/browse/QTBUG-41089 FIXME - Maybe https://codereview.qt-project.org/#/c/93851/ has a better way to solve this? https://github.com/qutebrowser/qutebrowser/issues/109 Args: urlstr: The URL as string. Return: The converted QUrl. """ # First we try very liberally to separate something like an IPv6 from the # rest (e.g. path info or parameters) match = re.fullmatch(r'\[?([0-9a-fA-F:.]+)\]?(.*)', urlstr.strip()) if match: ipstr, rest = match.groups() else: ipstr = urlstr.strip() rest = '' # Then we try to parse it as an IPv6, and if we fail use # QUrl.fromUserInput. try: ipaddress.IPv6Address(ipstr) except ipaddress.AddressValueError: return QUrl.fromUserInput(urlstr) else: return QUrl('http://[{}]{}'.format(ipstr, rest))
Example #7
Source File: GPUmodule.py From gpu-utils with GNU General Public License v3.0 | 6 votes |
def is_valid_pstate_list_str(self, ps_str: str, clk_name: str) -> bool: """ Check if the given p-states are valid for the given clock. :param ps_str: String of comma separated pstate numbers :param clk_name: The target clock name :return: True if valid """ if ps_str == '': return True if not re.fullmatch(PATTERNS['VALID_PS_STR'], ps_str): return False ps_list = self.prm.mclk_mask.split(',') if clk_name == 'MCLK' else self.prm.sclk_mask.split(',') for ps_val in ps_str.split(): if ps_val not in ps_list: return False return True
Example #8
Source File: configtypes.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
def to_py(self, value: _StrUnset) -> _StrUnsetNone: self._basic_py_validation(value, str) if isinstance(value, usertypes.Unset): return value elif not value: return None match = self.font_regex.fullmatch(value) if not match: # pragma: no cover # This should never happen, as the regex always matches everything # as family. raise configexc.ValidationError(value, "must be a valid font") for group in 'style', 'weight', 'namedweight', 'size': if match.group(group): raise configexc.ValidationError(value, "may not include a " "{}!".format(group)) return value
Example #9
Source File: docutils.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
def _parse_arg_inside(self, line: str) -> bool: """Parse subsequent argument lines.""" argname = self._cur_arg_name assert argname is not None descs = self.arg_descs[argname] assert isinstance(descs, list) if re.fullmatch(r'[A-Z][a-z]+:', line): if not descs[-1].strip(): del descs[-1] return True elif not line.strip(): descs.append('\n\n') elif line[4:].startswith(' '): descs.append(line.strip() + '\n') else: self._process_arg(line) return False
Example #10
Source File: configtypes.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
def to_py(self, value: _StrUnset) -> _StrUnsetNone: self._basic_py_validation(value, str) if isinstance(value, usertypes.Unset): return value elif not value: return None self._validate_encoding(value) self._validate_valid_values(value) if self.forbidden is not None and any(c in value for c in self.forbidden): raise configexc.ValidationError(value, "may not contain the chars " "'{}'".format(self.forbidden)) if self.minlen is not None and len(value) < self.minlen: raise configexc.ValidationError(value, "must be at least {} chars " "long!".format(self.minlen)) if self.maxlen is not None and len(value) > self.maxlen: raise configexc.ValidationError(value, "must be at most {} chars " "long!".format(self.maxlen)) if self.regex is not None and not re.fullmatch(self.regex, value): raise configexc.ValidationError(value, "does not match {}" .format(self.regex)) return value
Example #11
Source File: app_config.py From dash-masternode-tool with MIT License | 6 votes |
def set_rpc_encryption_pubkey(self, key: str): """ AES public key for additional RPC encryption, dedicated for calls transmitting sensitive information like protx. Accepted formats: PEM, DER. """ try: if key: # validate public key by deserializing it if re.fullmatch(r'^([0-9a-fA-F]{2})+$', key): serialization.load_der_public_key(bytes.fromhex(key), backend=default_backend()) else: pubkey = serialization.load_pem_public_key(key.encode('ascii'), backend=default_backend()) raw = pubkey.public_bytes(serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo) key = raw.hex() if self.__rpc_encryption_pubkey_object and (self.__rpc_encryption_pubkey_der != key or not key): self.__rpc_encryption_pubkey_der = None self.__rpc_encryption_pubkey_der = key except Exception as e: logging.exception('Exception occurred') raise
Example #12
Source File: request_connector.py From thingsboard-gateway with Apache License 2.0 | 6 votes |
def on_attributes_update(self, content): try: for attribute_request in self.__attribute_updates: if fullmatch(attribute_request["deviceNameFilter"], content["device"]) and fullmatch(attribute_request["attributeFilter"], list(content["data"].keys())[0]): converted_data = attribute_request["converter"].convert(attribute_request, content) response_queue = Queue(1) request_dict = {"config": {**attribute_request, **converted_data}, "request": request} attribute_update_request_thread = Thread(target=self.__send_request, args=(request_dict, response_queue, log), daemon=True, name="Attribute request to %s" % (converted_data["url"])) attribute_update_request_thread.start() attribute_update_request_thread.join() if not response_queue.empty(): response = response_queue.get_nowait() log.debug(response) del response_queue except Exception as e: log.exception(e)
Example #13
Source File: request_connector.py From thingsboard-gateway with Apache License 2.0 | 6 votes |
def server_side_rpc_handler(self, content): try: for rpc_request in self.__rpc_requests: if fullmatch(rpc_request["deviceNameFilter"], content["device"]) and fullmatch(rpc_request["methodFilter"], content["data"]["method"]): converted_data = rpc_request["converter"].convert(rpc_request, content) response_queue = Queue(1) request_dict = {"config": {**rpc_request, **converted_data}, "request": request} request_dict["config"].get("uplink_converter") rpc_request_thread = Thread(target=self.__send_request, args=(request_dict, response_queue, log), daemon=True, name="RPC request to %s" % (converted_data["url"])) rpc_request_thread.start() rpc_request_thread.join() if not response_queue.empty(): response = response_queue.get_nowait() log.debug(response) self.__gateway.send_rpc_reply(device=content["device"], req_id=content["data"]["id"], content=response[2]) self.__gateway.send_rpc_reply(success_sent=True) del response_queue except Exception as e: log.exception(e)
Example #14
Source File: opcua_uplink_converter.py From thingsboard-gateway with Apache License 2.0 | 6 votes |
def convert(self, config, data): device_name = self.__config["deviceName"] result = {"deviceName": device_name, "deviceType": self.__config.get("deviceType", "OPC-UA Device"), "attributes": [], "telemetry": [], } try: information_types = {"attributes": "attributes", "timeseries": "telemetry"} for information_type in information_types: for information in self.__config[information_type]: path = TBUtility.get_value(information["path"], get_tag=True) if isinstance(config, tuple): config_information = config[0].replace('\\\\', '\\') if path == config[0].replace('\\\\', '\\') or fullmatch(path, config[0].replace('\\\\', '\\')) else config[1].replace('\\\\', '\\') else: config_information = config.replace('\\\\', '\\') if path == config_information or fullmatch(path, config_information) or path.replace('\\\\', '\\') == config_information: result[information_types[information_type]].append({information["key"]: information["path"].replace("${"+path+"}", str(data))}) return result except Exception as e: log.exception(e)
Example #15
Source File: rest_connector.py From thingsboard-gateway with Apache License 2.0 | 6 votes |
def on_attributes_update(self, content): try: for attribute_request in self.__attribute_updates: if fullmatch(attribute_request["deviceNameFilter"], content["device"]) and \ fullmatch(attribute_request["attributeFilter"], list(content["data"].keys())[0]): converted_data = attribute_request["downlink_converter"].convert(attribute_request, content) response_queue = Queue(1) request_dict = {"config": {**attribute_request, **converted_data}, "request": regular_request} with self._app.test_request_context(): attribute_update_request_thread = Thread(target=self.__send_request, args=(request_dict, response_queue, log), daemon=True, name="Attribute request to %s" % (converted_data["url"])) attribute_update_request_thread.start() attribute_update_request_thread.join() if not response_queue.empty(): response = response_queue.get_nowait() log.debug(response) del response_queue except Exception as e: log.exception(e)
Example #16
Source File: gcs_observer.py From sacred with MIT License | 6 votes |
def _is_valid_bucket(bucket_name: str): """Validates correctness of bucket naming. Reference: https://cloud.google.com/storage/docs/naming """ if bucket_name.startswith("gs://"): return False if len(bucket_name) < 3 or len(bucket_name) > 63: return False # IP address if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", bucket_name): return False if not re.fullmatch(r"([^A-Z]|-|_|[.]|)+", bucket_name): return False if ".." in bucket_name: return False if "goog" in bucket_name or "g00g" in bucket_name: return False return True
Example #17
Source File: autils.py From nsf with MIT License | 6 votes |
def started_event(self, ex_info, command, host_info, start_time, config, meta_info, _id): prefix = config['dataset'] if config['run_descr']: prefix += '-' + config['run_descr'] def existing_run_nrs(): pattern = '{}(-\d+)?'.format(prefix) run_dirs = (d for d in os.listdir(self.basedir) if os.path.isdir(os.path.join(self.basedir, d))) for run_dir in run_dirs: match = re.fullmatch(pattern, run_dir) if match: num_str = match.group(1) yield int(num_str[1:] if num_str else 0) max_nr = max(existing_run_nrs(), default=None) if max_nr is None: return prefix else: return prefix + '-{}'.format(max_nr + 1)
Example #18
Source File: device.py From py-uio with MIT License | 6 votes |
def from_sysfs( cls, uio, info, parent=None ): index = int( re.fullmatch( r'map([0-9])', info.name ).group(1) ) def getinfo( attr ): with (info/attr).open() as f: return f.readline().rstrip() # If no name has been supplied, the region gets an auto-generated name # containing the full DT path. These are useless, ignore them. name = getinfo( 'name' ) if name == '' or name[0] == '/': name = None # get memory region bounds address = int( getinfo('addr'), 0 ) size = int( getinfo('size'), 0 ) return MemRegion( parent, address, size, name, uio, index )
Example #19
Source File: spikeglx.py From ibllib with MIT License | 6 votes |
def read_meta_data(md_file): """ Reads the spkike glx metadata file and parse in a dictionary Agnostic: does not make any assumption on the keys/content, it just parses key=values :param md_file: last sample to be read, python slice-wise :return: Data array, sync trace, meta-data """ with open(md_file) as fid: md = fid.read() d = {} for a in md.splitlines(): k, v = a.split('=') # if all numbers, try to interpret the string if v and re.fullmatch('[0-9,.]*', v) and v.count('.') < 2: v = [float(val) for val in v.split(',')] # scalars should not be nested if len(v) == 1: v = v[0] # tildes in keynames removed d[k.replace('~', '')] = v d['neuropixelVersion'] = _get_neuropixel_version_from_meta(d) d['serial'] = _get_serial_number_from_meta(d) return Bunch(d)
Example #20
Source File: emoji.py From Penny-Dreadful-Tools with GNU General Public License v3.0 | 6 votes |
def replace_emoji(text: str, client: Client) -> str: if text is None: return '' output = text symbols = re.findall(r'\{([A-Z0-9/]{1,3})\}', text) for symbol in symbols: name = symbol name = name.replace('/', '') if len(name) == 1: if re.fullmatch('[0-9]', name): name = '0' + name else: name = name + name emoji = find_emoji(name, client) if emoji is not None: output = output.replace('{' + symbol + '}', str(emoji)) return output
Example #21
Source File: dataset.py From typhon with MIT License | 6 votes |
def get_info_for_granule(self, p): """Return dict (re.fullmatch) for granule, based on re Arguments: p (pathlib.Path): path to granule Returns: dict: dictionary with info, such as returned by :func:`re.fullmatch`. """ if not isinstance(p, pathlib.Path): p = pathlib.Path(p) m = self._re.fullmatch(p.name) return m.groupdict()
Example #22
Source File: rest_models.py From QCPortal with BSD 3-Clause "New" or "Revised" License | 5 votes |
def rest_model(resource: str, rest: str) -> Tuple[ProtoModel, ProtoModel]: """ Acquires a REST Model. Parameters ---------- resource : str The REST endpoint resource name. rest : str The REST endpoint type: GET, POST, PUT, DELETE Returns ------- Tuple[ProtoModel, ProtoModel] The (body, response) models of the REST request. """ rest = rest.upper() matches = [] for model_re in __rest_models.keys(): if re.fullmatch(model_re, resource): try: matches.append(__rest_models[model_re][rest]) except KeyError: pass # Could have different regexes for different endpoint types if len(matches) == 0: raise KeyError(f"REST Model for endpoint {resource} could not be found.") if len(matches) > 1: warnings.warn( f"Multiple REST models were matched for {rest} request at endpoint {resource}. " f"The following models will be used: {matches[0][0]}, {matches[0][1]}.", RuntimeWarning, ) return matches[0] ### Generic Types and Common Models
Example #23
Source File: typed_returns.py From scVI with MIT License | 5 votes |
def process_return(lines): for line in lines: m = re.fullmatch(r"(?P<param>\w+)\s+:\s+(?P<type>[\w.]+)", line) if m: # Once this is in scanpydoc, we can use the fancy hover stuff yield f'-{m["param"]} (:class:`~{m["type"]}`)' else: yield line
Example #24
Source File: model.py From EasyPR-python with Apache License 2.0 | 5 votes |
def set_trainable(self, layer_regex, keras_model=None, indent=0, verbose=1): """Sets model layers as trainable if their names match the given regular expression. """ # Print message on the first call (but not on recursive calls) if verbose > 0 and keras_model is None: log("Selecting layers to train") keras_model = keras_model or self.keras_model # In multi-GPU training, we wrap the model. Get layers # of the inner model because they have the weights. layers = keras_model.inner_model.layers if hasattr(keras_model, "inner_model") \ else keras_model.layers for layer in layers: # Is the layer a model? if layer.__class__.__name__ == 'Model': print("In model: ", layer.name) self.set_trainable(layer_regex, keras_model=layer, indent=indent + 4) continue if not layer.weights: continue # Is it trainable? trainable = bool(re.fullmatch(layer_regex, layer.name)) # Update layer. If layer is a container, update inner layer. if layer.__class__.__name__ == 'TimeDistributed': layer.layer.trainable = trainable else: layer.trainable = trainable # Print trainble layer names if trainable and verbose > 0: log("{}{:20} ({})".format(" " * indent, layer.name, layer.__class__.__name__))
Example #25
Source File: model.py From EasyPR-python with Apache License 2.0 | 5 votes |
def ancestor(self, tensor, name, checked=None): """Finds the ancestor of a TF tensor in the computation graph. tensor: TensorFlow symbolic tensor. name: Name of ancestor tensor to find checked: For internal use. A list of tensors that were already searched to avoid loops in traversing the graph. """ checked = checked if checked is not None else [] # Put a limit on how deep we go to avoid very long loops if len(checked) > 500: return None # Convert name to a regex and allow matching a number prefix # because Keras adds them automatically if isinstance(name, str): name = re.compile(name.replace("/", r"(\_\d+)*/")) parents = tensor.op.inputs for p in parents: if p in checked: continue if bool(re.fullmatch(name, p.name)): return p checked.append(p) a = self.ancestor(p, name, checked) if a is not None: return a return None
Example #26
Source File: test_timer.py From python-devtools with MIT License | 5 votes |
def test_simple(): f = io.StringIO() t = debug.timer(name='foobar', file=f) with t: sleep(0.01) v = f.getvalue() assert re.fullmatch(r'foobar: 0\.01[012]s elapsed\n', v)
Example #27
Source File: dataset.py From CRNN.tf2 with MIT License | 5 votes |
def read_annotation(p, ignore_case): """Read an annotation file to get image paths and labels.""" with open(p) as f: line = f.readline().strip() print(f"Annotation path: {p} format: ", end="") if re.fullmatch(r".*/*\d+_.+_(\d+)\.\w+ \1", line): print("MJSynth") content = [l.strip().split() for l in f.readlines() + [line]] img_paths, labels = zip(*content) labels = [path.split("_")[1] for path in img_paths] elif re.fullmatch(r'.*/*word_\d\.\w+, ".+"', line): print("ICDAR2013") content = [l.strip().split(",") for l in f.readlines() + [line]] img_paths, labels = zip(*content) labels = [label.strip(' "') for label in labels] elif re.fullmatch(r".+\.\w+ .+", line): print("[image path] label") content = [l.strip().split() for l in f.readlines() + [line]] img_paths, labels = zip(*content) else: raise ValueError("Unsupported annotation format") dirname = os.path.dirname(p) img_paths = [os.path.join(dirname, path) for path in img_paths] if ignore_case: labels = [label.lower() for label in labels] return img_paths, labels
Example #28
Source File: get_configs.py From KortURL with MIT License | 5 votes |
def rate(self): pattern = "^[0-9]+\/(day|hour|min|sec)$" rate = settings.KORT_URL.get('IP_RATE') if re.fullmatch(pattern, rate): return rate else: return '3/sec'
Example #29
Source File: GPUmodule.py From gpu-utils with GNU General Public License v3.0 | 5 votes |
def read_gpu_ppm_table(self) -> None: """ Read the ppm table. """ if self.prm.vendor != GpuItem.GPU_Vendor.AMD: return if not self.prm.readable or self.prm.gpu_type in [GpuItem.GPU_Type.Legacy, GpuItem.GPU_Type.Unsupported]: return file_path = os.path.join(self.prm.card_path, 'pp_power_profile_mode') if not os.path.isfile(file_path): print('Error getting power profile modes: {}'.format(file_path), file=sys.stderr) sys.exit(-1) with open(file_path) as card_file: for line in card_file: linestr = line.strip() # Check for mode name: begins with '[ ]+[0-9].*' if re.fullmatch(r'[ ]+[0-9].*', line[0:3]): linestr = re.sub(r'[ ]*[*]*:', ' ', linestr) line_items = linestr.split() LOGGER.debug('PPM line: %s', linestr) if len(line_items) < 2: print('Error: invalid ppm: {}'.format(linestr), file=sys.stderr) continue LOGGER.debug('Valid ppm line: %s', linestr) self.ppm_modes[line_items[0]] = line_items[1:] self.ppm_modes['-1'] = ['AUTO', 'Auto'] rdata = self.read_gpu_sensor('power_dpm_force', vendor=GpuItem.GPU_Vendor.AMD, sensor_type='DEVICE') if rdata is False: print('Error: card file does not exist: {}'.format(file_path), file=sys.stderr) LOGGER.debug('Card file does not exist: %s', file_path) self.prm.readable = False else: self.set_params_value('power_dpm_force', rdata)
Example #30
Source File: route_handlers.py From sawtooth-core with Apache License 2.0 | 5 votes |
def _validate_id(resource_id): """Confirms a header_signature is 128 hex characters, raising an ApiError if not. """ if not re.fullmatch('[0-9a-f]{128}', resource_id): raise errors.InvalidResourceId(resource_id)