DEV Community

Adwaith Rajesh
Adwaith Rajesh

Posted on

An easy and fun way to permanently store python objects.

Object Bucket

An easy and fun way to store python objects.

Star it on GitHub


Description

Object Bucket is a python package that allows you to store python objects permanently in a more user friendly way.


Installation

The object-bucket package can be installed by using pip.

pip install object-bucket
Enter fullscreen mode Exit fullscreen mode

Usage

Creating new bucket.

  from object_bucket import Bucket

  test_bucket = Bucket("name-of-the-bucket")
Enter fullscreen mode Exit fullscreen mode
  • Adding droplets to the bucket, droplets are considered as objects that you want to save permanently.
  test_obj = [1, 2, 3, 4]
  test_bucket.add_droplet("droplet-name", test_obj)
Enter fullscreen mode Exit fullscreen mode

Trying to add a droplet with the same name will cause an error.

  • Adding multiple droplets. To add multiple droplets you have to have a dictionary that contains all the names and objects of the droplet. To add the dictionary you can use the add_droplets method.
  droplets = {
        "one": 1,
        "two": 2,
        "three": [2, 3, 4]
    }

    test_bucket.add_droplets(droplets)
Enter fullscreen mode Exit fullscreen mode

Modifying a droplet

  new_obj = {1: "a"}
  test_bucket.modify_droplet("droplet-name", new_obj)
Enter fullscreen mode Exit fullscreen mode

Trying to modify a droplet that does not exists will cause an error.

Saving a bucket

All the things mentioned above will not be added or saved permanently, to do so it is necessary to save the bucket.

  test_bucket.save_bucket()
Enter fullscreen mode Exit fullscreen mode

Retrieving values from a bucket.

 from object_bucket import Bucket
 test_bucket = Bucket("name-of-the-bucket")
 a = test.bucker.get_droplet("droplet-name")
 print(a)  # {1: "a"}

Enter fullscreen mode Exit fullscreen mode

Trying to get a droplet that does not exists will cause an error.

  • Get all the runtime droplets
 drop1 = [1, 2, 3, 4]
 drop2 = "Hello"
 drop3 = {1: "a", 2: "b"}
 test_bucket.add_droplet("drop1", drop1)
 test_bucket.add_droplet("drop2", drop2)
 test_bucket.add_droplet("drop3", drop3)

 # to get all the droplets
 a = test_bucket.get_all_droplets()
 print(a)

 # output
 {"drop1": [1, 2, 3, 4], "drop2": "Hello", "drop3": {1: "a", 2: "b"}}
Enter fullscreen mode Exit fullscreen mode
  • Deleting a bucket To delete the bucket and to clear the runtime storage of all the droplets.
 test_bucket.delete_bucket()
Enter fullscreen mode Exit fullscreen mode
  • You can also delete a bucket using remove_bucket function
  from object_bucket import remove_bucket
  remove_bucket("name-of_bucket_to_be_removed", bucket_file_path="file-path-of-the-bucket")
Enter fullscreen mode Exit fullscreen mode

Using the context manager.

It might be a hastle to remember to save to bucket, so you can use the context manager to avoid using the ""save_bucket"" method.

Note Using ""Bucket().delete_bucket"" inside the context
manager is useless as at the end the file will be saved automatically.

from object_bucket import Bucket

with Bucket("name-of-the-bucket") as b:
  # code to execute
  b.add_droplet("name", 1)
  # ...etc
  b.delete_bucket()  # wont work as the file will be again saved,
  # but the runtime contents will be cleared
Enter fullscreen mode Exit fullscreen mode

Some more stuff

  • You can use the if statement to check whether a bucket is empty or not
  from object_bucket import Bucket
  t = Bucket("name")
  if t:
    print("Hello")  # -> does not print anything as bucket is empty

  t.add_droplet("demo", 1)
  if t:
    print("Hello 2")  # -> prints "hello 2" as the bucket has at least one droplet
Enter fullscreen mode Exit fullscreen mode
  • To get the number of droplets in a bucket you can use the len method
  from object_bucket import Bucket
  t = Bucket("name")
  print(len(t)) # -> 0
  t.add_droplet("demo", [1, 2, 3])
  print(len(t)) # -> 1
Enter fullscreen mode Exit fullscreen mode

This will work with any python object, including functions, classes, lambda functions ..etc

Top comments (2)

Collapse
 
iceorfiresite profile image
Ice or Fire

Interesting... what's the benefit of using this over a database?

Collapse
 
adwaithrajesh profile image
Adwaith Rajesh • Edited

well a DB is to store a lot of info. If you are looking for something quick and easy for a small project then you can use this. I've also written blog post on storing object in PysonDB, the same principle can be applied in Storing objects in a JSON file. you can get the post here and moreover the project uses only one dependency that is dill. the reason it doesn't uses pickle is that pickle doesn't support some object like lambda functions. So this is also light weight.