Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64066
| 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!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'that?': 0.05; 'assignment': 0.07; 'class,': 0.07; 'duplicate': 0.07; 'explicit': 0.07; 'sys': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'methods,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 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; 'grep': 0.16; 'mundane': 0.16; 'possible?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'roy': 0.16; 'skip:@ 20': 0.16; 'subject:possible': 0.16; 'uniq': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'example': 0.22; 'import': 0.22; 'putting': 0.22; 'tests': 0.22; 'header:User- Agent:1': 0.23; 'sort': 0.25; "i've": 0.25; 'academic': 0.26; 'long,': 0.26; 'pass': 0.26; 'skip:" 20': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; "doesn't": 0.30; 'characters': 0.30; "i'm": 0.30; 'extract': 0.31; 'inspect': 0.31; 'trivial': 0.31; 'file': 0.32; 'class': 0.32; 'there.': 0.32; '(most': 0.33; 'third': 0.33; 'skip:_ 10': 0.34; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'there': 0.35; '2500': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'unit': 0.37; 'so,': 0.37; 'to:addr:python- list': 0.38; 'recent': 0.39; 'explain': 0.39; 'realize': 0.39; 'visual': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'above,': 0.60; 'life,': 0.60; 'first': 0.61; 'kind': 0.63; 'real': 0.63; 'happen': 0.63; 'more': 0.64; 'smith': 0.68; 'subject': 0.69; 'subject:get': 0.81; 'discover': 0.82; "'foo'": 0.84; 'characters,': 0.84; 'different.': 0.84; 'exercise.': 0.84; 'unique.': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Is it possible to get string from function? |
| Date | Thu, 16 Jan 2014 09:52:57 +0100 |
| Organization | None |
| References | <roy-406DCF.22465415012014@news.panix.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b66c.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| 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.5577.1389862382.18130.python-list@python.org> (permalink) |
| Lines | 92 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1389862382 news.xs4all.nl 2973 [2001:888:2000:d::a6]:37674 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:64066 |
Show key headers only | View raw
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>
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Is it possible to get string from function? Roy Smith <roy@panix.com> - 2014-01-15 22:46 -0500
Dynamic generation of test cases for each input datum (was: Is it possible to get string from function?) Ben Finney <ben+python@benfinney.id.au> - 2014-01-16 16:02 +1100
Re: Dynamic generation of test cases for each input datum (was: Is it possible to get string from function?) Roy Smith <roy@panix.com> - 2014-01-16 00:09 -0500
Re: Is it possible to get string from function? Chris Angelico <rosuav@gmail.com> - 2014-01-16 16:25 +1100
Re: Is it possible to get string from function? Roy Smith <roy@panix.com> - 2014-01-16 00:40 -0500
Re: Is it possible to get string from function? Chris Angelico <rosuav@gmail.com> - 2014-01-16 16:47 +1100
Re: Is it possible to get string from function? Steven D'Aprano <steve@pearwood.info> - 2014-01-16 07:16 +0000
Re: Is it possible to get string from function? Roy Smith <roy@panix.com> - 2014-01-16 09:30 -0500
Re: Is it possible to get string from function? Peter Otten <__peter__@web.de> - 2014-01-16 09:52 +0100
csiph-web