Re: slicing, mapping types, ellipsis etc.

2004-11-30 Thread Kent Johnson
Nick Coghlan wrote:
Jerry Sievers wrote:
Fellow Pythonists;
I am totally puzzled on the use of slicing on mapping types and

It's generally not supported (since slices are designed to work with the 
numerical indices of a sequence, not the arbitrary keys of a mapping).
Section 5.3.3 of the Language Reference seems to say that with extended 
slicing, the slice elements are used to create a key that indexes a 
mapping. "The semantics for an extended slicing are as follows. The 
primary must evaluate to a mapping object, and it is indexed with a key 
that is constructed from the slice list, as follows."

From my understanding of this thread so far, extended slicing is used 
as a form of indexing in Numeric. Are Numeric arrays considered 
mappings? Or is this paragraph in 5.3.3 off the mark?

Maybe instead of referring to mappings it should say "The primary must 
implement __getitem__(), which is called with a value that is 
constructed from the slice list, as follows."

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


Re: slicing, mapping types, ellipsis etc.

2004-11-30 Thread Kent Johnson
Nick Coghlan wrote:
Kent Johnson wrote:
Maybe instead of referring to mappings it should say "The primary must 
implement __getitem__(), which is called with a value that is 
constructed from the slice list, as follows."

In the section you mention, 'mapping' is equivalent to 'has __getitem__ 
defined', and I'd be surprised if it's an isolated usage. Python 
actually has trouble distinguishing between sequences and mappings, as 
anyone who as tried to use the 'isMapping' API would know (the isMapping 
API uses much the same definition as the reference manual does - so all 
sequences show up as mappings, as they map indices and slices to objects).

Section 3 of the reference manual is actually more use for anyone 
developing custom types that override special methods. E.g. proper 
handling of slice objects is described here under container emulation:
http://www.python.org/dev/doc/devel/ref/sequence-types.html
I understand that the distinction between sequences and mappings is 
fuzzy, as both use __getitem__() for access. But the usage of 'mapping' 
in 5.3.3 is inconsistent with the section you refer to. That page says 
"It is also recommended that mappings provide the methods keys(), 
values(), items(), has_key(), get(), clear(), setdefault(), iterkeys(), 
itervalues(), iteritems(), pop(), popitem(), copy(), and update() 
behaving similar to those for Python's standard dictionary objects."

and under __getitem__(): "For *sequence* types, the accepted keys should 
be integers and slice objects."

I just think 5.3.3 is unnecessarily opaque. Particularly since the only 
built-in mapping (dict) doesn't even accept simple slices as indices.

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


Re: access to generator state

2004-12-02 Thread Kent Johnson
Neal D. Becker wrote:
Only one problem.  Is there any way to access the state of a generator
externally?  In other words, the generator saves all it's local variables. 
Can an unrelated object then query the values of those variables?  (In this
case, to get at intermediate results)

You could make the generator a method of a class and store the generator state in instance variables 
instead of local variables:

>>> class Gen:
...   def __init__(self):
... self.i = 0
...   def __call__(self):
... while True:
...   yield self.i
...   self.i += 1
...
>>> g=Gen()
>>> g.i
0
>>> iter=g()
>>> iter.next()
0
>>> g.i
0
>>> iter.next()
1
>>> g.i
1
HTH
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: PySQLLite Speed

2004-12-03 Thread Kent Johnson
Kevin wrote:
Hello All,
I wanted to thank Roger Binn for his email.  He had
the answer to my issue with writing speed.  It's
actual made an incredible change in the preformace.  I
didn't have to go all the way to implementing the
synchronous mode(for my app).  Previously, I was
insert one record at a time.  The key was to write
them all at one time.  I moved up to a 13 meg file and
wrote it to the db in secs.  Now the issue is the 120
meg of RAM consumed by PyParse to read in a 13 meg
file.  If anyone has thoughts on that, it would be
great.  Otherwise, I will repost under a more specific
email.
If your data is (or can be) created by an iterator, you can use this recipe to group the data into 
batches of whatever size you choose and write the individual batches to the db.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279

Kent
Thanks,
  Kevin

db.execute("begin")
while i < TriNum
   db.execute("""insert into TABLE(V1_x)
values(%f),""" (data[i]))
i = i + 1
db.execute("commit")

	
		
__ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail
--
http://mail.python.org/mailman/listinfo/python-list


Refactoring a generator function

2004-12-04 Thread Kent Johnson
Here is a simple function that scans through an input file and groups the lines of the file into 
sections. Sections start with 'Name:' and end with a blank line. The function yields sections as 
they are found.

def makeSections(f):
currSection = []
for line in f:
line = line.strip()
if line == 'Name:':
# Start of a new section
if currSection:
yield currSection
currSection = []
currSection.append(line)
elif not line:
# Blank line ends a section
if currSection:
yield currSection
currSection = []
else:
# Accumulate into a section
currSection.append(line)
# Yield the last section
if currSection:
yield currSection
There is some obvious code duplication in the function - this bit is repeated 
2.67 times ;-):
if currSection:
yield currSection
currSection = []
As a firm believer in Once and Only Once, I would like to factor this out into a separate function, 
either a nested function of makeSections(), or as a separate method of a class implementation. 
Something like this:

def makeSections(f):### DOESN'T WORK ###
currSection = []
def yieldSection():
if currSection:
yield currSection
del currSection[:]
for line in f:
line = line.strip()
if line == 'Name:':
# Start of a new section
yieldSection()
currSection.append(line)
elif not line:
# Blank line ends a section
yieldSection()
else:
# Accumulate into a section
currSection.append(line)
# Yield the last section
yieldSection()
The problem is that yieldSection() now is the generator, and makeSections() is not, and the result 
of calling yieldSection() is a new iterator, not the section...

Is there a way to do this or do I have to live with the duplication?
Thanks,
Kent
Here is a complete program:
data = '''
Name:
City:




Name:
City:


'''
import cStringIO# just for test
def makeSections(f):
''' This is a generator function. It will return successive sections
of f until EOF.
Sections are every line from a 'Name:' line to the first blank line.
Sections are returned as a list of lines with line endings stripped.
'''
currSection = []
for line in f:
line = line.strip()
if line == 'Name:':
# Start of a new section
if currSection:
yield currSection
currSection = []
currSection.append(line)
elif not line:
# Blank line ends a section
if currSection:
yield currSection
currSection = []
else:
# Accumulate into a section
currSection.append(line)
# Yield the last section
if currSection:
yield currSection
f = cStringIO.StringIO(data)
for section in makeSections(f):
print 'Section'
for line in section:
print '   ', line
print
--
http://mail.python.org/mailman/listinfo/python-list


Re: about python advanced/new features documentation

2004-12-04 Thread Kent Johnson
Kl wrote:
Hi, python is really easy to learn in my opinion. There are loads of
tutorials/books on the web talking about the most common python features.
The problem comes when they add something new to the language or you want to
use advanced features. Since python is still evolving its difficult to find
good documentation on these new features/language changes.
Where can i find always up-to-date documentation about python? Unfortunately
the official tutorial and books become obsolete really fast.
The "What's New" documents that are bundled with the last several Python releases are very helpful. 
They link to the PEPs which generally give a lot of detail about a change.

Quite a few recipes were contributed to the online Python Cookbook 
demonstrating Python 2.4 features.
Kent
Thanks.

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


Re: string slicing

2004-12-05 Thread Kent Johnson
Ishwor wrote:
s = 'hello'
m = s[:]
m is s
True
 I discussed the *is* operator with some of the pythoners before as
well but it is somewhat different than what i intended it to do. The
LP2E by Mark & David says -
" m gets a *full top-level copy* of a sequence object- an object with
the same value but distinct piece of memory." but when i test them
with *is* operator then the result is True. Why is this happening??
This behaviour is due to the way strings are handled. In some cases strings are 'interned' which 
lets the interpreter keep only a single copy of a string. If you try it with a list you get a 
different result:

>>> s=list('hello')
>>> s
['h', 'e', 'l', 'l', 'o']
>>> m=s[:]
>>> m
['h', 'e', 'l', 'l', 'o']
>>> m is s
False
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: string slicing

2004-12-05 Thread Kent Johnson
Ishwor wrote:
On Sun, 05 Dec 2004 09:44:13 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote:
This behaviour is due to the way strings are handled. In some cases strings are 
'interned' which
lets the interpreter keep only a single copy of a string. If you try it with a 
list you get a
different result

Thanx Kent.  so for lists Python doesn't keep the same object in the
cache??? 
Right, AFAIK lists are not cached in this way.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Import a module without executing it?

2004-12-07 Thread Kent Johnson
Jay O'Connor wrote:
The real question, I suppose, is "what is a good technique to find what 
modules and classes implement or refer to particular names"
You might like to try ctags. I have had a good experience with it. It's not as automatic as I would 
like - you have to build a cross-reference table before you can use it - and it only finds 
implementors, not referrers. It integrates with lots of editors, I have used it with TextPad.
http://ctags.sourceforge.net/

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


Re: Import a module without executing it?

2004-12-08 Thread Kent Johnson
Andy, this is a nice example. It prompted me to look at the docs for compiler.visitor. The docs are, 
um, pretty bad. I'm going to attempt to clean them up a little. Would you mind if I include this 
example?

Thanks,
Kent
Andy Gross wrote:
Here's a quick example that will pull out all functions defined in the 
top-level of a module:

---
#/usr/bin/env python
from compiler import parse, walk
from compiler.visitor import ASTVisitor
testdata = r'''
def aFunction(anArg):
return anArg + 1
'''
class SimpleVisitor(ASTVisitor):
def visitFunction(self, parsedFunc):
print "Function %(name)s at %(lineno)s takes %(argnames)s " \
  " with code %(code)s" % parsedFunc.__dict__
if __name__ == "__main__":
ast = parse(testdata)
walk(ast, SimpleVisitor(), verbose=True)
---
[EMAIL PROTECTED]:~$ ./test.py
Function aFunction at 2 takes ['anArg']  with code 
Stmt([Return(Add((Name('anArg'), Const(1])

HTH,
/arg
On Dec 7, 2004, at 11:14 PM, Caleb Hattingh wrote:
Andy
thx for that.  I had a file called 'tktest.py' lying around, and I did:
'>>> a = compiler.parseFile('tktest.py')
And "a" looks something like this:
***
Stmt([Import([('Tkinter', None)]), Function(None, 'add_rows', ['w', 
'titles', 'rows'], [], 0, None, 
Stmt([Discard(CallFunc(Getattr(Name('w'), 'configure'), 
[Keyword('state', Const('normal'))], None, None)), For(AssName('r', 
'OP_ASSIGN'), Name('rows'), Stmt([For(AssTuple([AssName('t', 
'OP_ASSIGN'), AssName('v', 'OP_ASSIGN')]), CallFunc(Name('zip'), 
[Name('titles'), Name('r')], None, None), 
Stmt([Discard(CallFunc(Getattr(Name('w'), 'insert'), [Const('end'), 
Mod((Const('%s:\t%s\n'), Tuple([Name('t'), Name('v')])))], None, 
None))]), None), Discard(CallFunc(Getattr(Name('w'), 'insert'), 
[Const('end'), Const('\n')], None, None))]), None), 
Discard(CallFunc(Getattr(Name('w'), 'configure'), [Keyword('state', 
Const('disabled'))], None, None))])), Assign([AssName('app', 
'OP_ASSIGN')], CallFunc(Getattr(Name('Tkinter'), 'Tk'), [], None, 
None)), Assign([AssName('t', 'OP_ASSIGN')], 
CallFunc(Getattr(Name('Tkinter'), 'Text'), [Name('app'), 
Keyword('state', Const('disabled'))], None, None)), 
Discard(CallFunc(Getattr(Name('t'), 'pack'), [], None, None)), 
Assign([AssName('info', 'OP_ASSIGN')], List([List([Const('Ali'), 
Const(18)]), List([Const('Zainab'), Const(16)]), 
List([Const('Khalid'), Const(18)])])), 
Discard(CallFunc(Name('add_rows'), [Name('t'), List([Const('Name'), 
Const('Age')]), Name('info')], None, None)), 
Discard(CallFunc(Getattr(Name('app'), 'mainloop'), [], None, None))])
***

Pretty impressive :)
Do you know of more batteries that can process this stuff further, for 
interest sake (and maybe the OP)?

thx again
Caleb
On Tue, 7 Dec 2004 15:57:16 -0500, Andy Gross <[EMAIL PROTECTED]> wrote:
You'll want to use the "compiler" package.  compiler.parseFile will 
return an AST that you can inspect (which is not really 'reflection', 
btw).

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

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


Re: converting html escape sequences to unicode characters

2004-12-09 Thread Kent Johnson
harrelson wrote:
I have a list of about 2500 html escape sequences (decimal) that I need
to convert to utf-8.  Stuff like:
비
행
기
로
보
낼
거
에
요
내
면
금
이
얼
마
지
잠
Anyone know what the decimal is representing?  It doesn't seem to
equate to a unicode codepoint...
In well-formed HTML (!) these should be the decimal values of Unicode 
characters. See
http://www.w3.org/TR/html4/charset.html#h-5.3.1
These characters appear to be Hangul Syllables:
http://www.unicode.org/charts/PDF/UAC00.pdf
import unicodedata
nums = [
48708,
54665,
44592,
47196,
48372,
45244,
44144,
50640,
50836,
45236,
47732,
44552,
51060,
50620,
47560,
51648,
51104,
]
for num in nums:
print num, unicodedata.name(unichr(num), 'Unknown')
=>
48708 HANGUL SYLLABLE BI
54665 HANGUL SYLLABLE HAENG
44592 HANGUL SYLLABLE GI
47196 HANGUL SYLLABLE RO
48372 HANGUL SYLLABLE BO
45244 HANGUL SYLLABLE NAEL
44144 HANGUL SYLLABLE GEO
50640 HANGUL SYLLABLE E
50836 HANGUL SYLLABLE YO
45236 HANGUL SYLLABLE NAE
47732 HANGUL SYLLABLE MYEON
44552 HANGUL SYLLABLE GEUM
51060 HANGUL SYLLABLE I
50620 HANGUL SYLLABLE EOL
47560 HANGUL SYLLABLE MA
51648 HANGUL SYLLABLE JI
51104 HANGUL SYLLABLE JAM
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: gather information from various files efficiently

2004-12-13 Thread Kent Johnson
Keith Dart wrote:
try:
dict[a].append(b)
except KeyError:
dict[a] = [b]
or my favorite Python shortcut:
dict.setdefault(a, []).append(b)
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

2004-12-16 Thread Kent Johnson
Keith Dart wrote:
What I do is set Python's sys.ps1 variable to something else. I have a 
module called "interactive" that I import implicitly by shell alias:

py='python -i -c '\''import interactive'\'
Which, among other things, sets the prompt to "Python> "
You can do the same thing using a PYTHONSTARTUP file - see 
http://docs.python.org/tut/node4.html#SECTION00424

You can change the prompts with
import sys
sys.ps1 = ' >>> '
sys.ps2 = ' ... '
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: getopt: Make argument mandatory

2004-12-16 Thread Kent Johnson
Frans Englich wrote:
On Wednesday 15 December 2004 14:07, Diez B. Roggisch wrote:
In my use of getopt.getopt, I would like to make a certain parameter
mandatory. I know how to specify such that a parameter must have a value
if it's specified, but I also want to make the parameter itself
mandatory(combined with a mandatory value, the result is that the user
must specify a value).
Use optparse - in the examples section you'll find what you need.

I'm slow here, do you mean in of the callback examples in 6.21.4?
http://docs.python.org/lib/module-optparse.html
No, try here - this section seems to have been deleted from the Python 2.4 
docs!
http://www.python.org/doc/2.3.4/lib/optparse-extending-examples.html
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

2004-12-17 Thread Kent Johnson
Steven Bethard wrote:
Very cool.  I didn't know about this.  Does anyone know how to make it 
work with Pythonwin[1]?  (Obviously, I can type the above in manually 
every time, but I'd much rather have Pythonwin do this automatically for 
me.)

Steve
[1] I'd do my example code at the command prompt, but I can't live 
without copy-paste. ;)
You can copy and paste from a Windows command prompt. It's a bit bizarre, but
- In the system menu for a command window, pick Properties
- On the Options tab, turn on Quick Edit mode
- Now you can copy and paste with right-click (!). If you have text selected, right-click will copy, 
otherwise paste. It's a bit strange but it works.

I think it's wonderfully ironic that in Windows - which takes such pains to make everything keyboard 
accessible - in a *command line* window, which is using keyboard input by its nature - you have to 
use the mouse for copy and paste!!

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


Re: Wrapper objects

2004-12-10 Thread Kent Johnson
Nick Coghlan wrote:
Simon Brunning wrote:
This work - 
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52295>?

Only for old-style classes, though. If you inherit from object or 
another builtin, that recipe fails.
Could you explain, please? I thought __getattr__ worked the same with new- 
and old-style classes?
Thanks,
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I convert characters into integers?

2004-12-14 Thread Kent Johnson
Markus Zeindl wrote:
I have got a string from the user, for example "Hi!".
Now I get every character with a loop:

buffer = ""
for i in range(len(message)):
  ch = message[i-1:i]
for ch in message:
...
is simpler and more idiomatic.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Jython performance

2004-12-23 Thread Kent Johnson
Sean Blakey wrote:
On Wed, 22 Dec 2004 17:03:55 -0200, Gabriel Cosentino de Barros
<[EMAIL PROTECTED]> wrote:
On the "Best GUI for small-scale accounting app?" tread some people
mentioned jython. I went to read about it, but i was wondering if anyone has
any real project done with it and can give real world comments about
performance. 
I've been very happy with it's performance, after the one-time
interpreter startup.
That matches my experience as well. I have written a medium-sized GUI app using Jython and Swing and 
I am very happy with the performance. Most of the heavy lifting is done in Java libraries anyway - 
primarily Swing, dom4j and Velocity in my case.

The initial import of a module is relatively slow and I often defer importing a module until it is 
needed. Other than that I have been pleased with the performance.

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


Re: mathmatical expressions evaluation

2004-12-23 Thread Kent Johnson
Tonino wrote:
thanks all for the info - and yes - speed is not really an issue and no
- it is not an implementation of a complete financial system - but
rather a small subset of a investment portfolio management system
developed by "another company" ...
What I am trying to achieve is to parse a formula(s) and generate a
result ...
Why do you need to parse the formulas at runtime? It sounds like they are known in advance and you 
could just write functions to implement the calculations.

Kent
I will look into the pyparsing suggested and maybe even leave out
numarrays for now - as it seems a bit overkill ...
The formula are a bit complex and maybe even difficult to write out
Thanks all - I will post my progress ;)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda going out of fashion

2004-12-23 Thread Kent Johnson
Robin Becker wrote:
Alex Martelli wrote:
.
By the way, if that's very important to you, you might enjoy Mozart
(http://www.mozart-oz.org/)
.very interesting, but it wants to make me install emacs. :(
Apparently you can also use oz with a compiler and runtime engine...see
http://www.mozart-oz.org/documentation/apptut/index.html
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Jython & IronPython Under Active Development?

2004-12-27 Thread Kent Johnson
Steve Holden wrote:
Just a little further background. The Python Software Foundation 
recently awarded a grant to help to bring Jython into line with the 
current CPython release.
Is information publicly available about this and other PSF grants? I don't see any announcement on 
the PSF web site or in the grants-discuss mail list.

Thanks,
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: The Industry choice

2005-01-04 Thread Kent Johnson
Alex Martelli wrote:
Roy Smith <[EMAIL PROTECTED]> wrote:

Stefan Axelsson <[EMAIL PROTECTED]> wrote:
Yes, ignoring most of the debate about static vs. dynamic typing, I've
also longed for 'use strict'.
You can use __slots__ to get the effect you're after.  Well, sort of; it
only works for instance variables, not locals.  And the gurus will argue
that __slots__ wasn't intended for that, so you shouldn't do it.

There's a simple, excellent recipe by Michele Simionato, on both the
online and forthcoming 2nd edition printed Cookbook, showing how to do
that the right way -- with __setattr__ -- rather than with __slots__ .
The recipe is here (it took me a few minutes to find it, I found the title 
misleading):
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252158
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter: passing parameters to menu commands

2005-01-07 Thread Kent Johnson
Philippe C. Martin wrote:
I have many menu items and would like them all to call the same method
-However, I need the method called to react differently depending on the
menu item selected. Since the menu command functions do not seem to
receive any type of event style object, is there some type of Tkinter
call that would let my method know the menu id selected ?
Much as it seems to be out of favor, IMO this is a place where a lambda expression is very handy. 
You can make a callback for each menu item that binds an extra parameter to the handler:

# Based on an example by Fredrik Lundh
from Tkinter import *
def callback(code):
print "called the callback with code", code
root = Tk()
# create a menu
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=lambda: callback('New'))
filemenu.add_command(label="Open...", command=lambda: callback('Open'))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=lambda: callback('Exit'))
mainloop()
Of course you could do this with named forwarding functions if you prefer.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter: passing parameters to menu commands

2005-01-08 Thread Kent Johnson
Philippe C. Martin wrote:
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=lambda: callback('New'))
filemenu.add_command(label="Open...", command=lambda:

Of course you could do this with named forwarding functions if you
prefer
I'm not sure what 'named forwarding functions' 
Bad choice of terminology, I just mean you can explicitly define
def handleNew:
  callback('New')
etc.
are but I'm actually in a
class and when applying your suggestion in the following manner,
everything works (THANKS!)

def __Dec(self,p_string):
  for i in p_string:
self.__Insert(i)
.
.
.
#menu creation
l_dec.add_command(label = 'ATR', command=lambda: self.__Dec('ATR'))
l_dec.add_command(label = 'IN', command=lambda:self.__Dec('IN'))
.
.
.

Yet I have a question:
If I replace the menu creation code as below, and since __Insert appends
the string p_string into a text widget that is created _after_ the menu
creation; the method __Dec seems to be called at the menu creation and
I get an error in __Insert because the test widget is equal to None.
My reflexes of C programmer tell me that command=self.__Dec just
passes a method pointer (sorry I said it) to add_command - yet it does
not seem to be so.
>
> What is actually going on ?
>
>
> #menu creation
> l_dec.add_command(label = 'ATR', command=self.__Dec('ATR'))
> l_dec.add_command(label = 'IN', command=self.__Dec('IN'))
self.__Dec is a reference to the function. It is similar to a method pointer so you don't need to 
apologize ;) The name of a function without the () is a reference. When you append () it becomes a 
call to the referenced function.

The command parameter for the menu must be a reference to a function. The function is called with no 
arguments when the menu is invoked.

So, you need a function of no arguments to handle the command. For example,
def handleMenu():
  print 'Handled'
filemenu.add_command(label="New", command=handleMenu)
Note there is no () after handleMenu. 'command' is bound to the function object; the function is not 
called until later.

OK, now suppose you want to pass a parameter to handleMenu?
def handleMenu(menuName):
  print 'Handled', menuName
Now what do you put in the command parameter? This won't work because you are *calling* handleMenu 
and assigning the result of the call (in this case the value None) to 'command':

filemenu.add_command(label="New", command=handleMenu('New')) # WRONG
You need a new function of zero arguments to bind to 'command'. Here is one way 
to do it:
def handleNew():
  handleMenu('New')
filemenu.add_command(label="New", command=handleNew) # OK
Note, again, no () after handleNew. 'command' is bound to the function object 
again.
OK, what about lambda? lambda is a way to create a simple anonymous function. One simple use of 
lambda is to make a new function that binds a parameter to another function.

lambda: handleMenu('New')
defines a function that does the same thing as handleNew. The value of the lambda expression is the 
function object. So

filemenu.add_command(label="New", command=lambda: handleMenu('New')) # OK
is another way to get the result you want.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Speed revisited

2005-01-09 Thread Kent Johnson
Andrea Griffini wrote:
I've to admit that I also found strange that deleting the
first element from a list is not O(1) in python. My wild
guess was that the extra addition and normalization required
to have insertion in amortized O(1) and deletion in O(1) at
both ends of a random access sequence was going to have
basically a negligible cost for normal access (given the
overhead that is already present in python).
This was added to Python 2.4 as collections.deque
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Please Contribute Python Documentation!

2005-01-09 Thread Kent Johnson
Aahz wrote:
In article <[EMAIL PROTECTED]>,
Tony Meyer <[EMAIL PROTECTED]> wrote:
I don't think I've seen such a statement before - the stuff I've seen
all indicates that one should be submitting proper LaTeX docs/patches.
If plain-text contributions are welcome, could this be added to the doc
about contributing to the docs?  (I suppose I could submit a patch, but
that seems overkill ).

It *is* in the docs now -- see the top of
http://www.python.org/doc/2.4/doc/doc.html
It's also spelled out pretty clearly in the "About this document" document you get by clicking on 
the link at the bottom of every page.

Kent
(This has been the official policy for some time, but you're right that
it wasn't earlier documented.  So I filed a bug report. ;-)  If you
think this still isn't enough, file another.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python & unicode

2005-01-11 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
I forgot to add the following:

setattr(C, "Ã", u"The letter Ã")
getattr(C, "Ã")
u'The letter \xe8'
print getattr(C, "Ã")
The letter Ã
But try this:
 >>> C.Ã
  File "", line 1
C.âÂ
  ^
SyntaxError: invalid syntax
Python identifiers can be generic strings, including Latin-1
characters;
I don't think so. You have hacked an attribute with latin-1 characters in it, but you haven't 
actually created an identifier.

According to the language reference, identifiers can only contain letters a-z and A-Z, digits 0-9 
and underscore.
http://docs.python.org/ref/identifiers.html

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


Re: [csv module] duplication of end of line character in output file generated

2005-01-11 Thread Kent Johnson
simon.alexandre wrote:
Hi all,
I use csv module included in python 2.3. I use the writer and encouter the
following problem: in my output file (.csv) there is a duplication of the
end of line character, so when I open the csv file in Ms-Excel a blank line
is inserted between each data line.
From the docs for csv.writer():
writer(  	csvfile[, dialect='excel'[, fmtparam]])
...If csvfile is a file object, it must be opened with the 'b' flag on platforms where that 
makes a difference.

Windows is a "platform where that makes a difference." So try
  self.writer = csv.writer(file("Test.csv", "wb"))
Kent
OS: W2k
Someone has an idea ?
thanks in advance
the source code is the following:
-->
import csv
class CsvDumper:
   def __init__(self):
   self.object =
[['la','mb','mc','md'],['ma','mb','mc','md'],['ma','mb','mc','md']]
   self.writer = csv.writer(file("Test.csv", "w"))
   def DumpCsvFile(self):
  for row in self.object:
  self.writer.writerow(row)
c = CsvDumper()
c.DumpCsvFile()

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


Re: Python & unicode

2005-01-11 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
Kent:
I don't think so. You have hacked an attribute with latin-1
characters in it, but you
haven't actually created an identifier.

No, I really created an identifier. For instance
I can create a global name in this way:

globals()["è"]=1
globals()["è"]
1
Maybe I'm splitting hairs but to me an identifier is a syntactical element that can be used in 
specific ways. For example the syntax defines
attributeref ::=
 primary "." identifier
so if identifiers can contain latin-1 characters you should be able to say
C.è=1

Kent

According to the language reference, identifiers can only contain
letters a-z and A-Z,
digits 0-9 and underscore.
http://docs.python.org/ref/identifiers.html

The parser has this restriction, so it gets confused if it finds "è".
But the underlying
implementation just works for generic identifiers.
Michele Simionato
--
http://mail.python.org/mailman/listinfo/python-list


iTools

2005-01-16 Thread kent sin
Where can I download python-itools?

I found it in the python packages index but the site
is not contactable. 

Thank you.

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


Re: xml parsing escape characters

2005-01-20 Thread Kent Johnson
Luis P. Mendes wrote:
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1
this is the xml document:

http://www..";><DataSet>
~   <Order>
~ <Customer>439</Customer>
(... others ...)
~   </Order>
</DataSet>
This is an XML document containing a single tag, , whose content is text containing 
entity-escaped XML.

This is *not* an XML document containing tags , , , 
etc.
All the behaviour you are seeing is a consequence of this. You need to unescape the contents of the 
 tag to be able to treat it as structured XML.

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


Re: xml parsing escape characters

2005-01-20 Thread Kent Johnson
Irmen de Jong wrote:
Kent Johnson wrote:
[...]
This is an XML document containing a single tag, , whose 
content is text containing entity-escaped XML.

This is *not* an XML document containing tags , , 
, etc.

All the behaviour you are seeing is a consequence of this. You need to 
unescape the contents of the  tag to be able to treat it as 
structured XML.

The unescaping is usually done for you by the xml parser that you use.
Yes, so if your XML contains for example
<not a tag>
and you parse this and ask for the *text* content of the  tag, you will 
get the string
""
but it's still *not* a tag. If you try to get child elements of the  
element there will be none.
This is exactly the confusion the OP has.
--Irmen
--
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading ctor doesn't work?

2005-01-20 Thread Kent Johnson
Martin Häcker wrote:
Hi there,
I just tried to run this code and failed miserably - though I dunno 
why. Could any of you please enlighten me why this doesn't work?
Here is a simpler test case. I'm mystified too:
from datetime import datetime
class time (datetime):
  def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
datetime.__init__(self, 2001, 10, 31, hours, minutes, seconds, microseconds)
print time(1,2,3,4) # => 0001-02-03 04:00:00
print time()# => TypeError: function takes at least 3 arguments (0 
given)
What happens to the default arguments to time.__init__? What happens to the 2001, 10, 31 arguments 
to datetime.__init__?

I would expect the output to be
2001-10-31 01:02:03.04
2001-10-31 00:00:00.00
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading ctor doesn't work?

2005-01-20 Thread Kent Johnson
Paul McGuire wrote:
"Kent Johnson" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
Martin Häcker wrote:

Hi there,
I just tried to run this code and failed miserably - though I dunno
why. Could any of you please enlighten me why this doesn't work?
Here is a simpler test case. I'm mystified too:
from datetime import datetime
class time (datetime):
  def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
datetime.__init__(self, 2001, 10, 31, hours, minutes, seconds,
microseconds)
print time(1,2,3,4) # => 0001-02-03 04:00:00
print time()# => TypeError: function takes at least 3 arguments (0
given)
What happens to the default arguments to time.__init__? What happens to
the 2001, 10, 31 arguments
to datetime.__init__?
I would expect the output to be
2001-10-31 01:02:03.000004
2001-10-31 00:00:00.00
Kent

I can't explain this behavior, but this version does work (uses
datetime.combine instead of ctor)
-- Paul
from datetime import datetime, date as dt_date, time as dt_time
class time_d (datetime):
def __new__(cls, *args):
# default to no microseconds
if len(args)==3:
args = args + (0,)
if len(args)==4:
tmpdate = datetime.today()
h, mi, s, ms = args
return datetime.combine(tmpdate, dt_time(h,mi,s,ms))
elif len(args)==7:
y,m,d,h,mi,s,ms = args
return datetime.combine(dt_date(y,m,d), dt_time(h,mi,s,ms))
else:
raise TypeError, "wrong number of args"
print time_d(2001,10,31,1,2,3,4)
print time_d(1,2,3,4)
print time_d(1,2,3)

Ah, right. The light turns on...
datetime is immutable so overriding the constructor doesn't change the constructed object. You have 
to override __new__ instead.
http://www.python.org/2.2.1/descrintro.html#__new__

This works:
from datetime import datetime
class time (datetime):
def __new__(cls, hours=0, minutes=0, seconds=0, microseconds=0):
return datetime.__new__(cls, 2001, 10, 31, hours, minutes, seconds, 
microseconds)
print time(1,2,3,4) # => 2001-10-31 01:02:03.04
print time()# => 2001-10-31 00:00:00
Kent


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


Re: Overloading ctor doesn't work?

2005-01-21 Thread Kent Johnson
Nick Craig-Wood wrote:
Martin Häcker <[EMAIL PROTECTED]> wrote:
Now I thought, just overide the ctor of datetime so that year, month and 
  day are static and everything should work as far as I need it.

That is, it could work - though I seem to be unable to overide the ctor. :(
Its a bug!
  
http://sourceforge.net/tracker/index.php?func=detail&aid=720908&group_id=5470&atid=105470
However its been fixed in a recent Python 2.3.
My example was developed in Python 2.4. The problem was the immutability of 
datetime.
Kent
(I was bitten by the same thing which used to fail but now works after
an upgrade of python 2.3!)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help with saving and restoring program state

2005-01-25 Thread Kent Johnson
Jacob H wrote:
Hello list...
I'm developing an adventure game in Python (which of course is lots of
fun). One of the features is the ability to save games and restore the
saves later. I'm using the pickle module to implement this. Capturing
current program state and neatly replacing it later is proving to be
trickier than I first imagined, so I'm here to ask for a little
direction from wiser minds than mine!
When my program initializes, each game object is stored in two places
-- the defining module, and in a list in another module. The following
example is not from my actual code, but what happens is the same.
(code contained in "globalstate" module)
all_fruit = []
(code contained in "world" module)
class Apple(object): # the class hierarchy goes back to object, anyway
def __init__(self):
self.foo = 23
self.bar = "something"
globalstate.all_fruit.append(self)  
apple = Apple()
I enjoy the convenience of being able to refer to the same apple
instance through world.apple or globalstate.all_fruit, the latter
coming into play when I write for loops and so on. When I update the
instance attributes in one place, the changes are reflected in the
other place. But now comes the save and restore game functions, which
again are simplified from my real code:
My understanding of pickle is that it will correctly handle shared references in the saved data. So 
if you pack all your global dicts into one list and pickle that list, you will get what you want. 
See code changes below:

(code contained in "saveload" module)
import pickle
import world
  import globalstate
def savegame(path_to_name):
world_data = {}
for attr, value in world.__dict__.items():
	# actual code is selective about which attributes 
	# from world it takes -- I'm just keeping this 
	# example simple
	world_data[attr] = value
  the_whole_shebang = [ world_data, globalstate.all_fruit, globalstate.all_items ]
fp = open(path_to_name, "w")
  pickle.dump(the_whole_shebang, fp)
fp.close()

def loadgame(path_to_name):
fp = open(path_to_name, "r")
  the_whole_shebang = pickle.load(fp)
  world_data, globalstate.all_fruit, globalstate.all_items = 
the_whole_shebang
for attr, value in world_data.items():
setattr(world, attr, value)
fp.close()
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: fast list lookup

2005-01-26 Thread Kent Johnson
Klaus Neuner wrote:
Hello,
what is the fastest way to determine whether list l (with
len(l)>3) contains a certain element?
If you can use a set or dict instead of a list this test will be much 
faster.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.search - just skip it

2005-01-26 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
Input is this:
SET1_S_W CHAR(1) NOT NULL,
SET2_S_W CHAR(1) NOT NULL,
SET3_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,
;
.py says:
import re, string, sys
s_ora = re.compile('.*S_W.*')
lines = open("y.sql").readlines()
for i in range(len(lines)):
try:
if s_ora.search(lines[i]): del lines[i]
When you delete for example lines[0], the indices of the following lines change. So the former 
lines[1] is now lines[0] and will not be checked.

The simplest way to do this is with a list comprehension:
lines = [ line for line in lines if not s_ora.search(line) ]
Even better, there is no need to make the intermediate list of all lines, you 
can say
lines = [ line for line in open("y.sql") if not s_ora.search(line) ]
In Python 2.4 you don't have to make a list at all, you can just say
open("z.sql","w").writelines(line for line in open("y.sql") if not 
s_ora.search(line))
;)
Kent
except IndexError:
open("z.sql","w").writelines(lines)
but output is:
SET2_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,
;
It should delete every, not every other!
thx,
RasDJ
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help With Python

2005-01-26 Thread Kent Johnson
Thomas Guettler wrote:
# No comma at the end:
mylist=[]
for i in range(511):
mylist.append("Spam")
or just
mylist = ["Spam"] * 511
Kent
print ", ".join(mylist)
 Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Startying with Python, need some pointers with manipulating strings

2005-01-27 Thread Kent Johnson
Benji99 wrote:
I've managed to load the html source I want into an object 
called htmlsource using:


import urllib
sock = urllib.urlopen("URL Link")
htmlSource = sock.read()
sock.close()

I'm assuming that htmlSource is a string with \n at the end of 
each line.
NOTE: I've become very accustomed with the TStringList class in 
Delphi so forgive me if I'm trying to work in that way with 
Python...

Basically, I want to search through the whole string( 
htmlSource), for a specific keyword, when it's found, I want to 
know which line it's on so that I can retrieve that line and 
then I should be able to parse/extract what I need using Regular 
Expressions (which I'm getting quite confortable with). So how 
can this be accomplished?
The Pythonic way to do this is to iterate through the lines of htmlSource and process them one at a 
time.
htmlSource = htmlSource.split('\n')  # Split on newline, making a list of lines
for line in htmlSource:
  # Do something with line - check to see if it has the text of interest

You might want to look at Beautiful Soup. If you can find the links of interest by the tags around 
them it might do what you want:
http://www.crummy.com/software/BeautifulSoup/

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


Re: bound vs unbound functions

2005-01-29 Thread Kent Johnson
Michael Tobis wrote:
I'm trying to do metaprogramming. I'm sure I've got this all wrong
wrong wrong, but somehow my approach hasn't yet hit a brick wall.
Anyway, I'd like to dynamically add a method to an instance at
instantiation time. Something like
##
In [71]: class quux(object):
: def __init__(self,stuff):
: template = "def foo(self,b): print b + %s" % stuff
: exec(template)
: self.bazz = foo
:
In [72]: q = quux(5)
In [73]: q.bazz(4)
---
TypeError Traceback (most recent call
last)
/Users/tobis/PyNSol/
TypeError: foo() takes exactly 2 arguments (1 given)
In [74]: q.bazz("not much",4)
9

The thread Steve quoted suggests using new.instancemethod():
import new
class quux(object):
def __init__(self,stuff):
template = "def foo(self,b): print b + %s" % stuff
exec(template)
self.bazz = new.instancemethod(foo, self, quux)
There is no need for exec; you can define foo() directly as a nested 
function:
class quux(object):
def __init__(self,stuff):
def foo(self,b):
print b + stuff
self.bazz = new.instancemethod(foo, self, quux)
Of course for this simple example you can just remember stuff as an 
attribute:
class quux(object):
def __init__(self,stuff):
self.stuff = stuff
    def bazz(self, b):
print b + self.stuff
Kent
So the straightforward question is why, even though bazz is a method of
class quux, it doesn't have that extra call parameter 'self'. Is this a
problem? If I actually need a reference to self is it OK to do:
In [76]: q.bazz(q,4)
?
The more vague question is why do people despise 'exec', and how should
I do this sort of thing instead?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Possible additions to the standard library? (WAS: About standardlibrary improvement)

2005-02-04 Thread Kent Johnson
Daniel Bickett wrote:
|def reverse( self ):
|"""
|Return a reversed copy of string.
|"""
|string = [ x for x in self.__str__() ]
|string.reverse()
|return ''.join( string )
def reverse(self):
return self[::-1]
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Kent Johnson
Bo Peng wrote:
Dear list,
I have many dictionaries with the same set of keys and I would like to 
write a function to calculate something based on these values. For 
example, I have

a = {'x':1, 'y':2}
b = {'x':3, 'y':3}
def fun(dict):
  dict['z'] = dict['x'] + dict['y']
fun(a) and fun(b) will set z in each dictionary as the sum of x and y.
My function and dictionaries are a lot more complicated than these so I 
would like to set dict as the default namespace of fun. Is this 
possible? The ideal code would be:

def fun(dict):
  # set dict as local namespace
  # locals() = dict?
  z = x + y
You can part way there using keyword arguments. You just have to use dictionary syntax for changing 
values in the dictionary:

 >>> def f(d, x=None, y=None):
 ...   d['z'] = x + y
 ...
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 >>>
 >>> f(a, **a)
 >>> a
{'y': 2, 'x': 1, 'z': 3}
 >>> f(b, **b)
 >>> b
{'y': 3, 'x': 3, 'z': 6}
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Kent Johnson
Bo Peng wrote:
Yes. I thought of using exec or eval. If there are a dozen statements,
def fun(d):
  exec 'z = x + y' in globals(), d
seems to be more readable than
def fun(d):
  d['z'] = d['x'] + d['y']
But how severe will the performance penalty be?
You can precompile the string using compile(), you only have to do this 
once.
 >>> def makeFunction(funcStr, name):
 ...   code = compile(funcStr, name, 'exec')
 ...   def f(d):
 ... exec code in d
 ... del d['__builtins__'] # clean up extra entry in d
 ...   return f
 ...
 >>> f = makeFunction('z = x + y', 'f')
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 >>> f(a)
 >>> a
{'y': 2, 'x': 1, 'z': 3}
 >>> f(b)
 >>> b
{'y': 3, 'x': 3, 'z': 6}
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Kent Johnson
Bo Peng wrote:
Exec is slow since compiling the string and calls to globals() use a 
lot of time.  The last one is most elegant but __getattr__ and 
__setattr__ are costly. The 'evil hack' solution is good since 
accessing x and y takes no additional time.

Previous comparison was not completely fair since I could pre-compile 
fun2 and I used indirect __setattr__. Here is the new one:
 >>> # solution two: use exec
... def makeFunction(funcStr, name):
...   code = compile(funcStr, name, 'exec')
...   def f(d):
... exec code in d
...   return f
...
 >>> def fun2(d):
...   myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']
You are still including the compile overhead in fun2. If you want to see how fast the compiled code 
is you should take the definition of myfun out of fun2:

myfun = makeFunction('z = x + y', 'myfun')
def fun2(d):
  for i in xrange(0,N):
myfun(d)
  del d['__builtins__']
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-06 Thread Kent Johnson
Bo Peng wrote:
Kent Johnson wrote:
You are still including the compile overhead in fun2. If you want to 
see how fast the compiled code is you should take the definition of 
myfun out of fun2:
I assumed that most of the time will be spent on N times execution of 
myfunc.
Doh! Right.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Subclassing cElementTree.Element

2005-02-07 Thread Kent Johnson
Is it possible to subclass cElementTree.Element? I tried
 >>> import cElementTree as et
 >>> class Elt(et.Element):
 ...   pass
 ...
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: Error when calling the metaclass bases
cannot create 'builtin_function_or_method' instances
I want to create a tree where I can navigate from a node to its parent. The 
standard Element class
doesn't seem to support this so I am trying to make a subclass that does. The 
XML files in question
are large so the speed of cElementTree is very helpful.
Thanks,
Kent
(apologies if this is a duplicate post)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Big development in the GUI realm

2005-02-08 Thread Kent Johnson
Fredrik Lundh wrote:
Robert Kern wrote:

Fair enough. The only time I've seen it in dead-tree print was in Heinlein's _Time Enough For 
Love_, unattributed to anyone else.
Amazon.com "search inside the book" finds no hits for "malice" in this book.
http://www.amazon.com/gp/reader/0441810764/102-7636110-6481700?v=search-inside&keywords=malice

if that's true, it would seem that it predates the Hanlon reference by a
couple of years:
http://www.statusq.org/archives/2001/12/04
on the other hand, Google tells me that "Time Enough For Love" con-
tains a couple of other famous stupidity quotes, including:
"Never underestimate the power of human stupidity"
"Search inside the book" finds this *twice* in "Time Enough For Love".
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Name of type of object

2005-02-10 Thread Kent Johnson
Jive Dadson wrote:
I don't think I've quite got it.
The application I'm writing has some similarities to an interactive
shell.  Like an interactive shell, it executes arbitrary code that it
receives from an input stream.  When it gets an exception, it should
create an informative message, regardless of the type of exception.  The
traceback routine does that, somehow, some way, but I've tried to read
that code and figure out how and I don't get it.
The best I have so far is,
class Exec_thread(BG_thread):
""" Execute a line of code in the global namespace. """
def _process(s):
""" Process one instruction """
try:
exec s.product in globals()
except (Exception), e:
handle_error( typename(e)+ ": " + str(e) )
Have you looked at the traceback module? If you want to print the same kind of trace you get from 
Python, just use traceback.print_exc().

import traceback
try:
  # whatever
except:
  traceback.print_exc()
Kent

But that works only if the exception happens to be derived from
Exception.  How do I handle the
general case?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is this a bug? BOM decoded with UTF8

2005-02-11 Thread Kent Johnson
Diez B. Roggisch wrote:
I know its easy (string.replace()) but why does UTF-16 do
it on its own then? Is that according to Unicode standard or just
Python convention?

BOM is microsoft-proprietary crap. 
Uh, no. BOM is part of the Unicode standard. The intent is to allow consumers of Unicode text files 
to disambiguate UTF-8, big-endian UTF-16 and little-endian UTF-16.
See http://www.unicode.org/faq/utf_bom.html#BOM

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


Re: changing __call__ on demand

2005-02-13 Thread Kent Johnson
Stefan Behnel wrote:
Hi!
This somewhat puzzles me:
Python 2.4 (#1, Feb  3 2005, 16:47:05)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
.>>> class test(object):
...   def __init__(self):
... self.__call__ = self.__call1
...   def __call1(self):
... print 1
...   def __call__(self):
... print 2
...
.>>> t = test()
.>>> t()
2
It works the way you want if test is an old-style class:
 >>> class test:
 ...  def __init__(self):
 ...self.__call__ = self.__call1
 ...  def __call1(self):
 ...print 1
 ...  def __call__(self):
 ...print 2
 ...
 >>> t=test()
 >>> t()
1
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Second posting - Howto connect to MsSQL

2005-02-13 Thread Kent Johnson
John Fabiani wrote:
Hi,
Since this is (sort of) my second request it must not be an easy solution. 
Are there others using Python to connect MsSQL?  At the moment I'd accept
even a windows solution - although, I'm looking for a Linux solution.
On Windows, you can use http://sourceforge.net/projects/adodbapi
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can't subclass datetime.datetime?

2005-02-14 Thread Kent Johnson
Grant Edwards wrote:
Is it true that a datetime object can convert itself into a
string, but not the other way around?  IOW, there's no simple
way to take the output from str(d) and turn it back into d?
According to this thread, a patch has been checked in that adds strptime() to datetime. So there is 
something to look forward to...
http://tinyurl.com/4fbkb

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


Write Unicode str as utf-8

2005-02-14 Thread kent sin
Python support unicode, but some library don't. Write
is one of them. 

When writing a csv file, The rows contains numbers and
unicode str. It is a little pain to first convert all
unicode str to utf-8 before writing the row.

Are there anyway I can patch python such that It will
convert the unicode string to utf-8 before the write?
Where should I start?


Best rgs,

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


Re: Newbie help

2005-02-14 Thread Kent Johnson
Chad Everett wrote:
Nope,  I am trying to learn it on my own.  I am using the book by Michael 
Dawson.
You might be interested in the Python tutor mailing list which is 
specifically intended for beginners.
http://mail.python.org/mailman/listinfo/tutor
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie help

2005-02-14 Thread Kent Johnson
Kent Johnson wrote:
You might be interested in the Python tutor mailing list which is 
specifically intended for beginners.
http://mail.python.org/mailman/listinfo/tutor
Ah, I don't mean to imply that this list is unfriendly to beginners, or that you are not welcome 
here! Just pointing out another resource.

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


Re: more os.walk() issues... probably user error

2005-02-16 Thread Kent Johnson
rbt wrote:
rbt wrote:
This function is intended to remove unwanted files and dirs from 
os.walk(). It will return correctly *IF* I leave the 'for fs in 
fs_objects' statement out (basically leave out the entire purpose of 
the function).

It's odd, when the program goes into that statment... even when only a 
'pass', and nothing else is present, nothing is returned. Why is that? 
I'm testing Python 2.4 on Linux x86 and WinXP. Results are the same on 
either platform.

def build_clean_list(self, path):
file_skip_list = ['search_results.txt']
dir_skip_list = ['dev', 'proc', 'Temporary Internet Files']
fs_objects = os.walk(path, topdown=True)
fs_objects is a generator, not a list. This loop is exhausting fs_objects, so when you return 
fs_objects is at the end of iteration, there is nothing left.

##  for fs in fs_objects:
##
##for f in fs[2]:
##if f in file_skip_list:
##print f
##fs[2].remove(f)
##
##for d in fs[1]:
##if d in dir_skip_list:
##print d
##fs[1].remove(d)
Add this here:
 yield fs
and take out the return. This turns build_clean_list() into a generator function and you will be 
able to iterate the result.

Kent
return fs_objects

Just to clarify, it's wrong of me to say that 'nothing is returned'... 
in either case, this is what is returned:

Here's what was returned and its type:




But, I can't iterate over the returned object when I descend into the 
for statement I mentioned above.

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


Re: more os.walk() issues... probably user error

2005-02-16 Thread Kent Johnson
rbt wrote:
##  for fs in fs_objects:
##
##for f in fs[2]:
##if f in file_skip_list:
##print f
##fs[2].remove(f)
##
##for d in fs[1]:
##if d in dir_skip_list:
##print d
##fs[1].remove(d)
Will the changes I made (file and dir removals from os.walk()) be 
reflected in the generator object? Is it safe to remove objects this way 
and pass the results in a generator on to another function? Sorry for 
all the questions, I just like to fully understand something before I 
start doing it with confidence.
Yes. The docs for os.walk() explicitly state, "When topdown is true, the caller can modify the 
dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into 
the subdirectories whose names remain in dirnames."

So changes to the dir list affect the iteration; changes to the file list directly affect the value 
you return to the caller.

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


Re: Alternative to standard C "for"

2005-02-17 Thread Kent Johnson
James Stroud wrote:
It seems I need constructs like this all of the time
i = 0
while i < len(somelist):
  if oughta_pop_it(somelist[i]):
somelist.pop(i)
  else:
i += 1
There has to be a better way...
somelist[:] = [ item for item in somelist if not oughta_pop_it(item) ]
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: lambda closure question

2005-02-19 Thread Kent Johnson
Ted Lilley wrote:
What I want to do is pre-load functions with arguments by iterating
through a list like so:

class myclass:
...pass
def func(self, arg):
...print arg
mylist = ["my", "sample", "list"]
for item in mylist:
...setattr(myclass, item, lamdba self: func(self, item))
This attaches a list of functions to the class, making them bound
methods when called from a class instance.

obj = myclass()
obj.list()
list
Unfortunately, it doesn't work.  It seems the closure keeps track of
the variable fed to it dynamically - if the variable changes after the
lambda is created, the lambda still references the _variable_ not the
original _value_ and so gets the new value like so:

obj.sample()
list
obj.my()
list
The closure isn't bound until the scope containing it is exited. A simple workaround is to bind item 
as a default argument to the lambda:
for item in mylist:
setattr(myclass, item, lambda self, item=item: func(self, item))

You can also make a helper function that returns the closure, the closure will be bound each time 
the helper returns:

def make_f(item):
def f(self): func(self, item)
return f
mylist = ["my", "sample", "list"]
for item in mylist:
setattr(myclass, item, make_f(item))
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: could that be a mutable object issue ?

2005-02-19 Thread Kent Johnson
Philippe C. Martin wrote:
If I do this:

print 'LEN OF BOOK BEFORE APPEND: ', len(pickle.dumps(self.__m_rw))
self.__m_rw.books.append( [p_col1,p_col2,p_col3] )
print 'LEN OF BOOK AFTER APPEND: ', len(pickle.dumps(self.__m_rw))
I get the same length before and after append.
when I print self.__m_rw.books, I see my 'appends' in there, yet the
pickled object does not change.
How is __m_rw.books defined? If it is a class attribute of the class of __m_rw you will see this 
behavior. e.g.

 >>> class Mrw:
 ...   books = []
 ...
 >>> m=Mrw()
 >>> class Mrw:
 ...   books = []
 ...
 >>> __m_rw = Mrw()
 >>> __m_rw.books.append(1)
 >>> __m_rw.books
[1]
but __m_rw.books will not be pickled with __m_rw because it belongs to the 
class, not the instance.
The fix is to declare books as an instance attribute:
class Mrw:
  def __init__(self):
self.books = []
Kent
Any clue ?
Thanks
Philippe

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


Re: How Do I get Know What Attributes/Functions In A Class?

2005-02-21 Thread Kent Johnson
Hans Nowak wrote:
[EMAIL PROTECTED] wrote:
Hi,
I'm new to python.  Given a class, how can I get know what
attributes/functins in it without dig into the source?

Use the dir function:
 >>> from smtplib import SMTP
 >>> dir(SMTP)
['__doc__', '__init__', '__module__', 'close', 'connect', 'data', 
'debuglevel', 'docmd', 'does_esmtp', 'ehlo', 'ehlo_resp', 'expn', 
'file', 'getreply', 'has_extn', 'helo', 'helo_resp', 'help', 'login', 
'mail', 'noop', 'putcmd', 'quit', 'rcpt', 'rset', 'send', 'sendmail', 
'set_debuglevel', 'starttls', 'verify', 'vrfy']

To get more detailed information than just a list of names, see the 
inspect module.
Or use 'help' (which is build on top of inspect):
 >>> from smtplib import SMTP
 >>> help(SMTP)
Help on class SMTP in module smtplib:
class SMTP
 |  This class manages a connection to an SMTP or ESMTP server.
 |  SMTP Objects:
 |  SMTP objects have the following attributes:
 |  helo_resp
 |  This is the message given by the server in response to the
 |  most recent HELO command.
etc.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: recommended way of generating HTML from Python

2005-02-21 Thread Kent Johnson
Michele Simionato wrote:
The problem is a problem of standardization, indeed. There plenty of
recipes to
do the same job, I just would like to use a blessed one (I am teaching
a Python
course and I do not know what to recommend to my students).
Why not teach your students to use a template system?
FWIW, here is a my version of the recipe (stripped down to the bare
essentials)
.def makeattr(dict_or_list_of_pairs):
.dic = dict(dict_or_list_of_pairs)
.return " ".join("%s=%r" % (k, dic[k]) for k in dic)
.class HTMLTag(object):
.def __getattr__(self, name):
.def tag(value, **attr):
."""value can be a string or a sequence of strings."""
.if hasattr(value, "__iter__"): # is iterable
.value = " ".join(value)
.return "<%s %s>%s\n" % (name, makeattr(attr), value,
name)
.return tag
# example:
.html = HTMLTag()
.tableheader = ["field1", "field2"]
.tablebody = [["a1", "a2"],
. ["b1", "b2"]]
.html_header = [html.tr(html.th(el) for el in tableheader)]
.html_table = [html.tr(html.td(el) for el in row) for row in tablebody]
.print html.table(html_header + html_table)
*Shudder*
I've written web pages this way (using a pretty nice Java HTML generation package) and I don't 
recommend it. In my experience, this approach has several drawbacks:
- as soon as the web page gets at all complex, the conceptual shift from HTML to code and back is 
difficult.
- It is hard to work with a designer. The designer will give you sample web pages which then have to 
be hand-translated to code. Changes to the web page have to be located in the code.
- There is no separation of content and presentation

IMO templating systems are a much better solution. They let you express HTML in HTML directly; you 
communicate with a designer in a language the designer understands; you can separate content and 
presentation.

Kent

  Michele Simionato

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


Re: On eval and its substitution of globals

2005-02-23 Thread Kent Johnson
Paddy wrote:
Hi,
I got tripped up on the way eval works with respect to modules and
so wrote a test.
It seems that a function carries around knowledge of the globals()
present
when it was defined. (The .func_globals attribute)?
When evaluated using eval(...) the embedded globals can be overridden
with
the one passed through the eval(...) call
If however you create a new function that calls the first then eval's
global argument is only substituted in the outer call!
TEST:
=
def f1(): return A < B
...
def z(): return f1()
...
eval(f1.func_code,dict(A=1,B=2))
True
eval(z.func_code,dict(A=1,B=2, f1=f1))

Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 1, in z
  File "", line 1, in f1
NameError: global name 'A' is not defined
ENDTEST
===
Is there a way to do a deep substitution of the globals?
A workaround is to implement z() using eval() again, this forces f1() to use the same globals passed 
to z():
 >>> def z(): return eval(f1.func_code)
 ...
 >>> eval(z.func_code,dict(A=1,B=2, f1=f1))
True

Kent
I should add that f1 is given as-is. I can modify z,
and f1 is just one of many functions given and function z
is some boolean function of the f's
Thanks, Pad.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Style guide for subclassing built-in types?

2005-02-23 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
p.s. the reason I'm not sticking to reversed or even reverse : suppose
the size of the list is huge.
reversed() returns an iterator so list size shouldn't be an issue.
What problem are you actually trying to solve?
Kent

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


Re: unicode(obj, errors='foo') raises TypeError - bug?

2005-02-23 Thread Kent Johnson
Steven Bethard wrote:
Mike Brown wrote:
class C:
...   def __str__(self):
...  return 'asdf\xff'
...
o = C()
unicode(o, errors='replace')
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: coercing to Unicode: need string or buffer, instance found
[snip]
What am I doing wrong? Is this a bug in Python?

No, this is documented behavior[1]:
"""
unicode([object[, encoding [, errors]]])
...
For objects which provide a __unicode__() method, it will call this 
method without arguments to create a Unicode string. For all other 
objects, the 8-bit string version or representation is requested and 
then converted to a Unicode string using the codec for the default 
encoding in 'strict' mode.
"""

Note that the documentation basically says that it will call str() on 
your object, and then convert it in 'strict' mode.  You should either 
define __unicode__ or call str() manually on the object.
Not a bug, I guess, since it is documented, but it seems a bit bizarre that the encoding and errors 
parameters are ignored when object does not have a __unicode__ method.

Kent
STeVe
[1] http://docs.python.org/lib/built-in-funcs.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode(obj, errors='foo') raises TypeError - bug?

2005-02-23 Thread Kent Johnson
Martin v. LÃwis wrote:
Steven Bethard wrote:
Yeah, I agree it's weird.  I suspect if someone supplied a patch for 
this behavior it would be accepted -- I don't think this should break 
backwards compatibility (much).

Notice that the "right" thing to do would be to pass encoding and errors
to __unicode__. If the string object needs to be told what encoding it
is in, why not any other other object as well?
Unfortunately, this apparently was overlooked, and now it is too late
to change it (or else the existing __unicode__ methods would all break
if they suddenly get an encoding argument).
Could this be handled with a try / except in unicode()? Something like this:
 >>> class A:
 ...   def u(self):  # __unicode__ with no args
 ... print 'A.u()'
 ...
 >>> class B:
 ...   def u(self, enc, err):  # __unicode__ with two args
 ... print 'B.u()', enc, err
 ...
 >>> def convert(obj, enc='ascii', err='strict'): # unicode() function 
delegates to u()
 ...   try:
 ... obj.u(enc, err)
 ...   except TypeError:
 ... obj.u()
 ...
 >>> convert(a)
A.u()
 >>> convert(a, 'utf-8', 'replace')
A.u()
 >>> convert(b)
B.u() ascii strict
 >>> convert(b, 'utf-8', 'replace')
B.u() utf-8 replace
As for using encoding and errors on the result of str() conversion
of the object: how can the caller know what encoding the result of
str() is in, reasonably? 
The same way that the caller will know the encoding of a byte string, or of the result of 
str(some_object) - in my experience, usually by careful detective work on the source of the string 
or object followed by attempts to better understand and control the encoding used throughout the 
application.

It seems more correct to assume that the
str() result in in the system default encoding.
To assume that in absence of any guidance, sure, that is consistent. But to ignore the guidance the 
programmer attempts to provide?

One thing that hasn't been pointed out in this thread yet is that the OP could just define 
__unicode__() on his class to do what he wants...

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


Re: Communication between JAVA and python

2005-02-23 Thread Kent Johnson
Jacques Daussy wrote:
Hello
How can I transfert information between a JAVA application and a python 
script application. I can't use jython because, I must use python 
interpreter.I think to socket or semaphore, but can I use it on Windows 
plateform ?
Jython has an interpreter and Windows has sockets.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: On eval and its substitution of globals

2005-02-23 Thread Kent Johnson
Paddy wrote:
I do in fact have the case you mention. I am writing a module that will
manipulate functions of global variables where the functions are
defined in another module.
Would it be possible to have your functions take arguments instead of globals? That would seem to be 
a better design.

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


Re: Like overloading __init__(), but how?

2005-02-23 Thread Kent Johnson
John M. Gabriele wrote:
I know that Python doesn't do method overloading like
C++ and Java do, but how am I supposed to do something
like this:
This was just discussed. See
http://tinyurl.com/6zo3g
Kent
- incorrect 
#!/usr/bin/python
class Point3d:
  pass
class Vector3d:
  """A vector in three-dimensional cartesian space."""
  def __init__( self ):
"""Create a Vector3d with some reasonable default value."""
x, y, z = 0.0, 0.0, 0.0
  def __init__( self, x_from, y_from, z_from,
  x_to,   y_to,   z_to ):
"""Create a Vector3d from x-y-z coords."""
# ...
pass
  def __init__( self, point_from, point_to ):
"""Create a Vector3d from two Point3d objects."""
# ...
pass
  

  def __init__( self, same_as_this_vec ):
"""Create a Vector3d from a copy of another one."""
# ...
pass
p = Point3d()
p2 = Point3d()
# v = Vector3d( p2, p ) -- Nope. Only the last __init__() counts.
 
-- /incorrect ---

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


Re: Source Encoding GBK/GB2312

2005-02-23 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
When I specify an source encoding such as:
# -*- coding: GBK -*-
or
# -*- coding: GB2312 -*-
as the first line of source, I got the following error:
SyntaxError: 'unknown encoding: GBK'
Does this mean Python does not support GBK/GB2312?  What do I do?
GB2312 is supported in Python 2.4.
GB2312 support can be added to Python 2.3 with the CJKCodecs package:
http://cjkpython.i18n.org/
Kent
-
narke
--
http://mail.python.org/mailman/listinfo/python-list


Re: split a directory string into a list

2005-02-25 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
QUESTION:
How do I split a directory string into a list in Python, eg.
'/foo/bar/beer/sex/cigarettes/drugs/alcohol/'
becomes
['foo','bar','beer','sex','cigarettes','drugs','alcohol']
 >>> '/foo/bar/beer/sex/cigarettes/drugs/alcohol/'.strip('/').split('/')
['foo', 'bar', 'beer', 'sex', 'cigarettes', 'drugs', 'alcohol']
Kent
I was looking at the os.path.split command, but it only seems to
separate the filename from the path (or am I just using it wrong?). I
don't want to do it manually if I can help it, as there will have to be
exceptions for the cases where there is (not) a trailing (leading)
slash, or escape sequences involving /. Is there a built in command for
this?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Converting HTML to ASCII

2005-02-25 Thread Kent Johnson
gf gf wrote:
Hi.  I'm looking for a Python lib to convert HTML to
ASCII. 
You might find these threads on comp.lang.python interesting:
http://tinyurl.com/5zmpn
http://tinyurl.com/6mxmb
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: class factory example needed (long)

2005-03-01 Thread Kent Johnson
Gary Ruben wrote:
OK, I've managed to get this to work with Rainer's method, but I 
realised it is not the best way to do it, since the methods are being 
added by the constructor, i.e. they are instance methods. This means 
that every time a foo object is created, a whole lot of code is being 
run. It would be better to do the same thing with class 'static' 
methods, if this is possible, so that the methods are created just once.
Is this possible?
Here is a solution using a metaclass:
import Numeric
class MetaFoo(type):
def __init__(cls, name, bases, d):
super(MetaFoo, cls).__init__(cls, name, bases, d)
for u in ['sqrt', 'cos', 'tan']:
setattr(cls, u, lambda self, uf=getattr(Numeric, u): uf(self.value 
+ 42.0))
class Foo(object):
__metaclass__ = MetaFoo
def __init__(self, value):
self.value = float(value)
f = Foo(7)
print f.sqrt()
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expressions: large amount of or's

2005-03-01 Thread Kent Johnson
André Søreng wrote:
Hi!
Given a string, I want to find all ocurrences of
certain predefined words in that string. Problem is, the list of
words that should be detected can be in the order of thousands.
With the re module, this can be solved something like this:
import re
r = re.compile("word1|word2|word3|...|wordN")
r.findall(some_string)
Unfortunately, when having more than about 10 000 words in
the regexp, I get a regular expression runtime error when
trying to execute the findall function (compile works fine, but slow).
I don't know if using the re module is the right solution here, any
suggestions on alternative solutions or data structures which could
be used to solve the problem?
If you can split some_string into individual words, you could look them up 
in a set of known words:
known_words = set("word1 word2 word3 ... wordN".split())
found_words = [ word for word in some_string.split() if word in known_words ]
Kent
André
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expressions: large amount of or's

2005-03-01 Thread Kent Johnson
André Søreng wrote:
Hi!
Given a string, I want to find all ocurrences of
certain predefined words in that string. Problem is, the list of
words that should be detected can be in the order of thousands.
With the re module, this can be solved something like this:
import re
r = re.compile("word1|word2|word3|...|wordN")
r.findall(some_string)
Unfortunately, when having more than about 10 000 words in
the regexp, I get a regular expression runtime error when
trying to execute the findall function (compile works fine, but slow).
What error do you get? What version of Python are you using? re was changed in Python 2.4 to avoid 
recursion, so if you are getting a stack overflow in Python 2.3 you should try 2.4.

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


Re: Help- Simple recursive function to build a list

2005-03-01 Thread Kent Johnson
actuary77 wrote:
I am trying to write simple recursive function to build a list:
def rec(n,alist=[]):
_nl=alist[:]
print n,_nl
if n == 0:
print n,_nl
return _nl
else:
_nl=_nl+[n]
rec(n-1,_nl)
should be
return rec(n-1,_nl)
 >>> def rec(n,alist=[]):
 ... _nl=alist[:]
 ... print n,_nl
 ... if n == 0:
 ... print n,_nl
 ... return _nl
 ... else:
 ... _nl=_nl+[n]
 ... return rec(n-1,_nl)
 ...
 >>> _nl = rec(4)
4 []
3 [4]
2 [4, 3]
1 [4, 3, 2]
0 [4, 3, 2, 1]
0 [4, 3, 2, 1]
 >>> print _nl
[4, 3, 2, 1]
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help- Recursion v. Iter Speed comparison

2005-03-02 Thread Kent Johnson
Roy Smith wrote:
I believe Edouard Manet said it best, "Damn your recursion, Henri. 
Iteration, however complex, is always more efficient." (extra points if you 
can identify the source of that quote).  It's not clear what language Manet 
was talking about when he said that, but it's pretty much a universal truth.
GIYF
http://www.sprex.com/else/sidman/boite.html
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiline regex help

2005-03-03 Thread Kent Johnson
Yatima wrote:
Hey Folks,
I've got some info in a bunch of files that kind of looks like so:
Gibberish
53
MoreGarbage
12
RelevantInfo1
10/10/04
NothingImportant
ThisDoesNotMatter
44
RelevantInfo2
22
BlahBlah
343
RelevantInfo3
23
Hubris
Crap
34
and so on...
Anyhow, these "fields" repeat several times in a given file (number of
repetitions varies from file to file). The number on the line following the
"RelevantInfo" lines is really what I'm after. Ideally, I would like to have
something like so:
RelevantInfo1 = 10/10/04 # The variable name isn't actually important
RelevantInfo3 = 23   # it's just there to illustrate what info I'm
 # trying to snag.
Here is a way to create a list of [RelevantInfo, value] pairs:
import cStringIO
raw_data = '''Gibberish
53
MoreGarbage
12
RelevantInfo1
10/10/04
NothingImportant
ThisDoesNotMatter
44
RelevantInfo2
22
BlahBlah
343
RelevantInfo3
23
Hubris
Crap
34'''
raw_data = cStringIO.StringIO(raw_data)
data = []
for line in raw_data:
if line.startswith('RelevantInfo'):
key = line.strip()
value = raw_data.next().strip()
data.append([key, value])
print data

Score[RelevantInfo1][RelevantInfo3] = 22 # The value from RelevantInfo2
I'm not sure what you mean by this. Do you want to build a Score dictionary 
as well?
Kent
Collected from all of the files.
So, there would be several of these "scores" per file and there are a bunch
of files. Ultimately, I am interested in printing them out as a csv file but
that should be relatively easy once they are trapped in my array of doom
.
I've got a fairly ugly "solution" (I am using this term *very* loosely)
using awk and his faithfail companion sed, but I would prefer something in
python.
Thanks for your time.
--
http://mail.python.org/mailman/listinfo/python-list


Re: passing lists

2005-03-03 Thread Kent Johnson
Earl Eiland wrote:
This message contains the responses to two previous messages:
In response to Steven B.,the statements
Raw_packet_queue = enqueue(..., Raw_packet_queue, ...)  
print 'Sort 1 Raw_packet_queue is', Raw_packet_queue
produce the output
Sort 1 Raw_packet_queue is [[(,
'\x00\x00\xd1\xf0\x
[EMAIL PROTECTED]
81\x8a\x02\xbf\xb1\x13\x00\x16\xe5/\xd5\xa7vu\x0e\x08\x80\x10\xf8\xe0\x8a[\x00\x
00\x01\x01\x08\n\x15\xb7\x13\xba\x8e\x91\x9a\xfd'), ['E\x00\x004',
'@\x00', '\x0
6',
'\x89N\xa2j\x81\x8a\x02\xbf\xb1\x13\x00\x16\xe5/\xd5\xa7vu\x0e\x08\x80\x10\x
f8\xe0\x8a[\x00\x00\x01\x01\x08\n\x15\xb7\x13\xba\x8e\x91\x9a\xfd']]]
This consists of two lists [[pcap packet header, pcap packet body], [a
list of selected IP header fields]].  Raw_packet_queue[0][0][1] should
return the 'pcap packet body' list element.
No, Raw_packet_queue[0] is [pcap packet header, pcap packet body] so Raw_packet_queue[0][1] is pcap 
packet body. Raw_packet_queue[0][0][1] is (pcap packet header)[1] which doesn't work.

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


Re: binutils "strings" like functionality?

2005-03-03 Thread Kent Johnson
cjl wrote:
Hey all:
I am working on a little script that needs to pull the strings out of a
binary file, and then manipulate them with python.
The command line utility "strings" (part of binutils) has exactly the
functionality I need, but I was thinking about trying to implement this
in pure python.
I did some reading on opening and reading binary files, etc., and was
just wondering if people think this is possible, or worth my time (as a
learning exercise), or if something like this already exists.
I think a bare-bones version is pretty simple:
- read the file
- use re.split() to split on non-printable characters (string.printable could 
be handy here)
- print anything in the resulting list that is at least 4 characters
Since you mention this as a learning exercise I'll refrain from posting code but it's only a handful 
of lines...of course implementing all of 'strings' is a bit more work.

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


Re: greedy match wanted

2005-03-03 Thread Kent Johnson
alexk wrote:
My problem is as follows. I want to match urls, and therefore I have a
group
of long valid domain names in my regex:
 (?:com|org|net|biz|info|ac|cc|gs|ms|
 sh|st|tc|tf|tj|to|vg|ad|ae|af|ag|
 com\.ag|ai|off\.ai|al|an|ao|aq|
 com\.ar|net\.ar|org\.ar|as|at|co\.at| ... ) ...
However, for a url like kuku.com.to it matches the kuku.com part,
while I want it to match the whole kuku.com.to. Notice that both "com"
and "com.to" are present in the group above.
1. How do I give precedence for "com.to" over "com" in the above group
?
Maybe I can somehow sort it by lexicographic order and then by length,
or divide it to a set of sub-groups by length ?
According to the docs for re:
"As the target string is scanned, REs separated by "|" are tried from left to right. When one 
pattern completely matches, that branch is accepted. This means that once A matches, B will not be 
tested further, even if it would produce a longer overall match. In other words, the "|" operator is 
never greedy."

So putting "com.to" before "com" does what you want.
 >>> import re
 >>> re.search(r'com|com\.to', 'kuku.com.to').group()
'com'
 >>> re.search(r'com\.to|com', 'kuku.com.to').group()
'com.to'
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with recursion

2005-03-03 Thread Kent Johnson
vegetax wrote:
I am trying to make convert a directory "tree" in a list of list, but cant
find the algoritm =( ,for example a directory tree like :
#!/usr/bin/python
from os import listdir
from os.path import isdir,join,basename
dirpath  = '/tmp/test/'
res = []
def rec(f):
print f
for ele in listdir(f):
ele = join(f,ele)
if isdir(ele):
# append the directory name  
  res.append(basename(ele))
rec(ele)
else :
res.append(basename(ele))

rec(dirpath)
print res
The problem is that you are always appending individual names to the same list. If you make rec() 
return a list and append that to the current list you get what you want:

#!/usr/bin/python
from os import listdir
from os.path import isdir,join,basename
dirpath  = '/tmp/test/'
def rec(f):
res = []
for ele in listdir(f):
res.append(ele)
ele = join(f,ele)
if isdir(ele):
res.append(rec(ele))
return res
print rec(dirpath)
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiline regex help

2005-03-03 Thread Kent Johnson
Here is another attempt. I'm still not sure I understand what form you want the data in. I made a 
dict -> dict -> list structure so if you lookup e.g. scores['10/11/04']['60'] you get a list of all 
the RelevantInfo2 values for Relevant1='10/11/04' and Relevant2='60'.

The parser is a simple-minded state machine that will misbehave if the input does not have entries 
in the order Relevant1, Relevant2, Relevant3 (with as many intervening lines as you like).

All three values are available when Relevant3 is detected so you could do something else with them 
if you want.

HTH
Kent
import cStringIO
raw_data = '''Gibberish
53
MoreGarbage
12
RelevantInfo1
10/10/04
NothingImportant
ThisDoesNotMatter
44
RelevantInfo2
22
BlahBlah
343
RelevantInfo3
23
Hubris
Crap
34
Gibberish
53
MoreGarbage
12
RelevantInfo1
10/10/04
NothingImportant
ThisDoesNotMatter
44
RelevantInfo2
22
BlahBlah
343
RelevantInfo3
23
Hubris
Crap
34
SecondSetofGarbage
2423
YouGetThePicture
342342
RelevantInfo1
10/10/04
HoHum
343
MoreStuffNotNeeded
232
RelevantInfo2
33
RelevantInfo3
44
sdfsdf
RelevantInfo1
10/11/04
InsertBoringFillerHere
43234
Stuff
MoreStuff
RelevantInfo2
45
ExcitingIsntIt
324234
RelevantInfo3
60
Lalala'''
raw_data = cStringIO.StringIO(raw_data)
scores = {}
info1 = info2 = info3 = None
for line in raw_data:
if line.startswith('RelevantInfo1'):
info1 = raw_data.next().strip()
elif line.startswith('RelevantInfo2'):
info2 = raw_data.next().strip()
elif line.startswith('RelevantInfo3'):
info3 = raw_data.next().strip()
scores.setdefault(info1, {}).setdefault(info3, []).append(info2)
info1 = info2 = info3 = None
print scores
print scores['10/11/04']['60']
print scores['10/10/04']['23']
## prints:
{'10/10/04': {'44': ['33'], '23': ['22', '22']}, '10/11/04': {'60': ['45']}}
['45']
['22', '22']
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiline regex help

2005-03-03 Thread Kent Johnson
Steven Bethard wrote:
Kent Johnson wrote:
for line in raw_data:
if line.startswith('RelevantInfo1'):
info1 = raw_data.next().strip()
elif line.startswith('RelevantInfo2'):
info2 = raw_data.next().strip()
elif line.startswith('RelevantInfo3'):
info3 = raw_data.next().strip()
scores.setdefault(info1, {}).setdefault(info3, []).append(info2)
info1 = info2 = info3 = None

Very pretty. =)  I have to say, I hadn't ever used iterators this way 
before, that is, calling their next method from within a for-loop.  I 
like it. =)
I confess I have a nagging suspicion that someone who actually knows something about CPython 
internals will tell me why it's a bad idea...but it sure is handy!

Thanks for opening my mind. ;)
My pleasure :-)
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: There's GOT to be a better way!

2005-03-03 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
This would be a good case to use OO design, imo.  The following works
fine.  Simply instantiate the object, call the method, and you can
access (and manipulate) the "module's" variable to your heart's
content.
module.py
class object:
Ouch. Use a different name than 'object', which is the base class of all 
new-style classes.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: jython question (interesting behavior)

2005-03-03 Thread Kent Johnson
pythonUser_07 wrote:
Is this the correct place to post a jython question?
I posted the following in the jython group, but I figured I'd post here
too:
_
I am assuming that the PythonInterpreter environment is not a unique
environment from within a jvm.
Here is some pseudo code to show what I am talking about
1) create a jython module  file, lets call it "mytest"
   with a simple static class
   class simpleS:
  myVar = "test01"
2) Now set up two interpreter environments from within
   the same jvm
   PythonInterpreter py = new PythonInterpreter()
   PythonInterpreter py1 = new PythonInterpreter()
3) now in both interpreters, import the module
   py.exec("import mytest")
   py1.exec("import mytest")
   Now for both interpreters run
   "print mytest.simpleS.myVar"
4) Now for the crazy part.
   in the py interpreter run
   "mytest.simpleS.myVar = 'test02'
   in py1 look at the value
   "print mytest.simpleS.myVar"
Very interesting behavior.  So it seems that each python interpreter
instance does not act as its own seperate space.
My understanding is that the two PythonInterpreters wrap a single PySystemState which is where the 
modules are cached. So they share the same modules. If you want different behaviour you should give 
them each their own PySystemState.

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


Re: Setting default option values for Tkinter widgets

2005-03-04 Thread Kent Johnson
Steve Holden wrote:
[EMAIL PROTECTED] wrote:
Eric, your tagline doesn't parse correctly on my Python 2.4 shell.
So, are you using Windows?
If you interchange single and double quotes it works on Windows too
python -c "print ''.join([chr(154 - ord(c)) for c in 
'U(17zX(%,5.z^5(17l8(%,5.Z*(93-965$l7+-'])"
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Integer From A Float List?!?

2005-03-05 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
Hello NG,
sorry to bother you again with this question... I never used the "timeit"
function, and I would like to ask you if the call I am doing is correct:
C:\Python23\Lib>python timeit.py -n 1000 -s "from Numeric import ones" -s
 "floa
ts=ones((1000,1),'f')" -s "ints = floats.astype(int)"
1000 loops, best of 3: 0.0536 usec per loop
The -s option indicates Setup steps that are done outside the timing loop. So you have timed 1000 
empty loops, which is indeed impressively fast :-)

The correct command is
C:\Python23\Lib>python23 timeit.py -n 1000 -s "from Numeric import ones" -s 
"floats=ones((1000,1),'f')"  "ints = floats.astype(int)"
1000 loops, best of 3: 30.7 usec per loop

which is still impressively fast compared to map:
C:\Python23\Lib>python timeit.py -s "floats = map(float, range(1000))" "ints= 
map(int, floats)"
1000 loops, best of 3: 572 usec per loop
Kent
I used Numeric module to create a 1000 floats matrix of ones, and it seems
to me that is a lot faster than other solutions... but probably I am doing
something wrong in my call to the timeit function...
Thank you a lot.
Andrea.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Relative imports

2005-03-05 Thread Kent Johnson
Michael Hoffman wrote:
Chris wrote:
Why do relative imports cause warnings in PyLint?

http://www.python.org/peps/pep-0328.html#rationale-for-absolute-imports
I notice that this section says that
  from __future__ import absolute_import
will be a feature of Python 2.4. Apparently it didn't make the cut. I've posted 
a bug report.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing Python parse trees

2005-03-05 Thread Kent Johnson
Manlio Perillo wrote:
Anyway, here is an example of what I would like to do:
#begin
def foo(**kwargs): print kwargs
foo(a = 1, b = 2, c = 3)
#end
In the current implementation kwargs is a dict, but I need to have the
keyword argument sorted.
Unfortunately subclassing fron dict and installing the class in the
__builtin__ module (with the name 'dict') does not work, CPython uses
only builtin types.
With the compiler module I can obtain the keyword arguments in the
order the were specified.
The problem is how to do this for every call to foo!
Why not just pass the kind of argument you want? What is it you really need 
to do?
def foo(kwds): print kwds
foo(MyDict(a = 1, b = 2, c = 3))
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Relative imports

2005-03-05 Thread Kent Johnson
Chris wrote:
After reading that link I tried to change my imports like this:
" from .myPythonFileInTheSameFolder import MyClass"
This style of import is not yet implemented.
I'm getting more and more confused...
How can I correctly do a relative import ?
I think your choices are
- keep doing what you have been doing and ignore the warnings from PyLint
- keep doing what you have been doing and turn off the warnings from PyLint
- rewrite your imports to be absolute imports
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Equality operator

2005-03-05 Thread Kent Johnson
italy wrote:
Why doesn't this statement execute in Python:
1 == not 0
I get a syntax error, but I don't know why. 
Because == has higher precedence than 'not', so you are asking for
(1 == not) 0
Try
>>> 1 == (not 0)
True
Kent
Thanks,
Adam Roan
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can a method in one class change an object in another class?

2005-03-06 Thread Kent Johnson
Stewart Midwinter wrote:
I've got an app that creates an object in its main class (it also
creates a GUI).  My problem is that I need to pass this object, a
list, to a dialog that is implemented as a second class. I want to
edit the contents of that list and then pass back the results to the
first class.   So my question is, can a method in one class change an
object in another class?
Diez and Lee have shown you two ways to do this.
If the answer is no, I suppose I could pass in the list as an argument
when I create the second class, then return the contents of the list
when I end the methods in that second class.
This is almost what your example does, but you have made a small error. See 
below.
alternatively, I could make the list a global variable, then it would
be available to all classes.  I have a nagging feeling though that
global variables are to be avoided on general principle. Is this
correct?
Yes, it is correct.
Here's a simple example app that tries to have one class change the
object in another class.  It doesn't give the behaviour I want,
though.
---
#objtest.py
class first:
def __init__(self):
a = 'a'
self.a = a
print self.a
def update(self):
print 'initially, a is', self.a
self.a = second(self.a)
The line above is creating an instance of second and assigning it to self.a. What you want to do is 
create an instance of second, *call* it, and assign the result to self.a. So you should have
  self.a = second(self.a)(self.a)

The self.a parameter passed to second is never used. If you change 
second.__init__ to
  def __init__(self):
  pass
then the call in update() will be
  self.a = second()(self.a)
Kent
print 'afterwards, a is', self.a
class second:
def __init__(self, a):
pass
def __call__(self, a):
a = 'aa'
return a
if __name__ == '__main__':
app = first()
app.update()
thanks,
--
Stewart Midwinter
[EMAIL PROTECTED]
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: function with a state

2005-03-06 Thread Kent Johnson
Patrick Useldinger wrote:
The short answer is to use the global statement:
globe=0
def myFun():
  global globe
  globe=globe+1
  return globe
more elegant is:
globe=0
globe=myfun(globe)
def myFun(var):
  return var+1
This mystifies me. What is myfun()? What is var intended to be?
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: function with a state

2005-03-06 Thread Kent Johnson
Patrick Useldinger wrote:
Kent Johnson wrote:
globe=0
globe=myfun(globe)
def myFun(var):
  return var+1

This mystifies me. What is myfun()? What is var intended to be?

myfun is an error ;-) should be myFun, of course.
var is parameter of function myFun. If you call myFun with variable 
globe, all references to var will be replaced by globe inside function 
myFun.
Oh. I thought there was some deep magic here that I was missing :-)
You also have to define myFun before you call it...
def myFun(var):
  return var+1
globe = 0
...
globe = myFun(globe)
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: reversed heapification?

2005-03-07 Thread Kent Johnson
Stefan Behnel wrote:
Hi!
I need a general-purpose best-k sorting algorithm and I'd like to use heapq
for that (heapify + heappop*k). The problem is that heapify and heappop 
do not
support the "reverse" keyword as known from sorted() and list.sort(). While
the decorate-sort-undecorate pattern allows me to replace the "key" 
option, I
do not see an obvious way to have heapq work in a reverse way without 
making
assumptions on the data.
heapq.nlargest()
heapq.nsmallest()
?
Python 2.4 only
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: reversed heapification?

2005-03-07 Thread Kent Johnson
Stefan Behnel wrote:
Kent Johnson wrote:
heapq.nlargest()
heapq.nsmallest()

On second thought, that doesn't actually get me very far. I do not know 
in advance how many I must select since I need to remove duplicates 
*after* sorting (they are not necessarily 'duplicate' enough to fall 
into the same sort bucket). What I'd like to do is heapify and then 
create an iterator for the result. But since heapify doesn't support 
"reverse" ...

Any other ideas?
Wrap your data in a class that defines __cmp__ as the inverse of __cmp__ on the underlying data, 
then use heapq?
Just sort the list?

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


Re: shuffle the lines of a large file

2005-03-07 Thread Kent Johnson
Joerg Schuster wrote:
Hello,
I am looking for a method to "shuffle" the lines of a large file.
I have a corpus of sorted and "uniqed" English sentences that has been
produced with (1):
(1) sort corpus | uniq > corpus.uniq
corpus.uniq is 80G large. The fact that every sentence appears only
once in corpus.uniq plays an important role for the processes
I use to involve my corpus in.  Yet, the alphabetical order is an
unwanted side effect of (1): Very often, I do not want (or rather, I
do not have the computational capacities) to apply a program to all of
corpus.uniq. Yet, any series of lines of corpus.uniq is obviously a
very lopsided set of English sentences.
So, it would be very useful to do one of the following things:
- produce corpus.uniq in a such a way that it is not sorted in any way
- shuffle corpus.uniq > corpus.uniq.shuffled
Unfortunately, none of the machines that I may use has 80G RAM.
So, using a dictionary will not help.
There was a thread a while ago about choosing random lines from a file without reading the whole 
file into memory. Would that help? Instead of shuffling the file, shuffle the users. I can't find 
the thread though...

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


Re: parameter name conflict. How to solve?

2005-03-08 Thread Kent Johnson
Bo Peng wrote:
def func(output=''):
  output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell 
func that the first output is actually a function? (like in C++, 
::output(output) )
You could use a default argument:
def func(output='', output_fn=output):
  output_fn(output=output)
Kent
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   >