durusmail: quixote-users: Re: import and export - help!
import and export - help!
2004-04-18
Re: import and export - help!
2004-04-19
2004-04-20
2004-04-20
2004-04-20
2004-04-20
2004-04-21
2004-04-21
Jason E. Sibre (2 parts)
2004-04-21
Re: import and export - help! (resolved)
2004-04-21
2004-04-20
2004-04-20
Re: import and export - help!
Graham Fawcett
2004-04-20
John Miller wrote:
> On Apr 19, 2004, at 9:54 PM, Graham Fawcett wrote:
>> and in qxdemo/ui/__init__.py, I'd include
>>
>>    import books, keywords
>>    _q_exports = ['links', 'books', 'keywords']
>
>
> Actually, I have:
> '''
> _q_exports = ['links', 'book', 'keywords']
> from qxdemo.ui import links
> from qxdemo.ui.links import book, keywords

Using absolute module names, as you have done here, is a Good Idea.

I hadn't thought of exporting a module as well as some functions from
that module, but I suppose that's good if it works!


> However, I'm still baffled by the mechanics of importing and exporting
> files and methods *across directories*. Say, for example, I had a
> subdirectory inside the /qxdemo/ui directory - let's call it 'foo' and
> it has a 'bar.ptl' page with a method called 'run()'. How and where do
> I expose the 'run()' method? From /qxdemo/ui/__init__.py? From
> /qxdemo/ui/foo/__init__.py? From both? All the failed experiments I
> tried with qxdemo have left me wondering how to think 'quixotically'
> with respect to exposing methods and pages to the web across  directories.

There's no hard and fast rule, and there couldn't be a singular
"quixotic" approach. My advice is to think of the URL that you *want*,
and then build the namespaces to provide that URL.

     http://server/scriptname/foo/bar/run
     http://server/scriptname/foo/run
     http://server/scriptname/run

are all possible ways of executing qxdemo.ui.foo.bar.run(). It's just a
matter of whether you import (and subsequently export) the run()
function in

     qxdemo/ui/foo/bar.py        (where run() is defined)
     qxdemo/ui/foo/__init__.py
     qxdemo/ui/__init__.py

respectively.

In general:

- picture the URL that you *want* to bind to the function,

- take the URL apart, one step at a time, and provide a namespace to
handle each part in turn, until you get to the function at the end.

Whether you use modules, class instances, or some jury-rigged
dict-subclass-with-a-q-exports-attribute to implement your namespaces is
up to you...

-- Graham



reply