Re: I need some help interpreting this error
On 2/17/2021 10:40 AM, Chris Green wrote: I'm running this using Python 3.7 on a Linux system. Most of the time (i.e. for a couple of days now) the program has been satifactorily delivering mail messages, hundreds of them. However one mail message has provoked the following error:- chris@cheddar$ tail mail.err Traceback (most recent call last): File "/home/chris/.mutt/bin/filter.py", line 95, in if sbstrip in msghdr["subject"]: TypeError: argument of type 'Header' is not iterable But msghdr["subject"] is surely just a string isn't it? Obviously not. Why is it complaining about something of type 'Header'? Because you tried to iterate it. Header is defined in email.header (find 'class Header'). It has a __str__ to turn one into a string. I have never read the email doc so I have no idea if 'subject' being a Header is a bug. Grepping email package for case-sensitive word 'Header' also returns 3 comment saying that something might be a Header, so stringify it. I have the impression that these might have been added after the original code, perhaps in response to error reports. In any case, you can do the same. As I said the program has been running happily for a couple of days with no errors, I guess it must be something strange in a mail that has broken things - but what? To determine that, look at (after logging or saving) the raw email and maybe the resulting Message (using msg.as_string). # Read the message from standard input and make a message object from it # msg = mailbox.MaildirMessage(sys.stdin.buffer.read()) raw = sys.stdin.buffer.read() # So can save msg = mailbox.MaildirMessage(raw) msghdr["subject"] = msg.get("Subject", "unknown") ... if sbstrip in msghdr["subject"]: Replace with sub = msghdr["subject"] if 'Header' in str(sub.__class__): # Or import email.message.class and use isinstance # stringify or skip or ??? else: msg.replace_header("Subject", msghdr["subject"].replace(sbstrip, -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
On 2021-02-17 at 17:36:48 +, Chris Green wrote: > Stefan Ram wrote: > > Chris Green writes: > > >chris@cheddar$ tail mail.err > > >Traceback (most recent call last): > > > File "/home/chris/.mutt/bin/filter.py", line 95, in > > >if sbstrip in msghdr["subject"]: > > >TypeError: argument of type 'Header' is not iterable > > >But msghdr["subject"] is surely just a string isn't it? Why is it > > >complaining about something of type 'Header'? > > > > I presume that the error message has been edited (abbreviated). > > > > In "if sbstrip in msghdr["subject"]:", there is no argument. > > > > But the error message says "argument of ...". > > > > When something that is not iterable is presented to a for loop, > > the error message does not mention "argument": > > > I have output everything that appears, I've not changed it at all. > It's the whole content of the file ~/tmp/mail.err as it's the only > error that has occurred for the last day or so. The error log is > created by the line:- > > sys.stderr = open("/home/chris/tmp/mail.err", 'a') > > So that's everything that was output to stderr. > > I think you are puzzled in the same way that I was, the error message > doesn't make a lot of sense. At some point, the internal code for the "in" operator is likely iterating over the elements of msghdr["subject"]. That error message doesn't make a lot of sense if msghdr["subject"] is a sub-class of str. It makes more sense if msghdr["subject"] is something else. IMO, you need more information in the log, a try/except block to prevent the code from crashing, and, yes, perhaps some patience to wait for it to happen again. Sometimes, developing software isn't all fortune, fame, and glory. ;-) -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
On 2021-02-17 at 16:52:55 +, Chris Green wrote: > Stefan Ram wrote: > > Chris Green writes: > > >But msghdr["subject"] is surely just a string isn't it? Why is it > > >complaining about something of type 'Header'? > > > > What would you do to debug-print the type of an object? > > > I don't know, what would I do? :-) > > Without knowing what provokes the problem I could be waiting for days > or weeks even before I see the error again. As I'd need to print the > type for every message I'd get some big logs or I'd need to add a try: > except to trap the specific error. An intermittent problem! They're our favorite! ;-) Seriously, though, do you have a message that provokes the problem? Is there a log at all? Can you re-run that message through the code, in a debugger or otherwise? -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
Stestagg wrote: > I don't particularly like to encourage this shotgun help request because, > as previous commenter suggests, debugging this yourself is best. > > Sometimes debugging is super hard, and especially so when uncommon > situations occur, but it's always far easier to debug things when you have > visibility into the system under test. > > However, in this case, the email code is super complex, and this scenario > also looks very uncommon, but not unique: ( > https://github.com/Sydius/mbox-to-txt/issues/2), so you successfully > nerd-sniped me :). > > My *guess*, from reading the python standard library source code is that > you came across an email with some content in the subject line that is > considered a "surrogate", roughly, some badly encoded unicode or binary > data in it. > > When this happens, the code in some situations (depending on the policy...) > may return a header.Header() instance, rather than a > headerregistry.UniqueUnstructuredHeader > (which would have had a headerregistry.BaseHeader (mro: str) dynamically > attached). > > header.Header() does not inherit from str, and thus would throw the > traceback you observed. > Ah, thank you, a possible explanation. > Your suggestion of a try: catch: may make sense, alternately, you could > wrap the result in a call to str(): > > if sbstrip in str(msghdr["subject"]): > > which should attempt to encode the binary into some form of string object > for comparison (I haven't checked exactly what would happen, beyond: it > tries). > Yes, I did consider the str() approach but it feels a bit like making the problem go away without actually fixing it. However since the code in question is only 'cosmetic' (removing unwanted [list name] from the subject, it's not all *that* important to handle it properly. I just need to stop the error from killing my program. > It should be possible to create a test mbox with some funky bytes in the > subject, and try to reproduce it that way. > That's a point, with the clues you have given me I can try some 'bad' subject text and see if I can reproduce the error. Thanks again. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
Some sources, in case they help: Message.get() calls policy.header_fetch_parse ( https://github.com/python/cpython/blob/cd80f430daa7dfe7feeb431ed34f88db5f64aa30/Lib/email/message.py#L471 ) Compat32.header_fetch_parse calls self._sanitize_header ( https://github.com/python/cpython/blob/cd80f430daa7dfe7feeb431ed34f88db5f64aa30/Lib/email/_policybase.py#L311 ) _sanitize_header calls _has_surrogates ( https://github.com/python/cpython/blob/cd80f430daa7dfe7feeb431ed34f88db5f64aa30/Lib/email/_policybase.py#L287 ) _has_surrogates check: https://github.com/python/cpython/blob/cd80f430daa7dfe7feeb431ed34f88db5f64aa30/Lib/email/utils.py#L51 On Wed, Feb 17, 2021 at 5:42 PM Stestagg wrote: > I don't particularly like to encourage this shotgun help request because, > as previous commenter suggests, debugging this yourself is best. > > Sometimes debugging is super hard, and especially so when uncommon > situations occur, but it's always far easier to debug things when you have > visibility into the system under test. > > However, in this case, the email code is super complex, and this scenario > also looks very uncommon, but not unique: ( > https://github.com/Sydius/mbox-to-txt/issues/2), so you successfully > nerd-sniped me :). > > My *guess*, from reading the python standard library source code is that > you came across an email with some content in the subject line that is > considered a "surrogate", roughly, some badly encoded unicode or binary > data in it. > > When this happens, the code in some situations (depending on the > policy...) may return a header.Header() instance, rather than a > headerregistry.UniqueUnstructuredHeader (which would have had a > headerregistry.BaseHeader (mro: str) dynamically attached). > > header.Header() does not inherit from str, and thus would throw the > traceback you observed. > > Your suggestion of a try: catch: may make sense, alternately, you could > wrap the result in a call to str(): > > if sbstrip in str(msghdr["subject"]): > > which should attempt to encode the binary into some form of string object > for comparison (I haven't checked exactly what would happen, beyond: it > tries). > > It should be possible to create a test mbox with some funky bytes in the > subject, and try to reproduce it that way. > > Steve > > > On Wed, Feb 17, 2021 at 5:07 PM Chris Green wrote: > >> Stefan Ram wrote: >> > Chris Green writes: >> > >But msghdr["subject"] is surely just a string isn't it? Why is it >> > >complaining about something of type 'Header'? >> > >> > What would you do to debug-print the type of an object? >> > >> I don't know, what would I do? :-) >> >> Without knowing what provokes the problem I could be waiting for days >> or weeks even before I see the error again. As I'd need to print the >> type for every message I'd get some big logs or I'd need to add a try: >> except to trap the specific error. >> >> -- >> Chris Green >> · >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
Stefan Ram wrote: > Chris Green writes: > >chris@cheddar$ tail mail.err > >Traceback (most recent call last): > > File "/home/chris/.mutt/bin/filter.py", line 95, in > >if sbstrip in msghdr["subject"]: > >TypeError: argument of type 'Header' is not iterable > >But msghdr["subject"] is surely just a string isn't it? Why is it > >complaining about something of type 'Header'? > > I presume that the error message has been edited (abbreviated). > > In "if sbstrip in msghdr["subject"]:", there is no argument. > > But the error message says "argument of ...". > > When something that is not iterable is presented to a for loop, > the error message does not mention "argument": > I have output everything that appears, I've not changed it at all. It's the whole content of the file ~/tmp/mail.err as it's the only error that has occurred for the last day or so. The error log is created by the line:- sys.stderr = open("/home/chris/tmp/mail.err", 'a') So that's everything that was output to stderr. I think you are puzzled in the same way that I was, the error message doesn't make a lot of sense. > |>>> for i in 0: > |...print(i) > |... > |Traceback (most recent call last): > | File "", line 1, in > |TypeError: 'int' object is not iterable > > . > > -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
I don't particularly like to encourage this shotgun help request because, as previous commenter suggests, debugging this yourself is best. Sometimes debugging is super hard, and especially so when uncommon situations occur, but it's always far easier to debug things when you have visibility into the system under test. However, in this case, the email code is super complex, and this scenario also looks very uncommon, but not unique: ( https://github.com/Sydius/mbox-to-txt/issues/2), so you successfully nerd-sniped me :). My *guess*, from reading the python standard library source code is that you came across an email with some content in the subject line that is considered a "surrogate", roughly, some badly encoded unicode or binary data in it. When this happens, the code in some situations (depending on the policy...) may return a header.Header() instance, rather than a headerregistry.UniqueUnstructuredHeader (which would have had a headerregistry.BaseHeader (mro: str) dynamically attached). header.Header() does not inherit from str, and thus would throw the traceback you observed. Your suggestion of a try: catch: may make sense, alternately, you could wrap the result in a call to str(): if sbstrip in str(msghdr["subject"]): which should attempt to encode the binary into some form of string object for comparison (I haven't checked exactly what would happen, beyond: it tries). It should be possible to create a test mbox with some funky bytes in the subject, and try to reproduce it that way. Steve On Wed, Feb 17, 2021 at 5:07 PM Chris Green wrote: > Stefan Ram wrote: > > Chris Green writes: > > >But msghdr["subject"] is surely just a string isn't it? Why is it > > >complaining about something of type 'Header'? > > > > What would you do to debug-print the type of an object? > > > I don't know, what would I do? :-) > > Without knowing what provokes the problem I could be waiting for days > or weeks even before I see the error again. As I'd need to print the > type for every message I'd get some big logs or I'd need to add a try: > except to trap the specific error. > > -- > Chris Green > · > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
2qdxy4rzwzuui...@potatochowder.com wrote: > On 2021-02-17 at 16:42:03 +, > Chris Green wrote: > > > 2qdxy4rzwzuui...@potatochowder.com wrote: > > > On 2021-02-17 at 15:40:27 +, > > > Chris Green wrote: > > > > > > > I'm running this using Python 3.7 on a Linux system. > > > > > > > > Most of the time (i.e. for a couple of days now) the program has been > > > > satifactorily delivering mail messages, hundreds of them. However one > > > > mail message has provoked the following error:- > > > > > > > > chris@cheddar$ tail mail.err > > > > Traceback (most recent call last): > > > > File "/home/chris/.mutt/bin/filter.py", line 95, in > > > > if sbstrip in msghdr["subject"]: > > > > TypeError: argument of type 'Header' is not iterable > > > > > > > > > > > > But msghdr["subject"] is surely just a string isn't it? Why is it > > > > complaining about something of type 'Header'? > > > > > > Isn't it? ;-) > > > > > > First step: Print msghdr["subject"] and its type to know for sure. The > > > worst case is that you'll verify your assumption. > > > > > The documentation says "Headers are represented by customized > > subclasses of str", so it's a sub-class of str. > > So we still don't know what the content of msghdr["subject"] is at the > time the error occurs. I don't mean to sound harsh, but that the > documentation and the code are correct, and that they match, remain > assumptions. Sometimes, seeing an actual value tells you what went > wrong (e.g., "oh, that's the sender's address, not the receiver's > address," "oh, that's my 'time' class, not the one from the standard > library"). > > The traceback tells you that msghdr["subject"] is of type Header. Is > Header a sub-class of str? > That's exactly what puzzled me! The line that gets the value is:- msghdr["subject"] = msg.get("Subject", "unknown") What I need to know is how that can return a value of type Header, and not a str. > Again, the worst case of looking at the value (whether in a log or in a > debugger) is that you verify your assumption. > > > > IIRC, the subject header is actually optional. Maybe someone sent a > > > message without a subject? Is msghdr["subject"] None? > > > > If you look at the code (and the documentation) if there's no subject > > header I'll get the string "unknown", I've also tried sending myself > > an E-Mail with no header and not provoked the error. > > That's good news about subject-less emails not generating an error, and > separate (possibly related) good news about your code handling an email > without a subject header. ;-) I think the only sane approach at the moment may be to add a try: except: and output some diagnostic information. Though there may still be an issue when trying to output the "what is it" object to the error log of course. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
Stefan Ram wrote: > Chris Green writes: > >But msghdr["subject"] is surely just a string isn't it? Why is it > >complaining about something of type 'Header'? > > What would you do to debug-print the type of an object? > I don't know, what would I do? :-) Without knowing what provokes the problem I could be waiting for days or weeks even before I see the error again. As I'd need to print the type for every message I'd get some big logs or I'd need to add a try: except to trap the specific error. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
On 2021-02-17 at 16:42:03 +, Chris Green wrote: > 2qdxy4rzwzuui...@potatochowder.com wrote: > > On 2021-02-17 at 15:40:27 +, > > Chris Green wrote: > > > > > I'm running this using Python 3.7 on a Linux system. > > > > > > Most of the time (i.e. for a couple of days now) the program has been > > > satifactorily delivering mail messages, hundreds of them. However one > > > mail message has provoked the following error:- > > > > > > chris@cheddar$ tail mail.err > > > Traceback (most recent call last): > > > File "/home/chris/.mutt/bin/filter.py", line 95, in > > > if sbstrip in msghdr["subject"]: > > > TypeError: argument of type 'Header' is not iterable > > > > > > > > > But msghdr["subject"] is surely just a string isn't it? Why is it > > > complaining about something of type 'Header'? > > > > Isn't it? ;-) > > > > First step: Print msghdr["subject"] and its type to know for sure. The > > worst case is that you'll verify your assumption. > > > The documentation says "Headers are represented by customized > subclasses of str", so it's a sub-class of str. So we still don't know what the content of msghdr["subject"] is at the time the error occurs. I don't mean to sound harsh, but that the documentation and the code are correct, and that they match, remain assumptions. Sometimes, seeing an actual value tells you what went wrong (e.g., "oh, that's the sender's address, not the receiver's address," "oh, that's my 'time' class, not the one from the standard library"). The traceback tells you that msghdr["subject"] is of type Header. Is Header a sub-class of str? Again, the worst case of looking at the value (whether in a log or in a debugger) is that you verify your assumption. > > IIRC, the subject header is actually optional. Maybe someone sent a > > message without a subject? Is msghdr["subject"] None? > > If you look at the code (and the documentation) if there's no subject > header I'll get the string "unknown", I've also tried sending myself > an E-Mail with no header and not provoked the error. That's good news about subject-less emails not generating an error, and separate (possibly related) good news about your code handling an email without a subject header. ;-) -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
2qdxy4rzwzuui...@potatochowder.com wrote: > On 2021-02-17 at 15:40:27 +, > Chris Green wrote: > > > I'm running this using Python 3.7 on a Linux system. > > > > Most of the time (i.e. for a couple of days now) the program has been > > satifactorily delivering mail messages, hundreds of them. However one > > mail message has provoked the following error:- > > > > chris@cheddar$ tail mail.err > > Traceback (most recent call last): > > File "/home/chris/.mutt/bin/filter.py", line 95, in > > if sbstrip in msghdr["subject"]: > > TypeError: argument of type 'Header' is not iterable > > > > > > But msghdr["subject"] is surely just a string isn't it? Why is it > > complaining about something of type 'Header'? > > Isn't it? ;-) > > First step: Print msghdr["subject"] and its type to know for sure. The > worst case is that you'll verify your assumption. > The documentation says "Headers are represented by customized subclasses of str", so it's a sub-class of str. > IIRC, the subject header is actually optional. Maybe someone sent a > message without a subject? Is msghdr["subject"] None? If you look at the code (and the documentation) if there's no subject header I'll get the string "unknown", I've also tried sending myself an E-Mail with no header and not provoked the error. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
On 2021-02-17 at 15:40:27 +, Chris Green wrote: > I'm running this using Python 3.7 on a Linux system. > > Most of the time (i.e. for a couple of days now) the program has been > satifactorily delivering mail messages, hundreds of them. However one > mail message has provoked the following error:- > > chris@cheddar$ tail mail.err > Traceback (most recent call last): > File "/home/chris/.mutt/bin/filter.py", line 95, in > if sbstrip in msghdr["subject"]: > TypeError: argument of type 'Header' is not iterable > > > But msghdr["subject"] is surely just a string isn't it? Why is it > complaining about something of type 'Header'? Isn't it? ;-) First step: Print msghdr["subject"] and its type to know for sure. The worst case is that you'll verify your assumption. IIRC, the subject header is actually optional. Maybe someone sent a message without a subject? Is msghdr["subject"] None? -- https://mail.python.org/mailman/listinfo/python-list
I need some help interpreting this error
I'm running this using Python 3.7 on a Linux system. Most of the time (i.e. for a couple of days now) the program has been satifactorily delivering mail messages, hundreds of them. However one mail message has provoked the following error:- chris@cheddar$ tail mail.err Traceback (most recent call last): File "/home/chris/.mutt/bin/filter.py", line 95, in if sbstrip in msghdr["subject"]: TypeError: argument of type 'Header' is not iterable But msghdr["subject"] is surely just a string isn't it? Why is it complaining about something of type 'Header'? As I said the program has been running happily for a couple of days with no errors, I guess it must be something strange in a mail that has broken things - but what? Full program listed below:- #!/usr/bin/python3 # # # licenseApache v2 (http://www.apache.org/licenses/LICENSE-2.0) # author Chris Green - ch...@isbd.co.uk # # # # Mail filtering script # import mailbox import os import sys import time import mailLib import shlex # # # Redirect any exceptions to a file # sys.stderr = open("/home/chris/tmp/mail.err", 'a') # # # Some constants (i.e. configuration) # home = "/home/chris" logfile = home + "/tmp/mail.log" filtfile = home + "/.mutt/filter" mldir = home + "/mail/" indir = mldir + "In/" # # # Set to log to mail.log in ~/tmp with name 'filter' and the envelope/from # log = mailLib.initLog("filter") # # # Initialise destination mailbox name to its default "In/default" # dest = indir + "default" # # # Read the message from standard input and make a message object from it # msg = mailbox.MaildirMessage(sys.stdin.buffer.read()) # # # Extract various headers and the envelope/from # msghdr = {} msghdr["to"] = msg.get("To", "unknown").lower() msghdr["subject"] = msg.get("Subject", "unknown") msghdr["list-id"] = msg.get("List-Id", "unknown").lower() msghdr["list-post"] = msg.get("List-Post", "unknown").lower() msghdr["x-mailing-list"] = msg.get("X-Mailing-List", "unknown").lower() msghdr["unknown"] = "unknown" # # # See if there's a match in our filter file # f = open(filtfile, 'r') for ln in f:# for each line in filter if ln[0] == '#':# ignore comments continue # # # split the line into fields, shlex.split() does quoted strings, add a field # to create a dummy fourth field if there isn't one in the filter file # fld = shlex.split(ln) fld.append("") # # # copy the fields into better named variables # nm = fld[0] # name/alias destdir = fld[1] + "/" # the destination directory header = fld[2] # the header to find the match in address = fld[3].lower()# list address to match sbstrip = '[' + fld[4] + ']'# string to strip out of subject # # # Does the address in the header match this entry # if (address in msghdr[header]): # # # set the destination directory # dest = mldir + destdir + nm # # # Strip out list name (4th field) from subject if it's there # if sbstrip in msghdr["subject"]: msg.replace_header("Subject", msghdr["subject"].replace(sbstrip, '')) # # # we've found a match so assume we won't get another # break # # # deliver the message # mailLib.deliverMdMsg(dest, msg, log) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Hi! i need some help with a program in python on Raspberry pi3.
On Friday, April 14, 2017 at 3:27:29 PM UTC+1, Kasper wrote: > every time i run the program i get this messeage: > > Traceback (most recent call last): > File "smartmirror.py", line 159, in get_weather > temprature2 = "%S%S" % (str(int(weather_obj['currently']['temperature'])), > degree_sign) > KeyError: 'currently' > Error: 'currently'. Cannot get weather. > > How do i fix that? > > Here is the program: > > r = requests.get(weather_req_url) > weather_obj = json.loads(r.text) > > degree_sign= u'\N{DEGREE SIGN}' > temperature2 = "%s%s" % > (str(int(weather_obj['currently']['temperature'])), degree_sign) Find the correct name for the key by printing out `weather_obj`. At least I think so, as the line above does not match the line that you've given. Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: Hi! i need some help with a program in python on Raspberry pi3.
On 4/14/2017 10:27 AM, Kasper wrote: every time i run the program i get this messeage: Traceback (most recent call last): File "smartmirror.py", line 159, in get_weather temprature2 = "%S%S" % (str(int(weather_obj['currently']['temperature'])), degree_sign) KeyError: 'currently' Error: 'currently'. Cannot get weather. How do i fix that? Use a valid key for weather_obj. Check doc or add print(list(weather_obj.keys())) before the failing subscription. Here is the program: I second Steve's comment. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Hi! i need some help with a program in python on Raspberry pi3.
Kasper wrote: > every time i run the program i get this messeage: > > Traceback (most recent call last): > File "smartmirror.py", line 159, in get_weather > temprature2 = "%S%S" % (str(int(weather_obj['currently'] ['temperature'])), > degree_sign) > KeyError: 'currently' > Error: 'currently'. Cannot get weather. > > How do i fix that? > > Here is the program: > > weather_api_token = '16dc67b56f94f8083b1afed7e69c5dc1' # create account at > https://darksky.net/dev/ Oops, is that your personal token? You are probably not supposed to publish it and now run the risk of having it revoked. > values weather_unit = 'nb' # see https://darksky.net/dev/docs/forecast for > full list of unit parameters values You have to replace nb with one of the allowed values listed on the site. > weather_lang = 'nb' # see > https://darksky.net/dev/docs/forecast for full list of language parameters Again, you have to pick one of the values listed -- unless you want Norsk bokmål that is. -- https://mail.python.org/mailman/listinfo/python-list
Re: Hi! i need some help with a program in python on Raspberry pi3.
On Sat, 15 Apr 2017 12:27 am, Kasper wrote: > every time i run the program i get this messeage: > > Traceback (most recent call last): > File "smartmirror.py", line 159, in get_weather > temprature2 = "%S%S" % > (str(int(weather_obj['currently']['temperature'])), > degree_sign) > KeyError: 'currently' > Error: 'currently'. Cannot get weather. > > How do i fix that? We're unpaid volunteers, we're not being paid to debug your code. Don't dump over 300 lines of code in our lap and expect us to debug it. If you can't spend the time simplifying the problem and removing all the irrelevant details, how can you expect us to do it for you? If you want help, make it easy for people to help you. Cut your code down to the simplest, shortest amount of code that demonstrates the problem. If your code has nothing to do with the GUI, cut out all the GUI code. More advice here: http://sscce.org/ -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Hi! i need some help with a program in python on Raspberry pi3.
every time i run the program i get this messeage: Traceback (most recent call last): File "smartmirror.py", line 159, in get_weather temprature2 = "%S%S" % (str(int(weather_obj['currently']['temperature'])), degree_sign) KeyError: 'currently' Error: 'currently'. Cannot get weather. How do i fix that? Here is the program: # smartmirror.py # requirements # requests, feedparser, traceback, Pillow from Tkinter import * import locale import threading import time import requests import json import traceback import feedparser from PIL import Image, ImageTk from contextlib import contextmanager LOCALE_LOCK = threading.Lock() ui_locale = '' # e.g. 'fr_FR' fro French, '' as default time_format = 24 # 12 or 24 date_format = "%b %d, %Y" # check python doc for strftime() for options news_country_code = 'nb' weather_api_token = '16dc67b56f94f8083b1afed7e69c5dc1' # create account at https://darksky.net/dev/ weather_lang = 'nb' # see https://darksky.net/dev/docs/forecast for full list of language parameters values weather_unit = 'nb' # see https://darksky.net/dev/docs/forecast for full list of unit parameters values latitude = (59.1311800) # Set this if IP location lookup does not work for you (must be a string) longitude = (10.2166500) # Set this if IP location lookup does not work for you (must be a string) xlarge_text_size = 94 large_text_size = 48 medium_text_size = 28 small_text_size = 18 @contextmanager def setlocale(name): #thread proof function to work with locale with LOCALE_LOCK: saved = locale.setlocale(locale.LC_ALL) try: yield locale.setlocale(locale.LC_ALL, name) finally: locale.setlocale(locale.LC_ALL, saved) # maps open weather icons to # icon reading is not impacted by the 'lang' parameter icon_lookup = { 'clear-day': "assets/Sun.png", # clear sky day 'wind': "assets/Wind.png", #wind 'cloudy': "assets/Cloud.png", # cloudy day 'partly-cloudy-day': "assets/PartlySunny.png", # partly cloudy day 'rain': "assets/Rain.png", # rain day 'snow': "assets/Snow.png", # snow day 'snow-thin': "assets/Snow.png", # sleet day 'fog': "assets/Haze.png", # fog day 'clear-night': "assets/Moon.png", # clear sky night 'partly-cloudy-night': "assets/PartlyMoon.png", # scattered clouds night 'thunderstorm': "assets/Storm.png", # thunderstorm 'tornado': "assests/Tornado.png",# tornado 'hail': "assests/Hail.png" # hail } class Clock(Frame): def __init__(self, parent, *args, **kwargs): Frame.__init__(self, parent, bg='black') # initialize time label self.time1 = '' self.timeLbl = Label(self, font=('Helvetica', large_text_size), fg="white", bg="black") self.timeLbl.pack(side=TOP, anchor=E) # initialize day of week self.day_of_week1 = '' self.dayOWLbl = Label(self, text=self.day_of_week1, font=('Helvetica', small_text_size), fg="white", bg="black") self.dayOWLbl.pack(side=TOP, anchor=E) # initialize date label self.date1 = '' self.dateLbl = Label(self, text=self.date1, font=('Helvetica', small_text_size), fg="white", bg="black") self.dateLbl.pack(side=TOP, anchor=E) self.tick() def tick(self): with setlocale(ui_locale): if time_format == 12: time2 = time.strftime('%I:%M %p') #hour in 12h format else: time2 = time.strftime('%H:%M') #hour in 24h format day_of_week2 = time.strftime('%A') date2 = time.strftime(date_format) # if time string has changed, update it if time2 != self.time1: self.time1 = time2 self.timeLbl.config(text=time2) if day_of_week2 != self.day_of_week1: self.day_of_week1 = day_of_week2 self.dayOWLbl.config(text=day_of_week2) if date2 != self.date1: self.date1 = date2 self.dateLbl.config(text=date2) # calls itself every 200 milliseconds # to update the time display as needed # could use >200 ms, but display gets jerky self.timeLbl.after(200, self.tick) class Weather(Frame): def __init__(self, parent, *args, **kwargs): Frame.__init__(self, parent, bg='black') self.temperature = '' self.forecast = '' self.location = '' self.currently = '' self.icon = '' self.degreeFrm = Frame(self, bg="black") self.degreeFrm.pack(side=TOP, anchor=W) self.temperatureLbl = Label(self.degreeFrm, font=('Helvetica', xlarge_text_size), fg="white", bg="black") self.temperatureLbl.pack(side=LEFT, anchor=N) self.iconLbl = Label(self.degreeFrm, bg="black") self.iconLbl.pack(side=LEFT, anchor=N, padx=20) self.currentlyLbl = Label(self, font=('Helvetica', medium_text_size), fg="white", bg="black")
Re: I need some help with a regexp please
Frederic Rentsch wrote: > If I may add another thought along the same line: regular expressions > seem to tend towards an art form, or an intellectual game. Many > discussions revolving around regular expressions convey the impression > that the challenge being pursued is finding a magic formula much more > than solving a problem. In addition there seems to exist some code of > honor which dictates that the magic formula must consist of one single > expression that does it all. hear! hear! for dense guys like myself, regular expressions work best if you use them as simple tokenizers, and they suck pretty badly if you're trying to use them as parsers. and using a few RE:s per problem (or none at all) is a perfectly good way to get things done. -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
> for dense guys like myself, regular expressions work best if you use > them as simple tokenizers, and they suck pretty badly if you're trying > to use them as parsers. :) Well, I'm with you on that one Fredrik! :) -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
> I still don't touch regular expressions... They may be fast, but to > me they are just as much line noise as PERL... I can usually code a > partial "parser" faster than try to figure out an RE. Yes, it seems to me that REs are a bit "hit and miss" - the only way to tell if you've got a RE "right" is by testing exhaustively - but you can never be sure They are fine for simple pattern matching though. -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
Dennis Lee Bieber wrote: > On 25 Sep 2006 10:25:01 -0700, "codefire" <[EMAIL PROTECTED]> > declaimed the following in comp.lang.python: > > >> Yes, I didn't make it clear in my original post - the purpose of the >> code was to learn something about regexps (I only started coding Python >> last week). In terms of learning "a little more" the example was >> successful. However, creating a full email validator is way beyond me - >> the rules are far too complex!! :) >> > > I've been doing small things in Python for over a decade now > (starting with the Amiga port)... > > I still don't touch regular expressions... They may be fast, but to > me they are just as much line noise as PERL... I can usually code a > partial "parser" faster than try to figure out an RE. > If I may add another thought along the same line: regular expressions seem to tend towards an art form, or an intellectual game. Many discussions revolving around regular expressions convey the impression that the challenge being pursued is finding a magic formula much more than solving a problem. In addition there seems to exist some code of honor which dictates that the magic formula must consist of one single expression that does it all. I suspect that the complexity of one single expression grows somehow exponentially with the number of functionalities it has to perform and at some point enters a gray zone of impending conceptual intractability where the quest for the magic formula becomes particularly fascinating. I also suspect that some problems are impossible to solve with a single expression and that no test of intractability exists other than giving up after so many hours of trying. With reference to the OP's question, what speaks against passing his texts through several simple expressions in succession? Speed of execution? Hardly. The speed penalty would not be perceptible. Conversely, in favor of multiple expressions speaks that they can be kept simple and that the performance of the entire set can be incrementally improved by adding another simple expression whenever an unexpected contingency occurs, as they may occur at any time with informal systems. One may not win a coding contest this way, but saving time isn't bad either, or is even better. Frederic -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
Yes, I didn't make it clear in my original post - the purpose of the code was to learn something about regexps (I only started coding Python last week). In terms of learning "a little more" the example was successful. However, creating a full email validator is way beyond me - the rules are far too complex!! :) -- http://mail.python.org/mailman/listinfo/python-list
I need some help with a regexp please
Hi,My $0.02:re.compile('^\w+([\.-]?\w+)[EMAIL PROTECTED]([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|intl|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$')I picked it up from the Net, and while it may be not perfect (you've got lots of reply's telling you why),it's good enough for me.Good luck,Sorin-- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
John Machin wrote: > Ant wrote: > > John Machin wrote: > > ... > > > A little more is unfortunately not enough. The best advice you got was > > > to use an existing e-mail address validator. > > > > We got bitten by this at the last place I worked - we were using a > > regex email validator (from Microsoft IIRC) ... > Oh, sorry for the abbreviation. "use" implies "source from believedly > reliable s/w source; test; then deploy" :-) I actually meant that we got bitten by using a regex validator, not by using an existing one. Though we did get bitten by an existing one, and it being from Microsoft we should have known better ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
Ant wrote: > John Machin wrote: > ... > > A little more is unfortunately not enough. The best advice you got was > > to use an existing e-mail address validator. > > We got bitten by this at the last place I worked - we were using a > regex email validator (from Microsoft IIRC), and we kept having > problems with specific email addresses from Ireland. There are stack of > Irish email addresses out there of the form paddy.o'[EMAIL PROTECTED] - > perfectly valid email address, but doesn't satisfy the usual naive > versions of regex validators. > > We use an even worse validator at my current job, but the feeling the > management have (not one I agree with) is that unusual email addresses, > whilst perhaps valid, are uncommon enough not to worry about Oh, sorry for the abbreviation. "use" implies "source from believedly reliable s/w source; test; then deploy" :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Don't use regular expressions to "validate" email addresses (was: I need some help with a regexp please)
Ben Finney wrote: ... > The best advice I've seen when people ask "How do I validate whether > an email address is valid?" was "Try sending mail to it". There are advantages to the regex method. It is faster than sending an email and getting a positive or negative return code. The delay may not be acceptable in many applications. Secondly, the false negatives found by a reasonable regex will be few compared to the number you'd get if the smtp server went down, or a remote relay was having problems delivering the message etc etc. >From a business point of view, it is probably more important to reduce the number of false negatives than to reduce the number of false positives - every false negative is a potential loss of a customer. False positives? Who cares really as long as they are paying ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
John Machin wrote: ... > A little more is unfortunately not enough. The best advice you got was > to use an existing e-mail address validator. We got bitten by this at the last place I worked - we were using a regex email validator (from Microsoft IIRC), and we kept having problems with specific email addresses from Ireland. There are stack of Irish email addresses out there of the form paddy.o'[EMAIL PROTECTED] - perfectly valid email address, but doesn't satisfy the usual naive versions of regex validators. We use an even worse validator at my current job, but the feeling the management have (not one I agree with) is that unusual email addresses, whilst perhaps valid, are uncommon enough not to worry about -- http://mail.python.org/mailman/listinfo/python-list
Don't use regular expressions to "validate" email addresses (was: I need some help with a regexp please)
"John Machin" <[EMAIL PROTECTED]> writes: > A little more is unfortunately not enough. The best advice you got was > to use an existing e-mail address validator. The definition of a valid > e-mail address is complicated. You may care to check out "Mastering > Regular Expressions" by Jeffery Friedl. In the first edition, at least > (I haven't looked at the 2nd), he works through assembling a 4700+ byte > regex for validating e-mail addresses. Yes, that's 4KB. It's the best > advertisement for *not* using regexes for a task like that that I've > ever seen. The best advice I've seen when people ask "How do I validate whether an email address is valid?" was "Try sending mail to it". It's both Pythonic, and truly the best way. If you actually want to confirm, don't try to validate it statically; *use* the email address, and check the result. Send an email to that address, and don't use it any further unless you get a reply saying "yes, this is the right address to use" from the recipient. The sending system's mail transport agent, not regular expressions, determines which part is the domain to send the mail to. The domain name system, not regular expressions, determines what domains are valid, and what host should receive mail for that domain. Most especially, the receiving mail system, not regular expressions, determines what local-parts are valid. -- \ "I believe in making the world safe for our children, but not | `\our children's children, because I don't think children should | _o__) be having sex." -- Jack Handey | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
codefire wrote: > Hi, > > thanks for the advice guys. > > Well took the kids swimming, watched some TV, read your hints and > within a few minutes had this: > > r = re.compile(r'[EMAIL PROTECTED]@\s]+\.\w+') > > This works for me. That is if you have an invalid email such as > tony..bATblah.com it will reject it (note the double dots). > > Anyway, now know a little more about regexps :) A little more is unfortunately not enough. The best advice you got was to use an existing e-mail address validator. The definition of a valid e-mail address is complicated. You may care to check out "Mastering Regular Expressions" by Jeffery Friedl. In the first edition, at least (I haven't looked at the 2nd), he works through assembling a 4700+ byte regex for validating e-mail addresses. Yes, that's 4KB. It's the best advertisement for *not* using regexes for a task like that that I've ever seen. Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
Hi, thanks for the advice guys. Well took the kids swimming, watched some TV, read your hints and within a few minutes had this: r = re.compile(r'[EMAIL PROTECTED]@\s]+\.\w+') This works for me. That is if you have an invalid email such as tony..bATblah.com it will reject it (note the double dots). Anyway, now know a little more about regexps :) Thanks again for the hints, Tony -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
codefire wrote: > Hi, > > I am trying to get a regexp to validate email addresses but can't get > it quite right. The problem is I can't quite find the regexp to deal > with ignoring the case [EMAIL PROTECTED], which is not valid. Here's > my attempt, neither of my regexps work quite how I want: > > [code] > import os > import re > > s = 'Hi [EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED] @@not > [EMAIL PROTECTED] partridge in a pear tree' > r = re.compile(r'[EMAIL PROTECTED]@\s]+\.\w+') > #r = re.compile(r'[EMAIL PROTECTED]') > > addys = set() > for a in r.findall(s): > addys.add(a) > > for a in sorted(addys): > print a > [/code] > > This gives: > [EMAIL PROTECTED] > [EMAIL PROTECTED] > [EMAIL PROTECTED] <-- shouldn't be here :( > [EMAIL PROTECTED] > > Nearly there but no cigar :) > > I can't see the wood for the trees now :) Can anyone suggest a fix > please? > The problem is that your pattern doesn't start out by confirming that it's either at the start of a line or after whitespace. You could do this with a "look-behind assertion" if you wanted. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://holdenweb.blogspot.com Recent Ramblings http://del.icio.us/steve.holden -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
On 2006-09-21, codefire <[EMAIL PROTECTED]> wrote: > I am trying to get a regexp to validate email addresses but > can't get it quite right. The problem is I can't quite find the > regexp to deal with ignoring the case [EMAIL PROTECTED], > which is not valid. Here's my attempt, neither of my regexps > work quite how I want: I suggest a websearch for email address validators instead of writing of your own. Here's a hit that looks useful: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66439 -- Neil Cerutti Next Sunday Mrs. Vinson will be soloist for the morning service. The pastor will then speak on "It's a Terrible Experience." --Church Bulletin Blooper -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help with a regexp please
codefire wrote: > Hi, > > I am trying to get a regexp to validate email addresses but can't get > it quite right. The problem is I can't quite find the regexp to deal > with ignoring the case [EMAIL PROTECTED], which is not valid. Here's > my attempt, neither of my regexps work quite how I want: > > [code] > import os > import re > > s = 'Hi [EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED] @@not > [EMAIL PROTECTED] partridge in a pear tree' > r = re.compile(r'[EMAIL PROTECTED]@\s]+\.\w+') > #r = re.compile(r'[EMAIL PROTECTED]') > > addys = set() > for a in r.findall(s): > addys.add(a) > > for a in sorted(addys): > print a > [/code] > > This gives: > [EMAIL PROTECTED] > [EMAIL PROTECTED] > [EMAIL PROTECTED] <-- shouldn't be here :( > [EMAIL PROTECTED] > > Nearly there but no cigar :) > > I can't see the wood for the trees now :) Can anyone suggest a fix > please? > > Thanks, > Tony '[EMAIL PROTECTED](\.\w+)*' Works for me, and SHOULD for you, but I haven't tested it all that much. Good luck. -- http://mail.python.org/mailman/listinfo/python-list
I need some help with a regexp please
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case [EMAIL PROTECTED], which is not valid. Here's my attempt, neither of my regexps work quite how I want: [code] import os import re s = 'Hi [EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED] @@not [EMAIL PROTECTED] partridge in a pear tree' r = re.compile(r'[EMAIL PROTECTED]@\s]+\.\w+') #r = re.compile(r'[EMAIL PROTECTED]') addys = set() for a in r.findall(s): addys.add(a) for a in sorted(addys): print a [/code] This gives: [EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED] <-- shouldn't be here :( [EMAIL PROTECTED] Nearly there but no cigar :) I can't see the wood for the trees now :) Can anyone suggest a fix please? Thanks, Tony -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help
Tim Heaney wrote: > Several different people have written modules to help you read (and > write) ID3 tags. On a related topic, I have a Perl module that reads MP4/AAC tags - http://search.cpan.org/~jhar/MP4-Info/ - that I'm considering porting to Python. But before I start, is anyone aware of an existing Python module to read MP4/AAC tags? Jonathan. -- http://mail.python.org/mailman/listinfo/python-list
Re: I need some help
"dammix" <[EMAIL PROTECTED]> writes: > > I'm completely a newbye, I've started studying python since 3 weeks and > now I need to write a small program that reads the id3 tags from the > mp3 contained inside a cd, and then print them into a simple text file, > I hope it's possible to do this, and I hope you can help me too. Several different people have written modules to help you read (and write) ID3 tags. http://id3-py.sourceforge.net/ http://pyid3lib.sourceforge.net/ http://news.tiker.net/software/tagpy Pick one you like! Tim -- http://mail.python.org/mailman/listinfo/python-list
I need some help
hello, I'm completely a newbye, I've started studying python since 3 weeks and now I need to write a small program that reads the id3 tags from the mp3 contained inside a cd, and then print them into a simple text file, I hope it's possible to do this, and I hope you can help me too. thanks alot and sorry for my bad english but I'm italian Dany -- http://mail.python.org/mailman/listinfo/python-list