Mon, 19 Nov 2007, by [EMAIL PROTECTED]:
>
> On Tue, 2007-11-20 at 00:03 +0100, Theo v. Werkhoven wrote:
> > Mon, 19 Nov 2007, by [EMAIL PROTECTED]:
> >
> > > For all you scripting gurus out there. Can you help me out?
> > >
> > > I'm trying to convert a value to an output to the user of
> > > minutes:seconds.
> > >
> > > For example:
> > > if $A=100 (for seconds)
> > > Then echo "This is 1:40 minutes"
> > >
> > > How would I do this?
> >
> > $ A=100
> > $ python -c 'import time,sys; \
> > print "This is %d:%d minutes" % \
> > (time.localtime(float(sys.argv[2]))[4:6])' - $A
> > This is 1:40 minutes
>
> I have no idea what I am seeing, but it was just sweet enough to work!
> THANKS!
Not so difficult.
-c tells the python interpreter to run the command or script as supplied.
import time,sys tells python to use these two modules
There is a commend, pydoc, which can be used to see what a python module does,
so:
$ pydoc time
Help on module time:
NAME
time - This module provides various functions to manipulate time
values.
FILE
/usr/lib/python2.5/lib-dynload/time.so
MODULE DOCS
http://www.python.org/doc/current/lib/module-time.html
DESCRIPTION
[..]
Functions:
[..]
localtime() -- convert seconds since Epoch to local time tuple
[..]
FUNCTIONS
localtime(...)
localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,\
tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
$ pydoc sys
Help on built-in module sys:
NAME
sys
FILE
(built-in)
MODULE DOCS
http://www.python.org/doc/current/lib/module-sys.html
DESCRIPTION
This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.
Dynamic objects:
argv -- command line arguments; argv[0] is the script pathname if known
Thus the module time, called with function localtime(100), returns a
list of values, of which tm_min (the 5th) and tm_sec (the 6th) are
of interest. Because python counts from 0, and the intersection
returns [from:to-1] the values in the list that you want are
numbered [4:6].
localtime() wants a floating point number, thus the number from
stdin is converted to float first.
According to python's man-page, when python is called with -c and
there are arguments after the commands, argv[0] holds '-c', argv[1]
holds the first argument, argv[2] the 2nd etc. (My bad, the dash
before $A was not needed afterall).
This list (1,40) is fed to the printf style command, which prints
the list as two seperate numbers.
Python is a very usefull and powerfull language, with which you can
do anything from these small oneliners to multi million Euro projects.
The beauty of it is, that it's almost impossible to write unreadable
code in Python.
Theo
--
Theo v. Werkhoven Registered Linux user# 99872 http://counter.li.org
ICBM 52 13 26N , 4 29 47E. + ICQ: 277217131
SUSE 10.2 + Jabber: [EMAIL PROTECTED]
Kernel 2.6.20 + See headers for PGP/GPG info.
Claimer: any email I receive will become my property. Disclaimers do not apply.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]