durusmail: quixote-users: legal py & uri names don't overlap...
legal py & uri names don't overlap...
2004-04-05
2004-04-05
2004-04-05
2004-04-05
legal py & uri names don't overlap...
Neil Schemenauer
2004-04-05
On Mon, Apr 05, 2004 at 12:47:44PM -0400, Kendall Clark wrote:
> http://monkeyfist.com/rdf-model/foo
>
> which I'd like to map to:
>
> _q_exports = ["rdf-model"] where "rdf-model" is an instance of a
> ModelDispatch class. But, clearly, "rdf-model" isn't a legal Python
> variable name (alas -- my #1 Python wart!).
>
> What is the general trick to having URI path components with "-" and
> other illegal Python variable characters mapped to Python bits in
> Quixote? I'd prefer a solution other than doing a dynamic lookup; that
> will work in some cases, but not in others (I guess it will *work* in
> all cases, but ugh!).

You could also abuse Python's namespaces:

    def rdf_model(...):
        ...

    globals()['rdf-model'] = rdf_model

Using _q_lookup is probably the cleanest.  Something like:

    def _q_lookup(request, component):
        if component == 'rdf-model':
            return ModelDispatch(...)
        ...

If you don't want to treat some names specially, you could have a
standard pattern:

    def rdf_model(...):
        ...

    ...

    exports = {
        'rdf-model': rdf_model,
        'hello_world': hello_world,
        ...
    }

    def _q_lookup(request, component):
        return exports.get(component)

That last bit is untestest but should work.  Returning None from
_q_lookup signals a non-existent component, if I recall correctly.

  Neil


reply