Re: [Tutor] Should I be thinking of threads for this ?

2005-08-27 Thread Andreas Kostyrka
Am Donnerstag, den 25.08.2005, 20:28 +0100 schrieb Nick Lunt:
> Hello folks,
> 
> I have the following code taken from the Twisted examples -
> 
> [code]
> # filewatcher.py
> from twisted.application import internet
> 
> def watch(fp):
> fp.seek(fp.tell())
> for line in fp.readlines():
> sys.stdout.write(line)
> 
> import sys
> from twisted.internet import reactor
> s = internet.TimerService(1.0, watch, file(sys.argv[1]))
s = [internet.TimerService(1.0, watch, file(x) for x in sys.argv[1:]]
for x in s: x.startService()
> s.startService()
> reactor.run()
> s.stopService()
> [/code]
> 
> I find this piece of code amazing and I am keen to put it to use.
> If I run './filewatcher.py myfile' it will print out any changes made to 
> 'myfile', very similar to 'tail -f' .
> 
> Now if I want to have this program monitor several files at once I could 
> run './filewatcher.py file1 file2 filex' or './filewatcher.py file1 & 
> ./filewatcher file2 & etc' both with minor modifications to the code,  
Well, compared to threads, executing a stat and/or read (I guess it goes
the read way, which is interesting, usually one watches files by doing
stat) is not relevant.
> but I think that could cause performance problems relating to the OS.
> So I'm thinking I will need to learn python threads (no bad thing) 
How would adding Threads to the mix solve any performance problems *g*

Andreas


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Generate 8 digit random number

2005-08-27 Thread Alberto Troiano
Hey Tutors

I saw a lot of responses...After analyze them I have resumed two approaches

1.- Generate a random number from 0 to  and fill this number with 
zeros (Almost everyone's approach)
2.- Generate 8 random numbers and join them (Pietro and someone else)

Which one of this is more randomic? I mean which one of these has lower 
chances to get duplicates?
Until now I'm using approach number 2...Unless anyone has something 
against it...

Please, let me know what do you think about them

Thanks to all for the tremendous help (at least it is tremendous for me)

Alberto


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Should I be thinking of threads for this ?

2005-08-27 Thread Kent Johnson
Nick Lunt wrote:
> Hello folks,
> 
> I have the following code taken from the Twisted examples -
> 
> [code]
> # filewatcher.py
> from twisted.application import internet
> 
> def watch(fp):
> fp.seek(fp.tell())
> for line in fp.readlines():
> sys.stdout.write(line)
> 
> import sys
> from twisted.internet import reactor
> s = internet.TimerService(1.0, watch, file(sys.argv[1]))
> s.startService()
> reactor.run()
> s.stopService()
> [/code]
> 
> I find this piece of code amazing and I am keen to put it to use.
> If I run './filewatcher.py myfile' it will print out any changes made to 
> 'myfile', very similar to 'tail -f' .
> 
> Now if I want to have this program monitor several files at once I could 
> run './filewatcher.py file1 file2 filex' or './filewatcher.py file1 & 
> ./filewatcher file2 & etc' both with minor modifications to the code,  
> but I think that could cause performance problems relating to the OS.
> So I'm thinking I will need to learn python threads (no bad thing) 
> instead, but Im hoping that someone could tell me if that seems the best 
> way to go ?

What performance problems you you anticipate? I don't know much about Twisted 
but my understanding is that tasks are run in a single thread when they are 
ready. In your case you are scheduling a simple task to run every second. I 
would think that you could schedule several such tasks and they would each run 
every second. If your task were time-consuming you might have to worry about 
doing something different but in this case I think it will be fine. Just try 
something like

for name in sys.argv[1:]:
  s = internet.TimerService(1.0, watch, file(name))
  s.startService()

I suppose if you used threads there would be the possibility of a context 
switch while watch() is running, if one thread becomes blocked on I/O then 
another thread can run. I don't know how Twisted handles this - I think you 
have to wrap the file and stdio in Twisted object that handle the blocking. 
twisted.protocols.basic.FileSender and twisted.internet.stdio.StandardIO look 
like they may be starting points.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Strange XP stdin behaviour.

2005-08-27 Thread Tim Peters
[Alan Gauld]
> Thanks Danny, interesting link in that it shows a solution I
> didn't know about in the one-liner at the bottom of the discussion.
> 
> But really I was hoping someone could explain *why* there is a
> difference. If PATHEXT can detect that intest.py needs to be run
> through Python why doesn't redirection work as expected? What
> is happening to stdin/stdout in this case?

Alas, only Microsoft could explain this, and AFAIK they never really
have.  It's not unique to Python, of course.  Here's a confused MS
article about it:

http://support.microsoft.com/default.aspx?scid=kb;en-us;321788

_Some_ of the PATHEXT-related redirection bugs did get fixed in XP,
but not all of them.  I haven't tried adding the registry entry they
suggest there.  The

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer

key doesn't even exist on my XP (Pro, SP2) box, but does exist if I
start from HKEY_CURRENT_USER instead.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Should I be thinking of threads for this ?

2005-08-27 Thread Nick Lunt
Hi Kent,


> >
> > [code]
> > # filewatcher.py
> > from twisted.application import internet
> >
> > def watch(fp):
> > fp.seek(fp.tell())
> > for line in fp.readlines():
> > sys.stdout.write(line)
> >
> > import sys
> > from twisted.internet import reactor
> > s = internet.TimerService(1.0, watch, file(sys.argv[1]))
> > s.startService()
> > reactor.run()
> > s.stopService()
> > [/code]
> >

>
> What performance problems you you anticipate? I don't know much
> about Twisted but my understanding is that tasks are run in a
> single thread when they are ready. In your case you are
> scheduling a simple task to run every second. I would think that
> you could schedule several such tasks and they would each run
> every second. If your task were time-consuming you might have to
> worry about doing something different but in this case I think it
> will be fine. Just try something like
>
> for name in sys.argv[1:]:
>   s = internet.TimerService(1.0, watch, file(name))
>   s.startService()
>

That's one way I was thinking of doing it. I'll run it like that on about 10
active files and see how it stacks up.

> twisted.protocols.basic.FileSender and
> twisted.internet.stdio.StandardIO look like they may be starting points.

Thanks for the twisted pointers. I've been using twisted for a little while
but it's such a massive thing that it can be difficult to fully understand
whats happening.

Thanks for you help,
Nick .

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Strange XP stdin behaviour.

2005-08-27 Thread Alan Gauld
>> But really I was hoping someone could explain *why* there is a
>> difference. If PATHEXT can detect that intest.py needs to be run
>> through Python why doesn't redirection work as expected? What
>> is happening to stdin/stdout in this case?
>
> Alas, only Microsoft could explain this, and AFAIK they never really
>have.  It's not unique to Python, of course.  Here's a confused MS
>article about it:
>
> http://support.microsoft.com/default.aspx?scid=kb;en-us;321788

The timbot thus spoke. And if you don't know Tim then I guess
I can feel slightly better about never having come across
this bizarre "feature" before. :-)

Thanks for the link.

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Strange XP stdin behaviour.

2005-08-27 Thread Alan Gauld
> have.  It's not unique to Python, of course.  Here's a confused MS
> article about it:
>
> http://support.microsoft.com/default.aspx?scid=kb;en-us;321788
>
> _Some_ of the PATHEXT-related redirection bugs did get fixed in XP,
> but not all of them.  I haven't tried adding the registry entry they
> suggest there.  The key doesn't even exist on my XP (Pro, SP2) box,
> but does exist if I start from HKEY_CURRENT_USER instead.

The key is indeed in the wrong place but when you apply the fix it 
works!

Alan G. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Importing a List from Module

2005-08-27 Thread Tom Strickland
I have a module called "enterData" which generates a list, "close" from 
a data file. "close" is a list of floats. When I put a print statement 
in that module it will print out an individual member of the list. For 
example,

print close[0]


prints the first member of the list.

In my "main" module I import "enterData" and try to read the first 
element of "close" as follows:

import enterData
xy=enterData.close
print xy[0]   


When I do this it prints out the entire "close" list, not just the first 
term.

What's my mistake and how do I correct it?

Thank you!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Importing a List from Module

2005-08-27 Thread Kent Johnson
Tom Strickland wrote:
> I have a module called "enterData" which generates a list, "close" from 
> a data file. "close" is a list of floats. When I put a print statement 
> in that module it will print out an individual member of the list. For 
> example,
> 
> print close[0]
> 
> 
> prints the first member of the list.
> 
> In my "main" module I import "enterData" and try to read the first 
> element of "close" as follows:
> 
> import enterData
> xy=enterData.close
> print xy[0]   
> 
> 
> When I do this it prints out the entire "close" list, not just the first 
> term.
> 
> What's my mistake and how do I correct it?

What you have shown here looks fine to me. Can you show some more of enterData?

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Importing a List from Module

2005-08-27 Thread Byron
Tom Strickland wrote:
> In my "main" module I import "enterData" and try to read the first 
> element of "close" as follows:
> 
> import enterData
> xy=enterData.close
> print xy[0]   
> 
> 
> When I do this it prints out the entire "close" list, not just the first 
> term.


Hi Tom,

I would create a function in your module that returns the list.  Here's 
a quick, simplified example:

def returnList():
newList = []
newList += [123.45]
newList += [529.59]
newList += [259.92]
return newList

aList = returnList()
print aList


Note the return statement...  This enables assignment, as you have done 
in "xy=enterData.returnList()"

Hope this helps,

Byron
---

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor