if condition evaluates to False, an AssertionError is raised
if message is supplied, it is used as payload for AssertionError
in the command line, the -O flag can be used to disable active assert statements when running programs
$ python3 -m timeit -n1-s"from random import randrange; from sorted_set import SortedSet; s = SortedSet(randrange(1000) for _ in range(2000))""[s.count(i)] for i in range(1000)]"#=> 1 loops best of 3: 3.11 sec per loop
$ python3 -O-m timeit -n1-s"from random import randrange; from sorted_set import SortedSet; s = SortedSet(randrange(1000) for _ in range(2000))""[s.count(i)] for i in range(1000)]"#=> 1 loops best of 3: 3.26 msec per loop
with active assertions (without -O flag) => 3.11 sec per loop
without active assertions (with -O flag) => 3.26 msec per loop
Examples of good assert usage
Internal invariants
defmodulus_three(n):
r = n %3if r ==0:print("Multiple of 3")elif r ==1:print("Remainder 1")else:# r == 2 <= should replace with assertassert r ==2,"Remainder is not 2"print("Remainder 2")# and better yetdefmodulus_four(n):
r = n %4if r ==0:print("Multiple of 4")elif r ==1:print("Remainder 1")elif r ==2:print("Remainder 2")elif r ==3:print("Remainder 3")else:assertFalse,"This should never happen"
Class invariants
# sorted_set.pyfrom bisect import bisect_left
from collections.abc import Sequence, Set
from itertools import chain
classSortedSet(Sequence, Set):def__init__(self, items):
self._items =sorted(set(items))if items isnotNoneelse[]def__contains__(self, item):
index = bisect_left(self._items, item)return(index !=len(self._items))and(self._items[index]== item)def__len__(self):returnlen(self._items)def__iter__(self):for item in self._items:yield item
def__getitem__(self, index):
result = self._items[index]return SortedSet(result)ifisinstance(index,slice)else result
def__repr__(self):return"SortedSet({})".format(repr(self._items)if self._items else'')def__eq__(self, right_hand_side):ifnotisinstance(right_hand_side, SortedSet):return NotImplemented
return self._items == right_hand_side._items
def__ne__(self, right_hand_side):ifnotisinstance(right_hand_side, SortedSet):return NotImplemented
return self._items != right_hand_side._items
def_is_unique_and_sorted(self):returnall(
self._items[i]< self._items[i +1]for i inrange(len(self._items)-1))# NOTE: method assumes items already sorted because of its use of binary searchdefindex(self, item):
index = bisect_left(self._items, item)if(index !=len(self._items))and(self._items[index]== item):return index
raise ValueError("{} not found").format(repr(item))# NOTE: method depends on there being no duplicates in itemsdefcount(self, item):returnint(item in self)def__add__(self, right_hand_side):return SortedSet(chain(self._items, right_hand_side._items))def__mul__(self, right_hand_side):return self if right_hand_side >0else SortedSet()def__rmul__(self, left_hand_side):return self * left_hand_side
defissubset(self, iterable):return self <= SortedSet(iterable)defissuperset(self, iterable):return self >= SortedSet(iterable)definteraction(self, iterable):return self & SortedSet(iterable)defunion(self, iterable):return self | SortedSet(iterable)defsymmetric_difference(y)(self, iterable):return self ^ SortedSet(iterable)defdifference(self, iterable):return self - SortedSet(iterable)