On Sun, Oct 11, 2009 at 9:10 PM, Steven Roussey <[email protected]> wrote:
>
>> The predicate strings are interpreted by the user. So you can parse
>> arguments, and *interpret* any argument as a name to lookup in
>> ScopedContext. This is done with the FunctionRegistry API.
>
> If I understand this correctly, you mean that every predicate does a
> RegExp to see if it should handle it? So if I have a thousand
> predicates, every time there is a predicate, you do 1000 regular
> expression searches to see which one should get it?
No, there is no regex involved in looking up predicates/formatters.
I'm not sure where you're getting this -- yours is the one that uses
CALL_RE inside the template engine. Of course, someone may implement
Lookup with a regex, but that's up to the app.
Also, the lookups into FunctionRegistry all happen at compile time.
>> So for example, you can implement operators with "is" expressions
>> (color is "blue"), but it's not in the core language. It is
>> implementable just like the Pluralize formatter.
>
> A possible implementation of 'is' passed to a template:
>
> new Template( tpl_str, options:{ predicates={
> 'is': function(str, context, args) {
> if (!this.isRE)
> this.isRE = new RegExp('^(not\\s+)?"?(.*?)"?$');
> var matches = args.match(this.isRE);
> var val=matches[2]||'';
> matching= str==val;
> return matches[1] ? !matching : matching;
> }
> }})
That is not all that different than implementing 'is' with my system,
except that more things happen at compile time. You're doing a regexp
there at runtime.
> Or every predicate/formatter is an object { regexp:'^is (not\\s+)?"?
> (.*?)"?$', fn: function(str,context,args){...}, name:'The IS
> predicate' }. I guess I'll see what is in the Python version...
>
>> I think that will clear things up. Most of this mail misunderstands
>> what is already there.
>
> I find Python hard to read for the impatient. I'll have to slow down
> and look at the registry code. I was thinking that it was simply a way
> to deal with Python's issue of calling with a variable number of
> arguments, that I didn't think to look that there was more there.
Python is very similar to JavaScript. You just get rid of the braces
and semicolons, pretty much. And Python has inheritance like Java,
rather than JavaScript's prototypal inheritance. So it's basically
all syntactical differences.
I think you are not seeing the FunctionRegistry pattern. This is a
really straightforward pattern that is common in code of all
languages, and it's both more powerful and simpler than what you have.
I think you're just hung up a bit on the abstraction -- there is more
indirection going on.
But if you write down the JS code from the calling side should make
sense. It translates very easily from Python. In Python it looks
like this, in jsontemplate_unittest. There are 3 tests -- use a dict,
use a function, and use a FunctionRegistry.
So you can choose the simple way if you have a simple extension
method, or the complicate way if you need to do something like
implement "is". And you can mix the two with ChainedRegistry (need a
test for that).
class FunctionsApiTest(testy.Test):
"""Tests that can only be run internally."""
def testMoreFormattersAsDict(self):
t = jsontemplate.Template(
'Hello {name|lower} {name|upper}',
more_formatters={
'lower': lambda v: v.lower(),
'upper': lambda v: v.upper(),
})
self.verify.Equal(t.expand({'name': 'World'}), 'Hello world WORLD')
def testMoreFormattersAsFunction(self):
def MyFormatters(user_str):
if user_str == 'lower':
return lambda v: v.lower()
elif user_str == 'upper':
return lambda v: v.upper()
else:
return None
t = jsontemplate.Template(
'Hello {name|lower} {name|upper}', more_formatters=MyFormatters)
self.verify.Equal(t.expand({'name': 'World'}), 'Hello world WORLD')
def testMoreFormattersAsClass(self):
class MyFormatters(jsontemplate.FunctionRegistry):
def Lookup(self, user_str):
"""Returns func, args, type."""
if user_str == 'lower':
func = lambda v, context, args: v.lower()
elif user_str == 'upper':
func = lambda v, context, args: v.upper()
else:
func = None
return func, None
t = jsontemplate.Template(
'Hello {name|lower} {name|upper}', more_formatters=MyFormatters())
self.verify.Equal(t.expand({'name': 'World'}), 'Hello world WORLD')
Andy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JSON
Template" 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/json-template?hl=en
-~----------~----~----~----~------~----~------~--~---