PyFetch by Mathieu Courcier-Flores.

What is PyFetch?

PyFetch is a python library designed to make it easy to store data outside your script. It’s like sqlite3 but easier (and slower).

Importing the library:

Use from PyFetch import database to import the library.

Making a new object:

db = database("File.pf")

This creates a new PyFetch object, database() takes one argument which should be the path of your database. The path should always be of extension “pf” (PyFetch). This doesn’t create a new file.

Making a database file:

There are two methods of doing this;

db.make()   and   db.overwrite().

The former creates a new file with the path we gave if it doesn’t already exist.

The latter creates a new file with the path we gave and overwrites it if it already exists.

Inserting stuff in our database:

This is very easy to do, just pass in a dictionary item containing your information to this method:

db.insert(dictionary)

This inserts your dictionary on a newline.

Each line is assigned an id corresponding to the line number so it’s easier to find later on.

Example:

db.insert({"Mood": "Happy", "Raining": False})

db.insert({"Mood": "Angry", "Raining": False})

db.insert({"Mood": "Sad", "Raining": True})

This is what “File.pf” looks like now:

0—{“Mood”: “Happy”, “Raining”: False}

1—{“Mood”: “Angry”, “Raining”: False}

2—{“Mood”: “Sad”, “Raining”: True}

(0, 1, 2) are the ids of the dictionaries. We can use this to identify them later on.

Querying the database:

There are also two methods of doing this;

db.query()   and   db.fetch_all().

The former accepts one argument: the id of the dictionary you want. If the id exists the method will return the data.

The latter takes no arguments and returns a list with every dictionary in the file in order.

Removing an entry:

Removing a dictionary from our file is as easy as passing its id to the method:  db.remove()

Note:

Removing id number 2 in a file with ids ranging from 0 to 4 (for example) will leave a gap at index 2: (0, 1, 3, 4,). Call function re_id() to fix this

Reformating the ids in a database:

After removing ids, you’re left with unorganized dictionaries.

Example:

0—{“Mood”: “Happy”, “Raining”: False}

1—{“Mood”: “Angry”, “Raining”: False}

2—{“Mood”: “Sad”, “Raining”: True}

3—{“Mood”: “Joyful”, “Raining”: False}

4—{“Mood”: “Excited”, “Raining”: False}

5—{“Mood”: “Gloomy”, “Raining”: True}

Calling:

db.remove(1)   and   db.remove(3) will leave you with this:

0—{“Mood”: “Happy”, “Raining”: False}

2—{“Mood”: “Sad”, “Raining”: True}

4—{“Mood”: “Excited”, “Raining”: False}

5—{“Mood”: “Gloomy”, “Raining”: True}

Here our database is disorganized and messy, calling re_id() with no arguments will result in the ids being removed and reset to the corresponding line number:

0—{“Mood”: “Happy”, “Raining”: False}

1—{“Mood”: “Sad”, “Raining”: True}

2—{“Mood”: “Excited”, “Raining”: False}

3—{“Mood”: “Gloomy”, “Raining”: True}