RE: missing required positional arguments

"Boylan, Ross" <[email protected]>
Newsgroups gmane.comp.web.zope.zodb
Message-ID <[email protected]>
Thank you, Marius and Jim.  Rather than mess with hglib and __getnewargs__, with which I'm unfamiliar (though I see it in pickle), I converted to a namedtuple, which seems to  persist fine.

class revision(namedtuple('revision', ['rev', 'node', 'tags', 'branch', 'author', 'desc', 'date' ])):
    __slots__ = ()

    def __str__(self):
        short = self.desc.splitlines()[0]
        return "r{} {} {:%c} {}".format(self.rev, self.node[0:9], self.date, short)

The conversion function is a bit ugly since it also deals with the fact that most of the values in hglib are byte's, probably because of its Python 2 heritage:

Side note: "{}".format(alist), where alist is a list of revisions from the class above, does not use the str form above, since in a list the repr of the each item is displayed.  A quick and dirty solution is to reimplement __repr__.

If you're curious, here's the conversion function:
def newRevision(r):
    """Convert r from and hglib.client.revision to a new revision tuple.
    The primary motivation is that the input argument can not be pickled.
    A secondary motivation is to eliminate the <bytes> type used by hglib"""
    # it is somewhat mysterious to me that int() seems to work as needed for
    # the first argument: 900 == int(b'900')
    return revision(*([int(r.rev)] + [str(x, encoding="utf-8") if isinstance(x, bytes) else x for x in r[1:] ]))

Ross
________________________________
From: [email protected] [[email protected]] on behalf of Jim Fulton [[email protected]]
Sent: Thursday, November 17, 2016 4:14 AM
To: zodb [[email protected]]
Subject: Re: [ZODB] missing required positional arguments



On Thu, Nov 17, 2016 at 1:35 AM, Marius Gedminas <[email protected]<mailto:[email protected]>> wrote:
On Thu, Nov 17, 2016 at 03:28:24AM +0000, Boylan, Ross wrote:
> I'm having trouble pulling an instance back from the database (ZEO).  It's a revision object from hglib package.  My first try did not import hglib in the client; the second try did.  Both got the same error; see below for fuller output.
>
> Maybe the type in the database is defined using some kind of nested class I can't easily access, maybe tng.hglib.revision?
>
> https://selenic.com/repo/python-hglib/file/tip/hglib/client.py defines
> class revision(tuple):
>     def __new__(cls, rev, node, tags, branch, author, desc, date):
>         return tuple.__new__(cls, (rev, node, tags, branch, author, desc, date))

This class does not support pickling.

Thanks Marius.

  Try it:

   >>> import pickle
   >>> r = revision(1, 2, 3, 4, 5, 6, 7)
   >>> p = pickle.dumps(r)
   >>> r2 = pickle.loads(p)

The simplest way to make it pickle-compatible would be to override
revision.__getnewargs__().

Another way would be to create your own class for storing revision information and copy data from the hglib revision instances to instances of this new class. This has the advantage of remaining decoupled from hglib.

Jim

--
Jim Fulton
http://jimfulton.info

--
You received this message because you are subscribed to the Google Groups "zodb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]<mailto:[email protected]>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "zodb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.