From 6852f9e3c00fa9982c25a090a42802d0a31883c7 Mon Sep 17 00:00:00 2001 From: Victor Olaya Date: Mon, 31 Mar 2014 23:31:47 +0200 Subject: [PATCH] [processing] added new script example (contributed by Michael Douchin) --- ...ace_in_string_via_regex_dictionary.py.help | 34 ++++++++++++++++ ..._replace_in_string_via_regex_dictionary.py | 40 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 python/plugins/processing/script/scripts/Batch_ replace_in_string_via_regex_dictionary.py.help create mode 100644 python/plugins/processing/script/scripts/Batch_replace_in_string_via_regex_dictionary.py diff --git a/python/plugins/processing/script/scripts/Batch_ replace_in_string_via_regex_dictionary.py.help b/python/plugins/processing/script/scripts/Batch_ replace_in_string_via_regex_dictionary.py.help new file mode 100644 index 00000000000..2b7c6cd9eed --- /dev/null +++ b/python/plugins/processing/script/scripts/Batch_ replace_in_string_via_regex_dictionary.py.help @@ -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. \ No newline at end of file diff --git a/python/plugins/processing/script/scripts/Batch_replace_in_string_via_regex_dictionary.py b/python/plugins/processing/script/scripts/Batch_replace_in_string_via_regex_dictionary.py new file mode 100644 index 00000000000..0e9529159c8 --- /dev/null +++ b/python/plugins/processing/script/scripts/Batch_replace_in_string_via_regex_dictionary.py @@ -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) \ No newline at end of file