[Tutor] Python best practices

2009-11-27 Thread Stefan Lesicnik
hihi!  (two in 1 day!)

Is there a document, pep, wiki etc that defines best practice for python code?  
(maybe its more generic).

I keep stumbling on things I think, it would be nice if someone mentioned this. 
Some things ive seen

- keep you try blocks as small as possible, so you can isolate the error
- functions should return one value (im not 100% of this one)
- declare variable names in a certain way (place?)
- use the if __name__ == '__main__': main() convention

im sure there are tons more, that i only figure out after stumbling through 
them. does anyone know if its written down, or stumbling is just part of the 
learning process?

stefan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode mapping doesn't work

2009-11-27 Thread Lie Ryan

On 11/27/2009 12:06 PM, Alan Gauld wrote:

Huh?! Was this to the right place?
It doesn't seem to be related to the previous posts in the thread?

Confused

Alan G.



whoops.. wrong thread...


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python on multicore machines

2009-11-27 Thread OkaMthembo
Hi All,

Is there a python implementation that takes advantage of all cores on modern
multicore machines?

-- 
Regards,
Lloyd
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] socket timeout

2009-11-27 Thread Sander Sweers
2009/11/27 Stefan Lesicnik :
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> #s.setdefaulttimeout(1)
> s.connect((proxy,port))

I have never used socket but a quick look at the docs [1] my guess is
that you should use use s.settimeout() [2].

The docs say that setdefaulttimeout [3] will only be applied to *new*
socket object. You set the default after creating the socket object.

Greets
Sander

[1] http://docs.python.org/library/socket.html
[2] http://docs.python.org/library/socket.html#socket.socket.settimeout
[3] http://docs.python.org/library/socket.html#socket.setdefaulttimeout
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread Lie Ryan

On 11/27/2009 8:57 PM, OkaMthembo wrote:

Hi All,

Is there a python implementation that takes advantage of all cores on
modern multicore machines?


yes, Cpython if you used "multiprocessing". Ironpython and Jython 
doesn't have GIL. Unladen Swallow plans to remove GIL.


Many minor python branch have been attempted to remove GIL, just as many 
are rejected for merging back to the main trunk since they slow down 
single threading. You might be able to find these patches from the 
internet and download the appropriate python source, patch them, and get 
python minus GIL.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python best practices

2009-11-27 Thread Wayne Werner
On Fri, Nov 27, 2009 at 2:03 AM, Stefan Lesicnik  wrote:

> hihi!  (two in 1 day!)
>
> Is there a document, pep, wiki etc that defines best practice for python
> code?  (maybe its more generic).
>
>
 This one is fairly comprehensive:
http://www.python.org/dev/peps/pep-0008/

HTH,
Wayne

I keep stumbling on things I think, it would be nice if someone mentioned
> this. Some things ive seen
>
> - keep you try blocks as small as possible, so you can isolate the error
> - functions should return one value (im not 100% of this one)
> - declare variable names in a certain way (place?)
> - use the if __name__ == '__main__': main() convention
>
> im sure there are tons more, that i only figure out after stumbling through
> them. does anyone know if its written down, or stumbling is just part of the
> learning process?
>
> stefan
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread Kent Johnson
On Fri, Nov 27, 2009 at 4:57 AM, OkaMthembo  wrote:
> Hi All,
>
> Is there a python implementation that takes advantage of all cores on modern
> multicore machines?

Presumably you mean something like, "Is there a python implementation
that can run multiple compute-bound processes on multiple cores
concurrently."

Some options:
- the multiprocessing module in the std lib - here is an example of
using it with numpy:
http://folk.uio.no/sturlamo/python/multiprocessing-tutorial.pdf

- Jython and IronPython both have threading models that allow multiple
threads to run concurrently on multiple processors.

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread OkaMthembo
Sorry to double -post, Lie. Didn;t "Reply to all"..

Thats interesting - is Unladen Swallow production-ready? I've been over the
site briefly, but i can't decide whether it's still in incubation or not.

On Fri, Nov 27, 2009 at 3:04 PM, Lie Ryan  wrote:

> On 11/27/2009 8:57 PM, OkaMthembo wrote:
>
>> Hi All,
>>
>> Is there a python implementation that takes advantage of all cores on
>> modern multicore machines?
>>
>
> yes, Cpython if you used "multiprocessing". Ironpython and Jython doesn't
> have GIL. Unladen Swallow plans to remove GIL.
>
> Many minor python branch have been attempted to remove GIL, just as many
> are rejected for merging back to the main trunk since they slow down single
> threading. You might be able to find these patches from the internet and
> download the appropriate python source, patch them, and get python minus
> GIL.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Lloyd
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread Stefan Behnel
Lie Ryan, 27.11.2009 14:04:
> Unladen Swallow plans to remove GIL.

Don't bet your money on it.

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread OkaMthembo
Thanks, i must admit the concept of the GIL is cloudy to me - for example,
if the python interpreter on a single machine is
handling one process and locks until it is done, then on to the next one,
and so on - isn't that what causes speed issues?
I was wondering why python can't implicitly handle multiple processes at
once by using all machine cores (have many threads, each
invoking the interpreter and handling a process).

Maybe i should get up to speed on threads first to get the bigger picture?


On Fri, Nov 27, 2009 at 3:38 PM, Kent Johnson  wrote:

> On Fri, Nov 27, 2009 at 4:57 AM, OkaMthembo  wrote:
> > Hi All,
> >
> > Is there a python implementation that takes advantage of all cores on
> modern
> > multicore machines?
>
> Presumably you mean something like, "Is there a python implementation
> that can run multiple compute-bound processes on multiple cores
> concurrently."
>
> Some options:
> - the multiprocessing module in the std lib - here is an example of
> using it with numpy:
> http://folk.uio.no/sturlamo/python/multiprocessing-tutorial.pdf
>
> - Jython and IronPython both have threading models that allow multiple
> threads to run concurrently on multiple processors.
>
> Kent
>



-- 
Regards,
Lloyd
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread Stefan Behnel
Kent Johnson, 27.11.2009 14:38:
> On Fri, Nov 27, 2009 at 4:57 AM, OkaMthembo wrote:
>> Is there a python implementation that takes advantage of all cores on modern
>> multicore machines?
> 
> Presumably you mean something like, "Is there a python implementation
> that can run multiple compute-bound processes on multiple cores
> concurrently."
> 
> Some options:
> - the multiprocessing module in the std lib - here is an example of
> using it with numpy:
> http://folk.uio.no/sturlamo/python/multiprocessing-tutorial.pdf

In case this is about some kind of numeric computation that doesn't require
Python object types, another option is to use Cython and put the compute
intensive code into a "nogil" function that you can run multi-threaded.

If the above doesn't apply, it may be quite possible that the GIL is not
even the limiting factor. Unless the OP provides more insight, this is
impossible to answer.

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread Kent Johnson
On Fri, Nov 27, 2009 at 8:57 AM, OkaMthembo  wrote:
> Thanks, i must admit the concept of the GIL is cloudy to me - for example,
> if the python interpreter on a single machine is
> handling one process and locks until it is done, then on to the next one,
> and so on - isn't that what causes speed issues?

A single interpreter runs in a single process. Within that process,
only one thread can be interpreting Python byte codes at one time. If
a thread is blocked, for example waiting for I/O, another thread can
run. Some C extensions release the GIL so other threads can run
concurrently.

> I was wondering why python can't implicitly handle multiple processes at
> once by using all machine cores (have many threads, each
> invoking the interpreter and handling a process).

Because explicit is better than implicit?

The multiprocessing module allows you to control multiple processes
from Python and do pretty much what you describe above - a Python main
program can create multiple processes, each running a separate
interpreter, all working on a single problem.

> Maybe i should get up to speed on threads first to get the bigger picture?

At least make sure you understand the difference between threads and processes.

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread OkaMthembo
Thanks everyone,

I realise that my question is vague on a few fronts. To try and clarify,
supposing i had a box running a
Python web app on it, and the web server handles say, 10 concurrent
requests. Now i've read that only one
instance of the  Python interpreter can run on a single machine. Can that
interpreter be invoked by the web server
to run several requests at once, or do the requests get queued and handled
one at a time? If the interpreter cannot
handle concurrent requests by the web server, is it because of the GIL? I am
thinking that on multi-core machines
the interpreter should be able to run different processes on each core, thus
being able to serve more requests?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread OkaMthembo
Thanks Kent, that did clear some of the smog.

>At least make sure you understand the difference between threads and
processes.

Will be taking that advice. I think it's the reason i struggle to ask the
question better.

On Fri, Nov 27, 2009 at 5:12 PM, Kent Johnson  wrote:

> On Fri, Nov 27, 2009 at 8:57 AM, OkaMthembo  wrote:
> > Thanks, i must admit the concept of the GIL is cloudy to me - for
> example,
> > if the python interpreter on a single machine is
> > handling one process and locks until it is done, then on to the next one,
> > and so on - isn't that what causes speed issues?
>
> A single interpreter runs in a single process. Within that process,
> only one thread can be interpreting Python byte codes at one time. If
> a thread is blocked, for example waiting for I/O, another thread can
> run. Some C extensions release the GIL so other threads can run
> concurrently.
>
> > I was wondering why python can't implicitly handle multiple processes at
> > once by using all machine cores (have many threads, each
> > invoking the interpreter and handling a process).
>
> Because explicit is better than implicit?
>
> The multiprocessing module allows you to control multiple processes
> from Python and do pretty much what you describe above - a Python main
> program can create multiple processes, each running a separate
> interpreter, all working on a single problem.
>
> > Maybe i should get up to speed on threads first to get the bigger
> picture?
>
> At least make sure you understand the difference between threads and
> processes.
>
> Kent
>



-- 
Regards,
Lloyd
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread Stefan Behnel
OkaMthembo, 27.11.2009 16:15:
> i've read that only one
> instance of the  Python interpreter can run on a single machine.

That's likely the source of your confusion then. You can run any number of
Python interpreters on a single machine (limited by system resources like
RAM, obviously), and in fact, many web servers will do that for you to
handle multiple requests in parallel.

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread OkaMthembo
> That's likely the source of your confusion then. You can run any number of
> Python interpreters on a single machine (limited by system resources like
> RAM, obviously), and in fact, many web servers will do that for you to
> handle multiple requests in parallel.

Thanks Stefan, i was  not aware of the fact.

On Fri, Nov 27, 2009 at 5:42 PM, Stefan Behnel  wrote:

> OkaMthembo, 27.11.2009 16:15:
> > i've read that only one
> > instance of the  Python interpreter can run on a single machine.
>
> That's likely the source of your confusion then. You can run any number of
> Python interpreters on a single machine (limited by system resources like
> RAM, obviously), and in fact, many web servers will do that for you to
> handle multiple requests in parallel.
>
> Stefan
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Lloyd
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on multicore machines

2009-11-27 Thread Kent Johnson
On Fri, Nov 27, 2009 at 10:15 AM, OkaMthembo  wrote:
> Thanks everyone,
>
> I realise that my question is vague on a few fronts. To try and clarify,
> supposing i had a box running a
> Python web app on it, and the web server handles say, 10 concurrent
> requests.

This is still pretty vague, as there are many ways to write a Python
web app and most of those have many options for deployment. But any of
the major Python web frameworks have deployment options that support
concurrent requests. For example Django deployment options:
http://docs.djangoproject.com/en/dev/howto/deployment/#howto-deployment-index

Also, remember that a request handler will spend some of its time
blocked waiting for socket I/O or database access, and this is time
when other threads can run. Also, there are asynchronous request
handling models that do not rely on threads, for example Twisted.

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] AttributeError: Vector instance has no attribute 'Normalize'

2009-11-27 Thread Garrett Hartshaw
The program I am writing gives the following error message when run.

Traceback (most recent call last):
  File "./space.py", line 177, in 
    main()
  File "./space.py", line 173, in main
    player = Ship("space/models/fighter.3ds",
"space/models/realistic.bmp", Quaternion(), Vector(0, 0, -30), 1, 0.1)
  File "./space.py", line 131, in __init__
    Object.__init__(self, obj, tex, rot, pos)
  File "./space.py", line 124, in __init__
    self.model = Model(obj)
  File "./space.py", line 80, in __init__
    self.CalcNormals()
  File "./space.py", line 90, in CalcNormals
    vectB = (vectA - vectB).Normalize()
AttributeError: Vector instance has no attribute 'Normalize'

The definition for Vector is:

class Vector:
    def __init__(self, x=0, y=0, z=0):
        self.x = x
        self.y = y
        self.z = z

    def Normalize(self):
        return self / self.Length()

    def __sub__(self, right):
        return Vector(self.x - right.x, self.y - right.y, self.z - right.z)

so I cannot figure out what the error is.

(o_
//\
V_/_
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] AttributeError: Vector instance has no attribute 'Normalize'

2009-11-27 Thread bob gailer

Garrett Hartshaw wrote:

The program I am writing gives the following error message when run.

Traceback (most recent call last):
  File "./space.py", line 177, in 
main()
  File "./space.py", line 173, in main
player = Ship("space/models/fighter.3ds",
"space/models/realistic.bmp", Quaternion(), Vector(0, 0, -30), 1, 0.1)
  File "./space.py", line 131, in __init__
Object.__init__(self, obj, tex, rot, pos)
  File "./space.py", line 124, in __init__
self.model = Model(obj)
  File "./space.py", line 80, in __init__
self.CalcNormals()
  File "./space.py", line 90, in CalcNormals
vectB = (vectA - vectB).Normalize()
AttributeError: Vector instance has no attribute 'Normalize'
  


I get a different error:
 File "J:\pipelines\vector.py", line 8, in Normalize
   return self / self.Length()
AttributeError: Vector instance has no attribute 'Length'


The definition for Vector is:

class Vector:
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z

def Normalize(self):
return self / self.Length()

def __sub__(self, right):
return Vector(self.x - right.x, self.y - right.y, self.z - right.z)


  



--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode mapping doesn't work

2009-11-27 Thread spir
Lie Ryan  wrote:

> > funnychars = u"éèêëóòôöáàâäÉÈÊËÓÒÔÖÁÀÂÄ"
> > asciichars = ""
> >

In addition to Lie's reply, you will very probably need diacritic-free chars to 
be unicode, too. Otherwise prepare for later UnocideEn/De-codeError-s. As a 
rule of thumb, if you work with unicode thingies, make everything unicode in 
your whole program.

Denis


la vita e estrany

http://spir.wikidot.com/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python time

2009-11-27 Thread spir
Kent Johnson  wrote:

> On Wed, Nov 25, 2009 at 11:11 AM, spir  wrote:
> > Hello,
> >
> > How does python get the time in microseconds? (In other words, how would I 
> > get it if python (like some other languages) would provide time in whole 
> > seconds only?)
> 
> Use the source...in particular, see floattime() in timemodule.c:
> http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup
> 
> Kent
> 

Thanks, Kent.
So, python  uses C's gettimeofday() func when available (microsecond), or 
ftime() (millisecond), else it has only plain second precision using time(). 
But there is no matching system call AFAIK except for time (via datetime in 
unix-like systems). So, does someone know of any solution via the system 
itself? How do C funcs get precise time, anyway?

Denis


la vita e estrany

http://spir.wikidot.com/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] AttributeError: Vector instance has no attribute 'Normalize'

2009-11-27 Thread spir
Garrett Hartshaw  wrote:

> The program I am writing gives the following error message when run.
> 
> Traceback (most recent call last):
>   File "./space.py", line 177, in 
>     main()
>   File "./space.py", line 173, in main
>     player = Ship("space/models/fighter.3ds",
> "space/models/realistic.bmp", Quaternion(), Vector(0, 0, -30), 1, 0.1)
>   File "./space.py", line 131, in __init__
>     Object.__init__(self, obj, tex, rot, pos)
>   File "./space.py", line 124, in __init__
>     self.model = Model(obj)
>   File "./space.py", line 80, in __init__
>     self.CalcNormals()
>   File "./space.py", line 90, in CalcNormals
>     vectB = (vectA - vectB).Normalize()
> AttributeError: Vector instance has no attribute 'Normalize'
> 
> The definition for Vector is:
> 
> class Vector:
>     def __init__(self, x=0, y=0, z=0):
>         self.x = x
>         self.y = y
>         self.z = z
> 
>     def Normalize(self):
>         return self / self.Length()
> 
>     def __sub__(self, right):
>         return Vector(self.x - right.x, self.y - right.y, self.z - right.z)
> 
> so I cannot figure out what the error is.

You may print out the given vector's attr list using dir to check, or just 
check it actually holds a Normalise method, exactly at the point you want its 
norm. Something like:
    v = (vectA - vectB)
print (hasattr(v, "Normalise")); print (v.__class__)
vectB = v.Normalise()

The second print() is for this reason: I bet you have several instances (;-) of 
the Vector class on your system, and the one actually used is not the one you 
guess...

> (o_
> //\
> V_/_
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor






la vita e estrany

http://spir.wikidot.com/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python time

2009-11-27 Thread Alan Gauld

"spir"  wrote

So, python  uses C's gettimeofday() func when available 


It's not C's function, it's a Unix system call. 
It's been part of Unix since BSD 4.2


ftime() (millisecond), 


Also Unix and predates BSD 4.2...

else it has only plain second precision using time(). 


Which is an ANSI C function.

But there is no matching system call AFAIK except for time 
(via datetime in unix-like systems). 


time() is the lowest common denominator supported by any ANSI C system.
It returns a tm struct:

struct tm{
   int tm_sec;
   int tm_min;
   // etc up to
   int tm_yday;
   int tm_isdst;
}

So only seconds need to be supported for ANSI compliance.

gettimeofday() is the Unix system call that returns microseconds.

But to confuse things, on Unix there is also the time() library function
(not system call - subtle difference!) which returns a timeval structure:

struct timeval{
   long tv_sec;
   long tv_usec;
}

So even time() on Unix returns microsecs.

But on early DOS it only returns seconds.
(I may be wrong but I think time() on DOS 6 returned milliseconds?)

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python time

2009-11-27 Thread spir
"Alan Gauld"  wrote:

Thank you, Alan.

> "spir"  wrote
> 
> > So, python  uses C's gettimeofday() func when available 
> 
> It's not C's function, it's a Unix system call. 
> It's been part of Unix since BSD 4.2
>
> > ftime() (millisecond), 
> 
> Also Unix and predates BSD 4.2...

I am confused here. That's what I first thought (there _must_ be a way to get 
time more precise that seconds!). But on my system (ubuntu 9.10) I cannot find 
the proper manner to use these system calls. Even from the command-line 
directly. Certainly missing something _really_ obvious.

As for the code in timemodule.c, I read:

floattime(void)
{
/* There are three ways to get the time:
  (1) gettimeofday() -- resolution in microseconds
  (2) ftime() -- resolution in milliseconds
  (3) time() -- resolution in seconds
  In all cases the return value is a float in seconds.
  Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  fail, so we fall back on ftime() or time().
  Note: clock resolution does not imply clock accuracy! */
#ifdef HAVE_GETTIMEOFDAY
{
struct timeval t;
#ifdef GETTIMEOFDAY_NO_TZ
if (gettimeofday(&t) == 0)
return (double)t.tv_sec + t.tv_usec*0.01;
#else /* !GETTIMEOFDAY_NO_TZ */
if (gettimeofday(&t, (struct timezone *)NULL) == 0)
return (double)t.tv_sec + t.tv_usec*0.01;
#endif /* !GETTIMEOFDAY_NO_TZ */
}

#endif /* !HAVE_GETTIMEOFDAY */
...

This let me think gettimeofday() and ftime() are C routines, but they may ne 
wrappers around system calls.

> 
> > else it has only plain second precision using time(). 
> 
> Which is an ANSI C function.
> 
> > But there is no matching system call AFAIK except for time 
> > (via datetime in unix-like systems). 
> 
> time() is the lowest common denominator supported by any ANSI C system.
> It returns a tm struct:
> 
> struct tm{
> int tm_sec;
> int tm_min;
> // etc up to
> int tm_yday;
> int tm_isdst;
> }
> 
> So only seconds need to be supported for ANSI compliance.

Right. This is precisely the reason why some languages (eg lua) don't provide 
any way to get time in ms or us. And also the source of my original post on the 
topic.

> gettimeofday() is the Unix system call that returns microseconds.
> 
> But to confuse things, on Unix there is also the time() library function
> (not system call - subtle difference!) which returns a timeval structure:
> 
> struct timeval{
> long tv_sec;
> long tv_usec;
> }
> 
> So even time() on Unix returns microsecs.
> 
> But on early DOS it only returns seconds.
> (I may be wrong but I think time() on DOS 6 returned milliseconds?)
> 
> HTH,
 
So, I still have the issue of not beeing smart enough to access one of these 
systems-provided features. Would someone be nice & write an example call to use 
eg Unix's gettimeofday directly from python or from the command-line? (without 
using python's time module, indeed)

Denis


la vita e estrany

http://spir.wikidot.com/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python time

2009-11-27 Thread Kent Johnson
On Fri, Nov 27, 2009 at 6:07 PM, spir  wrote:

> I am confused here. That's what I first thought (there _must_ be a way to get 
> time more precise that seconds!). But on my system (ubuntu 9.10) I cannot 
> find the proper manner to use these system calls. Even from the command-line 
> directly. Certainly missing something _really_ obvious.

I think you have to call them from C or some other language that can
access the system calls.

>> So only seconds need to be supported for ANSI compliance.
>
> Right. This is precisely the reason why some languages (eg lua) don't provide 
> any way to get time in ms or us. And also the source of my original post on 
> the topic.

Some solutions for Lua here:
http://stackoverflow.com/questions/463101/lua-current-time-in-milliseconds

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python time

2009-11-27 Thread Modulok
>> Doesn't time.time return a float?
>>
>> >>> import time
>> >>> help(time.time)
>> Help on built-in function time in module time:
>>
>> time(...)
>> time() -> floating point number
>>
>> Return the current time in seconds since the Epoch.
>> Fractions of a second may be present if the system clock provides
>> them.
>>
>>
>> >>> time.time()
>> 1259288538.576565
>>
>> Right?
>> -Modulok-
>
> For sure, but this wasn't my question. I was precisely asking how python
> does that.

Clearly I have a mighty need to consume more coffee. My apologies for
misreading the original post.
-Modulok-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor