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.



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

Reply via email to