int's as keys for dictionaries
I'm running into an issue I don't understand when using ints as keys in hashes. <dtml-let d_test_parameter="{'1':'a', '2':'b'}"> <p>We set id="1"</p> <dtml-let id="1"> <p>This works: <dtml-var "d_test_parameter.get('1')"></p> <p>But this doesn't: <dtml-var "d_test_parameter.get(id)"></p> <p>Nor does this: <dtml-var "d_test_parameter.get('id')"></p> </dtml-let> <p>Now set id="'1'"</p> <dtml-let id="'1'"> <p>This works: <dtml-var "d_test_parameter.get('1')"></p> <p>Now this works: <dtml-var "d_test_parameter.get(id)"></p> <p>This still doesn't: <dtml-var "d_test_parameter.get('id')"></p> </dtml-let> </dtml-let> How can we force id to be interpreted as a string for the dictionary key in the first case? Thanks, Bryce
Nevermind, I figured it out. "%d" % id On Tue, 25 May 2004, Bryce Harrington wrote:
I'm running into an issue I don't understand when using ints as keys in hashes.
<dtml-let d_test_parameter="{'1':'a', '2':'b'}">
<p>We set id="1"</p> <dtml-let id="1"> <p>This works: <dtml-var "d_test_parameter.get('1')"></p>
<p>But this doesn't: <dtml-var "d_test_parameter.get(id)"></p>
<p>Nor does this: <dtml-var "d_test_parameter.get('id')"></p>
</dtml-let>
<p>Now set id="'1'"</p> <dtml-let id="'1'"> <p>This works: <dtml-var "d_test_parameter.get('1')"></p>
<p>Now this works: <dtml-var "d_test_parameter.get(id)"></p>
<p>This still doesn't: <dtml-var "d_test_parameter.get('id')"></p>
</dtml-let>
</dtml-let>
How can we force id to be interpreted as a string for the dictionary key in the first case?
Thanks, Bryce
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Tue, May 25, 2004 at 06:15:09PM -0700, Bryce Harrington wrote:
I'm running into an issue I don't understand when using ints as keys in hashes.
<dtml-let d_test_parameter="{'1':'a', '2':'b'}">
You're using strings as hashes, not ints. {'1':'a'} is not {1: 'a'}. -- Paul Winkler http://www.slinkp.com
On Tue, 25 May 2004, Paul Winkler wrote:
On Tue, May 25, 2004 at 06:15:09PM -0700, Bryce Harrington wrote:
I'm running into an issue I don't understand when using ints as keys in hashes.
<dtml-let d_test_parameter="{'1':'a', '2':'b'}">
You're using strings as hashes, not ints. {'1':'a'} is not {1: 'a'}.
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. Bryce
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? ben
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
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" /> This works for other types too. See a concise explanation in section 2.6.3 of Dieter Maurer's book: http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html#ZPublisher HTH, James -- James Henderson, Logical Progression Ltd. http://www.logicalprogression.net/ http://sourceforge.net/projects/mailmanager/
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
Bryce Harrington wrote:
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>
Argh! What are you doing?! ;-)
ints), but in putting them into record form, Zope is converting the keys to strings.
Er. No. I think you'll find browsers and http forms have no concept of anything other than character data and so Zope is doing nothing, as it should. What you want when using the record type is: <tr tal:repeat="parameters python:here.sql_list_test_parameters_for_test(test_uid=d_test_uid)"> <td>Record <tal:x replace="parameters/uid"></td> <td> <input type="hidden" name="d_test_parameter.uid:int:record" tal:attributes="value parameters/uid"> <input type="checkbox" name="d_test_parameter.something:record:ignore_empty" tal:attributes="value parameters/something"/> <!-- etc --> </td> </tr> then, in your processing script, "print context.REQUEST.d_test_parameter" will give: (('uid':1,'something':'xxx'),('uid':2,'something':'yyy'))
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;
Indeed. DateTime's are even more fun ;-)
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).
standard_error_message. But doing this kindof validation error catching there is a PITA. I normall just don't use things like :int and :date unless they're hidden form variables that are already populated, like your uid...
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.
You could just use Formulator ;-) cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
On Wednesday 26 May 2004 09:53 am, 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.
You may want to use zope's built in request marshalling to deal with this: <input type="text" name="key:int"> Where the ':int' instructs zope to convert to an integer in the request. Alec Mitchell
participants (6)
-
Alec Mitchell -
Ben Last (Zope) -
Bryce Harrington -
Chris Withers -
James Henderson -
Paul Winkler