Python Forum
How to redefine object so that all user- objects have the necessary capabilities? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to redefine object so that all user- objects have the necessary capabilities? (/thread-16173.html)



How to redefine object so that all user- objects have the necessary capabilities? - AlekseyPython - Feb-17-2019

When I used the pickle library, it turned out that it does not save the current values ​​of the class members (after recovery pickle fills them with the original values). Pickle save only current values of instance variables. Therefore, I wrote a class 'AbstractSavingObject', which implements saving, including the current values ​​of class variables. Now I want to give this opportunity to all my classes (for the ability to save in a dump all objects when an exception occurs) and do not want to write inheritance from 'AbstractSavingObject' everywhere.

How to do this?


RE: How to redefine object so that all user- objects have the necessary capabilities? - micseydel - Feb-18-2019

Could you post short code exemplifying what you're talking about?

Also, if you haven't heard already, pickle is typically considered a security hole, so you might want to consider JSON or something else for your serialization. That won't directly help you with saving class variable though, I suspect that's something you'll just have to do manually.


RE: How to redefine object so that all user- objects have the necessary capabilities? - AlekseyPython - Feb-20-2019

Thank you for your interest!

import os, pickle
class A:
    b=2
    def __init__(self, value):
        self.c = value
    
if not os.path.exists('MyPicl.bin'):
    with open('MyPicl.bin', 'wb') as dump:
        old_a = A(1)
        old_a.__class__.b = 3
    
        print(old_a.b)
        print(old_a.__class__.b)
        print(old_a.c)
        pickler = pickle.Pickler(dump, pickle.HIGHEST_PROTOCOL)
        pickler.dump(old_a)
        
else:
    with open('MyPicl.bin', 'rb') as dump:
        unpickler = pickle.Unpickler(dump)
        new_a = unpickler.load()
        
        print(new_a.b)
        print(new_a.__class__.b)
        print(new_a.c)
If you run this code twice (completely closing the program after the first launch), the first time you get the output:
Output:
3 3 1
, and the second time:
Output:
2 2 1
I want write like this:
object.parents.append(MyClassForCorrectSaving)
class A(object):
    b=2
    def __init__(self, value):
        self.c = value



RE: How to redefine object so that all user- objects have the necessary capabilities? - AlekseyPython - Feb-20-2019

(Feb-18-2019, 06:41 PM)micseydel Wrote: Could you post short code exemplifying what you're talking about?

Also, if you haven't heard already, pickle is typically considered a security hole, so you might want to consider JSON or something else for your serialization. That won't directly help you with saving class variable though, I suspect that's something you'll just have to do manually.

json has limitations:
a) tuple during serialization / deserialization transform into a list.
b) what happens with set- it's unknown.
c) dict keys should ONLY have the string / number type.

Therefore, this method does not allow writing arbitrary code.


RE: How to redefine object so that all user- objects have the necessary capabilities? - micseydel - Feb-27-2019

Apologies for taking a while to get back, things have been pleasantly busy.

I've looked a bit and I can't find any direct way to get what you want. If you don't mind sticking to Python literals instead of classes, ast.literal_eval might be worth taking a look at; assuming your objects are simple enough, you can just print them and then evaluate them with literal_eval when you read from a file.


RE: How to redefine object so that all user- objects have the necessary capabilities? - AlekseyPython - Mar-03-2019

My decision:
class MyObject:
    def __init__(self):
        print('I'm here!')

__builtins__.object = MyObject