Skip to content

Instantly share code, notes, and snippets.

@dstufft
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save dstufft/9271370 to your computer and use it in GitHub Desktop.

Select an option

Save dstufft/9271370 to your computer and use it in GitHub Desktop.
## Option 1
# warehouse/packaging/db.py
class Database:
def query_that_thing(self):
pass
# warehouse/accounts/db.py
class Database:
def query_that_other_thing(self):
pass
# warehouse/application.py
import warehouse.pacakging.db
import warehouse.accounts.db
class Application:
def __init__(self):
self.db = {
"packaging": warehouse.packaging.db.Database(),
"accounts": warehouse.accounts.db.Database(),
}
app = Application()
app.db["packaging"].query_that_thing()
## Option 2
# warehouse/packaging/db.py
class Database:
def query_that_thing(self):
pass
# warehouse/accounts/db.py
class Database:
def query_that_other_thing(self):
pass
# warehouse/db.py
import warehouse.packaging.db
import warehouse.accounts.db
class Database(warehouse.packaging.db.Database, warehouse.accounts.db.Database):
pass
# warehouse/application.py
import warehouse.db
class Application:
def __init__(self):
self.db = warehouse.db.Database()
app = Application()
app.db.query_that_thing()
## Option 3
# warehouse/db.py
class Database:
def query_that_other_thing(self):
pass
def query_that_other_thing(self):
pass
# warehouse/application.py
import warehouse.db
class Application:
def __init__(self):
self.db = warehouse.db.Database()
app = Application()
app.db.query_that_thing()

Thoughts

Option 1

Items are split up by "topic" in the code and when calling the query functions you can easily see where it comes from. However it's more verbose and I'm not sure that the namespacing is actually buying us anything at call time. Keeping things split up in the code makes it easier to write the functions though.

Option 2

Basically all the same reasons for keeping things split up at a code level, but removes the call time namespacing. This feels like it might more accurately represent the db as a single shared thing, however colliding function names may make this a bad idea? Will that actually be a problem in reality?

Note: This could also be written as a container class that dispatches to the contained Database classes instead of using subclassing.

Option 3

I really don't like this one much, it removes the clean seperation of code when written however I do like that it removes the namespacing on accessing app.db that I feel is unneeded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment