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.

Luciano Ramalho

May 03, 2018
Tweet

More Decks by Luciano Ramalho

Other Decks in Programming

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

    View Slide

  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

    View Slide

  3. MOTIVATION
    Some common use cases for sets
    3

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  12. LOGIC AND SETS
    A close relationship
    12

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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)

    View Slide

  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

    View Slide

  18. SETS IN SEVERAL
    LANGUAGES
    18

    View Slide

  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

    View Slide

  20. SETS IN PYTHON
    The built-in types
    20

    View Slide

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

    View Slide

  22. ANOTHER SET, FOR THE EXAMPLES
    22

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  27. DE MORGAN’S LAW: #1
    27

    View Slide

  28. DE MORGAN’S LAW: #2
    28

    View Slide

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

    View Slide

  30. SET OPERATORS AND METHODS (1)
    30

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. OPERATOR
    OVERLOADING
    Not as bad as they say
    35

    View Slide

  36. A DESIGNER’S DILEMMA
    36

    View Slide

  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

    View Slide

  38. COMPARISON OPERATORS
    38

    View Slide

  39. 39

    View Slide

  40. THE BEAUTY OF DOUBLE DISPATCH
    40

    View Slide

  41. CONCLUSION
    41

    View Slide

  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

    View Slide

  43. Luciano Ramalho

    @ramalhoorg | @standupdev

    [email protected]
    THANK YOU!

    View Slide