Giter Club home page Giter Club logo

pyp-w3-gw-simple-database-system's Introduction

[pyp-w3] Simple Database System

You'll need to build a simple database system using files. The fact that your database is using files underneath should be COMPLETELY hidden to your user. From the outside (public interface), the user must play with your database library as any other database client, without knowing how it works internally.

To start using a database, you can either create a new database:

>>> db = create_database('library')

or connect to an existing one:

>>> db = connect_database('library')  # library DB already exists

To display the whole list of tables in a database, use the show_tables function:

>>> db.show_tables()
["authors"]

Tables

Once you have your database connection, you must be able to create tables in the database. While creating a table, you must provide the columns configuration with proper names and data types for each of them.

>>> db.create_table('authors', columns=[
    {'name': 'id', 'type': 'int'},
    {'name': 'name', 'type': 'str'},
    {'name': 'birth_date', 'type': 'date'},
    {'name': 'nationality', 'type': 'str'},
    {'name': 'alive', 'type': 'bool'},
])

After the table is created, a new dynamic database attribute with the name of the table will be available. In the example above, when we created the table authors, we should see the following attribute: db.authors. That attribute will be the main accessor for table-level operations. That means, any time we want to interact with the authors table, we'll have to do it through db.authors.

There are a few operations we can do in a table-level fashion:

Table count

We should be able to get the count of rows in the table, using the count() method of that table:

>>> db.authors.count()
0

Inserting data

We need to use the insert table method to insert data in the table. You have to make sure the column order and data types are respected:

>>> db.authors.insert(1, 'Jorge Luis Borges', date(1899, 8, 24), 'ARG', False)

While inserting data, errors might occur:

>>> db.authors.insert(1, 'Jorge Luis Borges', date(1899, 8, 24), 'ARG', False, 'something-else')
ValidationError: Invalid amount of fields.

>>> db.authors.insert(1, 'Jorge Luis Borges', "1899-8-24", 'ARG', False) # must be a date object
ValidationError: 'Invalid type of field "birth_date": Given "str", expected "date"'

Getting information from a table

If you need to see the columns configuration of certain table, you can execute the describe function:

>>> db.authors.describe()
[
    {'name': 'id', 'type': 'int'},
    {'name': 'name', 'type': 'str'},
    {'name': 'birth_date', 'type': 'date'},
    {'name': 'nationality', 'type': 'str'},
    {'name': 'alive', 'type': 'bool'},
]

Querying

After inserting data in your tables, you probably want to query it. To do so we use the query table method. The result must be an iterator:

>>> arg_authors = self.db.authors.query(nationality='ARG')
>>> for author in arg_authors:
        print(author.name)
"Jorge Luis Borges"
"Julio Cortázar"

Note that each element returned by the iterator must be a custom object, with dynamic attributes containing values per each column in the row (ie: "id", "name", "birth_date", etc)

To fetch the whole list of rows in the table without any filtering, use the all table method:

>>> gen = self.db.authors.all()

The result returned by all is also an iterator, following the same specifications as the query function.

Extra points

There's a huge room for improvement in this specification. You can take a lot of learning from this example, after all, you're implementing a real database system. Some functionalities missing:

  • Sorting
  • indexing
  • Default, unique, required values
  • Foreign keys

pyp-w3-gw-simple-database-system's People

Contributors

martinzugnoni avatar psy-kosh avatar santiagobasulto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.