コード例 #1
0
ファイル: data.py プロジェクト: midnightskinhead/chula
def replace_all(subs, input_):
    """
    Simple text replacement, using an input_ dictionary against a string.

    @param subs: Dictionary of find/replace values
    @type subs: Dictionary
    @param input_: String to update
    @type input_: String
    @return: String
    
    >>> print replace_all({'l':'1', 'o':'0'}, "Hello world")
    He110 w0r1d
    """ 
    
    if not input_ is None and input_ != '':
        # Generate a regular expression using the dictionary keys
        regex = re.compile("(%s)" % "|".join(map(re.escape, subs.keys())))

        # Recursively for each match, look-up corresponding value in dictionary
        return regex.sub(lambda mo: subs[mo.string[mo.start():mo.end()]],
                         input_)
    else:
        return input_
コード例 #2
0
ファイル: data.py プロジェクト: jmcfarlane/chula
def replace_all(subs, string):
    """
    Simple text replacement, using an string dictionary against a string.

    :param subs: Dictionary of find/replace values
    :type subs: :class:`dict`
    :param string: String to update
    :type string: :class:`str`
    :rtype: :class:`str`

    >>> from chula import data
    >>> print data.replace_all({'l':'1', 'o':'0'}, "Hello world")
    He110 w0r1d
    """

    if not string is None and string != '':
        # Generate a regular expression using the dictionary keys
        regex = re.compile("(%s)" % "|".join(map(re.escape, subs.keys())))

        # Recursively for each match, look-up corresponding value in dictionary
        return regex.sub(lambda mo: subs[mo.string[mo.start():mo.end()]],
                         string)
    else:
        return string