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

50 views
Skip to first unread message

Jonathan Vanasco

unread,
Feb 22, 2012, 7:01:32 PM2/22/12
to pylons-discuss

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.

cd34

unread,
Feb 22, 2012, 8:46:41 PM2/22/12
to pylons-discuss
Two possible alternatives

# if you had another reason for a list comprehension
def unicode_wrap(listed):
     return [ unicode(i) for i in listed ]

def unicode_wrap(listed):
     return map(unicode, listed)

Chris Lambacher

unread,
Feb 22, 2012, 9:07:03 PM2/22/12
to pylons-...@googlegroups.com, formencod...@lists.sourceforge.net
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:

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

Jonathan Vanasco

unread,
Feb 24, 2012, 11:59:07 AM2/24/12
to pylons-discuss
Crhis- thanks. i'm on that list as well. this seems to be more of an
'implementation under pyrmaid' question than about how formencode
works, which is why I asked here.

Jonathan Vanasco

unread,
Feb 24, 2012, 12:05:49 PM2/24/12
to pylons-discuss
also, thanks all.

It seems like Pipe(Int(),OneOf()) is the 'right' option, as it'll get
me the ints i want.

cd34's suggestions were dead on and way better than my sleepless hack
of string tempating !
Reply all
Reply to author
Forward
0 new messages