Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Spelter
Jul 18, 2007
Are you puissant?

Cicero posted:

I'm using the subprocess module to execute shell commands inside a python script. I want the stdout from these new processes to basically shut up because they're really verbose but the print statements inside my python script to continue working. Right now it seems to be either both or none. I'm working on windows if that matters.

Probably best to just redirect stderr and stdout of the command you're running to NUL, like this:
code:
with open(os.devnull, 'w') as f_null:
    returncode = subprocess.call("somecommand", stdout = f_null, stderr = subprocess.STDOUT)
Since it uses os.devnull it should work on Windows and UNIX. This won't affect the output of your script, just the subprocess.

Adbot
ADBOT LOVES YOU

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

Cicero posted:

I'm using the subprocess module to execute shell commands inside a python script. I want the stdout from these new processes to basically shut up because they're really verbose but the print statements inside my python script to continue working. Right now it seems to be either both or none. I'm working on windows if that matters.

I had a similar issue with a batch processing routine, and ended up doing this:

code:
from subprocess import call

(create sh_script1.sh)

log_proc1=open("sh_script1.log","w")
call(["sh","sh_script1.sh"],stdout=sh_proc1)
I liked this approach because it gave me the benefit of having the output from the routines I was running somewhere in a log file. It became very useful for tracking down errors that prop up when processing a data set that doesn't behave as expected.

EDIT: oh damnit, missed that there was a new page. I like my method a litle better, but it depends on how useful the output would be for you.

Lurchington
Jan 2, 2003

Forums Dragoon

Cicero posted:

I'm using the subprocess module to execute shell commands inside a python script. I want the stdout from these new processes to basically shut up because they're really verbose but the print statements inside my python script to continue working. Right now it seems to be either both or none. I'm working on windows if that matters.

I've never actually had subprocess commands outputting anything unless I specifically tell it to. Setting the stderr and stdout kwargs to subprocess.PIPE gives you a file-like object that has the output streams with no issues.

Here's the PMOTW on subprocess, and it's pretty simple: http://blog.doughellmann.com/2007/07/pymotw-subprocess.html

On the other handle if you're just blindly calling subprocess.call everywhere, then yeah you'd get the command output interfering with your print statements

tef
May 30, 2004

-> some l-system crap ->
In subprocess, the child inherits stdout from the parent by default.

Try passing stdout=subprocess.PIPE into the subprocess command

http://docs.python.org/library/subprocess.html

When you do this, the popen object returned will have a stdout attribute you can read from and close, like a file handle.

duck monster
Dec 15, 2004

tef posted:

By the way, it is a bag of functions and variables as implemented within python. Python is not message style OO like Smalltalk, Obj-C or Ruby, it is closer to Simula style OO.


If you want to write in smalltalk so much, just write in smalltalk :-)
Its a bag of functions and variables in smalltalk too, if you really think about it. The diffrence amounts to syntactical sugar, really.

It doesn't matter. I'm talking about how to think in OO, and modelling the problem is pretty much your canonical approach.

Doctor Claw
Dec 25, 2007
I'll get you next time Gadget - next time!
As the OP hasn't been updated in nearly a year and a half, what is the best online resource for learning Python? I know HTML, CSS, PHP, Visual Basic (heh) and C++, I'm looking to learn Python for fun.

spankweasel
Jan 4, 2006

Doctor Claw posted:

As the OP hasn't been updated in nearly a year and a half, what is the best online resource for learning Python? I know HTML, CSS, PHP, Visual Basic (heh) and C++, I'm looking to learn Python for fun.

I constantly point folks who have a programming background at the MIT course in the OP. It's very well done and doesn't really go into the tedium of: "This is an if-statement ... This is a for-loop ..." because (I assume) MIT assumes that people know the ultra basics of programming, even in the intro level.

Yoshimo
Oct 5, 2003

Fleet of foot, and all that!

spankweasel posted:

I constantly point folks who have a programming background at the MIT course in the OP. It's very well done and doesn't really go into the tedium of: "This is an if-statement ... This is a for-loop ..." because (I assume) MIT assumes that people know the ultra basics of programming, even in the intro level.

What would be the best guide for people who DID want all that if-statement/for-loop information?

TasteMyHouse
Dec 21, 2006

Yoshimo posted:

What would be the best guide for people who DID want all that if-statement/for-loop information?

http://learnpythonthehardway.org/

spankweasel
Jan 4, 2006

TasteMyHouse posted:

http://learnpythonthehardway.org/

this. a thousand times this.

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

TasteMyHouse posted:

http://learnpythonthehardway.org/

Seconding this. I didn't learn python this way the first time, but when I went through this, I learned so much more about Python than I ever did by just going seat of the pants of "how the hell do I do this"??

Harold Ramis Drugs
Dec 6, 2010

by Y Kant Ozma Post
I stuck on the last part of a major project for my Python programming class. The program we're writing has to do all of the following things:

1.) Use a card object defined by the teacher to determine the value of a 5 card poker hand containing 5 cards.

2.) Take a numeric (positive integer) input from the user and repeat step 1 as many times. Record all of the hands to a text file

3.) Perform a simple statistical analysis of all of the hands collected from steps 1 and 2, and record this to a text file as well.

4.) If the user enters an input on step 2 that's not a positive integer, prompt the user to re-enter the input.

I can't for the life of me figure out part 4. Right now, the first part of my code looks like this:
code:
def main():
    
    xlimit = int(input('How many poker hands would you like to analyze? '))
    sfcount = 0
    fkcount = 0
    fhcount = 0
    fcount = 0
    scount = 0
    tkcount = 0
    tpcount = 0
    pcount = 0
    hccount = 0
    for a in range (xlimit):
        hand =[]
        for x in range(5):
            c = Card()
            for card in hand:
                while c.rank == card.rank and c.suit == card.suit:
                    c = Card()
            hand.append(c)
            hand.sort(key=lambda Card: Card.rank)

Is there a way I could put an 'if' or a 'while' statement on the first couple lines of code that will recursively restart the main function if the first input is not a positive integer?

Note: this is just the first couple lines of code, I don't see the need to post the entire thing.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Harold Ramis Drugs posted:

Is there a way I could put an 'if' or a 'while' statement on the first couple lines of code that will recursively restart the main function if the first input is not a positive integer?
Yep!

(before you ask "how?", you do it just like you said to do it)

You could also put the input into its own procedure, which returns when the user has entered a positive integer.

duck monster
Dec 15, 2004

What are peoples recomendations on editors. I cant keep pycharm stable on Lion anymore with its constant lockups, and for the loving life of me I cant get eclipse to recognise its python path source directories for django. I'm losing days of work to this poo poo and its driving me loving insane.

e: Oh great aptana is just an eclipse rebrand with that crazy retarded virtual file-structure poo poo. Just give me a directory listing you over-engineering java wingnuts grrrrrrrrr

e2: Ok, 15 minutes of "rebuilding workspace" later, I think Aptana just failed the audition. :/ Any other recomendations for a non eclipse or IDEA based python editor that "gets" django?

e3: BBedit cant have different docs in a split window. Delete. :suicide: What else is there?

e4: Oh hey jEdit is ugly as gently caress, but is stable , highlights python and html/js properly, has a multi-doc split windows and appears to just work. You'll do , you ugly little text editor! Shame about the eye-searing font though :/

e5: Oh hey sublime-text is pretty drat sweet!

duck monster fucked around with this message at 08:27 on Aug 9, 2011

Harold Ramis Drugs
Dec 6, 2010

by Y Kant Ozma Post

Janin posted:

Yep!

(before you ask "how?", you do it just like you said to do it)

You could also put the input into its own procedure, which returns when the user has entered a positive integer.

I'm feeling pretty retarded now, and I've been trying different types of 'while' and 'if' loops to get this working. Could you please show me how to make an input statement that will recursively restart the main function if any non-integer or integer < 1 is entered? I'm kicking myself in the head right now.


VVVVV Thanks VVVVVV

Harold Ramis Drugs fucked around with this message at 06:53 on Aug 9, 2011

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Harold Ramis Drugs posted:

I'm feeling pretty retarded now, and I've been trying different types of 'while' and 'if' loops to get this working. Could you please show me how to make an input statement that will recursively restart the main function if any non-integer or integer < 1 is entered? I'm kicking myself in the head right now.

Recursive main, checking for negative integers:

code:
def main():
  xlimit = int(input('How many poker hands would you like to analyze? '))
  if xlimit < 1:
    return main()
Separate function, with while loop:

code:
def input_positive_integer():
  while True:
    xlimit = int(input('How many poker hands would you like to analyze? '))
    if xlimit >= 1:
      return xlimit

def main():
  xlimit = input_positive_integer()
Separate function, also handling non-numeric inputs:

code:
def input_positive_integer():
  while True:
    raw = raw_input('How many poker hands would you like to analyze? ')
    try:
      xlimit = int(raw)
      if xlimit >= 1:
        return xlimit
    except ValueError:
      pass

Lurchington
Jan 2, 2003

Forums Dragoon

duck monster posted:

What are peoples recomendations on editors. I cant keep pycharm stable on Lion anymore with its constant lockups, and for the loving life of me I cant get eclipse to recognise its python path source directories for django. I'm losing days of work to this poo poo and its driving me loving insane.

e: Oh great aptana is just an eclipse rebrand with that crazy retarded virtual file-structure poo poo. Just give me a directory listing you over-engineering java wingnuts grrrrrrrrr

e2: Ok, 15 minutes of "rebuilding workspace" later, I think Aptana just failed the audition. :/ Any other recomendations for a non eclipse or IDEA based python editor that "gets" django?

e3: BBedit cant have different docs in a split window. Delete. :suicide: What else is there?

e4: Oh hey jEdit is ugly as gently caress, but is stable , highlights python and html/js properly, has a multi-doc split windows and appears to just work. You'll do , you ugly little text editor! Shame about the eye-searing font though :/

e5: Oh hey sublime-text is pretty drat sweet!

It's pretty barebones, but I like TextWrangler when doing one-off scripting.

I run PyCharm 1.5.3 on Lion (new mac mini) with no issues at all so I'm confused what's happening to you. :(

VVV PyScripter is alright in general, and since duck monster is on Mac, I'll mention that I was able to get it *working* with Wine on mac

Lurchington fucked around with this message at 21:11 on Aug 9, 2011

_aaron
Jul 24, 2007
The underscore is silent.

duck monster posted:

What are peoples recomendations on editors.
I've had pretty decent luck with PyScripter. I haven't done much complicated work though, and I don't use any "advanced" features, so YMMV.

edit: ^^ Totally missed that he wanted something Mac-specific. Oh well; I'll leave it here for others who want a nice, free, Windows-only (or Wine, apparently) editor.

_aaron fucked around with this message at 01:11 on Aug 10, 2011

TasteMyHouse
Dec 21, 2006
MacVim

Grabulon
Jul 25, 2003

People all over the internet recommend Dive into Python, but it's not popular around here, right?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Grabulon posted:

People all over the internet recommend Dive into Python, but it's not popular around here, right?

It's terrible.

madkapitolist
Feb 5, 2006
Hi guys I am very new to python and I am trying to write a script that pulls the newest file (this server gets a data dump every hour) from a sftp sever. I played around with the paramiko module and was able to write a script to pull a file of a certain name but I am completely lost when it comes to determining the newest file. Im not sure where to start, and I supposed to interpret os.stat? The files do have naming scheme which incorporates time/date etc etc. Any tutorials out there? Thanks

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
I recommend you use Fabric instead of paramiko, it has a nicer API built on top of paramiko. I've been using it to remote deploy our server stuff and it's really nice.

Lurchington
Jan 2, 2003

Forums Dragoon
I'd recommend paramiko when all you need is to log in and execute a remote script and maybe get the output. Otherwise I agree that Fabric is better for the heavier stuff. For what it's worth, it sounds like rsync with ssh keys is basically what you want?

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah

duck monster posted:

What are peoples recomendations on editors. I cant keep pycharm stable on Lion anymore with its constant lockups, and for the loving life of me I cant get eclipse to recognise its python path source directories for django. I'm losing days of work to this poo poo and its driving me loving insane.

e: Oh great aptana is just an eclipse rebrand with that crazy retarded virtual file-structure poo poo. Just give me a directory listing you over-engineering java wingnuts grrrrrrrrr

e2: Ok, 15 minutes of "rebuilding workspace" later, I think Aptana just failed the audition. :/ Any other recomendations for a non eclipse or IDEA based python editor that "gets" django?

e3: BBedit cant have different docs in a split window. Delete. :suicide: What else is there?

e4: Oh hey jEdit is ugly as gently caress, but is stable , highlights python and html/js properly, has a multi-doc split windows and appears to just work. You'll do , you ugly little text editor! Shame about the eye-searing font though :/

e5: Oh hey sublime-text is pretty drat sweet!
Coda is pretty great. Maybe TextMate?

They're both worth the price. TextMate is more flexible plugin-wise but Coda's integrated SFTP is way better than the available TextMate plugins.

duck monster
Dec 15, 2004

I'm pretty happy with Sublime text 2 at the moment. It seems to hit my feature list loving nicely and it appears to "get" django pretty well.

mdxi
Mar 13, 2006

to JERK OFF is to be close to GOD... only with SPURTING

I just started my first project of decent scale in Python. This, of course, means testing. Except that I really didn't like the look of unittest, and nose, and py.test (AKA JUnit). So I ported a teensy testing lib that I wrote for Javascript, and added a runner/harness script. If you're familiar with Perl's Test::More, it'll look familiar.

https://github.com/firepear/nanotest-py/

Let me know if it's of use to you, and/or if you have concrete requests for improvement.

Profane Obituary!
May 19, 2009

This Motherfucker is Dead

duck monster posted:

I'm pretty happy with Sublime text 2 at the moment. It seems to hit my feature list loving nicely and it appears to "get" django pretty well.

I'm trying out ST2 also, At the moment I like it over pycharm somewhat, I still have some problems with it over pycharm, like it's horrible spell checker, the fact that it doesn't seem to be able to handle virtualenvs/tab completion (least I havn't found out how yet), not saving your open files when switching projects, and no tab complete inside of strings for django settings.

tripwire
Nov 19, 2004

        ghost flow

Thermopyle posted:

It's terrible.

Really? I just compared it and it is a lot more sensible and readable than Zed Shaw's self congratulatory wank fest.

devilmouse
Mar 26, 2004

It's just like real life.

duck monster posted:

I'm pretty happy with Sublime text 2 at the moment. It seems to hit my feature list loving nicely and it appears to "get" django pretty well.

It's also written in python, if you want to extend it. The API is pretty nice too. If I used a GUI, it'd be Sublime 2!

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES
I've used IDLE and had no real issues with it. I know it's not liked very much in Python editor circles though.

Aquamacs might be a good alternative, and really Aquamacs is awesome as it is.

Angryhead
Apr 4, 2009

Don't call my name
Don't call my name
Alejandro




Hello everyone, quite new to Python/programming in general and I'm trying to remove certain characters from a string.
code:
allLines = allLines.replace('+', ' ')
allLines = allLines.replace('.', ' ')
allLines = allLines.replace('>', ' ')
and so on
This gets the job done, but I'm guessing that there's some kind of way to do it more efficiently.
Using Python 3.2 in case that matters.

Red Mike
Jul 11, 2011
code:
chars = '+.>'
for char in chars:
    line.replace(char,' ')
This is a somewhat readable solution. You could even use some sort of one-liner that involves map(), I believe. Not that it'd help readability.

tef
May 30, 2004

-> some l-system crap ->

Angryhead posted:

Hello everyone, quite new to Python/programming in general and I'm trying to remove certain characters from a string.

This gets the job done, but I'm guessing that there's some kind of way to do it more efficiently.
Using Python 3.2 in case that matters.

Regex (aka regular expressions) are a powerful way to process text.

http://docs.python.org/py3k/library/re.html#re.sub

YO MAMA HEAD
Sep 11, 2007

I resisted learning regexes (regeces?) for a long time (I'm a total hobbyist) until I did some screen scraping and really needed them. I like Red Mike's solution to this problem (and it's probably a handy reminder that you can iterate through strings so easily) but don't wait as long as I did to dive into regex!

Graviton v2
Mar 2, 2007

by angerbeet
3rd'd, the syntax is a bit rough (english slang meaning difficult) but skys the limit once you get over that hump.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

tripwire posted:

Really? I just compared it and it is a lot more sensible and readable than Zed Shaw's self congratulatory wank fest.

Not that I keep up with beginner python books (for example, I don't know who the hell Zed Shaw is or what book you're talking about), but when I was first learning python and programming (neglecting the fact that I learned QBasic 20 years ago) a couple years ago, Dive into Python was very irritating.

I'm not alone in my feeling that it's not very good:

quote:

Dive Into Python isn't just bad because of the use of ODBC, it's also just full of bad initial examples. Take a look at your first Python program and boggle at all the bizarre stuff a beginner has to suddenly comprehend:

A function, with a giant doc comment right away.
Weird underscore variables with a bad font making it look like one underscore.
A list comprehension for-loop to join a string using a format string off a tuple.
A dict, formatted with backslash chars that aren't even needed.
Holy crap, how in the hell is that a good first example? Even worse is it starts a trend within the book of using ODBC as a theme to teach Python.

I get that everyone learns in different ways, but it certainly seems to me that most people won't find Dive into Python a very good beginners book.

FWIW, I struggled with Dive Into Python for awhile before switching to Python for Software Design - How to Think Like a Computer Scientist and breezing through it and learning a ton.

Graviton v2
Mar 2, 2007

by angerbeet
I found the offical documentation a bit poo poo to be honest. Got lots more out of the community in general. Go fire up that browser!

Yay
Aug 4, 2007

Thermopyle posted:

I'm not alone in my feeling that it's not very good
Even if you don't know who he is, you know you're citing Zed Shaw there, in a discussion of whether (Zed Shaw's) LPTHW is a self-congratulatory wank fest, or a better source than Dive into Python?

I too, think the actual python docs are pretty dreadful. At least they've improved; <=2.5 docs were a nightmare both to navigate and read - the new sphinx docs are a godsend, in that respect.

Adbot
ADBOT LOVES YOU

Kimasu v2.0
Jan 19, 2001
Forum Veteran
Since we're talking about LPTHW, I started reading just for fun. I've been doing the "extra credit", but I am stuck on Example 17:

code:
from sys import argv
from os.path import exists

script, from_file, to_file = argv

input = open(from_file)
indata = input.read()

output = open(to_file, 'w')
output.write(indata)

output.close()
input.close()
Copy from one file to another. In the extra credit he says that he could do the whole program with one line of code. Well, I got the main part narrowed down to one line, but then I cannot figure out how to close the files.

code:
from sys import argv
from os.path import exists

script, from_file, to_file = argv

open(to_file, 'w').write(open(from_file).read())
All of the documentation I can find on close() seems to need a variable assigned to it, as I tried to_file.close() but that didn't work. What am I missing? My code runs and works just fine, but I'm assuming it is leaving the file open, correct?

  • Locked thread