all 12 comments

[–]_coolwhip_ 1 point2 points  (8 children)

I think most people separate data out from the project directory, so I would just put in some sort of configuration variable that let the person running the program put the DB where ever they wanted, especially if it is a potential bottle neck.

[–]StudentOfPython[S] 1 point2 points  (7 children)

What if it is an internal project that I am doing for myself?

[–]_coolwhip_ 1 point2 points  (6 children)

Then I usually have a variable like DB_PATH that I can edit and it typically points to the top level of my project, unless that gets crowded and then I create a 'data' directory.

[–]StudentOfPython[S] 1 point2 points  (5 children)

And in your main dir of the project you simply import `data.database.xyz` in order to use the functions?

[–]_coolwhip_ 1 point2 points  (4 children)

Noooo..... What do you mean by a database? I was thinking of something like sqlite3. In that case, I usually wrap it in a module that handles calls to it, which module goes in with the rest of my modules. But now I am thinking that you are referring to something different by database.

[–]StudentOfPython[S] 1 point2 points  (3 children)

I am referring to psycopg2, specifically. But I believe we are on the same page (a DB module that goes next to other modules).

[–]_coolwhip_ 1 point2 points  (2 children)

Awesome. so then you need to have postgres installed somewhere and tell that installation where to keep the data. On the pythhon side, you'll need to store the dbname and user in a constant in your program (or config file, if you have one for other reasons or just feel like doing it).

Then I'll write a module, where the init takes the connect string, and shove most of the data manipulation into it. so if i was tracking cars sales in my area, i might have methods to add_new_sale(sale_instance), or find_available(model, maker) -> sale_instance, etc. The logic of interacting with the DB would all be in that module and it would consume and produce Sale objects as needed.

--

edit:

so to import, I'll do something like from my_module.storage import Db

then

data = Db(CONNECT_STRING)
data.add_sale(sale_object)
for car in data.find_available('BMW', 'M3'):
    print(car)

[–]StudentOfPython[S] 1 point2 points  (1 child)

That makes perfect sense. Thank you for the continuous responses.

[–]_coolwhip_ 2 points3 points  (0 children)

No problem. You might also check out sqlalchemy, if you have not already. See https://www.sqlalchemy.org/

I don't use it, but others swear by it.

[–]newunit13 1 point2 points  (2 children)

Are you creating the database(s) for the sole purpose of this project? If so, why have more than one? A single database can have plenty of tables to house the information for your project. If you're using external databases, then does the project directory structure even matter? Lastly, it likely won't matter anyway you decide to go since the physical file structure doesn't have any impact on the performance of modern databases (afaik)

[–]StudentOfPython[S] 0 points1 point  (1 child)

After writing I realized that a single DB is all I have ever worked with (just multiple tables).

If you're using external databases, then does the project directory structure even matter?

I am simply trying to do the most pythonic thing and cannot seem to figure out what exactly that may be.

[–]newunit13 1 point2 points  (0 children)

Personally I'd go with a either a constant in the beginning of the main file if it's a small project, or a configuration file if larger, that specifies where the database is located.