|
Pollyanna posted:Is there, uh...an explanation on how to use Requests? Say I want to get the text on http://www.alcyone.com/oo/ , like the title and then a list of all the links there. The Requests quickstart guide talks about getting post data and keys and values and such...but no real explanation on "hey get this text". I am not good at HTTP, is this what Requests is good for? Or is it more complicated than that? Requests will get you the file, but you'll need some kind of parser like Beautiful Soup to actually do anything with the file (like grabbing all the links, as in your example).
|
# ? Aug 26, 2013 00:53 |
|
|
# ? May 9, 2024 04:18 |
|
Trying to figure out how to make an n * n matrix in Python and then populate it with random numbers. The user specifies a size (example, 5 makes a 5x5 matrix). I know how to make any matrix, as in:Python code:
Would make code:
|
# ? Aug 26, 2013 00:53 |
|
the posted:Trying to figure out how to make an n * n matrix in Python and then populate it with random numbers. The user specifies a size (example, 5 makes a 5x5 matrix). I know how to make any matrix, as in: I assume Python code:
|
# ? Aug 26, 2013 01:18 |
|
Pollyanna posted:Is there, uh...an explanation on how to use Requests? Say I want to get the text on http://www.alcyone.com/oo/ , like the title and then a list of all the links there. The Requests quickstart guide talks about getting post data and keys and values and such...but no real explanation on "hey get this text". I am not good at HTTP, is this what Requests is good for? Or is it more complicated than that? Python code:
Dren posted:QTimer should be protecting you from a lot of the mess that this code would cause if it were executed in a separate thread because it executes its callback in the event thread. If it didn't and the callback was executing from a different thread, you'd have bad problems if you happened to change parameters while the callback was executing. Dominoes fucked around with this message at 02:09 on Aug 26, 2013 |
# ? Aug 26, 2013 01:26 |
|
Hammerite posted:I assume Thanks, I think I found a better way though: Python code:
|
# ? Aug 26, 2013 01:36 |
|
Dominoes posted:You could use regular expressions to extract the links from the HTML. http://stackoverflow.com/a/1732454/23972 quote:You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the nerves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi͡ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of regex parsers for HTML will instantly transport a programmer's consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection will devour your HTML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fight he com̡e̶s, ̕h̵is un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo͟ur eye͢s̸ ̛l̕ik͏e liquid pain, the song of re̸gular expression parsing will extinguish the voices of mortal man from the sphere I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful the final snuffing of the lies of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL IS LOST the pon̷y he comes he c̶̮omes he comes the ichor permeates all MY FACE MY FACE ᵒh god no NO NOO̼OO NΘ stop the an*̶͑̾̾̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e not rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ (use lxml.html to parse HTML)
|
# ? Aug 26, 2013 01:58 |
|
Dominoes posted:You could use regular expressions to extract the links from the HTML. You are a bad person and you should feel bad
|
# ? Aug 26, 2013 02:02 |
|
lxml is an excellent choice to parse out html, find links and extract text. to wit: code:
|
# ? Aug 26, 2013 02:06 |
|
the posted:Trying to figure out how to make an n * n matrix in Python and then populate it with random numbers. The user specifies a size (example, 5 makes a 5x5 matrix). Use the functions in numpy.random. the posted:But how do I create one without knowing the size? More generally, you can allocate blocks using ones/zeros/np.random.whatever and fill them in element by element or stitch them together using hstack/vstack/dstack/concatenate/kron/etc to build up structured arrays.
|
# ? Aug 26, 2013 02:07 |
|
I thought beautifulsoup was the bestest HTML parser because it does sloppy parsing. That is, it will still work if the document is not well formed (like the tags aren't closed or something). XML parsers choke on stuff like that. Is lxml better than that now?
|
# ? Aug 26, 2013 02:36 |
|
I recently decided to sign up for a few college classes going towards a Programmer/Analyst degree concentration. I have been learning with Python 2. Well I thought I'd be a little step ahead of the class but towards the end of the class we will be using Python 3. So just how screwed up am I going to be trying to switch over. I'm hoping the transition won't be difficult since I'm very new to programming. Right now we are learning with Alicee which doesn't make much sense to me as how that is supposed to help you learn to program especially since you do not coding at all. But, just wanted to know if I need to be prepared for mass confusion going from Python 2 to 3.
|
# ? Aug 26, 2013 12:57 |
|
If you're maintaining an existing project, in which case it's a huge mess because you have to make sure your code doesn't make any incorrect assumptions) but if you're not it's not a big change. The biggest hurdle is the str/bytes thing, since in Python 2 unicode and str objects quack like the same duck whereas in Python 3 bytes objects are their own thing. At this point just about everything worth using is Python 3-compatible (the sticking points are Twisted, Pygame and Pyglet, all of which have bigger problems than Python 3 compatibility).
|
# ? Aug 26, 2013 13:32 |
|
The biggest changes you will notice are that print is a function and that division between two integers will give you a float result (if the result has a fractional part) instead of a truncated int.
|
# ? Aug 26, 2013 13:35 |
|
Dren posted:The biggest changes you will notice are that print is a function and that division between two integers will give you a float result (if the result has a fractional part) instead of a truncated int. After unicode being the default string type, of course.
|
# ? Aug 26, 2013 14:13 |
|
Nippashish posted:More generally, you can allocate blocks using ones/zeros/np.random.whatever and fill them in element by element or stitch them together using hstack/vstack/dstack/concatenate/kron/etc to build up structured arrays. Yes you can but you should try to create all (or as many as you can) of the blocks first, and stitch them all together once, because those operations are quite expensive in terms of reallocations and data copying.
|
# ? Aug 26, 2013 15:43 |
|
Hammerite posted:I assume This might work for numpy, but it's a really bad habit to get into for making matrix-type nested lists, for the following reason: Python code:
Better would be Python code:
|
# ? Aug 26, 2013 16:28 |
|
yaoi prophet posted:I totally write classes that I'll only ever have one instance of at a time, it helps group functionality together. Same with functions that I'll only call once. John Regehr has an excellent article on this: http://blog.regehr.org/archives/942 You should totally create functions that are only used once, if you think it helps. There's no rule that functions have to be called more than once.
|
# ? Aug 26, 2013 16:59 |
|
Hot Dog Day #42 posted:This might work for numpy, but it's a really bad habit to get into for making matrix-type nested lists, for the following reason: Ah, you have a very good point.
|
# ? Aug 26, 2013 18:02 |
|
Note: In the middle of posting this I realized my mistake and fixed it. But I'm going to post it here anyway so you can all laugh at me. gently caress recursion. It's simple but sometimes difficult for me to wrap my brain around a problem with it. Anyway... I'm having the problem with printing a very generic tree structure out to the screen. So far I've got this method that walks the tree and outputs it. But It prints the tree from the bottom up. I need it from top down. I have no idea what is going on. This method is part of a very simple class that contains the tree structure. Each instance contains a list of child nodes. code:
code:
Commence laughter at my expense.
|
# ? Aug 26, 2013 18:16 |
|
Recursion: Not that hard, actually.
|
# ? Aug 26, 2013 19:11 |
|
This might be in left-field/hail mary territory because it is such a niche use, but any suggestion how to open grib2 files using the PythonX,Y framework? I was using python 2.5 before and had a mix of library that worked for what I do (working with a lot of weather model data that is stored in the grib/grib2 format) but decided to try x,y to just have as many tools available as I could. Well, now I'm missing this one small but important piece. I found this but I got the same error indicated in the later comments. Any suggestions (besides reverting back to my 2.5 stuff)?
|
# ? Aug 26, 2013 21:13 |
|
SirPablo posted:This might be in left-field/hail mary territory because it is such a niche use, but any suggestion how to open grib2 files using the PythonX,Y framework? I was using python 2.5 before and had a mix of library that worked for what I do (working with a lot of weather model data that is stored in the grib/grib2 format) but decided to try x,y to just have as many tools available as I could. Well, now I'm missing this one small but important piece. I found this but I got the same error indicated in the later comments. Any suggestions (besides reverting back to my 2.5 stuff)? Have you looked at this? http://scitools.org.uk/iris/docs/latest/index.html
|
# ? Aug 26, 2013 22:25 |
|
Dominoes posted:regular expressions nnnnnnnnoooooooooooooooooooooooooooo I hate those. They're such a pain in the rear end to figure out and type. It's one of those things that you're likely to never be able to write on-the-fly. I'm gonna have to use them eventually though, and I dread that day. Hot Dog Day #42 posted:Requests will get you the file, but you'll need some kind of parser like Beautiful Soup to actually do anything with the file (like grabbing all the links, as in your example). This on the other hand makes it all better. Thanks for the link! oh my god Pollyanna fucked around with this message at 23:52 on Aug 26, 2013 |
# ? Aug 26, 2013 23:45 |
|
Pollyanna posted:nnnnnnnnoooooooooooooooooooooooooooo Try reading the rest of the posts after those two.
|
# ? Aug 26, 2013 23:46 |
|
Thermopyle posted:Try reading the rest of the posts after those two. I got it installed now. BS actually recommends doing so in the documentation, which I just started reading
|
# ? Aug 26, 2013 23:57 |
|
Why is this second line returning an invalid syntax?Python code:
|
# ? Aug 27, 2013 03:08 |
|
the posted:Why is this second line returning an invalid syntax? ** is what Python uses for exponents. ^ is a notation for exponents elsewhere; it's a bitwise operator in Python. You combined them. Dominoes fucked around with this message at 03:18 on Aug 27, 2013 |
# ? Aug 27, 2013 03:14 |
|
I have another noob question. I was doing this exercise at code academy that requires you to calculate the average of student test scores and one line was getting long so I tried to break it like this:code:
code:
EDIT: Found it after googling a bit! I needed to use "\" at the end of the line to continue the line. keyframe fucked around with this message at 03:39 on Aug 27, 2013 |
# ? Aug 27, 2013 03:32 |
|
Put a backslash after lines 1 and 2 of your function. You can cheat and not use it when breaking up something that uses commas, if the break's after a comma. When you get a chance, read Pep 8, for info on how to indent statements split across multiple lines. Dominoes fucked around with this message at 03:48 on Aug 27, 2013 |
# ? Aug 27, 2013 03:39 |
|
keyframe posted:EDIT: Found it after googling a bit! I needed to use "\" at the end of the line to continue the line. You can also use brackets: Python code:
|
# ? Aug 27, 2013 03:45 |
|
Thanks a lot!
|
# ? Aug 27, 2013 03:54 |
|
For most cases when you actually need a regex (not for parsing HTML) it's way easier to use http://txt2re.com/ than to try to develop a regex by hand.
|
# ? Aug 27, 2013 04:58 |
|
BeefofAges posted:For most cases when you actually need a regex (not for parsing HTML) it's way easier to use http://txt2re.com/ than to try to develop a regex by hand. Like manna from the gods. I'm crying now.
|
# ? Aug 27, 2013 05:17 |
|
SirPablo posted:This might be in left-field/hail mary territory because it is such a niche use, but any suggestion how to open grib2 files using the PythonX,Y framework? I was using python 2.5 before and had a mix of library that worked for what I do (working with a lot of weather model data that is stored in the grib/grib2 format) but decided to try x,y to just have as many tools available as I could. Well, now I'm missing this one small but important piece. I found this but I got the same error indicated in the later comments. Any suggestions (besides reverting back to my 2.5 stuff)? I don't know anything about pythonxy, never heard of it until now, but have you looked into using GDAL? I also use lots of weather model data and GDAL is my goto library for reading grib2. It's a C++ library, but it has bindings to many languages including python by using SWIG. With python, you can read each timestep's data as a numpy array. GDAL is very powerful and can reproject data, load and save many file formats through a common API, and is used by many mapping tools under the hood since it has such a large number of standard capabilities. I don't know about its python 2.5 support, I use it with 2.7. code:
|
# ? Aug 27, 2013 05:36 |
|
Gangsta Lean posted:I don't know anything about pythonxy, never heard of it until now, but have you looked into using GDAL? I also use lots of weather model data and GDAL is my goto library for reading grib2. It's a C++ library, but it has bindings to many languages including python by using SWIG. With python, you can read each timestep's data as a numpy array. GDAL is one of the libraries that comes with Pythonxy
|
# ? Aug 27, 2013 07:26 |
|
Gangsta Lean posted:I don't know anything about pythonxy, never heard of it until now, but have you looked into using GDAL? I also use lots of weather model data and GDAL is my goto library for reading grib2. It's a C++ library, but it has bindings to many languages including python by using SWIG. With python, you can read each timestep's data as a numpy array. Yes! This looks like it will work, thank you!
|
# ? Aug 27, 2013 07:50 |
|
This is a weird bug, but I hope someone knows what's going on. I have a module named foo, and a script run_foo.py. When I run the script, I get the error message: code:
code:
code:
|
# ? Aug 27, 2013 17:47 |
|
Another day another question I am trying to write a function that appends a user input number to the end of the list, this is what I have so far: code:
|
# ? Aug 27, 2013 18:26 |
|
code:
|
# ? Aug 27, 2013 18:40 |
|
|
# ? May 9, 2024 04:18 |
|
keyframe posted:Another day another question The .isdigit() is a method that only works on strings, so you have to check it with an individual elements of the list, not on the list itself. For example, you write mylist[0].isdigit() to check if the first element of the list is a digit. However, this will still throw an error if the element of the list you are checking is not a string. If mylist=[1,2,3] and I ask mylist[0].isdigit() then I'll get an AttributeError because mylist[0] is an integer, not a string, so it has no .isdigit() method. If you want to just check that an object is a string you can do either of the following: type(x) == str isinstance(x,str) So you can loop through your list and check each one of them this way (among many other ways) and then raise an error or whatever if you find one that doesn't work. code:
|
# ? Aug 27, 2013 18:45 |