Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python set practice

Python set practice

Let's talk about using sets in practice, and learn great API design ideas from Python's set types.

27c093d0834208f4712faaaec38c2c5c?s=128

Luciano Ramalho

May 03, 2018
Tweet

Transcript

  1. 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
  2. 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
  3. MOTIVATION Some common use cases for sets 3

  4. CASO DE USO #1 4 display product if all words

    in the query appear in the product description.
  5. SET-LESS SOLUTION #1 I’ve written code like this in Go,

    which lacks built-in sets: 5
  6. SET-LESS SOLUTION #2 More readable, but still inefficient: 6

  7. 7 What if… Later! I am too busy coding nested

    loops! www.workcompass.com/
  8. CASO DE USO #1 8 www.workcompass.com/ display product if all

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

    product if all words in the query appear in the product description.
  10. CASO DE USO #2 10 Mark all products previously favorited,

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

    products previously favorited, except those already in the shopping cart.
  12. LOGIC AND SETS A close relationship 12

  13. 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
  14. 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
  15. 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
  16. 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)
  17. 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
  18. SETS IN SEVERAL LANGUAGES 18

  19. 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
  20. SETS IN PYTHON The built-in types 20

  21. BUILDING A SET FROM A SERIES OF NUMBERS Using a

    set comprehension: 21
  22. ANOTHER SET, FOR THE EXAMPLES 22

  23. 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
  24. 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
  25. FUNDAMENTAL SET OPERATIONS 25 Intersection Union Symmetric difference (a.k.a. XOR)

    Difference
  26. SET COMPARISONS Subset and superset testing (set length does not

    matter!). In math: ⊂, ⊆, ⊃, ⊇. 26
  27. DE MORGAN’S LAW: #1 27

  28. DE MORGAN’S LAW: #2 28

  29. SET METHODS Going beyond what operators can do. 29

  30. SET OPERATORS AND METHODS (1) 30

  31. SET OPERATORS AND METHODS (2) Differences: 31

  32. SET TESTS All of these return a bool: 32

  33. ADDITIONAL METHODS These have nothing to do with math, and

    all to do with practical computing: 33
  34. ABSTRACT SET INTERFACES These interfaces are all defined in collections.abc.

    set and frozenset both implement Set set also implements MutableSet 34
  35. OPERATOR OVERLOADING Not as bad as they say 35

  36. A DESIGNER’S DILEMMA 36

  37. 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
  38. COMPARISON OPERATORS 38

  39. 39

  40. THE BEAUTY OF DOUBLE DISPATCH 40

  41. CONCLUSION 41

  42. 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
  43. Luciano Ramalho
 @ramalhoorg | @standupdev
 luciano.ramalho@thoughtworks.com THANK YOU!