Re: 'indent'ing Python in windows bat

Discussion in 'Python' started by David Smith, Sep 18, 2012.

  1. David Smith

    David Smith Guest

    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,
    David Smith, Sep 18, 2012
    #1
    1. Advertising

  2. 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
    Thomas Rachel, Sep 19, 2012
    #2
    1. Advertising

  3. David Smith

    Terry Reedy Guest

    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
    Terry Reedy, Sep 19, 2012
    #3
  4. David Smith

    David Smith Guest

    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.
    David Smith, Sep 19, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Smith

    'indent'ing Python in windows bat

    David Smith, Sep 18, 2012, in forum: Python
    Replies:
    0
    Views:
    127
    David Smith
    Sep 18, 2012
  2. Dave Angel

    Re: 'indent'ing Python in windows bat

    Dave Angel, Sep 18, 2012, in forum: Python
    Replies:
    0
    Views:
    141
    Dave Angel
    Sep 18, 2012
  3. Ian Kelly
    Replies:
    2
    Views:
    131
    Hans Mulder
    Sep 19, 2012
  4. Dennis Lee Bieber

    Re: 'indent'ing Python in windows bat

    Dennis Lee Bieber, Sep 18, 2012, in forum: Python
    Replies:
    0
    Views:
    154
    Dennis Lee Bieber
    Sep 18, 2012
  5. Terry Reedy

    Re: 'indent'ing Python in windows bat

    Terry Reedy, Sep 18, 2012, in forum: Python
    Replies:
    0
    Views:
    251
    Terry Reedy
    Sep 18, 2012
Loading...

Share This Page