On Sep 22, 2006, at 7:01 PM, Kevin Horn wrote:



On 9/22/06, Jorge Godoy <[EMAIL PROTECTED]> wrote:

"fumanchu" <[EMAIL PROTECTED]> writes:

> def quadratic(**kwds):
>     a = kwds.get('a', 1)
>     b = kwds.get('b', 2)
>     c = kwds.get ('c', 3)
>     a, b, c = map(int, (a, b, c))
>     root = math.sqrt((b ** 2) - (4 * a * c))
>     return ((-b + root)/2a), ((-b - root)/2a))

You can always make this look like:

def quadratic(**kwds):
     a, b, c = map(int, (kwds.get('a', 1),
                         kwds.get('b', 2),
                         kwds.get('c', 3)))
     root = math.sqrt((b ** 2) - (4 * a * c))
     return ((-b + root)/2a), ((-b - root)/2a))

(If you were using validators that "map" wouldn't be needed, making your code
even clearer ;-))


Can you provide an example of using validators here?

Sorry to interrupt... ;) I think He meant something like:

from tg.validators import Int
@validate(validator=dict(a=Int, b=Int, c=Int))
def quadratic(self, **kw):
    a = kw.get('a'); b= ....
    root = math.sqrt((b ** 2) - (4 * a * c))
    return ((-b + root)/2a), ((-b - root)/2a))

I personally tend to mix both named vars and kwds depending on the case. For example, control variables I name, data that gets thrown at a SO or SA mapped class I stuff in kwds for easier passing. Example:

def save(self, again=False, preview=False, **kw):
new = Whatever(**kw)
session.save(new)
flash("A new Whatever was created, beware")
if again:
return redirect('Whatever.url_for('add'))
if preview:
return redirect(new.url_for('preview'))
return redirect ('./')

Alberto

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to