|
JoeNotCharles posted:Nope, should be fine. (Don't actually put the "..." in - I just used that to mark code that I'd cut out.) Are you sure your indentation's correct? From one error to another: Traceback (most recent call last): File "C:\Python25\cpumon.py", line 44, in <module> except pywintypes.error: NameError: name 'pywintypes' is not defined I should probably get around to actually learning Python instead of hacking it together.
|
# ? Feb 2, 2008 01:38 |
|
|
# ? May 11, 2024 09:59 |
|
w_hat posted:From one error to another: Interesting you should say this now - Someone just posted an excellent Python reading list here: http://www.wordaligned.org/articles/essential-python-reading-list
|
# ? Feb 2, 2008 03:35 |
|
JoeNotCharles posted:You might have to import pywintypes, but it would give a different error if that was the problem. w_hat posted:From one error to another: Really, what was unclear about that?
|
# ? Feb 2, 2008 06:21 |
|
As a person who has learned C++ and PHP, how easy is Python to get into? Pros and cons versus PHP? Figured I'd ask in here since it doesn't really warrant its own thread.
|
# ? Feb 2, 2008 10:27 |
|
m0nk3yz posted:Interesting you should say this now - Someone just posted an excellent Python reading list here: http://www.wordaligned.org/articles/essential-python-reading-list Ohh and here I thought I was the only one that had this reaction when browsing the library docs: quote:itertools
|
# ? Feb 2, 2008 12:02 |
|
cLin posted:As a person who has learned C++ and PHP, how easy is Python to get into? Pros and cons versus PHP? Figured I'd ask in here since it doesn't really warrant its own thread. It's very easy to get into, and is generally better than PHP in every way save for not being ubiquitous on shared Web hosting environments like PHP is. I say this as someone who's been paid to write both PHP and Python and is currently paid to do...both My background pre-Python was Java, VB and PHP and I absolutely fell in love with Python for various reasons. Largely it's just very close to my idea of an ideal language - the syntax is incredible, being both clean and very powerful/concise; the library support is great; the philosophy behind it is well thought out for the most part; the community is full of experienced, intelligent and pragmatic coders from every programming background you could imagine; I could go on. Comparing to PHP, one of its largest strengths is that it's generally very consistent and clean, where PHP is one huge ugly namespace with inconsistent function names and/or argument specifications. In addition, PHP's type system is such that it's extremely easy to make mistakes that are difficult to debug (e.g. the implicit cast of a string to an int during some math on line 10 is causing tax to not be applied on line 257, but without any error messages of any sort cropping up) and more explicit languages like Python avoid that almost entirely.
|
# ? Feb 2, 2008 17:26 |
|
JoeNotCharles posted:Really, what was unclear about that? Christ, I totally missed that, thanks.
|
# ? Feb 2, 2008 18:01 |
|
bitprophet posted:It's very easy to get into, and is generally better than PHP in every way save for not being ubiquitous on shared Web hosting environments like PHP is. I say this as someone who's been paid to write both PHP and Python and is currently paid to do...both Wow, sounds like something I'd like to learn. What books do you python developers recommend? Something with examples since I learn best with it. How does a program work anyways? I know for PHP you can just write the code into the php file and execute it (printing hello world in index.php and going to index.php), but I never seen any websites with anything that'll indicate it runs off python code. edit: oh yea, main reason I am interested in it cause I noticed for sites like codegolf.com where programmers from many languages compete for shortest code, python is usually way shorter than php with perl leading as the smallest amount of coding. cLin fucked around with this message at 23:48 on Feb 2, 2008 |
# ? Feb 2, 2008 23:46 |
|
cLin posted:Wow, sounds like something I'd like to learn. What books do you python developers recommend? Something with examples since I learn best with it. How does a program work anyways? I know for PHP you can just write the code into the php file and execute it (printing hello world in index.php and going to index.php), but I never seen any websites with anything that'll indicate it runs off python code. Python has no explicit compilation step like C++, it's interpreted, meaning it's closer to Perl or shell scripting - you typically either write a source file and tell the interpreter to run it, or you just enter the interpreter yourself and execute code in there. It can also be run in a Web server environment like PHP typically is, but most people do that via a Web framework (Django, TurboGears, Pylons, etc) as opposed to random scripts running at e.g. https://www.mysite.com/myscript.py. That's why you never see sites looking like they run on Python - the frameworks mentioned tend to favor clean URLs (e.g. https://www.mysite.com/section/subsection/location/) and not URLs that look like filenames (In such a situation, there isn't technically one script getting called at that URL - it goes to a URL dispatch function which decides what code to execute/what template to render/etc) CodeGolf is kind of silly but does serve as a metric for how concise/powerful a language is. Python generally gets high marks in that department due to a combination of factors. For example, list comprehensions make iterative tasks really easy/simple, like here where I have a list of dicts (associative arrays in PHP) with peoples' names and ages and I want a list of just the ages: code:
code:
code:
code:
code:
code:
Also, while it appears trivial, note Python's lack of semicolons and braces for expression/block delimiters. It saves a lot of typing over time, and is one less thing to be thinking about / screwing up. And no, using indents to determine blocks is not a big deal, despite lots of people saying it turns them off. If you indent your PHP/Java/C++/whatever in a readable fashion, you're already doing it the Python way. bitprophet fucked around with this message at 05:39 on Feb 15, 2008 |
# ? Feb 3, 2008 02:37 |
|
Any books or online tutorials you recommend? I am sort of understanding the difference between the two and would like to learn more
|
# ? Feb 3, 2008 03:21 |
|
cLin: I don't think you can go past the official Python tutorial, at least to start with. The essential reading list linked earlier by m0nk3yz looks good, but I haven't read all of what it suggests, yet. I think that Python is the most beautiful and clean of the languages, but it has one thing that I really hate - double underscores ("dunders") for name mangling. There has to be a prettier way to do that.
|
# ? Feb 3, 2008 03:38 |
|
Thanks guys, links are all bookmarked. I just realized I should start learning javascript as well seeing how the projects I have been working on lately all required some form. But I must learn Python, it looks like a clean language.
|
# ? Feb 3, 2008 05:48 |
|
How do I get started writing tests for things in Django? I've never done testing in code, and my workflow tends to be: 1) Start server 2) Edit views.py or forms.py 3) Refresh browser, test data I think I could do this better. How?
|
# ? Feb 5, 2008 01:56 |
|
Er. Hate to be all RTFM on you but did you look at the documentation? Go to the Django documentation page and it's the 5th line down under the Reference header Granted, comprehensively testing web apps is a bit tougher because of their nature, but you can still get a lot done with a simple set of unit tests testing object creation/editing/deletion, making sure all your URLs result in HTTP 200s and aren't breaking, and putting doctests on any non-builtin model methods you write (e.g. if you do something like get_sum_of_child_numbers(), do a doctest to ensure it works correctly). So on and so forth.
|
# ? Feb 5, 2008 02:34 |
|
bitprophet posted:Er. Hate to be all RTFM on you but did you look at the documentation? Go to the Django documentation page and it's the 5th line down under the Reference header Haha drat I missed it while looking over the documentation page. Thanks.
|
# ? Feb 5, 2008 03:03 |
|
Do you guys know a good validating XML parser that would work on Python 2.5? I can't get PyXML to install, it's asking for 2.4 :/ What I really need is to validate an XML created with minidom in either DOM object, string of file format, I don't care which. e: I found an unofficial 2.5 release, but I'm not sure if I trust it enough to use it on the server Cowcatcher fucked around with this message at 20:17 on Feb 6, 2008 |
# ? Feb 6, 2008 19:57 |
|
PyRXP is probably your best choice.
|
# ? Feb 6, 2008 20:46 |
|
deimos posted:PyRXP is probably your best choice. It looks great, but there doesn't seem to be any xsd schema support, it just uses dtd for validating.
|
# ? Feb 6, 2008 21:32 |
|
Perhaps we should make a Django megathread, but I thought I'd post this little ditty in here anyway as it can be used outside of Django. So, for anyone who has played ToME or other roguelikes, you know there are a ton of different monsters, and knowing their characteristics gives you a leg up in the fight. Well, there used to be a ToME monster search page here, but it seems to have gone away. So I wrote a little parser that grabbed that info and put it into a db for a Django web app so I could search on certain criteria looking for monster info. Well, when I first started building the search page it looked a lot like this: code:
code:
For those a little lost, here's how it (the for loop portion) works:
I'm not a huge fan of the whole <fieldname>__<comparison> syntax that Django uses, but in this case it proved extremely useful.
|
# ? Feb 8, 2008 17:36 |
|
That's a cool solution! I also had a similar problem where we were filtering issues in a bug tracking system and at first also has a big if tree, but then I refactored it into this bit of code: code:
The magic is in the last line, which takes the querystring, turns it into a dict and uses it as arguments for the filter method. So you can do any kind of filtering you want off the bat, for example: ?page=1&issue_type__slug=bug&issue_status__slug=accepted&completed=no
|
# ? Feb 8, 2008 22:10 |
|
Also, does anyone know how to switch databases on the fly in Django? Or if that's even possible. I need that because when unit testing, I supply a JSON fixture that the tests use as sample data. When the tests are run, a temporary database is created and used and destroyed after the tests are done. But I'd like to pull some data from the regular database in the tests. That's because we've moved some setting values from settings.py (in order not to clutter it up too much) to the database and it would be cool to get those settings in the tests because they reference folders on your machine and I want the tests to be portable across machines.
|
# ? Feb 8, 2008 22:15 |
|
deep square leg posted:cLin: I don't think you can go past the official Python tutorial, at least to start with. The essential reading list linked earlier by m0nk3yz looks good, but I haven't read all of what it suggests, yet. Yeah I think everyone hates that part. Its like you take a beautiful language and lay a gigantic turd into the middle of all that beauty. But....... Guido knows best. I'm sure he's sitting on his magical throne watching this and saying "Ah Ducks, you'll understand it one day <insert monty python joke>"
|
# ? Feb 10, 2008 03:45 |
|
Webware for Python is in beta for 1.0 FINALLY This is a great stack for more traditional Tomcat style servelet style development. I love it. Its got a PSP implementation in there (Think PHP or ASP but with python) , a full persistant servlett stack, Kid templates , authentication stacks, OODB abstaction, you name it. Compared with Django and the like , this bad boys a grandfather of the scene, its been that long brewing. http://www.webwareforpython.org/ Honestly after using this for servlett apps, just mentioning Tomcat gives me the heebee-jeebees. Its just nicer with python.
|
# ? Feb 10, 2008 17:13 |
|
I don't get the hate for double underscores at all. I think they look neat. And it's got precedent in magic macros like __FILE__ and __LINE__ in .
|
# ? Feb 10, 2008 17:58 |
|
On the threading module page, why is it code:
code:
|
# ? Feb 10, 2008 18:12 |
|
Because they're trying to save an extra line. Both the object specific "value" (self.value) and the global variable "value" are affected, and set to "self.value + 1".
|
# ? Feb 10, 2008 19:07 |
|
epswing posted:On the threading module page, why is it Because he's updating the global value at the same time, seems like a convoluted way of doing it since he's returning it too. ynef posted:Because they're trying to save an extra line. Both the object specific "value" (self.value) and the global variable "value" are affected, and set to "self.value + 1". Yeah but he's returning and assigning to value anyways outside the critical section. You might as well return self.value. deimos fucked around with this message at 19:33 on Feb 10, 2008 |
# ? Feb 10, 2008 19:29 |
|
Ok so maybe I'm going blind but I don't see any global variables. I see two class definitions, a counter object and a for loop.
|
# ? Feb 10, 2008 20:40 |
|
epswing posted:Ok so maybe I'm going blind but I don't see any global variables. I see two class definitions, a counter object and a for loop. See that variable called "value"? That's global: the very same one is used in both classes. Python can be confusing that way, since the variable doesn't need to be declared to be global in a special way.
|
# ? Feb 10, 2008 20:51 |
|
Ok, let me ask this, if code:
code:
code:
code:
|
# ? Feb 10, 2008 21:23 |
|
epswing posted:Ok, let me ask this, if I am pretty sure it might be a remnant from the evolution of the example, ie. a previous version might have just used the global value without the return assignment.
|
# ? Feb 10, 2008 22:18 |
|
I haven't take the time to analyze the whole thing, but the difference is that in the version as written, the global value is updated while inside the critical section, and then again after it returns. That could be important if two threads try to access it at the same time.
|
# ? Feb 10, 2008 22:19 |
|
Yesterday Reddit pointed me to "How not to write Python code" and one of the things that caught my eye was the following:quote:Use Pythonesque coding pattern I am pretty new to this Python thing and it made me realize that I do not know much about coding in a manner consistent with other Python programmers. Could anyone point out examples of common Python techniques ("this is the way we do switch")?
|
# ? Feb 11, 2008 16:43 |
|
SmirkingJack posted:Yesterday Reddit pointed me to "How not to write Python code" and one of the things that caught my eye was the following: We do list comprehensions instead of accumulating lists in for loops. Bad: code:
code:
edit: also note that you can filter in list comprehensions (though the filter function also works in some cases) code:
|
# ? Feb 11, 2008 17:19 |
|
How exactly is that variable global? Can someone please explain to me? I would have thought `value` is not put in the global namespace when doingcode:
|
# ? Feb 11, 2008 19:38 |
|
This might warrant it's own thread but, I'm having problems with both Python24 and 25 installed on my Mac. I don't particularly remember installing 25 but, I guess it might have come from my Leopard upgrade. I don't honestly care much which version I am using but, my problem comes from the fact that any type of library or module install I am doing is defaulting into the 2.5 installation directory but, all my scripts are executed by 2.4.4. Can anyone help me sort out this all? Should I remove one installation (24 or 25) or how can I at least just get easy_install etc, to use one installation (preferably 24, since I already have a bunch of packages installed there) or migrate my packages over? I'm a bit dumbfounded right now
|
# ? Feb 11, 2008 19:44 |
|
Bonus posted:How exactly is that variable global? Can someone please explain to me? I would have thought `value` is not put in the global namespace when doing If it's declared/initialized in the global namespace, it can be accessed/modified by any method. If it's not, then yes it's local to that method. What you may not have seen is: code:
edit: durr No Safe Word fucked around with this message at 21:11 on Feb 11, 2008 |
# ? Feb 11, 2008 20:49 |
|
Bonus is right. To modify a global in a local namespace you need to use the global statement.
|
# ? Feb 11, 2008 20:55 |
|
No Safe Word: Umm, could you perhaps provide a working example of how that works? Here's what my interpreter says: code:
code:
|
# ? Feb 11, 2008 21:00 |
|
|
# ? May 11, 2024 09:59 |
|
Bonus posted:No Safe Word: Umm, could you perhaps provide a working example of how that works? value and you would indeed be shadowing the global value, but you can access the value that's in the global scope without the global keyword (you just can't modify it and expect the changes to propagate out of scope). code:
|
# ? Feb 11, 2008 21:10 |