hotshot load error

2005-03-09 Thread Charles Hartman
I've used profile before, but wanted to get more information so I  
thought I'd try hotshot instead. It works fine when used like profile.  
But when I run it using this line,
	prof = hotshot.Profile('ScanHot.prof', lineevents=1)
though it runs all right, when I try to load the resulting file I get  
this:

>>> hs = hotshot.stats.load("ScanHot.prof")
Traceback (most recent call last):
  File "", line 1, in ?
  File  
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
python2.3/hotshot/stats.py", line 12, in load
return StatsLoader(filename).load()
  File  
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
python2.3/hotshot/stats.py", line 51, in load
assert not self._stack
AssertionError

--which I don't understand. (The program is a GUI one using wxPython  
2.5, running from within the WingIDE on a Mac under OS 10.3.8, if any  
of that makes a difference. Nothing there prevents hotshot from loading  
a file that's been made without the lineevents=1 argument.)

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


a RegEx puzzle

2005-03-11 Thread Charles Hartman
I'm still shaky on some of sre's syntax. Here's the task: I've got 
strings (never longer than about a dozen characters) that are 
guaranteed to be made only of characters 'x' and '/'. In each string I 
want to find the longest continuous stretch of pairs whose first 
character is 'x' and the second is either mark. So if marks = 
'/xx/xxx///', the "winning" stretch begins at position 2 and is 6 
characters long ('x/xxx/'), which requires finding a second match that 
overlaps the first match (which is just 'xx' in position 1). (When 
there are multiple "winning" stretches, I might want to adjudicate 
among them, but that's a separate problem.) I hope this is clear 
enough.

Here's the best I've come up with so far:
pat = sre.compile('(x[x/])+')
(longest, startlongest) = max([(fnd.end()-fnd.start(), fnd.start()) for 
i in range(len(marks))
			for fnd in pat.finditer(marks,i)])

It seems to work, but it's not very smart. Given the string 
'//xx//', it returns a list whose first seven tuples are all 
'(2,6)', followed by a single '(2,7)', which gets returned because of 
max's secondary attention to the second element in the tuple. In other 
words this code searches from position 0 and finds (2,6), then searches 
from position 1 and finds (2,6) . . .

Obviously it ought to adjust the second argument to pat.finditer 
according to what it finds, instead of blindly stepping. (By the way, I 
can't give 'range' a stepsize of 2 because the "winning" stretch could 
begin with an odd-numbered character.) That sounds like a job for a 
generator. But (1) I haven't been able to work out the details (I'm 
even shakier on generators than I am on regexes!) and (2) that *seems* 
likely to be less efficient, maybe by a large enough factor that the 
obnoxiousness of getting the first search-return seven times is 
something I should just swallow.

What magic am I missing?
Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


hotshot ??

2005-03-11 Thread Charles Hartman
(I asked this a day or two ago; if there was an answer, I missed it.  
Anybody using hotshot?)

I've used profile before, but wanted to get more information so I  
thought I'd try hotshot instead. It works fine when used like profile.  
But when I run it using this line,
	prof = hotshot.Profile('ScanHot.prof', lineevents=1)
though it runs all right, when I try to load the resulting file I get  
this:

>>> hs = hotshot.stats.load("ScanHot.prof")
Traceback (most recent call last):
  File "", line 1, in ?
  File  
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
python2.3/hotshot/stats.py", line 12, in load
return StatsLoader(filename).load()
  File  
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
python2.3/hotshot/stats.py", line 51, in load
assert not self._stack
AssertionError

--which I don't understand. (The program is a GUI one using wxPython  
2.5, running from within the WingIDE on a Mac under OS 10.3.8, if any  
of that makes a difference. Nothing there prevents hotshot from loading  
a file that's been made without the lineevents=1 argument.)

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: a RegEx puzzle

2005-03-11 Thread Charles Hartman

Charles Hartman <[EMAIL PROTECTED]> wrote:
I'm still shaky on some of sre's syntax. Here's the task: I've got
strings (never longer than about a dozen characters) that are
guaranteed to be made only of characters 'x' and '/'.
One possibility is to cheat completely, and depending on memory 
constraints
and how often you need to do this, it might even make sense to do so.
Interesting -- I hadn't thought of that. It would, realistically, 
probably be 2 ** 14 or so, but what the hell. Trouble is, this would 
require me to pick out by hand/eye -- once only, admittedly -- 
something like 16,384 "longests," which I do not care to do. If I can 
compute them, instead, there's no reason not to do it on the fly; it 
doesn 't take *that* long.

There's only two possible values for each character, so you're really
dealing with binary numbers.  The max length is 12 characters (I'm
deliberately being oblivious to where you said "about" :-)), so there 
are
only 2^12 or 4096 possible strings.  You could probably afford to
pre-compute all 4096 strings, use them as dictionary keys, and store a
(start, end) tuple for each key.  Computation then becomes a simple
dictionary lookup.

In each string I
want to find the longest continuous stretch of pairs whose first
character is 'x' and the second is either mark. So if marks =
'/xx/xxx///', the "winning" stretch begins at position 2 and is 6
characters long ('x/xxx/'), which requires finding a second match that
overlaps the first match (which is just 'xx' in position 1). (When
there are multiple "winning" stretches, I might want to adjudicate
among them, but that's a separate problem.) I hope this is clear
enough.
Unfortunately, no, it's not very clear.  In fact, I have no clue what
you're trying to do.  Could you try explaining it again?
See below.

Charles Hartman
Professor of English, Poet in Residence
Hmmm.  Are you, by any chance, looking for meter patterns in verse?
Yes, of course. My program that scans iambic pentameters (Scandroid, 
ver. 0.2a) is available (GPL) on the "Programs" page of my site listed 
below. It pretty much works, which is interesting. (People who know how 
to do this generally don't believe a program can do it.) I'm a week or 
two (knock on wood) from version 1.0, which handles anapestic meters as 
well, and non-pentameter lengths, and do other stuff.

I'm not sure which part of my explanation wasn't clear. The string ('/' 
and 'x' only) will ultimately be divided into groups of one, two, 
three, or four marks, by many complex operations. One approach to the 
whole task (an approach I just discovered a month or two ago, which 
supplements a more chunk-by-chunk method) is to look for the longest 
substring within that string of (perhaps) 10 or twelve mrks, of which 
the following is true: if the substring is divided into pairs, each 
pair consists of a 'x' followed by either a 'x' or a '/'. The general 
problem is to the find the longest possible substring matching an sre 
specfication -- and a general solution to that doesn't seem to be 
obvious, though the need for it must arise often enough.

What I want is code (a line, a function . . .) that returns the 
beginning-point and length of that longest substring. (I could ask for 
the substring itself; it doesn't matter; the information sought will be 
some subset of the information supplied with every sre match object.) 
For my current "best" solution, see previous message. Hope this is any 
clearer.

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: a RegEx puzzle

2005-03-11 Thread Charles Hartman
If I'm understand you right, then I still didn't explain clearly. 
(Surprise!) If the string is '//xx//' then possible matches are at 
position 6 and 7 (both length 2, so "longest" doesn't even come into 
it). My code searches from position 0, then 1, then 2, and so on, to 
catch every possible pattern and then compare them for length.

You seem to be suggesting a different approach, one I hadn't thought 
of: explicitly test series of pairs, rather than the whole remaining 
string at each point, and do this just once starting at 0, and once 
starting at 1. That sounds as though it would work, though the regex 
would have to be called in a different way so as to seek 
non-overlapping patterns (rather than the elaborate precautions I've 
taken to seek overlapping ones) -- I'm not yet sure quite how, and I'm 
not yet clear that it's any more efficient and/or elegant than what 
I've got now. Hm -- lots to think about here. Thank you.

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
pat = sre.compile('(x[x/])+')
(longest, startlongest) = max([(fnd.end()-fnd.start(), fnd.start()) 
for
i in range(len(marks))
for fnd in pat.finditer(marks,i)])
If I'm understanding that correctly, the only way for you to get 
different
best matches are at offsets 0 and 1; offset 2 will yield the same 
matches
as 0, with the possibility of excluding the first two characters -- i. 
e.
any different matches should be guaranteed to be shorter. Therefore

... for i in range(2) ...
instead of
... for i in range(len(marks)) ...
should be sufficient.
Peter
--
http://mail.python.org/mailman/listinfo/python-list


Re: a RegEx puzzle

2005-03-11 Thread Charles Hartman
Thanks -- not only for the code, which does almost exactly what I need 
to do, but for the reminder (thanks also to Jeremy Bowers for this!) to 
prefer simple solutions. I was, of course, so tied up in getting my 
nifty one-liner right that I totally lost sight of how 
straightforwardly the job could be done; and now that I've got it, I've 
also got room to tune it. For instance,  your code keeps the first 
"longest" match if several are equal in length; my program will I think 
do slightly better if I keep the last "longest" instead, and changing 
that required changing > into >=, which even I can't screw up.

Thanks to everyone who's helped on this. Makes me wish I were going to 
pycon.

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
Kent Johnson wrote:
It's pretty simple to put re.search() into a loop where subsequent 
searches start from the character after where the previous one 
matched. Here is a solution that uses a general-purpose longest match 
function:

import re
# RE solution
def longestMatch(rx, s):
''' Find the longest match for rx in s.
Returns (start, length) for the match or (None, None) if no 
match found.
'''

start = length = current = 0
while True:
m = rx.search(s, current)
if not m:
break
mStart, mEnd = m.span()
current = mStart + 1
if (mEnd - mStart) > length:
start = mStart
length = mEnd - mStart
if length:
return start, length
return None, None
pairsRe = re.compile(r'(x[x/])+')
for s in [ '/xx/xxx///', '//xx//' ]:
print s, longestMatch(pairsRe, s)
--
http://mail.python.org/mailman/listinfo/python-list


Re: a RegEx puzzle (end of thread)

2005-03-12 Thread Charles Hartman
Won't extend this except to say thanks to Michael Spencer for another 
version. If I were doing it only once I'd use that. Since I do it more 
than once I should package it as a function.

Thanks.
Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python-list Digest, Vol 18, Issue 208

2005-03-13 Thread Charles Hartman
I know this isnt that big of a problem,
but i cannot think of one reason why they would not allow numbers 
preceded with a 0 to have a number
higher then a 7 in them...
And it seems very inconsistant to me...

Is there a reason for this?
I *love* questions I can answer! Answer: because that's how you tell 
Python you're entering an octal number. (Parallel to 0x for 
hexadecimals.) So beware of 010, which isn't the number of fingers you 
presumably have, unless you don't count the thumbs.

Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie: modifying a string in python

2005-03-16 Thread Charles Hartman
You're right that the code you give will not have the desired effect, because strings (unlike lists) are immutable. But slicing comes to your rescue:
for s in stks:
s = s.strip()
if s[-2:] == 'GR':
s = s[:-2] + 'GF'
-- though I'm sure more experienced people will offer more efficient solutions.

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com

 want to modify a string in the following way :
for s in stks:
  s = s.strip()
  if ( s[-2:] == ‘GR’ ):
  s[-2:]= ‘GF’
 
so, if the last two characters are GR, I want to change
them to GF ( there will be other if statements also but I am
just putting this one here for simplicity ).
 
I think the code is fine but I vaguely remember
reading somewhere in the documentation that
python strings can’t be changed ( only elements of lists can be ) ?.
Is that true or is it okay to do the above.
-- 
http://mail.python.org/mailman/listinfo/python-list

Windows question from Mac guy

2005-03-18 Thread Charles Hartman
I'm sitting here (briefly!) with a Windows machine trying to build a 
distributable for my app. I'm using py2exe and Inno Setup. (This is 
Apple-framework Python 2.3, wxPython 2.5.3.8.) Everything works! Except 
. . .

My app has a data file, scandictionary.txt, that it needs to load when 
it starts up. On Mac it gets stuffed into the app bundle so it's hidden 
and there's no problem finding it. On Windows (XP), Inno Setup is 
putting it where I expected it to be, in the {app} directory along with 
my app's .exe and the various .dlls etc that Python needs. But my app 
isn't finding it. Here's the code I use for that:

TEXTDICTIONARY = 'scandictionary.txt'
. . .
try:
f = open(TEXTDICT, 'rU')
except IOError: # dict file has gone astray
wildcard = "All files (*.*) | *.*"
dlg = wx.FileDialog(None, message="Locate the scandictionary 
file",
defaultDir=os.getcwd(), defaultFile="", 
wildcard=wildcard,
style=wx.OPEN | wx.CHANGE_DIR)
if dlg.ShowModal() == wx.ID_OK:
f = open(dlg.GetPath())
dlg.Destroy()
When it doesn't find the file by itself (why not??), it starts looking 
down in some godawful place in Common or something, which is likely to 
baffle a user. (It baffles me, though yes I *can* navigate to the right 
place.)

This is pretty much the same code I use when the user selects "Load 
text file," and the app goes straight to the right directory (its own 
directory), where it finds a sample text file I supply. Is os.getcwd() 
working differently in the two cases?

Help help, I'm confused. Any help much appreciated.
Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Windows question from Mac guy

2005-03-18 Thread Charles Hartman
On Mar 18, 2005, at 1:36 PM, [EMAIL PROTECTED] wrote:
Here is code that I use, it works both for the script and the exe:
Though that does *not* work on Mac, it *does* work on Windows. Bless 
you sir! I just put a sys.platform condition in and do it your elegant 
way for 'win32' and the simple way for 'darwin'. Many thanks!

Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


Text-to-speech

2005-03-19 Thread Charles Hartman
Does anyone know of a cross-platform (OSX and Windows at least) library 
for text-to-speech? I know  there's an OSX API, and probably also for 
Windows. I know PyTTS exists, but it seems to talk only to the Windows 
engine. I'd like to write a single Python module to handle this on both 
platforms, but I guess I'm asking too much -- it's too hardware 
dependent, I suppose. Any hints?

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Text-to-speech

2005-03-20 Thread Charles Hartman
-- or ". . . a guru named Guido / (Who monitors everything we do) /"  
and ending with something about "looking max in a Speedo," but  
fortunately it's not coming to me at the moment.

The closest I have an answer to your questions about Python and poetry  
(aside from the Scandroid) is a book called *Virtual Muse: Experiments  
in Computer Poetry* which Wesleyan published something close to ten  
years ago; I don't think it's out of print yet, but I don't keep good  
track. It was out of a casual remark there (about a very primitive  
program not even directly talked about in the boolk) that there came a  
casual remark from a reader last year which led to the Scandroid.

Charles Hartman
"The time has come for someone to put his foot down; and that foot is  
me." --Animal House

On Mar 20, 2005, at 2:10 AM, Tim Churches wrote:
Charles Hartman wrote:
Does anyone know of a cross-platform (OSX and Windows at least)  
library
for text-to-speech? I know  there's an OSX API, and probably also for
Windows. I know PyTTS exists, but it seems to talk only to the Windows
engine. I'd like to write a single Python module to handle this on  
both
platforms, but I guess I'm asking too much -- it's too hardware
dependent, I suppose. Any hints?

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
No, but I do wonder how many other users of Python are
poets-in-residence, or indeed, published poets?
And congratulations on the release of Scandroid Version 1.0a (written  
in
Python) on 18.iii.05 (as you elegantly record it).

All this begs the question: Have any poems been written in Python
(similar to the well-known Perl Poetry (see
http://directory.google.com/Top/Computers/Programming/Languages/Perl/ 
Poetry/
)?

Indeed, have any poems ever been written about Python - other than "The
Zen of Python" by Tim Peters? A limerick, even?
There once was a language called Python...
(which is pretty close to having three anapaestic left feet)
or more promisingly, rhyme-wise, but metrically rather worse :
There once was a mathematician named van Rossum...
Tim C
--
http://mail.python.org/mailman/listinfo/python-list


Re: Software for Poets (Was: Re: Text-to-speech)

2005-03-20 Thread Charles Hartman
On Mar 20, 2005, at 4:10 PM, Francis Girard wrote:
Hello M. Hartman,
It's a very big opportunity for me to find someone that both is a poet 
and
knows something about programming.

First, please excuse my bad english ; I'm a french canadian.
My French is a great deal worse than your English; fear not.
I am dreaming to write a software to help french poets to write strict
rigourous classical poetry. Since calssical poetry is somewhat 
mathematical,
a lot of tasks can be automatised :

1- Counting the number of syllabs ("pied" in french) in a verse
2- Checking the rimes ; determining the strength of a rime
3- Checking compliance of a poem to a fixed pre-determined classical 
form (in
french, we have distique, tercet, quatrain, quintain, sixain, huitain,
dizain, triolet, vilanelle, rondeau, rondel, ballade, chant royal, 
sonnet,
etc.)

4- Propose a synonym that will fit in a verse, i.e. with the right 
amount of
syllabs

5- Suggest a missing word or expression in a verse by applying the 
Shannon
text generation principle

First, do you think it may be a useful tool ?
That is a very deep question. (See below.)
What other features you think can make it usefull for a poet ?
The first task of cutting sentences into syllabs (phonetically of 
course, not
typographically) is already done. It's been difficult to get it right 
and to
make it guess correctly with a very very high percentage.

I can very well imagine that the next task is even more difficult. I 
need to
translate text into phonems. Do you know some software that does it ? 
I guess
that voice synthetisers that translates written text into spoken text 
must
first translate the text into phonems. Right ? Do you know if there 
some way
that I can re-use some sub-modules from these projects that will 
translate
text into phonems ?
The problems are hard ones. Getting reliable syllable divisions is, all 
by itself, a heart-breaker in English; I'm not sure whether harder or 
easier in French. (See the module syllables.py in the source code to my 
Scandroid program at the site listed below.)

Rhyme is harder -- I haven't yet tried it in English -- precisely 
because text-to-phoneme is very hard.

I haven't really worked with this, that is, with the sounds of speech 
(though I'm a musician as well as a poet), mostly because it's 
difficult. The projects in my *Virtual Muse: Experiments in Computer 
Poetry"[1], for example, deal almost entirely with language as a 
typographical phenomenon. So does my Scandroid, even though the 
material it's working with is all aimed at and motivated by the 
auditory qualities of poetry.

I do imagine you're right that the text-to-speech people have worked 
out a lot of this. The trouble is that so far I haven't seen 
public-domain code for the guts of such a program, which is what you 
would need.

Interesting to think about which problems change between French and 
English and which do not.

Good luck -- keep me posted.
[1] This was published by Wesleyan Univ Press, what, nine years ago. 
Probably out of print. I do know where to get some copies.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Text-to-speech

2005-03-21 Thread Charles Hartman

Maybe you can bind Festival
(http://www.cstr.ed.ac.uk/projects/festival/download.html) with SWIG.
Presumably somebody could; at this point it's well beyond me. But thank 
you for the suggestion.

Charles Hartman
http://cherry.conncoll.edu/cohar
--
http://mail.python.org/mailman/listinfo/python-list


intrusive posts

2005-03-23 Thread Charles Hartman
On Mar 23, 2005, at 7:10 PM, [EMAIL PROTECTED] wrote:
   7. (",) Do You Want To Know For Sure You Are Going To Heaven?
Is there no way of filtering this recurring offensive material from the 
list?

Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python-list Digest, Vol 18, Issue 391

2005-03-24 Thread Charles Hartman
On Mar 24, 2005, at 6:07 PM, [EMAIL PROTECTED] wrote:
The Python doc, though relatively incompetent, but the author have
tried the best.
I don't mean to be derisive, but the thought that someone who could 
commit this sentence should be complaining about the clarity of the 
writing in the Python docs is funny, and the didactic pose of the whole 
post is . . . derisory. The motive for the post escapes me, or I hope 
it does.

Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


Re: breaking up is hard to do

2005-03-25 Thread Charles Hartman
On Mar 25, 2005, at 5:16 PM, [EMAIL PROTECTED] wrote:
For example I have a class named Indicators. If I cut it out and put it
in a file call Ind.py then "from Ind import Indicators" the class can
no longer see my globals. This is true even when the import occurs
after the config file has been read and parsed.
I'm new enough at this myself so that (a) I understand how you get into 
this problematic situation and (b) you should take what I say with a 
spoon or two of salt.

I find it most useful to think of the problem as *getting rid of global 
variables*. (Compared with some other languages Python practically has 
no globals; module is the most global scope there is.) That means (1) 
designing some kind of clear and efficient package for data that you 
can pass back and forth between classes and modules; and (2) 
redesigning the classes themselves. The redesign is aimed at narrowing 
down how everything gets and sends data -- no more grabbing into the 
global air, but referring to some specific data package (which may be 
an instance of a special class).

The nice thing about this is that by implementing it, you're 
automatically greatly increasing the "object oriented" qualities of the 
program, with the consequent gains: modularity, clarity of interface . 
. . It tends to make the whole program's design clearer and clearer. So 
the rigors you go through in the redesign really pay off in your own 
understanding of your own program -- especially six months later.

I'm sure people who know a lot better what they're talking about will 
have more thorough answers for you.

Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


character-filtering and Word (& company)

2005-03-25 Thread Charles Hartman
I'm working on text-handling programs that want plain-text files as 
input. It's fine to tell users to feed the programs with plain-text 
only, but not all users know what this means, even after you explain 
it, or they forget. So it would be nice to be able to handle gracefully 
the stuff that MS Word (or any word-processor) puts into a file. 
Inserting a 0-127 filter is easy but not very friendly. Typically, the 
w.p. file loads OK (into a wx.StyledTextCtrl a.k.a Scintilla editing 
pane), and mostly be readable. Just a few characters will be wrong: 
"smart" quotation marks and the like.

Is there some well-known way to filter or translate this w.p. garbage? 
I don't know whether encodings are relevant; I don't know what encoding 
an MSW file uses. I don't see how to use s.translate() because I don't 
know how to predict what the incoming format will be.

Any hints welcome.
Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


list-comprehension and map question (simple)

2005-03-27 Thread Charles Hartman
I understand this toy example:
lines = "this is a group\nof lines of\nwords"
def getlength(w): return len(w)
s = map(getlength, [word for ln in lines.split() for word in  
ln.splitlines()])

(now s is [4, 2, 1, 5, 2, 5, 2, 5])
My question is whether there's any compact way to combine function  
calls, like this (which doesn't work):

lines = "this is a group\nof lines of\nwords"
def getlength(w): return len(w)
def timestwo(x): return x * 2
s = map(timestwo(getlength), [word for ln in lines.split() for word in  
ln.splitlines()])

(Under the WingIDE I get this traceback:
"/Applications/WingIDE-Professional-2.0.2/WingIDE.app/Contents/MacOS/ 
src/debug/server/_sandbox.py", line 1, in ?
# Used internally for debug sandbox under external interpreter
  File  
"/Applications/WingIDE-Professional-2.0.2/WingIDE.app/Contents/MacOS/ 
src/debug/server/_sandbox.py", line 1, in addone
# Used internally for debug sandbox under external interpreter
TypeError: unsupported operand type(s) for +: 'function' and 'int'
)

I hope the question is clear enough. I have a feeling I'm ignoring a  
simple technique . . .

Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


Re: list-comprehension and map question (simple)

2005-03-27 Thread Charles Hartman
On Mar 27, 2005, at 11:50 AM, Nicolas Évrard wrote:
I hope the question is clear enough. I have a feeling I'm ignoring a  
simple technique . . .
lambda !
map(lambda x: timestwo(getlength(x)), ...)
Ah, lambda! I've heard so much bad-mouthing of lambda that I forgot to 
learn it . . . This is quite cool, and it looks as though it would work 
with more complicated function calls than the ones in my toy example. 
Thanks.

Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


Re: list-comprehension and map question (simple)

2005-03-27 Thread Charles Hartman
On Mar 27, 2005, at 1:18 PM, Brian van den Broek wrote:
>>> def some_arbitrary_function(y):
... return ( (y * 42) - 19 ) % 12
...
>>> [some_arbitrary_function(len(x)) for x in lines.split()]
[5, 5, 11, 11, 5, 11, 5, 11]
>>>
I could be missing some edge cases, but it seems to me that if you 
have list comps you don't really need map, filter, and the like. The 
map portion can be done by nested function applications in the list 
comp itself.
A good point, and I think I see that. But ultimately what I'm wondering 
is whether a construction like this [1]:

for s in possScansions:
for a in algorithms:
(feet, test) = self.DoAlgorithm(a, s)
complexities[(s, a)] = (self._measureComplexity(feet, 
test), len(feet))

can be condensed in one or more of these ways. (Whether the result 
would be readable / maintainable is a separate question. So is whether 
it would be more efficient. At the moment I'm just trying to get clear 
about the syntax.)

[1] possScansions is a list of strings; algorithms is a list of ints; 
feet is a list of strings; test is a list of Booleans. complexities is 
a dictionary whose keys are those two-item tuples and whose values are 
the integers returned by self._measureComplexity

Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list


Re: list-comprehension and map question (simple)

2005-03-27 Thread Charles Hartman
I very much take your point. And thanks: that answers my syntax 
question (I think!) -- *and* tells me that I don't care.

Charles Hartman
On Mar 27, 2005, at 2:16 PM, [EMAIL PROTECTED] wrote:
...
>>> simpler == complexities
True
>>>
I've not the glimmer of a clue which would be faster, and don't care 
to check -- the evil way could be 5 times faster, and I wouldn't want 
it in my code :-)

Best,
Brian vdB

--
http://mail.python.org/mailman/listinfo/python-list


good design & method calls

2005-03-29 Thread Charles Hartman
I know the answer to this is going to be "It depends . . .", but I want 
to get my mind right. In Fowler's *Refactoring* I read: "Older 
languages carried an overhead in subroutine calls, which deterred 
people from small methods" (followed by the basic "Extract Method" 
advice). In Skip Montanaro's "Python Performance Tips" 
(http://manatee.mojam.com/~skip/python/fastpython.html) I read: ". . .  
use local variables wherever possible. If the above loop is cast as a 
function, append and upper become local variables. Python accesses 
local variables much more efficiently than global variables."

These two pieces of advice imply opposite kinds of code revisions. 
Obviously they have different purposes, and both are right at different 
times. I wonder if anyone has some wisdom about how to think about when 
or how often to do which, how to balance them ultimately, and so on.

Charles Hartman
Professor of English, Poet in Residence
the Scandroid is at: http://cherry.conncoll.edu/cohar/Programs
http://villex.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: good design & method calls

2005-03-29 Thread Charles Hartman
On Mar 29, 2005, at 10:36 AM, Peter Hansen wrote:
Sorry for the rant...  I didn't intend it to head
that way when I started out, but I seem to be on a
bit of an anti-optimization bent today. :-)
No, that's very helpful; thanks.
Charles Hartman
--
http://mail.python.org/mailman/listinfo/python-list