|
Anti-recommending web.py here. If you want a minimalist framework, use werkzeug. It's more flexible if you ever need the flexibility, instead of caging you in if you ever try to do something beyond what the standard thing provides. Also, use python 2.6 and not 3.0. As far as I know, there's no (decent) web frameworks ported to 3.x yet. There's discussions of why 2.x over 3.x earlier in this thread as well as in the "python 3.0 released" thread. And the sooner you learn about the MVC paradigm, the better. Jamming all of your application logic into your templates is so bad.
|
# ? May 6, 2009 08:20 |
|
|
# ? May 9, 2024 11:53 |
|
A headscratcher here: I registered and uploaded a new package to PyPi and only subsequently noticed that my documentation (a 'docs' dir in the top level of a setuptools enabled project) hadn't been included in the distribution. I'm sure this used to work, but setuptools doesn't want to include anything that isn't a strict Python package in a distribution. Weird. The setuptools documentation makes an aside that docs will be including in a generated source distribution, but that's not the case. So what gives? Sure I could manually tar the file and upload it but that defeats the point of 'python setup.py sdist upload'.
|
# ? May 6, 2009 21:16 |
What is the elegant/correct way to write a function that will iterate across both a list and a dictionary correctly. I'd like to say... code:
code:
|
|
# ? May 7, 2009 00:44 |
|
Jo posted:What is the elegant/correct way to write a function that will iterate across both a list and a dictionary correctly. You know about dict.itervalues(), right?
|
# ? May 7, 2009 00:58 |
Scaevolus posted:You know about dict.itervalues(), right? I do now. Still, that means when calling the function I'd have to say code:
EDIT: For more clarity, I'm curious about accessing in a very duck-typed object friendly style the iterators for lists and dictionaries. If this isn't possible, it's not a big deal. Just want to know if I'm missing something obvious. Jo fucked around with this message at 01:14 on May 7, 2009 |
|
# ? May 7, 2009 01:09 |
|
Habnabit, tbradshaw, deimos posted:Advice about getting into python as a web language. I really appreciate everyone's input. I wanted to start with 3.0 because I didn't want to teach myself syntactical things like the print statement vs. function bit twice - it would annoy the bejesus out of me. Thankfully, because of your input, I looked harder at 2.6 and saw that most common hangups from 3 are available in 2.6 (which is by design, I know). So I've dropped down to 2.6, which has now opened up a whole world of possibility for me. The only thing that you guys haven't answered for me, and I am hoping someone can point me in the right direction, is that I'd really like to avoid the "handles all paths" style of web application. I think it's absolutely asinine that I should have to configure apache to lock a webapp down into a subdirectory. What I really long for is the PHP style of being able to drop a PHP file anywhere and have ONLY that php file be callable. If I want parameters, I have to pass them with ?. Is this possible? EDIT: What I guess I am looking for is possibly a templating framework for Python? I am looking at them right now, but any pointers would be great. ShizCakes fucked around with this message at 01:22 on May 7, 2009 |
# ? May 7, 2009 01:18 |
ShizCakes posted:The only thing that you guys haven't answered for me, and I am hoping someone can point me in the right direction, is that I'd really like to avoid the "handles all paths" style of web application. I think it's absolutely asinine that I should have to configure apache to lock a webapp down into a subdirectory. What I really long for is the PHP style of being able to drop a PHP file anywhere and have ONLY that php file be callable. If I want parameters, I have to pass them with ?. Is this possible? I know that web.py provides the ability to use templates and gather parameters passed either in the form of urls http://www.omgitswebsite.com/this/is/aSetOfValues through clever use of url handlers OR http://www.omgitswebsite.com/this?is=aSetOfValues through web.input( is ). However, web.py won't do the folder/file specific sort of thing you want. You can't just drop python code into a page like PHP. Edit: Something of a hack in web.py to do something close. In the $webroot/templates/default.html: code:
code:
Jo fucked around with this message at 01:40 on May 7, 2009 |
|
# ? May 7, 2009 01:32 |
|
Jo posted:I do now. code:
|
# ? May 7, 2009 01:33 |
Scaevolus posted:What about this? Had something like that before the edit. I didn't think that one could do `for i in iterator` since an iterator does not itself return items, just moves through the items. To be more clear: `for i in thisThing:` `i` is an iterator. `in thisThing` returns an iterator Or I could be out of my mind. EDIT: Works! o>-< Jo fucked around with this message at 01:47 on May 7, 2009 |
|
# ? May 7, 2009 01:43 |
|
Jo posted:`for i in thisThing:` No code:
|
# ? May 7, 2009 02:55 |
|
hlfrk414 posted:(...) The loop will catch a StopIteration exception at the end and then it knows to finish the loop(...)
|
# ? May 7, 2009 03:55 |
|
PrBacterio posted:Ughh... is it just me or is this REALLY ugly? I usually really like the way things work in Python but using exceptions as a regular control flow mechanism? Really? How would you propose they do it? Java's iterators have a hasNext method for all iterators, but since there is no guarantee that it will be called before next, so next calls it as well, breaking the DRY principle. Plus it implies all iterators will know if it has something next before it tries to get something next. You should not use UNRELATED exceptions for regular flow control. That is why using IndexOutOfBoundsExceptions to know when to finish a loop is a horror, because it doesn't just mean that we finished looping. The StopInteration exception is ONLY used to stop a loop. It does not mean anything else. It allows a simple way to combine both getting an element and stopping iteration, since they are itimately tied anyways. Plus, it is an error to try to continue iteration past the end of a sequence anyways, so you'll need to throw an exception.
|
# ? May 7, 2009 04:09 |
|
Jo posted:I do now. The point of "duck typing" is to treat an object according only to its methods, not to its actual type. Checking types explicitly is just about the opposite of what you ought to do. The best way would be to have two functions, one for mappings (dicts, maps) and one for generic iterables (lists, sets, etc). If for some reason you're dead-set on handling unrelated interfaces in the same function, do something like this: code:
|
# ? May 7, 2009 05:14 |
Janin posted:The point of "duck typing" is to treat an object according only to its methods, not to its actual type. Checking types explicitly is just about the opposite of what you ought to do. Exactly my point. I wanted to avoid explicitly doing an isinstance( thing, dict ) to keep it up to the duct-typing standard. Just curious about whether there was a method across both the list and dict(values). Thank you very much for your help.
|
|
# ? May 7, 2009 06:15 |
|
PrBacterio posted:Ughh... is it just me or is this REALLY ugly? I usually really like the way things work in Python but using exceptions as a regular control flow mechanism? Really? it may not be "pythonic", although i couldn't say. personally, in the only real python app i've written, i use it for setting a booleans which then affects program flow, which isn't really directly controlling flow.
|
# ? May 7, 2009 08:12 |
|
PrBacterio posted:Ughh... is it just me or is this REALLY ugly? I usually really like the way things work in Python but using exceptions as a regular control flow mechanism? Really? I had the same reaction when that idiom was introduced. But I think this case gets a pass because the raise/catch is hidden. The warning about using exceptions for flow control is due to it obscuring the flow of execution but here (1) it's not obvious that exceptions are being used, that's just the implementation (2) the flow is obvious.
|
# ? May 7, 2009 11:10 |
|
Jo posted:What is the elegant/correct way to write a function that will iterate across both a list and a dictionary correctly. I'd avoid this situation entirely. Why wouldn't you know whether you have a list or a dict? Sometimes trying to be too flexible results in more fragile code. It's also easy enough to just pass d.itervalues() to the function in the first place and make the function take an iterable. ShizCakes posted:The only thing that you guys haven't answered for me, and I am hoping someone can point me in the right direction, is that I'd really like to avoid the "handles all paths" style of web application. I think it's absolutely asinine that I should have to configure apache to lock a webapp down into a subdirectory. What I really long for is the PHP style of being able to drop a PHP file anywhere and have ONLY that php file be callable. If I want parameters, I have to pass them with ?. Is this possible? Really, why is it that you're so against doing it the (typical) python way? Idioms don't always translate cleanly across languages, and this is definitely one of those things. It's not like you have to write code for each template and each static file. Your web framework will take care of dispatching for you. Static files will be automatically served and adding actions to controllers is really simple if you're using a decent framework. Here's some real live code from a python web application I've been working on! code:
|
# ? May 7, 2009 11:15 |
|
ShizCakes posted:What I really long for is the PHP style of being able to drop a PHP file anywhere and have ONLY that php file be callable. If I want parameters, I have to pass them with ?. Is this possible? Not to be pedantic, but that "style" of development really sucks. Even major php applications drop the "multiple entry points, multiple exit points" style and do something much more similar to what you're seeing with python web development. (Which is really just solid MVC style architecture.) As Habnabit suggests, really, you just want to embrace it. You're not going to find an elegant solution to do things like PHP (or old ASP) where you just have templates and put your program logic in them when needed because it's a very bad development practice and not even advanced PHP developers use it. As far as templating packages, I would humbly suggest Genshi. It's not the fastest, but it's extremely good at getting things Right.
|
# ? May 7, 2009 15:48 |
|
I've been doing project euler in python and I can't wrap my brain around this problem. http://projecteuler.net/index.php?section=problems&id=31 I know it can be recursive somehow but I just can't get it. Can anyone explain a simple version of it using a smaller set perhaps?
|
# ? May 7, 2009 18:53 |
|
Also, are there any 'better' versions of the python IDLE/console that can handle writing very large lists and such without becoming very non-responsive/crashing.
|
# ? May 7, 2009 23:06 |
|
Sylink posted:Also, are there any 'better' versions of the python IDLE/console that can handle writing very large lists and such without becoming very non-responsive/crashing. I've pasted megabyte-sized lists into the regular python console without a problem. But have you tried IPython?
|
# ? May 8, 2009 00:06 |
|
I have tried it, didn't really like it. I'm on windows and the standard interactive mode seems to poo poo itself if I have lists with tens of thousands of entries which seems trivial given how little memory it takes up and on modern processors. EDIT: Ok I just tried it from MSDOS prompt/cmd.exe and its a lot smoother. Must be the GUI thing taking a poo poo. Another question, on Windows, is there a way to open a command line in a directory then run a .py and have it end in interactive mode so I can check variables and whatnot for testing? EDIT2:I got it, had to set path variables and what not, python -i etc. Sylink fucked around with this message at 01:37 on May 8, 2009 |
# ? May 8, 2009 01:04 |
|
xposting from short questions that dont deserve their thread: I'm in a basic mathematical computer science course and finishing up a project on sorting (bubble sort, better bubble, quick sort). We were given pseudocode that would work perfectly when translated, however it doesn't seem to. We're supposed to keep the sort algorithms in a seperate module and call them from the first module's main function. http://www.chicagoburning.com/mp4/sort.py Can anyone tell me what I'm doing wrong? Its not sorting the list I have set up to test it in the main function. The other files in the directory are the main program and the explanation. I'm almost positive the main program works 100% and my hiccup is just the sorting.
|
# ? May 8, 2009 13:09 |
|
Not an Anthem posted:xposting from short questions that dont deserve their thread: When you call bub_sort1(L,len(L)) its working on a copy of your data and not returning anything. If you want to have a function gently caress around with global variables then declare them outside of the main function block. Alternatively, have the bub_sort function return a new sorted version of the list that was given to it; if you give it a list of tuples to sort for example, it doesn't make sense to try and shuffle them in place because tuples are immutable. Far better for you to rewrite that using code that would return a new sorted sequence matching whatever sequence type was given to it. tripwire fucked around with this message at 16:47 on May 8, 2009 |
# ? May 8, 2009 16:45 |
|
We're just using the sort functions to time how long it takes and comparing their big oh (bubsort is n^2, qsort is nlogn), we dont need the sorted lists.
|
# ? May 8, 2009 17:19 |
|
Returning the list is arguably part of the time, but also add the time module so you can time it.code:
|
# ? May 8, 2009 17:33 |
|
Quick question: I'm working on some kernel stuff for AndroidOS for the summer, and the git checkout requires this Python wrapper program. When I try to run it, I get code:
|
# ? May 8, 2009 19:23 |
|
Dijkstracula posted:I'm working on some kernel stuff for AndroidOS for the summer, and the git checkout requires this Python wrapper program. When I try to run it, I get ... Don't see anything to do with numpy there. Could it be that you are on a Mac and the script is trying to use Windows functions? That looks a bit weird there. The readline on OSX can be problematic. Failing anything else, reinstalling that may be fruitful.
|
# ? May 8, 2009 20:52 |
|
Dijkstracula posted:Quick question: Remove your pyreadline, it's a windows-only module. From http://www.google.com/search?hl=en&...Search&aq=f&oq=
|
# ? May 8, 2009 21:02 |
|
Sylink posted:Returning the list is arguably part of the time, but also add the time module so you can time it. Or better yet, use the "profile" module... code:
|
# ? May 8, 2009 21:42 |
|
Thanks for the responses.m0nk3yz posted:Remove your pyreadline, it's a windows-only module. From http://www.google.com/search?hl=en&...Search&aq=f&oq=
|
# ? May 8, 2009 22:07 |
|
Hey I didn't know about the profile module, thanks.
|
# ? May 8, 2009 22:58 |
|
Can someone explain parsing with elementtree to me, it makes no sense and the effbot stuff seems to all be about making xml, I just want to get information. Like if I have an element <title>, how do I get the actual content between the tags?
|
# ? May 9, 2009 10:09 |
|
Sylink posted:Can someone explain parsing with elementtree to me, it makes no sense and the effbot stuff seems to all be about making xml, I just want to get information. I've just done this for my university project. code:
XML would be so much easier to parse if you could just parse the XML in and access it this way: code:
|
# ? May 9, 2009 10:50 |
|
Sylink posted:Can someone explain parsing with elementtree to me, it makes no sense and the effbot stuff seems to all be about making xml, I just want to get information. I have no experience with elementtree, but you may want to look at BeautifulSoup.
|
# ? May 9, 2009 16:29 |
|
Centipeed posted:But this'll only work if your "title" elements are direct sub-elements of your root element, I believe. You focused on the wrong part of the example. The important part of that example is the ".text" property that retrieves the contained text just as you want it. Find it however you want. Centipeed posted:XML would be so much easier to parse if you could just parse the XML in and access it this way: That breaks because nested tags are as equally important as the text. Most importantly, in the base case tags hold other tags and *not* text. But, it's true, it would be easier for you if they ignored the breadth of XML and just focused on the small part that you're using. ;D
|
# ? May 9, 2009 16:38 |
|
tbradshaw posted:That breaks because nested tags are as equally important as the text. Most importantly, in the base case tags hold other tags and *not* text. But, it's true, it would be easier for you if they ignored the breadth of XML and just focused on the small part that you're using. ;D Is it wrong of me to think that "root.element1.element2" is more elegant than "root.find("element1").find("element2").text"? Obviously they've done it the second way because it wouldn't work my way, as you say. I just wish it were more intuitive.
|
# ? May 9, 2009 17:39 |
|
Centipeed posted:Is it wrong of me to think that "root.element1.element2" is more elegant than "root.find("element1").find("element2").text"? Obviously they've done it the second way because it wouldn't work my way, as you say. I just wish it were more intuitive. Warning: I don't know python, I'm in this thread to learn about it. Seems like you could write code for something like this: root.findByString("element1.element2.element3") or at least a function like: findElementByString(root, "element1.element2.element3") The function body is: (horrible pseudopseudocode follows) code:
taqueso fucked around with this message at 18:03 on May 9, 2009 |
# ? May 9, 2009 18:00 |
|
Centipeed posted:Is it wrong of me to think that "root.element1.element2" is more elegant than "root.find("element1").find("element2").text"? Obviously they've done it the second way because it wouldn't work my way, as you say. I just wish it were more intuitive. A wrapper for this was actually posted yesterday on http://planet.python.org , maybe they read this thread. quote:As you know, ElementTree's interface is boring and most time you don't need all these findalls and attribs. 80% of code requires simple operation like: "access to child B and get it's attribute C". So, why to type root.find('B').attrib['C'], when you can use simple construction like root.B.C? To accomplish this task, I wrote a simple wrapper for ElementTree. code:
|
# ? May 9, 2009 18:25 |
|
|
# ? May 9, 2024 11:53 |
|
It just seems like every time I want to parse xml its completely non-intuitive and voodoo. Not to mention they don't have practical examples most of the time it seems.
|
# ? May 9, 2009 18:48 |