|
deimos posted:You can't use type(obj)? Really you want to use isinstance (sometimes in conjunction with type), because that handles inheritance better and is just in general more robust. code:
|
# ¿ Nov 6, 2007 19:45 |
|
|
# ¿ Apr 27, 2024 20:28 |
|
Digital Spaghetti posted:Ohh, very handy this thread has come up. I've been looking at Django for a while, and I'm thinking of switching my project from CakePHP to Django - but I must admit I can't find any "nice" tutorials to get me started - Apart from the Django site and book, can anyone recommend any good resources for it for a beginner at Python? http://code.djangoproject.com/wiki/DjangoResources - this has tons of other links that are more specific http://blixtra.org/blog/2006/07/17/top-30-django-tutorials-and-articles/ - this is actually on that link but has other unique links
|
# ¿ Nov 12, 2007 14:58 |
|
SmirkingJack posted:Is this book you speak of The Definitive Guide to Django: Web Development Done Right or a different one? Looks like it, based on the publication details (author and publisher). I was hoping they'd go with the same color scheme they use on the website though
|
# ¿ Nov 16, 2007 15:42 |
|
For anyone using Django who doesn't regularly read James Bennett's blog, he recently put up a good three-part post on the new forms stuff (which has been in the SVN for a while but isn't in any release yet). Though if you're a Django user, I'd suggest reading his blog regularly along with Malcolm Tredinnick's blog (though it's less Django-dense) No Safe Word fucked around with this message at 01:09 on Nov 27, 2007 |
# ¿ Nov 26, 2007 19:46 |
|
m0nk3yz posted:If you want an excellent newforms walkthrough, check out the intro to newforms posts on James Bennett's blog: No Safe Word at the top of this page posted:For anyone using Django who doesn't regularly read James Bennett's blog, he recently put up a good three-part post on the new forms stuff (which has been in the SVN for a while but isn't in any release yet).
|
# ¿ Nov 29, 2007 18:39 |
|
Put quotes that will hack around it?code:
|
# ¿ Dec 6, 2007 21:33 |
|
I actually half-expected the book release to coincide with the Django 1.0 release, but I have no idea how far away the 1.0 release is, so that may not have been feasible. And in fact, I'm guessing it's probably a little ways away because it'd be a bad idea to release a book and then immediately obsolete it (granted, it'd still be valid for the 0.96 release, but there's a lot of new cool useful stuff in the 1.0 branch).
|
# ¿ Dec 17, 2007 18:05 |
|
Not to derail but the latest stuff I've been doing at work has been possible thanks to the help of some Wrox books And for Python-related stuff to get somewhat back on track: here's this!
|
# ¿ Dec 17, 2007 22:25 |
|
To get this back onto the front page where it belongs and to sort of continue the topic at hand, what do people think of the Python ORMs that are out there? I'm mainly a user of the one built into Django, and I think it's pretty awesome (with a few warts but what doesn't have those?). I know some live and die by SQLAlchemy and there are a few others out there which are pretty heavily used as well. Anybody a whiz with several who wants to provide a good comparison of them? I will say that when I was doing some ASP.NET (ugh) training last week, the instructor was all high on LINQ and talking about how in .NET 3.5 they had just introduced all this cool stuff and wow delayed execution and chained querysets and ooh so neat, which really blew the mind of some of the other folks in the training but I was like "yeah, some journalism major wrote the same thing for Django" (I don't know that Adrian actually wrote that part, but it sounds funnier ... though I didn't actually say it). Not to say LINQ isn't pretty cool (it is), but I just forget how cool some of the stuff we've already got in our pet frameworks/languages already is.
|
# ¿ Jan 7, 2008 23:41 |
|
From Simon Willison's blog, a public registry of Django users we Django folks should go register at.
|
# ¿ Jan 23, 2008 03:47 |
|
Since we've got a bit of a Django thing going on, what's the best idea for how to structure templates within a project and its apps? I've been kind of using this structure, but it conflates project and app when I think they ought to be logically separate:code:
|
# ¿ Jan 24, 2008 00:52 |
|
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 |
|
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 |
|
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 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 |
|
deimos posted:Hey I was wondering, what way do you guys use of getting a class from a string? I've been doing it in a fairly convoluted way and really think there's an easier way. You mean serialization? There are a couple of ways: the shelve and pickle modules that are built in will do it for you fairly easy and there are a number of other ways as well.
|
# ¿ Feb 15, 2008 22:35 |
|
Bonus posted:Here's a cool thing I've just found out while doing some stuff for school. The zip function works as its own inverse. code:
|
# ¿ Mar 14, 2008 19:00 |
|
Destroyenator posted:
The reason it works for this case is that the python interpreter pre-allocates objects for the first 256 or so small integers as singletons. Fake edit: code:
|
# ¿ Apr 3, 2008 03:39 |
|
tef posted:And, of course list=[x for x in "1234"] And of course not picking variable names that override/shadow builtin functions like list is preferred
|
# ¿ Apr 3, 2008 13:57 |
|
JoeNotCharles posted:Fixed your variable name for you. Argh, don't do this either :p (I'm sounding like the crotchety old man in this thread). If you want to use a single-letter variable name for a list, use L instead of l. l is a bad variable name in any language because it looks too much like 1 in fixed-width fonts.
|
# ¿ Apr 3, 2008 15:49 |
|
Anyone used any of the numerous python text parsing libraries out there? I'm playing around with pyparsing and having a bit of a time with it and was wondering: a) are there better options, or b) does anyone know pyparsing well enough that I should post questions here? I don't have anything specific yet, but it might be nice to know who has used what.
|
# ¿ Apr 9, 2008 22:12 |
|
Habnabit posted:I like pyparsing the best out of all of the python text parsers I've seen. I'm definitely not the best, but I can try to help you with problems. I've been working with for a little over a year. It's pretty slow, though, so you shouldn't use it if speed is a concern. I use it for lexing wiki pages, and it's fast enough to work with caching, but it definitely wouldn't work if the page was lexed every time it was loaded. Okay, well I guess this qualifies as a short question and the thread isn't exactly busting with other questions so here goes. I'm trying to write a stat file parser for TF2 (I guess I'm a statwhore ), but it uses some funky JSON-like syntax that should be easy to write a parser for using pyparsing but I'm having a bitch of a time understanding why some of it doesn't work. Here's a cut-down sample file: code:
So, the pseudo-BNF I worked up looks like: code:
code:
pyparsing.ParseException: Expected "}" (at char 132), (line:5, col:5), which refers to the line that starts with "aClassStats". It's like the failure is in the OR'ing of the statItem and statList, though I'm going to try parsing each individual line with all the objects and see if it matches where I expect. But if anything is glaringly wrong let me know.
|
# ¿ Apr 10, 2008 17:44 |
|
Kaluza-Klein posted:I have a list that I want to sort. The list would look something likee this: code:
Another better example (since sorting these normally produces the same results): code:
|
# ¿ Apr 10, 2008 20:43 |
|
Looks like Sun's App Engine/AWS-alike will handle python as well: http://research.sun.com/spotlight/2008/2008-04-09_caroline.html (see the graphic)
|
# ¿ Apr 14, 2008 21:36 |
|
Hero Pussy posted:I have a really stupid and really basic Python question because I was using string formatting operators and then my brain stopped working and I'm really tired and I can't find where I learned this in my Python book. %d is actually for integers. %f is for floats. http://www.python.org/doc/current/lib/typesseq-strings.html
|
# ¿ Apr 21, 2008 22:44 |
|
outlier posted:Simple. But you commonly also see a style where this is done: I've never seen that, and I don't know why you'd do that. It's kind of a useless breaking-out of a utility function. Just keeping the for and yield in-line is more readable to me and it's only marginally longer.
|
# ¿ Apr 23, 2008 20:41 |
|
bitprophet posted:I think the problem is you're passing >1 argument to 'print' (which doesn't even seem legal, how are you doing that?). The language reference confirms it as well as simply trying it in the interpreter. And I was ready to pass it through a bit of bytecode exploration using the code here, but SyntaxErrors abound. edit: and for those interested, when you "fix" it using the other way (putting a comma after name instead of using string formatting), it looks like this: code:
No Safe Word fucked around with this message at 18:43 on Apr 24, 2008 |
# ¿ Apr 24, 2008 18:41 |
|
Zedlic posted:I need to read a string containing ASCII control characters, but Python won't let me. Even if I read it as a raw string it says "SyntaxError: EOF while scanning triple-quoted string". Post the code. The error you've got sounds like not an error with your logic, but with your actual code (syntax ).
|
# ¿ May 2, 2008 16:54 |
|
karuna posted:Hey, how do you pass a variable within a URL? Are you trying to pass in paramters? Or just trying to interpolate the variable name into the string? The latter is super-simple: code:
quote:Also i'm having another problem..
|
# ¿ May 7, 2008 04:37 |
|
Mulloy posted:Well I'm going through this: http://docs.python.org/tut right now, and I'm going to poke around with the stuff in the OP. Anything you can recommend that'd be more specific to the project I described would be awesome, but one step at a time. And yeah, the guy who mentioned python mentioned the google API engine, but they're full at the moment, so I just downloaded the SDK to toy with once I get there. IDLE, the bundled editor with the Python distribution isn't bad for beginners, but you should try out all the popular coding editors and pick one I won't name my personal preference here for fear of starting up another thread of those, but there are probably some recommendations in the thread.
|
# ¿ May 9, 2008 20:53 |
|
As a note, you can just use list() on a generator expression to get the list, no need to do the comprehension. But one thing you have to remember about generators is that you basically can only iterate over them once and then they'll just return empty results (and/or raise StopIteration depending on how you use it). If you want to reuse the values to be iterated over a lot, then you should either continually reuse the same generator expression to create it or just store the results in a list if the size is sensible.
|
# ¿ May 15, 2008 05:32 |
|
JoeNotCharles posted:Yes, that's the problem.
|
# ¿ May 22, 2008 01:48 |
|
Sock on a Fish posted:I'm trying to decide between Django and TurboGears for a web app. One thing that I definitely don't like about Django is that updating my model requires manually generating and running SQL queries. I've looked at evolution, but I'm scared to use something that's not yet stable in my application. There's a whole Django thread actually, and I answered a similar question in it, which basically boils down to having django generate a sql dump script with your data in it, recreating the model (drop/recreate), and then re-inserting the data after you've modified the dump appropriately. If you use SQLite (which is awesome), it unfortunately doesn't support removal of columns from schema anyway, so you'd have to drop the table
|
# ¿ May 24, 2008 03:57 |
|
ashgromnies posted:I'm writing some image manipulation stuff in Python using PIL and was wondering if anyone would review a few basic functions I wrote(I'm a Python n00b). And I think using lambdas on the last bit is a little too "clever", just write the small function that actually does it: code:
code:
|
# ¿ May 28, 2008 22:53 |
|
Tuples are for collections of heterogeneous types, lists are for collections of homogeneous types. edit: Quote from GvR No Safe Word fucked around with this message at 06:49 on May 30, 2008 |
# ¿ May 30, 2008 06:44 |
|
chips posted:I worked out how to discover the number of arguments of a method/function in Python, but it doesn't seem to have a way to discover the names of the arguments - anyone know if there would be a way to do this? I realize that in many languages, this probably doesn't make sense, since the names of arguments are just labels local to the definition of the function. If anyone has a nice guide on reflection in Python, it'd be appreciated - I could only find some rather obtuse references that led me to find im_func and so on, which I have to admit is slightly easier than PHP5's reflection methods. You can do it, but it's not for the faint of heart. code:
I've never really messed with it, but I am marginally familiar with tooling around with func_code stuff because of this little tutorial/link on exploring Python bytecode: http://thermalnoise.wordpress.com/2007/12/30/exploring-python-bytecode/ edit: haha, and right after I made the post I noticed this: code:
No Safe Word fucked around with this message at 16:28 on Jun 3, 2008 |
# ¿ Jun 3, 2008 16:16 |
|
Scaevolus posted:In Python, 0 evaluates to False. (What you want is colorhash.has_key('r')) Is there really ever a reason to use has_key now that you can just use in? (Honest question, not snarky contrary response) code:
code:
|
# ¿ Jun 4, 2008 06:17 |
|
ATLbeer posted:Can someone show me the Python docs on the *array, **array information? I know what it does but, I've never actually seen the docs for it. It's also hard to google "python *" http://python.org/doc/current/ref/calls.html
|
# ¿ Jun 24, 2008 16:07 |
|
inveratulo posted:Okay so I need to create and use a multi-dimensional array of unknown size. Depends on what you want to do. If you're going to be doing math-like stuff, save yourself a headache and just start with NumPy from the get-go: http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html Otherwise, you can just start with a list, insert/append a new list, and so on. code:
|
# ¿ Oct 28, 2008 21:44 |
|
|
# ¿ Apr 27, 2024 20:28 |
|
JoeNotCharles posted:Aside, does anyone know a good site listing the complexity guarantees of common operations for common data types? I should know this off by heart but I always end up having to look it up... http://www.cplusplus.com/reference/stl/ (which are the STL container operations' complexities) for Python would be awesome.
|
# ¿ Oct 29, 2008 18:08 |