[Tutor] Interacting with stderr

Crush crushed26 at gmail.com
Thu Aug 28 02:28:58 CEST 2014


As far as sending the contents of stderr to a tmp file...hmmm, i will try that. My only concern is, many of these systems run live demo feeds for many days...10+. Im afraid the file will fill up very quickly and present problems. 

Stderr=PIPE causes the terminal to hang. I can not wait for the process to terminate, i need to read stderr as it is being fed data.. Would fifos be of any use here? 

I will research the tempfile module.

Bo 

> On Aug 27, 2014, at 7:38 PM, tutor-request at python.org wrote:
> 
> Send Tutor mailing list submissions to
>    tutor at python.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
>    https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>    tutor-request at python.org
> 
> You can reach the person managing the list at
>    tutor-owner at python.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
> 
> 
> Today's Topics:
> 
>   1. Re: Python Programming (Alan Gauld)
>   2. Re: simple unicode question (Alan Gauld)
>   3. Interacting with stderr (Crush)
>   4. Re: Interacting with stderr (Cameron Simpson)
>   5. Re: debug and execute python code in Mac (Alan Gauld)
>   6. Re: Interacting with stderr (Alan Gauld)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Wed, 27 Aug 2014 19:57:12 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Python Programming
> Message-ID: <ltl9m8$m45$1 at ger.gmane.org>
> Content-Type: text/plain; charset=windows-1252; format=flowed
> 
>> On 27/08/14 14:40, Jake wrote:
>> To whom it may concern,
>> My name is Jake and I have recently started the GCSE computing course with school.
> 
>> answera = input()
>> if answera == ["Oslo" or "oslo"]:
> 
> This doesn't do what you think it does.
> 
> ["Oslo" or "oslo"]  is a list
> 
> "Oslo" or "oslo"   is the content of the list and
> is a boolean expression which evaluates to True.
> (Each non-empty string is considered True by Python)
> 
> So your 'if' line looks to Python like:
> 
> if answera == [True]:
> 
> But answera is a string so it will never equal a list
> with a single boolean value so you go to the else
> clause.
> 
> A better way to do what you want is to convert the input
> to lowercase using the string lower() method and compare
> that to the string you want, like so:
> 
> if answera.lower() == "oslo":
> 
> If you need to check multiple possible answers you can use
> a list of strings and the 'in' operator like this:
> 
> if answera.lower() in ['amsterdam', 'london', 'oslo']:
> 
> HTH
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
> 
> 
> 
> ------------------------------
> 
> Message: 2
> Date: Wed, 27 Aug 2014 20:09:36 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] simple unicode question
> Message-ID: <ltladg$uva$1 at ger.gmane.org>
> Content-Type: text/plain; charset=utf-8; format=flowed
> 
> On 27/08/14 15:57, Albert-Jan Roskam wrote:
> 
>>> if *fewer* than 1 in 10 operations will raise an exception, then use
>>> try...except; but if *more* than 1 in 10 operations will raise an
>>> exception, and it is safe to do so, then LBYL may be appropriate.
>> 
>> 
>> Thanks a lot, Steven! This kind of stuff should be in a book.
> 
> It is in lots of books but they aren't Python books; they are generic 
> software engineering books. Because this is not really a language 
> specific type design issue, its general software design.
> 
>> Do you know of any real life examples where the code is written
>> in such a way that it could switch from AFTP to LBYL
> 
> It would be very unlikely since it would almost always be a case of 
> premature optimisation. You would need to be inside a very tight loop to 
> make that kind of optimisation worthwhile in real world scenarios.
> (And the cost of evaluating it would probably outweigh any gains.)
> 
> Much more important is to make the code correct and maintainable than to 
> worry about slight performance issues before they are a proven issue.
> Building those kind of self modifying control flows makes code much more 
> error prone and difficult to modify.
> 
> The vast majority of performance issues in software are to do with 
> blockages in IO processing(network, disk access, human input, etc)
> or database processing (big data or complex searches) or in traversing 
> complex/deep data structures. Very few real-world performance issues
> are related to simple code execution issues.
> 
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
> 
> 
> 
> ------------------------------
> 
> Message: 3
> Date: Wed, 27 Aug 2014 18:45:41 -0400
> From: Crush <crushed26 at gmail.com>
> To: "tutor at python.org" <tutor at python.org>
> Subject: [Tutor] Interacting with stderr
> Message-ID: <77745CA4-8F7D-410C-800D-F0A3C711D2F2 at gmail.com>
> Content-Type: text/plain; charset="us-ascii"
> 
> Hello, it has been a while and I hope I am sending to the correct email. 
> 
> How would I go about running a conditional statement against the contents of stderr. For instance, if "blah blah blah" is in stderr do X, else do Y. 
> 
> CODE: SELECT ALL
> 
> #!/usr/bin/env python
> import subprocess
> from subprocess import PIPE
> i = 0
> while i < 10:
>    p = subprocess.call("avconv -v verbose -re -analyzeduration 1000 -i http://localhost:6498/ms2/1382097438004/0MediaPlayer+0+/octoshape+hVV+octolive.americaone.com+V+aone+V+live+V+ONECONNXT_DEMO1_HD_flv/aoneliveONECONNXTDEMO1HDflv  -c:v rawvideo -c:a pcm_s16le -ar 48000 -f nut - | ./bmdplay -m 12 -f pipe:0", shell=True)
>    i += 1
>    if i == 10:
>        print "Gave up"
> 
> 
> The above code works and I get no tracebacks, however I need to add to it and check the contents of stderr. Avconv by default sends the audio and video to stdout, which then sends the signal to a capture card in the machine. Avconv is sending the status of the broadcasted signal i.e. frames, time, fps, etc. to stderr, which is displayed in the terminal. 
> 
> Bo 
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20140827/05ab01b4/attachment-0001.html>
> 
> ------------------------------
> 
> Message: 4
> Date: Thu, 28 Aug 2014 08:55:39 +1000
> From: Cameron Simpson <cs at zip.com.au>
> To: "tutor at python.org" <tutor at python.org>
> Subject: Re: [Tutor] Interacting with stderr
> Message-ID: <20140827225539.GA55624 at cskk.homeip.net>
> Content-Type: text/plain; charset=us-ascii; format=flowed
> 
>> On 27Aug2014 18:45, Crush <crushed26 at gmail.com> wrote:
>> Hello, it has been a while and I hope I am sending to the correct email.
>> 
>> How would I go about running a conditional statement against the contents of
>> stderr. For instance, if "blah blah blah" is in stderr do X, else do Y.
>> 
>> 
>>   #!/usr/bin/env python
>>   import subprocess
>>   from subprocess import PIPE
>>   i = 0
>>   while i < 10:
>>       p = subprocess.call("avconv -v verbose -re -analyzeduration 1000 -i http://localhost:6498/ms2/1382097438004/0MediaPlayer+0+/octoshape+hVV+octolive.americaone.com+V+aone+V+live+V+ONECONNXT_DEMO1_HD_flv/aoneliveONECONNXTDEMO1HDflv -c:v rawvideo -c:a pcm_s16le -ar 48000 -f nut - | ./bmdplay -m 12 -f pipe:0",
> shell=True)
>>       i += 1
>>       if i == 10:
>>           print "Gave up"
>> 
>> The above code works and I get no tracebacks, however I need to add to it and
>> check the contents of stderr. Avconv by default sends the audio and video to
>> stdout, which then sends the signal to a capture card in the machine. Avconv
>> is sending the status of the broadcasted signal i.e. frames, time, fps, etc.
>> to stderr, which is displayed in the terminal.
> 
> Send stderr to a file, using a parameter like:
> 
>   stderr=open('stderr.out', 'w')
> 
> in your call() call. Then open 'stderr.out' and look for the relevant 
> information.
> 
> Obviously in a "real" program you'd take care to make that a temporary file 
> with a unique name using the functions from the "tempfile" module, etc. But 
> make it work first.
> 
> Cheers,
> Cameron Simpson <cs at zip.com.au>
> 
> The Few. The Proud. The Politically Incorrect.  - Steve Masticola
> 
> 
> ------------------------------
> 
> Message: 5
> Date: Thu, 28 Aug 2014 00:31:08 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] debug and execute python code in Mac
> Message-ID: <ltlpns$sic$1 at ger.gmane.org>
> Content-Type: text/plain; charset=windows-1252; format=flowed
> 
> On 27/08/14 13:47, Patrick Thunstrom wrote:
> 
>>> I do not like the cmd/terminal execution. Thanks !
>> 
>> I don't know many ways to debug code that don't involve the terminal
>> in some fashion. Even with a good IDE you're going to have a terminal
>> built in for output.
> 
> Not necessarily. A text display widget within a GUI is very different 
> from a text based terminal. Many graphical debuggers display data 
> structures in a hierarchical model where you can expand/close the 
> various levels etc.
> 
> But I think the OP is specifically wanting to avoid command prompts. The 
> question is: Does he mean all prompts (including
> Python's) or just the OS prompt? If its all prompts then it gets
> much harder to find an IDE that will suit.
> 
> 
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
> 
> 
> 
> ------------------------------
> 
> Message: 6
> Date: Thu, 28 Aug 2014 00:38:09 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Interacting with stderr
> Message-ID: <ltlq51$v4s$1 at ger.gmane.org>
> Content-Type: text/plain; charset=windows-1252; format=flowed
> 
>> On 27/08/14 23:45, Crush wrote:
>> Hello, it has been a while and I hope I am sending to the correct email.
>> 
>> How would I go about running a conditional statement against the
>> contents of stderr. For instance, if "blah blah blah" is in stderr do X,
>> else do Y.
> 
> You can access stderr just like you access stdout.
> The recommended way is via communicate:
> 
> ---------------
> Popen.communicate(input=None, timeout=None)
> 
> Interact with process: Send data to stdin. Read data from stdout and 
> stderr, until end-of-file is reached. Wait for process to terminate. The 
> optional input argument should be data to be sent to the child process, 
> or None, if no data should be sent to the child. The type of input must 
> be bytes or, if universal_newlines was True, a string.
> 
> communicate() returns a tuple (stdoutdata, stderrdata).
> 
> Note that if you want to send data to the process?s stdin, you need to 
> create the Popen object with stdin=PIPE. Similarly, to get anything 
> other than None in the result tuple, you need to give stdout=PIPE and/or 
> stderr=PIPE too.
> --------------------
> 
> HTH
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
> 
> 
> 
> ------------------------------
> 
> Subject: Digest Footer
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
> 
> 
> ------------------------------
> 
> End of Tutor Digest, Vol 126, Issue 69
> **************************************


More information about the Tutor mailing list