The Voidspace Techie Blog

Gravatar The big thing for me about shells is that you need to be able to write the long one-liner once in a while, and Pythons syntax gets in the way. The best you could hope for is something like:

listdir('.') >> lambda i: (x for x in i if x.date.before('30 May 2007') >> printit

- Paddy.


Gravatar I find that ipython satisfies a good chunk of my shell needs and I rarely resort to a command prompt or cygwin. For example:

ils | ifilter('mdate > datetime.now() - timedelta(days=30)')

Also, the ils pipe allows arguments like ils('../foo'). More here:

http://ipython.scipy.org/moin/UsingIPipe

You can create custom classes which support ipipe too:

http://ipython.scipy.org/moin/ Su...SupportingIPipe

-Ali


Gravatar "The best you could hope for is something like: listdir('.') >> lambda i: (x for x in i if x.date.before('30 May 2007') >> printit"

Uhm... did you read my example?


Gravatar What happened to:

[printit(x) for x in listdir('.') if filedate(x) < '3/30/2007']

It just seems more 'pythonic' to me...


Gravatar @Darren

Suppose you have a set of data that you want to pass through several filters that also transform the data and then perform an action on each record.

With commandlets you can do:

some_data >> filter1 >> filter2 >> action

With traditional list comprehensions this would look like:

[filter2(filter1(action(x))) for x in some_data if filter1(x) is not ignored and filter2(x) is not ignored]

Which looks more Pythonic? Note that to use a transforming filter, each record has to go through the filter *twice* with the list comprehension as transforming and filtering have to be done separately.

To retain the exact same semantics you ought to do:

step1 = [filter1(x) for x in some_data if filter1(x) is not ignored]
[action(x) for x in step1 if filter2(x) is not ignored]

Rolling that exactly into a single list comprehension means one big-ass ugly list comprehension.


Gravatar This would be very cool integrated into Kamaelia, which already uses a unix shell-like metaphor for connecting cooperating processes.


Gravatar http://geophile.com/osh/ does something quite similar, although the syntax differs quite a bit; from the homepage: "Osh processes streams of Python objects using simple commands. Complex data processing is achieved by command sequences in which the output from one command is passed to the input of the next. This is similar to composing Unix commands using pipes. However, Unix commands pass strings from one command to the next, and the commands (grep, awk, sed, etc.) are heavily string-oriented. Osh commands send primitive Python types such as strings and numbers; composite types such as tuples, lists and maps; objects representing files, dates and times; or even user-defined objects."


Gravatar I was about to mention osh (which I wrote) but found that Mr. Hartl beat me to it, (thanks!)

I just want to point out in addition that osh works both from the command line and from within python. For example, to find all foobar processes from the command line:

osh f 'processes()' ^ expand ^ select 'p: "foobar" in p.command_line()' ^ f 'p: (p.pid(), p.command_line())' $

Or from the API:

osh(f(processes()), expand(), select(lambda p: "foobar" in p.command_line()), f(lambda p: (p.pid(), p.command_line()), out())


Gravatar Cool stuff, but I don't understand why you've disallowed calling the function with no arguments in Cmdlet.__call__.
Cmdlets might have local state that needs initialising but require no args.

For example:
http://pastebin.com/f7d72cbd5
(Sorry about the link, I couldn't find code tags for this comment system)


Gravatar Ha. I didn't disallow it, just didn't think of it.


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan