API Documentation¶
-
class
thanosdb.
ThanosDB
(location, auto_dump, sig)¶ A key-value based data structure which internally uses msgpack for storing data. Serialization and deserialization of data happens during dumping and loading operations.
Note
Install MessagePack as ThanosDB internally uses it for storage.
Example: >>> from thanosdb import thanosdb >>> db = thanosdb.load('avengers.db', True)
Parameters: - location (string) – location of msgpack database file
- auto_dump (boolean) – writes to disk after every operation if True
- sig (boolean) – used for graceful shutdown during dump if True
-
append
(key, more)¶ Add more to a key’s value
Example: >>> db.set('thor', 'Thor Odinson') True >>> db.append('thor', ' - God of Thunder') True >>> db.get('thor') 'Thor Odinson - God of Thunder'
Parameters: - key (string) – Name of key in db
- more (string) – Value associated with key
Returns: True if successful execution else false.
Return type: Boolean
-
dadd
(name, pair)¶ Add a key-value pair to a dict, “pair” is a tuple
Example: >>> db.dadd('infinity-stone', ('Soul Stone', 'Vormir')) True >>> db.dadd('infinity-stone', ('Time Stone', 'Earth')) True >>> db.get('infinity-stone') {'Soul Stone': 'Vormir', 'Time Stone': 'Earth'}
Parameters: - name (string) – Name of the key of dict in db
- pair (tuple) – key-value tuple to be added
Returns: True if name is string else Key-String Error.
Return type: Boolean
-
dcreate
(name)¶ Create a dict, name must be str
Example: >>> db.dcreate('infinity-stones') True
Parameters: name (string) – Name of the key of dict in db Returns: True if name is string else Key-String Error. Return type: Boolean
-
deldb
()¶ Delete everything from the database
Example: >>> db.deldb() True
-
dexists
(name, key)¶ Determine if a key exists or not in a dict
Example: >>> db.dexists('infinity-stone', 'Soul Stone') True
Parameters: - name (string) – Name of key of dict in db
- key (string) – key in the dict
Returns: True if key is found in the dict
Return type: Boolean
-
dget
(name, key)¶ Return the value for a key in a dict
Example: >>> db.dget('infinity-stone', 'Soul Stone') 'Vormir'
Parameters: - name (string) – Name of the key of dict in db
- key (string) – Name of key in dict
Returns: Value if key is found in the dict
-
dgetall
(name)¶ Return all key-value pairs from a dict
Example: >>> db.dgetall('infinity-stone') {'Soul Stone': 'Vormir', 'Time Stone': 'Earth'}
Parameters: name (string) – Name of the key of dict in db Returns: data stored in dict Return type: dict
-
dkeys
(name)¶ Return all the keys for a dict
Example: >>> db.dkeys('infinity-stone') dict_keys(['Soul Stone', 'Time Stone'])
Parameters: name (string) – Name of key of dict in db Returns: dict_keys Return type: dict_keys
-
dmerge
(name1, name2)¶ Merge two dicts together into name1
Example: >>> db.dadd('stone-owners', ('Dr. Strange', 'Time Stone')) >>> db.dmerge('infinity-stone', 'stone-owners') True
Parameters: - name1 (string) – Name of key of first dict
- name2 (string) – Name of key of second dict
Returns: True
Return type: Boolean
-
dpop
(name, key)¶ Remove one key-value pair in a dict
Example: >>> db.dpop('infinity-stone', 'Soul Stone') 'Vormir'
Parameters: - name (string) – Name of the key in db
- key (string) – Name of the key in dict
Returns: Value stored in key
Return type: datatype of value
-
drem
(name)¶ Remove a dict and all of its pairs
Example: >>> db.drem('infinity-stone') True
Parameters: name (string) – Name of the key of dict in db Returns: True Return type: Boolean
-
dump
()¶ Force dump memory db to file.
Example: >>> db.dump() True
Returns: True Return type: Boolean
-
dvals
(name)¶ Return all the values for a dict
Example: >>> db.dvals('infinity-stone') dict_values(['Soul Stone', 'Time Stone'])
Parameters: name (string) – Name of key of dict Returns: dict_values Return type: dict_values
-
exists
(key)¶ Return True if key exists in db, return False if not
Example: >>> db.exists('ironman') True
Parameters: key (string) – input key Returns: True if key exists in db, else False. Return type: Boolean
-
get
(key)¶ Get the value of a key
Example: >>> db.get('ironman') 'Tony Stark'
Parameters: key (string) – Name of key in db Returns: Value if key present else returns false.
-
getall
()¶ Return a list of all keys in db
Example: >>> db.getall() dict_keys(['ironman', 'thor', 'black-widow'])
Returns: List of all keys in db. Return type: dict_keys
-
ladd
(name, value)¶ Add a value to a list
Example: >>> db.ladd('avengers', 'Iron Man')
Parameters: - name (string) – key of dict
- value (string) – value to be appended to list with key name
Returns: True if successful execution else false.
Return type: Boolean
-
lappend
(name, pos, more)¶ Add more to a value in a list
Example: >>> db.lappend('ironman', 2, 'Jarvis')
Parameters: - name (string) – Name of key of list
- pos (int) – Index of list
- more (string) – string to be added with element at index pos of list associated with key name
Returns: True if successful execution else false.
Return type: Boolean
-
lcreate
(name)¶ Create an empty list with key name, name must be str
Example: >>> db.lcreate('avengers')
Parameters: name (string) – key of dict Returns: True if successful execution else false. Return type: Boolean
-
lexists
(name, value)¶ Determine if a value exists in a list
Example: >>> db.lexists('avengers', 'ironman') False
Parameters: - name (string) – Name of key of list in db
- value (string) – Element to check if present in list with key name
Returns: True if value exists in list else false.
Return type: Boolean
-
lextend
(name, seq)¶ Extend a list with a sequence
Example: >>> db.lextend('avengers', ['Thor', 'Hawkeye']) True
Parameters: - name (string) – Name of the key of list in db
- seq – list to be appended to list associated with key name in db
Returns: True if successful execution else false.
Return type: Boolean
-
lget
(name, pos)¶ Return one value in a list
Example: >>> db.lget('avengers', 1) 'ironman'
Parameters: - name (string) – Name of the key of list in db
- pos (int) – position of element in list
Returns: Value at index pos of list associated with key name in db.
-
lgetall
(name)¶ Return all values in a list
Example: >>> db.lgetall('avengers') ['ironman', 'thor', 'captain-america', 'hulk']
Parameters: name (string) – Name of the key of list in db Returns: List associated with key name in db else false if key not present. Return type: list
-
llen
(name)¶ Returns the length of the list
Example: >>> db.llen('avengers') 4
Parameters: name (string) – Name of the key of list in db Returns: Length of list associated with key name in dict. Return type: int
-
load
(location, auto_dump)¶ Loads, reloads or changes the path to the db file
Example: >>> from thanosdb import thanosdb >>> db = thanosdb.load('avengers.db', True, False)
Parameters: - location (string) – location of msgpack database file
- auto_dump (boolean) – writes to disk after every operation
- sig (boolean) – used for graceful shutdown during dump
-
lpop
(name, pos)¶ Remove one value in a list
Example: >>> db.lpop('avengers', 1)
Parameters: - name (string) – Name of the key of list in db
- pos (int) – index of the item in the list to be deleted
Returns: Value of deleted item.
Return type: string
-
lremlist
(name)¶ Remove a list and all of its values
Example: >>> db.lremlist('avengers')
Parameters: name (string) – Name of the key of list in db Returns: Length of list deleted from db with key name. Return type: int
-
lremvalue
(name, value)¶ Remove a value from a certain list
Example: >>> db.lremvalue('avengers', 'Black Widow') True
Parameters: - name (string) – Name of the key of list in db
- value (string) – Value to be deleted from list
Returns: True if successful execution else false.
Return type: Boolean
-
rem
(key)¶ Delete a key
Example: >>> db.rem('ironman') True
Parameters: key (string) – Name of the key to add in db Returns: True if key exists in db and gets deleted, else False if no such key in db. Return type: Boolean
-
set
(key, value)¶ Set the str value of a key
Example: >>> db.set('ironman', 'Tony Stark') True
Parameters: - key (string) – Name of the key to add in db
- value (string, dict, list) – Value associated with the key
Returns: True for successful execution else false.
Return type: boolean
-
totalkeys
(name=None)¶ Get a total number of keys, lists, and dicts inside the db
Example: >>> db.totalkeys() 5
Parameters: name (string) – None or name of key of dict Returns: Count of number of keys of dict. Return type: int
-
thanosdb.
load
(location, auto_dump, sig=True)¶ Return a thanosdb object. location is the path to the msgpack file.