Typecasting in DTML (or is this Python?)
Hello again list... I need to typecast [amount] as a string - but only within the history string. in my attempt below, history finished up as.... Advertising authority number 25020030718 For $['amount'] Where I need amount to show up as the specific amount. <dtml-call "REQUEST.set('amount',_['amount'])"> <dtml-call "REQUEST.set('history',_['history1'] + REQUEST['authority'] + ' For $' + _.str(['amount']))"> I've tried a few variants, but not had much joy. Any help would be fantastic... thanks in advance Julian
The bracket syntax you're using is probably not necessary in any of the cases you're using it... it's mostly for getting names that are determined at run time, eg: <dtml-var "_[my_name]"> If you're hand-coding literal strings, you're probably working too hard. Check out the % operator for string formatting. It's particularly useful for currency, as it is possible to specify decimal places and other fun stuff. Since you use the word "typecasting" I'm guessing you're probably familiar with this operator already. Try this for starters, replacing both tags: <dtml-var "'%s %s For $%d' % (history1, authority, amount)"> That might not be exactly right, but should get you closer to where you're going. HTH, Dylan On Fri, 2003-07-18 at 00:06, Julian Clark wrote:
Hello again list...
I need to typecast [amount] as a string - but only within the history string. in my attempt below, history finished up as....
Advertising authority number 25020030718 For $['amount']
Where I need amount to show up as the specific amount.
<dtml-call "REQUEST.set('amount',_['amount'])"> <dtml-call "REQUEST.set('history',_['history1'] + REQUEST['authority'] + ' For $' + _.str(['amount']))">
I've tried a few variants, but not had much joy.
Any help would be fantastic... thanks in advance
Julian
_______________________________________________ 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 )
Dylan, Thanks for your prompt reply. I'm only familiar with typecasting from my experience in Java (which isn't huge). it doesn't look like you're using the % as modulus in this context. Would you mind explaining a little of the syntax or pointing me towards a url which would have some tutorials/documentation. The fields which I am manipulating are driven by a form (actually, a series of forms) and so the data is a little dynamic - would this still be a suitable solution? The solution I had worked well until I changed the amount field in the database from text to numerical (so that other queries would operate correctly with it) at which point the history string lost its banana's because it was now dealing with a string and a float. Thanks Julian ----- Original Message ----- From: "Dylan Reinhardt" <zope@dylanreinhardt.com> To: "Julian Clark" <Julian.Clarke@b-online.com.au> Cc: "zope" <zope@zope.org> Sent: Friday, July 18, 2003 3:39 PM Subject: Re: [Zope] Typecasting in DTML (or is this Python?) The bracket syntax you're using is probably not necessary in any of the cases you're using it... it's mostly for getting names that are determined at run time, eg: <dtml-var "_[my_name]"> If you're hand-coding literal strings, you're probably working too hard. Check out the % operator for string formatting. It's particularly useful for currency, as it is possible to specify decimal places and other fun stuff. Since you use the word "typecasting" I'm guessing you're probably familiar with this operator already. Try this for starters, replacing both tags: <dtml-var "'%s %s For $%d' % (history1, authority, amount)"> That might not be exactly right, but should get you closer to where you're going. HTH, Dylan On Fri, 2003-07-18 at 00:06, Julian Clark wrote:
Hello again list...
I need to typecast [amount] as a string - but only within the history string. in my attempt below, history finished up as....
Advertising authority number 25020030718 For $['amount']
Where I need amount to show up as the specific amount.
<dtml-call "REQUEST.set('amount',_['amount'])"> <dtml-call "REQUEST.set('history',_['history1'] + REQUEST['authority'] + ' For $' + _.str(['amount']))">
I've tried a few variants, but not had much joy.
Any help would be fantastic... thanks in advance
Julian
_______________________________________________ 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 Fri, Jul 18, 2003 at 05:17:48PM +0800, Julian Clark wrote:
Dylan,
Thanks for your prompt reply. I'm only familiar with typecasting from my experience in Java (which isn't huge). it doesn't look like you're using the % as modulus in this context. Would you mind explaining a little of the syntax or pointing me towards a url which would have some tutorials/documentation.
<dtml-var "foo"> is shorthand for <dtml-var expr="foo">; in either case, the stuff in quotes is a Python expression. (I wasn't sure if you knew that.) So what you're really asking is for a Python tutorial. There are lots: http://www.python.org/doc/Newbies.html and the "official" tutorial: http://www.python.org/doc/current/tut/tut.html -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's COSMIC BARREL LAWYER! (random hero from isometric.spaceninja.com)
Julian Clark wrote:
Dylan,
Thanks for your prompt reply. I'm only familiar with typecasting from my experience in Java (which isn't huge). it doesn't look like you're using the % as modulus in this context. Would you mind explaining a little of the syntax or pointing me towards a url which would have some tutorials/documentation.
http://www.python.org/doc/current/lib/typesseq-strings.html
The fields which I am manipulating are driven by a form (actually, a series of forms) and so the data is a little dynamic - would this still be a suitable solution?
Yes
The solution I had worked well until I changed the amount field in the database from text to numerical (so that other queries would operate correctly with it) at which point the history string lost its banana's because it was now dealing with a string and a float.
The % stuff will work fine with that. You're still really abusing DTML though, please look at writing Python Scripts. You'll find it much less frustrating and we'll be much mroe able to help you :-) Chris
On Fri, 2003-07-18 at 02:17, Julian Clark wrote:
I'm only familiar with typecasting from my experience in Java
Ah, right. To me, typecasting is something you deal with in C. Anyway...
it doesn't look like you're using the % as modulus in this context.
You're right about that. x % y can mean two things, depending on what x and y are. If x and y are numeric types, it means "x mod y." If, however, x is a string containing special sequences like %s or %d and y is a variable or tuple that has the same number of elements, the expression returns y formatted in the way x specifies. Two quick examples: '%s' % x # x formatted as a string '%s %s' % (x, y) # x and y as a string with a space. There are other fun things you can do with this operator... it's worth reading up on. If you're just getting started with Python, you're in luck... there are a number of great tutorials available. See this page for links to a bunch of them: http://python.org/doc/Intros.html HTH, Dylan
participants (4)
-
Chris Withers -
Dylan Reinhardt -
Julian Clark -
Paul Winkler