On Sep 23, 2013, at 11:52 AM, Michael Bayer <mike...@zzzcomputing.com> wrote:

> 
> On Sep 23, 2013, at 11:15 AM, Donald Stufft <donald.stu...@gmail.com> wrote:
> 
>> 
>> 
>> Well mostly I've isolated other pieces of global state and while metadata 
>> itself probably isn't a problem I was hesitant to add it as a piece of 
>> global state since I didn't have anything else like that. I did come up with 
>> a solution though that I think works Ok. It's basically allowing me to 
>> declare my tables at the top level (but not use them from there) so that I 
>> can organize my code better, but then the "real" tables exist inside of my 
>> application object where I can use them.
>> 
>> Examples here:
>> 
>> https://github.com/dstufft/warehouse/blob/werkzeug/warehouse/packaging/models.py
>> https://github.com/dstufft/warehouse/blob/werkzeug/warehouse/models.py
>> https://github.com/dstufft/warehouse/blob/werkzeug/warehouse/application.py#L37-L46
>> 
>> I haven't settled on this approach (notably I need to figure out how it's 
>> going to interact with alembic) but so far It seems to work alright.
> 
> 
> In this pattern, warehouse.packaging.models has a dependency on "from 
> warehouse import models"  in order to get at the Table stub class - then the 
> application module reaches down into a set of warehouse.* modules to get at 
> the tables it wants.   warehouse.packaging.models is reluctant here to 
> declare itself as a part of something - it would rather that external actors 
> reach down to get at it.
> 
> If the warehouse.Table class were an actual SQLA Table object, and the 
> original question, "can I attach .metadata after the fact" were being used 
> here, this pattern would be a little broken - because 
> warehouse.packaging.models is advertising itself as "please reach down and 
> use me!", but then, "oh only *one* of you can use me!" - basically, 
> warehouse.application would be *setting global state* into a module that has 
> gone through great lengths to avoid knowing about any global state.    I find 
> declaration of state ownership preferable to runtime injection of that 
> ownership.
> 
> if you just changed line 17 of warehouse.packaging.models from "from 
> warehouse import models" to "from warehouse.models import metadata" and then 
> used sqlalchemy.Table directly, you'd trim out a lot of quasi-reinvention 
> here and make things a lot more idiomatic to outsiders IMHO.


Yea I eventually removed most of that. The database is pretty much a global 
either way. (Being a shared external resource) so trying to pretend it isn't 
wasn't being very helpful. I ended up doing:

https://github.com/dstufft/warehouse/blob/werkzeug/warehouse/application.py#L38-L42
https://github.com/dstufft/warehouse/blob/werkzeug/warehouse/application.py#L50-L52
https://github.com/dstufft/warehouse/blob/werkzeug/warehouse/packaging/models.py

Which I think is pretty much the same as what you suggested.

-----------------
Donald Stufft
PGP: 0x6E3CBCE93372DCFA // 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to