Re: a hobbyist's dilemma

2006-03-29 Thread Jim Sizelove
John Salerno wrote:
> Now that I've learned much of Python, I'm sort of stuck with what to do 
> with it. I'm not a professional programmer, so I don't really have a use 
> for Python now. But I really want to come up with some neat uses for it 
> (for fun, and so I don't just start forgetting it right after I learned 
> it).

Here are some other ideas:

You can find sample code for project ideas at Useless Python 
(http://uselesspython.com) and the Python Cookbook 
(http://aspn.activestate.com/ASPN/Python/Cookbook/).

Hang out at the python tutor mailing list 
(http://mail.python.org/mailman/listinfo/tutor) and learn from others 
and learn by helping others.

Enjoy your python adventures,
Jim Sizelove
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What does "::" mean?

2005-07-21 Thread Jim Sizelove
Robert Kern wrote:
> Michael Hoffman wrote:
> 
>> Robert Kern wrote:
> 
> 
>>> Well, that part's easy at least:
>>>
>>>  live[::-1]
>>>
>>> :-)  And so the circle is complete ...
>>
>>
>> What about reversed(live)? Or if you want a list instead of an 
>> iterator, list(reversed(live))?
> 
> 
> That's fine if you want to iterate over it. Often, especially with 
> strings, you just want an object of the same type back again.
> 
Then you could use:
     ''.join(reversed(live))

though, I think that live[::-1] is the most obvious way to do it.  :-)

Regards,
Jim Sizelove
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Merging overlapping spans/ranges

2005-05-10 Thread Jim Sizelove
Bengt Richter wrote:
> On Tue, 10 May 2005 15:14:47 +0200, Max M <[EMAIL PROTECTED]> wrote:
> 
> 
>>I am writing a "find-free-time" function for a calendar. There are a lot 
>>of time spans with start end times, some overlapping, some not.
>>
>>To find the free time spans, I first need to convert the events into a 
>>list of non overlapping time spans "meta-spans".
>>
>>This nice ascii graph should show what I mean.
>>
>>1) ---
>>2) ---
>>3)   ---
>>4)  -
>>5) -
>>
>>
>>>>---   # meta spans
>>
>>I can then iterate through the meta-spans and find non-busy times.
>>
>>I have written the class below, but it is rather O^2, so I wondered if 
>>anybody has an idea for a better approach?
>>
> 
> Maybe (not tested beyond what you see ;-)

It is with some trepidation that I write this message; after hanging 
around in c.l.p for the better part of a year, I have come to expect 
Bengt's messages to be right-on and error-free.

However this time...  :)
>  >>> def mergespans(spans):
>  ...  start = end = None
>  ...  for s,e in sorted(spans):
>  ...  if start is None: start, end = s,e; continue
>  ...  if s <= end: end = e; continue
>  ...  yield start, end
>  ...  start,end = s,e
>  ...  if start is not None: yield start, end
>  ...
>  >>> spans = [(0,3), (4,7), (2,5), (9,14), (12,17)]
>  >>> list(mergespans(spans))
>  [(0, 7), (9, 17)]

There can be a problem in mergespans if one span fits inside another:

  >>> spans = [(0,5), (4,7), (2,3), (9,14), (12,17)]
  >>> list(mergespans(spans))
  [(0, 3), (4, 7), (9, 17)]

Here is a revised version (not tested beyond what you see ;-)
  >>> def mergespans(spans):
  ... start = end = None
  ... for s,e in sorted(spans):
  ... if start is None:
  ... start, end = s,e
  ... continue
  ... if s <= end:
  ... if end < e:
  ... end = e
  ... continue
  ... yield start, end
  ... start,end = s,e
  ... if start is not None:
  ... yield start, end
  ...
  >>> list(mergespans([(0,5), (4,7), (2,3), (9,14), (12,17)]))
  [(0, 7), (9, 17)]
  >>> list(mergespans([(0,3), (4,7), (2,5), (9,14), (12,17)]))
  [(0, 7), (9, 17)]

Can anyone find any other errors in this? ;-)

With humble regards,
Jim Sizelove
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: key binding with mac

2005-04-26 Thread Jim Sizelove
Eric Texier wrote:
I am just starting to use python on the mac.
How do I get backspace, the arrows up/down and all the control like
ctrl-a to work nicely under the console.
for now I am getting a bunch of ^? ^[[A when I use any tcsh type of 
control.
thanks,
I believe you need to install readline support.  You can get it through
http://pythonmac.org/packages
or via fink.
You will find the Pythonmac-SIG mailing list very helpful for questions 
about Python on the Macintosh.  You can learn more at:
http://www.python.org/sigs/pythonmac-sig/

HTH,
Jim Sizelove
--
http://mail.python.org/mailman/listinfo/python-list


Re: recording data between [ and ]

2005-04-21 Thread Jim Sizelove
Simon Brunning wrote:
On 4/21/05, rbt <[EMAIL PROTECTED]> wrote:
string.between(data,[,])

def between(data, start, end):
return re.findall(re.escape(start) + r'([^]]*)'+ re.escape(end), data)

That's cool!
But it doesn't quite work if the end tag is not ']':
>>> import re
>>> def between(data, start, end):
... return re.findall(re.escape(start) + r'([^]]*)'+ re.escape(end), data)
...
>>> foo = '''   [lsass.exe]
... [System]   stuff
... x [firefox.exe] ..
... '''
>>> print between(foo, '[', ']')
['lsass.exe', 'System', 'firefox.exe']
>>> print between(foo, '<', '>')
['stuff', 'more> stuff\nx
Here's a revised version that will work with other tags:
>>> def between2(data, start, end):
... 	pattern = re.escape(start) + ' # start tag \n' +\
...   r'([^' + re.escape(end) + r']*)' + " # anything except end 
tag \n" +\
...   re.escape(end) + ' # end tag \n'
... 	return re.findall(pattern, data, re.VERBOSE)
...
>>> print between2(foo, '[', ']')
['lsass.exe', 'System', 'firefox.exe']
>>> print between2(foo, '<', '>')
['stuff', 'more', 'qqq']

Regards,
Jim Sizelove
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python / Win32 extensions compatibility with Windows XP

2005-04-12 Thread Jim Sizelove
Matthew wrote:
Hi:
I recently installed Python 2.4 and the Win 32 extensions on
Windows XP.  I had some problems with the COM makepy utility for the
Excel COM libraries.  I reported this problem to the sourceforge bug
tracker.
My question is , is python 2.3 and the win32 extensions more stable
than Python 2.4?
Thank You
Matthew Harelick
There were some memory issues that caused makepy on Python 2.4 to crash 
for some large type libraries (including Excel).  This problem has been 
fixed in build 204 of pywin32, released just today.

Regards,
Jim
--
http://mail.python.org/mailman/listinfo/python-list


Re: Running doctests with unittest

2005-03-09 Thread Jim Sizelove
Thomas Heller wrote:
I'm trying to integrate some doctest tests with unittest.  The tests
must be exposed as one or more subclasses of unittest.TestCase, so I'm
collecting them with a call to doctest.DocTestSuite(), and then add them
to a TestCase class I have created.
The tests seem to run, but they always seem to succeed - I have no idea
why.  Any ideas?
Thomas
---snip---
"""
print "Hi"
print 1213
"""
def func():
"""
>>> print "spam"
>>> print blah
"""
import doctest, unittest
suite = doctest.DocTestSuite()
class TestCase(unittest.TestCase):
pass
for index, test in enumerate(suite._tests):
setattr(TestCase, "test_%d" % index, test)
if __name__ == "__main__":
if 1:
import unittest
unittest.main()
else:
import doctest
doctest.testmod()
---snip---
I can't explain why all the tests seemed to pass, but I tried a 
different approach that works.

Once you have a suite object, you just need to run it.  The 
TextTestRunner works well for that.  I got output errors and failures by 
 doing the following:

if __name__ == '__main__':
import doctest, unittest
suite = doctest.DocTestSuite()
testRunner = unittest.TextTestRunner()
testRunner.run(suite)
HTH,
Jim Sizelove
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python COM Makepy Excel 9.0 error

2005-02-10 Thread Jim Sizelove
[EMAIL PROTECTED] wrote:
Has anyone seen this?  When I run the COM Makepy utility and choose
Microsoft Excel 9.0 Object Library (1.3), I get an application error
message:
PythonWin: Pythonwin.exe - Application Error
The instruction at "0x77fcc87" referenced memory at "0x0307a705".  The
memory could not be "read".
I do not get this error when I choose the Microsoft Office 9.0 Object
Library (2.1).
I have tried this from one other computer and got a similar error
message.  I looked all over the 'net for enlightenment and have so far
been unsuccessful.  Does anyone have an idea what can be going on and
how to get around it?
I am using Python 2.4 under Windows 2000SP4.  I have also installed
pywin32 (build 203 for Python 2.4) and py2exe-0.5.4 for Python 2.4.
Python 2.3 and the COM Makepy utility (Python 2.3 version) seems to run
okay.
I am writing a rather complicated set of instructions to manipulate a
series of Excel 2000 spreadsheets and have already added quite a few of
the features from Python 2.4, so I hope I do not have to revert back to
Python 2.3.  Any suggestions?
Thank you, in advance,
Christopher D. Morgan, P.E., Ph.D.
I experienced similar problems when moving some code from Python 2.3 to 
Python 2.4.  Seems that running "makepy -d ..." works.  See Mark 
Hammond's explanation at:

 http://mail.python.org/pipermail/python-win32/2004-December/002743.html
 [python-win32] WMI scripting makepy error with ActivePython 2.4.0
HTH,
Jim Sizelove
--
http://mail.python.org/mailman/listinfo/python-list


Re: Configuring Python for Tk on Mac

2005-01-21 Thread Jim Sizelove
Martyn Quick wrote:
On my desk here at work I have a Mac G4 running Mac OS X v10.2.8.
When I go into a terminal and type "python" up comes a nice python
interface and all seems great.  However when I type "import Tkinter"
I'm greeted by the following error.

import Tkinter
Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/lib/python2.2/lib-tk/Tkinter.py", line 35, in ?
import _tkinter # If this fails your Python may not be configured
for Tk
ImportError: No module named _tkinter
So I guess something about this implementation is not appropriately
configured.  I'm guessing this is the default behaviour since I don't
think anyone did anything special about python when they set up my
machine.
What do I do to set it up so I can use Tkinter?
Thanks... and sorry if this is an FAQ... but I couldn't find the info
easily.
Yours,
Martyn

You probably need to install Tcl/Tk Aqua:
http://tcltkaqua.sourceforge.net
After downloading and installing on my Mac running OS X v 10.2, I am 
able to open IDLE and other Tk apps.

HTH,
Jim Sizelove
--
http://mail.python.org/mailman/listinfo/python-list


Re: When was extended call syntax introduced?

2004-12-21 Thread Jim Sizelove
Edward K. Ream wrote:
Various documentation pages, e.g.
http://www.python.org/doc/2.3.3/lib/non-essential-built-in-funcs.html
state that the apply function has been deprecated since 2.3.
Can anyone tell me when extended call syntax was actually introduced? 
Neither googling nor brief checks of the 'What's new in Python' or the pep's 
has turned up this information.  Thanks.

Edward

Edward K. Ream   email:  [EMAIL PROTECTED]
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html


It looks like the extended call syntax was added in Python 2.0.  See 
"What's New in Python 2.0, 9.1 Minor Langage Changes" at 
http://www.amk.ca/python/2.0/new-python.html

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


Re: Easy "here documents" ??

2004-12-21 Thread Jim Sizelove
Doug Holton wrote:
Bengt Richter wrote:
variable1 = 1
variable2 = 2
s = """
  v = ${variable1}
  v2's value is: ${variable2}
"""
However, Python 3.0 is likely years away.  If you want to know how to 
run code like this today, consult Fredrik Lundh.

Or replace ${...} with equally simple %(...)s in the above and be 
happy ;-)

I'm afraid you are incorrect.  Simply replacing the $ with % in my 
example will not work in Python.
If you would like to use % instead of $, I recommend requesting that 
feature for Python 3.0: http://www.python.org/cgi-bin/moinmoin/Python3.0
Oh, but it does work:
>>> variable1 = 1
>>> variable2 = 2
>>> s = """
...v1 = %(variable1)s
...v2's value is: %(variable2)s
... """
>>> print s % vars()
   v1 = 1
   v2's value is: 2
--Jim
--
http://mail.python.org/mailman/listinfo/python-list


Re: PyCrust: What am I suppose to do?

2004-12-18 Thread Jim Sizelove
It's me wrote:
I am trying out PyCrust and at a lost what to do next.  With the previous
IDE I tried, the IDE pops up the console and the editor.  From the editor, I
can set up breakpoints and debug and so forth.  Yes, I can even run the
script.
With PyCrust, the nice looking 3-pane window pops up with lots of tabs...a
prompt...and then???   Where is the file I am trying to run?  Even the File
options are all greyed out...


You can try PyAlaMode instead.  Along with PyCrust, PyShell, and 
PyAlaCarte, it is part of the Py package of wxPython.  PyAlaMode allows 
you to edit and save files.  It is especially nice for trying some code 
in the interpreter, then copying that code and pasting into the editor 
window without pasting the prompts.

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


Re: Dynamically passing variables to unittest

2004-12-15 Thread Jim Sizelove
Tom Haddon wrote:
Hi Peter,
Yeah, you're right, the term "ConnectString" is a little confusing. Perhaps I 
should change that.
Here's a valid call to DB:
conn=DB.DB('pg','test','localhost',5432,'test','test')
In the context of this unittest, a valid syntax would be (except that this unittest would 
fail, as this is a "good" connection:
self.assertRaises(DB.InvalidConnectString, 
DB.DB,'pg','test','localhost',5432,'test','test')
You can try something like this (not tested):
>>> InvalidStrings=(['pg','test','localhost','5432','test','test'],
['pg','test','local',5432,'test','test'])
>>> for S in InvalidStrings:
... self.assertRaises(DB.InvalidConnectString, DB.DB, *S)
... 
Here the * operator unpacks a sequence when passed in to a function that 
is expecting positional arguments.  See the Python Tutorial, 4.7.4 
Unpacking Argument Lists for a better explanation.
http://docs.python.org/tut/node6.html

hth,
Jim
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help with use of code.InteractiveInterpreter for multiline code

2004-12-03 Thread Jim Sizelove
[EMAIL PROTECTED] wrote:
I'm trying to embed a Python interpreter in a GUI I'm developing, and
I'm having trouble understanding the proper use of
code.InteractiveInterpreter.
[examples of calling the interpreter instance]
What's the proper way to call the interpreter instance for a multiline
example like this?
Thanks in advance,
Rick
I haven't worked with the code module before, but here is my go at it. 
It seems that the call to runsource() needs the entire multiline code at 
once, with newlines including a trailing newline:

>>> import code
>>> a = code.InteractiveInterpreter()
>>> a.runsource('def q():\nprint "hi, a"\n')
False
>>> a.runsource('q()')
hi, a
False
With the InteractiveConsole, you can push each line of code 
individually, like so:

>>> import code
>>> b = code.InteractiveConsole()
>>> b.push('def q():')
True
>>> b.push('print "hi, b"')
True
>>> b.push('')
False
>>> b.runsource('q()')
hi, b
False
HTH,
Jim
--
http://mail.python.org/mailman/listinfo/python-list