Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Storing python objects inside PysonDB
Adwaith Rajesh
Adwaith Rajesh

Posted on

     

Storing python objects inside PysonDB

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

Consider I've functionfoo which takes an argumentx and returnx * (x + x). Which I want to store inside a DB.

deffoo(x:float)->float:returnx*(x+x)
Enter fullscreen modeExit fullscreen mode

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

importpicklebyte_string=pickle.dumps(foo)
Enter fullscreen modeExit fullscreen mode

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}"
Enter fullscreen modeExit fullscreen mode

This string can be now added to the DB

frompysondbimportdba=db.getDb("test.json")a.add({"name":"foo_function","obj":obj_string})
Enter fullscreen modeExit fullscreen mode

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

data=a.getBy({"name":"foo_function"})
Enter fullscreen modeExit fullscreen mode

Nowdata 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}]
Enter fullscreen modeExit fullscreen mode

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

str_obj=data[0]["obj"]
Enter fullscreen modeExit fullscreen mode

Remember thatstr_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.

importastbyte_obj=ast.literal_eval(str_obj)
Enter fullscreen modeExit fullscreen mode

To call the function we can do this.

call_obj=pickle.loads(byte_obj)print(call_obj(3))# output18
Enter fullscreen modeExit fullscreen mode

Entire Code

importastimportpicklefrompysondbimportdbdeffoo(x:float)->float:returnx*(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))
Enter fullscreen modeExit fullscreen mode

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A programmer by hobby. Loves tech and opensource. Python enthusiast.
  • Location
    Kannur Kerala
  • Education
    Sophomore
  • Work
    Freelancer
  • Joined

More fromAdwaith Rajesh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp