# This is RTS_Simpledatabase
a little module for Python to create databases.
### Update notes
- added Events
you can now add an @DatabaseEvent.<on_create, on_update, on_delete>(databasename) on a function and get the record being proccessed by one of these events
# How to use
For the "How to use" lets get some sample data:
| fieldname | userid | displayname | balance | joined | note
|----------------|---------------|---------------|----------|------------|-------
| example value | 9783836285131 | randomtimetv | 6.50 | 31.03.2024 |
| format | str | str | float | str | str
| state | unique | modular | modular | locked | loose
# Initiate the database
```py
from RTSDataBase import DB
Database = DB('users') #creates or reads the users.rtsdb
# does not overwrite headers if there are headers already present
# all 3 (fields, format, states) are required and have to have the same length (in this case 5 entries)
Database.setHeaders({
"fields": ["userid", "displayname", "balance", "joined", "note" ],
"format": ["str", "str" , "float" , "str" , "str" ],
"states": ["unique", "modular" , "modular", "locked", "loose"]
})
```
# Create a record
```py
Database.create({
"userid": "9783836285131",
"displayname": "randomtimetv",
"balance": 6.50,
"joined": "31.03.2024" # dd.mm.yyy
})
```
Note: As you can see the .create() does not set the "note" field, this is because of the state being "loose", more to that in the **States and Formats** section.
# Read a record
```py
# selectorFieldName is idealy a unique, locked or index field like in this case "userid"
# selectorFieldValue is the known full value of selectorFieldName in this case "9783836285131"
# specificFieldNameToRead can be specified to obtain the value of a single field of the specified record
# if no field is given it returns the full record
# Syntax: Database.read({<selectorFieldName>:<selectorFieldValue>, [field=specificFieldNameToRead]})
Database.read({"userid":"9783836285131"})
# Returns: {"userid": "9783836285131","displayname": "randomtimetv","balance": 6.50,"joined": "31.03.2024"}
Database.read({"userid":"9783836285131"},"displayname")
# Returns: "randomtimetv"
```
Does not return errors.
# Update a record
```py
# selectorFieldName is idealy a unique, locked or index field like in this case "userid"
# selectorFieldValue is the known full value of selectorFieldName in this case "9783836285131"
# targetField is the field you want to update, let's say "balance" needs to be updated
# newValue is the, you might have guessed it, new Value you want to set, lets say: 19.0
# Syntax: Database.update({<selectorFieldName>:<selectorFieldValue>}, <targetField>, <newValue>)
Database.update({"userid": "9783836285131"}, "balance", 19.0)
```
<br/>If something went wrong, like you tried to set the wrong type, you get:
```
InvalidType: "balance" does not match typerule "float" in: {'balance': '19'}
```
More to these errors in **States and Formats**
<br/><br/>Trying to update locked fields results in:
```
LockedField: "joined" can not be updated.
```
# Search records
```py
# Note: find is best used if you have a UserInterface with a search field
# query can be the full value or just a section of the actual value
# fieldname is by default __any and searches ALL fields except __id if they atleast partially contain query, specify to limit the search to a single field
# case_sensitive is by default True
# allow_typo is by default False
# Returns a list of all matching records
# Syntax: Database.find(<query type:string>, [fieldname=<fieldName>], [case_sensitive=<True|False>], [allow_typo=<True|False>])
Database.find("Random", fieldname="displayname")
# Returns: [] because "Random" is not contained in "displayname" ("random" would be contained)
Database.find("Random", fieldname="displayname", case_sensitive=False)
# Returns: [{"userid": "9783836285131","displayname": "randomtimetv","balance": 6.50,"joined": "31.03.2024"}]
# because this time case_sensitive is turned off
```
Does not throw errors.
# Test if a record exists
```py
# selectorFieldName is idealy a unique, locked or index field like in this case "userid"
# selectorFieldValue is the known full value of selectorFieldName in this case "9783836285131"
# Returns a boolean
# Syntax: Database.exists({<selectorFieldName>:<selectorFieldValue>})
Database.exists({"userid","9783836285131"})
# Returns: True
```
Does not throw errors.
# Delete a record
```py
# __id is the hidden and unique id of the record
# Syntax: Database.delete(<__id>)
Database.delete(1)
# Deletes the record with the __id 1
Database.delete(Database.read({"userid":"9783836285131"}, "__id"))
# Deletes the record
```
Does not throw errors.
# Mass data output (dumping)
```py
Database.header
# Returns a list of all present headers
Database.dump_header()
# Returns the full header segment with the fieldnames, types and states
# suported formats: csv, plain
Database.formated_dump(format)
# plain is default, it returns the data as it is saved
# csv, returns the database formated in csv seperated by "|"
```
# States and Formats
The State of a field can have following values:
```
unique = field can be changed but must contain a unique value among all records, only applies to the same field
locked = field needs to be set in .create(), can not be changed afterwards
modular = field can be updated without restrictions
index = locked and unique
loose = field can remain unset or undefined and is modular
```
All fieldtypes, except "loose", need to be set by their rules in .create() otherwise it will throw an error like
```
MissingField: "displayname" is missing in: {"userid": "9783836285131","balance": 6.50,"joined": "31.03.2024"}
```
<br/>There are a few supported formats as of now:
```
__any = ignores type notations, aka can have any type. Is not recommended
str, list, dict, bool, float, int = only accepts its propper type as value
nostr, nolist, nodict, nobool, nofloat, noint = accepts it's propper type or None as value
```
If there is a Type mismatch you get:
```
InvalidType: "userid" does not match typerule "str" in: {"userid":9783836285131,"displayname": "randomtimetv","balance": 6.50,"joined": "31.03.2024"}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/RandomTimeLP/RTS_DataBase/",
"name": "RTSDB",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "database",
"author": "RandomTimeTV",
"author_email": "dergepanzerte1@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/29/aa/a18f03545b01f2c5d7282fd112e83cae0b6580810b207972e0e2fa376f23/rtsdb-3.3.tar.gz",
"platform": null,
"description": "# This is RTS_Simpledatabase\r\n\r\na little module for Python to create databases.\r\n\r\n### Update notes\r\n\r\n- added Events\r\nyou can now add an @DatabaseEvent.<on_create, on_update, on_delete>(databasename) on a function and get the record being proccessed by one of these events\r\n\r\n# How to use\r\n\r\nFor the \"How to use\" lets get some sample data:\r\n\r\n| fieldname | userid | displayname | balance | joined | note \r\n|----------------|---------------|---------------|----------|------------|-------\r\n| example value | 9783836285131 | randomtimetv | 6.50 | 31.03.2024 | \r\n| format | str | str | float | str | str \r\n| state | unique | modular | modular | locked | loose \r\n\r\n# Initiate the database\r\n```py\r\nfrom RTSDataBase import DB\r\nDatabase = DB('users') #creates or reads the users.rtsdb\r\n\r\n# does not overwrite headers if there are headers already present\r\n# all 3 (fields, format, states) are required and have to have the same length (in this case 5 entries)\r\nDatabase.setHeaders({ \r\n \"fields\": [\"userid\", \"displayname\", \"balance\", \"joined\", \"note\" ], \r\n \"format\": [\"str\", \"str\" , \"float\" , \"str\" , \"str\" ], \r\n \"states\": [\"unique\", \"modular\" , \"modular\", \"locked\", \"loose\"]\r\n})\r\n```\r\n\r\n\r\n\r\n# Create a record\r\n```py\r\nDatabase.create({\r\n \"userid\": \"9783836285131\",\r\n \"displayname\": \"randomtimetv\",\r\n \"balance\": 6.50,\r\n \"joined\": \"31.03.2024\" # dd.mm.yyy\r\n})\r\n```\r\nNote: As you can see the .create() does not set the \"note\" field, this is because of the state being \"loose\", more to that in the **States and Formats** section.\r\n\r\n# Read a record\r\n```py\r\n# selectorFieldName is idealy a unique, locked or index field like in this case \"userid\"\r\n# selectorFieldValue is the known full value of selectorFieldName in this case \"9783836285131\"\r\n# specificFieldNameToRead can be specified to obtain the value of a single field of the specified record\r\n# if no field is given it returns the full record\r\n\r\n# Syntax: Database.read({<selectorFieldName>:<selectorFieldValue>, [field=specificFieldNameToRead]})\r\n\r\nDatabase.read({\"userid\":\"9783836285131\"})\r\n# Returns: {\"userid\": \"9783836285131\",\"displayname\": \"randomtimetv\",\"balance\": 6.50,\"joined\": \"31.03.2024\"}\r\n\r\nDatabase.read({\"userid\":\"9783836285131\"},\"displayname\")\r\n# Returns: \"randomtimetv\"\r\n\r\n```\r\nDoes not return errors.\r\n\r\n# Update a record\r\n```py\r\n# selectorFieldName is idealy a unique, locked or index field like in this case \"userid\"\r\n# selectorFieldValue is the known full value of selectorFieldName in this case \"9783836285131\"\r\n# targetField is the field you want to update, let's say \"balance\" needs to be updated\r\n# newValue is the, you might have guessed it, new Value you want to set, lets say: 19.0\r\n\r\n# Syntax: Database.update({<selectorFieldName>:<selectorFieldValue>}, <targetField>, <newValue>)\r\n\r\nDatabase.update({\"userid\": \"9783836285131\"}, \"balance\", 19.0)\r\n```\r\n<br/>If something went wrong, like you tried to set the wrong type, you get:\r\n```\r\n InvalidType: \"balance\" does not match typerule \"float\" in: {'balance': '19'}\r\n```\r\nMore to these errors in **States and Formats**\r\n<br/><br/>Trying to update locked fields results in:\r\n```\r\n LockedField: \"joined\" can not be updated.\r\n```\r\n\r\n\r\n# Search records\r\n```py\r\n# Note: find is best used if you have a UserInterface with a search field\r\n# query can be the full value or just a section of the actual value\r\n# fieldname is by default __any and searches ALL fields except __id if they atleast partially contain query, specify to limit the search to a single field\r\n# case_sensitive is by default True\r\n# allow_typo is by default False\r\n\r\n# Returns a list of all matching records\r\n\r\n# Syntax: Database.find(<query type:string>, [fieldname=<fieldName>], [case_sensitive=<True|False>], [allow_typo=<True|False>])\r\n\r\nDatabase.find(\"Random\", fieldname=\"displayname\")\r\n# Returns: [] because \"Random\" is not contained in \"displayname\" (\"random\" would be contained)\r\n\r\nDatabase.find(\"Random\", fieldname=\"displayname\", case_sensitive=False)\r\n# Returns: [{\"userid\": \"9783836285131\",\"displayname\": \"randomtimetv\",\"balance\": 6.50,\"joined\": \"31.03.2024\"}]\r\n# because this time case_sensitive is turned off\r\n```\r\nDoes not throw errors.\r\n\r\n\r\n# Test if a record exists\r\n```py\r\n# selectorFieldName is idealy a unique, locked or index field like in this case \"userid\"\r\n# selectorFieldValue is the known full value of selectorFieldName in this case \"9783836285131\"\r\n\r\n# Returns a boolean\r\n\r\n# Syntax: Database.exists({<selectorFieldName>:<selectorFieldValue>})\r\n\r\nDatabase.exists({\"userid\",\"9783836285131\"})\r\n# Returns: True\r\n```\r\nDoes not throw errors.\r\n\r\n# Delete a record\r\n```py\r\n# __id is the hidden and unique id of the record\r\n# Syntax: Database.delete(<__id>)\r\n\r\nDatabase.delete(1)\r\n# Deletes the record with the __id 1\r\n\r\nDatabase.delete(Database.read({\"userid\":\"9783836285131\"}, \"__id\"))\r\n# Deletes the record \r\n```\r\nDoes not throw errors.\r\n\r\n# Mass data output (dumping)\r\n```py\r\nDatabase.header\r\n# Returns a list of all present headers\r\n\r\nDatabase.dump_header()\r\n# Returns the full header segment with the fieldnames, types and states\r\n\r\n# suported formats: csv, plain \r\nDatabase.formated_dump(format)\r\n# plain is default, it returns the data as it is saved \r\n# csv, returns the database formated in csv seperated by \"|\"\r\n```\r\n\r\n# States and Formats\r\n\r\n\r\nThe State of a field can have following values:\r\n```\r\nunique = field can be changed but must contain a unique value among all records, only applies to the same field\r\nlocked = field needs to be set in .create(), can not be changed afterwards\r\nmodular = field can be updated without restrictions\r\nindex = locked and unique\r\nloose = field can remain unset or undefined and is modular\r\n```\r\nAll fieldtypes, except \"loose\", need to be set by their rules in .create() otherwise it will throw an error like \r\n```\r\n MissingField: \"displayname\" is missing in: {\"userid\": \"9783836285131\",\"balance\": 6.50,\"joined\": \"31.03.2024\"}\r\n```\r\n\r\n<br/>There are a few supported formats as of now:\r\n```\r\n__any = ignores type notations, aka can have any type. Is not recommended\r\nstr, list, dict, bool, float, int = only accepts its propper type as value\r\nnostr, nolist, nodict, nobool, nofloat, noint = accepts it's propper type or None as value\r\n```\r\nIf there is a Type mismatch you get:\r\n```\r\n InvalidType: \"userid\" does not match typerule \"str\" in: {\"userid\":9783836285131,\"displayname\": \"randomtimetv\",\"balance\": 6.50,\"joined\": \"31.03.2024\"}\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT with required credit to the author.",
"summary": "Create yourself a simple database with this package.",
"version": "3.3",
"project_urls": {
"Homepage": "https://github.com/RandomTimeLP/RTS_DataBase/"
},
"split_keywords": [
"database"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6b123e982f0c37ca0d951df17e3f4c4ce9c5e0cb914beb2ea4dec4a553afed70",
"md5": "a97d88971aa9b8e4950cb175f8904673",
"sha256": "478807c7c869b7f27ddc87558b52ac9ef3b4114a3f11025f5532a5d4e19e26e1"
},
"downloads": -1,
"filename": "RTSDB-3.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a97d88971aa9b8e4950cb175f8904673",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 10190,
"upload_time": "2024-12-07T13:50:55",
"upload_time_iso_8601": "2024-12-07T13:50:55.803366Z",
"url": "https://files.pythonhosted.org/packages/6b/12/3e982f0c37ca0d951df17e3f4c4ce9c5e0cb914beb2ea4dec4a553afed70/RTSDB-3.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "29aaa18f03545b01f2c5d7282fd112e83cae0b6580810b207972e0e2fa376f23",
"md5": "52f7e7036418dd70c2816c12a388405b",
"sha256": "161128f4e68453450a872e389cd3f9019db2a1723b1d07bcf72fdeb9e0aa5dbe"
},
"downloads": -1,
"filename": "rtsdb-3.3.tar.gz",
"has_sig": false,
"md5_digest": "52f7e7036418dd70c2816c12a388405b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9691,
"upload_time": "2024-12-07T13:50:57",
"upload_time_iso_8601": "2024-12-07T13:50:57.422546Z",
"url": "https://files.pythonhosted.org/packages/29/aa/a18f03545b01f2c5d7282fd112e83cae0b6580810b207972e0e2fa376f23/rtsdb-3.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-07 13:50:57",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "RandomTimeLP",
"github_project": "RTS_DataBase",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "rtsdb"
}