Python re.search() Examples

The following are 30 code examples of re.search(). 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: test.py    From aegea with Apache License 2.0 16 votes vote down vote up
def call(self, cmd, **kwargs):
        print('Running "{}"'.format(cmd), file=sys.stderr)
        expect = kwargs.pop("expect", [dict(return_codes=[os.EX_OK], stdout=None, stderr=None)])
        process = subprocess.Popen(cmd, stdin=kwargs.get("stdin", subprocess.PIPE), stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE, **kwargs)
        out, err = process.communicate()
        return_code = process.poll()
        out = out.decode(sys.stdin.encoding)
        err = err.decode(sys.stdin.encoding)

        def match(return_code, out, err, expected):
            exit_ok = return_code in expected["return_codes"]
            stdout_ok = re.search(expected.get("stdout") or "", out)
            stderr_ok = re.search(expected.get("stderr") or "", err)
            return exit_ok and stdout_ok and stderr_ok
        if not any(match(return_code, out, err, exp) for exp in expect):
            print(err)
            e = subprocess.CalledProcessError(return_code, cmd, output=out)
            e.stdout, e.stderr = out, err
            raise e
        return self.SubprocessResult(out, err, return_code) 
Example #2
Source File: test_exception_trace.py    From clikit with MIT License 7 votes vote down vote up
def test_render_debug_better_error_message_recursion_error_with_multiple_duplicated_frames():
    io = BufferedIO()
    io.set_verbosity(VERBOSE)

    with pytest.raises(RecursionError) as e:
        first()

    trace = ExceptionTrace(e.value)

    trace.render(io)

    expected = r"...  Previous 2 frames repeated \d+ times".format(
        filename=re.escape(trace._get_relative_file_path(__file__)),
    )

    assert re.search(expected, io.fetch_output()) is not None 
Example #3
Source File: setup.py    From L.E.S.M.A with Apache License 2.0 6 votes vote down vote up
def find_version(*file_paths):
    # Open in Latin-1 so that we avoid encoding errors.
    # Use codecs.open for Python 2 compatibility
    try:
        f = codecs.open(os.path.join(here, *file_paths), 'r', 'latin1')
        version_file = f.read()
        f.close()
    except:
        raise RuntimeError("Unable to find version string.")

    # The version line must have the form
    # __version__ = 'ver'
    version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
                              version_file, re.M)
    if version_match:
        return version_match.group(1)
    raise RuntimeError("Unable to find version string.")


# Get the long description from the relevant file 
Example #4
Source File: wiki_data.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def remove_consecutive(self, ner_tags, ner_values):
    for i in range(len(ner_tags)):
      if ((ner_tags[i] == "NUMBER" or ner_tags[i] == "MONEY" or
           ner_tags[i] == "PERCENT" or ner_tags[i] == "DATE") and
          i + 1 < len(ner_tags) and ner_tags[i] == ner_tags[i + 1] and
          ner_values[i] == ner_values[i + 1] and ner_values[i] != ""):
        word = ner_values[i]
        word = word.replace(">", "").replace("<", "").replace("=", "").replace(
            "%", "").replace("~", "").replace("$", "").replace("£", "").replace(
                "€", "")
        if (re.search("[A-Z]", word) and not (is_date(word)) and not (
            self.is_money(word))):
          ner_values[i] = "A"
        else:
          ner_values[i] = ","
    return ner_tags, ner_values 
Example #5
Source File: _cpmodpy.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_process(cmd, args=''):
    fullcmd = '%s %s' % (cmd, args)
    pipeout = popen(fullcmd)
    try:
        firstline = pipeout.readline()
        cmd_not_found = re.search(
            b'(not recognized|No such file|not found)',
            firstline,
            re.IGNORECASE
        )
        if cmd_not_found:
            raise IOError('%s must be on your system path.' % cmd)
        output = firstline + pipeout.read()
    finally:
        pipeout.close()
    return output 
Example #6
Source File: test_symbol.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_zero_prop():
    data = mx.symbol.Variable('data')
    for i in range(10):
        data = data * data

    exe = data.simple_bind(ctx=mx.cpu(), data=(10, 3, 256, 256))
    big = int(re.search('Total (\d+) MB allocated', exe.debug_str()).group(1))

    exe = data.simple_bind(ctx=mx.cpu(), data=(10, 3, 256, 256), grad_req='null')
    small1 = int(re.search('Total (\d+) MB allocated', exe.debug_str()).group(1))

    data = mx.sym.stop_gradient(data)
    exe = data.simple_bind(ctx=mx.cpu(), data=(10, 3, 256, 256))
    small2 = int(re.search('Total (\d+) MB allocated', exe.debug_str()).group(1))

    assert big > small2
    assert small1 == small2 
Example #7
Source File: isodump3.py    From multibootusb with GNU General Public License v2.0 6 votes vote down vote up
def writeDir_r(self, det_dir, dire, pp, r, all_type):
        #gen.log "writeDir_r:(%s)"%(det_dir)
        dirs = self.readDirItems(dire.locExtent, dire.lenData)
        for d in dirs:
            if not d.fIdentifier in [".", ".."]:
                if (pp != None) and (pp.search(d.fIdentifier) == None):
                    match = False
                else:
                    match = True
                #gen.log "mathing %s, %s, (%x)"%(match, d.fIdentifier, d.fFlag)
                p = det_dir + "/" + d.fIdentifier
                if d.fFlag & 0x02 == 0x02:
                    if not os.path.exists(p):
                        os.makedirs(p, 0o777)
                    if r:
                        if match:
                            self.writeDir_r(p, d, None, r, all_type) # Don't need to match subdirectory.
                        else:
                            self.writeDir_r(p, d, pp, r, all_type)
                elif match:
                    self.writeFile(d, p, all_type)
            # if not d.fIdentifier end #
        # for d in dirs end # 
Example #8
Source File: sina.py    From backtrader-cn with GNU General Public License v3.0 6 votes vote down vote up
def get_stock_price(self, symbol):
        """
        获取股票当前价格
        :param symbol:
        :return:
        """
        url = "http://hq.sinajs.cn/list=s_%s" % symbol
        r = self.session.get(url)
        m = re.search(r'"(.*)"', r.text)
        if m and m.groups()[0]:
            name, price, _, _, _, _ = m.groups()[0].split(',')
            logger.info("股票[%s](%s)当前价格为: %s" % (name, symbol, price))
            return price
        else:
            logger.error("获取股票%s当前价格失败" % symbol)
            raise StockMatchError()

    # fixme: 该方法暂时只支持买入A股,不支持操作美股和港股 
Example #9
Source File: extraction.py    From git2net with GNU Affero General Public License v3.0 6 votes vote down vote up
def is_binary_file(filename, file_content):
    if filename is None:
        return False
    else:
        try:
            extension = re.search(r'.*\.([^\.]+)$', filename).groups()[0]
        except AttributeError:
            extension = None

        if extension in binary_extensions:
            return True
        else:
            try:
                file_content.encode('utf-8', errors='strict')
            except UnicodeEncodeError:
                return True
            else:
                return False 
Example #10
Source File: main.py    From wafw00f with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def matchHeader(self, headermatch, attack=False):
        if attack:
            r = self.attackres
        else: r = rq
        if r is None:
            return
        header, match = headermatch
        headerval = r.headers.get(header)
        if headerval:
            # set-cookie can have multiple headers, python gives it to us
            # concatinated with a comma
            if header == 'Set-Cookie':
                headervals = headerval.split(', ')
            else:
                headervals = [headerval]
            for headerval in headervals:
                if re.search(match, headerval, re.I):
                    return True
        return False 
Example #11
Source File: i_to_m.py    From cWMI with Apache License 2.0 6 votes vote down vote up
def convert_param(method, param):
    # remove notation, split by upper, convert to lowercase
    param_sanitized = param.replace('*', '')
    substr = param_sanitized
    try:
        substr = re.search('([A-Z]\w+)', param_sanitized).group(1)
    except:
        pass
    case_re = re.compile(r'((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
    converted_param = case_re.sub(r'_\1', substr).lower()
    if converted_param in keyword.kwlist or converted_param in dir(__builtins__):
        converted_param += '_param'
    # check for duplicates. if seen, append number to end
    if 'params' in method and len([param for param in method['params'] if param['name'] == converted_param]):
        param_names = [param['name'] for param in method['params']]
        for x in range(2, 10):
            count_name = '{:s}{:d}'.format(converted_param, x)
            if count_name not in param_names:
                converted_param = count_name
                break
    return converted_param 
Example #12
Source File: filter_url.py    From lyrebird-api-coverage with MIT License 5 votes vote down vote up
def filter_re(self, url, pattern_list):
        flag = False
        for pattern_item in pattern_list:
            if re.search(pattern_item, url) is not None:
                return True
            else:
                flag = False
        return flag 
Example #13
Source File: repl.py    From Dumb-Cogs with MIT License 5 votes vote down vote up
def irdir(*args, re_exclude=None, re_search=None):
    """Dir([object[,re_exclude[,re_search]]]) --> Dir[list of attributes]
    slightly more useful dir() with search/exclusion

    returns a Dir object.

    Like built-in function dir,
    If called without an argument, return the names in the current scope.
    Else, return an alphabetized list of names comprising (some of) the attributes
    of the given object, and of attributes reachable from it.

    If re_exclude or re_search arguments are given,
    they are used as exclusion and search regexes on the object's list of attributes/
    If left blank, private and protected attributes are excluded by default

    Dir() objects can have their search/exclusion regexes added to via the operator pairs:
    Search, Exclusion
    -----------------
         +, -
         @, //
        [], del (also provides indexed get/deletion)
    search, exclude (methods)

    * done as a function for more legible help()
    ** At the moment there are many unimplemented list-like methods. Most of them don't work.
       I will remove them later. If you need it to act like a list instead of a Dir, just list(it)
    """
    return Dir(*args, re_exclude=re_exclude, re_search=re_search) 
Example #14
Source File: setup.py    From keras-gpt-2 with MIT License 5 votes vote down vote up
def find_version(*file_paths):
    version_file = read_file(*file_paths)
    version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M)
    if version_match:
        return version_match.group(1)
    raise RuntimeError('Unable to find version string.') 
Example #15
Source File: repl.py    From Dumb-Cogs with MIT License 5 votes vote down vote up
def __getitem__(self, i):
        """returns the i'th item.
        If i is a str, returns a new Dir instance with a more constrained search"""
        return self.search(i) if isinstance(i, str) else self.list[i] 
Example #16
Source File: partition_s3_logs.py    From aws-waf-security-automations with Apache License 2.0 5 votes vote down vote up
def parse_alb_logs(key, filename):
    # Get year, month and day
    time_stamp = re.search('(\\d{4})/(\\d{2})/(\\d{2})', key)
    year, month, day = time_stamp.group(0).split('/')

    # Get hour
    time_stamp = re.search('(\\d{8})T(\\d{2})', filename)
    hour = time_stamp.group(0).split('T')[1]

    # Create destination path
    dest = 'AWSLogs-Partitioned/year={}/month={}/day={}/hour={}/{}' \
           .format(year, month, day, hour, filename)

    return dest 
Example #17
Source File: repl.py    From Dumb-Cogs with MIT License 5 votes vote down vote up
def _search(self, add: str=None):
        if add:
            self._re_search.append(add)
        for s in self._re_search:
            self.list = [i for i in self.list if re.search(s, i)] 
Example #18
Source File: wiki_data.py    From DOTA_models with Apache License 2.0 5 votes vote down vote up
def is_date(word):
  if (not (bool(re.search("[a-z0-9]", word, re.IGNORECASE)))):
    return False
  if (len(word) != 10):
    return False
  if (word[4] != "-"):
    return False
  if (word[7] != "-"):
    return False
  for i in range(len(word)):
    if (not (word[i] == "X" or word[i] == "x" or word[i] == "-" or re.search(
        "[0-9]", word[i]))):
      return False
  return True 
Example #19
Source File: optimization.py    From BERT-Classification-Tutorial with Apache License 2.0 5 votes vote down vote up
def _do_use_weight_decay(self, param_name):
        """Whether to use L2 weight decay for `param_name`."""
        if not self.weight_decay_rate:
            return False
        if self.exclude_from_weight_decay:
            for r in self.exclude_from_weight_decay:
                if re.search(r, param_name) is not None:
                    return False
        return True 
Example #20
Source File: _version.py    From NiBetaSeries with MIT License 5 votes vote down vote up
def git_get_keywords(versionfile_abs):
    """Extract version information from the given file."""
    # the code embedded in _version.py can just fetch the value of these
    # keywords. When used from setup.py, we don't want to import _version.py,
    # so we do it with a regexp instead. This function is not used from
    # _version.py.
    keywords = {}
    try:
        f = open(versionfile_abs, "r")
        for line in f.readlines():
            if line.strip().startswith("git_refnames ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["refnames"] = mo.group(1)
            if line.strip().startswith("git_full ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["full"] = mo.group(1)
            if line.strip().startswith("git_date ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["date"] = mo.group(1)
        f.close()
    except EnvironmentError:
        pass
    return keywords 
Example #21
Source File: versioneer.py    From NiBetaSeries with MIT License 5 votes vote down vote up
def versions_from_file(filename):
    """Try to determine the version from _version.py if present."""
    try:
        with open(filename) as f:
            contents = f.read()
    except EnvironmentError:
        raise NotThisMethod("unable to read _version.py")
    mo = re.search(r"version_json = '''\n(.*)'''  # END VERSION_JSON",
                   contents, re.M | re.S)
    if not mo:
        mo = re.search(r"version_json = '''\r\n(.*)'''  # END VERSION_JSON",
                       contents, re.M | re.S)
    if not mo:
        raise NotThisMethod("no version_json in _version.py")
    return json.loads(mo.group(1)) 
Example #22
Source File: versioneer.py    From NiBetaSeries with MIT License 5 votes vote down vote up
def git_get_keywords(versionfile_abs):
    """Extract version information from the given file."""
    # the code embedded in _version.py can just fetch the value of these
    # keywords. When used from setup.py, we don't want to import _version.py,
    # so we do it with a regexp instead. This function is not used from
    # _version.py.
    keywords = {}
    try:
        f = open(versionfile_abs, "r")
        for line in f.readlines():
            if line.strip().startswith("git_refnames ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["refnames"] = mo.group(1)
            if line.strip().startswith("git_full ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["full"] = mo.group(1)
            if line.strip().startswith("git_date ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["date"] = mo.group(1)
        f.close()
    except EnvironmentError:
        pass
    return keywords 
Example #23
Source File: predicate.py    From python-clean-architecture with MIT License 5 votes vote down vote up
def search(self, regex: str) -> Predicate:
        """
        Run a regex test against the value (only substring string has to
        match).
        >>> var('f1').search(r'^\\w+$')

        :param regex: The regular expression to use for matching
        """
        return self._build_predicate(
            lambda lhs, value: bool(re.search(regex, lhs)),
            Operation.SEARCH,
            (self._path, regex)
        ) 
Example #24
Source File: isodump3.py    From multibootusb with GNU General Public License v2.0 5 votes vote down vote up
def usage():
    """ Prompt user how to use   """
    gen.log("""
Usage: isodump  dump-what [options]  iso-file
       [dump-what]
       -----------
       boot               - Dump boot record.
       primary-volume     - Dump primary volume.
       pathtable          - Dump path table.
       dir-record [block number] [length] - Dump a raw data of a Directory Record

       iso:/dir [-r]  [-o output] [-p pattern]  - Dump a dirctory or file to [output]
           -r    recursively visit directory.
           -p    spcify a Regular expression pattern for re.search(pattern,).

isodump xx.iso              - Dump the root directory
isodump pathtable xx.iso    - Dump the path table record.

isodump iso:/ -r     xx.iso
    -- Dump the root directory of xx.iso recursively.

isodump iso:/ -r -o /tmp/iso    xx.iso
    -- Extract the iso to /tmp/iso/.

isodump iso:/boot -o /tmp/iso/boot    xx.iso
    -- Extract the /boot directory of xx.iso to /tmp/iso/boot.

isodump iso:/boot/grup.cfg -o /tmp/grub.cfg  xx.iso
    -- Extract the file "grup.cfg" to "/tmp/grub.cfg"

isodump iso:/boot -r -o /tmp/iso -p "*.cfg"  xx.iso
    -- Extract any files or directories under /boot maching "*.cfg" to /tmp/iso/.
""")
    sys.exit(-1) 
Example #25
Source File: isodump3.py    From multibootusb with GNU General Public License v2.0 5 votes vote down vote up
def checkIntegrity(self):
        if self.priVol == None: # no primary volume
            return False

        if self.priVol.ptSize == 0: # empty ?
            return True

        path_table = self.readPathtableL()
        if path_table == []: # pathtable record is broken.
            return False

        # find last file item to check
        for dr in reversed(path_table):
            #gen.log dr.fIdentifier
            dirs = self.readDirItems(dr.locExtent, BLOCK_SIZE)
            if len(dirs) > 2:
                dot = dirs[0]
                dirs2 = self.readDirItems(dot.locExtent, dot.lenData) # get the whole items.
                for dr2 in reversed(dirs2): # search last file item.
                    if dr2.fFlag == 0:
                        #gen.log "get last file(%s)"%(dr2.fIdentifier)
                        try:
                            #self.isoFile.seek(BLOCK_SIZE * dr2.locExtent+dr2.lenData)
                            lastfile_end = BLOCK_SIZE * dr2.locExtent + dr2.lenData
                            self.isoFile.seek(0, os.SEEK_END)
                            iso_end = self.isoFile.tell()
                            #gen.log("%d-->%d")%(lastfile_end, iso_end)
                            if iso_end >= lastfile_end:
                                return True
                            else:
                                return False
                        except(IOError):
                            #gen.log "exception when seek. iso is broken"
                            return False
            elif len(dirs) < 2: # Dir record is broken. At least, should have two entries.
               return False
        return True

########################################################################### 
Example #26
Source File: grub.py    From multibootusb with GNU General Public License v2.0 5 votes vote down vote up
def grub_raw_iso(mbus_grub_cfg_path):
    """
    Generic menu entry for booting ISO files directly using memdisk. Should have enough memory to load to RAM
    :return:
    """
    menu_entry = '    search --set -f /multibootusb/' + iso.iso_basename(config.image_path) + '/' + iso.iso_name(config.image_path) + '\n' \
                 '    menuentry ' + iso.iso_basename(config.image_path) + ' {\n' \
                 '    linux16 /multibootusb/memdisk iso raw vmalloc=750M\n' \
                 '    initrd16 /multibootusb/' + iso.iso_basename(config.image_path) + '/' + iso.iso_name(config.image_path) + '\n' \
                 '}\n'
    with open(mbus_grub_cfg_path, 'a') as f:
        f.write("#start " + iso.iso_basename(config.image_path) + "\n")
        f.write(menu_entry)
        f.write("#end " + iso.iso_basename(config.image_path) + "\n")
    return menu_entry 
Example #27
Source File: gen.py    From multibootusb with GNU General Public License v2.0 5 votes vote down vote up
def grub_efi_exist(grub_efi_path):
    """
    Detect efi present in USB disk is copied by multibootusb.
    :param isolinux_path: Path to "grub efi image"
    :return: True if yes else False
    """
    from . import iso
    if grub_efi_path is not None:
        sl = list(iso.strings(grub_efi_path))
        for strin in sl:
            if re.search(r'multibootusb', strin, re.I):
                return True
        return False 
Example #28
Source File: update_cfg_file.py    From multibootusb with GNU General Public License v2.0 5 votes vote down vote up
def config_is_persistence_aware(self, content):
        """ Used to restrict update of boot parameters to persistent-aware
        menu entries if the distribution provides any.
        """
        return self.persistence_awareness_checking_re.search(content) \
            is not None 
Example #29
Source File: update_cfg_file.py    From multibootusb with GNU General Public License v2.0 5 votes vote down vote up
def extract_distroinfo_from_file(self, subpath, regex, distro_group,
                                    version_group):
        content = self.file_content(subpath)
        if not content:
            return None
        m = re.compile(regex, re.I).search(content)
        if not m:
            return None
        return (m.group(distro_group),
                [int(x) for x in m.group(version_group).split('.')]) 
Example #30
Source File: setup.py    From django-json-widget with MIT License 5 votes vote down vote up
def get_version(*file_paths):
    """Retrieves the version from django_json_widget/__init__.py"""
    filename = os.path.join(os.path.dirname(__file__), *file_paths)
    version_file = open(filename).read()
    version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
                              version_file, re.M)
    if version_match:
        return version_match.group(1)
    raise RuntimeError('Unable to find version string.')


# version = get_version("django_json_widget", "__init__.py")