Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating relational data (or a database) in Python?

I often use Python to prototype ideas for other platforms, a scratch pad of sorts. Right now I want to play with some ideas related to relational data in a database.

What is the best way to represent a database in Python? A set of tuples? Using a module like SQLite?

I'm looking for simple solutions in Python. If the solution is too "databasey" I mine as well do my prototyping in the database itself.

UPDATE: I wont actually be using Python with the database (I don't even have a specific database in mind), I just want to think with codes about questions like "If I have X relational data, can I answer Y questions, and solve Z problems?"

like image 330
Buttons840 Avatar asked Jan 12 '12 18:01

Buttons840


People also ask

How do you simulate data in Python?

Open up your terminal, navigate to where you've stored simulate.py , and run the following command: $ python simulate.py Input # of cashiers working: You'll be prompted to select the parameters you want for your simulation.

How do you create a relational database in Python?

We first create a database engine and then connect to the database engine using the to_sql function of the SQLAlchemy library. In the below example we create the relational table by using the to_sql function from a dataframe already created by reading a csv file.

Is Python good for simulation?

It is a good first language for people who have not programmed before, and it provides high-level data structures that are well-suited to express solutions to the problems we are interested in. Modeling and Simulation in Python is a Free Book.

Can Python make a database?

Creating a database in MySQL using pythonYou can connect to an existing database or, create your own. You would need special privileges to create or to delete a MySQL database. So if you have access to the root user, you can create any database.


2 Answers

Either sqllite3 or an Object Relational Mapper such as SQLAlchemy.

sqllite3 is very easy to set up and works just like a SQL database (for prototyping purposes, of course- not for production).

SQLAlchemy is a great way to work with a real database engine (which can itself be sqllite3) as if it were a set of objects. Creating a table, for example, looks something like this (from this tutorial):

from sqlalchemy import *

db = create_engine('sqlite:///tutorial.db')
metadata = BoundMetaData(db)

users = Table('users', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('name', String(40)),
    Column('age', Integer),
    Column('password', String),
)
users.create()

Similarly, it has simple functions and classes that allow you to insert elements, create relationships, and everything else you can do in a database.

ETA: I understand you won't actually be using Python with the database. However, working with an ORM like SQLAlchemy lets you deal with relational data as objects. It is approximately as easy as any form of prototyping, with the added benefit that it is closer to what you'll end up doing.

like image 154
David Robinson Avatar answered Sep 29 '22 13:09

David Robinson


The defacto relational database is SQLite, which can be used with a simple import sqlite3 statement. You can look at the documentation for further usage.

Alternatively, if you don't want to go down that path, you can use a dictionary for its (key, value) pairs. That can be done like so:

d = {}
d['key'] = 'value'
like image 20
Makoto Avatar answered Sep 29 '22 14:09

Makoto