[Python-ideas] PEP 4XX: Adding sys.implementation
Barry Warsaw
barry at python.org
Wed May 2 00:28:26 CEST 2012
On Apr 30, 2012, at 09:22 PM, Eric Snow wrote:
>> I agree that sequence semantics are meaningless here. Presumably, a
>> dictionary is proposed because this
>>
>> cache_tag = sys.implementation.get('cache_tag')
>>
>> is nicer than
>>
>> cache_tag = getattr(sys.implementation, 'cache_tag', None)
>
>That's a good point. Also, a dict better reflects a collection of
>variables that a dotted-access object, which to me implies the
>potential for methods as well.
>
>> OTOH, maybe we need a nameddict type!
>
>You won't have to convince _me_. :)
Well, I was being a bit facetious. You can easily implement those semantics
in pure Python. 5 minute hack below.
Cheers,
-Barry
-----snip snip-----
#! /usr/bin/python3
_missing = object()
import operator
import unittest
class Implementation:
cache_tag = 'cpython33'
name = 'CPython'
def __getitem__(self, name, default=_missing):
result = getattr(self, name, default)
if result is _missing:
raise AttributeError("'{}' object has no attribute '{}'".format(
self.__class__.__name__, name))
return result
def __setitem__(self, name, value):
raise TypeError('read only')
def __setattr__(self, name, value):
raise TypeError('read only')
implementation = Implementation()
class TestImplementation(unittest.TestCase):
def test_cache_tag(self):
self.assertEqual(implementation.cache_tag, 'cpython33')
self.assertEqual(implementation['cache_tag'], 'cpython33')
def test_name(self):
self.assertEqual(implementation.name, 'CPython')
self.assertEqual(implementation['name'], 'CPython')
def test_huh(self):
self.assertRaises(AttributeError, operator.getitem,
implementation, 'droids')
self.assertRaises(AttributeError, getattr,
implementation, 'droids')
def test_read_only(self):
self.assertRaises(TypeError, operator.setitem,
implementation, 'droids', 'looking')
self.assertRaises(TypeError, setattr,
implementation, 'droids', 'looking')
self.assertRaises(TypeError, operator.setitem,
implementation, 'cache_tag', 'xpython99')
self.assertRaises(TypeError, setattr,
implementation, 'cache_tag', 'xpython99')
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120501/8c105337/attachment.pgp>
More information about the Python-ideas
mailing list