|
Don't do "is False" either. Just do "if not os.path.exists(sys.argv[1])"
|
# ? Mar 4, 2013 19:39 |
|
|
# ? May 9, 2024 01:05 |
|
Suspicious Dish posted:Don't do "is False" either. Just do "if not os.path.exists(sys.argv[1])" I still don't understand why. What is the advantage of doing: "if not os.path.exists(sys.argv[1])" instead of "if os.path.exists(sys.argv[1]) is False" or "if os.path.exists(sys.argv[1]) == False" Is this an elegance thing or is this something about how the guts of Python works? I don't see an advantage over any of those short of "it's tradition". "It's tradition" is a perfectly acceptable answer too, Lord knows its often the answer to why we do things in astro.
|
# ? Mar 4, 2013 21:50 |
|
JetsGuy posted:I still don't understand why. What is the advantage of doing: I don't really know what the Pythonesque (TM) answer is but to me saying "if not <thing>" does a better job of expressing your intentions than "if <thing> == False" (or "is"). The "not" version makes it clear (in part because it reads like English) that you're checking for a negative response; whereas the "False" version ties us up in detail (that a negative response is given in the form of the constant False). Of course there are much worse ways you can obfuscate what code is doing but it's the same sort of thing, just on a trivial level. Edit: Suspicious Dish's post suggests a related point, which is that you wouldn't write "if x == True:"; you'd just write "if x:". By analogy you shouldn't write "if x == False:", just "if not x:". Hammerite fucked around with this message at 22:16 on Mar 4, 2013 |
# ? Mar 4, 2013 22:12 |
|
This isn't really a Python thing. The if statement tests if something, when converted to a bool, is True. if foo == True: or if foo is True: is considered bad style because the if statement already does the tests for you, so you're adding noise that does nothing. In Python, the three statements if foo is True:, if foo == True: and if foo: have different meanings. The first one is asking if the value referenced by the "foo" variable is the same object as the one in the "True" object. Thus, if "" is False: will fail, and so will if 0 is False:. if foo == True: asks the object if it compares equivalent to the object referenced by the "True" variable. bool objects, for historical reasons, say that they are equal to 0 or 1, so if 1 == True: will succeed. But, empty strings do not compare equivalent to False, so if "" == False: will fail. And finally, if foo: asks if the object is considered True or False. Any non-zero number considers itself True, and zero considers itself False. All non-empty strings consider themselves True, and the empty string considers itself False. So, both if not 0: and if not "": will succeed. In the case of os.path.exists, this doesn't matter, because it's guaranteed to return a boolean, but not everything may. You almost always want the last behavior, as it leaves things up to the object's discretion, and more things will cast correctly by implementing __nonzero__ (or __bool__ in Python 3), but they not implement __eq__ correctly for boolean types.
|
# ? Mar 4, 2013 22:13 |
|
In addition to Suspicious Dish's (thorough) answer, you can also get into trouble with and/or -- these are control flow operators, not Boolean operators. People on the python-ideas mailing list have had trouble with assuming that and and or return True or False, and the solution is always "don't explicitly check against True/False and you'll be fine".
|
# ? Mar 4, 2013 22:26 |
|
Lysidas posted:In addition to Suspicious Dish's (thorough) answer, you can also get into trouble with and/or -- these are control flow operators, not Boolean operators. Huh? But they're listed under the huge heading "Boolean Operations" in the official docs. (I'm not a super advanced programmer, so perhaps there's something more subtle lurking here.) edit: I clicked on the the word and and it brought me to this section, which contains that extra subtlety I was wondering about. And also I see the title is technically "boolean operations" not "boolean operators". FoiledAgain fucked around with this message at 22:47 on Mar 4, 2013 |
# ? Mar 4, 2013 22:43 |
|
Yikes, the "x is false" in that section is poorly worded. We don't really have a name for "bool(x)", though.
|
# ? Mar 4, 2013 22:49 |
|
This is quite instructive, too:code:
|
# ? Mar 4, 2013 22:53 |
|
Is there a standard normal table anywhere that can take a Z-value and spit out a probability? Basically just a function call to one of these. And maybe one that will do the reverse. I'm sure it wouldn't be too hard just to put one in a .csv and read it, but if there's a library or module that contains one then it's probably worth learning.
|
# ? Mar 5, 2013 11:30 |
|
Wolfgang Pauli posted:Is there a standard normal table anywhere that can take a Z-value and spit out a probability? Basically just a function call to one of these. And maybe one that will do the reverse. I'm sure it wouldn't be too hard just to put one in a .csv and read it, but if there's a library or module that contains one then it's probably worth learning. Yup, what you are looking for is called a cumulative distribution function. The so-called "z-value" is just giving you the value of the cdf at a given point about the distribution. scipy.stats probably has everything you'll need as far as that goes. The exact method you'll want is scipy.stats.norm.cdf(). I threw in a few Zs to test it out and it seems to default to a mean of 0 and and SD of 1, which is likely what you want. If you want the "Z-value" for any other kind of normal distribution, you will have to change this via the options loc and scale. I only briefly played with those parameters but they appear to set the average and standard deviation (also referred to as the width) of the normal distribution you are interested in. http://docs.scipy.org/doc/scipy/reference/stats.html http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html
|
# ? Mar 5, 2013 15:36 |
|
Wolfgang Pauli posted:Is there a standard normal table anywhere that can take a Z-value and spit out a probability? Basically just a function call to one of these. And maybe one that will do the reverse. I'm sure it wouldn't be too hard just to put one in a .csv and read it, but if there's a library or module that contains one then it's probably worth learning. You probably want to use the continuous statistical distributions in scipy. code:
|
# ? Mar 5, 2013 15:38 |
|
Also, thanks very much to the very thorough explanations as to why I shouldn't check against True/False/None. The fact that the checks are already returning True/False is really what clicked with me. ------------ As an addendum to my previous post, the whole reason why we use Z is just a short hand way of talking about the probability of being that many standard deviations off the mean. Remember, in a normal distribution, ~68% of the values are ONE sigma within the mean, ~95% are within TWO sigma, and ~99.7% are within THREE sigma. So the Z-value tables are normalized to a function with mu = 0 and sd = 1 just so we can talk about it easily. There's really not much of a reason to use the arguments aside from laziness. If you know the size of the residual, you can use the table.
|
# ? Mar 5, 2013 16:00 |
|
Isn't calling a CDF directly going to be way more computationally expensive than a call to a table? I'm looking for something I can use in real-time. I didn't just want to compute from the probability distribution itself because I figured it would send performance to poo poo. *edit* Pygame question: if I draw a polygon or circle primitive then that's still a Rect object, yes? I'm trying to find a way to do UI click detection for non-standard shapes (I'm overlaying these on a map). If I can draw a whatever-shaped polygon and use polygon.collidepoint() then that'd be really easy. Wolfgang Pauli fucked around with this message at 19:08 on Mar 5, 2013 |
# ? Mar 5, 2013 18:39 |
|
Wolfgang Pauli posted:Isn't calling a CDF directly going to be way more computationally expensive than a call to a table? I'm looking for something I can use in real-time. I didn't just want to compute from the probability distribution itself because I figured it would send performance to poo poo. Looking at the source for the method, it appears that's what it's doing. It maps it based on arguments, but it's looking it up. A lookup table is certainly going to be faster, especially if you want to devote your system resources to other things. It makes sense too, because why constantly deal with computing the actual CDF? Gross. Just spit out the numbers for the numbers you have and provide good approximations if the user gives you something wonky. Incidentally, code:
|
# ? Mar 5, 2013 20:32 |
|
A stupid post was here.
onionradish fucked around with this message at 20:39 on Mar 5, 2013 |
# ? Mar 5, 2013 20:33 |
|
How quick is creating a scipy KDTree? I want to use it to implement a scene graph for 3D rendering but, since it can't be modified, you have to recreate it every time your data changes. Alternatively, is it possible to modify the class to allow adding and removing points?
|
# ? Mar 5, 2013 21:09 |
|
scissorman posted:How quick is creating a scipy KDTree? I have a friend who does 3D modeling of Stars in (I think) Python. It'll take a few days to get an answer from him, but I bet that could work great for you.
|
# ? Mar 5, 2013 21:33 |
|
Is there a way to reflect tables with Flask-SQLAlchemy? I'm at a dead end trying to adapt a small app to use Flask-SQLAlchemy in order to emit stuff from an exiting database to an internal webpage without having to have to redo a bunch of boilerplate stuff. Flask 0.9 Flask-SQLAlchemy 0.16 Using straight SQLAlchemy, this works: Python code:
If I try to do the same using flask-sqlalchemy, the errors start: code:
Python code:
I've read in the docs that reflect() exists but it dies. Python code:
|
# ? Mar 7, 2013 01:09 |
|
What's the general feel regarding Python 2 vs Python 3? None of the computers at the place where I work have Python 3 installed, and I know that it's not backwards compatible, so isn't every new Python 2 script just going to create problems in the future when Python 2 eventually gets abandoned? Python 3.x doesn't come with our redhat installs, and getting IT to install it for us would be a pain. Is this worth it? I've been slowly ramping up my effort to get people in my workplace to switch from MATLAB to numpy/scipy, but if this is creating headaches for a Python 2.X to 3.X switchover in the future then I'd like to make the switch happen sooner rather than later aeverous posted:What do you guys use for your Python work, I'm currently using Notepad++ with the pyNPP plugin but earlier this year I used VS2010 for a C# project and gently caress if I didn't get really spoiled by the code completion. I've looked around at Python IDEs and they all look a bit crap except PyCharm which is pretty expensive. Are there any free/OSS Python IDEs with really solid code completion and a built in dark theme? Vim Although recently I tried Spyder 2 on my Windows box (comes with the Pythonxy package) and it worked really really great, so I may start using that more.
|
# ? Mar 7, 2013 05:39 |
|
Python 3 doesn't really improve that much to cut the heartache.
|
# ? Mar 7, 2013 05:54 |
|
QuarkJets posted:I've been slowly ramping up my effort to get people in my workplace to switch from MATLAB to numpy/scipy, but if this is creating headaches for a Python 2.X to 3.X switchover in the future then I'd like to make the switch happen sooner rather than later Basically everyone doing numeric work in python is using 2.7 and will continue to do so for the foreseeable future.
|
# ? Mar 7, 2013 06:07 |
|
I use Python 3 for everything. Everything that's worth using it in my line of work has great support for it: numpy, scipy, matplotlib, biopython, networkx, ipython, etc.
|
# ? Mar 7, 2013 15:17 |
|
In my experience, 2to3 works pretty well for converting python2 code to be compatible with python3. The biggest hurdle is the bytes/str/unicode change, but as long as you don't do much work with strings it should be pretty painless to migrate. Also, as far as a Matlab replacement, I'm pretty sure most projects are now python3 compatible.
|
# ? Mar 7, 2013 15:39 |
|
I spent an hour or so hunting an irritating bug down and I'm still not entirely sure *why* it's a problem: With this code: Python code:
Python code:
Replacing it with mydict.clear() resolved the problem and is how it should have been done in the first place, but I'm still interested in what's actually going on here and whether it's some weird property of dictionaries/references that I haven't heard about. The error was obtuse enough that had I not known how to work with python bytecode I probably wouldn't have found the solution yet. Anyone have any ideas?
|
# ? Mar 7, 2013 15:49 |
|
The Gripper posted:Anyone have any ideas? I think it's because in the first case, the interpreter hunts for mydict, doesn't find it in local scope and so goes up a level to global, finds it, and runs fine. In the second case, the interpreter finds a mydict in local scope, but it's not assigned before you refer to it, so it throws up an error. In general if you want to use a global variable, you should mark it global at the beginning of the local scope where you're going to use it. Python code:
|
# ? Mar 7, 2013 15:53 |
|
Emacs Headroom posted:I think it's because in the first case, the interpreter hunts for mydict, doesn't find it in local scope and so goes up a level to global, finds it, and runs fine. In the second case, the interpreter finds a mydict in local scope, but it's not assigned before you refer to it, so it throws up an error. In general if you want to use a global variable, you should mark it global at the beginning of the local scope where you're going to use it. edit; I guess it makes sense and it's even in the Python FAQ, I don't know how I haven't come across it before. The Gripper fucked around with this message at 16:10 on Mar 7, 2013 |
# ? Mar 7, 2013 16:00 |
|
A wide ranging query about Python web development. Years ago, I made a website for a laboratory I was working with. It incorporated access to a database, with record browsing, tools for search and visualization in a number of ways. It also functioned as a CMS for various groups that used the database or were involved in lab activities, being somewhere they put reference documents, notices and so on. There was the added complication that some of the website was open to the public, and some only for members of particular groups. I did the site up in Plone. NEVER DO THIS. Anyway, to my surprise, the thing has worked fine for 5 years, with next to no maintenance or attention. Now, I'm between jobs and my old colleagues asked me if I can update the site. Which brings me to the main question: what framework should I use? Plone is out of the question. By default I thought of Django (which I have a little experience in), but I'm unsure if the mix of record browsing and content would sit well. The task is a bit too involved for the various microframeworks. Opinions? I want to work in Python because that's my environment of choice and I got a heap of code from last time that I can reuse.
|
# ? Mar 7, 2013 17:43 |
|
The Gripper posted:
You need to declare global mydict if you want to rebind it in the local scope. http://docs.python.org/2/reference/simple_stmts.html#global mydict = {} tries to create a new dictionary object and assign it to mydict. This breaks because python doesn't know if you want to bind the object to a new local or assign to the global variable. mydict.clear() works because you're accessing the existing dictionary object and calling a method on it. This will fix your code. Python code:
DARPA fucked around with this message at 17:50 on Mar 7, 2013 |
# ? Mar 7, 2013 17:46 |
|
Wildtortilla posted:I'm currently taking PSU's Certification in Geographic Information Systems (aka, intelligently using ArcMAP). In May I'll be starting my final course for the certificate and I have a huge array of options, but I'm leaning towards their course in Python. From looking at job postings for GIS positions, I'd wager at least 50% of postings include knowing Python as a desirable skill. However, coming from a background in geology, I have no experience with coding; would the links in the OP be a good place for me to start or should I start elsewhere since I have no experience? The first link in the tutorials "MIT Introduction to Computer Science and Programming" and the contents of this post seem like they'd be a good start for me. Any suggestions? Get out of my industry and go back to the field! But yeah, the mit edx course and text book is very good, definitely take arcpy course if it's offered, programming and scripting for esri products is in high demand. Just don't get shoehorned into a computer janitor job, your skill is geospatial analysis. Then again, model builder will do anything you would want to script anyway, if you're interested in programming take the python course. The biggest skill set should be database design and management, I would say that's more important than python. standardtoaster fucked around with this message at 17:58 on Mar 7, 2013 |
# ? Mar 7, 2013 17:49 |
|
DARPA posted:You need to declare global mydict if you want to rebind it in the local scope.
|
# ? Mar 7, 2013 17:53 |
|
After spending the night away from it, I've managed to solve my own problem. I needed to add db.metadata.reflect(db.get_engine(app)) and then redefine all my models like so: Python code:
|
# ? Mar 7, 2013 17:56 |
|
JetsGuy posted:I have a friend who does 3D modeling of Stars in (I think) Python. It'll take a few days to get an answer from him, but I bet that could work great for you. So that buddy of mine got back to me. While he does indeed use python for some tasks, the 3D simulation / modeling he does is in FORTRAN90. Sorry if I got your hopes up.
|
# ? Mar 7, 2013 20:09 |
|
JetsGuy posted:So that buddy of mine got back to me. While he does indeed use python for some tasks, the 3D simulation / modeling he does is in FORTRAN90. Sorry if I got your hopes up. Thank you for asking. I'll probably use KDTree for static geometry like terrain and find some other solution for dynamic objects.
|
# ? Mar 7, 2013 20:26 |
|
Thanks for the feedback, guys Modern Pragmatist posted:In my experience, 2to3 works pretty well for converting python2 code to be compatible with python3. The biggest hurdle is the bytes/str/unicode change, but as long as you don't do much work with strings it should be pretty painless to migrate. That's good news; it sounds like I don't need to worry about this too much and can just make a casual python3 software request without banging on doors
|
# ? Mar 7, 2013 21:28 |
|
outlier posted:A wide ranging query about Python web development.
|
# ? Mar 7, 2013 21:42 |
|
Hi. Newbie python programmer that needs a little direction or help. I'm trying to convert each letter in a string where each letter in the alphabet correspond to an alternating letter in a key. What I'm thinking is to convert the string into a list, find out where each letter in the list of the string corresponds to a position in the alphabet, take that position and use it to find the corresponding letter in the key, do that for the entire list of the string, and then convert that back into a string. Here is my code if anyone wants to help code:
|
# ? Mar 9, 2013 02:13 |
|
Mad Pino Rage posted:Hi. Newbie python programmer that needs a little direction or help. I'm trying to convert each letter in a string where each letter in the alphabet correspond to an alternating letter in a key. No idea if python optimizes the lambda to create variables first then use later or if it recreates the dict every call, but this is what I'd do: Python code:
Or if you call print ''.join(<map>) instead: 'GVHGRMT' If you wanna really be sure, or to expand it way out for readability: Python code:
|
# ? Mar 9, 2013 03:07 |
|
Jewel posted:
Thank you for help. Just to be clear that I'm understanding the code. zip is a function that creates a list of tuples for each value of alphabet that corresponds to key Dictionary makes a key-value pair(not fully too sure understanding this) But I'm not familiar with lambda or how that all works exactly together, and so far it looks like another way to make a function. EDIT Well, tutorials and documentation isn't making it all clear, but I broke it down and ran it to see what it exactly did. Zip made everything into [('A','Z'),('B','Y'),....] tuples Dictionary made the tuples into key-value pairs in a list ['A':'Z'....] But calling lambda outputs "function lambda at 0x033E95B0" ???? Map runs lambda as a function or procedure for every iteration for input say I typed in 'apple' and it would come out at this point as ['Z','G','G','E','P'] and the ''.join combined the list into a string and then I assigned that to a variable and made it available outside the function DoggesAndCattes fucked around with this message at 04:46 on Mar 9, 2013 |
# ? Mar 9, 2013 04:08 |
|
Mad Pino Rage posted:Thank you for help. Just to be clear that I'm understanding the code. Yes! Python code:
But you can just as easily do: Python code:
|
# ? Mar 9, 2013 04:47 |
|
|
# ? May 9, 2024 01:05 |
|
Jewel posted:No idea if python optimizes the lambda to create variables first then use later or if it recreates the dict every call, but this is what I'd do: It will call the dict function on every call of the lambda, since it doesn't know that the dict call is the same as it is now, as the global may have changed. Just do this: Python code:
|
# ? Mar 9, 2013 05:17 |