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 as it is written in python, keep in mind that every function has been optimized to run as fast as possible).
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}
Methods:
count_lines():
Takes no argument and returns the number of lines in your database, which corresponds to the number of dictionaries.
line_num_through_id():
Takes an integer id as its only argument and returns the line number corresponding to the id. The line number is normally always the same as the id number except after removing an entry without calling re_id()
is_valid_path():
Takes no arguments and returns True if the database path is valid or False if it’s not. This method serves no purpose to the user. it was supposed to be private but in my efforts of making everything as open-source as possible, I made every method public.
make():
Takes no arguments and creates a database file with the path passed in when the object was made. Nothing happens if the database already exists.
insert():
Takes a single dictionary item and appends it in the database on a new line and a new id.
query():
Takes an id as its only argument and returns the dictionary stored at that location.
Raises an IndexError if the id does not exist.
remove():
Takes an id as its only argument and removes the dictionary stored at that location. Raises an IndexError if the id does not exist.
fetch_all():
Takes no arguments and returns a list with every dictionary in order. The ids are stripped away.
fetch_all_raw():
Takes no arguments and returns a list with every line in the database in order. The ids are kept and each item in the list is a string.
update():
Takes two arguments, an id and a dictionary. This method quickly replaces the dictionary present at the location of the id with the dictionary passed in as the second argument.
re_id():
Takes no arguments and reformats the database so that all ids are correct and ordered. Read “Reformating the ids in a database” for more information.
overwrite():
Takes no arguments and creates a database file with the path passed in when the object was made. Overwrites if the database already exists.
fetch_by_key():
Takes a single string argument and returns a list with all dictionaries that have matching keys in order.
There are 2 optional parameters:
exact and fast. By default, exact is set to False, and fast is set to True.
exact= True will search every key in every dictionary for an exact match.
exact= False will search every key in every dictionary and return the dictionary if the key given as an argument is in the key of the dictionary.
exact= False , fast= True will quickly search the database line by line and return the dictionary present in that line if the given key is present somewhere in the line (May return false positives). You may give the method a whole dictionary as a string type or parts of a dictionary.
fetch_by_value():
Takes a single string argument and returns a list with all dictionaries that have matching values in order.
There are 2 optional parameters:
exact and fast . By default, exact is set to False, and fast is set to True.
exact= True will search every value in every dictionary for an exact match.
exact= False will search every value in every dictionary and return the dictionary if the value given as an argument is in the value of the dictionary.
exact= False , fast= True will quickly search the database line by line and return the dictionary present in that line if the given value is present somewhere in the line (May return false positives). You may give the method a whole dictionary as a string type or parts of a dictionary.
clear():
Takes no arguments. Removes everything in the database.
quick_write():
Takes a list containing dictionaries as an argument. Serves the same functionality as insert() but instead of writing a single dictionary at a time, it quickly writes every dictionary in the list provided as an argument. Calling quick_write() with a list of 50 000 dictionaries is way faster than calling insert() with a single dictionary 50 000 times.
quick_remove():
Takes a list containing ids as an argument. Serves the same functionality as remove() but instead of removing a single dictionary at a time, it quickly deletes every dictionary in the list provided as an argument. Calling quick_remove() with a list of 50 000 ids is way faster than calling remove() with a single id 50 000 times.
id_by_value():
Takes a string value as a single argument and returns a list of ids that have dictionaries with matching values.
fast is an optional parameter that is set to false by default.
Setting it to true will dramatically speed up the search but may very rarely return false positives. I recommend always setting it to true.
id_by_key():
Takes a string key as a single argument and returns a list of ids that have dictionaries with matching keys.
fast is an optional parameter that is set to false by default.
Setting it to true will dramatically speed up the search but may very rarely return false positives. I recommend always setting it to true.
All methods:
10. update()
11. re_id()
12. overwrite()
13. fetch_by_key()
14. fetch_by_value()
15. clear()
16. quick_write()
17. quick_remove()
18. id_by_value()
19. id_by_key()