User Tools

Site Tools


python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python [2021/01/22 21:04] – created rogerpython [2026/03/27 00:52] (current) – Update deprecated Python 2.7 assert methods to Python 3 equivalents roger
Line 21: Line 21:
 | assertNotIsInstance      | not is instance(a,b)                    | 2.7 | | assertNotIsInstance      | not is instance(a,b)                    | 2.7 |
 | assertRaises             | fun(*args,**kwds) raises exc            |     | | assertRaises             | fun(*args,**kwds) raises exc            |     |
-assertRaisesRegexp       | fun(*args,**kwds) raises exc(regex)     2.|+assertRaisesRegex        | fun(*args,**kwds) raises exc(regex)     3.|
 | assertAlmostEqual        | round(a-b,7) == 0                           | | assertAlmostEqual        | round(a-b,7) == 0                           |
 | assertNotAlmostEqual     | round(a-b,7) != 0                           | | assertNotAlmostEqual     | round(a-b,7) != 0                           |
Line 28: Line 28:
 | assertLess               | a < b                                   | 2.7 | | assertLess               | a < b                                   | 2.7 |
 | assertLessEqual          | a <= b                                  | 2.7 | | assertLessEqual          | a <= b                                  | 2.7 |
-assertRegexpMatches      | r.search(s)                             | 2.7 +assertRegex              | r.search(s)                             3.2 | 
-assertNotRegexpMatches   | not r.search(s)                         | 2.7 +assertNotRegex           | not r.search(s)                         3.2 | 
-assertItemsEqual         | sorted(a) == sorted(b)                  | 2.+assertCountEqual         | sorted(a) == sorted(b)                  | 3.
-| assertDictContainsSubset | all the key/value pairs in a exist in b | 2.7 |+| assertDictContainsSubset | all the key/value pairs in a exist in b | 2.7 (removed in 3.2) |
 | assertMultiLineEqual     | strings                                 | 2.7 | | assertMultiLineEqual     | strings                                 | 2.7 |
 | assertSequenceEqual      | sequences                               | 2.7 | | assertSequenceEqual      | sequences                               | 2.7 |
Line 73: Line 73:
  
 ==== Convert strings to datetime ==== ==== Convert strings to datetime ====
 +
 +More info https://stackabuse.com/converting-strings-to-datetime-in-python/
  
 <code python> <code python>
Line 103: Line 105:
 print('yay!') if today == 'friday' else print('boo') print('yay!') if today == 'friday' else print('boo')
 >>> yay!  >>> yay! 
 +</code>
 +
 +==== Unpack extras on a tuple or list ====
 +
 +<code python>
 +t = (0, 1, 2, 3, 4)
 +
 +a, b, *c = t
 +
 +print(a)
 +print(b)
 +print(c)
 +# 0
 +# 1
 +# [2, 3, 4]
 +
 +print(type(c))
 +# <class 'list'>
 +
 +a, *b, c = t
 +
 +print(a)
 +print(b)
 +print(c)
 +# 0
 +# [1, 2, 3]
 +# 4
 +
 +*a, b, c = t
 +
 +print(a)
 +print(b)
 +print(c)
 +# [0, 1, 2]
 +# 3
 +# 4
 +
 </code> </code>
  
  
python.1611349459.txt.gz · Last modified: 2024/11/17 12:59 (external edit)