python 2.7
DESCRIPTION
Introduction to Python 2.7TRANSCRIPT
Python 2.7
ふるかわとおる @torufurukawa株式会社ビープラウド
2.7 と 3.1.2 が最新安定版
どのバージョンを使うべきか決めないと
バージョンごとの違いが分かればなぁ
良くも悪くも違いが見えにくい
大雑把な位置づけ
2.5 2.6 2.7
3.0 3.1
3.x と 2.x の断絶
2.5 2.6 2.7
3.0 3.1
2.x 系は終了
2.5 2.6 2.7
3.0 3.1
3.x からのバックポート
2.5 2.6 2.7
3.0 3.1
2.5 2.6 2.7
3.0 3.1
set と dict のリテラル>>> {1,2,3}set([1, 2, 3])>>> {x for x in range(3)}set([0, 1, 2]) >>> {i: i*2 for i in range(4)}{0: 0, 1: 2, 2: 4, 3: 6}
OrderedDict
>>> from collections import OrderedDict>>> d = OrderedDict([('first',1), ('second',2)])
>>> dOrderedDict([('first', 1), ('second', 2)])>>> [x for x in d]['first', 'second’]
unittest.TestCase の新メソッド• assertRegexpMatches(text, regexp, msg=None)• assertNotRegexpMatches(text, regexp, msg=None)• assertIn(first, second, msg=None)• assertNotIn(first, second, msg=None)• assertItemsEqual(actual, expected, msg=None)• assertSetEqual(set1, set2, msg=None)• assertDictEqual(expected, actual, msg=None)• assertDictContainsSubset(expected, actual, msg=None)• assertListEqual(list1, list2, msg=None)• assertTupleEqual(tuple1, tuple2, msg=None)• assertSequenceEqual(seq1, seq2, msg=None, seq_type=None)
シーケンスに含まれるかテスト self.assert_(5 in [1,2,3])
↓Traceback (most recent call last): File "bar.py", line 10, in test_in self.assert_(5 in [1,2,3])AssertionError: False is not True
Fales は True ではありません
シーケンスに含まれるかテスト new
self.assertIn(5, [1,2,3])↓
Traceback (most recent call last): File "bar.py", line 9, in test_in self.assertIn(5, [1,2,3])AssertionError: 5 not found in [1, 2, 3]
5 は [1,2,3] に、ない
class MyTest(unittest.TestCase): @unittest.expectedFailure # Fail するものとして扱
う def test2(self): self.assertEqual(1, 2)
@unittest.skip(‘skip me’) # テストしない def test1(self): self.assertEqual(False, True)
3.x に興味あるけど、まだ移行できない人におすすめ
2.5 2.6 2.7
3.0 3.1