Python re.sub() Examples

The following are code examples for showing how to use re.sub(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: pycos   Author: pgiri   File: dispycos.py    (license) View Source Project 9 votes vote down vote up
def __init__(self, node, platform='', cpus=0, memory=0, disk=0):
        if node.find('*') < 0:
            try:
                info = socket.getaddrinfo(node, None)[0]
                ip_addr = info[4][0]
                if info[0] == socket.AF_INET6:
                    ip_addr = re.sub(r'^0*', '', ip_addr)
                    ip_addr = re.sub(r':0*', ':', ip_addr)
                    ip_addr = re.sub(r'::+', '::', ip_addr)
                node = ip_addr
            except:
                node = ''

        if node:
            self.ip_rex = node.replace('.', '\\.').replace('*', '.*')
        else:
            logger.warning('node "%s" is invalid', node)
            self.ip_rex = ''
        self.platform = platform.lower()
        self.cpus = cpus
        self.memory = memory
        self.disk = disk 
Example 2
Project: socialhome   Author: jaywink   File: utils.py    (GNU Affero General Public License v3.0) View Source Project 6 votes vote down vote up
def safe_text_for_markdown(text):
    """Clean the text using bleach but keep certain Markdown sections.

    Markdown code ie ` or ``` combos. For single `, do not allow line breaks between the tag.
    Quotes ie '> ' which bleach would clean up.
    """
    code_blocks, text = code_blocks_add_markers(text)
    # Store quotes next
    text = re.sub(r"(^> )", "%%safe_quote_in_start%%", text)
    text = re.sub(r"(\n> )", "%%safe_quote_in_new_line%%", text, flags=re.DOTALL)
    # Nuke all html, scripts, etc
    text = bleach.clean(text or "")
    # Return quotes
    text = text.replace("%%safe_quote_in_start%%", "> ")
    text = text.replace("%%safe_quote_in_new_line%%", "\n> ")
    text = code_blocks_restore(code_blocks, text)
    return text 
Example 3
Project: alfred-mpd   Author: deanishe   File: docopt.py    (license) View Source Project 6 votes vote down vote up
def parse_pattern(source, options):
    tokens = TokenStream(re.sub(r'([\[\]\(\)\|]|\.\.\.)', r' \1 ', source),
                         DocoptLanguageError)
    result = parse_expr(tokens, options)
    if tokens.current() is not None:
        raise tokens.error('unexpected ending: %r' % ' '.join(tokens))
    return Required(*result) 
Example 4
Project: devops-playground   Author: jerrywardlow   File: digital_ocean.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def to_safe(self, word):
        ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups '''
        return re.sub("[^A-Za-z0-9\-\.]", "_", word) 
Example 5
Project: my-first-blog   Author: AnkurBegining   File: manifest.py    (license) View Source Project 6 votes vote down vote up
def _glob_to_re(self, pattern):
        """Translate a shell-like glob pattern to a regular expression.

        Return a string containing the regex.  Differs from
        'fnmatch.translate()' in that '*' does not match "special characters"
        (which are platform-specific).
        """
        pattern_re = fnmatch.translate(pattern)

        # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
        # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
        # and by extension they shouldn't match such "special characters" under
        # any OS.  So change all non-escaped dots in the RE to match any
        # character except the special characters (currently: just os.sep).
        sep = os.sep
        if os.sep == '\\':
            # we're using a regex to manipulate a regex, so we need
            # to escape the backslash twice
            sep = r'\\\\'
        escaped = r'\1[^%s]' % sep
        pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re)
        return pattern_re 
Example 6
Project: OpenCouture-Dev   Author: 9-9-0   File: adidasREQ.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def addToCart(self):
        print '\nADD TO CART -----------------'
        session_get = self.user_session.get(self.URL_product_url, headers=self.get_headers)
        #print session_get.content
        soup = BeautifulSoup(session_get.content, 'lxml')
        
        results = soup.find_all('select', class_='size-select')
        #print results

        for item in results[0].select('option'):
            re_result = re.sub(self.sub_pattern, '', item.string)
            #print re_result
            matchObj = re.search(r"^%s+$" % self.user_size, re_result)
            if matchObj:
                self.post_data_addToCart['pid'] = item['value']
                self.post_data_addToCart['masterPID'] = item['value'].partition("_")[0]
                print self.post_data_addToCart
                break

        session_post = self.user_session.post(url=self.URL_cart_post_url, headers=self.post_headers, data=self.post_data_addToCart)
        print 'Add To Cart Status: ' + str(session_post.status_code) 
Example 7
Project: seq2seq   Author: google   File: process_story.py    (license) View Source Project 6 votes vote down vote up
def process_story(text):
  """Processed a story text into an (article, summary) tuple.
  """
  # Split by highlights
  elements = text.split("@highlight")
  elements = [_.strip() for _ in elements]

  story_text = elements[0]
  highlights = elements[1:]

  # Join all highlights into a single blob
  highlights_joined = "; ".join(highlights)
  highlights_joined = re.sub(r"\s+", " ", highlights_joined)
  highlights_joined = highlights_joined.strip()

  # Remove newlines from story
  # story_text = story_text.replace("\n", " ")
  story_text = re.sub(r"\s+", " ", story_text)
  story_text = story_text.strip()

  return story_text, highlights_joined 
Example 8
Project: database_assetstore   Author: OpenGeoscience   File: postgres_sqlalchemy.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def _get_column_info(self, name, format_type, *args, **kwargs):
    """
    When the PGDialect or its subclasses get column information, if the type
    is unknown and certain conditions are met, create a new type dynamically.
    This wraps the original function (see sqlalchemy's
    dialects/postgresql/base.py file).
    """
    attype = re.sub(r'\(.*\)', '', format_type)
    attype = re.sub(r'\[\]', '', attype)
    info = gci(self, name, format_type, *args, **kwargs)
    if (info['type'] == sqlalchemy.sql.sqltypes.NULLTYPE and
            attype.lower() not in dialect.base.ischema_names and
            attype not in KnownTypes):
        newtype = type(str(attype), (DynamicType,), {'name': str(attype)})
        newtype.__visit_name__ = attype
        dialect.base.ischema_names[attype.lower()] = newtype
        KnownTypes[attype] = newtype
        info = gci(self, name, format_type, *args, **kwargs)
    return info 
Example 9
Project: cellranger   Author: 10XGenomics   File: common.py    (license) View Source Project 6 votes vote down vote up
def format_name(display_name, prefix, prefixes, prefix_format_func=None):
    if prefix is not None and prefix_format_func is not None:
        prefix = prefix_format_func(prefix)

    # Default multi -> '' if no format func given
    if prefix_format_func is None and prefix == cr_constants.MULTI_REFS_PREFIX:
        prefix = ''

    if len(prefixes) > 1 or '%s' in display_name:
        display_name = add_prefix(prefix, display_name)

    # Replace underscores w/ spaces
    display_name = display_name.replace('_', ' ')

    # Collapse whitespace
    display_name = re.sub('\s+', ' ', display_name)

    return display_name 
Example 10
Project: cellranger   Author: 10XGenomics   File: common.py    (license) View Source Project 6 votes vote down vote up
def format_description(description, prefix, prefixes, prefix_format_func=None):
    if prefix is not None and prefix_format_func is not None:
        prefix = prefix_format_func(prefix)

    # Default multi -> '' if no format func given
    if prefix_format_func is None and prefix == cr_constants.MULTI_REFS_PREFIX:
        prefix = ''

    if '%s' in description:
        # Escape stray percents
        description = re.sub('%([^s])', '%%\\1', description)

        # Only add the prefix if there are multiple possibilities
        s = str(prefix) if len(prefixes) > 1 and prefix else ''
        description = description % s

    # Collapse whitespace
    description = re.sub('\s+', ' ', description)

    return description 
Example 11
Project: data_pipeline   Author: Yelp   File: containers.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, additional_compose_file=None, additional_services=None):
        # To resolve docker client server version mismatch issue.
        os.environ["COMPOSE_API_VERSION"] = "auto"
        dir_name = os.path.split(os.getcwd())[-1]
        self.project = "{}{}".format(
            re.sub(r'[^a-z0-9]', '', dir_name.lower()),
            getpass.getuser()
        )
        self.additional_compose_file = additional_compose_file

        self.services = ["zookeeper", "schematizer", "kafka"]

        if additional_services is not None:
            self.services.extend(additional_services)

        # This variable is meant to capture the running/not-running state of
        # the dependent testing containers when tests start running.  The idea
        # is, we'll only start and stop containers if they aren't already
        # running.  If they are running, we'll just use the ones that exist.
        # It takes a while to start all the containers, so when running lots of
        # tests, it's best to start them out-of-band and leave them up for the
        # duration of the session.
        self.containers_already_running = self._are_containers_already_running() 
Example 12
Project: BitBot   Author: crack00r   File: tl_object.py    (license) View Source Project 6 votes vote down vote up
def infer_id(self):
        representation = self.__repr__(ignore_id=True)

        # Clean the representation
        representation = representation\
            .replace(':bytes ', ':string ')\
            .replace('?bytes ', '?string ')\
            .replace('<', ' ').replace('>', '')\
            .replace('{', '').replace('}', '')

        representation = re.sub(
            r' \w+:flags\.\d+\?true',
            r'',
            representation
        )
        return crc32(representation.encode('ascii')) 
Example 13
Project: jobs   Author: josiahcarlson   File: jobs.py    (license) View Source Project 6 votes vote down vote up
def _check_inputs_and_outputs(fcn):
    @functools.wraps(fcn)
    def call(conn, inputs, outputs, identifier, *a, **kw):
        assert isinstance(inputs, (list, tuple, set)), inputs
        assert isinstance(outputs, (list, tuple, set)), outputs
        assert '' not in inputs, inputs
        assert '' not in outputs, outputs
        # this is for actually locking inputs/outputs
        inputs, outputs = list(map(str, inputs)), list(map(str, outputs))
        locks = inputs + [''] + outputs

        if kw.pop('history', None):
            igraph = [EDGE_RE.sub('*', inp) for inp in inputs]
            ograph = [EDGE_RE.sub('*', out) for out in outputs]
            graph_id = EDGE_RE.sub('*', str(identifier))
            graph = igraph + [''] + ograph + ['', graph_id]
            if all(x.startswith('test.') for x in igraph + ograph):
                graph = ['', '']
        else:
            graph = ['', '']

        return fcn(conn, locks, graph, str(identifier), *a, **kw)
    return call 
Example 14
Project: ml   Author: hohoins   File: cifar10.py    (license) View Source Project 6 votes vote down vote up
def _activation_summary(x):
  """Helper to create summaries for activations.

  Creates a summary that provides a histogram of activations.
  Creates a summary that measure the sparsity of activations.

  Args:
    x: Tensor
  Returns:
    nothing
  """
  # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training
  # session. This helps the clarity of presentation on tensorboard.
  tensor_name = re.sub('%s_[0-9]*/' % TOWER_NAME, '', x.op.name)
  # tf.histogram_summary(tensor_name + '/activations', x)
  tf.summary.histogram(tensor_name + '/activations', x)
  # tf.scalar_summary(tensor_name + '/sparsity', tf.nn.zero_fraction(x))
  tf.summary.scalar(tensor_name + '/sparsity', tf.nn.zero_fraction(x)) 
Example 15
Project: ml   Author: hohoins   File: cifar10.py    (license) View Source Project 6 votes vote down vote up
def _activation_summary(x):
  """Helper to create summaries for activations.

  Creates a summary that provides a histogram of activations.
  Creates a summary that measure the sparsity of activations.

  Args:
    x: Tensor
  Returns:
    nothing
  """
  # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training
  # session. This helps the clarity of presentation on tensorboard.
  tensor_name = re.sub('%s_[0-9]*/' % TOWER_NAME, '', x.op.name)
  # tf.histogram_summary(tensor_name + '/activations', x)
  tf.summary.histogram(tensor_name + '/activations', x)
  # tf.scalar_summary(tensor_name + '/sparsity', tf.nn.zero_fraction(x))
  tf.summary.scalar(tensor_name + '/sparsity', tf.nn.zero_fraction(x)) 
Example 16
Project: kinect-2-libras   Author: inessadl   File: filelist.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def glob_to_re(pattern):
    """Translate a shell-like glob pattern to a regular expression.

    Return a string containing the regex.  Differs from
    'fnmatch.translate()' in that '*' does not match "special characters"
    (which are platform-specific).
    """
    pattern_re = fnmatch.translate(pattern)

    # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
    # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
    # and by extension they shouldn't match such "special characters" under
    # any OS.  So change all non-escaped dots in the RE to match any
    # character except the special characters.
    # XXX currently the "special characters" are just slash -- i.e. this is
    # Unix-only.
    pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', r'\1[^/]', pattern_re)

    return pattern_re 
Example 17
Project: kinect-2-libras   Author: inessadl   File: util.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def subst_vars (s, local_vars):
    """Perform shell/Perl-style variable substitution on 'string'.  Every
    occurrence of '$' followed by a name is considered a variable, and
    variable is substituted by the value found in the 'local_vars'
    dictionary, or in 'os.environ' if it's not in 'local_vars'.
    'os.environ' is first checked/augmented to guarantee that it contains
    certain values: see 'check_environ()'.  Raise ValueError for any
    variables not found in either 'local_vars' or 'os.environ'.
    """
    check_environ()
    def _subst (match, local_vars=local_vars):
        var_name = match.group(1)
        if var_name in local_vars:
            return str(local_vars[var_name])
        else:
            return os.environ[var_name]

    try:
        return re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s)
    except KeyError, var:
        raise ValueError, "invalid variable '$%s'" % var

# subst_vars () 
Example 18
Project: kinect-2-libras   Author: inessadl   File: utils.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def formataddr(pair):
    """The inverse of parseaddr(), this takes a 2-tuple of the form
    (realname, email_address) and returns the string value suitable
    for an RFC 2822 From, To or Cc header.

    If the first element of pair is false, then the second element is
    returned unmodified.
    """
    name, address = pair
    if name:
        quotes = ''
        if specialsre.search(name):
            quotes = '"'
        name = escapesre.sub(r'\\\g<0>', name)
        return '%s%s%s <%s>' % (quotes, name, quotes, address)
    return address 
Example 19
Project: kinect-2-libras   Author: inessadl   File: cookielib.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
def join_header_words(lists):
    """Do the inverse (almost) of the conversion done by split_header_words.

    Takes a list of lists of (key, value) pairs and produces a single header
    value.  Attribute values are quoted if needed.

    >>> join_header_words([[("text/plain", None), ("charset", "iso-8859/1")]])
    'text/plain; charset="iso-8859/1"'
    >>> join_header_words([[("text/plain", None)], [("charset", "iso-8859/1")]])
    'text/plain, charset="iso-8859/1"'

    """
    headers = []
    for pairs in lists:
        attr = []
        for k, v in pairs:
            if v is not None:
                if not re.search(r"^\w+$", v):
                    v = HEADER_JOIN_ESCAPE_RE.sub(r"\\\1", v)  # escape " and \
                    v = '"%s"' % v
                k = "%s=%s" % (k, v)
            attr.append(k)
        if attr: headers.append("; ".join(attr))
    return ", ".join(headers) 
Example 20
Project: Instagram   Author: Fastcampus-WPS-5th   File: comment.py    (license) View Source Project 6 votes vote down vote up
def make_html_content_and_add_tags(self):
        # ????? ???? ?????
        p = re.compile(r'(#\w+)')
        # findall???? ???? ????? ???
        tag_name_list = re.findall(p, self.content)
        # ?? content(Comment??)? ??? ??
        ori_content = self.content
        # ????? ????
        for tag_name in tag_name_list:
            # Tag??? ????? ??, ????? ???? ????? _??
            tag, _ = Tag.objects.get_or_create(name=tag_name.replace('#', ''))
            # ?? content? ??? ??
            change_tag = '<a href="{url}" class="hash-tag">{tag_name}</a>'.format(
                # url=reverse('post:hashtag_post_list', args=[tag_name.replace('#', '')]),
                url=reverse('post:hashtag_post_list',
                            kwargs={'tag_name': tag_name.replace('#', '')}),
                tag_name=tag_name
            )
            ori_content = re.sub(r'{}(?![<\w])'.format(tag_name), change_tag, ori_content, count=1)
            # content? ??? Tag??? ??? tags??? ??
            if not self.tags.filter(pk=tag.pk).exists():
                self.tags.add(tag)
        # ??? ??? ???? html_content? ??
        self.html_content = ori_content
        super().save(update_fields=['html_content']) 
Example 21
Project: ln2sql   Author: FerreroJeremy   File: test_unit.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def _clean_output(s):
    s = s.split("SELECT")[1]  # remove table schema
    s = re.sub("\\033.*?m", "", s)  # remove color codes
    s = s.replace('\n', ' ')  # remove '\n'
    s = s.split(';')[0]  # remove spaces after ;
    s = "SELECT" + s + ';'  # put back lost SELECT and ';'
    return s 
Example 22
Project: ln2sql   Author: FerreroJeremy   File: test_thesaurus.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def _clean_output(s):
    s = s.split("SELECT")[1]  # remove table schema
    s = re.sub("\\033.*?m", "", s)  # remove color codes
    s = s.replace('\n', ' ')  # remove '\n'
    s = s.split(';')[0]  # remove spaces after ;
    s = "SELECT" + s + ';'  # put back lost SELECT and ';'
    return s 
Example 23
Project: pycos   Author: pgiri   File: dispycos.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, node, platform='', cpus=0, memory=0, disk=0):
        if node.find('*') < 0:
            try:
                info = socket.getaddrinfo(node, None)[0]
                ip_addr = info[4][0]
                if info[0] == socket.AF_INET6:
                    ip_addr = re.sub(r'^0*', '', ip_addr)
                    ip_addr = re.sub(r':0*', ':', ip_addr)
                    ip_addr = re.sub(r'::+', '::', ip_addr)
                node = ip_addr
            except:
                node = ''

        if node:
            self.ip_rex = node.replace('.', '\\.').replace('*', '.*')
        else:
            logger.warning('node "%s" is invalid', node)
            self.ip_rex = ''
        self.platform = platform.lower()
        self.cpus = cpus
        self.memory = memory
        self.disk = disk 
Example 24
Project: django-openapi-gen   Author: Ecognize   File: router.py    (MIT License) View Source Project 5 votes vote down vote up
def make_regex(self, path, named = False):
        regex = re.sub(SWAGGER_PARAMS_REGEX, DJANGO_PARAMS_STRING, path) if named else path
        regex = re.sub(URL_SLASHES_REGEX, DJANGO_URL_SUBSTRING, regex)

        return regex

    #: create handler ready for urlization 
Example 25
Project: HatDecrypter   Author: HatBashBR   File: crackers.py    (license) View Source Project 5 votes vote down vote up
def decrypt(hash, tipo):
    global word
    
    try:
        if(tipo == 0):
            url = BeautifulSoup(urllib.urlopen("https://md5.gromweb.com/?md5=" + hash), "html.parser")
        else:
            url = BeautifulSoup(urllib.urlopen("https://sha1.gromweb.com/?hash=" + hash), "html.parser")
            
        password = url.find("em", {"class": "long-content string"})
        password = re.sub(re.compile("<.*?>"), "", str(password)).strip()
        if str(password) == "None":
            print word+"\t\t\t\t[-] Senha nao encontrada! :-("
        else:
            print word+"\t\t\t\t[+] Senha encontrada: " + password
    except IOError:
       decryptwl(hash, tipo) 
Example 26
Project: linux-soft-exploit-suggester   Author: belane   File: linux-soft-exploit-suggester.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def cleanName(soft_name):
    """ Clean package name from common strings """
    for badword in badpackages:
        soft_name = re.sub(r'-' + badword, '', soft_name)
    return soft_name 
Example 27
Project: socialhome   Author: jaywink   File: previews.py    (GNU Affero General Public License v3.0) View Source Project 5 votes vote down vote up
def fetch_oembed_preview(content, urls):
    """Fetch first oembed content for a list of urls."""
    for url in urls:
        # See first if recently cached already
        if OEmbedCache.objects.filter(url=url, modified__gte=now()-datetime.timedelta(days=7)).exists():
            oembed = OEmbedCache.objects.get(url=url)
            Content.objects.filter(id=content.id).update(oembed=oembed)
            return oembed
        # Fetch oembed
        options = {}
        if url.startswith("https://twitter.com/"):
            # This probably has little effect since we fetch these on the backend...
            # But, DNT is always good to communicate if possible :)
            options = {"dnt": "true"}
        try:
            oembed = PyEmbed(discoverer=OEmbedDiscoverer()).embed(url, **options)
        except (PyEmbedError, PyEmbedDiscoveryError, PyEmbedConsumerError, ValueError):
            continue
        if not oembed:
            continue
        # Ensure width is 100% not fixed
        oembed = re.sub(r'width="[0-9]*"', 'width="100%"', oembed)
        oembed = re.sub(r'height="[0-9]*"', "", oembed)
        try:
            with transaction.atomic():
                oembed = OEmbedCache.objects.create(url=url, oembed=oembed)
        except IntegrityError:
            # Some other process got ahead of us
            oembed = OEmbedCache.objects.get(url=url)
            Content.objects.filter(id=content.id).update(oembed=oembed)
            return oembed
        Content.objects.filter(id=content.id).update(oembed=oembed)
        return oembed
    return False 
Example 28
Project: socialhome   Author: jaywink   File: models.py    (GNU Affero General Public License v3.0) View Source Project 5 votes vote down vote up
def fix_local_uploads(self):
        """Fix the markdown URL of local uploads.

        Basically these need to be remote compatible. So make this:

            ![](/media/markdownx/12345.jpg

        to this:

            ![](https://socialhome.domain/media/markdownx/12345.jpg
        """
        self.text = re.sub(r"!\[\]\(/media/uploads/", "![](%s/media/uploads/" % settings.SOCIALHOME_URL, self.text) 
Example 29
Project: ThreatPrep   Author: ThreatResponse   File: common.py    (MIT License) View Source Project 5 votes vote down vote up
def get_description(self):
        """Gets the docstring of the instance."""
        return re.sub('\n\W+',' ', self.__doc__) 
Example 30
Project: flora   Author: Lamden   File: api.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def clean(s):
	return re.sub('[^A-Za-z0-9]+', '', s) 
Example 31
Project: hdcp_test   Author: imamotts   File: hdcp_sink.py    (MIT License) View Source Project 5 votes vote down vote up
def __init__(self, conf_file):

        f = open(conf_file, "r")
        conf = yaml.load(f)
        f.close()

        cert_bin = self.__hex2bin(conf["cert_hex"])

        priv = conf["priv_key"]
        P_bin           = self.__hex2bin(priv["P_hex"])
        Q_bin           = self.__hex2bin(priv["Q_hex"])
        d_mod_p_1_bin   = self.__hex2bin(priv["d_mod_p-1_hex"])
        d_mod_q_1_bin   = self.__hex2bin(priv["d_mod_q-1_hex"])
        inv_q_mod_p_bin = self.__hex2bin(priv["inv_q_mod_p_hex"])

        self.__cert_bin   = cert_bin
        self.__priv_key_P = int.from_bytes(P_bin, "big")
        self.__priv_key_Q = int.from_bytes(Q_bin, "big")
        self.__priv_key_d_mod_p_1   = int.from_bytes(d_mod_p_1_bin, "big")
        self.__priv_key_d_mod_q_1   = int.from_bytes(d_mod_q_1_bin, "big")
        self.__priv_key_inv_q_mod_p = int.from_bytes(inv_q_mod_p_bin, "big")


        f = open("yaml/global.yaml", "r")
        conf = yaml.load(f)
        f.close()

        global_constant_hex = re.sub(r'\s', "", conf["global_constant"])
        self.__global_constant_bin = bytes.fromhex(global_constant_hex) 
Example 32
Project: hdcp_test   Author: imamotts   File: hdcp_sink.py    (MIT License) View Source Project 5 votes vote down vote up
def __hex2bin(self, hex_str):
        ret_bin = bytes.fromhex(re.sub(r'\s', "", hex_str))
        return ret_bin 
Example 33
Project: Cortex-Analyzers   Author: CERT-BDF   File: download_hashes.py    (license) View Source Project 5 votes vote down vote up
def run(path, quiet=False):
    """
    Downloads all available hash files to a given path.
    
    :param path: Path to download directory
    :param quiet: If set to True, no progressbar is displayed
    """
    if os.path.isdir(path):
        session = requests.Session()
        session.headers = {'User-agent': 'Mozilla/5.0 Chrome/57.0.2987.110'}
        max_num = max(list(map(int, re.sub(r'[\<\>]',
                                           '',
                                           '\n'.join(re.findall(r'\>[1-9][0-9]{2}\<',
                                                                session.get('https://virusshare.com/hashes.4n6').text
                                                                )
                                                     )
                                           ).split('\n')
                               )
                           )
                      )
        if not quiet:
            p = progressbar.ProgressBar(max_value=max_num)
        for i in range(max_num):
            filename = str(i).zfill(3) + '.md5'
            if os.path.exists(os.path.join(path, filename)):
                continue
            if not quiet:
                p.update(i)
            url = URL + filename
            head = session.head(url)
            if head.status_code == 200:
                body = session.get(url, stream=True)
                with io.open(os.path.join(path, str(i).zfill(3) + '.md5'), mode='wb') as afile:
                    for chunk in body.iter_content(chunk_size=1024):
                        afile.write(b'' + chunk)
                body.close()
    else:
        print('Given path is not a directory.')
        sys.exit(1) 
Example 34
Project: txt2evernote   Author: Xunius   File: textparse.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def unifyNewline(cls, text):
        return re.sub("\r\n|\r", "\n", text) 
Example 35
Project: txt2evernote   Author: Xunius   File: editor.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def ENMLtoText(contentENML):
        soup = BeautifulSoup(contentENML.decode('utf-8'))

        for section in soup.select('li > p'):
            section.replace_with( section.contents[0] )

        for section in soup.select('li > br'):
            if section.next_sibling:
                next_sibling = section.next_sibling.next_sibling
                if next_sibling:
                    if next_sibling.find('li'):
                        section.extract()
                else:
                    section.extract()

        Editor.checklistInENMLtoSoup(soup)

        for section in soup.findAll('en-todo', checked='true'):
            section.replace_with('[x]')

        for section in soup.findAll('en-todo'):
            section.replace_with('[ ]')

        content = html2text.html2text(str(soup).decode('utf-8'), '', 0)
        content = re.sub(r' *\n', os.linesep, content)

        return content.encode('utf-8') 
Example 36
Project: myhdlpeek   Author: xesscorp   File: myhdlpeek.py    (MIT License) View Source Project 5 votes vote down vote up
def __sub__(self, trc):
        return self.apply_op2(trc, operator.sub) 
Example 37
Project: myhdlpeek   Author: xesscorp   File: myhdlpeek.py    (MIT License) View Source Project 5 votes vote down vote up
def _clean_names(cls):
        '''
        Remove indices from non-repeated peeker names that don't need them.

        When created, all peekers get an index appended to their name to
        disambiguate any repeated names. If the name isn't actually repeated,
        then the index is removed.
        '''

        index_re = '\[\d+\]$'
        for name, peeker in cls._peekers.items():
            if not peeker.name_dup:
                new_name = re.sub(index_re, '', name)
                if new_name != name:
                    peeker.trace.name = new_name
                    cls._peekers[new_name] = cls._peekers.pop(name) 
Example 38
Project: almond-nnparser   Author: Stanford-Mobisocial-IoT-Lab   File: thingtalk.py    (license) View Source Project 5 votes vote down vote up
def clean(name):
    if name.startswith('v_'):
        name = name[len('v_'):]
    return re.sub('([^A-Z])([A-Z])', '$1 $2', re.sub('_', ' ', name)).lower() 
Example 39
Project: almond-nnparser   Author: Stanford-Mobisocial-IoT-Lab   File: prepare.py    (license) View Source Project 5 votes vote down vote up
def clean(name):
    if name.startswith('v_'):
        name = name[len('v_'):]
    return re.sub('([^A-Z])([A-Z])', '$1 $2', re.sub('_', ' ', name)).lower() 
Example 40
Project: newsreap   Author: caronc   File: CodecYenc.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def parse_article(self, subject, *args, **kwargs):
        """
        Takes a an article header and returns it's parsed content if it's
        successful. Otherwise it returns None.
        """

        matched = NZB_SUBJECT_PARSE.match(subject)
        if matched is None:
            # subject is not parsable
            return None

        results = {}

        # Trim results
        if matched.group('desc') is not None:
            results['desc'] = re.sub('[\s-]+$', '', matched.group('desc'))
        if matched.group('fname') is not None:
            results['fname'] = matched.group('fname').strip()

        # Support conversion of integers
        for _attr in ['index', 'count', 'yindex', 'ycount', 'size']:
            if matched.group(_attr) is not None:
                results[_attr] = int(matched.group(_attr))

        return results 
Example 41
Project: newsreap   Author: caronc   File: NNTPHeader.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def __fmt_key(self, key):
        """Formats the hash key for more consistent hits; hence fetching the
        'Message-ID' key should still be fetched even if the user indexes
        with 'message-id'.
        """
        def _fmt(_k):
            return _k.group(1) + _k.group(2).upper()

        if not isinstance(key, basestring):
            # Handle invalid key entries types
            key = str(key)

        key = re.sub(
            # Flip -id to ID (short for Identifier)
            # Flip -crc to CRC (short for Cyclic Redundancy Check)
            r'([_-])((id|crc)([^a-z0-9]|$))',
            _fmt,
            re.sub(r'(^|\s|[_-])(\S)', _fmt, key.strip().lower()),
            flags=re.IGNORECASE,
        )
        if key in VALID_HEADER_ENTRIES or key.startswith(UNKNOWN_PREFIX):
            return key
        return UNKNOWN_PREFIX + key 
Example 42
Project: Starfish   Author: BillWang139967   File: si.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def cpuinfo(self):
        models = []
        bitss = []
        cpuids = []
        with open('/proc/cpuinfo', 'r') as f:
            for line in f:
                if 'model name' in line or 'physical id' in line or 'flags' in line:
                    item, value = line.strip().split(':')
                    item = item.strip()
                    value = value.strip()
                    if item == 'model name':
                        models.append(re.sub('\s+', ' ', value))
                    elif item == 'physical id':
                        cpuids.append(value)
                    elif item == 'flags':
                        if ' lm ' in value:
                            bitss.append('64bit')
                        else:
                            bitss.append('32bit')
        cores = [{'model': x, 'bits': y} for x, y in zip(models, bitss)]
        cpu_count = len(set(cpuids))
        if cpu_count == 0: cpu_count = 1
        return {
            'cores': cores,
            'cpu_count': cpu_count,
            'core_count': len(cores),
        } 
Example 43
Project: fakester   Author: pawelad   File: models.py    (MIT License) View Source Project 5 votes vote down vote up
def clean(self):
        """
        Extends Django's default `clean()` method and add simple local path
        cleaning
        """
        # Remove leading slashes in local path
        self.local_path = self.local_path.lstrip('/')

        # Remove multiple slash characters in a row
        self.local_path = re.sub('/+', '/', self.local_path)

        return super().clean() 
Example 44
Project: xr-telemetry-m2m-web   Author: cisco   File: types.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def _dict(self):
        """
        Construct an ordered dict from the naming elements of the path
        """
        d = OrderedDict()
        for json_fragment in re.findall('\((.*?)\)', self, re.M):
            json_ordered_dict = json.loads(re.sub("'", '"', json_fragment), 
                                           object_pairs_hook=OrderedDict)
            d.update(json_ordered_dict)
        return d 
Example 45
Project: xr-telemetry-m2m-web   Author: cisco   File: reformat.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def process_reply(reply, nested=False):
    """
    Process a reply so it looks nice:

    - if it's from the prototype yang integration, ditch the 'data' root
    - convert from list to nested format if requested
    - convert quotes to avoid escaping
    """
    try:
        # @@@ strip 'data' from yang output
        reply['result'] = reply['result'][0]['data'] 
    except Exception:
        pass

    # If required, and query successful, convert the reply['result'] format.
    try:
        if nested:
            reply['result'] = reformat(reply['result'])
    except KeyError:
        # Fails silently if there is no 'reply['result']' in the reply['result'], this
        # means an error occurred.
        pass

    # @@@ cheesily try to avoid \" everywhere, at cost of valid json
    return re.sub(r'\\"', "'", json.dumps(reply)) 
Example 46
Project: xr-telemetry-m2m-web   Author: cisco   File: render_current_config.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def handle_misses(self, request, mapper, map_misses):
        self.num_misses = len(map_misses)
        self.found_values = []
        for i, miss in enumerate(map_misses):
            progress_indicator(
                request,
                "Incomplete mapping from cache, calculating remainder [{}/{}]..."
                    .format(i + 1, self.num_misses))
            print('### FETCHING MISS {}/{}: {}'.format(i, self.num_misses, miss))
            d = scrape.schema_describe('config ' + miss, request.sdata)
            def got_miss_reply(result, key):
                result = [re.sub("'", '"', path) for path in result]
                if len(result) == 0:
                    result = None
                print('### GOT RESULT ({} remaining, {} saved) {} -> {}'.format(
                        self.num_misses, len(self.found_values), key, result))
                self.found_values.append((key, result))
                self.num_misses -= 1
                if self.num_misses == 0:
                    # Got everything!
                    mapper.populate_cache(self.found_values)
                    self.attempt_map(request, second_attempt=True)
            d.addCallback(got_miss_reply, miss) 
Example 47
Project: xr-telemetry-m2m-web   Author: cisco   File: render_manage_intf.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def display_result(result, sdata):
    # @@@@ some error handling would be nice
    res = result['result']
    if res is None:
        sdata.set_html('#manage_intf_success', 'No changes required')
    elif not isinstance(res, list):
        sdata.set_html('#manage_intf_success', 'Created new commit point ' + res)
    else:
        # @@@ for now convert double-quotes to single, even though it's invalid
        # @@@ JSON, to make it look nicer
        res = '<pre>' + json.dumps(res, indent=4) + '</pre>'
        res = re.sub(r'\\"', "'", res)
        sdata.set_html('#manage_intf_success', res)
        sdata.highlight('#manage_intf_success')
    sdata.add_to_push_queue('stop_current_spinner') 
Example 48
Project: xr-telemetry-m2m-web   Author: cisco   File: render_protobuf.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def render_POST(self, request):
        #
        # Turn a path or show command into a set of candidate protobuf definitions.
        # If it looks like a show command, then schema-describe it, get the set of
        # paths, and run the GPB generation on each one. Otherwise, just run that on
        # the sole provided path.
        #
        path = request.args['path'][0].strip()
        if path.startswith('sh'):
            d = scrape.schema_describe(path, request.sdata)
        else:
            d = defer.succeed([path])
        
        def request_protobufs(paths):
            print('### PROTOBUF PATHS = {}'.format(paths))
            ds = []
            for path in reversed(paths):
                path = re.sub('\(.*?\)', '', path)
                ds.append(request.sdata.api.cli_exec(
                            'run telemetry_generate_gpb "{}"'.format(path)))
            return defer.DeferredList(ds)
        d.addCallback(request_protobufs)

        def get_protobufs(replies):
            line = '-' * 77
            sep = '\n//\n// ' + line + '\n//\n\n'
            text = sep.join([reply[1]['result'] for reply in replies])
            request.sdata.set_text('#protobuf_result', text)
            request.sdata.add_to_push_queue('stop_current_spinner')
            request.sdata.highlight('#protobuf_result')
        d.addCallback(get_protobufs)

        request.setHeader('Content-Type', 'application/json')
        return '{}' 
Example 49
Project: djaio   Author: Sberned   File: utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def get_int_or_none(value):
    """
    >>> get_int_or_none(123)
    123
    >>> get_int_or_none('123asda')
    123
    """
    if isinstance(value, int):
        return value
    try:
        return int(re.sub(r'^(\d+)(.*)$', r'\1', value))
    except (TypeError, ValueError):
        return None 
Example 50
Project: devops-playground   Author: jerrywardlow   File: ec2.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def uncammelize(self, key):
        temp = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', key)
        return re.sub('([a-z0-9])([A-Z])', r'\1_\2', temp).lower()