Slide 1

Slide 1 text

u s i n g & b u i l d i n g PYTHON SET PRACTICE Learn great API design ideas from Python's set types. Luciano Ramalho @standupdev

Slide 2

Slide 2 text

GOALS 1
 Show why Python’s set types are a great example of API design. 2
 Explain the __magic__ behind the set types, and how to build your own. 2

Slide 3

Slide 3 text

MOTIVATION Some common use cases for sets 3

Slide 4

Slide 4 text

CASO DE USO #1 4 display product if all words in the query appear in the product description.

Slide 5

Slide 5 text

SET-LESS SOLUTION #1 I’ve written code like this in Go, which lacks built-in sets: 5

Slide 6

Slide 6 text

SET-LESS SOLUTION #2 More readable, but still inefficient: 6

Slide 7

Slide 7 text

7 What if… Later! I am too busy coding nested loops! www.workcompass.com/

Slide 8

Slide 8 text

CASO DE USO #1 8 www.workcompass.com/ display product if all words in the query appear in the product description.

Slide 9

Slide 9 text

CASO DE USO #1 9 Q ⊂ D www.workcompass.com/ display product if all words in the query appear in the product description.

Slide 10

Slide 10 text

CASO DE USO #2 10 Mark all products previously favorited, except those already in the shopping cart.

Slide 11

Slide 11 text

CASO DE USO #2 11 F ∖ C Mark all products previously favorited, except those already in the shopping cart.

Slide 12

Slide 12 text

LOGIC AND SETS A close relationship 12

Slide 13

Slide 13 text

Nobody has yet discovered a branch of mathematics that has successfully resisted formalization into set theory. Thomas Forster
 Logic Induction and Sets, p. 167 13

Slide 14

Slide 14 text

LOGIC CONJUNCTION IS INTERSECTION x belongs to the intersection of A with B. is the same as: x belongs to A and
 x also belongs to B. Math notation: x ∈ (A ∩ B) ⟺ (x ∈ A) ∧ (x ∈ B) In computing: AND 14

Slide 15

Slide 15 text

LOGIC DISJUNCTION: UNION x belongs to the union of A and B. is the same as: x belongs to A or
 x belongs to B. Math notation: x ∈ (A ∪ B) ⟺ (x ∈ A) ∨ (x ∈ B) In computing: OR 15

Slide 16

Slide 16 text

SYMMETRIC DIFFERENCE x belongs to A or
 x belongs to B but
 does not belong to both Is the same as: x belongs to the union of A with B less the intersection of A with B. Math notation:
 In computing: XOR 16 x ∈ (A ∆ B) ⟺ (x ∈ A) ⊻ (x ∈ B)

Slide 17

Slide 17 text

DIFFERENCE x belongs to A but
 does not belong to B. is the same as: elements of A minus elements of B Math notation: x ∈ (A ∖ B) ⟺ (x ∈ A) ∧ (x ∉ B) 17

Slide 18

Slide 18 text

SETS IN SEVERAL LANGUAGES 18

Slide 19

Slide 19 text

SETS IN SEVERAL STANDARD LIBRARIES Some languages/platform APIs that implement sets in their standard libraries 19 Java Set interface: < 10 methods; 8 implementations Python set, frozenset: > 10 methods and operators .Net (C# etc.) ISet interface: > 10 methods; 2 implementations JavaScript (ES6) Set: < 10 methods Ruby Set: > 10 methods and operators Python, .Net and Ruby offer rich set APIs

Slide 20

Slide 20 text

SETS IN PYTHON The built-in types 20

Slide 21

Slide 21 text

BUILDING A SET FROM A SERIES OF NUMBERS Using a set comprehension: 21

Slide 22

Slide 22 text

ANOTHER SET, FOR THE EXAMPLES 22

Slide 23

Slide 23 text

STRING REPRESENTATION The __str__ and __repr__ methods: __str__ is used by str() and print(). __repr__ is used by repr() and by the console, debugger etc. 23

Slide 24

Slide 24 text

ELEMENT CONTAINMENT: THE IN OPERATOR O(1) in sets, because they use a hash table to hold elements. Implemented by the __contains__ special method: 24

Slide 25

Slide 25 text

FUNDAMENTAL SET OPERATIONS 25 Intersection Union Symmetric difference (a.k.a. XOR) Difference

Slide 26

Slide 26 text

SET COMPARISONS Subset and superset testing (set length does not matter!). In math: ⊂, ⊆, ⊃, ⊇. 26

Slide 27

Slide 27 text

DE MORGAN’S LAW: #1 27

Slide 28

Slide 28 text

DE MORGAN’S LAW: #2 28

Slide 29

Slide 29 text

SET METHODS Going beyond what operators can do. 29

Slide 30

Slide 30 text

SET OPERATORS AND METHODS (1) 30

Slide 31

Slide 31 text

SET OPERATORS AND METHODS (2) Differences: 31

Slide 32

Slide 32 text

SET TESTS All of these return a bool: 32

Slide 33

Slide 33 text

ADDITIONAL METHODS These have nothing to do with math, and all to do with practical computing: 33

Slide 34

Slide 34 text

ABSTRACT SET INTERFACES These interfaces are all defined in collections.abc. set and frozenset both implement Set set also implements MutableSet 34

Slide 35

Slide 35 text

OPERATOR OVERLOADING Not as bad as they say 35

Slide 36

Slide 36 text

A DESIGNER’S DILEMMA 36

Slide 37

Slide 37 text

DESIGN DECISIONS HAVE CONSEQUENCES Compound interest in Python (works for any numeric types that implement the needed operators): Compound interest in Java, if you need to use a non-primitive numeric type such as BigDecimal: 37

Slide 38

Slide 38 text

COMPARISON OPERATORS 38

Slide 39

Slide 39 text

39

Slide 40

Slide 40 text

THE BEAUTY OF DOUBLE DISPATCH 40

Slide 41

Slide 41 text

CONCLUSION 41

Slide 42

Slide 42 text

LEARNING FROM SETS Set operations can greatly simplify logic. Pythonic objects should implement __repr__, __eq__. Pythonic collections should implement __len__, __iter__, __contains__, and accept iterable arguments. Check out this example showing how to implement class designed for dense sets of small integers: Much more about these subjects in Fluent Python. 42 https://github.com/standupdev/uintset

Slide 43

Slide 43 text

Luciano Ramalho
 @ramalhoorg | @standupdev
 [email protected] THANK YOU!