Wrap a function

J

Joan Miller

I believe you're working on Linux, so how about using "sed"? Here's a
(prettified) BASH transcript of a sed script (edit.sed) transforming a
6-line text file (myprog.py). The text file has both Python statements
and "special commands", which have "$ " at the beginning of the line.

 >>> cat myprog.py
print "hello"
$ ls -l
r = range(10)
$ grep foo bar.data
pass
print "bye"

 >>> cat edit.sed
s/^\$ \(.*\)/Run("\1")/

 >>> sed -f edit.sed data.txt
print "hello"
Run("ls -l")
r = range(10)
Run("grep foo bar.data")
pass
print "bye"

-John

Yes, this would a well solution. Simple and fast to build.
 
J

Joan Miller

        I shall blaspheme, and suggest that maybe the language you want to
use is REXX (ooREXX or Regina).

        By default, ANY statement that can not be confused for a REXX
language statement is sent to the currently defined command handler
(Which on most OSs is equivalent to Python's os.system() call; the late
Amiga, and IBM's mainframe OS had features that support defining other
applications as command handlers).

        A common practice is to put quotes about the first word of the
command to ensure it gets treated as external command.
I prefer Python since that has a great standard library where I get a
well logging system between another things. In addition, the main
intended audience will be for system administrators who many of them
already have used it.
 
D

Dennis Lee Bieber

Sounds like the REXX designers already got the blaspheming covered
when they came up with such an inelegant-sounding feature...
Ah, but on the original OS, and on the Amiga, it was quite an
elegant feature...

It meant any application that implemented a REXX command host
interface could be scripted by a REXX program -- AND that script was not
tied to just one application. It made REXX a multi-application scripting
language.

One could, fairly easily, script operations that could pull
paragraphs out of a word processor, and paste them into a page layout
program, using what was the equivalent of command line statements unique
to each.

I've not used REXX in some years -- Python is a cleaner syntax for
pure programming (REXX uses a "," as the continuation character, which
makes for weird statements when one continues a comma separated sequence
of parameters over multiple lines)... OTOH, my first real Python program
(on something predating 1.5.2) was a simple outgoing SMTPd on the Amiga
-- and I used REXX as the spooling mechanism from the client. If I'd
gotten fancier, I'd have had it going direct (since [forgive me, I've
forgotten your name] the person who ported Python to the Amiga DID
implement a REXX interface module).
 
A

Aahz

I shall blaspheme, and suggest that maybe the language you want to
use is REXX (ooREXX or Regina).

By default, ANY statement that can not be confused for a REXX
language statement is sent to the currently defined command handler
(Which on most OSs is equivalent to Python's os.system() call; the late
Amiga, and IBM's mainframe OS had features that support defining other
applications as command handlers).

How is that different from bash scripting?
 
D

Dan Stromberg

Joan said:
I've to call to many functions with the format:

run("cmd")

were "cmd" is a command with its arguments to pass them to the shell
and run it, i.e.

run("pwd")

or

run("ls /home")

Does anybody knows any library to help me to avoid the use of the main
quotes, and brackets?

I would to use anything as:

$ ls /home => run("ls /home")

or, at least

run pwd => run("pwd")
How about this?

def pwd(): return run("pwd")

pwd()

def ls(l=False, files=()):
args = []
if l: args.insert(0, '-l')
args.append(files)
return run("ls", args)

ls(l=True, "/foo")

There would be to make a function for each system command to use so it
would be too inefficient, and follow the problem with the quotes.

The best is make a parser into a compiled language
'disagree.

Best is to have a text file outside your program, in which you define
commands and symbolic names for those commands.

Then you have a python module which reads these commands and names, and
creates functions that invoke them via the specified name.

This way you get concise syntax, don't have to type as much boilerplate,
and don't add line noise.

Mixing two languages as though they were the same language greatly
increases the complexity of the original language with little gain.

Consider PowerShell - it's almost like two different languages smushed
together, and you have to know how a command was implemented to know how
to read it.

Also, what if someday The Powers That Be (the python language core
designers) decide they need to use $ for something? I hope they won't,
but if they do, your preprocessor might make quite a mess of it.
 
D

Dan Stromberg

Ben said:
I shall blaspheme, and suggest that maybe the language you want
to use is REXX (ooREXX or Regina).

Heh. That isn't blasphemy, because no true Pythonista [0] would claim
Python to be the god of that domain.

It's no sin to say that Python isn't a good choice for specific things;
and “I want to write programs by indistinguishably mixing statements
with external system calls†is one of them, IMO
From
http://stromberg.dnsalias.org/~dstromberg/debugging-with-syscall-tracers.html#terminology

A quick note on terminology: open() is typically a system call.
fopen is probably never a system call - instead, it is a function in
the C library that wraps open(), making open() easier to use. Then
there's the system() function - like fopen(), it isn't really a
system call, despite its name. Rather, it is a C library function
that typically will wrap the fork() and exec*() system calls.
 
D

Dennis Lee Bieber

How is that different from bash scripting?

Uhm... Does a bash script support inlining statements from some
other language?

Python's shutil library essentially disappears in REXX as one can
plug in the native command line statement for those functions.
 
A

Aahz

Uhm... Does a bash script support inlining statements from some
other language?

Python's shutil library essentially disappears in REXX as one can
plug in the native command line statement for those functions.


But in bash scripting, you'd just use rsync or cp or rm -- maybe an
example would make clearer how REXX differs from bash.
 
D

Dennis Lee Bieber

But in bash scripting, you'd just use rsync or cp or rm -- maybe an
example would make clearer how REXX differs from bash.

I suspect the only really good examples would have to written under
OS/VMS (where it originated, written to be a more powerful & friendlier
replacement for the EXEC scripting language) or AmigaOS -- to
demonstrate the switching interaction of command handlers. REXX
implementations for Windows and Linux pretty much only support the
"shell" as a command handler, whereas the two named OS could address
editors (and on the Amiga, word processors, desktop publishing programs,
terminal emulators/comm programs, system editor).

My Amiga's been in storage since Win95 days, so this is a fictitious
example based on the reference manuals.

address command /* use normal command shell to process commands */
file = 'some.file'
'run ed' file /* start ED editor in background, editing 'file'*/
address ED /* send commands to the ED instance */
'b' /* go to bottom of file */
'i /text to be inserted before bottom line/'
't' /* to to top */
'a /text inserted after first line/'
find = 'needle'
replace = 'thorn'
'rpe /' || find || '/' || replace '/'
'x' /* save & exit */
address command
'type' file /* type file to screen */
'filenote' file "edited via AREXX script"
 
A

Aahz

I suspect the only really good examples would have to written under
OS/VMS (where it originated, written to be a more powerful & friendlier
replacement for the EXEC scripting language) or AmigaOS -- to
demonstrate the switching interaction of command handlers. REXX
implementations for Windows and Linux pretty much only support the
"shell" as a command handler, whereas the two named OS could address
editors (and on the Amiga, word processors, desktop publishing programs,
terminal emulators/comm programs, system editor).

My Amiga's been in storage since Win95 days, so this is a fictitious
example based on the reference manuals.

address command /* use normal command shell to process commands */
file = 'some.file'
'run ed' file /* start ED editor in background, editing 'file'*/
address ED /* send commands to the ED instance */
'b' /* go to bottom of file */
'i /text to be inserted before bottom line/'
't' /* to to top */
'a /text inserted after first line/'
find = 'needle'
replace = 'thorn'
'rpe /' || find || '/' || replace '/'
'x' /* save & exit */
address command
'type' file /* type file to screen */
'filenote' file "edited via AREXX script"

IOW, kinda like AppleScript?
 
D

Dennis Lee Bieber

IOW, kinda like AppleScript?

Never seen AppleScript... Suspect REXX predates it.

Oh, definitely -- wikipedia gives AppleScript a fall 1993 release.
My ARexx manual is dated 1987 -- six years earlier; and IBM's release
was around 1982
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top