1 #!/usr/bin/env python
2 '''a pythonic factor'''
3 import unittest
4 import platform
5
6 class TestPyfactorTestCase(unittest.TestCase):
7 def setUp(self):
8 '''setting up stuff'''
13
14 def testplatformbuiltins(self): 15
'''platform.__builtins__.blah '''
16 self.assertEquals(platform.__builtins__.__class__, "<type 'd
ict'>")
17
18
19 def tearDown(self):
20 print 'cleaning stuff up'
21
22 if __name__ == "__main__":
23 unittest.main()
Is there an error in my syntax? Why is my test failing? Line 16.
python stfu/testing/test_pyfactor.py
Fcleaning stuff up
======================================================================
FAIL: platform.__builtins__.blah
----------------------------------------------------------------------
Traceback (most recent call last):
File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
self.assertEquals(platform.__builtins__.__class__, "<type 'dict'>")
AssertionError: <type 'dict'> != "<type 'dict'>"
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
--
John Maclean MSc. (DIC) Bsc. (Hons),Core Linux Systems Engineering,07739
171 531
What happens if you change this line:
self.assertEquals(platform.__builtins__.__class__, "<type 'dict'>")
To something like:
self.assertEquals(platform.__builtins__.__class__, type(dict()))
or
self.assertEquals(str(platform.__builtins__.__class__), "<type 'dict'>")
--
mph
Try replacing line 16 with this:
self.assertEqual(type(platform.__builtins__), dict)
Cheers,
Cliff
> Is there an error in my syntax? Why is my test failing? Line 16.
>
> ======================================================================
> FAIL: platform.__builtins__.blah
> ----------------------------------------------------------------------
> Traceback (most recent call last):
> File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
> self.assertEquals(platform.__builtins__.__class__, "<type 'dict'>")
> AssertionError: <type 'dict'> != "<type 'dict'>"
>
> ----------------------------------------------------------------------
To express the condition "SOMEOBJECT must be a dictionary", use:
isinstance(SOMEOBJECT, dict)
SOMEOBJECT might actually be an instance of any derived class and still
pass the test; that's usually the desired behavior.
In the rare cases where only a very specific type is allowed, use this
form instead:
type(SOMEOBJECT) is dict
The test case above should read then:
self.assert_(isinstance(platform.__builtins__, dict),
type(platform.__builtins__))
--
Gabriel Genellina
This is cool. Thanks for your replies.
self.assertEqual(platform.__builtins__.__class__, dict,
"platform.__class__ supposed to be dict")
self.assertEqual(platform.__name__, 'platform' )
--
John Maclean
07739 171 531
MSc (DIC)
Enterprise Linux Systems Engineer
The preferred spelling for:
platform.__builtins__.__class__
would be
type(platform.__builtins__)
It's shorter and easier to read, but essentially says the same thing.
You can also use it on integer literals, which you can't do with your
syntax:
>>> type(1)
<type 'int'>
>>> 1.__class__
...
SyntaxError: invalid syntax
Admittedly, this is a trivial benefit. If you're using a literal, you
already know what type you're dealing with.
Cheers,
Cliff
Agreed
> It's shorter and easier to read, but essentially says the same thing.
>
> You can also use it on integer literals, which you can't do with your
> syntax:
>
> >>> type(1)
> <type 'int'>
> >>> 1.__class__
> ...
> SyntaxError: invalid syntax
Add the needed space and it works fine.
>>> 1 .__class__
<class 'int'>
A possible use of literal int attributes is for bound mehods:
>>> inc = 1 .__add__
>>> inc(3)
4
>>> inc(3.0)
NotImplemented
Whereas def inc(n): return n+1 is generic and would return 4.0.
Terry Jan Reedy