0

I have implemented a class which when instantiated dumped itself into the a pickle file in a form of a list, heres the psudo code for my case:

"""template/base.py"""
import pickle

DB_NAME = "db.pickle"

def update_records(filename, record_list):
    with open(filename, 'wb') as f:
        pickle.dump(record_list, f)

class Base:
    records = []
    def __init__(self):
        ...

    def __new__(cls, *args, **kwargs):
        self = super(Base, cls).__new__(cls)
        cls.records.append(self)
        update_records(DB_NAME, cls.records)
        return self

and i had it subclassed into 5 different classes, inwhich CodingProblem is one of them, so i can use its new facility, in another script i did.

from templates import coding_probs

project = coding_probs.CodingProblem("codewar")

this saved the project to the db.pickle file but when i tried to unpickle them in the same file like below:

from pickle import load
from templates import coding_probs
from config import DB_NAME

# project = coding_probs.CodingProblem("codewar")

with open(DB_NAME, 'rb') as f:
    data = load(f)

print(data)

it gave the error

AttributeError: Can't get attribute 'CodingProblem' on <module 'templates.coding_probs' from 'C:\\Users\\muham\\Desktop\\fun and learning\\project_manager\\templates\\coding_probs.py'> i have consulted the following questions

Pickle AttributeError: Can't get attribute 'Wishart' on <module '__main__' from 'app.py'>

AttributeError: Can't get attribute 'GaitDataset' on <module '__main__' (built-in)>

but they didnt helped my case.

0 Answers0