Wrap a function

J

Joan Miller

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

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

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")
 
J

Joan Miller

Check the docs on os.system().
No. I've a function that uses subprocess to run commands on the same
shell and so substitute to bash scrips. But a script full of run
("shell_command --with --arguments") is too verbose.
 
S

Steve Holden

Joan said:
No. I've a function that uses subprocess to run commands on the same
shell and so substitute to bash scrips. But a script full of run
("shell_command --with --arguments") is too verbose.

So rewrite the script to read the commands from a file and execute them
one by one?

regards
Steve
 
J

John Posner

No. I've a function that uses subprocess to run commands on the same
shell and so substitute to bash scrips. But a script full of run
("shell_command --with --arguments") is too verbose.

I'm suspicious of your original intent. Essentially, you want to write
code in which a literal string, such as ...

ls -l

.... is *not* enclosed in quotes. Why run the risk of creating confusion
(in other people who look at your code, in syntax-checking tools, etc.)
between variables and literals?

But I'm in sympathy with your desire to make the code as clean as
possible and to minimize the number of times you have to type a quote
character. My suggestions:

1. Create a function (say, "Run") that encapsulates as much of the
syntax as possible: os.system(), subprocess.call(), string-splitting,
whatever. So an invocation would look like this:

Run("ls -l *.txt")

(I think you've already done this step.)

2. Find a text editor that supports keyboard macros, so that a single
keystroke turns this text line:

ls -l *.txt

.... into this one:

Run("ls -l *.txt")

HTH,
John
 
J

Joan Miller

So rewrite the script to read the commands from a file and execute them
one by one?
I had thinked about that but the problem is that I would that were
mixed with python code, so can be get the output from a system command
and manipulate it from python
 
J

Joan Miller

I'm suspicious of your original intent. Essentially, you want to write
code in which a literal string, such as ...

   ls -l

... is *not* enclosed in quotes. Why run the risk of creating confusion
(in other people who look at your code, in syntax-checking tools, etc.)
between variables and literals?
Yes but to that code could be prepend a sign as '$' to be identified
and so be parsed.
But I'm in sympathy with your desire to make the code as clean as
possible and to minimize the number of times you have to type a quote
character. My suggestions:

1. Create a function (say, "Run") that encapsulates as much of the
syntax as possible: os.system(), subprocess.call(), string-splitting,
whatever. So an invocation would look like this:

   Run("ls -l *.txt")

(I think you've already done this step.)
Yes, I made a funtion very cool to call to system commands, that works
well with pipes and passes the variables (i.e. "LANG=C grep -e 'foo' /
home")
2. Find a text editor that supports keyboard macros, so that a single
keystroke turns this text line:

   ls -l *.txt

... into this one:

   Run("ls -l *.txt")
This is not what I'm looking for. I'm supposing that could be solved
with a DSL or a macro library, any?
 
S

Sean DiZazzo

Yes but to that code could be prepend a sign as '$' to be identified
and so be parsed.







Yes, I made a funtion very cool to call to system commands, that works
well with pipes and passes the variables (i.e. "LANG=C grep -e 'foo' /
home")





This is not what I'm looking for. I'm supposing that could be solved
with a DSL or a macro library, any?

Python is not perl.

Thank God/Guido.
 
P

Peter

I'm suspicious of your original intent. Essentially, you want to write
code in which a literal string, such as ...

   ls -l

... is *not* enclosed in quotes. Why run the risk of creating confusion
(in other people who look at your code, in syntax-checking tools, etc.)
between variables and literals?

But I'm in sympathy with your desire to make the code as clean as
possible and to minimize the number of times you have to type a quote
character. My suggestions:

1. Create a function (say, "Run") that encapsulates as much of the
syntax as possible: os.system(), subprocess.call(), string-splitting,
whatever. So an invocation would look like this:

   Run("ls -l *.txt")

(I think you've already done this step.)

2. Find a text editor that supports keyboard macros, so that a single
keystroke turns this text line:

   ls -l *.txt

... into this one:

   Run("ls -l *.txt")

HTH,
John

I can't see you avoiding quotes etc, but extending on John's comment,
the obvious next step would be to run everything in a loop i.e. place
all the commands into a list and create a loop that ran each command
in the list.

Almost all editors support macros - most editors support some form of
language sensitive editing (NOT the prompt call parameters style but
rather help with the syntax via a 'form' style of fill-in) that will
allow you to reduce typing effort. But macros would be the first and
easiest choice for this activity.

Peter
 
J

Joan Miller

I can't see you avoiding quotes etc, but extending on John's comment,
the obvious next step would be to run everything in a loop i.e. place
all the commands into a list and create a loop that ran each command
in the list.
Yes, but could be necessary that were mixed with python code.
Almost all editors support macros - most editors support some form of
language sensitive editing (NOT the prompt call parameters style but
rather help with the syntax via a 'form' style of fill-in) that will
allow you to reduce typing effort. But macros would be the first and
easiest choice for this activity.
The goal of my program is substitute to bash scripts, so the macros in
editors are irrelevant fo this one.
 
J

Joan Miller

Yes, but could be necessary that were mixed with python code.


The goal of my program is substitute to bash scripts, so the macros in
editors are irrelevant fo this one.

I think that the best solution that I've is to build a program that
parses the script to convert *$ command* to run("command") before of
be called by python.
 
S

Steve Holden

Yingjie said:
We all know that Python is dynamically typed, and dynamically typed languages are generally slower than statically typed ones. I wonder if it is possible at all for Python to mix statically-typed-ness with dynamically-typed-ness to boost up its speed a little bit, especially when speed is needed. For example, you define a function like this:

def speed(float dist, float time):
return dist/time

then the compiler would generate code to first check parameter types (or even do some casts if appropriate, say cast an int into float) in the beginning of this function. and the rest of the function would then be compiled with the assumption that 'dist' and 'time' are of the type float.

Of course, dynamically-typed-ness is still the same as before. Python is well known for providing multiple programming paradigms, I wonder if we could also sneak this in nicely.

Any thoughts?
Google for "Python function annotations": the features you want are
already there in the language specification.

regards
Steve
 
J

Jonathan Gardner

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


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




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")
 
J

Joan Miller

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

were "cmd" is a command with its arguments to pass them to the shell
and run it, i.e.
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
 
J

John Posner

I think that the best solution that I've is to build a program that
parses the script to convert *$ command* to run("command") before of
be called by python.

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.
print "hello"
$ ls -l
r = range(10)
$ grep foo bar.data
pass
print "bye"

s/^\$ \(.*\)/Run("\1")/

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

-John
 
J

Jonathan Gardner

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

Yeah, you could do that. Or you can simply rely on /bin/sh to do the
parsing and everything else for you. No need to re-invent the wheel. I
don't think Python will ever beat sh as a shell replacement.

When people say that Python is great for some situations, but not so
much for others, I think they thought of running commands like this as
"other",
 
A

alex23

Joan Miller said:
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")

It's not a library, but IPython[1] provides a lot of what you're
after:

IPython 0.9.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints
more.

In [1]: ls /home
bb/ dcallan/ ehornsby/ ftp/ uqamartl/ uqckorte/ uqmtrev2/

In [2]: path = '/home'

In [3]: ls $path
bb/ dcallan/ ehornsby/ ftp/ uqamartl/ uqckorte/ uqmtrev2/

In [4]: output = !ls $path

In [5]: output
Out[5]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: bb
1: dcallan
2: ehornsby
3: ftp
4: uqamartl
5: uqckorte
6: uqmtrev2

In [6]: output[6]
Out[6]: 'uqmtrev2'

You'll need to run your scripts with IPython, so this may not be a
solution if you plan on distributing them.

[1] http://ipython.scipy.org/
 
D

Dennis Lee Bieber

No. I've a function that uses subprocess to run commands on the same
shell and so substitute to bash scrips. But a script full of run
("shell_command --with --arguments") is too verbose.

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.
 
C

Chris Rebert

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

Sounds like the REXX designers already got the blaspheming covered
when they came up with such an inelegant-sounding feature...
       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.

Cheers,
Chris
 
J

Joan Miller

Yeah, you could do that. Or you can simply rely on /bin/sh to do the
parsing and everything else for you. No need to re-invent the wheel. I
don't think Python will ever beat sh as a shell replacement.

When people say that Python is great for some situations, but not so
much for others, I think they thought of running commands like this as
"other",

I started to working on this project (Scripy [1]) because I wanted to
hacking cryptsetup in my ubuntu. The funcionts to manage its
initialization are in bash and it goes to be non-maintainable code,
cryptic and very hard to debug (as whatever bash script of medium
size). Here you have the beast:

http://bazaar.launchpad.net/~ubuntu...ic/annotate/head:/debian/cryptdisks.functions

Using Scripy I can debug easily the commands run from the shell, and
log all if I would.

Now, thanks to Scripy I've created a script for easily disks
partitioning [2] using a simple cofiguration in YAML [3]. The great
thing is that I can add volumes and encrypted partitions :)

The only problem is that it's too verbose but I could rename *run()*
by a simple function as *_()* or *r()*


[1] http://bitbucket.org/ares/scripy/src/
[2] http://bitbucket.org/ares/scripypartition/src/tip/lib/scripy/part/disk.py#cl-22
[3] http://bitbucket.org/ares/scripypartition/src/tip/bin/init_crypto.py#cl-46
 

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

Forum statistics

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

Latest Threads

Top