Re: a question for anyone using formencode and pylons/pyramid ...

Chris Lambacher <[email protected]> Wed, 22 Feb 2012 21:07:03 -0500
Newsgroups gmane.comp.web.pylons.general,gmane.comp.python.formencode
Message-ID <CAAXXHgK=NGaYFadCiMusjT2p1Jry7CiM7iMHeaah_1M1uWdfdQ@mail.gmail.com>
--90e6ba3090f2aa61fe04b99819b2
Content-Type: text/plain; charset=ISO-8859-1

Hi Vanasco,

This is a generic FormEncode question so you should ask on the FormEncode
list. I copied that list on this email. The FormEncode list is pretty low
volume, but the maintainers do respond ;).

You might want to include what you want as your final state (for instance I
assume you want ints rather than unicode values).

You could then use a Pipe validator:

from formencode import Pipe
from formencode.validators import OneOf, Int

account_type_ids= (1,2,3,4,5)

#must be one of the items in account_type_ids
validator = Pipe(Int(),OneOf(account_ type_ids)

#must be int between 1 and 5
validator = Int(min=1, max=5)

Note that if account_type_ids is not *actually* a list or tuple there may
be other options including DictConverter.

Some useful doc links:
http://www.formencode.org/en/latest/modules/compound.html#formencode.compound.Pipe

http://www.formencode.org/en/latest/modules/validators.html#formencode.validators.Int
http://www.formencode.org/en/latest/modules/validators.html#formencode.validators.DictConverter


-Chris

On Wednesday, February 22, 2012, Jonathan Vanasco wrote:

>
> the request get/post data comes in from pyramid as unicode strings.
>
> the values i have in the database / my code for constant on many items
> are integers
>
> let's use this as an example...
>
>   account_type_ids= (1,2,3,4,5)
>   other_type_ids= (1,2,3,4,5)
>   third_type_ids= (1,2,3,4,5)
>
>
> is there a better way of doing this:
>
>   def unicode_wrap(listed):
>       return [ u"%s" % i for i in listed ]
>
>   class MySchema(formencode.Schema):
>       account_type_id =
> formencode.validators.OneOf( unicode_wrap(account_type_ids),
> not_empty=True )
>       other_type_id =
> formencode.validators.OneOf( unicode_wrap(other_type_ids),
> not_empty=True )
>       third_type_id =
> formencode.validators.OneOf( unicode_wrap(third_type_ids),
> not_empty=True )
>
> i've combed the formencode docs and list archives, but just I can't
> figure out a good way to handle this.
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to pylons-discuss-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
> To unsubscribe from this group, send email to
> pylons-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-discuss-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To unsubscribe from this group, send email to pylons-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.


--90e6ba3090f2aa61fe04b99819b2
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi Vanasco,<div><br></div><div>This is a generic FormEncode question so you=
 should ask on the FormEncode list. I copied that list on this email. The F=
ormEncode list is pretty low volume, but the maintainers do respond ;).</di=
v>
<div><br></div><div>You might want to include what you want as your final s=
tate (for instance I assume you want ints rather than unicode values).</div=
>
<div><br></div><div>You could then use a Pipe validator:</div><div><br></di=
v><div>from formencode import Pipe</div><div>from formencode.validators imp=
ort OneOf, Int</div><div><div><br></div><div>account_type_ids=3D (1,2,3,4,5=
)</div>
</div><div><br></div><div>#must be one of the items in account_type_ids</di=
v><div>validator =3D Pipe(Int(),OneOf(account_
type_ids)</div><div><br></div><div>#must be int between 1 and 5</div><div>v=
alidator =3D Int(min=3D1, max=3D5)</div><div><br></div><div>Note that if ac=
count_type_ids is not *actually* a list or tuple there may be other options=
 including DictConverter.</div>
<div><br></div><div>Some useful doc links:</div><div><a href=3D"http://www.=
formencode.org/en/latest/modules/compound.html#formencode.compound.Pipe">ht=
tp://www.formencode.org/en/latest/modules/compound.html#formencode.compound=
.Pipe</a>=A0</div>
<div><a href=3D"http://www.formencode.org/en/latest/modules/validators.html=
#formencode.validators.Int">http://www.formencode.org/en/latest/modules/val=
idators.html#formencode.validators.Int</a>
</div><div><a href=3D"http://www.formencode.org/en/latest/modules/validator=
s.html#formencode.validators.DictConverter">http://www.formencode.org/en/la=
test/modules/validators.html#formencode.validators.DictConverter</a>=A0</di=
v>
<div><br></div><div>-Chris<br>
<br>On Wednesday, February 22, 2012, Jonathan Vanasco  wrote:<br><blockquot=
e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol=
id;padding-left:1ex"><br>
the request get/post data comes in from pyramid as unicode strings.<br>
<br>
the values i have in the database / my code for constant on many items<br>
are integers<br>
<br>
let&#39;s use this as an example...<br>
<br>
 =A0 account_type_ids=3D (1,2,3,4,5)<br>
 =A0 other_type_ids=3D (1,2,3,4,5)<br>
 =A0 third_type_ids=3D (1,2,3,4,5)<br>
<br>
<br>
is there a better way of doing this:<br>
<br>
 =A0 def unicode_wrap(listed):<br>
 =A0 =A0 =A0 return [ u&quot;%s&quot; % i for i in listed ]<br>
<br>
 =A0 class MySchema(formencode.Schema):<br>
 =A0 =A0 =A0 account_type_id =3D<br>
formencode.validators.OneOf( unicode_wrap(account_type_ids),<br>
not_empty=3DTrue )<br>
 =A0 =A0 =A0 other_type_id =3D<br>
formencode.validators.OneOf( unicode_wrap(other_type_ids),<br>
not_empty=3DTrue )<br>
 =A0 =A0 =A0 third_type_id =3D<br>
formencode.validators.OneOf( unicode_wrap(third_type_ids),<br>
not_empty=3DTrue )<br>
<br>
i&#39;ve combed the formencode docs and list archives, but just I can&#39;t=
<br>
figure out a good way to handle this.<br>
<br>
--<br>
You received this message because you are subscribed to the Google Groups &=
quot;pylons-discuss&quot; group.<br>
To post to this group, send email to <a>pylons-discuss-/[email protected]</a>=
.<br>
To unsubscribe from this group, send email to <a>pylons-discuss+unsubscribe=
@googlegroups.com</a>.<br>
For more options, visit this group at <a href=3D"http://groups.google.com/g=
roup/pylons-discuss?hl=3Den" target=3D"_blank">http://groups.google.com/gro=
up/pylons-discuss?hl=3Den</a>.<br>
<br>
</blockquote></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups "=
pylons-discuss" group.<br />
To post to this group, send email to pylons-discuss-/JYPxA39Uh5TLH3MbocFF+G/[email protected]<br />
To unsubscribe from this group, send email to pylons-discuss+unsubscribe@go=
oglegroups.com.<br />

For more options, visit this group at http://groups.google.com/group/pylons=
-discuss?hl=3Den.<br />



--90e6ba3090f2aa61fe04b99819b2--