Storing python objects inside PysonDB

Let’s see how we can store python objects, this include strings, list, function, etc.., inside a DB like pysonDB

Consider I’ve function foo which takes an argument x and return x * (x + x). Which I want to store inside a DB.

def foo(x: float) ->…


This content originally appeared on DEV Community and was authored by Adwaith Rajesh

Let's see how we can store python objects, this include strings, list, function, etc.., inside a DB like pysonDB

Consider I've function foo which takes an argument x and return x * (x + x). Which I want to store inside a DB.

def foo(x: float) -> float:
    return x * (x + x)

First things first we nee to convert this object to byte string. To do that we can use the pickle. module.

import pickle
byte_string = pickle.dumps(foo)

This byte string cannot be directly added to a PysonDB database, as byte strings are not JSON serializable. So a simple workaround will be to add quotes around the byte string which can be easily done like this.

obj_string = f"{byte_string}"

This string can be now added to the DB

from pysondb import db

a = db.getDb("test.json")
a.add({
    "name": "foo_function",
    "obj": obj_string
})

To get the object back we can do the following steps.

data = a.getBy({"name": "foo_function"})

Now data will look something like this.

[{'name': 'foo_function', 'obj': "b'\\x80\\x04\\x95\\x14\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x8c\\x08__main__\\x94\\x8c\\x03foo\\x94\\x93\\x94.'", 'id': 316182400052721056}]

To convert the obj string back to a callable function. We can do the following.

str_obj = data[0]["obj"]

Remember that str_obj is still a string and not a byte string, which is what we need, since the required byte string is inside this string, we can simply evaluate the string.

import ast
byte_obj = ast.literal_eval(str_obj) 

To call the function we can do this.

call_obj = pickle.loads(byte_obj)
print(call_obj(3))

# output
18

Entire Code

import ast
import pickle

from pysondb import db

def foo(x: float) -> float:
    return x * (x + x)

byte_string = pickle.dumps(foo)
obj_string = f"{byte_string}"

a = db.getDb("test2.json")
a.add({
    "name": "foo_function",
    "obj": obj_string
})

data = a.getBy({"name": "foo_function"})

str_obj = data[0]["obj"]
byte_obj = ast.literal_eval(str_obj)

call_obj = pickle.loads(byte_obj)
print(call_obj(3))

So we have successfully stored a python object inside a DB. The steps are the same for all the objects like list or dict.


This content originally appeared on DEV Community and was authored by Adwaith Rajesh


Print Share Comment Cite Upload Translate Updates
APA

Adwaith Rajesh | Sciencx (2021-06-13T02:14:27+00:00) Storing python objects inside PysonDB. Retrieved from https://www.scien.cx/2021/06/13/storing-python-objects-inside-pysondb/

MLA
" » Storing python objects inside PysonDB." Adwaith Rajesh | Sciencx - Sunday June 13, 2021, https://www.scien.cx/2021/06/13/storing-python-objects-inside-pysondb/
HARVARD
Adwaith Rajesh | Sciencx Sunday June 13, 2021 » Storing python objects inside PysonDB., viewed ,<https://www.scien.cx/2021/06/13/storing-python-objects-inside-pysondb/>
VANCOUVER
Adwaith Rajesh | Sciencx - » Storing python objects inside PysonDB. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/13/storing-python-objects-inside-pysondb/
CHICAGO
" » Storing python objects inside PysonDB." Adwaith Rajesh | Sciencx - Accessed . https://www.scien.cx/2021/06/13/storing-python-objects-inside-pysondb/
IEEE
" » Storing python objects inside PysonDB." Adwaith Rajesh | Sciencx [Online]. Available: https://www.scien.cx/2021/06/13/storing-python-objects-inside-pysondb/. [Accessed: ]
rf:citation
» Storing python objects inside PysonDB | Adwaith Rajesh | Sciencx | https://www.scien.cx/2021/06/13/storing-python-objects-inside-pysondb/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.