Skip to content

Commit 6852f9e

Browse files
committed
[processing] added new script example (contributed by Michael Douchin)
1 parent 8792bdc commit 6852f9e

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(dp0
2+
S'ALG_DESC'
3+
p1
4+
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
5+
p2
6+
sS'replaceDict'
7+
p3
8+
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
9+
p4
10+
sS'verbose'
11+
p5
12+
VIf True, the processing dialog will print the input, replace dictionnary and the output in the message box.
13+
p6
14+
sS'ALG_CREATOR'
15+
p7
16+
VMicha�l DOUCHIN ( 3liz.com )\u000a@kimaidou
17+
p8
18+
sS'ignore_case'
19+
p9
20+
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.
21+
p10
22+
sS'ALG_HELP_CREATOR'
23+
p11
24+
VMicha�l DOUCHIN ( 3liz.com )\u000a@kimaidou
25+
p12
26+
sS'output'
27+
p13
28+
VThe modified string with all search strings replaced by the given values.
29+
p14
30+
sS'input'
31+
p15
32+
VThe string which must be modified by replacing string(s) by other string(s)
33+
p16
34+
s.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
##[3liz]=group
2+
##Batch string replace via regex dictionnary=name
3+
##input=string John has a blue car.
4+
##ignore_case=boolean True
5+
##verbose=boolean False
6+
##replaceDict=string {'John': 'Mary', 'blue': 'red', 'car': 'bike'}
7+
##output=output string
8+
9+
import ast
10+
import re
11+
12+
if not input: input = ''
13+
if not replaceDict: replaceDict = '{}'
14+
15+
if verbose:
16+
progress.setText('INPUT = \n%s\n' % input)
17+
progress.setText('REPLACE DICT = \n%s\n' % replaceDict)
18+
19+
reOption = re.MULTILINE
20+
if ignore_case:
21+
reOption = reOption|re.IGNORECASE
22+
23+
# Set output string
24+
output = input
25+
26+
# Get replaceDict string
27+
# and convert it to dict
28+
d = ast.literal_eval(replaceDict)
29+
30+
# Only replace strings if d is a dict
31+
if d and isinstance(d, dict):
32+
for k, v in d.items():
33+
# Replace search string by value
34+
r = re.compile(k, reOption)
35+
output = r.sub(v, output)
36+
else:
37+
progress.setText('ERROR - Replace dict does not represent a dictionnary. String not changed !' )
38+
39+
if verbose:
40+
progress.setText('OUTPUT = \n%s\n' % output)

0 commit comments

Comments
 (0)