Why does the Syntaxerror f-string: expecting '}' happening in my code? - python-3.x

Here is where the error happened
print(f'Hey, {Username}, your password is {'*' * Password}. The password is **{len(Password)}** long.')

That is because you are using single quote for main string as well as '*'.
Python interprets it as -
print( f'Hey, {Username}, your password is {'# string ends here.
*' * Password}. The password is **{len(Password)}** long.')
The last bracket goes unmatched and hence the error.
A quick fix would be to use double quotes in one of the strings.
print(f'Hey, {Username}, your password is {"*" * Password}. The password is **{len(Password)}** long.')

Related

Escape character in Groovy to AnsibleTower

During passcode transfer from groovy script to ansible tower plugin, is there any way to send passcode with special character with slash "<passcode>"
ansibleTower(async: false, credential: 'test_cred', extraVars: """
---
password: "U*gLO8br1dMJ\y!U"
"""
While passing a special character it is not able to read the passcode. Is there any regular expression to send this special character
Unable to request job template invocation Extra vars are bad: ["Cannot parse as JSON (error: Expecting value: line 2 column 1 (char 1)) or YAML (error: while scanning a double-quoted scalar\n in \"<unicode string>\", line 4, column 16:\n sudo_password: \"U*gLO8br1dMJ\\y!U\"\n ^\nfound unknown escape character 'y'\n in \"<unicode string>\", line 4, column 30:\n sudo_password: \"U*gLO8br1dMJ\\y!U\"\n ^)."]
Adding an extra "\" in the password will suffice to escape the password. Like "U*gLO8br1dMJ\\y!U" will suffice

System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:18]

#SalesforceChallenge
I'm trying to escape a string but I had no success so far.
This is the response body I'm getting:
{"text":"this \"is something\" I wrote"}
Please note that there are 2 backslashes to escape the double quotes char. (This is a sample. Actually I have a big to escape with lots of "text" elements.)
When I try to deserialize it I get the following error:
System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:18]
I've tried to escape by using:
String my = '{"text":"this \"is something\" I wrote"}';
System.debug('test 0: ' + my);
System.debug('test 1: ' + my.replace('\"', '-'));
System.debug('test 2: ' + my.replace('\\"', '-'));
System.debug('test 3: ' + my.replace('\\\"', '-'));
System.debug('test 4: ' + my.replace('\\\\"', '-'));
--- Results:
[22]|DEBUG|test 0: {"text":"this "is something" I wrote"}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[24]|DEBUG|test 2: {"text":"this "is something" I wrote"}
[25]|DEBUG|test 3: {"text":"this "is something" I wrote"}
[26]|DEBUG|test 4: {"text":"this "is something" I wrote"}
--- What I need as result:
{"text":"this -is something- I wrote"}
Please, does someone has any fix to share?
Thanks a lot.
This is the problem with your test runs in Anonymous Apex:
String my = '{"text":"this \"is something\" I wrote"}';
Because \ is an escape character, you need two backslashes in an Apex string literal to produce a backslash in the actual output:
String my = '{"text":"this \\"is something\\" I wrote"}';
Since Apex quotes strings with ', you don't have to escape the quotes for Apex; you're escaping them for the JSON parser.
The same principle applies to the strings you're trying to use to do replacements: you must escape the \ for Apex.
All that said, it's unclear why you are trying to manually alter this string. The payload
{"text":"this \"is something\" I wrote"}
is valid JSON. In general, you should not perform string replacement on inbound JSON structures in Apex unless you're attempting to compensate for a payload that contains an Apex reserved word as a key so that you can use typed deserialization.

Remove all punctuation from string except full stop (.) and colon (:) in Python

I am trying to remove all punctuation marks from a string except (.) and (:).
This is what I have implemented:
import string
import re
remove = string.punctuation
remove = remove.replace(".", "")
pattern = r"[{}]".format(remove)
line = "NETWORK [listener] connection accepted from 127.0.0.1:59926 #4785 (3 connections now open)"
re.sub(pattern, "", line)
Current output:
NETWORK listener connection accepted from 12700159926 4785 3 connections now open
Desired output:
NETWORK listener connection accepted from 127.0.0.1:59926 4785 3 connections now open
What am I doing wrong? Thanks for the help!
Apart from the fact you don't remove the : from the pattern, the pattern you end up with is:
[!"#$%&'()*+,-/:;<=>?#[\]^_`{|}~]
^^^
Note that ,-/ bit. In a regex, that means all characters between , and / inclusive, including - and ..
You would possibly be better of constructing it manually so as to avoid any tricky escaping requirements other than what you need, something like (untested so I'm not sure if more escaping is required):
pattern = "[!\"#$%&'()*+,\-/:;<=>?#[\]^_`{|}~]"
Alternatively, I'd probably rather allow a specific set of characters to survive rather than specifying a set to remove (the regex will be a lot simpler):
re.sub("[^a-zA-Z :\.]", "", line)
This will only allow alphanumerics, spaces, the colon and the period - everything else will be stripped.
This should work for you:
import string
import re
remove = string.punctuation
remove = re.sub(r"[.:-]+", "", remove)
pattern = r"[{}]".format(remove + '-')
line = "NETWORK [listener] connection accepted from 127.0.0.1:59926 #4785 (3 connections now open)"
re.sub(pattern, "", line)
Output:
NETWORK listener connection accepted from 127.0.0.1:59926 4785 3 connections now open
Details:
For remove = re.sub(r"[.:-]+", "", remove): In character class adding : and - for removal since an unescaped hyphen in middle of a character class acts as range rather than literal -
For r"[{}]".format(remove + '-') we add - in character class in the end, note that unescaped hyphen at the end of [...] is fine
you don't escape special characters in string.punctuation for your regex. also you forgot to replace :!
use re.escape to escape regex special characters in punctuation. your final pattern will be [\!\"\#\$\%\&\'\(\)\*\+\,\-\/\;\<\=\>\?\#\[\\\]\^_\`\{\|\}\~]
import string
import re
remove = string.punctuation
remove = remove.replace(".", "")
remove = remove.replace(":", "")
pattern = r"[{}]".format(re.escape(remove))
line = "NETWORK [listener] connection accepted from 127.0.0.1:59926 #4785 (3 connections now open)"
line = re.sub(pattern, "", line)
output:
NETWORK listener connection accepted from 127.0.0.1:59926 4785 3 connections now open

EOL Error in Python

I am having the following error, can someone explain to me what can I do to fix it.
def increment(i):
request =("https://www.minsalud.gov.co/sites/rid/Paginas/freesearchresults.aspx?k=&k=Salud%20Mental%20Legislacion#k=%2CSalud%20Mental%20Legislacion=+ 1"+ i+")
EOL while scanning string literal
You are missing a closing " and ) at the end of your line
request =("https://www.minsalud.gov.co/sites/rid/Paginas/freesearchresults.aspx?k=&k=Salud%20Mental%20Legislacion#k=%2CSalud%20Mental%20Legislacion=+ 1"+ i+")")

Take out string from "<string>" in Lua

I want to parse some data from a string in Lua. I have tried several combination of string.match and string.sub but no luck. Here is the detail...
str='[{id:78749,name:Hrithik Roshan,character:Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,name:Priyanka Chopra,character:Kaali Gawde,ord'
fixstr = string.gsub(str,"name:","<actors>")
fixstr = string.gsub(fixstr,"character:","<actors>")
print(fixstr)
fixstr1 = string.match( fixstr, "<actors>(.+)<actors>")
print(fixstr1)
Output:
Output of rint(fixstr)
[{id:78749,<actors>Hrithik Roshan,<actors>Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,<actors>Priyanka Chopra,<actors>Kaali Gawde,ord
Output of print(fixstr1)
Hrithik Roshan,<actors>Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,<actors>Priyanka Chopra,
What I am trying to do is get all the string between <actors>...<actors>
but it didn't worked. Can anybody help on this?
To get all the strings between <actors> and <actors>, use string.gmatch for global match:
for name in string.gmatch(fixstr, "<actors>(.-)<actors>") do
print(name)
end
Note the use of .- in which - matches zero or more occurrences, but it's non-greedy.
Output:
Hrithik Roshan,
Priyanka Chopra,
Actually, unless you need fixstr for other uses, you don't need to substitute name: and character: to <actors>, just one run of string.gmatch can do the job:
for name in string.gmatch(str, "name:(.-)character:") do
print(name)
end

Resources