1 of 41

Friendly Python Classes

Ąžuolas Krušna | Nov 30 2024

Dataclasses, Operators, Composition

2 of 41

2

Agenda

  1. Why do we need classes?
  2. Instruments to create Friendly Classes
  3. Definition of a Friendly Class
  4. Examples of forging a friendly metric
  5. Practice | Expand the functionality of the metric
  6. Practice | Build a minimalistic analytical Cube

3 of 41

3

Why do we need classes?

  • No need
  • Help to organize code
  • Library is built with classes
    1. Don’t inherit, e.g. Pandas
    2. Inherit, e.g. Django

4 of 41

4

What instruments will we use to create a Friendly Class?

  • Dataclasses
  • Operator overloading
  • Composition over Inheritance

5 of 41

5

So how are these Friendly Python Classes?

  1. Easy to understand and to talk to — easy to read and write
  2. Understand each other without words — use mathematical operators*
  3. Easy to reach — easy to call

6 of 41

  1. Easy to understand and to talk to — easy to read and write

6

7 of 41

7

  1. Traditional way
  1. 0xa5739s987d8f9
  2. MetricV1Revenue
  3. MetricV1
  4. <__main__.MetricV1 at 0x7e2ae0123df0

module

class

address in the memory

8 of 41

8

  1. Friendlier

9 of 41

9

  1. Utilizing dataclasses

type-hints are necessary

10 of 41

10

  1. Default values in dataclasses

11 of 41

11

  1. Static class variables in dataclasses

12 of 41

12

  1. What about __init__?

13 of 41

  • Understand each other without words — use mathematical operators

13

14 of 41

14

  1. Let’s divide!

15 of 41

15

  1. Division example 1

16 of 41

16

  1. Division example 2

17 of 41

  • Easy to reach — easy to call

17

18 of 41

18

  1. Composition
  1. Inheritance

19 of 41

19

  1. Inherited metrics

20 of 41

20

  1. Inherited metric objects

21 of 41

21

  1. Inherited metric objects 2

22 of 41

22

  1. Quick and simple; debug support
  1. (+ - * /) — Revenue / Sessions returns RevenuePerSessions

Key Takeaway. Friendly Class

23 of 41

Practice time!

23

24 of 41

24

Division by metrics and numbers

25 of 41

25

Division by a number example

26 of 41

26

Division by strings

27 of 41

27

Number division by a metric

28 of 41

28

Number division by a metric illustration

3.14 / MetricRevenue =>

29 of 41

29

Number division by a metric example

30 of 41

30

Multiplication and division of metrics and numbers

31 of 41

31

RPM metric

32 of 41

32

Bounced sessions metric

33 of 41

33

Dimensions

34 of 41

34

Analytical Cube

35 of 41

35

Minerva Cube

36 of 41

36

SQL query

37 of 41

37

Summary

  • Why do we need classes? — no need but they help organizing code and sometimes libraries enforce it
  • Instruments to create Friendly Classes — dataclasses, operators, composition
  • Definition of Friendly Class — easy to understand and talk to, understands without words*, easy to reach
  • Examples of forging a friendly metric
  • Practice | Expanded the functionality of the metric
  • Practice | Built a minimalistic Analytical Cube

38 of 41

38

  1. Luciano Ramalho (2022). Fluent Python: Clear, Concise, and Effective Programming (2nd ed.). O’Reilly. https://www.oreilly.com/library/view/fluent-python-2nd/9781492056348/
  2. Brandon Rhodes. The Composition Over Inheritance Principle https://python-patterns.guide/gang-of-four/composition-over-inheritance/

Literature

39 of 41

39

Bonus exercises. Operators

  • Implement Metrics addition
  • Implement Metrics subtraction
  • Create a derivative Metric based on at least 2 mathematical operations with other Metrics
  • Implement Metrics addition with numbers
  • Implement Metrics subtraction with numbers

40 of 41

40

Run the code yourself

  • Google Colab Notebook can be found at GitHub @AzisK https://github.com/AzisK

41 of 41

41

Could you benefit from Friendly Classes?

Let’s chat!