[Tutor] delphi, pascal and Python

2009-05-20 Thread Andy Cheesman
Hi people

Is there a Method for wrapping delphi and/or pascal code into python like
SWIG?
I've googled to no avail, Can anyone help me?

Andy

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


[Tutor] Iterating over list of functions

2009-05-20 Thread Robert Berman
Hi,

Given a list of options: option_1...option_n. For each option I have
a corresponding function: func_1. func_n. I have all function names
defined in a list similar to flist = [func_1, func_2,...func_n]
which I know is a legitimate construct having found a similar  construct
discussed by Kent Johnson in 2005.

What I do not know how to do is to call the selected function. If the
index of options is 1, then I want to call func_2; do I code
flist[index]? I do not think Python has a branch indirect construct so I
cannot use anything similar to that methodology.  What is the best
approach to take to solve this problem?

Thank you for any assistance, hints, solutions, and guidelines.

Robert




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


Re: [Tutor] delphi, pascal and Python

2009-05-20 Thread Christian Witts

Andy Cheesman wrote:

Hi people

Is there a Method for wrapping delphi and/or pascal code into python like
SWIG?
I've googled to no avail, Can anyone help me?

Andy

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

  

Take a look at this topic http://forum.qgis.org/viewtopic.php?f=5&t=4341
It's from November 2008 so should still be current.

--
Kind Regards,
Christian Witts


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


Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Lie Ryan

Robert Berman wrote:

Hi,

Given a list of options: option_1...option_n. For each option I have
a corresponding function: func_1. func_n. I have all function names
defined in a list similar to flist = [func_1, func_2,...func_n]
which I know is a legitimate construct having found a similar  construct
discussed by Kent Johnson in 2005.

What I do not know how to do is to call the selected function. If the
index of options is 1, then I want to call func_2; do I code
flist[index]? I do not think Python has a branch indirect construct so I
cannot use anything similar to that methodology.  What is the best
approach to take to solve this problem?



Functions in python is first-class object. It can be passed around like 
regular objects, and can be put into lists like regular objects.


opts = [opt1, opt2, opt3]
funcs = [func1, func2, func3]

for f, o in zip(funcs, opts):
f(o)

funcs[1](o[0]) # equivalent to func2(opt1)

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


Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Christian Witts

Robert Berman wrote:

Hi,

Given a list of options: option_1...option_n. For each option I have
a corresponding function: func_1. func_n. I have all function names
defined in a list similar to flist = [func_1, func_2,...func_n]
which I know is a legitimate construct having found a similar  construct
discussed by Kent Johnson in 2005.

What I do not know how to do is to call the selected function. If the
index of options is 1, then I want to call func_2; do I code
flist[index]? I do not think Python has a branch indirect construct so I
cannot use anything similar to that methodology.  What is the best
approach to take to solve this problem?

Thank you for any assistance, hints, solutions, and guidelines.

Robert




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

  

Why not use a dictionary to do the heavy lifting for you

>>> import string
>>> funcs = {1:string.upper, 2:string.lower}
>>> funcs[1]('this is a simple test')
'THIS IS A SIMPLE TEST'
>>> funcs[2]('THIS IS A SIMPLE TEST')
'this is a simple test'

--
Kind Regards,
Christian Witts


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


Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Emile van Sebille

On 5/20/2009 7:25 AM Robert Berman said...

Hi,

Given a list of options: option_1...option_n. For each option I have
a corresponding function: func_1. func_n. I have all function names
defined in a list similar to flist = [func_1, func_2,...func_n]
which I know is a legitimate construct having found a similar  construct
discussed by Kent Johnson in 2005.

What I do not know how to do is to call the selected function. If the
index of options is 1, then I want to call func_2; do I code
flist[index]?


Yes - with trailing () of course...

def func1():return 1
def func2():return 2
def func3():return 3
flist = [func1,func2,func3]
flist[0]()
flist[1]()
flist[2]()

Emile


 I do not think Python has a branch indirect construct so I

cannot use anything similar to that methodology.  What is the best
approach to take to solve this problem?

Thank you for any assistance, hints, solutions, and guidelines.

Robert




___
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] Iterating over list of functions

2009-05-20 Thread Robert Berman
Thank you, Emile.

That is the exact answer I needed.

Robert


On Wed, 2009-05-20 at 07:48 -0700, Emile van Sebille wrote:

> On 5/20/2009 7:25 AM Robert Berman said...
> > Hi,
> > 
> > Given a list of options: option_1...option_n. For each option I have
> > a corresponding function: func_1. func_n. I have all function names
> > defined in a list similar to flist = [func_1, func_2,...func_n]
> > which I know is a legitimate construct having found a similar  construct
> > discussed by Kent Johnson in 2005.
> > 
> > What I do not know how to do is to call the selected function. If the
> > index of options is 1, then I want to call func_2; do I code
> > flist[index]?
> 
> Yes - with trailing () of course...
> 
> def func1():return 1
> def func2():return 2
> def func3():return 3
> flist = [func1,func2,func3]
> flist[0]()
> flist[1]()
> flist[2]()
> 
> Emile
> 
> 
>   I do not think Python has a branch indirect construct so I
> > cannot use anything similar to that methodology.  What is the best
> > approach to take to solve this problem?
> > 
> > Thank you for any assistance, hints, solutions, and guidelines.
> > 
> > Robert
> > 
> > 
> > 
> > 
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> 
> ___
> 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] Iterating over list of functions

2009-05-20 Thread Robert Berman
Thank you, Christian. This solution was one I was not expecting and am
glad to receive it. It is one I will explore in greater detail later.

Robert


On Wed, 2009-05-20 at 16:44 +0200, Christian Witts wrote:
> Robert Berman wrote:
> > Hi,
> >
> > Given a list of options: option_1...option_n. For each option I have
> > a corresponding function: func_1. func_n. I have all function names
> > defined in a list similar to flist = [func_1, func_2,...func_n]
> > which I know is a legitimate construct having found a similar  construct
> > discussed by Kent Johnson in 2005.
> >
> > What I do not know how to do is to call the selected function. If the
> > index of options is 1, then I want to call func_2; do I code
> > flist[index]? I do not think Python has a branch indirect construct so I
> > cannot use anything similar to that methodology.  What is the best
> > approach to take to solve this problem?
> >
> > Thank you for any assistance, hints, solutions, and guidelines.
> >
> > Robert
> >
> >
> >
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >   
> Why not use a dictionary to do the heavy lifting for you
> 
>  >>> import string
>  >>> funcs = {1:string.upper, 2:string.lower}
>  >>> funcs[1]('this is a simple test')
> 'THIS IS A SIMPLE TEST'
>  >>> funcs[2]('THIS IS A SIMPLE TEST')
> 'this is a simple test'
> 

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


Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Alan Gauld


"Robert Berman"  wrote


Thank you, Christian. This solution was one I was not expecting and am
glad to receive it. It is one I will explore in greater detail later.


A dictionary of functions is the most common way to tackle 
this fairly common requirement. It combines readability with 
ease of maintenance, there is no chance of using the wrong 
index.



Why not use a dictionary to do the heavy lifting for you

 >>> import string
 >>> funcs = {1:string.upper, 2:string.lower}
 >>> funcs[1]('this is a simple test')
'THIS IS A SIMPLE TEST'
 >>> funcs[2]('THIS IS A SIMPLE TEST')
'this is a simple test'



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

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


[Tutor] Iterating through a function list

2009-05-20 Thread Robert Berman
Hi,

Given a list of options: option_1...option_n. For each option I have
a corresponding function: func_1. func_n. I have all function names
defined in a list similar to flist = [func_1, func_2,...func_n]
which I know is a legitimate construct having found a similar  construct
discussed by Kent Johnson in 2005.

What I do not know how to do is to call the selected function. If the
index of options is 1, then I want to call func_2; do I code
flist[index]? I do not think Python has a branch indirect construct so I
cannot use anything similar to that methodology.  What is the best
approach to take to solve this problem?

Thank you for any assistance, hints, solutions, and guidelines.

Robert

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


Re: [Tutor] delphi, pascal and Python

2009-05-20 Thread Alan Gauld


"Andy Cheesman"  wrote


Is there a Method for wrapping delphi and/or pascal code into python like
SWIG?
I've googled to no avail, Can anyone help me?


Try this:

http://membres.lycos.fr/marat/delphi/python.htm

Alan G 



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


Re: [Tutor] Iterating over list of functions

2009-05-20 Thread spir
Le Wed, 20 May 2009 10:25:21 -0400,
Robert Berman  s'exprima ainsi:

> What I do not know how to do is to call the selected function.

If you have options and functions "hard-coded" in lists (or if you get them 
from outside), you can still let python build a dict for you, using "zip":

l1 = [1,2,3]
l2 = [9,8,7]
ll = zip(l1,l2); print ll
d = dict(ll); print d
==>
[(1, 9), (2, 8), (3, 7)]
{1: 9, 2: 8, 3: 7}

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


Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Robert Berman
Alan,

The emphasis of your reply certainly makes me look at the dictionary
solution as the most 'correct' solution to utilize. Before I change the
code I just implemented, let me make sure I understand exactly what you
are saying and what you are advocating. The 'dictionary of functions' is
the 'best' approach because of simplicity and because it minimizes
chances or errors. The one area you did not address is the area of
efficiency. Is this method also the most efficient solution?

The reason I am trying to pin you are two fold. First, you are a
powerful spokesman for 'correct and proper' coding in the Python
community and I take  your opinions and commentaries seriously. Again,
how efficient is this method and is it truly the 'most pythonesque'
oriented code.

Thanks again,

Robert


On Wed, 2009-05-20 at 16:07 +0100, Alan Gauld wrote:

> "Robert Berman"  wrote
> 
> > Thank you, Christian. This solution was one I was not expecting and am
> > glad to receive it. It is one I will explore in greater detail later.
> 
> A dictionary of functions is the most common way to tackle 
> this fairly common requirement. It combines readability with 
> ease of maintenance, there is no chance of using the wrong 
> index.
> 
> >> Why not use a dictionary to do the heavy lifting for you
> >> 
> >>  >>> import string
> >>  >>> funcs = {1:string.upper, 2:string.lower}
> >>  >>> funcs[1]('this is a simple test')
> >> 'THIS IS A SIMPLE TEST'
> >>  >>> funcs[2]('THIS IS A SIMPLE TEST')
> >> 'this is a simple test'
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating over list of functions

2009-05-20 Thread ALAN GAULD
> understand exactly what you are saying and what you are advocating. 
> The 'dictionary of functions' is the 'best' approach because of simplicity 
> and because it minimizes chances or errors. 

Correct. Maintaining synch of indexes between two arrays of data items
is always going to be a risky business.

> The one area you did not address is the area of efficiency. 
> Is this method also the most efficient solution?


It is an efficient solution - I haven't benchmarked it.
But bear in mind that Python uses dictionaries internally to store variables 
and other object references - in cluding functions. Similarly classes in 
Python are implemented as a specialised kind of dictionary. Thus when 
you call a function in Python or access a class feature you are going 
via a dictionary (representing the namespace of the module). Most 
folks seem to find function dispatch fast enough!

Another aspect to consider. If you have a lot of options you are probably 
going to use a search of your options list to find the index before 
applying that index to your list of functions. (If you hard code the 
index then you might as well hard code the function call!) The 
dictionary avoids the need to lookup the index.

> you are a powerful spokesman for 'correct and proper' coding in the 
> Python community and I take  your opinions and commentaries seriously. 

Blush... I have opinions that's true. 
But as for "correct and proper"? -  I usually defer to Kent for that :-)

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


[Tutor] Getting file properties on winodws

2009-05-20 Thread vishwajeet singh
Hi,

I am trying to read properties of file on windows like there is a property
call Keywords on file; I am to read this property independent of file type.

I tried using win32api and win32file but I was not able to find any such
function; GetFileAttributes gives some limited attributes.



-- 
Cheers,
Vishwajeet
http://www.singhvishwajeet.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Getting file properties on winodws

2009-05-20 Thread Tim Golden

vishwajeet singh wrote:

Hi,

I am trying to read properties of file on windows like there is a property
call Keywords on file; I am to read this property independent of file type.


There's an unpolished (indeed, unfinished) example here:

 http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html

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


Re: [Tutor] Getting file properties on winodws

2009-05-20 Thread vishwajeet singh
Thanks that helped.

On Thu, May 21, 2009 at 2:34 AM, Tim Golden  wrote:

> vishwajeet singh wrote:
>
>> Hi,
>>
>> I am trying to read properties of file on windows like there is a property
>> call Keywords on file; I am to read this property independent of file
>> type.
>>
>
> There's an unpolished (indeed, unfinished) example here:
>
>
> http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html
>
> TJG
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Cheers,
Vishwajeet
http://www.singhvishwajeet.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] web cam

2009-05-20 Thread Ricardo Aráoz
Hi, a friend of mine has asked me how difficult would it be to send web
cam images through python program as a hobby project.
Honestly, at this point I have no idea where does he want to put python
in the equation nor what does the project entail. So I'm asking for
pointers to :
a) Any already done projects in python
b) keywords to google for
c) what parts do you think I'll need to put together (web service,
client, browser) and which modules do you recommend.

I know this is a half baked question, just some pointers on where to
start from would be enough.

Thanks


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


Re: [Tutor] web cam

2009-05-20 Thread vince spicer
I've seen this win32 library:

http://videocapture.sourceforge.net via
http://technobabbler.com?p=22

for linux

http://www.antonym.org/libfg

Vince

2009/5/20 Ricardo Aráoz 

> Hi, a friend of mine has asked me how difficult would it be to send web
> cam images through python program as a hobby project.
> Honestly, at this point I have no idea where does he want to put python
> in the equation nor what does the project entail. So I'm asking for
> pointers to :
> a) Any already done projects in python
> b) keywords to google for
> c) what parts do you think I'll need to put together (web service,
> client, browser) and which modules do you recommend.
>
> I know this is a half baked question, just some pointers on where to
> start from would be enough.
>
> Thanks
>
>
> ___
> 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] Iterating through a function list

2009-05-20 Thread Dave Kuhlman
On Wed, May 20, 2009 at 10:02:22AM -0400, Robert Berman wrote:
> 
>Hi,
>Given a list of options: option_1...option_n. For each option I
>have a corresponding function: func_1. func_n. I have all function
>names defined in a list similar to flist = [func_1,
>func_2,...func_n] which I know is a legitimate construct having
>found a similar  construct discussed by Kent Johnson in 2005.
>What I do not know how to do is to call the selected function. If the
>index of options is 1, then I want to call func_2; do I code
>flist[index]?

Yes.

Then to call that function, do:

flist[index](arg1, ...)

or do:

func = flist[index]
func(arg1, ...)

Similarly, if you need to look up a function by name or some other
key then use a dictionary.  For example:

funcs = {'func_name_one': func1, ...}

if name in funcs:
funcs[name](arg, ...)

Python is making this too easy for you, making it hard to spot the
solution.

Think of parentheses as a "function call operator", which you can
apply to any callable value.

- Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] web cam

2009-05-20 Thread Kent Johnson
2009/5/20 Ricardo Aráoz :
> Hi, a friend of mine has asked me how difficult would it be to send web
> cam images through python program as a hobby project.
> Honestly, at this point I have no idea where does he want to put python
> in the equation nor what does the project entail. So I'm asking for
> pointers to :
> a) Any already done projects in python
> b) keywords to google for
> c) what parts do you think I'll need to put together (web service,
> client, browser) and which modules do you recommend.
>
> I know this is a half baked question, just some pointers on where to
> start from would be enough.

Google 'python web cam' for some ideas.

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


[Tutor] Hi everyone

2009-05-20 Thread Doug Reid
Hi,
 
I'm teaching myself Python mainly for to use as a hobby.  I'd like to do 
graphical programs eventually and maybe some simple graphic games.  I feel I'm 
doing well with the tutorial I'm using but it would be nice to have some real 
people to ask questions and opinions, so on that note, I'm having a little 
trouble understanding the following code.  I grasp the basics of what it does 
but am wondering if someone could explain it in simpler terms..
 
In the tutorial, I'm using Tuples and there is a Word Jumble game given to show 
how this can be used.  A tuple of words is created:
 
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
correct=word
 
Then a variable that will hold the jumbled word is defined: jumble=' '
Ok, then a variable: word=random.choice(WORDS) is created to pick a random 
element from WORDS.  I get this part.
 
Now here is the code I'm having trouble following:
 
while word:
    position = random.randrange(len(word))
    jumble += word[position]
    word = word[:position] + word[(position + 1):]
 
position = random.randrange(len(word)). This will create a starting point for 
the program to pick a letter out of a word at random using the length of the 
word as a range I think. Like if the word is 'python' and since it has 6 
letters, random.randrange(len(word)) will pick a random starting point such as 
the letter 'y' for example. It's gonna be inside a while loop, so the next time 
it runs, 'python' will be 'pthon' and the random range will be 5 letters and on 
down till there is no more letters, is this right?
 
Ok, so everyone of the letters that was 'extracted' from the word 'python' will 
be put into the variable 'jumble' that was defined as empty.
 
This is the part that troubles me:
 
word = word[:position] + word[(position + 1):]
 
I know basically it is creating a new string through the while loop for 
extracint letters.  But I don't feel I understand it fully,  The expression is 
confusing to me.  Here is an excerpt from the tutorial that explains it and 
confuses me more:
 
"The next line in the loop,
   word = word[:position] + word[(position + 1):]

creates a new version of word minus the one letter at position position. Using 
slicing, the computer creates two new strings from word. The first slice, 
word[:position], is every letter up to, but not including, word[position]. The 
next slice, word[(position + 1):], is every letter after word[position]. These 
two string are joined together and assigned to word, which is now equal to its 
old self, minus the one letter word[position]."
 
Can someone explain this in simpler terms? I'm sorry this is so lengthy for my 
first post:)
 
Any help will be appreciated
 
Thanks,
 
Doug

 


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


Re: [Tutor] delphi, pascal and Python

2009-05-20 Thread Marc Tompkins
On Wed, May 20, 2009 at 11:10 AM, Alan Gauld wrote:

>
> "Andy Cheesman"  wrote
>
>  Is there a Method for wrapping delphi and/or pascal code into python like
>> SWIG?
>> I've googled to no avail, Can anyone help me?
>>
>
> Try this:
>
> http://membres.lycos.fr/marat/delphi/python.htm
>
> Alan G
>

That's a package to let you embed Python in Delphi; the OP wants to go the
other direction.

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


Re: [Tutor] web cam

2009-05-20 Thread David

Ricardo Aráoz wrote:

Hi, a friend of mine has asked me how difficult would it be to send web
cam images through python program as a hobby project.
Honestly, at this point I have no idea where does he want to put python
in the equation nor what does the project entail. So I'm asking for
pointers to :
a) Any already done projects in python
b) keywords to google for
c) what parts do you think I'll need to put together (web service,
client, browser) and which modules do you recommend.

I know this is a half baked question, just some pointers on where to
start from would be enough.

Thanks


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



You can upload still shots from the web cam with pycurl.

--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Dave Angel

Robert Berman wrote:


Thank you, Christian. This solution was one I was not expecting and am
glad to receive it. It is one I will explore in greater detail later.

Robert


On Wed, 2009-05-20 at 16:44 +0200, Christian Witts wrote:
  

Robert Berman wrote:


Hi,

Given a list of options: option_1...option_n. For each option I have
a corresponding function: func_1. func_n. I have all function names
defined in a list similar to flist = [func_1, func_2,...func_n]
which I know is a legitimate construct having found a similar  construct
discussed by Kent Johnson in 2005.

What I do not know how to do is to call the selected function. If the
index of options is 1, then I want to call func_2; do I code
flist[index]? I do not think Python has a branch indirect construct so I
cannot use anything similar to that methodology.  What is the best
approach to take to solve this problem?

Thank you for any assistance, hints, solutions, and guidelines.

Robert




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

  
  

Why not use a dictionary to do the heavy lifting for you

 >>> import string
 >>> funcs = {1:string.upper, 2:string.lower}
 >>> funcs[1]('this is a simple test')
'THIS IS A SIMPLE TEST'
 >>> funcs[2]('THIS IS A SIMPLE TEST')
'this is a simple test'




(Your top-posting makes the thread hard to follow)

Note that once it's a dictionary, you can use whatever keys you would 
normally use in a dictionary.  For example, if the options are strings, 
use a string as the key.  If they literally are numbers, then a list is 
preferable, but a dictionary gives you other choices.


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


Re: [Tutor] Hi everyone

2009-05-20 Thread Lie Ryan

Doug Reid wrote:

Now here is the code I'm having trouble following:
 
while word:

position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
 
position = random.randrange(len(word)). This will create a starting 
point for the program to pick a letter out of a word at random using the 
length of the word as a range I think. Like if the word is 'python' and 
since it has 6 letters, random.randrange(len(word)) will pick a random 
starting point such as the letter 'y' for example. It's gonna be inside 
a while loop, so the next time it runs, 'python' will be 'pthon' and the 
random range will be 5 letters and on down till there is no more 
letters, is this right?


`position` is an integer. It is the index of the letter we want to put 
into `jumble`. The first line generates a random index, the second line 
copies the letter and append it to `jumble`, and the third line removes 
that letter from `word`.


Ok, so everyone of the letters that was 'extracted' from the word 
'python' will be put into the variable 'jumble' that was defined as empty.
 
This is the part that troubles me:
 
word = word[:position] + word[(position + 1):]


How do we remove a single letter? In python, string is immutable; we 
must create a new string that has the required property (i.e. letter in 
`position` removed).


Rather than removing, we copied the part of string before `position` 
(word[:position]) and after `position` (word[position+1:]) and 
concatenating them together to become our new `word`.


How does the slicing works?

The standard model for "slicing and indexing" in python is like this:

-6  -5  -4  -3  -2  -1
 +---+---+---+---+---+---+
 | p | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6

Let's say `position` is 3 (i.e. the letter "h").

word[:3] is:

-6  -5  -4  -3  -2  -1
 +---+---+---+---+---+---+
 | p | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
 |___|
   word[0:3]

which is "pyt"

while

word[3+1:] -> word[4:] is:

 -6  -5  -4  -3  -2  -1
 +---+---+---+---+---+---+
 | p | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
 |___|
 word[4:6]

which is "on"

concatenating "pyt" + "on", we get "pyton" (i.e. "h" removed)


An extended version of the line that may be more easily digestible:

letters_before_position = word[:position]
letters_after_position = word[position+1:]
word = letters_before_position + letters_after_position


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