durusmail: quixote-users: Re: Illegal Python Names cookbook recipe
Illegal Python Names cookbook recipe
2004-04-05
2004-04-05
2004-04-05
Bug fixes (was: Illegal Python Names cookbook recipe)
2004-04-07
2004-04-07
Bug fixes (was: Illegal Python Names cookbookrecipe)
2004-04-07
Patches for .7a3
2004-04-07
Re: Patches for .7a3
2004-04-08
StaticFile is broken (Quixote-0.7a3, scgi-1.2a2, Apache/1.3.27, FreeBSD 4.7)
2004-04-08
Re: Patches for .7a3
2004-04-21
2004-04-21
2004-04-06
2004-04-06
2004-04-06
2004-04-06
2004-04-06
2004-04-06
2004-04-06
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
Re: Illegal Python Names cookbook recipe
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-07
2004-04-08
2004-04-07
2004-04-07
2004-04-07
2004-04-06
2004-04-05
2004-04-05
2004-04-05
Re: R: [Quixote-users] Illegal Python Names cookbook recipe
2004-04-06
Re: Illegal Python Names cookbook recipe
Tom Jenkins
2004-04-07
Jason E. Sibre wrote:
>
> [Is the horse still breathing?]
>
> Along those lines, here's another idea for a helper class that makes the
> concept of putting stuff in _q_exports possible, this time with no
> modifications to Qx at all (except possibly making the helper class
> available somewhere).

Oooo and ONE MORE ! Let's hear it for Metaclasses!

class QExportType(type):
    def __new__(metacls, name, bases, dictionary):
       q_map = dictionary.get('_q_map')
       # if we have a _q_map entry then we add the keys to the _q_exports
       # list so the Publisher can find them and we'll stuff the _q_map
       # key/values into the dictionary so we don't need a _q_resolve
       if q_map:
          dictionary.setdefault('_q_exports', []).extend(q_map.keys())
          dictionary.update(q_map)
       return super(QExportType, metacls).__new__(metacls, name,
             bases, dictionary)



class Mod1(object):
    __metaclass__ = QExportType

    import foo, bar, twenty_bar, twelve_foo

    _q_exports = ['foo', 'bar']

    _q_map = {'20bar': twenty_bar,
         '12foo': twelve_foo,
         }

    def __call__(self, request):
       return 'in __call__'

    def _q_index(self, request):
       return 'in _q_index'


class Mod2(object):
    __metaclass__ = QExportType

    import foo, bar

    _q_exports = ['foo', 'bar']

    def __call__(self, request):
       return 'in __call__'

    def _q_index(self, request):
       return 'in _q_index'


I-wish-modules-could-have-a-metaclass.-ly yours

Tom




reply