Re: validating set of integers
Chris Lambacher <[email protected]> Thu, 19 Jan 2012 09:43:10 -0500
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <CAAXXHg+1Z_gbXKS6t=gHiODeJruxTBaAXpMz1roiaeE8qPPSMg@mail.gmail.com> |
--===============1127425543741928595==
Content-Type: multipart/alternative; boundary=e0cb4efe2cde4820d004b6e295bb
--e0cb4efe2cde4820d004b6e295bb
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Hi =C5=81ukasz,
I'll assume that 'validators.OneOf[1,2,3,4]' was a typo and that you meant
validators.OneOf([1,2,3,4]).
You need to combine two validators using either All or Pipe (see
http://www.formencode.org/en/latest/modules/compound.html).
Pipe will be most intuitive:
>>> from formencode import validators, Pipe
>>> pv =3D Pipe(validators.Int(), validators.OneOf([1,2,3,4]))
>>> pv.to_python('1')
1
All is the same but you need to put the validators in reverse order that
you wish them to evaluate for to_python:
>>> from formencode import validators, All
>>> av =3D All(validators.OneOf([1,2,3,4]), validators.Int())
>>> av.to_python('1')
1
>>> from formencode import Invalid
>>> try:
... av.to_python('b')
... except Invalid, e:
... print e
...
Please enter an integer value
-Chris
On Thu, Jan 19, 2012 at 5:44 AM, Lukasz Michalski <[email protected]> wrote:
> Hi,
>
> Does anyone have an example how to validate set of integers from input
> string?
>
> validators.OneOf[1,2,3,4,5] throws an exception when '1' is passed.
> validators.Int() AFAIK does not allow to specify set of valid values.
>
> Is there any method to combine those two?
>
> Thanks,
> =C5=81ukasz
>
>
> -------------------------------------------------------------------------=
-----
> Keep Your Developer Skills Current with LearnDevNow!
> The most comprehensive online learning library for Microsoft developers
> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
> Metro Style Apps, more. Free future releases when you subscribe now!
> http://p.sf.net/sfu/learndevnow-d2d
> _______________________________________________
> FormEncode-discuss mailing list
> FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/formencode-discuss
>
--=20
Christopher Lambacher
[email protected]
--e0cb4efe2cde4820d004b6e295bb
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Hi=C2=A0
=C5=81ukasz,<div><br></div><div>I'll assume that 'validators.OneOf[=
1,2,3,4]' was a typo and that you meant validators.OneOf([1,2,3,4]).</d=
iv><div><br></div><div>You need to combine two validators using either All =
or Pipe (see=C2=A0<a href=3D"http://www.formencode.org/en/latest/modules/co=
mpound.html">http://www.formencode.org/en/latest/modules/compound.html</a>)=
.=C2=A0</div>
<div><br></div><div>Pipe will be most=C2=A0intuitive:</div><div><br></div><=
div><div>>>> from formencode import validators, Pipe</div><div>>=
;>> pv =3D Pipe(validators.Int(), validators.OneOf([1,2,3,4]))</div><=
div>
>>> pv.to_python('1')</div><div>1</div><div><br></div></di=
v><div>All is the same but you need to put the validators in reverse order =
that you wish them to evaluate for to_python:</div><div><div><br></div>
<div>>>> from formencode import validators, All</div><div>>>=
> av =3D All(validators.OneOf([1,2,3,4]),=C2=A0validators.Int())</div><d=
iv>>>> av.to_python('1')</div><div>1</div></div><div><div>=
>>> from formencode import Invalid</div>
<div>>>> try:</div><div>... =C2=A0 =C2=A0 av.to_python('b'=
)</div><div>... except Invalid, e:</div><div>... =C2=A0 =C2=A0print e</div>=
<div>...</div><div>Please enter an integer value</div><div><br></div></div>=
<div><br></div><div>
-Chris</div><div><br><div class=3D"gmail_quote">On Thu, Jan 19, 2012 at 5:4=
4 AM, Lukasz Michalski <span dir=3D"ltr"><<a href=3D"mailto:[email protected]">=
[email protected]</a>></span> wrote:<br><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
Does anyone have an example how to validate set of integers from input<br>
string?<br>
<br>
validators.OneOf[1,2,3,4,5] throws an exception when '1' is passed.=
<br>
validators.Int() AFAIK does not allow to specify set of valid values.<br>
<br>
Is there any method to combine those two?<br>
<br>
Thanks,<br>
=C5=81ukasz<br>
<br>
---------------------------------------------------------------------------=
---<br>
Keep Your Developer Skills Current with LearnDevNow!<br>
The most comprehensive online learning library for Microsoft developers<br>
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,<br=
>
Metro Style Apps, more. Free future releases when you subscribe now!<br>
<a href=3D"http://p.sf.net/sfu/learndevnow-d2d" target=3D"_blank">http://p.=
sf.net/sfu/learndevnow-d2d</a><br>
_______________________________________________<br>
FormEncode-discuss mailing list<br>
<a href=3D"mailto:FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org">FormEncode-disc=
[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/formencode-discuss"=
target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/formencode-=
discuss</a><br>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>Christopher =
Lambacher<br><a href=3D"mailto:[email protected]">[email protected]=
et</a><br>
</div>
--e0cb4efe2cde4820d004b6e295bb--
--===============1127425543741928595==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
--===============1127425543741928595==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--===============1127425543741928595==--