7

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.

Chud37
  • 4,907
  • 13
  • 64
  • 116
  • Maybe some another file has imported this file and `endDomains` is being used in that file. – ZdaR May 11 '17 at 06:42
  • @ZdaR, Oh? How would I test that? – Chud37 May 11 '17 at 06:42
  • Changing `endDomains` into other name such as `end_domains` to check the output again. – Kir Chou May 11 '17 at 06:43
  • what are these modules ? `import pyHook import pythoncom` – ZdaR May 11 '17 at 06:44
  • @KirChou I changed to `end_domains` and got the same result (`http;//google.com`) – Chud37 May 11 '17 at 06:44
  • @ZdaR I don't know, I literally just got into Python yesterday. This code has been modified from here: https://softwarerecs.stackexchange.com/questions/30389/industrial-handheld-qrcode-scanner-open-url-in-browser – Chud37 May 11 '17 at 06:45
  • `re` and `webbrowser` are both redundent in the above programs, `re` is regex and the webbrowser is used to load a page in the webbrowser – Chud37 May 11 '17 at 06:54
  • @Chud37 Firstly you should have credited me for my code in the question. Secondly you've completely changed the original code. In the original re, and webbrowser were both used. endDomains is also referenced. You didn't remove one line, you removed like 10. Also test2 and test3 produce the same output. – Ciprum May 12 '17 at 09:07
  • @Janekmuric I'm sorry, I did credit you in an earlier question but because I'd changed it so much and I was honestly in a rush I didnt get round to it. Thank you for looking at the question though. I know I changed it a huge amount, but I wrote the above to simply try and find the bug. They don't produce the same output on my machine, as I stated above. What could possible cause that? – Chud37 May 12 '17 at 10:02
  • @Janekmuric And can you explain the semicolon and the fact that its all lowercase? I was also really confused by that. Once again Thank you so much for taking the time to look. – Chud37 May 12 '17 at 10:03

1 Answers1

1

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.

Community
  • 1
  • 1
Matt
  • 11
  • 1
  • Hey Matt! The problem was that I wanted to run the program *without* `endDomains` in there, because it wasnt used, but removing it caused the two different outputs, and I couldnt figure out why. – Chud37 May 12 '17 at 12:53
  • I get that. To be clear.... The modules your are using must be affecting the context somehow. There is no way in standard python that would happen. See the example I gave. There must be something that is altering the usage of split (inherited) or something similar. I can't see why even in that case it would affect chars unless the module is also using a global chars. – Matt May 12 '17 at 17:14