[wxPython-users] Mystery: wx.grid, a filter function, and Unicode

Marc Tompkins marc.tompkins at gmail.com
Wed Jan 2 22:52:27 PST 2008


I may be naive here, but isn't it possible that the error means exactly what
it says: translate() takes one argument, and you're calling it with two :
s.translate(allchars, delchars)

I agree that the exception's being called a "TypeError" is a bit misleading
- I would have called it a ParameterError or ArgumentError, maybe.

On Jan 2, 2008 10:11 PM, Bob Klahn <bobklahn at comcast.net> wrote:

>  (This is the same note I sent a few minutes ago, but with a P.S.
> identifying my wxPython/Python/OS versions.)
>
>
> Here's a character-filter-generating function (a minor variation of  a
> recipe in "Python Cookbook" section 1.10) that I'm using in one of my
> applications:
>
>         def makefilter( chars, delete=3DTrue ):
>              """
>              Given a string of plain characters to (a) keep or (b) delete,
>              build a filtering function that, applied to any string s,
>              returns a copy of s containing
>                (a) only the characters to be kept, or
>                (b) all but the characters to be deleted.
>              """
>              import string
>              allchars =3D string.maketrans('','')
>              if delete: delchars =3D chars
>              else:      delchars =3D allchars.translate(allchars, chars)
>              def thefilter(s):
>                  return s.translate(allchars, delchars)
>              return thefilter
>
> I'm using the above function inside a wx.grid cell renderer:
>
>         class CellRenderer(gridlib.PyGridCellRenderer):
>              def __init__(self):
>                  gridlib.PyGridCellRenderer.__init__(self)
>                  self.filter =3D utils.makefilter('{}\x08\x0C',delete=3DT=
rue)
>
> The problem:  When the cell renderer executes and the filter is invoked (
> e.g., as self.filter(word) ), it fails on the
>         return s.translate(allchars, delchars)
> line in makefilter:
>     TypeError: translate() takes exactly one argument (2 given)
>
> I.e, Python thinks that string s is a Unicode string.  But I'm not using
> Unicode strings anywhere!  Any ideas where wxPython might be changing my
> plain strings into Unicode strings?  I'm not able to provide a simple code
> example.
>
> Filters generated by makefilter work perfectly outside of my wxPython
> application.
>
> Bob
>
> P.S. I'm using wxPython 2.8.4.0 with Python 2.5.1 under Windows XP SP2.
>
>
>


-- =

www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200801=
02/3e06c100/attachment.htm


More information about the wxpython-users mailing list