After I run my Python code on a big file of only HTTP headers, it gives me the above error. Any idea what that means?
Here is a piece of the code:
users = output.split(' ')[1]
accesses = output.split(' ')[3]
ave_accesses = int(accesses)/int(users)
Basically the 'users' are users who have accessed a website and 'accesses' are the total number of accesses by the users to that site. The 'ave_accesses' gives the number of accesses to that site by an average user. I hope this is enough to clear things, if not I can explain more.
thanks a lot, Adia.
It means that you are trying to convert a string to an integer, and the value of the string is 'MSIE'. The traceback will have a filename near this error and the line number (e.g., /my/module.py:123). Open the file and go to the line the error occurred, you should see a call to int() with a parameter. That parameter is probably supposed to be a number in string form, but it's not. You probably got your parsing code a little wrong, and fields were mixed up.
To track down the problem, use print statements around the code to see what is not working as expected. You can also use pdb.
I think, your header output is garbled. It is obviously looking for a number where it is find an string MSIE (which may be the value for User-Agent).
Related
I am trying to make a revision material for classmates, and I have a list of terms for OCR Computer Science, in a dictionary named "my_dict". However, if a term is entered incorrectly, it just sends an error message to the Python shell. If anyone can help me add an error message to the code provided, that would be much appreciated.
I have tried basic if, while, next loops, but to no avail.
def button_click():
typed_text = (entry1.get()).lower()
output_text.delete(0.0, END)
if typed_text is in my_dict{}:
meaning = my_dict[(typed_text)]
else:
meaning = str("Are you sure you entered the term correctly?"
output_text.insert(END, meaning)
I expect the output to fill the output box with the error message "Are you sure you entered the term correctly?", but the actual output is an invalid syntax currently.
my_dict{} is not a valid syntax, you just have to pass my_dict for the in expression. is in is also not valid syntax, so just use in
In the str("Are you sure you entered the term correctly?" you are missing a paranthese at the end.
There are some spots where you don't need extra parantheses. Ditch them in (entry1.get()) and [(typed_text)]
I can't really make any sense of this error. When I try to commit a change to user.picture, it sends the error written in the title. I haven't been able to find any other example of this error occurring in anything related to this.
Here's the error:
not all arguments converted during string formatting on line 429
Line 429 reads like so:
db.session.commit()
Here's my store:
store = FileSystemStore(
path='bin/static/img',
base_url='/'
)
If I use a store like this instead, I get the same error:
store = HttpExposedFileSystemStore(
path='bin/static/img/',
prefix='static/img/'
)
Here's an example of me attempting to commit a given picture
with store_context(main.AppClass.store):
current_user.picture.from_file(_profile_picture)
db.session.commit()
Here's an example of me trying to do the same with binary instead
with store_context(main.AppClass.store):
byte_str = base64.b64encode(_profile_picture.read())
decoded_byte_str = base64.b64decode(byte_str)
current_user.picture.from_blob(decoded_byte_str)
db.session.commit()
They both give me the same error, which leads me to believe there may be something wrong with my Store, but I can't really begin to comprehend how.
However, if I do something like this instead as a test to see if it works at all:
with store_context(main.AppClass.store):
current_user.set_name("asd")
db.session.commit()
It commits with no issue. I'm sort of at a loss here, any help is appreciated.
Warning: I'm a total newbie; apologies if I didn't search for the right thing before submitting this question. I found lots on how to ignore errors, but nothing quite like what I'm trying to do here.
I have a simple script that I'm using to grab data off a database, parse some fields apart, and re-write the parsed values back to the database. Multiple users are submitting to the database according to a delimited template, but there is some degree of non-compliance, meaning sometimes the string won't contain all/any delimiters. My script needs to be able to handle those instances by throwing them out entirely.
I'm having trouble throwing out non-compliant strings, rather than just ignoring the errors they raise. When I've tried try-except-pass, I've ended up getting errors when my script attempts to append parsed values into the array I'm ultimately writing back to the db.
Originally, my script said:
def parse_comments(comments):
parts = comments.split("||")
if len(parts) < 20:
raise ValueError("Comment didn't have enough || delimiters")
return Result._make([parts[i].strip() for i in xrange(2, 21, 3)])
Fully compliant uploads would append Result to an array and write back to db.
I've tried try/except:
def parse_comments(comments):
parts = comments.split("||")
try:
Thing._make([parts[i].strip() for i in xrange(2, 21, 3)])
except:
pass
return Thing
But I end up getting an error when I try and append the parsed values to an array -- specifically TypeError: 'type' object has no attribute 'getitem'
I've also tried:
def parse_comments(comments):
parts = comments.split("||")
if len(parts) >= 20:
Thing._make([parts[i].strip() for i in xrange(2, 21, 3)])
else:
pass
return Thing
but to no avail.
tl;dr: I need to parse stuff and append parsed items. If a string can't be parsed how I want it, I want my code to ignore that string entirely and move on.
But I end up getting an error when I try and append the parsed values to an array -- specifically TypeError: 'type' object has no attribute 'getitem'
Because Thing means the Thing class itself, not an instance of that class.
You need to think more clearly about what you want to return when the data is invalid. It may be the case that you can't return anything directly usable here, so that the calling code has to explicitly check.
I am not sure I understand everything you want to do. But I think you are not catching the error at the right place. You said yourself that it arose when you wanted to append the value to an array. So maybe you should do:
try:
# append the parsed values to an array
except TypeError:
pass
You should give the exception type to catch after except, otherwise it will catch any exception, even a user's CTRL+C which raise a KeyboardInterrupt.
I have a function called getHTML(URL) which returns a 1-tuple containing a string of the words on that web page. If I simply call getHTML in the shell directly, it works fine. However, when I try to call getHTML in another function, it returns a tuple containing a blank string. Here's the code:
def exampleFunction():
return getHTML(input('Enter a URL: '))
Now in the shell, if I type exampleFunction() and enter a URL (string) when prompted, it simply returns:
('')
a blank string in a tuple. I have no idea why this isn't working. I'm also quite new to Python so please excuse me if this is a result of a trivial error I've made.
Also, the getHTML function code was given to us by the professor and it is quite long and complicated. It's guaranteed to be error-free, so I haven't posted it because I don't see the need to. Again, it works fine when I simply call it directly in the shell.
EDIT:
Here's an example of the output when I call it in the shell. First line is input, second is output:
getHTML('http://networks.cs.northwestern.edu/EECS110-s13/projects/project2/page3.htm')
('\n\n\n\n\n\n\n\n\penguins, penguins, penguins! this webpage has penguins. page 1 page 2\n\n\n\n\n\n\n\n\')
Again, the code he gave us is massive and if I posted it here no one would read it. I did check, however, and he is using return rather than print.
Also, tell me if you can't access that URL and I'll try another.
I'm trying to run some queries against Pubmed's Eutils service. If I run them on the website I get a certain number of records returned, in this case 13126 (link to pubmed).
A while ago I bodged together a python script to build a query to do much the same thing, and the resultant url returns the same number of hits (link to Eutils result).
Of course, not having any formal programming background, it was all a bit cludgy, so I'm trying to do the same thing using Biopython. I think the following code should do the same thing, but it returns a greater number of hits, 23303.
from Bio import Entrez
Entrez.email = "A.N.Other#example.com"
handle = Entrez.esearch(db="pubmed", term="stem+cell[All Fields]",datetype="pdat", mindate="2012", maxdate="2012")
record = Entrez.read(handle)
print(record["Count"])
I'm fairly sure it's just down to some subtlety in how the url is being generated, but I can't work out how to see what url is being generated by Biopython. Can anyone give me some pointers?
Thanks!
EDIT:
It's something to do with how the url is being generated, as I can get back the original number of hits by modifying the code to include double quotes around the search term, thus:
handle = Entrez.esearch(db='pubmed', term='"stem+cell"[ALL]', datetype='pdat', mindate='2012', maxdate='2012')
I'm still interested in knowing what url is being generated by Biopython as it'll help me work out how i have to structure the search term for when i want to do more complicated searches.
handle = Entrez.esearch(db="pubmed", term="stem+cell[All Fields]",datetype="pdat", mindate="2012", maxdate="2012")
print(handle.url)
You've solved this already (Entrez likes explicit double quoting round combined search terms), but currently the URL generated is not exposed via the API. The simplest trick would be to edit the Bio/Entrez/__init__.py file to add a print statement inside the _open function.
Update: Recent versions of Biopython now save the URL as an attribute of the returned handle, i.e. in this example try doing print(handle.url)