|
Dominoes posted:I'm learning Python, and chose Python 3 because it's the newer, refined version. You're more likely to run into compatibility problems with external modules in Python 3, since most of the information and development resources available are for Python 2. Philosophically, I suggest using Python 3 - you'd be helping push the community to upgrade. Not to rain on your parade, but you really won't be. The scientific community may never change to Python 3 for example. The reason is the same reason why there are still many coding libraries out there in regular use that are in FORTRAN etc. If you have a whole code base of tools written in Py2, you really don't want to spend the time "upgrading" to Py3. ESPECIALLY when half the libraries you use have completely changed how some of their functions work from 2 to 3. It's a headache I don't want to deal with, and I'm not alone there. Python 2.6/2.7 really would be my suggestion to any newbie. **HOWEVER**, the question is, as always, "what do you want to do?" If you're just doing python for your own fun, by all means, use Py3. If you want it as a resume line, you want to find out what version of python the community you want to work with uses. Scientists who use python almost universally use Py2.x. Outside of science, I know guys who use both Py3 and Py2. While the differences aren't awful, the minor selling point that you use the version they do may help you.
|
# ? Apr 9, 2013 23:23 |
|
|
# ? May 9, 2024 05:27 |
|
BeefofAges posted:If you're going to use subprocess with shell=True, make sure to read up on all the potential security problems that go along with it. Also, I found that sometimes setting shell=True made the subproc run like I expected, but a little extra syntax work made it so I didn't have to use shell=True. Try to avoid it, even if you're hardcoding in all the commands. That said, there ARE times you HAVE to use it, unfortunately.
|
# ? Apr 9, 2013 23:24 |
|
There are never times that you have to use shell=True. If you need to unfortunately need to parse input from a configuration file (which you should never, ever, ever do), you might be able to get by with the shlex module.
|
# ? Apr 10, 2013 00:29 |
|
shell=True neatly solves that guy's problem. If all he's doing is lspci piped through grep with no user input in the shell command then shell=True is not that bad unless I'm missing something. Suggesting that it never be used seems extreme.
|
# ? Apr 10, 2013 00:58 |
|
Is there a way to procedurally change a function based on variables, like in a loop? The best way I can describe it is with psuedocode:Python code:
|
# ? Apr 10, 2013 01:25 |
|
Dominoes posted:Is there a way to procedurally change a function based on variables, like in a loop? The best way I can describe it is with psuedocode: There's probably a better way to do this, and I'm tired or I would think about it and suggest one. But yes you can do that using __getattribute__. Python code:
|
# ? Apr 10, 2013 01:53 |
|
Hammerite posted:There's probably a better way to do this, and I'm tired or I would think about it and suggest one. But yes you can do that using __getattribute__.
|
# ? Apr 10, 2013 02:01 |
|
If you want to keep attributes, use getattr instead of __getattribute__, but chances are you want to use a list.
|
# ? Apr 10, 2013 02:22 |
|
Dren posted:Suggesting that it never be used seems extreme. Never use shell=True is sort of like never use lambda.
|
# ? Apr 10, 2013 03:20 |
|
I have been following this thread for a while and have loved the back-and-forth. There are quite a few folks that really know the ins and outs of the language. I'd like to get to that point. I have read several books on Python, primarily on syntax and other very small programs. I'm looking to find something a little bigger that I can really learn the language as well as Pythonic programming techniques. I suppose my question is what did you all use to learn the language? I have an idea for what I would like to build, some form of a custom web application. Anytime I go to check out starting something with Django, I get lost in all of the complexity of the whole thing. It seems built to handle everything, when I really want to start with the bare bones and expand from there. I'd like to play around with basic site functionality, handling requests, displaying data from a database, etc. before digging into some of the details. Is there anything simple and open source that would give that flexibility?
|
# ? Apr 10, 2013 11:11 |
|
Hitch posted:I have been following this thread for a while and have loved the back-and-forth. There are quite a few folks that really know the ins and outs of the language. I'd like to get to that point. I have read several books on Python, primarily on syntax and other very small programs. I'm looking to find something a little bigger that I can really learn the language as well as Pythonic programming techniques. Most web frameworks use the same MVC (Model-View-Controller) setup that Django uses. If you want to build web applications, I highly advise you to start learning how to use frameworks - and Django is a pretty good one to start with. There's a fairly decent beginner's tutorial on their site, I suggest giving it a go. Rohaq fucked around with this message at 17:19 on Apr 10, 2013 |
# ? Apr 10, 2013 12:52 |
|
Suspicious Dish posted:There are never times that you have to use shell=True. If you need to unfortunately need to parse input from a configuration file (which you should never, ever, ever do), you might be able to get by with the shlex module. Shlex certainly is a godsend for many instances, but I have yet to find a way to call IDL -c without shell=True and have it work right. Suggesting that it can ALWAYS be gotten around seems implausible to me.
|
# ? Apr 10, 2013 17:11 |
|
Python code:
Also let's assume I want to extend the functionality of the method to handle different classes. Should I even do that or should I just make separate methods for each one?
|
# ? Apr 10, 2013 18:54 |
|
ARACHNOTRON posted:
Why not do it the other way around by asking the class do something (and that something is class specific)?
|
# ? Apr 10, 2013 19:14 |
|
JetsGuy posted:Shlex certainly is a godsend for many instances, but I have yet to find a way to call IDL -c without shell=True and have it work right. IDL -c?
|
# ? Apr 10, 2013 19:14 |
|
accipter posted:Why not do it the other way around by asking the class do something (and that something is class specific)? I kind of answered my own question while I was pondering this, because I have the data I want to pull out in a class variable with the same name for every class I'm using. So I don't need to do what I'm trying to do and can just check if the var is there. But to expand on the question, for future use (maybe): Assume I'm in a situation where, for some reason, I do not want the first module to be directly interacting with certain things, but I do want the classes to. And I'm dead-set on keeping those classes inside the first module. (current use is database stuff and I want to make a little proxy to initialize my db)
|
# ? Apr 10, 2013 19:34 |
|
Suspicious Dish posted:IDL -c? I'm sorry, I meant to say IDL -e, which is useful for calling IDL functions from outside IDL. It opens IDL, runs the program, and closes IDL. Example: Python code:
|
# ? Apr 10, 2013 21:45 |
|
ARACHNOTRON posted:
Avoid it if you can, but if you must, use isinstance Python code:
code:
|
# ? Apr 10, 2013 23:24 |
|
I know about isinstance but how is that going to work if my classes are in a different module and this one doesn't import anything? Because I thought about using it, decided to look up whether or not it would work, and got a big "there is seriously no info on that, sucker". Smarmy Coworker fucked around with this message at 23:53 on Apr 10, 2013 |
# ? Apr 10, 2013 23:51 |
|
You want this function to specially handle a specific class, but you don't want to import the definition of that class? This sounds really weird. What are you actually trying to accomplish?
|
# ? Apr 10, 2013 23:56 |
|
Actually that possibility didn't even cross my mind. I don't know what I was thinking!!
|
# ? Apr 11, 2013 00:15 |
|
Is there a working PDF manipulation module for Python 3? I've tried Pypdf, but it glitches out when I try to install with PIP. I'd like to merge PDF files. If I use Pypdf2, I get the following message using this code:code:
"File "c:\...merger.py", line 97, in merge elif type(fileobj) == file: NameError: global name 'file' is not defined" Line 97 of merger.py is " elif type(fileobj) == file:" I get similar errors in my own code when using code such as "input1 = PdfFileReader(file("document1.pdf", "rb"))" - that's a copy and paste from http://www.blog.pythonlibrary.org/2012/07/11/pypdf2-the-new-fork-of-pypdf/ Dominoes fucked around with this message at 02:22 on Apr 11, 2013 |
# ? Apr 11, 2013 00:41 |
|
JetsGuy posted:I'm sorry, I meant to say IDL -e, which is useful for calling IDL functions from outside IDL. It opens IDL, runs the program, and closes IDL. Python code:
|
# ? Apr 11, 2013 00:56 |
|
Hitch posted:I have been following this thread for a while and have loved the back-and-forth. There are quite a few folks that really know the ins and outs of the language. I'd like to get to that point. I have read several books on Python, primarily on syntax and other very small programs. I'm looking to find something a little bigger that I can really learn the language as well as Pythonic programming techniques. Just do your project and stop worrying about it all! I never read any books about Python; what really got me to learn it well was when I forced myself to actually start using it a lot. Every time I had an idea for a project, I'd do it in Python and see it through. Google, the official documentation, this thread, stack overflow, stuff like that. I don't post much in here but after a few years of just using the language I understand a lot of ins and outs, little tricks, common patterns and stuff like that. It will come naturally as long as you're reading a lot of other people's code and writing a lot of your own!
|
# ? Apr 11, 2013 05:58 |
|
For the python-daemon library (if anyone is familiar), how do I set it so a process doesn't have a timeout and will run forever? Just self.timeout = 0? The reason I ask is I'm having a rabbitmq worker (using pika) consume messages from a channel, and it sleeps until there is a message so it just dies when the timeout is up and no messages have been received, which I don't want.
|
# ? Apr 11, 2013 15:32 |
|
MeramJert posted:Just do your project and stop worrying about it all! I never read any books about Python; what really got me to learn it well was when I forced myself to actually start using it a lot. Every time I had an idea for a project, I'd do it in Python and see it through. Google, the official documentation, this thread, stack overflow, stuff like that. I don't post much in here but after a few years of just using the language I understand a lot of ins and outs, little tricks, common patterns and stuff like that. It will come naturally as long as you're reading a lot of other people's code and writing a lot of your own! Yeah, I think this is right. I also have the tendency to want to try to understand the whole Whatsit before I start doing something with the Whatsit. That doesn't work too well though. Just start using the Whatsit and fix problems and learn things as you need them. And be prepared to go back and rewrite large swaths as you learn more. Also don't get complacent. When you get something working using Whatsit Feature X, don't just forget about it. When you're reading stuff about Whatsit and see something new about Feature X that makes how you used it stupid...go back and fix it.
|
# ? Apr 11, 2013 18:15 |
|
Suspicious Dish posted:
I'll try this when I get to the machine that has the procedure, but I seem to remember the computer complaining loudly about not knowing what to do without the shell. Thanks though!
|
# ? Apr 11, 2013 18:36 |
|
ARACHNOTRON posted:For the python-daemon library (if anyone is familiar), how do I set it so a process doesn't have a timeout and will run forever? Just self.timeout = 0? The reason I ask is I'm having a rabbitmq worker (using pika) consume messages from a channel, and it sleeps until there is a message so it just dies when the timeout is up and no messages have been received, which I don't want. Python code:
DARPA fucked around with this message at 19:58 on Apr 11, 2013 |
# ? Apr 11, 2013 19:01 |
|
ARACHNOTRON posted:For the python-daemon library (if anyone is familiar), how do I set it so a process doesn't have a timeout and will run forever? Just self.timeout = 0? The reason I ask is I'm having a rabbitmq worker (using pika) consume messages from a channel, and it sleeps until there is a message so it just dies when the timeout is up and no messages have been received, which I don't want. I'm also interested in writing a RabbitMQ listener daemon in Python, as it happens, to replace a perl script I wrote a year back that's started misbehaving recently for reasons unknown. Any tips?
|
# ? Apr 11, 2013 20:35 |
|
I thought I understood unicode encoding/decoding, but this has me really confused. I have a big set of objects called Segments that respresent consonants and vowels of a language. It's partially random which Segments end up in any given run of my program, so I like to check in the interactive window. I'm having trouble getting the right symbols to print. The relevent part of the Segment object is this part: code:
code:
It's hard for my lazy-rear end fingers to type print segment.symbol all the time, and I'd like a proper printout when I type just segment. What do I need to fix? edit: and apparently my problems extend to forum posting too...that 'm-with-a-dot-underneath' didn't show up properly here either, but at least you can still tell there are two different things getting printed. FoiledAgain fucked around with this message at 00:48 on Apr 12, 2013 |
# ? Apr 12, 2013 00:33 |
|
I spent a day and a half fighting a similar "problem" and had myself convinced I didn't understand Unicode either. My code was actually fine -- the problem was the output console (Windows Powershell). Try running the code in IDLE, or writing the output to a file and see if you're getting the value you expect. It may be your output console that doesn't understand Unicode, not you. From IDLE: >>> m=u'm\u0325' >>> print m m̥ edit: ^^ the M-dot doesn't show when it's enclosed in a 'code' block on the forum
|
# ? Apr 12, 2013 01:07 |
|
onionradish posted:I spent a day and a half fighting a similar "problem" and had myself convinced I didn't understand Unicode either. My code was actually fine -- the problem was the output console (Windows Powershell). Try running the code in IDLE, or writing the output to a file and see if you're getting the value you expect. It may be your output console that doesn't understand Unicode, not you. Yeah, I should have noticed that - printing to file has never been a problem. I'm running this in PyScripter, if that means anything to anyone. edit: and I tested this out with IDLE and it works fine. So the problem is not me. *whew*. Thanks! I found this too: code:
FoiledAgain fucked around with this message at 01:18 on Apr 12, 2013 |
# ? Apr 12, 2013 01:14 |
|
DARPA posted:It sounds like your application isn't looping. You might need something like: Huh. Actually yes! I opened the connection and channels and whatnot in __init__ so that is most likely the problem. I had no idea python-daemon did that. I will test things out.
|
# ? Apr 12, 2013 01:24 |
|
When you're dealing with Unicode strings you can do the encoding to ASCII (or whatever your terminal's encoding is) before you print. This will allow you to manage the errors. See http://docs.python.org/2/howto/unicode.html for more information. I'm pretty sure that when you use 'print' str() gets called on your object, and in 2.x str only does ASCII. So consider that too. Maybe what you want with repr() is to directly return self.symbol so that no transcoding is performed.
|
# ? Apr 12, 2013 02:36 |
|
Is there a handy but thorough list of advantages gained by using new-styled classes in Python 2.X? I know a few of the advantages, like being able to user super, but I'd like to have a whole list handy to show to a coworker
|
# ? Apr 12, 2013 08:45 |
|
Descriptors, being able to use type(X) instead of X.__class__, metaclasses, __new__, and more
|
# ? Apr 12, 2013 09:25 |
|
ARACHNOTRON posted:Huh. Okay this code worked with a different program and does not work with this one. The only difference is I already had a list of the _id's I wanted (there were only 25 as opposed to 2500) and could create a DB connection in the M class. Output: code:
Python code:
Python code:
Is it generally a cool thing that cool people do, though? Smarmy Coworker fucked around with this message at 16:07 on Apr 12, 2013 |
# ? Apr 12, 2013 14:33 |
|
ARACHNOTRON posted:And then it stops, no .pid file is made anywhere, doing python .py stop gives an error because it isn't running anymore. Python code:
|
# ? Apr 12, 2013 16:11 |
|
Interestingly that is the path most of the examples had in them. I looked and yeah there is totally no ttr in /dev/, but then what is the correct path?
|
# ? Apr 12, 2013 16:20 |
|
|
# ? May 9, 2024 05:27 |
|
Haha wow thanks. I just routed it to stdout andcode:
|
# ? Apr 12, 2013 16:24 |