Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'assignment': 0.07; 'class,': 0.07; 'duplicate': 0.07; 'explicit': 0.07; 'none,': 0.07; 'receipts': 0.07; 'sys': 0.07; 'though:': 0.07; 'string': 0.09; '"__main__":': 0.09; '%s"': 0.09; '__name__': 0.09; 'methods,': 0.09; 'subject:string': 0.09; 'unittest': 0.09; 'valueerror:': 0.09; 'python': 0.11; 'def': 0.12; '"some': 0.16; 'dict': 0.16; 'different,': 0.16; 'executed,': 0.16; 'function?': 0.16; 'grep': 0.16; 'mundane': 0.16; 'possible?': 0.16; 'roy': 0.16; 'skip:@ 20': 0.16; 'subject:possible': 0.16; 'to:addr:web.de': 0.16; 'uniq': 0.16; 'skip:= 10': 0.16; 'thursday,': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'thu,': 0.19; 'example': 0.22; 'import': 0.22; 'putting': 0.22; 'tests': 0.22; 'sort': 0.25; "i've": 0.25; 'academic': 0.26; 'long,': 0.26; 'pass': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'function': 0.29; 'skip:- 40': 0.29; 'raise': 0.29; "doesn't": 0.30; 'characters': 0.30; 'cool': 0.30; "i'm": 0.30; 'assert': 0.31; 'extract': 0.31; 'inspect': 0.31; 'trivial': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'third': 0.33; 'date:': 0.34; 'skip:_ 10': 0.34; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'skip:s 30': 0.35; 'skip:u 20': 0.35; 'no,': 0.35; 'but': 0.35; 'there': 0.35; '2500': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'january': 0.37; 'unit': 0.37; 'wrong': 0.37; 'so,': 0.37; 'email addr:python.org': 0.37; 'question,': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'explain': 0.39; 'subject:': 0.39; 'realize': 0.39; 'visual': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'above,': 0.60; 'life,': 0.60; 'received:98.137': 0.60; 'first': 0.61; 're:': 0.63; 'kind': 0.63; 'real': 0.63; 'happen': 0.63; 'more': 0.64; 'email name:python-list': 0.65; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'smith': 0.68; 'subject': 0.69; 'received:98.136': 0.69; 'subject:get': 0.81; 'discover': 0.82; "'foo'": 0.84; '2014,': 0.84; 'characters,': 0.84; 'different.': 0.84; 'exercise.': 0.84; 'otten': 0.84; 'received:bullet.mail.gq1.yahoo.com': 0.84; 'approach.': 0.91 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 853211.49803.bm@omp1080.mail.gq1.yahoo.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1389869982; bh=OfdHDb7hjmuotJHOXSqKOOt3vFWNdvKv4LXS+bRInqU=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:Message-ID:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=EZn0CyRp16UDWeI8/TBhq2IocZLm3TF9XifPcxIlBZflNcLkxP+/jgbfrb2h5pahsiXuU4gxBGTe940OW0SMuT0W3+lhf4A9QIJotv5dnp086y+nkbIrTDIWHndvOAWd7B36l/nJoHagSPLFbofCG84cJs5UXqSjRDmPQBF8K3s= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:Message-ID:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=5HlUZIoTRX/NjVAuJY4AuliFkXsG/Zpzi2vepSFe9Ai4/1Ra8BKg+1dZIpnxKSkBIctZIpFqYeenbZerD6umhUJjSKGLFV5QzjFW9hKZTs0NXElAA0vhEXuFKtxp/zaefXb4nfPEyXUzurmWjPj2WiMvKC4uzHUZRnqvZCMs0uk=; X-YMail-OSG: dOx7ho8VM1mc39WcF4LtQ0MxWtTNpv91gEespiRbvcYl8yG a6zNOkCAC2vZ0gE15EzyEPOfOlE1ddVIAFyfFD.I03k5RK.GT2.QyngSQhtQ iXs5c9eMh5eVpx8cmIwmTgBVBjlcxjVbVf6PjiiD4HDPbGq1QdjPGHElx3c4 uTT16vO6uosgF7_DQVIx7xKjBCYcBKWcZTp1zuGhDM9VS7gAsOMXCPqvDVTr E1UFQSzqiJ9wiLxeYutQYMEdQvBhMXVBis9q3hhdYDJl8OffR5bSrEw_3ogc gN4poKakRZnnKKjZYRbJo6Z3.dAHJ.9aU5nEzua5g4alfIcocfFaVVhVVLoz .NaaFSaSG2C8riKRmP3MMSoqYpIJt5QtqaqVizbJDu6a0jnIUs1th64HqoN1 ZlHMyB8OorSrKlcWqS7GXXy_7AxwWywsYyeBtadHg8zFfipIJfq0fBnxwZfT GiFuehZvCTBH00sKTaVDoDr7l2Ugw_YvlPaDf6EePUJTV2nwqTuU8JJMYVTB giAbnYq47aDkYXhyGHoCKfFr4XCYdgzgwNCLL_ynI6sTnxztdBBKvjUrwSvg 6vhPfzGWxLuQBd9fXu42oO1fLYPC7yReNRuEfus3P72Xe7.Y7id4y2Crguni sE93B4d9IgGe36uGhCmY14Q3x6F1mBBLQawpE90dbZwlbdO9wUe54dJU- X-Rocket-MIMEInfo: 002.001, DQoNCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tDQpPbiBUaHUsIDEvMTYvMTQsIFBldGVyIE90dGVuIDxfX3BldGVyX19Ad2ViLmRlPiB3cm90ZToNCg0KIFN1YmplY3Q6IFJlOiBJcyBpdCBwb3NzaWJsZSB0byBnZXQgc3RyaW5nIGZyb20gZnVuY3Rpb24_DQogVG86IHB5dGhvbi1saXN0QHB5dGhvbi5vcmcNCiBEYXRlOiBUaHVyc2RheSwgSmFudWFyeSAxNiwgMjAxNCwgOTo1MiBBTQ0KIA0KIFJveSBTbWl0aCB3cm90ZToNCiANCiA.IEkgcmVhbGl6ZSB0aGUgc3ViamVjdCABMAEBAQE- X-Mailer: YahooMailClassic/387 YahooMailWebService/0.8.173.622 Date: Thu, 16 Jan 2014 02:59:42 -0800 (PST) From: Albert-Jan Roskam Subject: Re: Is it possible to get string from function? To: python-list@python.org, Peter Otten <__peter__@web.de> In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 151 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389870171 news.xs4all.nl 2946 [2001:888:2000:d::a6]:49127 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64073 -------------------------------------------- On Thu, 1/16/14, Peter Otten <__peter__@web.de> wrote: Subject: Re: Is it possible to get string from function? To: python-list@python.org Date: Thursday, January 16, 2014, 9:52 AM =20 Roy Smith wrote: =20 > I realize the subject line is kind of meaningless, so let me explain :-) >=20 > I've got some unit tests that look like: >=20 > class Foo(TestCase): >=A0=A0=A0def test_t1(self): >=A0 =A0=A0=A0RECEIPT =3D "some string" >=20 >=A0=A0=A0def test_t2(self): >=A0 =A0=A0=A0RECEIPT =3D "some other string" >=20 >=A0=A0=A0def test_t3(self): >=A0 =A0=A0=A0RECEIPT =3D "yet a third string" >=20 > and so on.=A0 It's important that the strings be mutually unique.=A0 In the > example above, it's trivial to look at them and observe that they're all > different, but in real life, the strings are about 2500 characters long, > hex-encoded.=A0 It even turns out that a couple of the strings are > identical in the first 1000 or so characters, so it's not trivial to do > by visual inspection. >=20 > So, I figured I would write a meta-test, which used introspection to > find all the methods in the class, extract the strings from them (they > are all assigned to a variable named RECEIPT), and check to make sure > they're all different. >=20 > Is it possible to do that?=A0 It is straight-forward using the inspect > module to discover the methods, but I don't see any way to find what > strings are assigned to a variable with a given name.=A0 Of course, that > assignment doesn't even happen until the function is executed, so > perhaps what I want just isn't possible? >=20 > It turns out, I solved the problem with more mundane tools: >=20 > grep 'RECEIPT =3D ' test.py | sort | uniq -c >=20 > and I could have also solved the problem by putting all the strings in a > dict and having the functions pull them out of there.=A0 But, I'm still > interested in exploring if there is any way to do this with > introspection, as an academic exercise. =20 Instead of using introspection you could make it explicit with a decorator: =20 $ cat unique_receipt.py=20 import functools import sys import unittest =20 _receipts =3D {} def unique_receipt(receipt): =A0 =A0 def deco(f): =A0 =A0 =A0 =A0 if receipt in _receipts: =A0 =A0 =A0 =A0 =A0 =A0 raise ValueError( =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "Duplicate receipt {!r} in \n=A0 =A0 {} and \n=A0 =A0 {}".format( =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 receipt, _receipts[receipt], f)) =A0 =A0 =A0 =A0 _receipts[receipt] =3D f =A0 =A0 =A0 =A0 @functools.wraps(f) =A0 =A0 =A0 =A0 def g(self): =A0 =A0 =A0 =A0 =A0 =A0 return f(self, receipt) =A0 =A0 =A0 =A0 return g =A0 =A0 return deco =20 class Foo(unittest.TestCase): =A0 =A0 @unique_receipt("foo") =A0 =A0 def test_t1(self, RECEIPT): =A0 =A0 =A0 =A0 pass =20 =A0 =A0 @unique_receipt("bar") =A0 =A0 def test_t2(self, RECEIPT): =A0 =A0 =A0 =A0 pass =20 =A0 =A0 @unique_receipt("foo") =A0 =A0 def test_t3(self, RECEIPT): =A0 =A0 =A0 =A0 pass =20 if __name__ =3D=3D "__main__": =A0 =A0 unittest.main() $ python unique_receipt.py=20 Traceback (most recent call last): =A0 File "unique_receipt.py", line 19, in =A0 =A0 class Foo(unittest.TestCase): =A0 File "unique_receipt.py", line 28, in Foo =A0 =A0 @unique_receipt("foo") =A0 File "unique_receipt.py", line 11, in deco =A0 =A0 receipt, _receipts[receipt], f)) ValueError: Duplicate receipt 'foo' in=20 =A0 =A0 and =20 =A0 =A0 =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D> Very cool approach. Question, though= : what would be wrong with the following approach: import unittest class Test(unittest.TestCase): receipts =3D {} def unique_value(self, k, v): assert Test.receipts.get(k) is None, "Duplicate: %s" % v Test.receipts[k] =3D v =20 def test_a(self): self.unique_value("large_value", "foo") def test_b(self): self.unique_value("large_value", "bar") # oh no, a duplicate!=20 def test_c(self): self.unique_value("another_large_value", "blah") =09=09 unittest.main()