Equivalent to perl -e

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Equivalent to perl -e

Chris Lasher
My professor and advisor has been "inspired" by me to give Python a
try. He's an avid Perl user, and challenged me with the following:

What is the Python equivalent to perl -e '<some oneliner>'?

Embarassingly, I had no answer, but I figure, someone on the list will
know. His use of Python is at stake; he threatened that, since he's
dependant enough on using perl -e within Emacs enough, if it can't be
done in Python, he won't take the language seriously. Help me, Python
Tutor, you're his only hope!

Thanks in advance,
Chris
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

Kent Johnson
Chris Lasher wrote:
> My professor and advisor has been "inspired" by me to give Python a
> try. He's an avid Perl user, and challenged me with the following:
>
> What is the Python equivalent to perl -e '<some oneliner>'?

python -c

More details here:
http://linuxcommand.org/man_pages/python1.html

Kent

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

Glenn T Norton
In reply to this post by Chris Lasher
Chris Lasher wrote:

>My professor and advisor has been "inspired" by me to give Python a
>try. He's an avid Perl user, and challenged me with the following:
>
>What is the Python equivalent to perl -e '<some oneliner>'?
>
>Embarassingly, I had no answer, but I figure, someone on the list will
>know. His use of Python is at stake; he threatened that, since he's
>dependant enough on using perl -e within Emacs enough, if it can't be
>done in Python, he won't take the language seriously. Help me, Python
>Tutor, you're his only hope!
>
>Thanks in advance,
>Chris
>_______________________________________________
>Tutor maillist  -  [hidden email]
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>
How about...
python -c "for x in 'Tell them to jump on board and take the blue pill':
print x"

Glenn

--
"Ketchup. For the good times... " - Ketchup Advisory Board
Glenn Norton
Application Developer
Nebraska.gov
1-402-471-2777
[hidden email]

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

Chris Lasher
Haha! I'll relay that message! Thanks Kent and Glenn!

Chris

On 10/15/06, Glenn T Norton <[hidden email]> wrote:

> Chris Lasher wrote:
>
> >My professor and advisor has been "inspired" by me to give Python a
> >try. He's an avid Perl user, and challenged me with the following:
> >
> >What is the Python equivalent to perl -e '<some oneliner>'?
> >
> >Embarassingly, I had no answer, but I figure, someone on the list will
> >know. His use of Python is at stake; he threatened that, since he's
> >dependant enough on using perl -e within Emacs enough, if it can't be
> >done in Python, he won't take the language seriously. Help me, Python
> >Tutor, you're his only hope!
> >
> >Thanks in advance,
> >Chris
> >_______________________________________________
> >Tutor maillist  -  [hidden email]
> >http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
> How about...
> python -c "for x in 'Tell them to jump on board and take the blue pill':
> print x"
>
> Glenn
>
> --
> "Ketchup. For the good times... " - Ketchup Advisory Board
> Glenn Norton
> Application Developer
> Nebraska.gov
> 1-402-471-2777
> [hidden email]
>
>
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

Tim Peters
In reply to this post by Chris Lasher
[Chris Lasher]
> My professor and advisor has been "inspired" by me to give Python a
> try. He's an avid Perl user, and challenged me with the following:
>
> What is the Python equivalent to perl -e '<some oneliner>'?

The initally attractive but unsatisfying answer is:

    python -c '<some oneliner>'

The reason it's "unsatisfying" is that Python isn't concerned with making:

    <some oneliner>

pleasant, or even sanely possible, for many tasks.  Perl excels at
one-liners; Python doesn't much care about them.

> Embarassingly, I had no answer, but I figure, someone on the list will
> know. His use of Python is at stake; he threatened that, since he's
> dependant enough on using perl -e within Emacs enough, if it can't be
> done in Python, he won't take the language seriously. Help me, Python
> Tutor, you're his only hope!

Like many Python (very) old-timers, I used Perl heavily at the time
Python came out.  As was also true for many of them, as time went on
the size of a new program I was willing to write in Perl instead of in
Python got smaller and smaller, eventually reaching "almost 0".  I
still use Perl some 15 years later, but now /only/ for "perl -e"-style
1-liners at an interactive shell.  If it takes more than a line, I
stick it in a module (and maybe a class) for reuse later.

Python's strengths are more in readability, helpful uniformity, easy
use of classes and rich data structures, and maintainability.  Cryptic
one-liners are in general (but not always) opposed to all of those.

So, ya, "python -c" exists, but your professor won't be happy with it.
 That's fine!  If one-liners are all he cares about, Perl is usually
the best tool for the job.
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

Chris Lasher
Points well taken. In fact, the example he demonstrated to me as a
one-liner was a regular expression as a line filter in
Emacs--essentially just a grep. There's no Pythonic equivalent to
this. Right tool for the right job, as you said. He was half-joking
about not learning Python if it lacked the option to execute snippets.

His lab maintains a significant amount of Perl code; he was intrigued
by my zealous enthusiasm for Python and my assertion that, personally,
I experienced greater long-term readability with my scripts written in
Python over those written in Perl. I think once he begins to
experience Python he will come to understand why it's not suited for
one-liners, and why that's a Good Thing.

Excellent reply!

Chris


On 10/15/06, Tim Peters <[hidden email]> wrote:

> [Chris Lasher]
> > My professor and advisor has been "inspired" by me to give Python a
> > try. He's an avid Perl user, and challenged me with the following:
> >
> > What is the Python equivalent to perl -e '<some oneliner>'?
>
> The initally attractive but unsatisfying answer is:
>
>     python -c '<some oneliner>'
>
> The reason it's "unsatisfying" is that Python isn't concerned with making:
>
>     <some oneliner>
>
> pleasant, or even sanely possible, for many tasks.  Perl excels at
> one-liners; Python doesn't much care about them.
>
> > Embarassingly, I had no answer, but I figure, someone on the list will
> > know. His use of Python is at stake; he threatened that, since he's
> > dependant enough on using perl -e within Emacs enough, if it can't be
> > done in Python, he won't take the language seriously. Help me, Python
> > Tutor, you're his only hope!
>
> Like many Python (very) old-timers, I used Perl heavily at the time
> Python came out.  As was also true for many of them, as time went on
> the size of a new program I was willing to write in Perl instead of in
> Python got smaller and smaller, eventually reaching "almost 0".  I
> still use Perl some 15 years later, but now /only/ for "perl -e"-style
> 1-liners at an interactive shell.  If it takes more than a line, I
> stick it in a module (and maybe a class) for reuse later.
>
> Python's strengths are more in readability, helpful uniformity, easy
> use of classes and rich data structures, and maintainability.  Cryptic
> one-liners are in general (but not always) opposed to all of those.
>
> So, ya, "python -c" exists, but your professor won't be happy with it.
>  That's fine!  If one-liners are all he cares about, Perl is usually
> the best tool for the job.
>
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

wesley chun
> His lab maintains a significant amount of Perl code

this sounds like a full-time job on its own.  everyone brings up a
good point... use the right tool for the job.  if it's one-liners,
that's what perl -e is made for.  for everything else that you
mentioned above, Python is the the one, preferably the only one
--obvious way to do it.  :-)

one perl'y thing that i *do* use is perl -c, for checking your syntax.
 i don't believe there's an equivalent way to do that from the Python
cmd-line (correct me if i'm wrong!), but i wrote up a short script I
called (pydashc.py) which just uses compile() to get the same
effect... very useful for when i don't want to *run* something to see
if i made any typos.

regards,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

David Rock
In reply to this post by Chris Lasher
* Chris Lasher <[hidden email]> [2006-10-15 22:07]:
> Haha! I'll relay that message! Thanks Kent and Glenn!
>

Here is one I actually use in real life.  I needed something to figure
out what the previous year, month, etc for rolling up old log files.
The best thing I could think of for date calculation was datetime.

This is embedded inside a shell script.

python -c '
import time
import datetime
dtup_now = time.localtime()
y,m,d = dtup_now[:3]
d_today = datetime.datetime(y,m,d)
d_delta = datetime.timedelta(d_today.day)
last_month = d_today - d_delta
d_delta = datetime.timedelta(last_month.day)
two_month = last_month - datetime.timedelta(last_month.day)
d_delta = datetime.timedelta(two_month.day)
del_month = two_month - datetime.timedelta(two_month.day)
print "%d %02d %d%02d" % (last_month.year, last_month.month, del_month.year, del_month.month)'

What you will notice is it gets complicated in a hurry if you try to do
loops or anything fancy because of formatting constraints.  Not that it
can't be done, but it would hurt to try. :-)

--
David Rock
[hidden email]
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

Alan Gauld

"David Rock" <[hidden email]> wrote in message

> This is embedded inside a shell script.
>
> python -c '
> import time
> import datetime
> dtup_now = time.localtime()
> y,m,d = dtup_now[:3]
> d_today = datetime.datetime(y,m,d)
> d_delta = datetime.timedelta(d_today.day)
> last_month = d_today - d_delta
> d_delta = datetime.timedelta(last_month.day)
> two_month = last_month - datetime.timedelta(last_month.day)
> d_delta = datetime.timedelta(two_month.day)
> del_month = two_month - datetime.timedelta(two_month.day)
> print "%d %02d %d%02d" % (last_month.year, last_month.month,
> del_month.year, del_month.month)'


Why?
Why not just put it in a Python script?
I'm missing something I think.

Alan G.

>
> What you will notice is it gets complicated in a hurry if you try to
> do
> loops or anything fancy because of formatting constraints.  Not that
> it
> can't be done, but it would hurt to try. :-)
>
> --
> David Rock
> [hidden email]
> _______________________________________________
> Tutor maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/tutor
>


_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

Hansen, Mike
In reply to this post by Chris Lasher
 

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of wesley chun
> Sent: Monday, October 16, 2006 12:51 AM
> To: Chris Lasher
> Cc: Python Tutor
> Subject: Re: [Tutor] Equivalent to perl -e
>
> > His lab maintains a significant amount of Perl code
>
> this sounds like a full-time job on its own.  everyone brings up a
> good point... use the right tool for the job.  if it's one-liners,
> that's what perl -e is made for.  for everything else that you
> mentioned above, Python is the the one, preferably the only one
> --obvious way to do it.  :-)
>
> one perl'y thing that i *do* use is perl -c, for checking your syntax.
>  i don't believe there's an equivalent way to do that from the Python
> cmd-line (correct me if i'm wrong!), but i wrote up a short script I
> called (pydashc.py) which just uses compile() to get the same
> effect... very useful for when i don't want to *run* something to see
> if i made any typos.
>
> regards,
> -- wesley

I had this same issue when I moved from Perl to Python. You might take a
look at pyflakes, pychecker, or pylint.

Mike
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

David Rock
In reply to this post by Alan Gauld
* Alan Gauld <[hidden email]> [2006-10-16 17:32]:
>
> Why?
> Why not just put it in a Python script?
> I'm missing something I think.

I don't think you are missing anything.  It was something that just sort
of happened one day.  I was trying to do something fairly simple in a
shell script and didn't have a good way to get the date info I wanted,
so I started playing around with python -e.  It really SHOULD just be a
python script that calls shell stuff, not the other way 'round :-)

Still, in the spirit of the OP, I thought it would be appropriate to
share.

--
David Rock
[hidden email]
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent to perl -e

nibudh
On 10/17/06, David Rock <[hidden email]> wrote:
* Alan Gauld <[hidden email]> [2006-10-16 17:32]:
>
> Why?
> Why not just put it in a Python script?
> I'm missing something I think.

I don't think you are missing anything.  It was something that just sort
of happened one day.  I was trying to do something fairly simple in a
shell script and didn't have a good way to get the date info I wanted,
so I started playing around with python -e.  It really SHOULD just be a
python script that calls shell stuff, not the other way 'round :-)


Hi, list.

This is my first post...

What i've found is that my quick and dirty oneliners can go from host to host with me and having them "all in one file" makes it easier to move around or share...

Recently i wrote a shell script called "hostingdata" that returns a report on a given domain name. It mostly stays on my monitoring server and i have broken out some of the functions into a seperate file so i can reuse them in other scripts.

However one of the helpdesk guys saw the script and he wanted to be able to run it from his computer, I bundled up the functions back into the main script and copied it into his ~/bin.

It just seemed easier that way. One needs to gauge whether doing this is likely to cause more problems than it's worth... but for small scripts with a small user base this would seem to work fine.

Cheers,

nibudh.


_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor