### Install pysonDB via pip Source: https://pysondb.github.io/pysonDB Standard installation command for the latest version of pysonDB. ```bash pip install pysondb ``` -------------------------------- ### Install pysonDB Source: https://pysondb.github.io/pysonDB Install the specific stable version of pysonDB via pip. ```bash pip install pysondb==1.6.1 ``` -------------------------------- ### Retrieve Data with get() Source: https://pysondb.github.io/pysonDB/get Fetch a specific number of records from the database. ```python >> from pysondb import db >> a=db.getDb("path.json") >> a.get() >> [{"name":"pysondb","type":"DB"}] >> a.get(1) >> [{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"}] ``` -------------------------------- ### Example JSON Database File Source: https://pysondb.github.io/pysonDB/delete The structure of the JSON file used for database operations. ```json {"data": [{"name": "PysonDB", "type": "DB", "score": "10", "id": 5161221802}, {"name": "Pyson-CLI", "type": "CLI", "score": "10", "id": 2242313690}, {"name": "Tiny", "type": "DB", "score": "9", "id": 6991190264}, {"name": "QWERTY", "type": "DB", "score": "5", "id": 9416036202}]} ``` -------------------------------- ### get(n, objectify=False) Source: https://pysondb.github.io/pysonDB/get Retrieves a specified number of records from the database. By default, it returns one record. ```APIDOC ## get(n, objectify=False) ### Description Retrieves data from the database. If 'n' is provided, it returns that many records; otherwise, it returns one record by default. ### Parameters - **n** (int) - Optional - The number of records to retrieve. - **objectify** (bool) - Optional - Whether to return objects instead of dictionaries. ``` -------------------------------- ### Define Database JSON Structure Source: https://pysondb.github.io/pysonDB/get Example JSON structure for a PysonDB database file. ```json {"data":[{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}]} ``` -------------------------------- ### Create JSON Database via CLI Source: https://pysondb.github.io/pysonDB/cli Initializes a new JSON database file in the current directory. ```cmd pysondb create [name] ``` ```cmd Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Admin\Desktop\pysondb>pysondb create filedb.json Succesfull created filedb.json in the directory. ``` -------------------------------- ### Initialize Database in Python Source: https://pysondb.github.io/pysonDB Create or connect to a JSON database file within a Python script. ```python from pysondb import db a=db.getDb("db.json") ``` -------------------------------- ### Create Database via CLI Source: https://pysondb.github.io/pysonDB Create a new JSON database file using the pysonDB command line interface. ```bash pysondb create database_name.json ``` -------------------------------- ### Basic CRUD Operations Source: https://pysondb.github.io/pysonDB Initialize a database, add multiple records, and retrieve all data. ```python >> from pysondb import db >> a=db.getDb("path/to/json.json") >> a.addMany([{"name":"pysondb","type":"DB"},{"name":"pysondb-cli","type":"CLI"}]) >> a.getAll() >> [{"name":"pysondb","type":"DB"},{"name":"pysondb-cli","type":"CLI"}] ``` -------------------------------- ### Retrieve All Data with getAll() Source: https://pysondb.github.io/pysonDB/get Fetch every record stored in the database. ```python >> from pysondb import db >> a=db.getDb("path.json") >> a.getAll() >> [{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}] ``` -------------------------------- ### Delete Record by ID Source: https://pysondb.github.io/pysonDB/delete Demonstrates initializing the database and removing a specific record using its ID. ```python >> from pysondb import db >> a=db.getDb("file.json") >> a.deleteById("6991190264") >> # The JSON data with ID "6991190264" is deleted.Lets verify. >> a.getAll() >> [{"name": "PysonDB", "type": "DB", "score": "10", "id": 5161221802}, {"name": "Pyson-CLI", "type": "CLI", "score": "10", "id": 2242313690}, {"name": "QWERTY", "type": "DB", "score": "5", "id": 9416036202}] ``` -------------------------------- ### getAll(objectify=False) Source: https://pysondb.github.io/pysonDB/get Retrieves all records stored in the database. ```APIDOC ## getAll(objectify=False) ### Description Returns all data currently stored in the database. ### Parameters - **objectify** (bool) - Optional - Whether to return objects instead of dictionaries. ``` -------------------------------- ### Display JSON Data via CLI Source: https://pysondb.github.io/pysonDB/cli Outputs the contents of a JSON database file in a formatted table. ```json { "data": [ { "name": "PysonDB", "type": "DB", "score": "10", "id": 5161221802 }, { "name": "Pyson-CLI", "type": "CLI", "score": "10", "id": 2242313690 }, { "name": "TinyDb", "type": "DB", "score": "9", "id": 6991190264 }, { "name": "QWERTY", "type": "DB", "score": "5", "id": 9416036202 } ] } ``` ```cmd Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Admin\Desktop\pysondb>pysondb display filedb.json +-----------+------+-------+------------+ | name | type | score | id | +-----------+------+-------+------------+ | PysonDB | DB | 10 | 5161221802 | +-----------+------+-------+------------+ | Pyson-CLI | CLI | 10 | 2242313690 | +-----------+------+-------+------------+ | Tiny | DB | 9 | 6991190264 | +-----------+------+-------+------------+ | QWERTY | DB | 5 | 9416036202 | +-----------+------+-------+------------+ ``` -------------------------------- ### Perform regex search on database Source: https://pysondb.github.io/pysonDB/re_search Demonstrates using reSearch to find records matching specific regex patterns. ```python >> from pysondb import db >> a=db.getDb("file.json") >> print(a.reSearch("name", r"\w{3}\d{2}@c-py")) >> [{'name': 'def23@c-py', 'type': 'TUI', 'id': 200151702869331613}] >> print(a.reSearch("name", "stuff\(py")) [{'name': 'stuff(py', 'type': 'GUI', 'id': 115618300909661724}] ``` -------------------------------- ### Merge JSON Databases via CLI Source: https://pysondb.github.io/pysonDB/cli Combines data from two JSON files into a primary or new target file. ```cmd pysondb merge primary.json seconday.json pysondb merge primary.json seconday.json new.json ``` -------------------------------- ### update(query, what_to_update) Source: https://pysondb.github.io/pysonDB/update Updates all records that match the provided query criteria. ```APIDOC ## update(query, what_to_update) ### Description Updates all records in the database that match the specified query criteria. ### Parameters - **query** (JSON) - Required - The criteria used to find records to update. - **what_to_update** (JSON) - Required - The key-value pairs to update in the matching records. ### Request Example ```python a.update({"name": "Pyson-CLI"}, {"name": "PysonCLI"}) ``` ``` -------------------------------- ### Find Record by ID with find() Source: https://pysondb.github.io/pysonDB/get Retrieve a single record using its integer ID. ```python >> from pysondb import db >> a=db.getDb("path.json") >> a.getAll() >> [{"name":"pysondb","type":"DB",id:1234},{"name":"py_cli","type":"CLI",id:5678},{"name":"py_cli2","type":"CLI",id:9101112}] >> a.find(1234) >> {"name":"pysondb","type":"DB",id:1234} ``` -------------------------------- ### Add multiple records with addMany() Source: https://pysondb.github.io/pysonDB/add Inserts a list of dictionaries into the database. If one item in the list fails schema validation, it is skipped while valid items are still processed. ```python >> from pysondb import db >> a=db.getDb("pathtojson.json") >> a.addMany([{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"}]) >> # Both data is added in the database. >> a.add([{"namme":"pyson","type":"DB"},{"name":"py_cli2","type":"CLI"}]) The first data wont be added as the key "name" is mispelled as "namme" But the second data will be added. >>a.getAll() {"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}] ``` -------------------------------- ### Add single record with add() Source: https://pysondb.github.io/pysonDB/add Inserts a single dictionary into the database. The method returns a unique ID for the added record. ```python >> from pysondb import db >> #from pysondb import getDb >> #a=getDb("test.json") >> a=db.getDb("pathtojson.json") >> a.add({"name":"pysondb","type":"DB"}) >> # returns 1929323232 which is a ID assigned to the above data. >> a.add({"namme":"pyson","type":"DB"}) >> # The data wont be added as the key "name" is mispelled as "namme" ``` -------------------------------- ### Convert CSV to JSON via CLI Source: https://pysondb.github.io/pysonDB/cli Converts a CSV file into a JSON database file. ```cmd pysondb convert example.csv example.json ``` ```cmd Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Admin\Desktop\pysondb>pysondb convert file.csv file.json Reading data from file.csv Writing data into file.json Conversion Succesfull ``` -------------------------------- ### Update Data by Query Source: https://pysondb.github.io/pysonDB/update Updates all records matching the provided query criteria. ```python >> from pysondb import db >> a=db.getDb("file.json") >> a.update({"name":"Pyson-CLI"},{"name":"PysonCLI"}) >> a.getBy({"name":"PysonCLI"}) >> # In the file.json the data is updated for all data where name=Pyson-CLI >> # We can verify it below. >> [{"name": "PysonCLI", "type": "CLI", "score": "10", "id": 2242313690}] ``` -------------------------------- ### addMany(list) Source: https://pysondb.github.io/pysonDB/add Adds multiple dictionary records to the database. Each record is validated against the schema, and only valid records are inserted. ```APIDOC ## addMany(list) ### Description Adds a list of dictionary records to the database. The method checks each item for schema regularity; valid items are added while irregular items are rejected. ### Parameters - **list** (list of dicts) - Required - A list of dictionaries to be added to the database. ### Example ```python from pysondb import db a = db.getDb("pathtojson.json") a.addMany([{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"}]) ``` ``` -------------------------------- ### Convert JSON to CSV via CLI Source: https://pysondb.github.io/pysonDB/cli Converts a JSON database file into a CSV format, optionally specifying a target filename. ```cmd pysondb converttocsv example/registry.json pysondb converttocsv example/registry.json target.csv ``` ```bash Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Admin\Desktop\pysondb>pysondb converttocsv example/registry.json File converted and saving to converted.csv C:\Users\Admin\Desktop\pysondb>pysondb converttocsv example/registry.json filenew.csv File converted and saving to filenew.csv ``` ```bash gitpod/workspace/pysonDB$ pysondb converttocsv example/registry.json File converted and saving to converted.csv gitpod /workspace/pysonDB$ pysondb converttocsv example/registry.json filenew.csv File converted and saving to filenew.csv ``` -------------------------------- ### add(json) Source: https://pysondb.github.io/pysonDB/add Adds a single dictionary record to the database. The method validates the input against the existing database schema and returns the assigned ID if successful. ```APIDOC ## add(json) ### Description Adds a single record to the database. The schema of the first added data defines the schema for the entire database; irregular data will be rejected. ### Parameters - **json** (dict) - Required - A dictionary representing the data to be added. ### Returns - **id** (int) - The unique ID assigned to the added data. ### Example ```python from pysondb import db a = db.getDb("pathtojson.json") a.add({"name":"pysondb","type":"DB"}) ``` ``` -------------------------------- ### getBy(query, objectify=False) Source: https://pysondb.github.io/pysonDB/get Retrieves records that match the provided JSON query. ```APIDOC ## getBy(query, objectify=False) ### Description Filters and returns records that match the criteria specified in the query object. ### Parameters - **query** (dict) - Required - A JSON object representing the filter criteria. - **objectify** (bool) - Optional - Whether to return objects instead of dictionaries. ``` -------------------------------- ### reSearch(key, regex, objectify=False) Source: https://pysondb.github.io/pysonDB/re_search Searches for objects in the database where the value of the specified key matches the provided regular expression. ```APIDOC ## reSearch(key, regex, objectify=False) ### Description Searches for objects where the value of the key follows the given regex pattern. ### Parameters - **key** (string) - Required - The key in the database objects to search against. - **regex** (string) - Required - The regular expression pattern to match. - **objectify** (boolean) - Optional - If set to True, returns objects as objects instead of dictionaries. ### Usage Example ```python from pysondb import db a = db.getDb("file.json") # Search for names matching a specific regex pattern results = a.reSearch("name", r"\w{3}\d{2}@c-py") print(results) ``` ``` -------------------------------- ### Query Data with getBy() Source: https://pysondb.github.io/pysonDB/get Filter records based on a provided JSON query object. ```python >> from pysondb import db >> a=db.getDb("path.json") >> a.getBy({"type":"CLI"}) >> [{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}] >> a.getBy({"name":"py_cli"}) >> [{"name":"py_cli","type":"CLI"}] ``` -------------------------------- ### Delete JSON Database via CLI Source: https://pysondb.github.io/pysonDB/cli Removes an existing JSON database file after user confirmation. ```cmd pysondb delete [name] ``` ```cmd Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Admin\Desktop\pysondb>pysondb delete file2.json Do you want to remove the json file..(y/n)y File deleted. C:\Users\Admin\Desktop\pysondb>pysondb delete filedb.json Do you want to remove the json file..(y/n)n Action terminated ``` -------------------------------- ### updateById(ID, what_to_update) Source: https://pysondb.github.io/pysonDB/update Updates a specific data record identified by its unique ID. ```APIDOC ## updateById(ID, what_to_update) ### Description Updates a record in the database that matches the provided unique ID. ### Parameters - **ID** (Integer) - Required - The unique ID of the record to update. - **what_to_update** (JSON) - Required - The key-value pairs to update in the record. ### Request Example ```python a.updateById("9416036202", {"name": "Qwerty12"}) ``` ``` -------------------------------- ### find(id) Source: https://pysondb.github.io/pysonDB/get Retrieves a single record by its unique ID. ```APIDOC ## find(id) ### Description Finds and returns a single record matching the provided integer ID. ### Parameters - **id** (int) - Required - The unique identifier of the record to retrieve. ``` -------------------------------- ### Update Data by ID Source: https://pysondb.github.io/pysonDB/update Updates a specific record identified by its unique integer ID. ```python >> from pysondb import db >> a=db.getDb("file.json") >> a.updateById("9416036202",{"name":"Qwerty12"}) >> a.getBy({"name":"Qwerty12"}) >> #In the file.json the data is updated. >> #We can verify it below. >> [{"name": "Qwerty12", "type": "DB", "score": "5", "id": 9416036202}] ``` -------------------------------- ### Update Array Field Source: https://pysondb.github.io/pysonDB/update Appends a value to an existing array field, optionally enforcing a maximum capacity. ```python >> from pysondb import db >> a=db.getDb("file.json") >> a.getBy({"name":"ArrayEntry"}) >> [{"name":"ArrayEntry", "epic_array":[0,1,2]}] >> a.updateArray({"name":"ArrayEntry"},"epic_array",3) >> a.getBy({"name":"ArrayEntry"}) >> # In the file.json the data is updated for all data where name=Pyson-CLI >> # We can verify it below. >> [{"name": "ArrayEntry", "epic_array":[0,1,2,3]}] >> a.updateArray({"name":"ArrayEntry"}, "epic_array", 4, 4) >> a.getBy({"name":"ArrayEntry"}) >> [{"name": "ArrayEntry", "epic_array":[1,2,3,4]} ``` -------------------------------- ### updateArray(query, prop_to_update, value_to_append, max_array_capacity) Source: https://pysondb.github.io/pysonDB/update Appends a value to an existing array property within records matching the query. ```APIDOC ## updateArray(query, prop_to_update, value_to_append, max_array_capacity) ### Description Appends a value to an existing array property in records matching the query. If max_array_capacity is provided and the array length exceeds it, the first element is removed. ### Parameters - **query** (JSON) - Required - The criteria used to find records. - **prop_to_update** (String) - Required - The name of the array property to update. - **value_to_append** (Any) - Required - The value to add to the array. - **max_array_capacity** (Float) - Optional - The maximum size of the array before elements are popped. ### Request Example ```python a.updateArray({"name": "ArrayEntry"}, "epic_array", 3) ``` ``` -------------------------------- ### deleteById(ID) Source: https://pysondb.github.io/pysonDB/delete Deletes the JSON data entry associated with the provided ID from the database. ```APIDOC ## deleteById(ID) ### Description Deletes the JSON data with the given ID from the database. ### Method `deleteById(ID)` ### Parameters - **ID** (string/int) - Required - The unique identifier of the record to be deleted. ### Usage Example ```python from pysondb import db a = db.getDb("file.json") a.deleteById("6991190264") ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.