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


Groups > comp.lang.python > #29427 > unrolled thread

Re: 'indent'ing Python in windows bat

Started byDavid Smith <davids@invtools.com>
First post2012-09-18 09:03 -0400
Last post2012-09-19 16:09 -0400
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: 'indent'ing Python in windows bat David Smith <davids@invtools.com> - 2012-09-18 09:03 -0400
    Re: 'indent'ing Python in windows bat Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-19 11:22 +0200
      Re: Re: 'indent'ing Python in windows bat David Smith <davids@invtools.com> - 2012-09-19 08:27 -0400
      Re: 'indent'ing Python in windows bat Terry Reedy <tjreedy@udel.edu> - 2012-09-19 14:18 -0400
      Re: Re: 'indent'ing Python in windows bat David Smith <davids@invtools.com> - 2012-09-19 16:09 -0400

#29427 — Re: 'indent'ing Python in windows bat

FromDavid Smith <davids@invtools.com>
Date2012-09-18 09:03 -0400
SubjectRe: 'indent'ing Python in windows bat
Message-ID<mailman.870.1347973403.27098.python-list@python.org>
Thank you all. Roy Smith gets the most thanks, though he didn't answer 
my general question -- he showed me how to look at that specific 
structure differently. Terry Reedy might get thanks for her idea if I 
can ever figure the correct escape sequences that will make both windows 
and the Python interpreter happy. Bat makes bash/sed combos look like a 
breeze...

I thought you guys wouldn't want a treatise about WHY I was doing it 
this way and left it at one sentence. For whatever record, this is the 
sentence most missed.
> I'm converting windows bat files little by little to Python 3 as I find time and learn Python.

I COULD stop doing all my other work to learn Python and convert all the 
batch files in one fell swoop. Efficiency? Fast way to get fired. Better 
to fit this in during the many small breaks I have. That's how the bat 
files were built over time in the first place. Or this email.

I COULD break down each batch file and write dozens of mini python 
scripts to be called. I already have a few, too. Efficiency? Speed is 
bad, but these are bat files, after all. The cost of trying to work with 
a multitude of small files is high, though, and I realized I had better 
go to a mix.

Some sections can be broken down to one liners. Efficiency? Speed is 
terrible, but it's far faster than typing commands. OTOH, I have the 
organization I need on the original bat file, which is slowly being 
rem'ed out. As I learn and have the time, the one-liners will melt 
together into a py file to be called from the bat file. Eventually, the 
bat will disappear back into the broken Window from whence it came.

Ugly, eh? I have under my belt scads of different languages from Fortran 
(using JCL!), Pascal, C++ to bash, sed, awk to Forth, assembly and a 
large cast of others. No big deal. My brain and Python, however, do NOT 
mix. I have been trying to learn the thing for over a decade and figure 
this will either force my brain into seeing the heart of the beast, or 
be swallowed in the attempt.

Bat files are ugly cripples, but even on Windows a two-legged quick and 
dirty dog is better than mistake-prone typing and button clicking. After 
conversion, I'm aiming to make these erstwhile ugly cripples fly when I 
find the time and as I stuff more Python down my gullet.

I agree. For those who have the unbroken time and understanding of 
Python, this is idiotic.

back to work,

[toc] | [next] | [standalone]


#29476

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2012-09-19 11:22 +0200
Message-ID<k3c2st$l6l$1@r03.glglgl.gl>
In reply to#29427
Am 18.09.2012 15:03 schrieb David Smith:

> I COULD break down each batch file and write dozens of mini python
> scripts to be called. I already have a few, too. Efficiency? Speed is
> bad, but these are bat files, after all. The cost of trying to work with
> a multitude of small files is high, though, and I realized I had better
> go to a mix.

In order to achieve this, it might be very useful to either have a 
module for each (bigger) part to be achieved which you can call with

     python -m modulename arg1 arg2 arg3

and putting the Python code into modulename.py.

Or you have one big "interpreter" which works this way:

class Cmd(object):
     """
     Command collector
     """
     def __init__(self):
         self.cmds = {}
     def cmd(self, f):
         # register a function
         self.cmds[f.__name__] = f
         return f
     def main(self):
         import sys
         sys.exit(self.cmds[sys.argv[1]](*sys.argv[2:]))

cmd = Cmd()

@cmd.cmd
def cmd1(arg1, arg2):
     do_stuff()
     ...
     return 1 # error -> exit()

@cmd.cmd
def cmd2():
     ...

if __name__ == '__main__':
     cmd.main()


This is suitable for many small things and can be used this way:

bat cmds
python -m thismodule cmd1 a b
other bat cmds
python -m thismodule cmd2
...

HTH,

Thomas

[toc] | [prev] | [next] | [standalone]


#29486

FromDavid Smith <davids@invtools.com>
Date2012-09-19 08:27 -0400
Message-ID<mailman.913.1348057641.27098.python-list@python.org>
In reply to#29476
On 2012-09-19 05:22, Thomas Rachel wrote:
> Am 18.09.2012 15:03 schrieb David Smith:
>
>> I COULD break down each batch file and write dozens of mini python
>> scripts to be called. I already have a few, too. Efficiency? Speed is
>> bad, but these are bat files, after all. The cost of trying to work with
>> a multitude of small files is high, though, and I realized I had better
>> go to a mix.
>
> In order to achieve this, it might be very useful to either have a
> module for each (bigger) part to be achieved which you can call with
...

> Or you have one big "interpreter" which works this way:
>
> class Cmd(object):
>      """
>      Command collector
>      """
...
...
>
> This is suitable for many small things and can be used this way:
...
> Thomas

Thomas,
Beautiful. Gotta love it. I'll see if I can get the "interpreter" going. 
I particularly like it because I will be able to copy and paste 
wholesale when I stitch the final product back together again. Many thanks.

Going back to the one-liner, I discovered the following individual lines 
work:
print('hi')
if 1: print('hi')
print('hi');print('hi2')
if 1: print('hi');print('hi2')

but not:
print('hi');if 1: print('hi')

Chokes on the 'if'. On the surface, this is not consistent.

I'll drop the one-liners for now since I have something that I can work 
with as I learn to wrestle with Python.

thanks again.

[toc] | [prev] | [next] | [standalone]


#29512

FromTerry Reedy <tjreedy@udel.edu>
Date2012-09-19 14:18 -0400
Message-ID<mailman.932.1348078726.27098.python-list@python.org>
In reply to#29476
On 9/19/2012 8:27 AM, David Smith wrote:

> but not:
> print('hi');if 1: print('hi')
>
> Chokes on the 'if'. On the surface, this is not consistent.

Yes it is. ; can only be followed by simple statements. The keyword for 
compound statememts must be the first non-indent token on a line. That 
is why I suggested at the beginning of the thread to insert '\n', 
stating correctly that it works for exec().

 >>> exec("print('hi');if 1: print('hi')")
Traceback (most recent call last):
   File "<pyshell#0>", line 1, in <module>
     exec("print('hi');if 1: print('hi')")
   File "<string>", line 1
     print('hi');if 1: print('hi')
                  ^
SyntaxError: invalid syntax
 >>> exec("print('hi');\nif 1: print('hi')")
hi
hi
 >>> exec("print('hi')\nif 1: print('hi')")
hi
hi

Someone raised the issue of whether the bat interpreter passes along the 
quoted string unchanged or if it interprets '\' or '\n' itself and in 
the latter case whether one to do anything so that python will see '\n' 
after any fiddling by the bat interpreter. It seems that \ is not 
interpreted within strngs by bat, but the problem is that the string is 
then seen by python as code, not as a string literal, and so python does 
not 'cook' it either. Running tem.bat from a command line (which echoes 
line from .bat), so I see the output, I get (Win7)

C:\Programs\Python33>python -c "print(1)\nif 1: print(2)"
   File "<string>", line 1
     print(1)\nif 1: print(2)
                            ^
SyntaxError: unexpected character after line continuation character

One gets the same response interactively from
 >>> print('hi')\nif 1: print('hi')
or
 >>> exec("print('hi')\\nif 1: print('hi')")

The fix is to quote and pass the exact code that worked above in the 
python shell, keeping in mind that the outer quotes must be the double 
quote characters recognized by windows.

C:\Programs\Python33>python -c "exec('print(1)\nif 1: print(2)')"
1
2

I did check that windows % interpolation of .bat args works within '' 
quoted strings. Change tem.bat to
python -c "exec('print(%1)\nif 1: print(2)')"
and calling 'tem 3' prints
3
2

That said, if you have many multiline statements, putting them in a 
separate file or files may be a good idea.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#29525

FromDavid Smith <davids@invtools.com>
Date2012-09-19 16:09 -0400
Message-ID<mailman.939.1348085390.27098.python-list@python.org>
In reply to#29476
On 2012-09-19 14:18, Terry Reedy wrote:
> stating correctly that it works for exec().

My mistake. I fancied you were talking shell, not python. I now see that 
Python 3 has exec() as a built-in.

python -c "exec('print(\"hi\")\nif 0:\n print(\"hi\")\nelif 1:\n 
print(\"hi2\")')"
worked right off the *.bat. Shades of sed!
Note I used a one space indentation. A tab works fine, too.

> python -c "exec('print(%1)\nif 1: print(2)')"
> and calling 'tem 3' prints
> 3
> 2
Thanks for the exhaustive study. :-) I'll keep it in mind. I hope I 
don't have to do this, though.

> That said, if you have many multiline statements, putting them in a
> separate file or files may be a good idea.

ASAP I'm hoping to have each bat swallowed completely by python. My 
current "bathon" or "pytch" file closes an old session then opens the 
session I select just like the bat mom used to bake.

Thank you again, Terry, and thanks to all -- even the *nix'ers. Might 
come in handy if I get back into that again.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web