Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] added new script example (contributed by Michael Douchin)
  • Loading branch information
volaya committed Mar 31, 2014
1 parent 8792bdc commit 6852f9e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
@@ -0,0 +1,34 @@
(dp0
S'ALG_DESC'
p1
VThis algorithm is made to perform powerfull batch search and replace operations inside a string.\u000a\u000aThe user passes a string to be modified, and a string representation of a Python dictionnary with keys as strings to search and values as keys to replace.\u000a\u000aWhen running the algorithm, the following example input string and dictionnary is proposed by default to help the user understand the concept:\u000a\u000ainput string = "John has a blue car."\u000aReplace dictionnary = {"John": "Mary", "blue": "red", "car": "bike"}\u000aGenerated output = "Mary has a red bike."\u000a\u000a\u000a
p2
sS'replaceDict'
p3
VA string representation of a Python dictionnary. \u000a\u000aSimple example : {"John": "Mary", "blue": "red"}\u000awill replace all the occurences of "John" by "Mary" and all the blue by "red" .\u000a\u000aThe Python module "re" is used to give the power of regular expressions. So you can use complex search string and replace string with regexp syntax. The following example shows how to parse a date :\u000a\u000ainput = "2014-03-27"\u000areplace dictionnary = {r"([0-9]{4})-([0-9]{2})-([0-9]{2})" : r"\u005c3/\u005c2/\u005c1" }\u000aoutput = "27/03/2014"\u000a
p4
sS'verbose'
p5
VIf True, the processing dialog will print the input, replace dictionnary and the output in the message box.
p6
sS'ALG_CREATOR'
p7
VMicha�l DOUCHIN ( 3liz.com )\u000a@kimaidou
p8
sS'ignore_case'
p9
VIf True, the search and replace will be case insensitive. Which means that if you search "Bob" and want to replace it with "John", all the "bob" or "boB" will also be replaced.
p10
sS'ALG_HELP_CREATOR'
p11
VMicha�l DOUCHIN ( 3liz.com )\u000a@kimaidou
p12
sS'output'
p13
VThe modified string with all search strings replaced by the given values.
p14
sS'input'
p15
VThe string which must be modified by replacing string(s) by other string(s)
p16
s.
@@ -0,0 +1,40 @@
##[3liz]=group
##Batch string replace via regex dictionnary=name
##input=string John has a blue car.
##ignore_case=boolean True
##verbose=boolean False
##replaceDict=string {'John': 'Mary', 'blue': 'red', 'car': 'bike'}
##output=output string

import ast
import re

if not input: input = ''
if not replaceDict: replaceDict = '{}'

if verbose:
progress.setText('INPUT = \n%s\n' % input)
progress.setText('REPLACE DICT = \n%s\n' % replaceDict)

reOption = re.MULTILINE
if ignore_case:
reOption = reOption|re.IGNORECASE

# Set output string
output = input

# Get replaceDict string
# and convert it to dict
d = ast.literal_eval(replaceDict)

# Only replace strings if d is a dict
if d and isinstance(d, dict):
for k, v in d.items():
# Replace search string by value
r = re.compile(k, reOption)
output = r.sub(v, output)
else:
progress.setText('ERROR - Replace dict does not represent a dictionnary. String not changed !' )

if verbose:
progress.setText('OUTPUT = \n%s\n' % output)

0 comments on commit 6852f9e

Please sign in to comment.