|
GrumpyDoctor posted:Here's a total newbie question code:
|
# ? Jun 6, 2012 19:04 |
|
|
# ? May 9, 2024 02:29 |
|
Unless you're using NumPy, a list comprehension is probably the best way to do it. [10] * 5 will work but is a bad idea -- each element of that list will be a reference to the same object and this can come back to bite you in the rear end when you're not expecting it:Python code:
EDIT: beaten; using [10] * 5 is okay only if you know that you're not making a list of anything mutable. EDIT 2: FoiledAgain that is not Python REPL output Lysidas fucked around with this message at 19:18 on Jun 6, 2012 |
# ? Jun 6, 2012 19:07 |
|
Don't do this if you want to build an actual application, as _ is commonly used to mark translatable strings:Python code:
Python code:
|
# ? Jun 6, 2012 19:29 |
|
Lysidas posted:each element of that list will be a reference to the same object and this can come back to bite you in the rear end Did not know this. Thanks! quote:EDIT 2: FoiledAgain that is not Python REPL output Consider me properly chastised.
|
# ? Jun 6, 2012 19:29 |
|
I'm reading through O'Reilly's "Learning Python", 4th ed. I'm reading the chapter on numeric types, specifically the pages about the decimal module. The author seems to suggest setting setcontext().prec to 2 in order to do calculations with money. But from my experiments it seems like this sets the number of significant digits used for results, rather than the number of digits used to the right of the decimal point. For example, the following example is given in the book (slightly paraphrased):Python code:
Python code:
I'm running Python 3.2.3 on 64-bit Windows 7.
|
# ? Jun 6, 2012 23:00 |
|
Maybe you can use an integer for the dollars and a Decimal with fixed precision for the cents? Dunno, since the documentation clearly indicates that prec is precision and not digits past the decimal. Maybe the book is just wrong. Plus yeah: Python code:
|
# ? Jun 6, 2012 23:28 |
|
The book is giving terrible advice. Don't go mucking around with global constants like the decimal context precision, it will bite you right in the rear end. Use integers, scaled to the level of precision needed. If you just need to work with cents and nothing smaller, then treat 100 as one dollar.
|
# ? Jun 7, 2012 01:46 |
|
That's what I thought. I have another question. I understand that / performs division and yields a floating-point result, in Python 3.X. So I reasoned that if the operands are too large to be converted to floats, then the interpreter should fail to do the division, even if the result is something that can be represented comfortably as a float. But this is not what happens. Python code:
Python code:
|
# ? Jun 7, 2012 02:44 |
|
It could just be doing integer division and casting the result to a float.
|
# ? Jun 7, 2012 02:54 |
|
I believe it's doing the equivalent of float(Decimal(x) / Decimal(y))
|
# ? Jun 7, 2012 02:59 |
|
I have a set of data representing a probability distribution function stored as an array of arrays. I'd like to compute the cumulative distribution function (i.e. the sum of each element and all those proceeding it) for each row (and then another CDF of each row's total). Is there a clever way to do this using list comprehensions? Right now I've gotcode:
|
# ? Jun 7, 2012 13:11 |
|
Hubis posted:I have a set of data representing a probability distribution function stored as an array of arrays. I'd like to compute the cumulative distribution function (i.e. the sum of each element and all those proceeding it) for each row (and then another CDF of each row's total). Is there a clever way to do this using list comprehensions? Right now I've got I would use numpy and cumsum: Python code:
Modern Pragmatist fucked around with this message at 13:28 on Jun 7, 2012 |
# ? Jun 7, 2012 13:24 |
|
I have never used Python in any serious way before, and I decided that I would like to stream some data to the browser using Tornado. My only feedback so far is that the way Python interprets indentations loving sucks rear end. UGH.
|
# ? Jun 7, 2012 13:28 |
|
Stop using notepad.
|
# ? Jun 7, 2012 13:40 |
|
What do you mean the way it interprets indentations sucks rear end? As long as you are consistent, it shouldn't matter. Just set it in your text editor and forget about it.
|
# ? Jun 7, 2012 13:41 |
|
I'm on a mac right now so no notepad The specific situation was that I did not indent a couple lines below a method definition and the error messages produced were sufficiently confusing that I went looking all over hell and back before I found my simple mistake. That is all, carry on.
|
# ? Jun 7, 2012 14:06 |
|
Christ. Whining about indents in Python is about 15 years out of date. There are enough editors out there that know how to handle Python.
|
# ? Jun 7, 2012 14:27 |
|
Isn't the message in this case reasonably clear? I'm legitimately curious about which cases cause Python to tell you something other than the following:Python code:
code:
|
# ? Jun 7, 2012 14:28 |
|
who wants to talk about tabs vs spaces
|
# ? Jun 7, 2012 14:29 |
|
Soft tabs forever.
|
# ? Jun 7, 2012 14:56 |
|
etcetera08 posted:who wants to talk about tabs vs spaces One would think inciting a "discussion" about whitespace would be grounds for probation in a Python thread. That said, I reckon semantic whitespace makes it a very good language learn on, as it forces the beginner programmer to make consistently laid out code. Breaking whitespace requirements requires them to make a conscious choice to do so, which means hopefully there's a decent reason to do so.
|
# ? Jun 7, 2012 15:19 |
|
Here's the demo of my Tornado based monstrosity. Python part:code:
code:
Edit: it's just a demo, I can see at least one bug right away. My Rhythmic Crotch fucked around with this message at 15:42 on Jun 7, 2012 |
# ? Jun 7, 2012 15:39 |
|
Lysidas posted:There are a lot of ways to approach this, but I'd probably do something like the following: vikingstrike posted:^^Kind of beaten. His way works too. Ridgely_Fan posted:I would organize things a bit differently. For a set of things with members, I tend to use a list of dictionaries (unless they need associated methods, in which case I use classes). Harm Barn Gumshoe posted:Well I got hella beaten on code examples, but the big takeaway should be that you need to consider how you want your data to be stored (your "desired behavior" example was so close!). You need some kind of container to hold an individual student's information (after all, it's the student that has a Name or a Grade, not a classroom), THEN you can add them to a larger classroom container (so you can say "get the Grades from everybody in the classroom" etc.). Hey guys, I know this is a week+ late, but I wanted to thank y'all for all thr great responses to my question. I'm sorry I didn't respond earlier, I generally only read the python thread at work, and it's been very busy here lately. IN any case, thanks again for all the great insights, and I'm looking forward to being able to incorporate this kind of thing into my codes to better recall data.
|
# ? Jun 7, 2012 17:08 |
|
My Rhythmic Crotch posted:I have never used Python in any serious way before, and I decided that I would like to stream some data to the browser using Tornado. May I introduce you to Aquamacs? http://aquamacs.org/ It's emacs, but you can actually use regular OSX commands (e.g. apple-c, apple-v) like a normal human being instead of using all the lovely emacs commands! I like it because I can use some of the *good* emacs things (e.g. rectangle deletion), but still use regular keyboard commands that most of us are used to.
|
# ? Jun 7, 2012 17:11 |
|
JetsGuy posted:May I introduce you to Aquamacs? If you don't mind using the mouse you can do rectangle delete in most OS X text editors by holding Option and dragging. I don't have a Mac at the moment but I think it might work in MacVim too?
|
# ? Jun 7, 2012 19:30 |
|
Let's say I want to use Enthought and IDLE. On Windows: 1. Download executable install file. 2. Run it. 3. Enjoy your IDLE shortcut created on your desktop and all packages are installed! On Linux: 1. Download enthought.sh package. 2. Figure out how to do anything with it. Wait, I have to go into the terminal? Okay.. now what? CHMOD? Okay. Now... I ran it I think? It unpacked something. 3. Let me try typing IDLE in the terminal now. Hmm, that doesn't work. Let me do some research online. 4. Okay someone says to do SUDO apt-get install idle. Okay that installed it I think. 5. Okay running IDLE. Great! Now let me try to use a simple package that was included with it like Pylab. 6. It doesn't have Pylab installed? And they wonder why no one uses Linux. Jesus Christ, I am going to throw this laptop out of the window. :tenbux: to the first forum member who can get Enthought running properly on my laptop in Ubuntu, including all needed modules like pylab and all the parts that come with.
|
# ? Jun 7, 2012 19:56 |
|
If you're using Ubuntu you should use apt-get to install python packages (or the graphical thing, Synaptic I believe). sudo apt-get install idle python-matplotlib That should pull in numpy, scipy, and the pylab libraries. You might want to think about ipython as well (it's a nice shell for interactive python sessions). edit: if there's another library you find isn't installed, search for it -- e.g. code:
Emacs Headroom fucked around with this message at 20:07 on Jun 7, 2012 |
# ? Jun 7, 2012 20:04 |
|
There are plenty of people that use Linux for development. In fact, I think it's easier/more straight forward to install modules from Linux than other OSes. To each their own. This will probably help you: https://svn.enthought.com/enthought/wiki/Install/ubuntu edit: If you're unsure what to do with an enthought.sh file, your frustration with Linux is just a lack of familiarity with the environment it seems. .sh is the file extension of a shell script. All you should have needed to do is run 'sudo sh enthought.sh'. The script itself probably downloaded/installed things, but .sh itself is not a zip file, like you referred to. Also, your linux distro should come with python out of the box. All you should have had to do is run 'python' from a terminal to go into the interactive shell. Like the last guy said, just use apt-get for most things. vikingstrike fucked around with this message at 20:53 on Jun 7, 2012 |
# ? Jun 7, 2012 20:49 |
|
vikingstrike posted:There are plenty of people that use Linux for development. In fact, I think it's easier/more straight forward to install modules from Linux than other OSes. To each their own. Especially for python, windows really doesn't play nice.
|
# ? Jun 7, 2012 21:08 |
|
the posted:Let's say I want to use Enthought and IDLE. Why would you expect pylab (matplotlib) to come installed with IDLE? You told it to install IDLE. Use Ridgely_Fan's advice. You always want to try to go with the stable version of software provided with the distro unless you have a very specific reason for installing a different version. Also, Python development is a million times better on linux or OS X than windows. The kind of things you are complaining about will literally become second nature once you've done them once. And yes, you have to use the terminal so learn to embrace it. You may want to spend some time reading up on linux basics before blasting it.
|
# ? Jun 7, 2012 21:20 |
|
the posted:And they wonder why no one uses Linux. Jesus Christ, I am going to throw this laptop out of the window. I hope you do a few years of python development in linux, so you can look back at this statement and laugh at yourself. It's perfectly sane, you're just unfamiliar with it and throwing a fit because it doesn't behave the way you think it should.
|
# ? Jun 7, 2012 21:28 |
|
I honestly hope so. Sorry if that whole post seemed stupid, but I was just banging my head in frustration at it. It just doesn't seem as intuitive as a Windows environment.
|
# ? Jun 7, 2012 22:26 |
|
Automated package managements is one of the best things about Linux / BSD / etc. It's very conceptually different from the Windows way of doing things, but if you're going to use Linux much at all it's very worth reading about : http://library.linode.com/using-linux/package-management#sph_package-management-concepts By the way, if my advice above helped, send my to the Red Cross.
|
# ? Jun 7, 2012 22:32 |
|
the posted::tenbux: to the first forum member who can get Enthought running properly on my laptop in Ubuntu, including all needed modules like pylab and all the parts that come with. Did you update your PATH and PYTHONPATH? EPD installs in a subdir in whatever random place you run the script. Did you try just "ipython --pylab" or even just "from pylab import *" ? And IDLE... why on earth?
|
# ? Jun 7, 2012 23:33 |
|
the posted:I honestly hope so. Sorry if that whole post seemed stupid, but I was just banging my head in frustration at it. It just doesn't seem as intuitive as a Windows environment. Windows is great if all you need is to install programs and run them in the default way. If you want to run a proper development environment set up just the way you like it, however, Linux is awesome once you know the ropes. Package management means the install steps for near god drat anything you need is apt-get install application and for the rest of the proprietary stuff, the shell scripts become second nature after a while, and there's no shortage of instructions out there.
|
# ? Jun 7, 2012 23:45 |
|
Even better, use OS X and you can have both!
|
# ? Jun 7, 2012 23:52 |
|
Sinestro posted:Even better, use OS X and you can have both! Oh god Python on OSX. I develop on a Mac Pro, and I was initially in love with it. That was until I actually started using a package manager. You get to choose fink or macports, and you better hope everything you want is on one of those, because god forbid you try to mix compiled programs with fink/port installed dependences or the other way around, much less between the two. It also seems like every third library I try to install is just completely broken on 10.7, and requires me to either patch it myself or badger the developers into fixing it. Using Python on OSX is a nightmare with package managers. Everything is beautiful if I download binaries or compile everything myself (except for the fact that you have to fight with the default system installed Python sometimes). I'm experimenting with using a Linux VM and just running everything in that either with ssh or PyCharm remote interpreters. OnceIWasAnOstrich fucked around with this message at 00:10 on Jun 8, 2012 |
# ? Jun 8, 2012 00:07 |
|
OnceIWasAnOstrich posted:Oh god Python on OSX. I develop on a Mac Pro, and I was initially in love with it. That was until I actually started using a package manager. You get to choose fink or macports, and you better hope everything you want is on one of those, because god forbid you try to mix compiled programs with fink/port installed dependences or the other way around, much less between the two. With pythonz and Homebrew, it is actually awesome. Fink/MacPorts are both huge clusterfucks, though.
|
# ? Jun 8, 2012 00:14 |
|
I'm using IDLE, because, well, that's the first thing I used when I started on my Windows desktop so I'm used to it. I'm doing physics programming, so I just need a GUI where I can code small programs that use Pylab/matplotlib/scipy/numpy. That's about it.
|
# ? Jun 8, 2012 00:25 |
|
|
# ? May 9, 2024 02:29 |
|
the posted:I'm using IDLE, because, well, that's the first thing I used when I started on my Windows desktop so I'm used to it. Are you using IPython yet? You should.
|
# ? Jun 8, 2012 00:28 |