[Tutor] 'sphere' object is unindexable

2009-02-06 Thread Mr Gerard Kelly
I am trying to get around the problem of sphere object being unindexable.

I need to make spheres appear, positioned according to some list, for
example:

for i in [0,1]:
  sphere(pos=(0,i,0), radius=0.1)
  

Is there any way of making these different spheres behave differently
without using indexing?

For example, I would like to be able to make the lower sphere go red
when it is clicked on, using this fragment of code:

while True:
  if scene.mouse.events:
m = scene.mouse.getevent()
if m.pick is ball[0]:
  ball[0].color = color.red

But obviously it won't work because I haven't named it ball[0]. I can't
figure out a way to name the objects based on their position without
using indexing. Is there any way to do this?

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


Re: [Tutor] passing unknown no of arguments

2009-02-06 Thread amit sethi
>> AFAIK MS Silverlight allows you to run .NET languages in your browser,

so for that i guess i would have to use ironpython and not Cpython which is
what i normally use . Are there any big problems porting the code from one
to other that i need to be worried about.


On Fri, Feb 6, 2009 at 12:30 AM, Alan Gauld wrote:

>
> "Kent Johnson"  wrote
>
>>
>>  From the Silverlight FAQ:
>>>
>> Silverlight will support all major browsers on both Mac OS X, Linux
>> and on Windows.
>>
>
> I briefly tried and failed to get it working on MacOS X and lost interest.
> Looks like I might be a bit premature in ignoring it. I'll have another
> go...
>
> Alan G.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] totally stumped on signal code, wont work in this instance

2009-02-06 Thread dave selby
Hi,

I have been hitting my head on a brick wall on a bit of code that
refuses to work. I have similar code in other scripts that works AOK
I have commented out all functionality except the core problem ...


import sys, threading, time, os.path, urllib, time, signal, ConfigParser, logger


def main():
#logger.log('daemon starting ...', 'CRIT')
#read_config()
#thread1 = Thread1_PTZ()
#thread1.setDaemon(True)
#thread1.start()
#thread2 = Thread2_PTZ_Park()
#thread2.setDaemon(True)
#thread2.start()
#thread3 = Thread3_PTZ_Preset()
#thread3.setDaemon(True)
#thread3.start()

while True: # sleep to keep daemons alive :)
time.sleep(60 * 60 * 24)


def signal_hup(signum, frame):
"""
SIGHUP, unlike all other daemons SIGHUP causes this daemon to exit killing
all its daemon threads. This is a workaround. Because 'kmotion_ptzd' is
threaded the only way to get the threads to reliably reload their config
is to kill and restart else they languish in a sleep state for ? secs.

args: discarded
excepts :
return  : none
"""

print 'sighup :)'
#logger.log('signal SIGHUP detected, shutting down due to
threading', 'CRIT')
#sys.exit()

...
main()

So main just sits there AOK. I send a sighup to the script with

pkill -SIGHUP -f python.+kmotion_ptzd.py

and I get ...

d...@main-system:~/kmotion2/core$ ./kmotion_ptzd.py
Hangup
d...@main-system:~/kmotion2/core$

OK so I would have expected signal_hup to have intercepted the signal
and printed 'sighup :)'
and also not have exited in this case.

Any ideas, I can solve most of my python probs but this one has me stumped.

Cheers

Dave










-- 

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] any best practice on how to glue tiny tools together

2009-02-06 Thread Daniel
Hi Tutors,

I want to use python to finish some routine data processing tasks
automatically (on Windows).

The main task could be split to sub small tasks. Each can be done by
executing some small tools like "awk" or by some other python scripts.
One example of such task is conducting a data processing job, including:

   1. use tool-A to produce some patterns.
   2. feed tool-B with these patterns to mine more related data
   3. repeat these tasks circularly until meeting some conditions.

The real task includes more tools which run in parallel or sequential
manner.

I know how to do this with modules like subprocess, but the final python
program looks somewhat messy and hard to adapt for changes.

Do you have any best practices on this?

Thanks a lot,
Danny
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Designing a Dialog in Python

2009-02-06 Thread Alan Gauld
"Wayne Watson"  wrote 

Signature.htmlWhen I used VBasic many years ago, 
it had the ability to design a dialog and then attach it 
to the code. Is there something like this available for Python?


What I think you are looking for is a GUI design tool.
There are several for Python, some commercial some free.
In my experience none of them are anywhere near as good 
as the VB or Delphi ones. However SPE worked, and I 
know some folks have gotten Glade to work.

I think dabo might work too.

And if you are on a Mac you can use the standard XCode 
development tools to build a GUI and connect Python 
via the PyObjC bridge.


The bigger issue is that all of these tools are tied to GUI 
frameworks so you need to learn how to program using 
the framework first. wxPython or PyQt seem to be the 
most popular for GUI bulders but a few have modified 
the underlying framework to produce their own dialect. 
(I think dabo is an example)


However most Python programmers still seem to build 
their GUIs using vanilla code rather than a visual editor.
If you don't  need the visual design tools then you can 
design dialogs etc in your favourite editor and for that 
Tkinter/Tix come as standard.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



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


Re: [Tutor] 'sphere' object is unindexable

2009-02-06 Thread Alan Gauld

"Mr Gerard Kelly"  wrote

I am trying to get around the problem of sphere object being 
unindexable.


Can you give some background? Where are you getting these spheres?
Is this a graphics program or a tookit of some kind?

I need to make spheres appear, positioned according to some list, 
for

example:

for i in [0,1]:
 sphere(pos=(0,i,0), radius=0.1)


And does this work? Or do you get an error? If so what?

Is there any way of making these different spheres behave 
differently

without using indexing?


Where is the indexing issue?
Are you trying to store the spheres in a list and index the list
to access a sphere or are you trying to use an index to access
some aspect of the sphere?

Show us the code that sdoesn't work and the error message.
Don't just describe it. Show us. Don't assume we know what
you are trying to do - we are not psychic.


For example, I would like to be able to make the lower sphere go red
when it is clicked on, using this fragment of code:

while True:
 if scene.mouse.events:
   m = scene.mouse.getevent()
   if m.pick is ball[0]:
 ball[0].color = color.red

But obviously it won't work because I haven't named it ball[0].


So why don't you name it ball? and what is m.pick supposed to be?
Some attribute of a muse event based on your code but I have
no idea what it might be!


figure out a way to name the objects based on their position without
using indexing. Is there any way to do this?


A standard list should work and so provided you stored your
spheres in a list when you created them you should be able to
access them via an index. But without real code and a real
error trace we can't help very much other than by making
speculative guesses.


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] 'sphere' object is unindexable

2009-02-06 Thread Mr Gerard Kelly
Sorry, I do see that I wrote that question in a confusing way, but I've
worked out where I was going wrong now. Thanks for taking a look.

- Original Message -
From: Alan Gauld 
Date: Friday, February 6, 2009 7:22 pm
Subject: Re: [Tutor] 'sphere' object is unindexable
> "Mr Gerard Kelly"  wrote
> 
> >I am trying to get around the problem of sphere object being 
> >unindexable.
> 
> Can you give some background? Where are you getting these spheres?
> Is this a graphics program or a tookit of some kind?
> 
> > I need to make spheres appear, positioned according to some list, 
> > for
> > example:
> >
> > for i in [0,1]:
> >  sphere(pos=(0,i,0), radius=0.1)
> 
> And does this work? Or do you get an error? If so what?
> 
> > Is there any way of making these different spheres behave 
> > differently
> > without using indexing?
> 
> Where is the indexing issue?
> Are you trying to store the spheres in a list and index the list
> to access a sphere or are you trying to use an index to access
> some aspect of the sphere?
> 
> Show us the code that sdoesn't work and the error message.
> Don't just describe it. Show us. Don't assume we know what
> you are trying to do - we are not psychic.
> 
> > For example, I would like to be able to make the lower sphere go red
> > when it is clicked on, using this fragment of code:
> >
> > while True:
> >  if scene.mouse.events:
> >m = scene.mouse.getevent()
> >if m.pick is ball[0]:
> >  ball[0].color = color.red
> >
> > But obviously it won't work because I haven't named it ball[0].
> 
> So why don't you name it ball? and what is m.pick supposed to be?
> Some attribute of a muse event based on your code but I have
> no idea what it might be!
> 
> > figure out a way to name the objects based on their position without
> > using indexing. Is there any way to do this?
> 
> A standard list should work and so provided you stored your
> spheres in a list when you created them you should be able to
> access them via an index. But without real code and a real
> error trace we can't help very much other than by making
> speculative guesses.
> 
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] confusing enumerate behavior

2009-02-06 Thread spir
Le Fri, 6 Feb 2009 12:30:31 +0530,
jitendra gupta  a écrit :

> > #BEGIN
> > my_input = "one two three four five six seven eight nine ten"
> > text = my_input.split()
> > for i,v in enumerate(text):
> > line =  text[i-3], text[i-2], text[i-1], v, text[i+1], text[i+2],
> > text[i+3]
> > print line
> > # END

You do not need enumerate in that case:

offset = 3  # constant
my_input = "one two three four five six seven eight nine ten"
words = my_input.split()
for i in range(offset, len(words)-offset):
# beware of right-open range:
slice =  [word for word in words[i-offset : i+offset+1]]
print i,slice
==>
3 ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
4 ['two', 'three', 'four', 'five', 'six', 'seven', 'eight']
5 ['three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
6 ['four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

Adapt using a 'guard' based on offset's value if you actually want all lines.

Denis
--
la vida e estranya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] totally stumped on signal code, wont work in this instance

2009-02-06 Thread Kent Johnson
On Fri, Feb 6, 2009 at 4:06 AM, dave selby  wrote:

> import sys, threading, time, os.path, urllib, time, signal, ConfigParser, 
> logger
> 
>
> def main():
>
>while True: # sleep to keep daemons alive :)
>time.sleep(60 * 60 * 24)
>
>
> def signal_hup(signum, frame):
>"""
>SIGHUP, unlike all other daemons SIGHUP causes this daemon to exit killing
>all its daemon threads. This is a workaround. Because 'kmotion_ptzd' is
>threaded the only way to get the threads to reliably reload their config
>is to kill and restart else they languish in a sleep state for ? secs.
>
>args: discarded
>excepts :
>return  : none
>"""
>
>print 'sighup :)'
>#logger.log('signal SIGHUP detected, shutting down due to
> threading', 'CRIT')
>#sys.exit()
>
> ...
> main()

Don't you have to register your handler with something like
signal.signal(signal.SIGHUP, signal_hup)
?

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


Re: [Tutor] any best practice on how to glue tiny tools together

2009-02-06 Thread spir
Le Fri, 6 Feb 2009 06:44:11 -0500,
Kent Johnson  a écrit :

> My first thought was, use shell pipelines and bash. Then I remembered,
> David Beazley shows how to use generators to implement a processing
> pipeline in Python:
> http://www.dabeaz.com/generators-uk/

see also
http://en.wikipedia.org/wiki/Python_Pipelines

--
la vida e estranya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] any best practice on how to glue tiny tools together

2009-02-06 Thread Kent Johnson
On Fri, Feb 6, 2009 at 7:21 AM, spir  wrote:

> see also
> http://en.wikipedia.org/wiki/Python_Pipelines

That looks pretty dead, or at least unpublished. The google code
project is almost two years old but it contains no code.

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


Re: [Tutor] confusing enumerate behavior

2009-02-06 Thread عماد نوفل
On Fri, Feb 6, 2009 at 6:54 AM, Kent Johnson  wrote:

> 2009/2/6 jitendra gupta :
>
> > Try this if u r looking for this kind of solution
> my_input = "one two three four five six seven eight nine ten"
> text = my_input.split()
> for i in range(len(text)):
> >if i+3>=len(text):
> >print text[i-3:len(text):1]
> >elif i<=2:
> >print text[0:i+4]
> >else:
> >print text[i-3:i+4]
>
> You can simplify this using the min() and max() functions:
> for i in range(len(text)):
> print text[max(0, i-3):min(i+3, len(text))]
>
> Kent
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


Thank you all for the great solutions. I'm really grateful, but also still
confused about indexing now. Why is it that if i = 0 indexing starts with
-3.  I never read this before, and I thus assume it is an advanced thing, or
why is it not in elementary to intermediate resources ( The ones I used at
least). Can somebody please suggest an accessible tutorial on this?
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington


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


Re: [Tutor] 'sphere' object is unindexable

2009-02-06 Thread Carnell, James E

Sorry, I do see that I wrote that question in a confusing way, but I've
worked out where I was going wrong now. Thanks for taking a look.

> >   m.pick = ["stab","stab","stab","..."]


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


Re: [Tutor] (no subject)

2009-02-06 Thread bob gailer

Okeke emmanuel wrote:





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

At least the subject is accurate!

--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] any best practice on how to glue tiny tools together

2009-02-06 Thread bob gailer

spir wrote:

Le Fri, 6 Feb 2009 06:44:11 -0500,
Kent Johnson  a écrit :

  

My first thought was, use shell pipelines and bash. Then I remembered,
David Beazley shows how to use generators to implement a processing
pipeline in Python:
http://www.dabeaz.com/generators-uk/



see also
http://en.wikipedia.org/wiki/Python_Pipelines
  


Thank you for the plug! The code is currently in flux so not available. 
I hope to have an alpha version out soon.


--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] any best practice on how to glue tiny tools together

2009-02-06 Thread bob gailer

Kent Johnson wrote:

On Fri, Feb 6, 2009 at 7:21 AM, spir  wrote:

  

see also
http://en.wikipedia.org/wiki/Python_Pipelines



That looks pretty dead, or at least unpublished. The google code
project is almost two years old but it contains no code.
  


It is sluggish but not dead. The code is on my PC - not ready to 
release. I started the google code to help establish the project. Since 
development is voluntary right now it is slow. Keep tuned...


FWIW I was on target for an alpha version when I got sidetracked by 
writing a "universal parser" which I am now "finalizing".


Assistants are always welcome.

--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2009-02-06 Thread OkaMthembo
Lol..indeed [?]


On Fri, Feb 6, 2009 at 5:08 PM, bob gailer  wrote:

> Okeke emmanuel wrote:
>
>>
>>
>> 
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
> At least the subject is accurate!
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Lloyd Dube
<<338.png>>___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] (no subject)

2009-02-06 Thread Okeke emmanuel



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


Re: [Tutor] confusing enumerate behavior

2009-02-06 Thread Kent Johnson
2009/2/6 jitendra gupta :

> Try this if u r looking for this kind of solution
my_input = "one two three four five six seven eight nine ten"
text = my_input.split()
for i in range(len(text)):
>if i+3>=len(text):
>print text[i-3:len(text):1]
>elif i<=2:
>print text[0:i+4]
>else:
>print text[i-3:i+4]

You can simplify this using the min() and max() functions:
for i in range(len(text)):
print text[max(0, i-3):min(i+3, len(text))]

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


Re: [Tutor] passing unknown no of arguments

2009-02-06 Thread Kent Johnson
On Fri, Feb 6, 2009 at 3:43 AM, amit sethi  wrote:
>>> AFAIK MS Silverlight allows you to run .NET languages in your browser,
>
> so for that i guess i would have to use ironpython and not Cpython which is
> what i normally use . Are there any big problems porting the code from one
> to other that i need to be worried about.

http://www.codeplex.com/IronPython/Wiki/View.aspx?title=Differences&referringTitle=Home

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


Re: [Tutor] any best practice on how to glue tiny tools together

2009-02-06 Thread Kent Johnson
On Fri, Feb 6, 2009 at 4:11 AM, Daniel  wrote:
> Hi Tutors,
>
> I want to use python to finish some routine data processing tasks
> automatically (on Windows).
>
> The main task could be split to sub small tasks. Each can be done by
> executing some small tools like "awk" or by some other python scripts.
> One example of such task is conducting a data processing job, including:
>
> use tool-A to produce some patterns.
> feed tool-B with these patterns to mine more related data
> repeat these tasks circularly until meeting some conditions.
>
> The real task includes more tools which run in parallel or sequential
> manner.
>
> I know how to do this with modules like subprocess, but the final python
> program looks somewhat messy and hard to adapt for changes.
>
> Do you have any best practices on this?

My first thought was, use shell pipelines and bash. Then I remembered,
David Beazley shows how to use generators to implement a processing
pipeline in Python:
http://www.dabeaz.com/generators-uk/

It's a fascinating read, it might take a couple of times to get it but
it might fit your needs quite well. You would write a generator that
wraps a subprocess call and use that to access external tools; other
pieces and the control logic would be in Python.

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


[Tutor] parallel port help

2009-02-06 Thread Patrick

Hi Everyone

I would like to write a Python program to control the data lines of a 
parallel port.


PyParallel seems buggy, even the install script has one and it has 
practically no documentation.


Portio also failed to install for me.

Is there any other library you could point me too, or do you have any 
other advice? Is there someway to control individual data lines from bash?



Thanks in advance-Patrick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] WINSOCK stdin question

2009-02-06 Thread Tom Green
Hello Python group,

I need some advice on a problem I am facing.  Basically I have a binary that
sends out a reverse shell (CMD prompt).  I have a listening Python socket
and I would like to know how I can redirect the CMD prompt to stdin, so I
can interact with the host that is sending the shell.  Basically this is a
reverse shell.  Any tips is much appreciated.

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


[Tutor] improving the speed of prime number code

2009-02-06 Thread H.G. le Roy
Hi,

I'm stuck with Problem 10 (
http://projecteuler.net/index.php?section=problems&id=10) :-)

A part of my code deals with the calculation of prime numbers. However it is
really slow. Hopefully you have some ideas how to make it faster.

pz = [2]
# only iterate over odd numbers
for i in xrange(3,200,2):
  remprod = 1  # product of remainders
  for j in xrange(len(pz)):
remprod *= (i % pz[j])
if remprod == 0:  # if a number is divisible wo remainder its not prime
  break
  if remprod > 0:
pz.append(i)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] improving the speed of prime number code

2009-02-06 Thread taserian
One potential way to speed up is not to divide by every prime in your pz
list, but only up to the square root of i.

For example, to test if 37 is prime, you would only need to look at primes
less than or equal to int(sqrt(37)) = 6.08. . .

Tony R.

On Fri, Feb 6, 2009 at 4:19 PM, H.G. le Roy  wrote:

> Hi,
>
> I'm stuck with Problem 10 (
> http://projecteuler.net/index.php?section=problems&id=10) :-)
>
> A part of my code deals with the calculation of prime numbers. However it
> is really slow. Hopefully you have some ideas how to make it faster.
>
> pz = [2]
> # only iterate over odd numbers
> for i in xrange(3,200,2):
>   remprod = 1  # product of remainders
>   for j in xrange(len(pz)):
> remprod *= (i % pz[j])
> if remprod == 0:  # if a number is divisible wo remainder its not prime
>   break
>   if remprod > 0:
> pz.append(i)
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] improving the speed of prime number code

2009-02-06 Thread Kent Johnson
On Fri, Feb 6, 2009 at 4:19 PM, H.G. le Roy  wrote:

> A part of my code deals with the calculation of prime numbers. However it is
> really slow. Hopefully you have some ideas how to make it faster.
>
> pz = [2]
> # only iterate over odd numbers
> for i in xrange(3,200,2):
>   remprod = 1  # product of remainders

Not sure why you accumulate the products, just keep a flag or use an
else clause on the for loop.

>   for j in xrange(len(pz)):
for p in pz:
> remprod *= (i % pz[j])
> if remprod == 0:  # if a number is divisible wo remainder its not prime
>   break
>   if remprod > 0:
> pz.append(i)

You inner loop could be (not tested)
for p in pz:
  if i % p == 0:
break
else:
  pz.append(i)

You might search the archives for more ideas, this is a popular
question. There is a very slick Python implementation of the Sieve of
Eratosthenes that uses slice assignment to mark off the non-primes.

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


Re: [Tutor] improving the speed of prime number code

2009-02-06 Thread bob gailer

H.G. le Roy wrote:

Hi,

I'm stuck with Problem 10 
(http://projecteuler.net/index.php?section=problems&id=10 
) :-)


A part of my code deals with the calculation of prime numbers. However 
it is really slow. Hopefully you have some ideas how to make it faster.


pz = [2]
# only iterate over odd numbers
for i in xrange(3,200,2):
  remprod = 1  # product of remainders
  for j in xrange(len(pz)):
remprod *= (i % pz[j])
if remprod == 0:  # if a number is divisible wo remainder its not 
prime

  break
  if remprod > 0:
pz.append(i)
  
I know prime # algorithms have been discussed on at least one of the 
Python lists, and are also found in various internet resources.


Without consulting them let me offer:
- odd prime numbers are = 6x-1 or 6x+1 where x is integer.
   That reduces the # of candidates.
- the highest candidate you need to test is <= the square root of the 
candidate.

   That reduces cycles of the inner for loop
- there is no need to initialize remprod
- you can use "for j in pz:" to access successive pz's
   in which case you'd write i % j
- there is no need to multiply remprod by the remainders as you don't 
use that result later.


--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Closing and Open File--TkFileDialogs

2009-02-06 Thread Wayne Watson
Title: Signature.html




I'm using asksaveasfilename, and, as I understand it, it returns the
file name to me in write mode. I don't recall that there's a way to
reference the file with only the filename. That is, there's no
filename.write(). Comments?

While I'm at it, I have found a good example program for TkFileDialog
methods, but the description of them on the internet is pretty shallow.
For example, An Intro to Tkinter, Lundh's Tkinter pages, NM Tech pdf
files and Python docs have little to say. 
-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

Q: What do you do when your resistors get to hot?
A: Open the switch and coulomb they off.
 -- Anon. (fortunately!)

Web Page: 



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


Re: [Tutor] WINSOCK stdin question

2009-02-06 Thread Alan Gauld


"Tom Green"  wrote

I need some advice on a problem I am facing.  Basically I have a 
binary that

sends out a reverse shell (CMD prompt).


OK, You lost me right here.

What is a reverse shell? That's a new one for me?
(I tried wikipedia but drew a blank)


I have a listening Python socket and I would like to know how I can
redirect the CMD prompt to stdin,


It might be useful to know what OS you are on and how you are
running this application. Also is the listening socket on the same
computer as the "reverse shell"?


can interact with the host that is sending the shell.


The fact you mention host suggests its a separate box, but I'm not 
sure.


Curious,

Alan G 



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


[Tutor] How to avoid error message: "_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?"

2009-02-06 Thread Judith Flores
Hello,

   I get the error message above quite often when trying to open some csv 
files. I noticed that if I open the csv file in excel and delete the "empty" 
columns at the right end of my data, the error message doesn't appear.

My code looks like this:

myfile=open(csvfile)
reader=csv.reader(myfile)
header=reader.next()

Thank you,

Judith

Let me know if you would like to have some of my csv files.



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


Re: [Tutor] Closing and Open File--TkFileDialogs

2009-02-06 Thread Kent Johnson
On Fri, Feb 6, 2009 at 6:53 PM, Wayne Watson
 wrote:
> I'm using asksaveasfilename, and, as I understand it, it returns the file
> name to me in write mode. I don't recall that there's a way to reference the
> file with only the filename. That is, there's no filename.write(). Comments?

I think you are confused between askopenfile() and askopenfilename().
The first opens the file and returns a file object, the second returns
just the file name and you open the file yourself.

> While I'm at it, I have found a good example program for TkFileDialog
> methods, but the description of them on the internet is pretty shallow. For
> example, An Intro to Tkinter, Lundh's Tkinter pages, NM Tech pdf files and
> Python docs have little to say.

This is helpful:
http://tkinter.unpythonic.net/wiki/tkFileDialog

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


Re: [Tutor] WINSOCK stdin question

2009-02-06 Thread Kent Johnson
On Fri, Feb 6, 2009 at 3:52 PM, Tom Green  wrote:
> Hello Python group,
>
> I need some advice on a problem I am facing.  Basically I have a binary that
> sends out a reverse shell (CMD prompt).  I have a listening Python socket
> and I would like to know how I can redirect the CMD prompt to stdin, so I
> can interact with the host that is sending the shell.  Basically this is a
> reverse shell.  Any tips is much appreciated.

Like Alan, I never heard of reverse shell before. With a little
googling I think I understand it but I don't understand your question.
Is the python program on the client (user) side or the server side?

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


Re: [Tutor] WINSOCK stdin question

2009-02-06 Thread Tom Green
The Python app is on the Server side. Sorry, I don't mean to cause hassle
for anyone.  I appreciate everyone's assistance.

I am trying to reproduce what Netcat does.

Thanks,
Mike

On Fri, Feb 6, 2009 at 9:19 PM, Kent Johnson  wrote:

> On Fri, Feb 6, 2009 at 3:52 PM, Tom Green  wrote:
> > Hello Python group,
> >
> > I need some advice on a problem I am facing.  Basically I have a binary
> that
> > sends out a reverse shell (CMD prompt).  I have a listening Python socket
> > and I would like to know how I can redirect the CMD prompt to stdin, so I
> > can interact with the host that is sending the shell.  Basically this is
> a
> > reverse shell.  Any tips is much appreciated.
>
> Like Alan, I never heard of reverse shell before. With a little
> googling I think I understand it but I don't understand your question.
> Is the python program on the client (user) side or the server side?
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Designing a Dialog in Python

2009-02-06 Thread W W
On Fri, Feb 6, 2009 at 3:12 AM, Alan Gauld wrote:

> "Wayne Watson"  wrote
>
>> Signature.htmlWhen I used VBasic many years ago, it had the ability to
>> design a dialog and then attach it to the code. Is there something like this
>> available for Python?
>>
>
> However most Python programmers still seem to build their GUIs using
> vanilla code rather than a visual editor.
> If you don't  need the visual design tools then you can design dialogs etc
> in your favourite editor and for that Tkinter/Tix come as standard.


I'm aware of a visual design tool for Tkinter. I tried it (nowhere near as
polished as delphi/VB of course) for about 30 seconds before deciding I
didn't need the visual design tool. I forget what the editor was called
though >.< doh!

Of course you could always roll your own in Tkinter ;)
-the other Wayne
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] any best practice on how to glue tiny tools together

2009-02-06 Thread Daniel
These are really valuable info. I will try.

Thanks a lot.

On Fri, Feb 6, 2009 at 7:44 PM, Kent Johnson  wrote:

>  On Fri, Feb 6, 2009 at 4:11 AM, Daniel  wrote:
> > Hi Tutors,
> >
> > I want to use python to finish some routine data processing tasks
> > automatically (on Windows).
> >
> > The main task could be split to sub small tasks. Each can be done by
> > executing some small tools like "awk" or by some other python scripts.
> > One example of such task is conducting a data processing job, including:
> >
> > use tool-A to produce some patterns.
> > feed tool-B with these patterns to mine more related data
> > repeat these tasks circularly until meeting some conditions.
> >
> > The real task includes more tools which run in parallel or sequential
> > manner.
> >
> > I know how to do this with modules like subprocess, but the final python
> > program looks somewhat messy and hard to adapt for changes.
> >
> > Do you have any best practices on this?
>
> My first thought was, use shell pipelines and bash. Then I remembered,
> David Beazley shows how to use generators to implement a processing
> pipeline in Python:
> http://www.dabeaz.com/generators-uk/
>
> It's a fascinating read, it might take a couple of times to get it but
> it might fit your needs quite well. You would write a generator that
> wraps a subprocess call and use that to access external tools; other
> pieces and the control logic would be in Python.
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Picking up citations

2009-02-06 Thread Dinesh B Vadhia
Hi!  I want to process text that contains citations, in this case in legal 
documents, and pull-out each individual citation.  Here is a sample text:

text = "Page 500 Carter v. Jury Commission of Greene County, 396 U.S. 320, 90 
S.Ct. 518, 24 L.Ed.2d 549 (1970); Lathe Turner v. Fouche, 396 U.S. 346, 90 
S.Ct. 532, 24 L.Ed.2d 567 (1970); White v. Crook, 251 F.Supp. 401 (DCMD 
Ala.1966). Moreover, the Court has also recognized that the exclusion of a 
discernible class from jury service injures not only those defendants who 
belong to the excluded class, but other defendants as well, in that it destroys 
the possibility that the jury will reflect a representative cross section of 
the community. In John Doggone Williams v. Florida, 399 U.S. 78, 90 S.Ct. 1893, 
234, 26 L.Ed.2d 446 (1970), we sought to delineate some of the essential 
features of the jury that is guaranteed, in certain circumstances, by the Sixth 
Amendment. We concluded that it comprehends, inter alia, 'a fair possibility 
for obtaining a representative cross-section of the community.' 399 U.S., at 
100, 90 S.Ct., at 1906.9 Thus if the Sixth Amendment were applicable here, and 
petitioner were challenging a post-Duncan petit jury, he would clearly have 
standing to challenge the systematic exclusion of any identifiable group from 
jury service."

The results required are:

Carter v. Jury Commission of Greene County, 396 U.S. 320 (1970)
Carter v. Jury Commission of Greene County, 90 S.Ct. 518 (1970)
Carter v. Jury Commission of Greene County, 24 L.Ed.2d 549 (1970)

Lathe Turner v. Fouche, 396 U.S. 346 (1970)
Lathe Turner v. Fouche, 90 S.Ct. 532 (1970)
Lathe Turner v. Fouche, 24 L.Ed.2d 567 (1970)

White v. Crook, 251 F.Supp. 401 (DCMD Ala.1966)

John Doggone Williams v. Florida, 399 U.S. 78 (1970)
John Doggone Williams v. Florida, 90 S.Ct. 1893, 234 (1970)
John Doggone Williams v. Florida, 26 L.Ed.2d 446 (1970)

Before attempting to solve this problem I thought I'd first ask if anyone has 
seen a solution before?  

If not, I'll follow-up with some rules and an initial attempt.

Thanks

Dinesh


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