Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: 'indent'ing Python in windows bat (http://www.velocityreviews.com/forums/t952371-re-indenting-python-in-windows-bat.html)

David Smith 09-18-2012 01:03 PM

Re: 'indent'ing Python in windows bat
 
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,

Thomas Rachel 09-19-2012 09:22 AM

Re: 'indent'ing Python in windows bat
 
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

Terry Reedy 09-19-2012 06:18 PM

Re: 'indent'ing Python in windows bat
 
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


David Smith 09-19-2012 08:09 PM

Re: Re: 'indent'ing Python in windows bat
 
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.



All times are GMT. The time now is 05:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.