validate wx.TextCtrl unicode

Hello,

I am dealing with wx.TextCtrl and when grabbing the value I need to do
some validation on it. The validation could be anything, it is defined
by the user. Example, the validation might be a function that verifies
the value is an integer greater than 0 or a function that verifies if
the value is a string containing negative integers.

I was wondering how wxpython users generally use unicode strings to do
validation. Would regular expression be the best? I read this post :
http://groups.google.ca/group/wxpython-users/browse_thread/thread/85a06fd9d4145f70/a76e920c4bcba5cc?lnk=gst&q=validate+TextCtrl+unicode#a76e920c4bcba5cc

but they suggest to use IntCtlr, which isn't a solution to me, because
the control I must use is a TextCtrl.

Any ideas or suggestions on how to deal with this ?

Cheers,

david.lygagnon wrote:

I am dealing with wx.TextCtrl and when grabbing the value I
need to do some validation on it. The validation could be
anything, it is defined by the user. Example, the validation
might be a function that verifies the value is an integer
greater than 0 or a function that verifies if the value is a
string containing negative integers.

I don't really understand the question, and I don't know much about unicode,
so take my comments with a pinch of salt, but here goes.

You presumably know that wxPython strings are unicode strings (unless you
have a non-unicode build - unlikely).

There is no problem in converting a unicode string to an integer -

int('1')

1

int(u'1')

1

There are various string methods that may be of use -

'a'.isdigit()

False

'1'.isdigit()

True

u'a'.isdigit()

False

u'1'.isdigit()

True

There is also isnumeric(), which only operates on unicode strings. From the
docs, numeric characters include digit characters, and all characters that
have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE
FIFTH.

You probably know that in python 2.x, 'a' creates a byte string, and u'a'
creates a unicode string.

You probably also know that this has changed in python 3.x - 'a' creates a
unicode string. If you particularly want a byte string, you can use b'a'.

You may or may not know that python 2.6 allows the following 'future'
statement -
  from __future__ import unicode_literals

This causes python 2.6 to behave the same as python 3.x.

I don't know if this helps at all. If no-one else chimes in, perhaps you can
give an example of what the actual problem is.

Frank Millman

Hi David,

david.lygagnon wrote:

Hello,

I am dealing with wx.TextCtrl and when grabbing the value I need to do
some validation on it. The validation could be anything, it is defined
by the user. Example, the validation might be a function that verifies
the value is an integer greater than 0 or a function that verifies if
the value is a string containing negative integers.

I was wondering how wxpython users generally use unicode strings to do
validation. Would regular expression be the best? I read this post :
http://groups.google.ca/group/wxpython-users/browse_thread/thread/85a06fd9d4145f70/a76e920c4bcba5cc?lnk=gst&q=validate+TextCtrl+unicode#a76e920c4bcba5cc

but they suggest to use IntCtlr, which isn't a solution to me, because
the control I must use is a TextCtrl.

Any ideas or suggestions on how to deal with this ?

Have a look at the wx.lib.masked controls.

If the controls should only contain integers then use masked.numctrl or masked.intctrl otherwise use masked.textctrl, check out the demo for it to see all the predefined validations.

Werner

And you should also look at Validators in general (i.e.
wx.PyValidator). They're also in the demo. Basically, you do something
like this to apply one:

wx.TextCtrl(self, -1, "", validator = MyValidator)

Werner's suggestion is definitely good too. I've used both of these
methods successfully.

···

On Nov 29, 2:55 am, werner <wbru...@free.fr> wrote:

Hi David,

david.lygagnon wrote:
> Hello,

> I am dealing with wx.TextCtrl and when grabbing the value I need to do
> some validation on it. The validation could be anything, it is defined
> by the user. Example, the validation might be a function that verifies
> the value is an integer greater than 0 or a function that verifies if
> the value is a string containing negative integers.

> I was wondering how wxpython users generally use unicode strings to do
> validation. Would regular expression be the best? I read this post :
>http://groups.google.ca/group/wxpython-users/browse_thread/thread/85a

> but they suggest to use IntCtlr, which isn't a solution to me, because
> the control I must use is a TextCtrl.

> Any ideas or suggestions on how to deal with this ?

Have a look at the wx.lib.masked controls.

If the controls should only contain integers then use masked.numctrl or
masked.intctrl otherwise use masked.textctrl, check out the demo for it
to see all the predefined validations.

Werner

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org