[Pythonmac-SIG] Python Help?

2005-06-02 Thread Matthew S-H
I've tried finding the solution to this problem using Google as well as spending hours fooling around to try to fix the problem.I'm a bit new at programming and decided to take Python as a first language.I know I could easily get around this by doing it a different way, but I wanted to figure out why this way wasn't working.Here is the code:#---START---##SETUPimport stringfrom string import *#--def pigTranslator(text):    list = text.split()    list = puncSep(list)    wordsWithCaps = capRemem(list)    list = makeLC(list)    list = pigTranslate(list)    list = capFromMem(list, wordsWithCaps)    list = addSpaces(list)    return join(list,"")#--def pigLatin(word):    if word[0] in "aeiouAEIOU":        return word + "way"    else:        count = 1        for char in word[1:]:            if char in "aeiouAEIOUyY" and not (word[count] == "u" and word[count - 1] == "q"):                return word[count:] + word[:count] + "ay"            else:                count = count + 1        return word + "ay"#--##Seperates words with punctuation into 2 seperate words.def puncSep(list):    currentWord = -1    for word in list:        currentWord = currentWord + 1        if word[-1] in punctuation:#            list = list[:currentWord] + [word[0:-1], word[-1]] + list[currentWord + 1:]            list[currentWord:currentWord + 1] = [word[:-1], word[-1]]            currentWord = currentWord + 1    return list#--##Creates a list stating the word# of each word beginning with a capital.def capRemem(list):    currentWord = -1    wordsWithCaps = [[], []]    for word in list:        currentWord = currentWord + 1        if word != upper(word):            if word[0] in uppercase:                wordsWithCaps[1] = wordsWithCaps[1] + [currentWord]        else:            wordsWithCaps[0] = wordsWithCaps[0] + [currentWord]    return wordsWithCaps#--##Makes all words in list completely lowercasedef makeLC(list):    currentWord = -1    for word in list:        currentWord = currentWord + 1        list[currentWord] = lower(word)    return list#--def pigTranslate(list):    currentWord = -1    for word in list:        currentWord = currentWord + 1        if word not in punctuation:            list = list[:currentWord] + [pigLatin(word)] + list[currentWord + 1:]    return list#--def capFromMem(list, wordsWithCaps):    for reference in wordsWithCaps[1]:        list[reference] = capitalize(list[reference])    for reference in wordsWithCaps[0]:        list[reference] = upper(list[reference])    return list#--def addSpaces(list):    lastWord = -1    for word in list[1:]:        lastWord = lastWord + 1        if word not in punctuation:            list[lastWord] = list[lastWord] + " "    return list#--##Function Callsw = raw_input("enter a sentence: ")print pigTranslator(w)print ""#---END---Here is the code I am having trouble with:##Seperates words with punctuation into 2 seperate words.def puncSep(list):    currentWord = -1    for word in list:        currentWord = currentWord + 1        if word[-1] in punctuation:#            list = list[:currentWord] + [word[0:-1], word[-1]] + list[currentWord + 1:]            list[currentWord:currentWord + 1] = [word[:-1], word[-1]]            currentWord = currentWord + 1    return listThe line that is commented out is the way I had been doing it (which worked fine).  I just wanted to try doing it this way.However, when this runs (use "U?" as an example) it returns an error ("IndexError: string index out of range").I was wondering if any of you have any idea why this might be happening and how I might get around this without going about it a different way.Also, if anyone could be so kind as to explain the difference in effect between the first method and the second method.Thank in advance to anyone who replies with some help.~Matt
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Python Help?

2005-06-02 Thread Charles Hartman
On Jun 1, 2005, at 10:33 PM, Matthew S-H wrote:##Seperates words with punctuation into 2 seperate words.def puncSep(list):    currentWord = -1    for word in list:        currentWord = currentWord + 1        if word[-1] in punctuation:#            list = list[:currentWord] + [word[0:-1], word[-1]] + list[currentWord + 1:]            list[currentWord:currentWord + 1] = [word[:-1], word[-1]]            currentWord = currentWord + 1    return listA couple of problems here (though you'll get better advice from more experienced people). You start with a list of strings, but your code replaces one (or more) of them, not with a different string or two strings, but with a tuple whose elements are two strings. The comma is what does that. Then (I thnk) you're expecting the size of your list to adjust itself, so that the index of the last element will be one larger than it was before the substitution. But the list's length -- the number of elements in the list -- hasn't changed; it's just that one of them has been replaced with a tuple, a different kind of object from a string.One easy (not necessarily efficient) way to revise it would be (off the top of my head without testing)        list[currentWord] = word[:-1]        list.insert(currentWord + 1, word[-1])(though there are more elegant ways to do it without using the currentWord indexing variable).By the way, "list" is an operator in Python (it turns its argument into a list), so it's a bad idea to use that as the name of a variable.Charles Hartman___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Python Help?

2005-06-02 Thread Chris Barker
Charles Hartman wrote:
> On Jun 1, 2005, at 10:33 PM, Matthew S-H wrote:

>> list[currentWord:currentWord + 1] = [word[:-1], word[-1]]

> You start with a list of strings, but your code replaces one (or more) 
> of them, not with a different string or two strings, but with a tuple 
> whose elements are two strings. The comma is what does that.

Well, no. It's a 2-element list, not a tuple (the [ ] make it a list), 
and he's assigning it to a slice, which should work:

 >>> l = [1,2,3,4]
 >>> l[2:3] = [5,6]
 >>> l
[1, 2, 5, 6, 4]
 >>>

So what is going on? I wrote a little test, and inserted a print statement:

import string
##Separates words with punctuation into 2 separate words.
def puncSep(list):
 currentWord = -1
 for word in list:
 currentWord = currentWord + 1
 print currentWord, word
 if word[-1] in string.punctuation:
##list = list[:currentWord] + [word[0:-1], word[-1]] + 
list[currentWord + 1:]
 list[currentWord:currentWord + 1] = [word[:-1], word[-1]]
 currentWord = currentWord + 1
 return list


#L = "This is a sentence.".split()
L = ["Word?"]
print L
print puncSep(L)

Running it gave me:
[EMAIL PROTECTED] junk $ ./piglatin.py
['Word?']
0 Word?
2 ?
4
Traceback (most recent call last):
   File "./piglatin.py", line 113, in ?
 print puncSep(L)
   File "./piglatin.py", line 103, in puncSep
 if word[-1] in string.punctuation:
IndexError: string index out of range

same error, but I got a hint: the last "word" is an empty string, which 
is why you got the IndexError. So I added another print statement:

 print "adding:", [word[:-1], word[-1]]
 list[currentWord:currentWord + 1] = [word[:-1], word[-1]]

[EMAIL PROTECTED] junk $ ./piglatin.py
['Word?']
0 Word?
adding: ['Word', '?']
2 ?
adding: ['', '?']
4
Traceback (most recent call last):
   File "./piglatin.py", line 114, in ?
 print puncSep(L)
   File "./piglatin.py", line 103, in puncSep
 if word[-1] in string.punctuation:
IndexError: string index out of range

So you are adding an empty string. I'm not totally sure why yet, but I 
see a common trip-up in this code:

Never alter a list while iterating through it with a for loop!

(actually, it's not never, but don't do it unless you know what you are 
doing.)

I'll check if this is the problem by printing the list as we go:
 print "adding:", [word[:-1], word[-1]]
 list[currentWord:currentWord + 1] = [word[:-1], word[-1]]
 print "the list is now:", list

and we get:[EMAIL PROTECTED] junk $ ./piglatin.py
['Word?']
0 Word?
adding: ['Word', '?']
the list is now: ['Word', '?']
2 ?
adding: ['', '?']
the list is now: ['Word', '?', '', '?']
4
Traceback (most recent call last):
   File "./piglatin.py", line 115, in ?
 print puncSep(L)
   File "./piglatin.py", line 103, in puncSep
 if word[-1] in string.punctuation:
IndexError: string index out of range

So what happened? The iteration started with the one word in the list: 
"Word?". Then that was replaced by two words: ["Word", "?"], Now this 
list has two elements, so the iteration continues, and the next word in 
the list is "?". It gets replaced by ["","?"].. whoops, that's not 
supposed to happen!

So what's the solution? two options:
1) make sure you only iterate through the original number of items in 
the list:

replace:
 for word in list:
 currentWord = currentWord + 1

with:
 while currentWord < len(list)-1:
 currentWord = currentWord + 1
 word = list[currentWord]

that's a bit ugly, so I'd rather move the increment to the end of the 
while block:


and move the increment to the end of the loop:

def puncSep(list):
 currentWord = 0
 while currentWord < len(list)-1:
 word = list[currentWord]
 if word[-1] in string.punctuation:
 list[currentWord:currentWord + 1] = [word[:-1], word[-1]]
 currentWord += 1
 currentWord += 1
 return list

Another option, and one I'd probably do, is to create a new, list, 
rather than altering the one you have in place:

def puncSep(list):
 currentWord = 0
 newList = []
 for currentWord, word in enumerate(list):
 if word[-1] in string.punctuation:
 newList.extend([word[:-1], word[-1]])
 else:
 newList.append(word)
 return newList

But what if there is a punctuation mark by itself? (which I suppose is a 
syntax error in the input, but probably best to check for it):

before: ['two', 'words', '?']
after: ['two', 'words', '', '?']

It adds an empty string, which you don't want:
 if len(word)> 1 and word[-1] in string.punctuation:

before: ['two', 'words', '?']
after: ['two', 'words', '?']

There, that's fixed it.

Now, a few style issues:

1) don't use "import *", you can get name clashes, and it's hard to know 
where stuff comes from when you look at your code later.

2) as pointed out, don't use "list" as 

[Pythonmac-SIG] py2app data files

2005-06-02 Thread Charles Moad
 py2app-0.2 sticks the data files for matplotlib in /usr/local/ 
share/share/matplotlib instead of /System/Library/Frameworks/ 
Python.framework/Versions/2.3/share/matplotlib when running  
bdist_mpkg then installing the created package.  Any clues???   
(basemap files go to the wrong place too)

Thanks,
 Charlie
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


[Pythonmac-SIG] MacPython addons and Tiger

2005-06-02 Thread Jon Rosebaugh
I recently did an "Archive & Install" from 10.3 to 10.4. Under 10.3 I
used the version of python that shipped with the OS, and the MacPython
addons. After the upgrade, the IDE didn't work, which wasn't really
any great surprise. What _was_ a surprise, however, was that the
MacPython site doesn't have a new set of addons for 10.4. Are these
still being worked on, or should I try some other method of
development?
-- 
Bloggity: http://blog.inklesspen.com/
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] py2app data files

2005-06-02 Thread Robert Kern
Charles Moad wrote:
>  py2app-0.2 sticks the data files for matplotlib in /usr/local/ 
> share/share/matplotlib instead of /System/Library/Frameworks/ 
> Python.framework/Versions/2.3/share/matplotlib when running  
> bdist_mpkg then installing the created package.  Any clues???   
> (basemap files go to the wrong place too)

Use --install-data=/usr/local . matplotlib will look in 
/usr/local/share/matplotlib . You will need to edit a line somewhere in 
basemap to look for data in /usr/local/share/basemap .

It's better this way; trust me.

-- 
Robert Kern
[EMAIL PROTECTED]

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] MacPython addons and Tiger

2005-06-02 Thread Bob Ippolito
On Jun 2, 2005, at 6:48 PM, Jon Rosebaugh wrote:

> I recently did an "Archive & Install" from 10.3 to 10.4. Under 10.3 I
> used the version of python that shipped with the OS, and the MacPython
> addons. After the upgrade, the IDE didn't work, which wasn't really
> any great surprise. What _was_ a surprise, however, was that the
> MacPython site doesn't have a new set of addons for 10.4. Are these
> still being worked on, or should I try some other method of
> development?

The older ones probably work if you also install TigerPython23Compat  
-- however, you're better off just using Python 2.4.1.  Forget about  
Python 2.3.

-bob

___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


[Pythonmac-SIG] Spe-OSX package, wxGladeOSX updates; Boa Constructor coming

2005-06-02 Thread Kevin Walzer
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi everyone,

I've updated my SPE-OSX package to support Bob's MacPython 2.4.1 package
only, as well as wxPython 2.6.1. I'm now also supporting Tiger only.
I've seen general improvements in stability with the latest versions of
wxPython and Python.

The applications themselves are at the same version except for wxGlade,
which I've updated to v. 0.4 from CVS. There seem to have been a lot of
bug fixes with wxGlade, and I haven't seen the crashes that made earlier
versions very difficult to use.

Finally, I'm working on assembling a Mac package of Boa Constructor
(http://boa-constructor.sourceforge.net), which hasn't run well on OS X
in quite some time. (It was only recently updated to support a version
of wxPython later than 2.4.) It seems to run well with the latest
versions of Python and wxPython, so those will be requirements as well.
I'll post a general announcement when it's available.

- --
Cheers,

Kevin Walzer, PhD
WordTech Software--Open Source Applications and Packages for OS X
http://www.wordtech-software.com
http://www.kevin-walzer.com
http://www.smallbizmac.com.
mailto:[EMAIL PROTECTED]
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.1 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCn9TTJmdQs+6YVcoRAu4IAJ9fHxAbP0ba1XJV0N2iaMtZk5DhRACfUQ3y
vmXAvKVcaJv/eiypDvLS3bM=
=GqVS
-END PGP SIGNATURE-
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Appscript Installer 1.1a1

2005-06-02 Thread Nick Matsakis

On Thu, 2 Jun 2005, Nick Matsakis wrote:

> Finally, on the matter of Python 2.4, is there a standard place that the
> macpython 2.4.1 looks for packages?  /Library/Python/2.4/site-packages,
> perhaps?

So, my understanding is that independent framework builds of macpython
default to only looking in their site-packages folder (in fact, this is
how Apple's installs works, except that their site-packages folders are
softlinks to folders within /Library/Python).

In building the appscript installer, I see two options: either create a
package that is able to find the python frameworks by using Apple's system
for upgrading relocateable bundles, then as the user where to install the
software OR install the 2.4 software in /Library/Python/2.4/site-packages
and provide a couple of .pth files on the disk image that people can drag
into their installation's site package.  I'm strongly leaning towards the
second option, because its a lot less work for me and I think it offers
more flexibility to the end user as well.

Thoughts?

Nick Matsakis
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Appscript Installer 1.1a1

2005-06-02 Thread Bob Ippolito

On Jun 2, 2005, at 10:02 PM, Nick Matsakis wrote:

>
> On Thu, 2 Jun 2005, Nick Matsakis wrote:
>
>
>> Finally, on the matter of Python 2.4, is there a standard place  
>> that the
>> macpython 2.4.1 looks for packages?  /Library/Python/2.4/site- 
>> packages,
>> perhaps?
>>
>
> So, my understanding is that independent framework builds of macpython
> default to only looking in their site-packages folder (in fact,  
> this is
> how Apple's installs works, except that their site-packages folders  
> are
> softlinks to folders within /Library/Python).
>
> In building the appscript installer, I see two options: either  
> create a
> package that is able to find the python frameworks by using Apple's  
> system
> for upgrading relocateable bundles, then as the user where to  
> install the
> software OR install the 2.4 software in /Library/Python/2.4/site- 
> packages
> and provide a couple of .pth files on the disk image that people  
> can drag
> into their installation's site package.  I'm strongly leaning  
> towards the
> second option, because its a lot less work for me and I think it  
> offers
> more flexibility to the end user as well.

MacPython 2.4.1 looks in the site-packages directory embedded in its  
framework.  There is only one supported framework location (/Library/ 
Frameworks), so there's no need to care about alternatives.  If you  
build the installer with py2app's bdist_mpkg, you don't have to care  
about these details, because it does the right thing.

-bob

___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Appscript Installer 1.1a1

2005-06-02 Thread Nick Matsakis

On Thu, 2 Jun 2005, Bob Ippolito wrote:

> MacPython 2.4.1 looks in the site-packages directory embedded in its
> framework.  There is only one supported framework location (/Library/
> Frameworks), so there's no need to care about alternatives.

Frameworks can also appear in application bundles too, though it may not
be the case with the Python 2.4 framework.  It seems like the
/Library/Python system is a better one to standardize on, since it mirrors
the way that a lot of other software is installed on the mac. (e.g.
/Library/Java).  Also, it seems strange to install into a framework that
doesn't exist.  Maybe the 2.4 installer should just be a separate package,
then.

> If you build the installer with py2app's bdist_mpkg, you don't have to
> care about these details, because it does the right thing.

What do you mean?

Nick
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


Re: [Pythonmac-SIG] Appscript Installer 1.1a1

2005-06-02 Thread Bob Ippolito

On Jun 2, 2005, at 11:19 PM, Nick Matsakis wrote:

>
> On Thu, 2 Jun 2005, Bob Ippolito wrote:
>
>
>> MacPython 2.4.1 looks in the site-packages directory embedded in its
>> framework.  There is only one supported framework location (/Library/
>> Frameworks), so there's no need to care about alternatives.
>>
>
> Frameworks can also appear in application bundles too, though it  
> may not
> be the case with the Python 2.4 framework.  It seems like the
> /Library/Python system is a better one to standardize on, since it  
> mirrors
> the way that a lot of other software is installed on the mac. (e.g.
> /Library/Java).  Also, it seems strange to install into a framework  
> that
> doesn't exist.  Maybe the 2.4 installer should just be a separate  
> package,
> then.

What planet are you on? :)

MacPython 2.4.1 only exists in /Library/Frameworks.  No other  
installation location is supported unless you compile it yourself.   
If you compile it yourself with a different prefix, you lose  
privileges to use installers.  So far, nobody cares.  If it's shoved  
into an application, it shouldn't be touched.

An installer, as built by bdist_mpkg, won't install anything unless  
Python is where it expects it to be.  Thus, it's not possible to  
install into a framework that doesn't exist.

Maybe I'm missing something.  Are you saying that you have an  
installer that attempts to install an extension that's "compatible  
with either Python 2.3 or Python 2.4"?  Well, if that's what you're  
saying, then it's not going to work anyway.  Extensions are only  
compatible across micro Python revisions (i.e. Python 2.3.0 and  
Python 2.3.5), if and only if they were linked in such a way that  
allows it (i.e. with PantherPythonFix installed on a Panther machine).

>> If you build the installer with py2app's bdist_mpkg, you don't  
>> have to
>> care about these details, because it does the right thing.
>>
>
> What do you mean?



-bob

___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig