From brett at python.org Tue Oct 10 12:40:59 2017 From: brett at python.org (Brett Cannon) Date: Tue, 10 Oct 2017 16:40:59 +0000 Subject: [Python-porting] python 2/3 compatibility of function call In-Reply-To: <652c277c-52c1-25a7-6eda-3d8042a41cb1@gmail.com> References: <652c277c-52c1-25a7-6eda-3d8042a41cb1@gmail.com> Message-ID: Did you try decorating the method with @staticmethod? On Mon, 9 Oct 2017 at 08:47 Radostin Stoyanov wrote: > Hello all, > > I am working on a patch series for virt-manager [1] which aims to resolve > compatibility issues with Python 3. However I stuck with the following > issue: > > 1. When I run the tests with Python 3 some of them fail with: > > TypeError: set_memory_cb() takes 4 positional arguments but 5 were > given > > These errors are coming from this function call > > : > > self.cb(parser, inst, self.val, self) > > Where `self.cb` is assigned to `ParserMemory.set_memory_cb`. > > I found that the concept of ?unbound methods? has been removed in Python > 3. [2] > When referencing a method as a class attribute, you now get a plain > function object. > > Example: > > - Python 2 > > >>> class X: > ... def foo(self): return 1 > ... > >>> bar = X.foo > >>> type(bar) > > >>> X.foo(X()) > 1 > >>> X.foo(0) > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method foo() must be c > alled with X instance as first argument ( > got int instance instead) > > - Pyhton 3 > > >>> class X: > ... def foo(self): return 1 > ... > >>> bar = X.foo > >>> type(bar) > > >>> X.foo(X()) > 1 > >>> X.foo(0) > 1 > > However, I could not reproduce the issue causing TypeError as above. One > hacky solution could be to change functions like `set_memory_cb` in the > following way: > > from > > def set_memory_cb(self, inst, val, virtarg): > setattr(inst, virtarg.cliname, int(val) * 1024) > > to > > def set_memory_cb(self, inst, val, virtarg, ignore=None): > if ignore: > self, inst, val, virtarg = inst, val, virtarg, ignore > setattr(inst, virtarg.cliname, int(val) * 1024) > > However, I would like to ask for suggestions on how this issue could be > resolved properly? > > [1] https://github.com/virt-manager/virt-manager > [2] > https://docs.python.org/release/3.0.1/whatsnew/3.0.html#operators-and-special-methods > > Kind Regards, > Radostin > _______________________________________________ > Python-porting mailing list > Python-porting at python.org > https://mail.python.org/mailman/listinfo/python-porting > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zhongxiaobin76 at aliyun.com Mon Oct 30 01:48:27 2017 From: zhongxiaobin76 at aliyun.com (zhongxiaobin76) Date: Mon, 30 Oct 2017 13:48:27 +0800 Subject: [Python-porting] =?utf-8?q?Ask_a_question_about_Python_accessing?= =?utf-8?q?_mongodb=2E_Thanks=2E?= Message-ID: When I?found?problems?in?the?process?of?accessing?mongodb?with?Python, Who?can?I?ask?for?help? I?want?to?create?an?object?with?Python?to?receive?a?document?in?mongodb,?and?the?document?format?is?like?this:{?"_id"?:?ObjectId("59f2d36fb934f31fdc002485"),?"Name"?:?"???1711",?"Date"?:?"20 14/12/12",?"data"?:?[?{?"time"?:?"0901",?"Open"?:?"13280.00",?"Max"?:?"13280.00" ,?"Min"?:?"13280.00",?"Close"?:?"13280.00",?"Volume"?:?"0",?"Amount"?:?"580",?"S ettlement"?:?"0.00",?"EXPMA1"?:?"13304.91",?"EXPMA2"?:?"13301.51"?},?{?"time"?: "0902",?"Open"?:?"13280.00",?"Max"?:?"13280.00",?"Min"?:?"13280.00",?"Close"?:?" 13280.00",?"Volume"?:?"0",?"Amount"?:?"580",?"Settlement"?:?"0.00",?"EXPMA1"?:?" 13301.08",?"EXPMA2"?:?"13300.66"?}]}There?are?about?300?records?in?the?data. How?do?I?write?an?object?that?encapsulates?the?Mongo?document?like?the?object?VtBarData?in?the?code?below?def loadBar(self, dbName, collectionName, days): print "zxb ctaEngine CtaEngine loadBar days = ", days startDate = self.today - timedelta(days) d = {'datetime':{'$gte':startDate}} barData = self.mainEngine.dbQuery(dbName, collectionName, d, 'datetime') l = [] for d in barData: print "zxb ctaEngine CtaEngine loadBar d = ", d bar = VtBarData() bar.__dict__ = d l.append(bar) return l -------------- next part -------------- An HTML attachment was scrubbed... URL: