Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64073
| 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 | <fomcl@yahoo.com> |
| 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 <fomcl@yahoo.com> |
| Subject | Re: Is it possible to get string from function? |
| To | python-list@python.org, Peter Otten <__peter__@web.de> |
| In-Reply-To | <lb86kj$993$1@ger.gmane.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5582.1389870171.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
--------------------------------------------
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
Roy Smith wrote:
> I realize the subject line is kind of meaningless, so
let me explain :-)
>
> I've got some unit tests that look like:
>
> class Foo(TestCase):
> def test_t1(self):
> RECEIPT = "some string"
>
> def test_t2(self):
> RECEIPT = "some other string"
>
> def test_t3(self):
> RECEIPT = "yet a third string"
>
> and so on. It's important that the strings be
mutually unique. 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. 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.
>
> 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.
>
> Is it possible to do that? 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. Of course, that
> assignment doesn't even happen until the function is
executed, so
> perhaps what I want just isn't possible?
>
> It turns out, I solved the problem with more mundane
tools:
>
> grep 'RECEIPT = ' test.py | sort | uniq -c
>
> 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. But, I'm still
> interested in exploring if there is any way to do this
with
> introspection, as an academic exercise.
Instead of using introspection you could make it explicit
with a decorator:
$ cat unique_receipt.py
import functools
import sys
import unittest
_receipts = {}
def unique_receipt(receipt):
def deco(f):
if receipt in _receipts:
raise ValueError(
"Duplicate receipt {!r} in \n {} and \n
{}".format(
receipt, _receipts[receipt], f))
_receipts[receipt] = f
@functools.wraps(f)
def g(self):
return f(self,
receipt)
return g
return deco
class Foo(unittest.TestCase):
@unique_receipt("foo")
def test_t1(self, RECEIPT):
pass
@unique_receipt("bar")
def test_t2(self, RECEIPT):
pass
@unique_receipt("foo")
def test_t3(self, RECEIPT):
pass
if __name__ == "__main__":
unittest.main()
$ python unique_receipt.py
Traceback (most recent call last):
File "unique_receipt.py", line 19, in <module>
class Foo(unittest.TestCase):
File "unique_receipt.py", line 28, in Foo
@unique_receipt("foo")
File "unique_receipt.py", line 11, in deco
receipt, _receipts[receipt], f))
ValueError: Duplicate receipt 'foo' in
<function test_t1 at 0x7fc8714af5f0> and
<function test_t3 at 0x7fc8714af7d0>
============> Very cool approach. Question, though: what would be wrong with the following approach:
import unittest
class Test(unittest.TestCase):
receipts = {}
def unique_value(self, k, v):
assert Test.receipts.get(k) is None, "Duplicate: %s" % v
Test.receipts[k] = v
def test_a(self):
self.unique_value("large_value", "foo")
def test_b(self):
self.unique_value("large_value", "bar") # oh no, a duplicate!
def test_c(self):
self.unique_value("another_large_value", "blah")
unittest.main()
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Is it possible to get string from function? Albert-Jan Roskam <fomcl@yahoo.com> - 2014-01-16 02:59 -0800 Re: Is it possible to get string from function? Roy Smith <roy@panix.com> - 2014-01-16 08:29 -0800
csiph-web