Package: pi7db
Pi7db.Pi7db is a open-source powerful python package to manage directory based database in JSON format with number of features like Filter, sort, update etc.WHY I USE Pi7db ?There are some reasons may you like.NOSQLFlexible DataNo Need To Make Connection To Database. (Most Hated Stuff)Simple And Super Fast.Directory Based Databse (Place Where You Want).Advance Filter Options Like (Less Than, Greater Than, String Filter, List Filter etc).Read, Write, Sort, Update, Trash, Filter Data With Advance Features.Encrypt Data With Password.DATABASE STRUCTUREData is stored in binary files inside collections. Database structure is similar to mongodb.File Structure{"un_id":20202171823558180,"title":"This Is File","description":"This file strore information.","tags":["pi7db","database","NoSQL"],"views":10,"cr_dc_path":"Blog_Database/Collection_name/FileName.bin","comments":[{"user":"john","message":"My Name is Jonh"}]}There is un_id and cr_dc_path is avalible in every file by default represent a uniqe key for file and file path in systemINSTALLATIONInstallation with pip$pip3installpi7dbGetting StartedFirst import pi7db package in python file and create a object by passing argument Database name. It will automatically create a directory in current folder with that name where all datafiles will be stored.frompi7dbimportpi7dbdb=pi7db("Blog_Database")Manual Database pathif you want to store database in a specific directory then pass second argument of path.frompi7dbimportpi7dbdb=pi7db("Blog_Database","path/to/dir")Write DataWrite function is used for write Json data.frompi7dbimportpi7dbdb=pi7db("Blog_Database")data={"Name":"John","Surname":"Diamond","Tel-Number":9876543210,"Age":30,"Subject":["Python","Ruby","Perl"],"comments":[{"user":"Stephin","message":"John Is Best!"},{"user":"Alex","message":"We Love You John!"}]}db.write('Users',data)# This Will Write Data In Users Collection.Write Data In Specific File Manually.frompi7dbimportpi7dbdb=pi7db("Blog_Database")data={"Name":"John","Surname":"Diamond","Tel-Number":9876543210,"Age":30,"Subject":["Python","Ruby","Perl"],"comments":[{"user":"Stephin","message":"John Is Best!"},{"user":"Alex","message":"We Love You John!"}]}data2={"Name":"Richard","Surname":"Stallman","Tel-Number":9876543000,"Age":40,"Subject":["UNIX","C","Perl"],"comments":[{"user":"Marshal","message":"I Love C Language."},{"user":"Della","message":"C Is A Fast Language"}]}db.write('Users','John',data)db.write('Users','Richard',data2)# This Will Write Data In john (filename john) file inside Users Collection.Following Directory Tree Will Be Created In Your PC.You can directly call file during read time this will increase speed of your program.
For Example: You Can Call Specific Blog Post By Its Name.Read DataData read is pretty easy and fast in pi7db. With the help of read function.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.read()# This Will Read Entire Data In Database And Return Output In Dict.# But Read Entire Data Is Not Recomended When Your Database Size Is Large For Example 500mb.Read Data From CollectionData read is pretty easy and fast in pi7db. With the help of read function.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.read('Users')# This Will Read Entire Data In Users Collection And Return Output In Dict.Output:{"data":[{"un_id":"20202660459428127","Name":"John","Surname":"Diamond","Tel-Number":9876543210,"Age":30,"Subject":["Python","Ruby","Perl"],"comments":[{"user":"Stephin","message":"John Is Best!"},{"user":"Alex","message":"We Love You John!"}],"cr_dc_path":"Blog_Database/Users/John.bin"},{"un_id":"20202590449478829","Name":"Richard","Surname":"Stallman","Tel-Number":9876543000,"Age":40,"Subject":["UNIX","C","Perl"],"comments":[{"user":"Marshal","message":"I Love C Language."},{"user":"Della","message":"C Is A Fast Language"}],"cr_dc_path":"Blog_Database/Users/Richard.bin"}],"status":1}In output status:1 = Sucess0 = ErrorRead Data From Filefrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.read('Users','John')# This Will Read Entire Data In Users Collection And Return Output In Dict.Output:{"data":[{"unid":"20202660459428127","Name":"John","Surname":"Diamond","Tel-Number":9876543210,"Age":30,"Subject":["Python","Ruby","Perl"],"comments":[{"user":"Stephin","message":"John Is Best!"},{"user":"Alex","message":"We Love You John!"}],"cr_dc_path":"Blog_Database/Users/John.bin"}],"status":1}Read Data From File By KeyNamefrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.read('Users','John','Surname')# This Will Read Entire Data In Users Collection And Return Output In Dict.Output:DiamondFILTER DATAFilter Is Most Powerfull Funtion Of This Package. You Can Filter Data According To Your Choice There Are Many Number Of Features Avalible In Filter Function. In Filter Function It Is Possible To Filter Data From Nested Dict Or List Of Dicts As The Way You Want. Filter Makes This Database More Flexible And Powefull.
Some Features Are Shown Below:Operators (Used For Filter Data)ORANDLT (Less Than)GT (Greater Than)GET (Greater And Equal Than)LET (Less And Equal Than)String Filter (User To Filter Data According To String)Forward String FilterBackward String FilterCenter String FilterList Filter (Filter Data From List)Nested Filter (Filter Data From Nested Dict)Simple Filterfrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.filter({"Name":"John"})#This will filter all data where name is John and return output.Output{"data":[{"unid":"20203953408762519","Name":"John","Surname":"Diamond","Tel-Number":9876543210,"Age":30,"Subject":["Python","Ruby","Perl"],"comments":[{"user":"Stephin","message":"John Is Best!"},{"user":"Alex","message":"We Love You John!"}],"cr_dc_path":"Blog_Database/Users/John.bin"}],"status":1}AND Filterfrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.filter({"Name":"John","Surname":"Diamond","Tel-Number":9876543210})This Will Check In Database:if Name is Johnand Surname is Diamondand Tel-Number is 9876543210If All These Conditions are True Then Return Output Otherwise Return Nothing.OR FilterNote:Before Using "OR" Operator You Have To Import It.frompi7dbimportpi7db,ORdb=pi7db("Blog_Database")db.filter({"Name":"John"},OR,{"Surname":"Diamond","Tel-Number":9876543210})This Will Check In Database:if Name is JohnOR Surname is Diamond and Tel-Number is 9876543210If All These Conditions are True Then Return Output Otherwise Return Nothing.GT (GREATER THAN) FilterNote:Befor Using "GT" Operator You Have To Import It.frompi7dbimportpi7db,GTdb=pi7db("Blog_Database")db.filter({"Age":(GT,19)})This Will Check In Database:if Age Greater Than 19If This Condition is True Then Return Output Otherwise Return Nothing.LT (LESS THAN) FilterNote:Before Using "LT" Operator You Have To Import It.frompi7dbimportpi7db,LTdb=pi7db("Blog_Database")db.filter({"Age":(LT,39)})This Will Check In Database:if Age Less Than 39LET (Less and Equal THAN) And GET (Greater and Equal Than) FilterUse Of These Filters Are Similarly ToGTAndLTOperators. For ExampleNote:Before Using Operator You Have To Import It.frompi7dbimportpi7db,GET,LETdb=pi7db("Blog_Database")#LET FILTER (Less and Equal THAN)db.filter({"Age":(LET,40)})#GET FILTER (Greater and Equal Than)db.filter({"Age":(GET,40)})If This Condition is True Then Return Output Otherwise Return Nothing.STRING FiltersThere Are Number Of String Filters are Avalible In pi7db.Forward String FilterBackward String FilterCenter String FilterFORWARD STRING Filter**Is Used In Front Of String You Want To Filter. For Example**String. This Will Check If String After**Mathches With Any Value In DataBase. And It Will Ignore Front Portion Of String AS Shown In Code Below.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.filter({"Name":"**ard"})#This Will Check If Name Ends With "ard" For Example: "Richard".This Will Check In Database:if Name Ends With"ard"BACKWARD STRING Filter**Is Used In End Of String You Want To Filter. For ExampleString**. This Will Check If String Before**Mathches With Any Value In DataBase. And It Will Ignore Back Portion Of String AS Shown In Code Below.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.filter({"Name":"Ric**"})#This Will Check If Name Starts With "Ric" For Example: "Richard".This Will Check In Database:if Name Starts With"Ric"Output{"data":[{"un_id":"20202590449478829","Name":"Richard","Surname":"Stallman","Tel-Number":9876543000,"Age":40,"Subject":["UNIX","C","Perl"],"comments":[{"user":"Marshal","message":"I Love C Language."},{"user":"Della","message":"C Is A Fast Language"}],"cr_dc_path":"Blog_Database/Users/Richard.bin"}],"status":1}CENTER STRING Filter**Is Used In Front And Back Of String You Want To Filter. For Example**String**. This Will Check If String Mathches in Center With Any Value In DataBase.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.filter({"Name":"**ich**"})#This Will Check If "ich" is in Name. For Example: "Richard".This Will Check In Database:if"ich"In NameLIST FilterIt Is Also Possible To Filter Data From List. As Shown Below:frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.filter({"Subject":["UNIX","Perl"]})#This Will Check If "UNIX" And "Perl" Exist In Subject.This Will Check In Database:if"UNIX"Exist In List Of Subjectif"Perl"Exist In List Of SubjectIf Both The Conditions Are True Then It Will Return Output.NESTED FilterIt Is Also Possible To Filter Data From List. As Shown Below:frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.filter({"comments":[{"user":"Marshal"}]})#This Will Check If user "Marshal" exist in "comments".This Will Check In Database:if user"Marshal"Exist In List Of commentsAnother Example Of NESTED FilterIn Nested Filters You Can Use Any Filter LikeGT,GET,LETetc. In This Example We Will Cover Nested Filter With String Filter.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.filter({"comments":[{"message":"**Love**"}]})#This Will Check If "Love" Word In message.This Will Check In Database:if message Is About"Love"If The Conditions Is True Then It Will Return Output.Example: Use Of Multiple Filters TogetherIn This Example We Will Use "OR", "And","GT" and String Filters.
First ImportGTandORFrom pi7dbfrompi7dbimportpi7db,OR,GTdb=pi7db("Blog_Database")db.filter("Subject":["C"],OR,{"Surname":"Stall**","Tel-Number":9876543210},OR,{"Age":(GT,18)})This Will Check Following Conditions:IfCIn Subject List.OR Surname Is "Diamond" AND TEl-Number Is 9876543210.OR Age Is Greater Than 18.If Only One Condition True Then It Will Return Output. Because We UseOROperator In Filter.DELETE DATAVarious Options Are Avalible For Delete Data.Delete A Key From File. For Example (delete Surname From John File In Users Collection)Delete A Specific File. For Example (Delete John File From Users Collection)Delete Entire Collection. For Example (Delete Users)Delete A Key From Filefrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.trash('Users','John','Surname')This Will Delete Surname From John File In Users Collection.Output:{'success': 'Deleted! Surname Deleted From John!', 'status': 1}Delete A File From Collectionfrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.trash('Users','John')This Will Delete John File From Users Collection.Output:{'success': 'Deleted! Johan!', 'status': 1}Delete A Collectionfrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.trash('Users')This Will Delete Entire Collection.Output:{'success': 'Deleted! Collection Users Deleted Successfully!', 'status': 1}DELETE BY FILTERPi7db provide number of features. It is also possible to delete data by filter.
Two argumentswhereanddropkeyis used for delete data.Delete Data With Where Argumentfrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.trash(where={"Name":"John"})#This will delete all The files where Name is John.# For Delete Only From Users Collection Use db.trash("Users",where={"Name":"John"})Output:TrueThis will delete all The files where Name is John. This is one from advance features of pi7db.Delete Data With Where Argument Using Multiple FiltersIt is Also Possible To Delete Data With Multiple Filters As Shown Belowfrompi7dbimportpi7db,OR,GTdb=pi7db("Blog_Database")db.trash("Users",where=[{"Name":"**jo**","Surname":"Diamond"},OR,{"Age":(GT,50)}])This Will Check Following Conditions:IfjoIn Name AND Surname Is "Diamond"OR Age Is Greater Than 50.
If These Two Conditions Are True Then It Will Be Removed From Database.Delete Data With "Where" And "Dropkey" ArgumentDelete a key with filterfrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.trash('Users',where={"Name":"John"},dropkey='Tel-Number')#This will delete all Tel-Number Keys From All files where Name is John.Output:TrueThis will delete all Tel-Number Keys From All files where Name is John.
You Can Use Number Of Filters To Delete Data As Shown In Filter Data Funtion Below.NOTE:NESTED KEY DELETE IS NOT SUPPORTED IN CURRENT VERSION OF pi7db. (comming soon).UPDATE DataIf You Want To Update Data Then Use Update Function. In Update Function If is Also Possible To Update By Filter.UPDATE Data In Specific FileIt Takes Three Arguments Collection_Name , File_Name And Dict Data
For Example:db.update("Collection_name","File_Name",{"Data":"Data"})As Shown Below:frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.update('Users','John',{'Name':"Johnson","Surname":"Retchie","Father_name":"Sam"})Output:{'success': 'Success! Value Update in John!', 'status': 1}UPDATE Using Where ArgumentIt Takes Two Or Three Arguments Dict Data
For Example:db.update("Collection_name",{"Data":"Data"}, where="Name":"John")#With Collection Namedb.update({"Data":"Data"}, where="Name":"John")#Without Collection NameCollection Name Is Recomended. It Will Increase Speed Of Update.As Shown Below:frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.update({"Father_name":"Sam Diamond"},where={"Name":"John"})#For Pass Multiple Argument In Where Use list#for Example where = [ {"Name":"John"} ,OR, {"Surname":"**mnd"}]It Will Update Father_name Key Where Name Is John. If Father_name Key Not Exist Then It Will Add Automatically where Name Is John.Output:TrueUPDATE Data In All FileSometime We Want To Update Data In All Files Present In Database.Then We Can UseString Filter.
As Shown Below:frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.update({'Added_Date':"2020-05-21"},where={"Name":"**"})This Will Update Data In All Files Where Name Key Is Present.Output:{'success': 'Success! Value Update in John!', 'status': 1}UPDATE Data With INCREMENT FunctionINCREMENTFunction Provide Help In Update Data With Less Efforts For Example If You Want To Update Age of John then{"Age":increment(2)}. This Will Increment John's Age By 2 For example 40 + 2 = 42.
Increment Function Take Single Int Argement For example 9. or any other Integer.
Example:Note : You Have To Import increment Function From pi7dbfrompi7dbimportpi7db,incrementdb=pi7db("Blog_Database")#Using Where Argumentdb.update({"Age":increment(5)},where={"Name":"John"})#For Pass Multiple Argument In Where Use list#for Example (Using OR operator):- where = [ {"Name":"John"} ,OR, {"Surname":"**mnd"}]#OR IN A SPECIFIC FILEdb.update("Users","John",{"Age":increment(5)})It Will Increment John's Age By 5OutputTrueUPDATE Data With decrement FunctionDECREMENTFunction Is Opposite To Increment Function It Help In Decrement In Value. For Example:-{"Age":decrement(3)}. This Will Decrement John's Age By 3 For example 40 - 3 = 39.
Decrement Function Take Single Int Argement For example 5. or any other Integer.
Example:Note : You Have To Import decrement Function From pi7dbfrompi7dbimportpi7db,decrementdb=pi7db("Blog_Database")# UPDATE IN A SPECIFIC FILEdb.update("Users","John",{"Age":decrement(2)})#OR USING WHERE ARGUMENTdb.update({"Age":decrement(2)},where={"Name":"John"})#For Pass Multiple Argument In Where Use list#for Example (Using OR operator):- where = [ {"Name":"John"} ,OR, {"Surname":"**mnd"}]It Will Decrement John's Age By 2OutputTrueNOTE:NESTED UPDATE IS NOT SUPPORTED IN CURRENT VERSION OF pi7db. (comming soon).SORT DataFor Get Sorted Data UsesortFunction. Sort Function Take one Argument Of Data Typeset. For Example:db.sort({"Name"})By Default Data Is In Ascending Order For Set Manually:For Sort Data In Ascending Order Usedb.sort({"Name"}, order=asc)For Sort Data In Descending Order Usedb.sort({"Name"}, order=dsc)NOTE:ImportascanddscFor Use Order.frompi7dbimportpi7db,asc,dscdb=pi7db("Blog_Database")# By Default Order Is Ascendingdb.update({"Name"})#For Descending Order Usedb.update({"Name"},order=dsc)SORT Data In Nested DictNote:Data Sortion Work Only With Nested Dict Not With list.
For Example We Have Data In Database In This Order:{"User":{"Name":"Richard","Subject":{"Science":"Chemistry","Computer":"CSE"},"Teacher":{"Science":"Fury","Computer":"James"},}}How To Sort This Nested Data ?For Sort By Name In This Data We Will Pass Argument Likedb.sort({"User","Name"})For Sort By Specific Subject In This Data We Will Pass Argument Likedb.sort({"User","Subject","Science"})For Sort By Specific Teacher Name We Will Pass Argument Likedb.sort({"User","Teacher","Science"})You Can Also Pass Collection_Name Before Dict. Collection Name Is Recommended.
For Sort In Specific Collection We Will Pass Argument Of Collection Name For Example:db.sort("Users",{"User","Subject","Science"})SORT Filtered DataThere Is A Functiondb.sortdictIn pi7db Which Is Used To Sort Filtered Data. In Other Handdb.sortFunction Is Used To Sort All Data.for Example:frompi7dbimportpi7db,dscdb=pi7db("Blog_Database")filtered_data=db.filter({"Name":"**"})db.sortdict(filtered_data,{"Surname"},order=dsc)output{"data":[{"un_id":"20202590449478829","Name":"Richard","Surname":"Stallman","Tel-Number":9876543000,"Age":40,"Subject":["UNIX","C","Perl"],"comments":[{"user":"Marshal","message":"I Love C Language."},{"user":"Della","message":"C Is A Fast Language"}],"cr_dc_path":"Blog_Database/Users/Richard.bin"},{"un_id":"20202660459428127","Name":"John","Surname":"Diamond","Tel-Number":9876543210,"Age":30,"Subject":["Python","Ruby","Perl"],"comments":[{"user":"Stephin","message":"John Is Best!"},{"user":"Alex","message":"We Love You John!"}],"cr_dc_path":"Blog_Database/Users/John.bin"}],"status":1}Some Other Advance Arguments:There Are Some Other Arguments Help In Speed Of Data Read, Ignore Files, Read Data From Specific Part.
List Of Arguments:FROM(Starting Point File Number)TO(End Point Of File Number)FIRST(Read Number Of Files From Start)LAST(Read Number Of Files From Last)IGNORE(Ignore Specific File)FROMArgumentSuppose You Have 1000 Number Of Files In Database. But You Want To Ignore First 10 Files And Read Other 990 Files ThenFROMArgument Can Help You.
For Example:NOTE:WriteFROMInCapital Wordsfrompi7dbimportpi7dbdb=pi7db("Blog_Database")db.read(FROM=10)TOArgumentSuppose You Have 1000 Files In Database. But You Want To Read From 10 To 50 Files. For Example:NOTE:WriteTOInCapital Words.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.read(FROM=10,TO=50)FIRSTArgumentSuppose You Have 1000 Files In Database. But You Want To Read Only First 10 Files.For Example:NOTE:WriteFIRSTInCapital Words.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.read(FIRST=10)This Will Read And Return First 10 Files.LASTArgumentSuppose You Have 1000 Files In Database. But You Want To Read Only Last 10 Files.For Example:NOTE:WriteLASTInCapital Words.frompi7dbimportpi7dbdb=pi7db("Blog_Database")db.read(LAST=10)This Will Read And Return LAST 10 Files.IGNOREArgumentSuppose You Want To Ignore A File During Read, Sort Or Filter Time Then Use Ignore Argumen.For Example:NOTE:WriteIGNOREInCapital Words.frompi7dbimportpi7dbdb=pi7db("Blog_Database")#IGNORE SINGLE FILEdb.read(IGNORE="John")#IGNORE MULTIPLE FILES USING LISTdb.read(IGNORE=["John","Richard"])IGNORE = "John"# This Will Ignore Only John FileIGNORE = ["John","Richard"]# This Will Ignore All Files In ListYou Can Use These Arguments Withsort,filterandreadFunctionsFor Example:frompi7dbimportpi7dbdb=pi7db("Blog_Database")#With READdb.read(IGNORE="John")#With SORTdb.sort({"Name"},FROM=1,TO=10)#With FILTERdb.filter({"Name":"J**"},FIRST=50)MORE FEATURES COMMING SOON, THANKYOU.