I have a QR reader. When the QR reader scans, I want to monitor what it scans, But i've run into a weird problem, and as i'm so new to Python I really have no idea why its happening. Okay so, Below are two seemingly (to me) identical programs, apart from line. When that line is removed, I get different results (seemingly the charset changes). I REALLY dont understand why.
test2.py:
# -*- coding: utf-8 -*-
import pyHook
import pythoncom
import re
import webbrowser
chars = ""
def pressed_chars(event):
global chars
if event.Ascii:
char = chr(event.Ascii)
if event.Ascii == 3:
quit()
else:
chars += char
print chars
proc = pyHook.HookManager()
proc.KeyDown = pressed_chars
proc.HookKeyboard()
pythoncom.PumpMessages()
Produces the following output when scanning a QR code with the contents http://google.com
:
H
HT
HTT
HTTP
HTTP:
HTTP:?
HTTP:??
HTTP:??G
HTTP:??GO
HTTP:??GOO
HTTP:??GOOG
HTTP:??GOOGL
HTTP:??GOOGLE
HTTP:??GOOGLE>
HTTP:??GOOGLE>C
HTTP:??GOOGLE>CO
HTTP:??GOOGLE>COM
HTTP:??GOOGLE>COM
And now test3.py:
# -*- coding: utf-8 -*-
import pyHook
import pythoncom
import re
import webbrowser
endDomains = ".com|.net|.org|.edu|.gov|.mil|.aero|.asia|.biz|.cat|.coop|.info|.int|.jobs|.mobi|.museum|.name|.post|.pro|.tel|.travel".split("|")
chars = ""
def pressed_chars(event):
global chars
if event.Ascii:
char = chr(event.Ascii)
if event.Ascii == 3:
quit()
else:
chars += char
print chars
proc = pyHook.HookManager()
proc.KeyDown = pressed_chars
proc.HookKeyboard()
pythoncom.PumpMessages()
Produces the following output:
h
ht
htt
http
http;
http;/
http;//
http;//g
http;//go
http;//goo
http;//goog
http;//googl
http;//google
http;//google.
http;//google.c
http;//google.co
http;//google.com
http;//google.com
If I remove any part of the endDomains
variable, the program changes. Is there some characters that I cant see that I'm removing or something that changes things? Why on earth is python producing these two completely different results when removing a variable that the program doesn't even use?
Edit: It seems to be connected to the .split("|")
, and rather not the variable. If I remove the .split("|")
the program breaks again.
Second Edit Credit for the original source that I used goes to Janekmuric from their answer here.
The way you have defined endDomains is not very 'pythonic'
it would be better to just define it as a list.
endDomains = [
".com", ".net", ".org", ".edu", ".gov", ".mil", ".aero", ".asia",
".biz", ".cat", ".coop", ".info", ".int", ".jobs", ".mobi", ".museum",
".name", ".post", ".pro", ".tel", ".travel"
]
Are you able to return the value and have it picked up so the construction happens outside the event? Unfortunately I'm not on windows so I can not test this. I did write this:
"""Mock."""
endDomains = [
".com", ".net", ".org", ".edu", ".gov", ".mil", ".aero", ".asia",
".biz", ".cat", ".coop", ".info", ".int", ".jobs", ".mobi", ".museum",
".name", ".post", ".pro", ".tel", ".travel"
]
chars = ""
def pressed_chars(event):
global chars
char = chr(ord(event))
if event == '3':
return False
else:
chars += char
print(chars)
return True
result = True
while result:
string = input("Some input please: ")
for character in string:
result = pressed_chars(character)
print("Done")
This does not suffer from the same issue. So I would expect that the issue in pyHook or pythoncom. Also it's worth noting globals are not typically used and because it adds to confusion: Use of "global" keyword in Python
hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With