[Tutor] How to generate permutations of a given string

2006-11-27 Thread Asrarahmed Kadri

Hello folks,


Can someone help me with a simple algorithm to generate permutations of a
given string:

Input string:>> 'abc'

Output:--->>  ['abc','acb','bac','bca','cab','cba']

Thanks.
Regards,
Asrarahmed Kadri




-
To HIM you shall return.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Newbie question: join() string method

2006-11-27 Thread Moedeloos Overste
Hi,

I'm trying to use the join() method on a list to be able to store the 
contents of that list in a file. Basically I want the contents of the list 
stored without the []. I've tried numerous ways of using the join() method, 
but they all return errors. I've tried using the tutorial and documentation 
on this one but still can't work it out.

errors: TypeError: sequence item 0: expected string, int found



code:
LotNumbers = random.sample(range(1,45), 6) #random numbers from range into 
list)
','.join()  #?what's the 
correct syntax?
fout = open("draw__output.dat", "a")
fout.write(LotNumbers) #writing string to 
file
fout.close()
vDraws = vDraws - 1

Can someone show me the correct way to use the join() method in this case?

Many thanks,

Robert

_
De makers van Hotmail hebben groot nieuws! Meer weten? Klik hier! 
http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl

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


Re: [Tutor] Newbie question: join() string method

2006-11-27 Thread Andreas Kostyrka
",".join(str(x) for x in intList)

Andreas

_ Ursprüngliche Mitteilung _
Betreff:[Tutor] Newbie question: join() string method
Autor:  "Moedeloos Overste" <[EMAIL PROTECTED]>
Datum:  27. November 2006 13:20:41

Hi,

I'm trying to use the join() method on a list to be able to store the 
contents of that list in a file. Basically I want the contents of the list 
stored without the []. I've tried numerous ways of using the join() method, 
but they all return errors. I've tried using the tutorial and documentation 
on this one but still can't work it out.

errors: TypeError: sequence item 0: expected string, int found



code:
LotNumbers = random.sample(range(1,45), 6) #random numbers from range into 
list)
','.join()  #?what's the 
correct syntax?
fout = open("draw__output.dat", "a")
fout.write(LotNumbers) #writing string to 
file
fout.close()
vDraws = vDraws - 1

Can someone show me the correct way to use the join() method in this case?

Many thanks,

Robert

_
De makers van Hotmail hebben groot nieuws! Meer weten? Klik hier! 
http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl

___
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] Newbie question: join() string method

2006-11-27 Thread jhl

Hi,

I am not sure what 'random' does (what package is it from?), but

list=['1','2','3']
slist=','.join(list)

works, while

list=[1,2,3]
slist=','.join(list)

does not.  It appears 'join' only works on lists (and maybe tuples?) of
string objects and the list must be passed in as an argument.  Try
translating your list into a list of string representation of numbers and
then using join.

Hope this is a useful clue,
-Jason

On 11/27/06, Moedeloos Overste <[EMAIL PROTECTED]> wrote:


Hi,

I'm trying to use the join() method on a list to be able to store the
contents of that list in a file. Basically I want the contents of the list
stored without the []. I've tried numerous ways of using the join()
method,
but they all return errors. I've tried using the tutorial and
documentation
on this one but still can't work it out.

errors: TypeError: sequence item 0: expected string, int found



code:
LotNumbers = random.sample(range(1,45), 6) #random numbers from range into
list)
','.join()  #?what's
the
correct syntax?
fout = open("draw__output.dat", "a")
fout.write(LotNumbers) #writing string to
file
fout.close()
vDraws = vDraws - 1

Can someone show me the correct way to use the join() method in this case?

Many thanks,

Robert

_
De makers van Hotmail hebben groot nieuws! Meer weten? Klik hier!
http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl

___
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] What are the issues in running Python script on another machine (developed on Windows but will be using on Linux)

2006-11-27 Thread Python
On Sun, 2006-11-26 at 15:14 +, Asrarahmed Kadri wrote:
>  
>  
> Hi folks,
>  
> I have a couple of programs that I want to test on a different
> machine..
>  
> I have developed these on Win-XP platform, but I want to test them on
> my college's machine, which is running Linux.
>  
> Are there any issues involved or i just need to take my files on a USB
> memory stick, and copy paste and that is it..?
>  

You may need to use dos2unix to convert the DOS/Windows line endings
(\r\n)
to Unix/Linux/BSD endings
(\n)

Example usage is:
dos2unix myscript.py

It's a simple thing to try if you get mysterious errors.

> thanks for the help.
>  
> Regards,
> Asrarahmed 
> 
> -- 
> To HIM you shall return. 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] What are the issues in running Python script on another machine (developed on Windows but will be using on Linux)

2006-11-27 Thread Kent Johnson
Python wrote:
> On Sun, 2006-11-26 at 15:14 +, Asrarahmed Kadri wrote:
>>  
>>  
>> Hi folks,
>>  
>> I have a couple of programs that I want to test on a different
>> machine..
>>  
>> I have developed these on Win-XP platform, but I want to test them on
>> my college's machine, which is running Linux.
>>  
>> Are there any issues involved or i just need to take my files on a USB
>> memory stick, and copy paste and that is it..?
>>  
> 
> You may need to use dos2unix to convert the DOS/Windows line endings
>   (\r\n)
> to Unix/Linux/BSD endings
>   (\n)
> 
> Example usage is:
>   dos2unix myscript.py
> 
> It's a simple thing to try if you get mysterious errors.

Since Python 2.3, script files are opened with universal newline support:
http://www.python.org/doc/2.3.5/whatsnew/node7.html

It is a good idea in portable code to use os.path.join() to create file 
paths, rather than hard-coding the path separator (/ or \). Or you can 
just use / as a file separator which works on Windows and Linux.

You could conceivably have issues with text file encoding if you are 
working with text data that includes characters outside the ASCII set.

Kent

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


Re: [Tutor] Newbie question: join() string method

2006-11-27 Thread Moedeloos Overste
Ha!

Thanx for all the input everybody. I finally got it to work. The contents of 
the list is now stored in the file without the []. The solution is in the 
code below. Next step in my learning process is reading the file contents 
and storing it in a dictionary.

One question: When I run the program from IDLE it writes the data to file 
but when I run it from the command prompt(win32)  it doesn't. why is this?



code:

vDraws = input("How many times do you want to draw the lottery? :>")

# Draw lottery numbers & writing them to file

while vDraws > 0:
LotNumbers = random.sample(range(1,45), 6) #random numbers from range 
into list)
strgOutput=",".join(str(i) for i in LotNumbers)   #??converting list 
to string to store it.
fout = open("draw__output.dat", "a")
fout.write(strgOutput + "\n")  #writing string to file
fout.close()
vDraws = vDraws - 1



>From: "Andreas Kostyrka" <[EMAIL PROTECTED]>
>Reply-To: "Andreas Kostyrka" <[EMAIL PROTECTED]>
>To: "Moedeloos Overste" <[EMAIL PROTECTED]>
>CC: tutor@python.org
>Subject: Re: [Tutor] Newbie question: join() string method
>Date: Mon, 27 Nov 2006 13:32:59 +0100
>
>",".join(str(x) for x in intList)
>
>Andreas
>
>_ Ursprüngliche Mitteilung _
>Betreff:   [Tutor] Newbie question: join() string method
>Autor: "Moedeloos Overste" <[EMAIL PROTECTED]>
>Datum: 27. November 2006 13:20:41
>
>Hi,
>
>I'm trying to use the join() method on a list to be able to store the
>contents of that list in a file. Basically I want the contents of the list
>stored without the []. I've tried numerous ways of using the join() method,
>but they all return errors. I've tried using the tutorial and documentation
>on this one but still can't work it out.
>
>errors: TypeError: sequence item 0: expected string, int found
>
>
>
>code:
>LotNumbers = random.sample(range(1,45), 6) #random numbers from range into
>list)
> ','.join()  #?what's 
>the
>correct syntax?
> fout = open("draw__output.dat", "a")
> fout.write(LotNumbers) #writing string to
>file
> fout.close()
> vDraws = vDraws - 1
>
>Can someone show me the correct way to use the join() method in this case?
>
>Many thanks,
>
>Robert
>
>_
>De makers van Hotmail hebben groot nieuws! Meer weten? Klik hier!
>http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor

_
Windows Live Mail: Slim, Persoonlijk, Betrouwbaar en het blijft natuurlijk 
gratis! http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl

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


Re: [Tutor] How to generate permutations of a given string

2006-11-27 Thread Alan Gauld
"Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote

> Can someone help me with a simple algorithm to generate permutations 
> of a
> given string:

Try Googling for

python string permutation.

Several approaches came up including a cookbook page entry.

Alan G. 


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


Re: [Tutor] __init__.py for running a file from commandline?

2006-11-27 Thread Marcus Goldfish

On 11/11/06, Kent Johnson <[EMAIL PROTECTED]> wrote:


Marcus Goldfish wrote:
> Hoping someone can help with this...
>
> I have a logical python namespace using a directory tree and __init__.py
> files.  For example, PYTHONPATH points to ~pyroot, and I have the
following:
>
> ~pyroot/
> ~pyroot/utils/
> ~pyroot/utils/commands/mygrep.py
>
> Which makes it nice to code:
>
> # some python script
> import utils.commands.mygrep as grep
>
> However, I have a problem when running python scripts from the command
> line.  I would like to do this:
>
>  > python utils.commands.mygrep.py
>
> but it doesn't work.  Is there a trick, or something that I am missing,
> that will let me run scripts like that?

python utils/commands/mygrep.py
will work if mygrep.py doesn't import other modules from utils; not sure
if it will work with imports.

Kent



Kent,

Hmmm... I tried this on my WinXP box-- didn't work nor with backslashing).
I checked my environment variables in my shell, and PYTHONPATH points to
~pyroot.  Am I missing something?  Could this be a 'nix thing?

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


[Tutor] e: moving from pack to grid in tkinter

2006-11-27 Thread Tony Cappellini

Date: Mon, 27 Nov 2006 18:07:33 +1300
From: "John Fouhy" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] moving from pack to grid in tkinter
To: tutor-python 
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 27/11/06, Tony Cappellini <[EMAIL PROTECTED]> wrote:

I've got a main window which is 640 * 400.
  self.root.geometry("600x400")

 self.label.pack(side=BOTTOM, fill=X)
This line would put the label at the bottom of the window, and extend to
both left and right edges of the window.



So, something like a status bar?

Yes exactly. I started with pack- and the status bar was ok.
Then I read the pdf at Effbot.org. I need to add several widgets to the main
frame, so I figured I would use grid for all of them.

Using pack() is really the easiest way to do this.  Here's what I'd do:



But i'm trying to get away from pack() and use grid()



Not sure why you have two column= options here.. One of them should be
columnspan, maybe?

Just a typi- I originally had columnspan, to see how it affected the widget-
it didnt'.
I was in the middle of an edit when I decided to post the message- but I had
tried cloumn with and without columnspan, no effect.


Using grid geometry, widgets will take their natural size and sit in
the middle of the cell you place them in.  If you want to override
this, you need to specify which edge or edges you want the widget to
stick to.



self.label.grid(row=5, column=0, columnspan=2, sticky=E+W)

I tried sticky E+W- again no effect on the edges of the label:(


f there is nothing in a row, the row will have a height of zero.  So

So how does one place a label (or any other widget) at the bottom row of a
screen using grid()?
It worked fine using pack- but the turoail pdf recommended grid() is easier
when working with several widgets in a common container widget


HTH!

Well thanks anyway. The pdf has really confused me.
Is there an official tkinter.org doc reference or something?

--
John.


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


Re: [Tutor] __init__.py for running a file from commandline?

2006-11-27 Thread Kent Johnson
Marcus Goldfish wrote:
> 
> 
> On 11/11/06, *Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> 
> Marcus Goldfish wrote:
>  > Hoping someone can help with this...
>  >
>  > I have a logical python namespace using a directory tree and
> __init__.py
>  > files.  For example, PYTHONPATH points to ~pyroot, and I have the
> following:
>  >
>  > ~pyroot/
>  > ~pyroot/utils/
>  > ~pyroot/utils/commands/mygrep.py
>  >
>  > Which makes it nice to code:
>  >
>  > # some python script
>  > import utils.commands.mygrep as grep
>  >
>  > However, I have a problem when running python scripts from the
> command
>  > line.  I would like to do this:
>  >
>  >  > python utils.commands.mygrep.py 
>  >
>  > but it doesn't work.  Is there a trick, or something that I am
> missing,
>  > that will let me run scripts like that?
> 
> python utils/commands/mygrep.py
> will work if mygrep.py doesn't import other modules from utils; not
> sure
> if it will work with imports.
> 
> Kent
> 
> 
> Kent,
> 
> Hmmm... I tried this on my WinXP box-- didn't work nor with 
> backslashing).  I checked my environment variables in my shell, and 
> PYTHONPATH points to ~pyroot.  Am I missing something?  Could this be a 
> 'nix thing?

It works for me on WinXP to run a simple script with no dependencies, 
with forward or back slashes. I don't have PYTHONPATH set at all. How 
does it fail for you?

Kent

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


Re: [Tutor] __init__.py for running a file from commandline?

2006-11-27 Thread Michael P. Reilly

Marcus,

When you type something from the command-line, you are at the whims of the
WinXP command shell.  You have to follow its rules, not Python's.  It would
need to have "python" in %PATH%, and then it would need to have to run
"python C:\path\to\pyroot\utils\commands\mygrep.py".  The arguments are
determined before Python is even started, and they are parsed by the WinXP
DOS-a-like shell. (Also why you cannot have ".", only Python understands
dots).

Kent mentioned issues with importing modules, and those would still hold
true since those are after Python starts.  Also, the WinXP shell, does
handle forward slashs, but you were probably not in the proper directory for
the shell to find the file using "utils/commands/mygrep.py" pathname.

HTH,
 -Arcege

On 11/27/06, Marcus Goldfish <[EMAIL PROTECTED]> wrote:




On 11/11/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> Marcus Goldfish wrote:
> > Hoping someone can help with this...
> >
> > I have a logical python namespace using a directory tree and
> __init__.py
> > files.  For example, PYTHONPATH points to ~pyroot, and I have the
> following:
> >
> > ~pyroot/
> > ~pyroot/utils/
> > ~pyroot/utils/commands/mygrep.py
> >
> > Which makes it nice to code:
> >
> > # some python script
> > import utils.commands.mygrep as grep
> >
> > However, I have a problem when running python scripts from the command
> > line.  I would like to do this:
> >
> >  > python utils.commands.mygrep.py
> >
> > but it doesn't work.  Is there a trick, or something that I am
> missing,
> > that will let me run scripts like that?
>
> python utils/commands/mygrep.py
> will work if mygrep.py doesn't import other modules from utils; not sure
>
> if it will work with imports.
>
> Kent


Kent,

Hmmm... I tried this on my WinXP box-- didn't work nor with
backslashing).  I checked my environment variables in my shell, and
PYTHONPATH points to ~pyroot.  Am I missing something?  Could this be a 'nix
thing?

Marcus



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






--
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie question: join() string method

2006-11-27 Thread Alan Gauld
"Moedeloos Overste" <[EMAIL PROTECTED]> wrote

> One question: When I run the program from IDLE it writes the data to 
> file
> but when I run it from the command prompt(win32)  it doesn't. why is 
> this?

How do you know? Have you searched for the file?
Or are you looking in the same file that IDLE produced?
I would expect the code to create a new file in the current
directory - wherever you started the program. Did you look
there?

---
while vDraws > 0:
LotNumbers = random.sample(range(1,45), 6) #random numbers from 
range
into list)
strgOutput=",".join(str(i) for i in LotNumbers) 
#??converting list
to string to store it.
fout = open("draw__output.dat", "a")
fout.write(strgOutput + "\n")  #writing string to file
fout.close()
--

It's probably better to put the open/close outside the loop

fout = open(...,'w')
while vDraws > 0
...
fout.write(strgOutput + '\n')
vDraws -= 1
fout.close()

That way you can use 'w' to write the file(unless you really want the
previous runs to be in there too.) Also this will save the program
opening and closing the file for each line which is quite a slow 
process.

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] moving from pack to grid in tkinter

2006-11-27 Thread Alan Gauld
"Tony Cappellini" <[EMAIL PROTECTED]> wrote

>>>
> But i'm trying to get away from pack() and use grid()

There's no good reason not to use pack where it makes sense.
You can mix n match provided you keep it consistent within a
single container(usually a frame)

Grid is best where the controls conform to a grid, typically
when laid out across the Frame.
Pack is a natural where you want to stack things on
top of each other.

Typically I use pack for the frames in the main window
and grid for the widgets inside each frame.

A status bar is most naturally expressed as a frame at
the bottom of your window containing a label widget so
I'd use pack for it.

You can force it wirth grid by picking a very large number
but its a bit of a kluge in my view.

>>>f there is nothing in a row, the row will have a height of zero. 
>>>So
> So how does one place a label (or any other widget) at the bottom 
> row of a
> screen using grid()?

Are you using grid on your top window or on a frame within that?
It sounds like maybe the former. Try creating a frame and
gridding it into the main window. Then grid your widgets
inside the Frame. (I haven't tried this but it sounds like a 
possibility!)
That way the window should shrink to fit the frame...
.
> Well thanks anyway. The pdf has really confused me.
> Is there an official tkinter.org doc reference or something?

Have you checked the Tkinter page in the Python.org web site?
Also the Tcl/Tk documentation gives a fairly definitive description
of how things work, albeit expressed in Tcl speak

HTH,

Alan G. 


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


[Tutor] AttributeError: 'list' object has no attribute 'copy'

2006-11-27 Thread Keith Troell
I'm a rank beginner. I am running Python 2.3.5 on the Terminal of Mac  
OSX.

The Tutorial (5.1) mentions list.copy as a list method. When I run

scores = [1, 2, 3]
copy = scores.copy ()

I get: AttributeError: 'list' object has no attribute 'copy'

However, I am able to  use .copy for dictionaries without any problem.
 >>> scores = {}
 >>> copy = scores.copy ()
 >>> copy
{}
 >>>


Do I need to import a module to use list.copy?
What am I missing?

TIA,

Keith





A++ G++ PKR+ !PEG B- TB ADB-- M+++ CHOW++
http://zbigniew.pyrzqxgl.com/bargegeek.html


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


Re: [Tutor] AttributeError: 'list' object has no attribute 'copy'

2006-11-27 Thread Kent Johnson
Keith Troell wrote:
> I'm a rank beginner. I am running Python 2.3.5 on the Terminal of Mac  
> OSX.
> 
> The Tutorial (5.1) mentions list.copy as a list method. When I run

Are you sure? I don't see it here:
http://www.python.org/doc/2.3.5/tut/node7.html#SECTION00710

> 
> scores = [1, 2, 3]
> copy = scores.copy ()
> 
> I get: AttributeError: 'list' object has no attribute 'copy'

You can copy a list with the list() function or by slicing:
copy = list(scores)
or
copy = scores[:]

Kent

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


Re: [Tutor] ashamed

2006-11-27 Thread Moedeloos Overste

I shamefully bowe my head. How stupid of me not to think of that. I assumed 
that as the script is in a certain directory the output would also be in 
that directory. A very humbling learning experience

Thank you for the pointer on file open/close outside of the loop. That 
should speed things up

Right now I'm kept busy by dictionaries. I've created a dictionary 
containing all possible keys(lottery numbers) with their values set to zero. 
Now I want read the file and for each time a key is in the file i want it's 
value to go up +1. Like a counter. So far I've not been succesfull:-) see 
code.



d={1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 13:0, 
14:0, 15:0,
16:0, 17:0, 18:0, 19:0, 20:0, 21:0, 22:0, 23:0, 24:0, 25:0, 26:0, 27:0, 
28:0,
29:0, 30:0, 31:0, 32:0, 33:0, 34:0, 35:0, 36:0, 37:0, 38:0, 39:0, 40:0, 
41:0, 42:0,
43:0, 44:0, 45:0}

done=0
fd = open("draw__output.txt",'r')
while not done:
line = fd.readline()
if line == '':
done = 1
else:
   for i in line:
  d[i] = int(d[i])+ 1




>From: "Alan Gauld" <[EMAIL PROTECTED]>
>To: tutor@python.org
>Subject: Re: [Tutor] Newbie question: join() string method
>Date: Mon, 27 Nov 2006 18:46:04 -
>
>"Moedeloos Overste" <[EMAIL PROTECTED]> wrote
>
> > One question: When I run the program from IDLE it writes the data to
> > file
> > but when I run it from the command prompt(win32)  it doesn't. why is
> > this?
>
>How do you know? Have you searched for the file?
>Or are you looking in the same file that IDLE produced?
>I would expect the code to create a new file in the current
>directory - wherever you started the program. Did you look
>there?
>
>---
>while vDraws > 0:
> LotNumbers = random.sample(range(1,45), 6) #random numbers from
>range
>into list)
> strgOutput=",".join(str(i) for i in LotNumbers)
>#??converting list
>to string to store it.
> fout = open("draw__output.dat", "a")
> fout.write(strgOutput + "\n")  #writing string to file
> fout.close()
>--
>
>It's probably better to put the open/close outside the loop
>
>fout = open(...,'w')
>while vDraws > 0
> ...
> fout.write(strgOutput + '\n')
> vDraws -= 1
>fout.close()
>
>That way you can use 'w' to write the file(unless you really want the
>previous runs to be in there too.) Also this will save the program
>opening and closing the file for each line which is quite a slow
>process.
>
>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

_
Windows Live Mail: Slim, Persoonlijk, Betrouwbaar en het blijft natuurlijk 
gratis! http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl

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


Re: [Tutor] adjust key bindings

2006-11-27 Thread John CORRY
Luke,

Thanks for the response.  I am trying to help someone out on the Pygtk
list.  I forgot to enclose his reasons for wanting to assign the key
press.  They are below:  It looks like he likes his keys set out in a
certain way.  I am happy with mine the way they are!  I used "z" in
isolation just to get the logic right.  I know I can assign a function
to Z and make the Treeview move down one row and select the row below
but I don't want to have to do this for all the different buttons.  I am
trying to do it without having to assign and call a whole load of
different functions.

> I just start to use pygtk ... so it is just my first
> question :-)
> 
> I would like to create a small file manager based on 'lfm' (curses 
> based file manager). I used glade for the gui and I am able to display

> the existing files and directories using two treeview widgets.
> Now, at the beginning I am kind of stuck with the key bindings. In
'lfm'
> it was pretty easy to define special key bindings:
> 
> 
>   keytable = {
>   # movement
>   ord('p'): 'cursor_up',
>   ord('k'): 'cursor_up',
>   ord('K'): 'cursor_up2',
>   ord('P'): 'cursor_up',
>   curses.KEY_UP: 'cursor_up',
>   ord('n'): 'cursor_down',
>   ord('j'): 'cursor_down',
>   ord('J'): 'cursor_down2',
>   ord('N'): 'cursor_down',
>   curses.KEY_DOWN: 'cursor_down',
>   curses.KEY_PPAGE: 'page_previous',
>   curses.KEY_BACKSPACE: 'page_previous',
>   0x08: 'page_previous', # BackSpace
>   0x10: 'page_previous', # Ctrl-P
>   curses.KEY_NPAGE: 'page_next',
>   ord(' '): 'page_next',
>   0x0E: 'page_next', # Ctrl-N
>   curses.KEY_HOME: 'home',
>   0x16A: 'home',
>   ord('H'): 'home',
>   0x001: 'home',
>   curses.KEY_END: 'end',
>   ord('G'): 'end',
>   0x181: 'end',
>   0x005: 'end',
>   ord('h'): 'cursor_left',
>   ord('l'): 'cursor_right',
>   curses.KEY_LEFT: 'cursor_left',
>   curses.KEY_RIGHT: 'cursor_right',
>   ord('g'): 'goto_dir',
>   0x13: 'goto_file', # Ctrl-S
>   0x14: 'tree',  # Ctrl-T
>   ord('0'): 'bookmark_0',
>   ord('1'): 'bookmark_1',
>   ...
> 
> 
> with such a keytable I am able to bind different 'def's to every 
> existing key. As you can see, I like it a lot to use 'vim-like' keys 
> for moving around; 'j' and 'k' to move a row up and down. In glade I 
> found those 'accelerators', but it is just for certain functions.
> Does anyone have an idea about using such a keybinding in
> pygtk? Would be nice!
>

I have attempted to answer his question but I am not sure I am on the
right track.  Is there a better way to do it?

Regards,

John.

-Original Message-
From: Luke Paireepinart [mailto:[EMAIL PROTECTED] 
Sent: 27 November 2006 01:30
To: [EMAIL PROTECTED]
Cc: tutor@python.org
Subject: Re: [Tutor] adjust key bindings

John CORRY wrote:
> Hi All,
>
> I have been trying to bind the "z" key to the "Down" key using Python
> 2.4, Glade 2 and Pygtk.  I have posted this problem on the Pygtk list
> but have had no response.  I was hoping somebody on the tutor list
could
> help.  I think that I am close.  I can capture the "z" key press and
> assign a "Down" key press but I can't get the "Down" key press to
> register on the Treeview in the GUI.
>
> Any thoughts greatly appreciated.
>
> Thanks,
>
> John.
I highly doubt that what you want to do when someone hits a 'z' is to 
generate a 'down' keypress.
What if the user assigned the down key to z?
then you'd have a
z -> down -> z -> down ->  infinite loop.
What I expect you want is that each key, z and down, perform the same 
action.
In other words, they both call the same function.
So basically what you'd want is something like this (non-pyGTK specific 
code)

def aFunc():
print "Hello, World!"

bindKey = {'down':aFunc}

keypress = raw_input("What keypress do you want to perform?")

bindKey[keypress]()#this will call the 'aFunc' function if they type 
'down', otherwise, it'll probably crash.

bindKey['z'] = aFunc

bindKey['z']()# now performs the same function as
bindkey['down']()#this does.

If you really do want to generate 'down' keypresses when someone hits 
'z', please explain why, and I will try to the best of my abilities to 
help you in that regard!
Good Luck!
-Luke
>
>   



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


Re: [Tutor] e: moving from pack to grid in tkinter

2006-11-27 Thread John Fouhy
On 28/11/06, Tony Cappellini <[EMAIL PROTECTED]> wrote:
> But i'm trying to get away from pack() and use grid()

Why?  Sometimes pack() is the right option.  You can do things with
pack that you can't do with grid.  You can use both in your program --
just don't use both in the same frame.

> Well thanks anyway. The pdf has really confused me.
> Is there an official tkinter.org doc reference or something?

Um, not really.  There are official Tk docs (at http://tcl.tk), and a
few sites around with Tkinter docs (effbot, New Mexico Tech), but
nothing "official".

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


Re: [Tutor] Newbie question: join() string method

2006-11-27 Thread wesley chun
> strgOutput=",".join(str(i) for i in LotNumbers)   #??converting list
> to string to store it.

note that here, you are *not* converting the list to a string to store
it.  you are converting each list member to a string and creating a
new "list" (actually generator [expression]) for the string.join()
method to takes its contents (now strings) and concatenate them
together into one large string delimited by ','s.

cheers,
-- 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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] AttributeError: 'list' object has no attribute 'copy'

2006-11-27 Thread Keith Troell

On Nov 27, 2006, at 1:55 PM, Kent Johnson wrote:

> Keith Troell wrote:
>> I'm a rank beginner. I am running Python 2.3.5 on the Terminal of  
>> Mac  OSX.
>> The Tutorial (5.1) mentions list.copy as a list method. When I run
>
> Are you sure? I don't see it here:
> http://www.python.org/doc/2.3.5/tut/ 
> node7.html#SECTION00710

Nope, it's not there. Must have had a brain spasm...

>
>> scores = [1, 2, 3]
>> copy = scores.copy ()
>> I get: AttributeError: 'list' object has no attribute 'copy'
>
> You can copy a list with the list() function or by slicing:
> copy = list(scores)
> or
> copy = scores[:]
>

That'll work. Thanks

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


Re: [Tutor] How to generate permutations of a given string

2006-11-27 Thread Carroll, Barry
Hello, Asrarahmed

> -Original Message-
> Date: Mon, 27 Nov 2006 12:06:54 +
> From: "Asrarahmed Kadri" <[EMAIL PROTECTED]>
> Subject: [Tutor] How to generate permutations of a given string
> To: tutor-python 
> Message-ID:
>   <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hello folks,
> 
> 
> Can someone help me with a simple algorithm to generate permutations
of a
> given string:
> 
> Input string:>> 'abc'
> 
> Output:--->>  ['abc','acb','bac','bca','cab','cba']
> 
> Thanks.
> Regards,
> Asrarahmed Kadri
> -
> To HIM you shall return.

I'm not sure these qualify as "simple", but they work.  This was one of
my very first projects in Python, so it may be more complicated than
necessary.  

##
def permute (word):
"""
Accepts a string. 
Returns a list of all permutations of the string using all
characters.  
"""
retList=[]
if len(word) == 1:
# There is only one possible permutation
retList.append(word)
else:
# Return a list of all permutations using all characters
for pos in range(len(word)):
# Get the permutations of the rest of the word 
permuteList=permute2(word[0:pos]+word[pos+1:len(word)])
# Now, tack the first char onto each word in the list
# and add it to the output
for item in permuteList:
retList.append(word[pos]+item)
return retList
##

Regards,
 
Barry
[EMAIL PROTECTED]
541-302-1107

We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed


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


[Tutor] Convert string to file handle

2006-11-27 Thread Shitiz Bansal
Hi,
i have a string abc.
i need a file handle f  pointing to a file which contains this string.
is there any way i can achieve this without actually writing the contents to a 
file and then opening it? for performance reasons i want to run this entire 
process within the physical memory!

Thanks,
Shitiz

 __
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Convert string to file handle

2006-11-27 Thread John Fouhy
On 28/11/06, Shitiz Bansal <[EMAIL PROTECTED]> wrote:
> Hi,
> i have a string abc.
> i need a file handle f  pointing to a file which contains this string.
> is there any way i can achieve this without actually writing the contents to
> a file and then opening it? for performance reasons i want to run this
> entire process within the physical memory!

Have a look at the StringIO module:
http://www.python.org/doc/current/lib/module-StringIO.html

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


Re: [Tutor] How to generate permutations of a given string

2006-11-27 Thread John Fouhy
On 28/11/06, Carroll, Barry <[EMAIL PROTECTED]> wrote:
> I'm not sure these qualify as "simple", but they work.  This was one of
> my very first projects in Python, so it may be more complicated than
> necessary.

This is an alternative approach:
http://mail.python.org/pipermail/tutor/2005-May/038059.html

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


Re: [Tutor] Convert string to file handle

2006-11-27 Thread Bob Gailer
Shitiz Bansal wrote:
> Hi,
> i have a string abc.
> i need a file handle f  pointing to a file which contains this string.
For what?
> is there any way i can achieve this without actually writing the 
> contents to a file and then opening it? for performance reasons i want 
> to run this entire process within the physical memory!
Depends on what for.

-- 
Bob Gailer
510-978-4454

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


Re: [Tutor] Tutor Digest, Vol 33, Issue 100

2006-11-27 Thread Danny Yoo
> done=0
> fd = open("draw__output.txt",'r')
> while not done:
>line = fd.readline()
>if line == '':
>done = 1
>else:
>   for i in line:
>  d[i] = int(d[i])+ 1

Code simplification: you can do a loop directly across files.  Files 
provide an "iterator" that allow us to march line by line across it.  The 
above code can be transformed into:

#
fd = open("draw__output.txt",'r')
for line in fd:
 for i in line:
 d[i] = int(d[i])+ 1
#

For more information about iterators, see:

 http://www.python.org/dev/peps/pep-0234/

In particular, skip down to the Dictionary Iterators and File Iterators 
sections: they should help show how to use the standard iterators types in 
Python.

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


Re: [Tutor] ashamed

2006-11-27 Thread Alan Gauld

"Moedeloos Overste" <[EMAIL PROTECTED]> wrote

> d={1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 
> 13:0,
> 14:0, 15:0,
>16:0, 17:0, 18:0, 19:0, 20:0, 21:0, 22:0, 23:0, 24:0, 25:0, 26:0, 
> 27:0,
> 28:0,
>29:0, 30:0, 31:0, 32:0, 33:0, 34:0, 35:0, 36:0, 37:0, 38:0, 39:0, 
> 40:0,
> 41:0, 42:0,
>43:0, 44:0, 45:0}

You probably don't need to initialise all the values, just test to
see if its already set (either by catching any KeyError exceptions)
or use get() method to return zero as a default as you go (see
my code below).

> done=0
> fd = open("draw__output.txt",'r')
> while not done:
>line = fd.readline()
>if line == '':
>done = 1

You can replace all of that with a Python for loop:

for line in open("draw_output.txt"):

>   for i in line:
>  d[i] = d.get(i,0)+ 1

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


[Tutor] How to Automatically generate a synopsis for a Module

2006-11-27 Thread Tim Johnson
Hello: 
If I start the python interpreter, then import a file, and
type 
help(module_name_here)
Documentation about the module is displayed in detail.

I would like to write a python script that 
Searches for all python scripts in a directory, and
extracts (initially) the description for each script as
a string so that I can print to stdout.

IOWS:
I want to extract what help(module) would display in
the python interpreter.

Any ideas? :-) 
TIA
tim

-- 
Tim Johnson <[EMAIL PROTECTED]>
  http://www.alaska-internet-solutions.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to Automatically generate a synopsis for a Module

2006-11-27 Thread Tim Johnson
  Amazing what google can do
 I think pydoc does it all...
 pydoc -p 1234 sets up the server what what I need.
 Never mind! :-)
 tim

* Tim Johnson <[EMAIL PROTECTED]> [061127 17:24]:
> Hello: 
> If I start the python interpreter, then import a file, and
> type 
> help(module_name_here)
> Documentation about the module is displayed in detail.
> 
> I would like to write a python script that 
> Searches for all python scripts in a directory, and
> extracts (initially) the description for each script as
> a string so that I can print to stdout.
> 
> IOWS:
> I want to extract what help(module) would display in
> the python interpreter.
> 
> Any ideas? :-) 
> TIA
> tim
> 
> -- 
> Tim Johnson <[EMAIL PROTECTED]>
>   http://www.alaska-internet-solutions.com
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Tim Johnson <[EMAIL PROTECTED]>
  http://www.alaska-internet-solutions.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with Tkinter, please

2006-11-27 Thread Dick Moores
At 08:55 PM 11/26/2006, you wrote:
>On 27/11/06, Dick Moores <[EMAIL PROTECTED]> wrote:
>>I have a question about the "EXIT" button's command,
>>command=sys.exit. It fails to quit the program when I've entered too
>>large an integer for factoring, or for prime testing, and I don't
>>want to wait any more for the result. When I'm running the program
>>from my Win XP console, I can quit with a simple Ctrl+C, but this
>>won't be available to the friend I may send the executable to. Is it
>>possible to build in a way to smoothly interrupt, say, a factoring
>>process that's taking too long?
>
>Well, as you may know, the "core" of Tkinter (or most GUIs, I think)
>is the event loop.  The event loop basically does this:
>1. Look for events (mouse clicks, keypresses, etc.)
>2. Process events (update widgets, call callbacks)
>3. GOTO 1.
>
>So, in this specific case, the event loop is doing this:
>
>1. Look for events ==> user clicks on "factorise" button.
>2. Process events ==> call "factorise" button callback.
>3. Callback runs...
>4. ...callback completes.
>5. Look for events ==> user clicks on "exit" button.
>6. Process events ==> call "exit" button callback.
>7. Python exits.
>
>So, you can see why clicking your exit button won't interrupt the
>calculation in progress.  The only way you can do that is if you can
>get the event loop to start looking for events again before the
>callback finishes.  And that means THREADS.

Thanks, John. I know nothing about threading. I see that Chun's _Core 
Python Programming_, 2nd ed. has a 30-page chapter on it. A good 
place to start?

Dick


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


[Tutor] Rounding a float to n significant digits

2006-11-27 Thread Dick Moores
I just dug this Tim Smith creation out of the Tutor archive.

def round_to_n(x, n):
"""
Rounds float x to n significant digits, in scientific notation.
Written by Tim Peters. See his Tutor list post of 7/3/04 at
http://mail.python.org/pipermail/tutor/2004-July/030324.html
"""
if n < 1:
raise ValueError("number of significant digits must be >= 1")
return "%.*e" % (n-1, x)

Thought others might find it of use.

Dick Moores

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


Re: [Tutor] Please suggest a python book

2006-11-27 Thread wesley chun
> I am new to python. I have a vacation of 15 days. Please suggest me a python
> book (or a website).
>
> I already know C language. I have heard much about Python.


there was a long thread about this in the main newsgroup several
months ago... i suppose these threads show up annually.  anyway, it
was an in-depth back-n-forth, and hopefully you will be able to find
something to your liking.  some discussion on my book, core python
programming, appears in the final couple of theads.

http://groups.google.com/group/comp.lang.python/browse_thread/thread/4a9e9a76c623e451
http://groups.google.com/group/comp.lang.python/browse_thread/thread/a3157bfacca26a3
http://groups.google.com/group/comp.lang.python/browse_thread/thread/839a4afb79b7c258

hope this helps!
-- 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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with Tkinter, please

2006-11-27 Thread wesley chun
> >>I have a question about the "EXIT" button's command,
> >>command=sys.exit. It fails to quit the program when I've entered too
> >>large an integer for factoring, or for prime testing, and I don't
> >>want to wait any more for the result.
> >
> >So, you can see why clicking your exit button won't interrupt the
> >calculation in progress.  The only way you can do that is if you can
> >get the event loop to start looking for events again before the
> >callback finishes.  And that means THREADS.
>
> Thanks, John. I know nothing about threading. I see that Chun's _Core
> Python Programming_, 2nd ed. has a 30-page chapter on it. A good
> place to start?


it is a good place to start, but there isn't a combination
GUI+threading example in the book... perhaps 3rd edition

in order to make your app "do what you want," you need to have the
main thread running the GUI, e.g., mainloop(), and fire off threads to
do the calculations.  if your operating system supports daemon
threads, set your unimportant threads as daemon threads -- from the
docs:

"The entire Python program exits when no active non-daemon threads are left."

http://docs.python.org/lib/thread-objects.html

if your OS threads don't support this, then you have to manage a
shared "QUIT" variable which you can set in your quit routine which
calls root.quit() if root is your main Tk window (sys.exit() is a
little rough), and have your worker threads continually check the
status of such a flag during execution. when the user quits, this flag
can be set, telling the workers to put down their shovels and go home.
 :-)

hope this helps!
-- 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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor