[Zope] int's as keys for dictionaries

Bryce Harrington bryce at osdl.org
Wed May 26 13:43:18 EDT 2004


On Wed, 26 May 2004, James Henderson wrote:
> On Wednesday 26 May 2004 5:53 pm, Bryce Harrington wrote:
> > On Wed, 26 May 2004, Ben Last (Zope) wrote:
> > > > Bryce Harrington
> > > >
> > > > > > <dtml-let d_test_parameter="{'1':'a', '2':'b'}">
> > > >
> > > > Yes, that is correct.  The data structure has strings, but I need to
> > > > access them using ints as keys.  So I have to convert the ints into
> > > > strings in order to retrieve the data.
> > >
> > > So why not init with
> > > d_test_parameter="{1:'a', 2:'b'}
> > > ...so that your keys are ints from the beginning?
> >
> > Zope is converting them to strings on form submission.
> >
> > Bryce
>
> Hi Bryce,
>
> Did you know that you can convert values directly to ints by adding ":int" to
> the value of the "name" atribute in the form element, e.g.:
>
> <input name="smtp_port:int" />
>

Thanks, but yes, I know about this.  But that is for the _value_,
whereas in my situation the conversion is occuring for the _key_.
Specifically, my input form has widgets like:

 <dtml-in prefix="loop" expr="method.sql_list_test_parameters_for_test(test_uid=d_test_uid)">
  ...
      <input type="checkbox" name="d_test_parameter.<dtml-var uid>:record:ignore_empty" value="1" /></td>
 </dtml-in>

The d_test_parameter is converted into a dictionary by the ":record"
modifier, with the keys being the id's that I pull from the database (as
ints), but in putting them into record form, Zope is converting the keys
to strings.

Now, it happens that the values also need to be ints in some cases (but
the user can leave them blank), but that's a separate issue.  I did try
appending the ":int" to that, but there were two problems.  First, by
putting ":int" on the end it appears to make the field required - i.e.,
the user can't leave it blank; I looked for an ":optional" modifier
since there's a ":required" one, but have not yet found it.  Second, if
the user enters a non-int value, Zope catches it and displays an error
page (which is good) but I have not been able to find out how to replace
that error message with a user friendly one of my own, and from what
I've read on the Zope site, there is no such capability (which is bad).
So it looks like in order to address both these issues, I need to forgo
the ":int" modifier and create my own error handling system.  I took
this from reading http://www.zope.org/Members/Zen/howto/FormVariableTypes.

Bryce






More information about the Zope mailing list