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


Groups > comp.lang.python > #94814

Re: How to re-write this bash script in Python?

Return-Path <rosuav@gmail.com>
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; 'python,': 0.02; 'subject:Python': 0.05; 'method.': 0.05; 'bash': 0.07; 'bug.': 0.07; 'host,': 0.07; 'works.': 0.07; 'cc:addr:python-list': 0.09; 'scripts': 0.09; 'subject:How': 0.09; 'command.': 0.09; 'input,': 0.09; 'lengths': 0.09; 'script,': 0.09; 'subject:script': 0.09; 'unexpectedly': 0.09; 'python': 0.10; 'output': 0.13; 'translate': 0.15; 'algorithmic': 0.16; 'bash,': 0.16; 'blindly': 0.16; 'describing': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'massively': 0.16; 'naive': 0.16; 'pair,': 0.16; 'scripts.': 0.16; 'wrote:': 0.16; 'translation': 0.16; 'instance,': 0.18; 'shell': 0.18; 'language': 0.19; 'changes': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'suggested': 0.20; 'work,': 0.21; 'do.': 0.22; 'sends': 0.22; 'pass': 0.22; 'am,': 0.23; 'code.': 0.23; 'seems': 0.23; 'needed.': 0.23; 'sat,': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'script': 0.25; 'command': 0.26; 'rest': 0.26; 'chris': 0.26; 'supported': 0.27; 'equivalent': 0.27; 'right.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'turns': 0.27; 'host': 0.28; 'invoke': 0.29; 'environment': 0.29; 'that.': 0.30; 'code': 0.30; 'option': 0.31; 'implement': 0.32; 'generally': 0.32; 'usually': 0.33; 'case,': 0.34; 'languages': 0.34; 'running': 0.34; 'received:google.com': 0.35; 'trouble': 0.35; 'configured': 0.35; 'execution': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'lines': 0.36; '(and': 0.36; 'basic': 0.36; 'notes': 0.36; "wasn't": 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'method': 0.37; 'things': 0.38; 'doing': 0.38; '(with': 0.38; 'experience,': 0.38; 'end': 0.39; 'goes': 0.39; 'data': 0.39; 'along': 0.39; 'subject:-': 0.39; 'well.': 0.40; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'programs': 0.62; 'matter': 0.63; 'more': 0.63; 'different': 0.63; 'goal': 0.64; 'between': 0.65; 'techniques': 0.65; "they're": 0.66; 'direct': 0.68; 'manner': 0.69; 'approaches': 0.72; 'carefully': 0.72; 'long-term': 0.72; 'special': 0.73; 'intelligent': 0.76; 'chrisa': 0.84; "op's": 0.84; 'piping': 0.84; 'presumably': 0.84; 'subject:write': 0.84; 'subject:this': 0.85; 'to:none': 0.91; 'edwards': 0.91; 'email...': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=MuAIkTVDrWuMwUB0j2IF49d4El7wl7epStMpY7iJJrc=; b=cF1KFHgs4+bulRh9XNeBJtRHknYM0BJ0WCwY7PwHsW+fraCW1feFYivy6xDNwg50Fg 4a0qjfDkK69TZVb18RkLRXIpO182Xp3MlRS3InSA7UoiFc/aqAvTk4zrpzP6WCTMmBJa YN9CYJ4s+04AROaRUJSA+wXy3wdYvYwC2aj3+wL2ym4dtoJ5Kz6gATg6pGf/7tOcRcmn mp9jC9y6SjeFOfXoBToGqpuoCV+H4JCK5czqepXX1z8gl+EG2k80AExUOdC9EK1h+AZQ mHDw80Ffx42/TSkUAqVI2J/vvQlY9Pt9aQbcypOHVV5WKeRJ5iSB5fMsfKSW9f0GNc3C +ZxQ==
MIME-Version 1.0
X-Received by 10.50.124.97 with SMTP id mh1mr6325353igb.92.1438354380711; Fri, 31 Jul 2015 07:53:00 -0700 (PDT)
In-Reply-To <mpg0ik$mfi$1@reader1.panix.com>
References <b07086dc-0946-4398-b621-23d564d54507@googlegroups.com> <mailman.1110.1438328836.3674.python-list@python.org> <mpg0ik$mfi$1@reader1.panix.com>
Date Sat, 1 Aug 2015 00:53:00 +1000
Subject Re: How to re-write this bash script in Python?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.1116.1438354383.3674.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1438354383 news.xs4all.nl 2962 [2001:888:2000:d::a6]:44067
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:94814

Show key headers only | View raw


On Sat, Aug 1, 2015 at 12:26 AM, Grant Edwards <invalid@invalid.invalid> wrote:
> On 2015-07-31, Chris Angelico <rosuav@gmail.com> wrote:
>
>> There are two basic approaches to this kind of job.
>>
>> 1) Go through every line of bash code and translate it into
>>    equivalent Python code. You should then have a Python script which
>>    blindly and naively accomplishes the same goal by the same method.
>
> In my experience, that works OK for C (with a little post-translation
> tweaking and re-factoring).  But, it's a pretty lousy method for bash
> scripts.  There are a lot of things that are trivial in Python and
> complex/hard in bash (and a few vice versa), so a direct translation
> usually turns out to be a mess.  You end up with a lot of Python code
> where only a couple lines are really needed. You also end up doing
> things in a bizarre manner in Python because the simple, easy, right
> way wasn't supported by bash.

Right. The two techniques I suggested can be generalized to any
language pair, but some work better this way than others do. Shell
scripts are something of a special case, because they're massively
optimized toward running other programs and piping output into input,
which applications languages like Python are not as good at; so the
naive transformation leads to code that goes to ridiculous lengths to
invoke five subprocesses and move data between them, where a more
intelligent approach might invoke one process, and then do the rest in
Python code. The trouble is, you really need to know what your code is
doing, because the non-naive transformation generally has a different
set of assumptions. For instance, the OP's shell script calls on the
'mailx' command. What's it do? Presumably it sends an email... well,
Python can do that. But what if the mailx command on this host has
been carefully configured to pass mail along via a specific relay
host, and that direct access on port 25 has been blocked? How would
you know? So it's not just a matter of translating the script, you
have to know its execution environment as well.

>> 2) Start by describing what you want to accomplish, and then
>>    implement that in Python, using algorithmic notes from the bash code.
>>
>> The second option seems like a lot more work, but long-term it often
>> isn't, because you end up with better code.
>
> And the code works. :)
>
> For bash, I really recommend 2)

Yeah. You remove the ability for environmental changes to unexpectedly
affect the script, which is often a feature and not a bug.

ChrisA

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


Thread

How to re-write this bash script in Python? sutanu.das@gmail.com - 2015-07-30 11:31 -0700
  Re: How to re-write this bash script in Python? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-07-30 20:36 +0100
  Re: How to re-write this bash script in Python? random832@fastmail.us - 2015-07-30 16:17 -0400
  Re: How to re-write this bash script in Python? Chris Angelico <rosuav@gmail.com> - 2015-07-31 17:47 +1000
    Re: How to re-write this bash script in Python? Grant Edwards <invalid@invalid.invalid> - 2015-07-31 14:26 +0000
      Re: How to re-write this bash script in Python? Chris Angelico <rosuav@gmail.com> - 2015-08-01 00:53 +1000

csiph-web