Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6769817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:14:18+00:00 2026-05-26T15:14:18+00:00

I have been using Python’s pickle module for implementing a thin file-based persistence layer.

  • 0

I have been using Python’s pickle
module for implementing a thin file-based persistence layer. The
persistence layer (part of a larger library) relies heavily on pickle’s persistent_id feature
to save objects of specified classes as separate files.

The only issue with this approach is that pickle files are not human
editable, and I’d much rather have objects saved in a format that is
human readable and editable with a text editor (e.g., YAML or JSON).

Do you know of any library that uses a human-editable format and
offers features similar to pickle‘s persistent_id? Alternatively,
do you have suggestions for implementing them on top of a YAML- or
JSON-based serialization library, without rewriting a large subset of
pickle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:14:19+00:00Added an answer on May 26, 2026 at 3:14 pm

    I haven’t tried this yet myself, but I think you should be able to do this elegantly with PyYAML using what they call “representers” and “resolvers”.

    EDIT

    After an extensive exchange of comments with the poster, here is a method to achieve the required behavior with PyYAML.

    Important Note: If a Persistable instance has another such instance as an attribute, or contained somehow inside one of its attributes, then the contained Persistable instance will not be saved to yet another separate file, rather it will be saved inline in the same file as the parent Persistable instance. To the best of my understanding, this limitation also existed in the OP’s pickle-based system, and may be acceptable for his/her use cases. I haven’t found an elegant solution for this which doesn’t involve hacking yaml.representer.BaseRepresenter.

    import yaml
    from functools import partial
    
    class Persistable(object):
        # simulate a unique id
        _unique = 0
    
        def __init__(self, *args, **kw):
            Persistable._unique += 1
            self.persistent_id = ("%s.%d" %
                                  (self.__class__.__name__, Persistable._unique))
    
    def persistable_representer(dumper, data):
        id = data.persistent_id
        print "Writing to file: %s" % id
        outfile = open(id, 'w')
        outfile.write(yaml.dump(data))
        outfile.close()
        return dumper.represent_scalar(u'!xref', u'%s' % id)
    
    class PersistingDumper(yaml.Dumper):
        pass
    
    PersistingDumper.add_representer(Persistable, persistable_representer)
    my_yaml_dump = partial(yaml.dump, Dumper=PersistingDumper)
    
    def persistable_constructor(loader, node):
        xref = loader.construct_scalar(node)
        print "Reading from file: %s" % id
        infile = open(xref, 'r')
        value = yaml.load(infile.read())
        infile.close()
        return value
    
    yaml.add_constructor(u'!xref', persistable_constructor)
    
    
    # example use, also serves as a test
    class Foo(Persistable):
        def __init__(self):
            self.one = 1
            Persistable.__init__(self)
    
    class Bar(Persistable):
        def __init__(self, foo):
            self.foo = foo
            Persistable.__init__(self)
    
    foo = Foo()
    bar = Bar(foo)
    print "=== foo ==="
    dumped_foo = my_yaml_dump(foo)
    print dumped_foo
    print yaml.load(dumped_foo)
    print yaml.load(dumped_foo).one
    
    print "=== bar ==="
    dumped_bar = my_yaml_dump(bar)
    print dumped_bar
    print yaml.load(dumped_bar)
    print yaml.load(dumped_bar).foo
    print yaml.load(dumped_bar).foo.one
    
    baz = Bar(Persistable())
    print "=== baz ==="
    dumped_baz = my_yaml_dump(baz)
    print dumped_baz
    print yaml.load(dumped_baz)
    

    From now on use my_yaml_dump instead of yaml.dump when you want to save instances of the Persistable class to separate files. But don’t use it inside persistable_representer and persistable_constructor! No special loading function is necessary, just use yaml.load.

    Phew, that took some work… I hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using python with regex to clean up a text file. I
I have been using NotePAD++ for editing Python scripts. I recently downloaded the PyDEV
I have been trying to make a case for using Python at my work.
I have been asked to write a program using python for an assignment. I
I've been using python for years, but I have little experience with python web
I am trying to talk to a device using python. I have been handed
I have recently been working with Python using Komodo Edit and other simpler editors
I've been using Textmate for Ruby/Python scripting for awhile and now have a need
I have been programming using Python for slightly more than half an year now
I have been using emacs for a while for mainly python programming, and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.