Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90586

Re: use of subprocess module inside generator

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gherron@digipen.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'parameters': 0.04; 'output': 0.05; 'root': 0.05; 'assignment': 0.07; 'none:': 0.07; 'advance': 0.07; 'below)': 0.09; 'caller': 0.09; 'converted': 0.09; 'expected.': 0.09; 'here?': 0.09; 'newly': 0.09; 'overwrite': 0.09; 'pyodbc': 0.09; 'restriction': 0.09; 'statements': 0.09; 'subject:module': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'times,': 0.14; 'windows': 0.15; '(via': 0.16; 'bug,': 0.16; 'function).': 0.16; 'generator.': 0.16; 'helps.': 0.16; "microsoft's": 0.16; 'oddity': 0.16; 'preserved': 0.16; 'procedure?': 0.16; 'shifts': 0.16; 'stdout': 0.16; 'subject:generator': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'looked': 0.18; 'module': 0.19; 'file,': 0.19; 'seems': 0.21; 'command': 0.22; 'appears': 0.22; 'code,': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'certainly': 0.24; 'either.': 0.24; "i've": 0.25; '(see': 0.26; 'this:': 0.26; 'pass': 0.26; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'quickly': 0.29; 'change,': 0.30; 'returned': 0.30; "i'm": 0.30; 'code': 0.31; 'fine,': 0.31; 'gary': 0.31; 'minor': 0.31; 'probably': 0.32; 'guess': 0.33; 'sense': 0.34; 'skip:_ 10': 0.34; 'problem': 0.35; 'skip:u 20': 0.35; 'something': 0.35; 'but': 0.35; 'accessing': 0.36; 'doubt': 0.36; 'module.': 0.36; 'yield': 0.36; 'done': 0.36; 'next': 0.36; 'so,': 0.37; 'received:10': 0.37; 'server': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'easy': 0.60; 'future': 0.60; 'around.': 0.60; 'hope': 0.61; 'new': 0.61; 'simply': 0.61; "you've": 0.63; 'making': 0.63; 'kind': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'spot': 0.65; 'charset:windows-1252': 0.65; 'within': 0.65; 'hours': 0.66; 'latest': 0.67; 'importantly,': 0.68; 'institute': 0.72; 'received:10.10': 0.74; 'goal': 0.75; 'hoping': 0.75; 'received:204': 0.75; 'behavior': 0.77; 'dr.': 0.77; '100%': 0.77; 'directed': 0.83; 'flexible,': 0.84; 'apparent': 0.91
Date Wed, 13 May 2015 14:58:37 -0700
From Gary Herron <gherron@digipen.edu>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: use of subprocess module inside generator
References <5553A959.1070305@sbcglobal.net>
In-Reply-To <5553A959.1070305@sbcglobal.net>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.463.1431554327.12865.python-list@python.org> (permalink)
Lines 87
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431554327 news.xs4all.nl 2920 [2001:888:2000:d::a6]:38903
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90586

Show key headers only | View raw


On 05/13/2015 12:43 PM, Peter wrote:
>
> I'm using Python 3.4.3 on Windows 7 (with latest patches) to develop a 
> sqlcmd module for accessing SQL Server (via Microsoft's sqlcmd.exe).  
> My goal is to develop a 100% Python 3 module that's easy to use, 
> flexible, and by design shifts the majority of future SQL Server 
> Python database access maintenance to Microsoft.  So far, I've 
> successfully and quickly converted a few of my Python pyodbc 
> applications/tools to use this new module.  However, I recently ran 
> into an apparent Python issue which cost me a few hours to diagnose 
> and work around.

I doubt that you've hit a bug -- we've all done this kind of thing many 
times, and there's certainly no restriction on making procedure calls 
within a generator -- so that's probably not the problem either.

My guess is that you've misinterpreted the failure of the original 
code.  I don't know how that might be, but I do spot one oddity in your 
original code which may be responsible.  See below ...

>
> I'm hoping that someone might know what the root cause of my issue 
> was.  Perhaps I've hit a bug/restriction with Python generators?
>
> My original generator function looked like this:
>
> def _raw_data(cl, stdout, *, opath=None, timeout=timeout):
>     stdout = subprocess.check_output(cl, universal_newlines=True, 
> timeout=timeout)

This seems muddled -- you pass in a parameter, stdout, only to 
immediately overwrite its value with the output of check_output. What 
was in stdout originally, and more importantly, did you expect the newly 
assigned value from check_output to be returned to the calling 
procedure?  If so, that's your bug, because parameters in function calls 
don't work that way.

This makes sense with your workaround, since the assignment to stdout is 
preserved when done outside the function.

I hope that helps.

Gary Herron



>
>     if opath is None:
>         for line in stdout.splitlines():
>             yield line.strip()
>     else:
>         with open(opath) as f:
>             for line in f:
>                 yield line.strip()
>
> The above function appeared to work fine, if the command line directed 
> sqlcmd.exe to send its output to stdout.  However, if the command line 
> directed sqlcmd.exe to send its output to a file, 
> subprocess.check_output would never be called when next was called on 
> the returned generator.  I verified this behavior with print 
> statements inside my code, as well as, inside the subprocess module.
>
> My work around was to simply move the call to subprocess.check_output 
> outside of the generator function (see below) to its caller (a 
> non-generator function).  With this minor change, everything appears 
> to work as expected.  OK, so am I missing something here?
>
> def _raw_data(stdout, *, opath=None):
>     if opath is None:
>         for line in stdout.splitlines():
>             yield line.strip()
>     else:
>         with open(opath) as f:
>             for line in f:
>                 yield line.strip()
>
> Thank you in advance for your assistance.
>
> Peter Santoro


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: use of subprocess module inside generator Gary Herron <gherron@digipen.edu> - 2015-05-13 14:58 -0700

csiph-web