Things I wish I Knew Before Using Python for Data Processing - - PowerPoint PPT Presentation

things i wish i knew before using python for data
SMART_READER_LITE
LIVE PREVIEW

Things I wish I Knew Before Using Python for Data Processing - - PowerPoint PPT Presentation

Things I wish I Knew Before Using Python for Data Processing Miguel Cabrera @mfcabrera 20 July 2016 Hello! I am Miguel! Data Engineer / Scien6st @ TrustYou Python ~ 2 years Berlin


slide-1
SLIDE 1

Things I wish I Knew Before Using Python for Data Processing

¡

Miguel ¡Cabrera ¡ @mfcabrera ¡ ¡

20 ¡July ¡2016 ¡

slide-2
SLIDE 2

Hello!

I ¡am ¡Miguel! ¡

Data ¡Engineer ¡/ ¡Scien6st ¡@ ¡TrustYou ¡ Python ¡~ ¡2 ¡years ¡ ¡ Berlin ¡ ¡ ¡ @mfcabrera ¡ mfcabrera ¡at ¡gmail ¡ ¡ ¡ hAp://mfcabrera.com ¡ ¡

2

slide-3
SLIDE 3
  • (Rela6vely) ¡New ¡to ¡Python, ¡mostly ¡Scien6fic ¡

stack ¡

  • You ¡have ¡used ¡things ¡like ¡Numpy, ¡Scikit-­‑

Learn, ¡Gensim, ¡etc… ¡ ¡

  • Your ¡job ¡6tle ¡includes ¡either ¡the ¡word ¡Data ¡
  • r ¡“Machine ¡Learning”. ¡

¡

  • Not ¡necessarily ¡a ¡trained ¡SoWware ¡Engineer ¡

3

Priors!

slide-4
SLIDE 4
  • Data ¡Scien6st? ¡
  • Data ¡Analyst? ¡
  • Data ¡Engineer? ¡
  • Machine ¡Learning ¡Developer? ¡
  • SoWware ¡Developer? ¡
  • Other? ¡

4

Who is who?

slide-5
SLIDE 5
  • Basic ¡Concepts ¡and ¡prac6ces ¡
  • Some ¡goodies ¡of ¡the ¡collec6on ¡

module ¡

  • Iterators ¡and ¡Iterables ¡
  • Conclusion ¡ ¡

¡ ¡ ¡ ¡

5

Agenda

slide-6
SLIDE 6
  • Recent ¡university ¡grad ¡
  • Mostly ¡R ¡and ¡Matlab ¡ ¡
  • Writes ¡niWy ¡code ¡to ¡classify ¡

documents ¡using ¡Jupyter ¡ Notebooks ¡

  • Mostly ¡NltK ¡and ¡Scikit-­‑Learn ¡

¡

6

David’s Story

slide-7
SLIDE 7

7

slide-8
SLIDE 8

8

slide-9
SLIDE 9

9

slide-10
SLIDE 10

10

Data Science Spaghetti Code

slide-11
SLIDE 11

1.

Back to the basics

11

slide-12
SLIDE 12

Code vs Software

12 Daniel ¡Moisset: ¡hAps://www.youtube.com/watch?v=4dlWg0B4ASw ¡

slide-13
SLIDE 13

“Code is something that runs on a Computer”

13

slide-14
SLIDE 14

Code does not necessarily…

¡

  • Have ¡tests ¡ ¡
  • Follow ¡conven6ons ¡
  • Have ¡documenta6on ¡
  • Follow ¡processes ¡

14

slide-15
SLIDE 15

“Software is the programming text that is part of a deliverable”

15

slide-16
SLIDE 16

You want to build Software…

¡

  • Maintainable ¡
  • Testable ¡
  • Deployable ¡

16

slide-17
SLIDE 17

Python ¡is ¡an ¡interpreted, ¡ interac8ve, ¡object-­‑oriented ¡ programming ¡language. ¡It ¡ incorporates ¡modules, ¡excep8ons, ¡ dynamic ¡typing, ¡very ¡high ¡level ¡ dynamic ¡data ¡types, ¡and ¡classes. ¡ ¡

17

Python is…

Source: https://docs.python.org/3/faq/general.html#what-is-python

slide-18
SLIDE 18

(OOP) ¡is ¡a ¡programming ¡paradigm ¡ based ¡on ¡the ¡concept ¡of ¡"objects", ¡ which ¡may ¡contain ¡data, ¡in ¡the ¡ form ¡of ¡fields, ¡oGen ¡known ¡as ¡ a0ributes; ¡and ¡code, ¡in ¡the ¡form ¡of ¡ procedures, ¡oGen ¡known ¡as ¡

  • methods. ¡

18

Object Oriented Programming (OOP)

Source: https://en.wikipedia.org/wiki/Object-oriented_programming
slide-19
SLIDE 19

How does an object look in Python?

19

slide-20
SLIDE 20

How does an object look in Python?

20

Cookie Cutters & Cookies

slide-21
SLIDE 21

How does an object look in Python?

21

Classes & Objects

slide-22
SLIDE 22

22

class ¡Cookie(object): ¡ ¡def ¡__init__(self, ¡sugar=5): ¡ ¡ ¡self.sugar ¡= ¡sugar ¡ ¡def ¡eat(self): ¡ ¡ ¡pass ¡ ¡def ¡split(self): ¡ ¡ ¡pass ¡ ¡

slide-23
SLIDE 23

23

class ¡Cookie(object): ¡ ¡def ¡__init__(self, ¡sugar=5): ¡ ¡ ¡self.sugar ¡= ¡sugar ¡ ¡def ¡eat(self): ¡ ¡ ¡pass ¡ ¡def ¡split(self): ¡ ¡ ¡pass ¡ ¡

slide-24
SLIDE 24

24

class ¡Cookie(object): ¡ ¡def ¡__init__(self, ¡sugar=5): ¡ ¡ ¡self.sugar ¡= ¡sugar ¡ ¡def ¡eat(self): ¡ ¡ ¡pass ¡ ¡def ¡split(self): ¡ ¡ ¡pass ¡ ¡

slide-25
SLIDE 25

25

class ¡Cookie(object): ¡ ¡def ¡__init__(self, ¡sugar=5): ¡ ¡ ¡self.sugar ¡= ¡sugar ¡ ¡def ¡eat(self): ¡ ¡ ¡pass ¡ ¡def ¡split(self): ¡ ¡ ¡pass ¡

slide-26
SLIDE 26

26

class ¡Cookie(object): ¡ ¡def ¡__init__(self, ¡sugar=5): ¡ ¡ ¡self.sugar ¡= ¡sugar ¡ ¡def ¡eat(self): ¡ ¡ ¡pass ¡ ¡def ¡split(self): ¡ ¡ ¡pass ¡ ¡ c ¡= ¡Cookie(3) ¡

slide-27
SLIDE 27

27

class ¡Alfajor(Cookie): ¡ ¡ ¡def ¡__init__(self, ¡chocolate=10, ¡sugar=10): ¡ ¡super(Alfajor, ¡self).__init__(sugar=sugar) ¡ ¡self.chocolate ¡= ¡chocolate ¡ ¡ a ¡= ¡Alfajor(chocolate=20, ¡sugar=30) ¡

slide-28
SLIDE 28

28

from ¡sklearn ¡import ¡svm ¡ data ¡ ¡= ¡# ¡multiple ¡lines ¡to ¡load ¡the ¡data ¡ X ¡= ¡# ¡multiples ¡lines ¡extract ¡the ¡features ¡ y ¡= ¡# ¡... ¡ clf ¡= ¡svm.SVC() ¡ clf.fit(X, ¡y) ¡ clf.predict(...) ¡ # ¡multiples ¡lines ¡store ¡the ¡results ¡

slide-29
SLIDE 29

How do I write good

  • bject oriented code?

29

slide-30
SLIDE 30

How do I write good OO code?

¡

  • DRY ¡
  • KISS ¡
  • SOLID ¡

30

slide-31
SLIDE 31

Every ¡piece ¡of ¡knowledge ¡must ¡ have ¡a ¡single, ¡unambiguous, ¡ authorita8ve ¡representa8on ¡within ¡ a ¡system ¡

31

Source: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself

DRY: Don’t Repeat Yourself

slide-32
SLIDE 32

Simplicity ¡is ¡the ¡ul8mate ¡ sophis8ca8on ¡

32

Leonardo Davicni

KISS: Keep it Simple Stupid

slide-33
SLIDE 33
  • Single ¡responsibility ¡principle ¡
  • Open/closed ¡principle ¡
  • Liskov ¡subs6tu6on ¡principle ¡
  • Interface ¡segrega6on ¡principle ¡
  • Dependency ¡inversion ¡principle ¡

¡

33

Be SOLID

“Principles Of OOD”, Robert C. Martin Source: https://es.wikipedia.org/wiki/SOLID

slide-34
SLIDE 34
  • Single ¡responsibility ¡principle ¡
  • Open/closed ¡principle ¡
  • Liskov ¡subs6tu6on ¡principle ¡
  • Interface ¡segrega6on ¡principle ¡
  • Dependency ¡inversion ¡principle ¡

¡

34

Be SOLID

“Principles Of OOD”, Robert C. Martin Source: https://es.wikipedia.org/wiki/SOLID

slide-35
SLIDE 35

Learn OOP in Python

35

slide-36
SLIDE 36

36

Coding Conventions

slide-37
SLIDE 37
  • “Readability ¡counts” ¡(PEP20) ¡
  • Spaces ¡vs. ¡Tabs ¡
  • Indenta6on ¡rules ¡
  • Code ¡organiza6on ¡
  • PEP-­‑8 ¡is ¡the ¡de-­‑facto ¡style ¡

¡ ¡

37

Coding Conventions

slide-38
SLIDE 38

38

PEP-8

hAp://pep8.org ¡ ¡

slide-39
SLIDE 39

39

slide-40
SLIDE 40
  • Project ¡structure ¡
  • Tes6ng ¡(Check ¡out ¡py.test!) ¡
  • Versioning ¡and ¡branching ¡ ¡
  • Code ¡Reviews ¡
  • SoWware ¡Development ¡Life ¡Cycle ¡

40

Other Topics

slide-41
SLIDE 41

Books!

41

slide-42
SLIDE 42

Talks @ Europython

42

¡

  • Clean ¡Code ¡in ¡Python ¡by ¡Mariano ¡Anaya ¡ ¡

(Today ¡at ¡15:45 ¡Barria ¡2) ¡

  • What’s ¡the ¡point ¡of ¡Object ¡Orienta6on? ¡

by ¡Iwan ¡Vosloo ¡(Thursday ¡11:15 ¡A2) ¡

¡

slide-43
SLIDE 43

2.

Tips & Tricks

43

slide-44
SLIDE 44

2.

Tips & Tricks

The ¡Collec6on ¡Module ¡

44

slide-45
SLIDE 45

45

Counting

slide-46
SLIDE 46

First Attempt

46

slide-47
SLIDE 47

47

items ¡= ¡["a", ¡"b", ¡"a", ¡"x", ¡"x", ¡"y", ¡"c", ¡"c", ¡ "a"] ¡ ¡ item_counts ¡= ¡{} ¡ ¡ for ¡i ¡in ¡items: ¡ ¡ ¡ ¡ ¡if ¡i ¡in ¡items: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡item_counts[i] ¡= ¡item_counts[i] ¡+ ¡1 ¡ ¡ ¡ ¡ ¡else: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡item_counts[i] ¡= ¡1 ¡

Using dicts!

slide-48
SLIDE 48

48

items ¡= ¡["a", ¡"b", ¡"a", ¡"x", ¡"x", ¡"y", ¡"c", ¡"c", ¡ "a"] ¡ ¡ item_counts ¡= ¡{} ¡ ¡ for ¡i ¡in ¡items: ¡ ¡ ¡ ¡ ¡try: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡item_counts[i] ¡= ¡item_counts[i] ¡+ ¡1 ¡ ¡ ¡ ¡ ¡except ¡KeyError: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡item_counts[i] ¡= ¡1 ¡

Using dicts (EAFP version)

slide-49
SLIDE 49

We can do better

49

slide-50
SLIDE 50

Let’s use defaultdict ¡

50

slide-51
SLIDE 51

51

from ¡collections ¡import ¡defaultdict ¡ ¡ items ¡= ¡["a", ¡"b", ¡"a", ¡"x", ¡"x", ¡"y", ¡"c", ¡"c", ¡ "a"] ¡ ¡ item_counts ¡= ¡defaultdict(int) ¡ ¡ for ¡i ¡in ¡items: ¡ ¡ ¡ ¡ ¡item_counts[i] ¡= ¡item_counts[i] ¡+ ¡1 ¡ ¡ defaultdict(int, ¡{'a': ¡3, ¡'b': ¡1, ¡'c': ¡2, ¡'x': ¡2, ¡ 'y': ¡1}) ¡ ¡ ¡

Using defaultdict

slide-52
SLIDE 52

Let’s use Counter ¡

52

slide-53
SLIDE 53

53

from ¡collections ¡import ¡Counter ¡ ¡ items ¡= ¡["a", ¡"b", ¡"a", ¡"x", ¡"x", ¡"y", ¡"c", ¡"c", ¡ "a"] ¡ ¡ item_counts ¡= ¡Counter(items) ¡ print(item_counts) ¡ ¡

Using Counter

Counter({'a': ¡3, ¡'x': ¡2, ¡'c': ¡2, ¡'b': ¡1, ¡'y': ¡1}) ¡

slide-54
SLIDE 54

Counter’s extra goodies ¡

54

slide-55
SLIDE 55

55

Extra goodies

>>> ¡c ¡= ¡Counter(a=3, ¡b=1) ¡ >>> ¡d ¡= ¡Counter(a=1, ¡b=2) ¡ >>> ¡c.most_common() ¡ ¡ >>> ¡c.values() ¡ >>> ¡c ¡+ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ >>> ¡c ¡-­‑ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ >>> ¡c ¡& ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡intersection: ¡ ¡min(c[x], ¡ d[x]) ¡ >>> ¡c ¡| ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡union: ¡ ¡max(c[x], ¡d[x]) ¡

slide-56
SLIDE 56

Counter is a class ¡

56

slide-57
SLIDE 57

Classes can be extended ¡

57

slide-58
SLIDE 58

58

Counter based PMF

from ¡collections ¡import ¡Counter ¡ ¡ ¡ class ¡PMF(Counter): ¡ ¡ ¡ ¡ ¡def ¡normalize(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡total ¡= ¡float(sum(self.values())) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡key ¡in ¡self: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self[key] ¡/= ¡total ¡

slide-59
SLIDE 59

59

Counter based PMF

from ¡collections ¡import ¡Counter ¡ ¡ ¡ class ¡PMF(Counter): ¡ ¡ ¡ ¡ ¡def ¡normalize(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡total ¡= ¡float(sum(self.values())) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡key ¡in ¡self: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self[key] ¡/= ¡total ¡ ¡ ¡ ¡ ¡ ¡def ¡__init__(self, ¡*args, ¡**kwargs): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡super(PMF, ¡self).__init__(…) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.normalize() ¡

slide-60
SLIDE 60
  • Use ¡the ¡and ¡extend ¡Counter ¡

class ¡

  • Awesome ¡ar6cle ¡from ¡

@TreyHunner ¡on ¡Coun6ng ¡[1]. ¡ ¡

¡ 60

On Counting and Python

[1] http://treyhunner.com/2015/11/counting-things-in-python/

slide-61
SLIDE 61

Named Tuples

61

slide-62
SLIDE 62
  • Code ¡around ¡the ¡dict, ¡tuples ¡or ¡

lists ¡

  • Never ¡know ¡what ¡to ¡expect ¡
  • Code ¡becomes ¡hard ¡to ¡read ¡

¡

62

Named Tuples

slide-63
SLIDE 63

63

Example

from ¡math ¡import ¡sqrt ¡ pt1 ¡= ¡(1.0, ¡5.0) ¡ pt2 ¡= ¡(2.5, ¡1.5) ¡ ¡ ¡ line_length ¡= ¡sqrt((pt1[0] ¡-­‑ ¡pt2[0])**2 ¡+ ¡(pt1[1] ¡

  • ­‑ ¡pt2[1])**2) ¡

Source: ¡hAp://stackoverflow.com/ques6ons/2970608/what-­‑are-­‑named-­‑tuples-­‑in-­‑python ¡

slide-64
SLIDE 64

64

Example

¡ ¡ ¡ ¡ ¡ line_length ¡= ¡sqrt((pt1[0] ¡-­‑ ¡pt2[0])**2 ¡+ ¡(pt1[1] ¡

  • ­‑ ¡pt2[1])**2) ¡

Source: ¡hAp://stackoverflow.com/ques6ons/2970608/what-­‑are-­‑named-­‑tuples-­‑in-­‑python ¡

slide-65
SLIDE 65

65

Example

from ¡collections ¡import ¡namedtuple ¡ from ¡math ¡import ¡sqrt ¡ Point ¡= ¡namedtuple('Point', ¡'x ¡y') ¡ pt1 ¡= ¡Point(1.0, ¡5.0) ¡ pt2 ¡= ¡Point(2.5, ¡1.5) ¡ ¡ line_length ¡= ¡sqrt((pt1.x ¡-­‑ ¡pt2.x)**2 ¡+ ¡(pt1.y ¡-­‑ ¡ pt2.y)**2) ¡

Source: ¡hAp://stackoverflow.com/ques6ons/2970608/what-­‑are-­‑named-­‑tuples-­‑in-­‑python ¡

slide-66
SLIDE 66

66

It has cool methods

_asdict

Return ¡a ¡new ¡OrderedDict ¡which ¡ maps ¡field ¡names ¡to ¡their ¡values ¡ _make(iterable) Class ¡method ¡that ¡makes ¡a ¡new ¡ instance ¡from ¡an ¡exis6ng ¡sequence ¡

  • r ¡iterable. ¡
slide-67
SLIDE 67

67

Extending NamedTuple

from ¡collections ¡import ¡namedtuple ¡ ¡ _HotelBase ¡= ¡namedtuple( ¡ ¡ ¡ ¡ ¡ ¡ ¡'HotelDescriptor', ¡ ¡ ¡ ¡ ¡ ¡ ¡['cluster_id', ¡'trust_score', ¡'reviews_count', ¡ ¡ ¡'category_scores', ¡'intensity_factors'], ¡ ) ¡ ¡ class ¡HotelDescriptor(_HotelBase): ¡ ¡ ¡ ¡ ¡def ¡compute_prior(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡not ¡self.trust_score ¡or ¡not ¡self.reviews_count: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡raise ¡NotEnoughDataForRanking("…") ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡_compute_prior(self.trust_score,…) ¡

slide-68
SLIDE 68

2.

Tips & Tricks

2.1 ¡Iterators ¡& ¡Iterables ¡

68

slide-69
SLIDE 69

69

l ¡= ¡[1, ¡2, ¡3, ¡4] ¡ for ¡i ¡in ¡x: ¡ ¡print(x) ¡

slide-70
SLIDE 70

70

an ¡iterator ¡

comprehension ¡ (an) ¡iterable ¡ produces ¡ typically ¡is ¡

iter() ¡

always ¡is ¡ a ¡generator ¡ ¡ expression ¡ a ¡generator ¡ ¡ funcCon ¡

is ¡ is ¡

a ¡generator ¡

container ¡ (list, ¡dict, ¡etc) ¡ ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

By Vincent Driessen - Source: http://nvie.com/posts/iterators-vs-generators/

slide-71
SLIDE 71

71

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

slide-72
SLIDE 72

72

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡

slide-73
SLIDE 73

73

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡

slide-74
SLIDE 74

74

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ container ¡ (list, ¡dict, ¡etc) ¡

slide-75
SLIDE 75

75

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ container ¡ (list, ¡dict, ¡etc) ¡

assert ¡1 ¡in ¡[1, ¡2, ¡3] ¡ ¡

slide-76
SLIDE 76

76

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ container ¡ (list, ¡dict, ¡etc) ¡ ¡

assert ¡1 ¡in ¡{1, ¡2, ¡3} ¡ ¡

slide-77
SLIDE 77

77

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ typically ¡is ¡ container ¡ (list, ¡dict, ¡etc) ¡ ¡

slide-78
SLIDE 78

78

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ typically ¡is ¡ container ¡ (list, ¡dict, ¡etc) ¡ ¡

l ¡= ¡[1, ¡2, ¡3, ¡4] ¡ x ¡= ¡iter(l) ¡ y ¡= ¡iter(l) ¡ ¡

¡

slide-79
SLIDE 79

79

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ typically ¡is ¡ container ¡ (list, ¡dict, ¡etc) ¡ ¡

l ¡= ¡[1, ¡2, ¡3, ¡4] ¡ x ¡= ¡iter(l) ¡ y ¡= ¡iter(l) ¡ ¡ type(l) ¡

>> ¡<class ¡'list'> ¡

type(x) ¡

>> ¡<class ¡'list_iterator'> ¡

slide-80
SLIDE 80

80

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ typically ¡is ¡ container ¡ (list, ¡dict, ¡etc) ¡ ¡

l ¡= ¡[1, ¡2, ¡3, ¡4] ¡ x ¡= ¡iter(l) ¡ y ¡= ¡iter(l) ¡ ¡ type(l) ¡

>> ¡<class ¡'list'> ¡

type(x) ¡

>> ¡<class ¡'list_iterator'> ¡

¡ next(x) ¡

>> ¡1 ¡

next(y) ¡

>> ¡1 ¡

next(y) ¡

>> ¡2 ¡

slide-81
SLIDE 81

81

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ typically ¡is ¡ container ¡ (list, ¡dict, ¡etc) ¡ ¡

l ¡= ¡[1, ¡2, ¡3, ¡4] ¡ for ¡e ¡in ¡l: ¡ ¡print(e) ¡

slide-82
SLIDE 82
  • An ¡iterable ¡is ¡any ¡object ¡that ¡can ¡

return ¡an ¡iterator ¡

  • Containers, ¡ ¡files, ¡sockets, ¡etc. ¡
  • Implement ¡__iter__(). ¡
  • Some ¡of ¡them ¡may ¡be ¡infinite ¡
  • The ¡itertools ¡contain ¡many ¡

helper ¡func6ons ¡

82

Iterables

slide-83
SLIDE 83

83

class ¡InverseReader(object): ¡ ¡ ¡ ¡ ¡def ¡__init__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡with ¡open('file.txt') ¡as ¡f: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.lines ¡= ¡f.readlines() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.index ¡= ¡len(self.lines) ¡-­‑ ¡1 ¡ ¡ ¡ ¡ ¡ ¡def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡self ¡ ¡ ¡ ¡ ¡ ¡def ¡next(self): ¡ ¡# ¡Python ¡3 ¡__next__ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.index ¡-­‑= ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡self.index ¡< ¡0: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡raise ¡StopIteration ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡self.lines[self.index] ¡

slide-84
SLIDE 84

84

ir ¡= ¡InverseReader() ¡ ¡ for ¡line ¡in ¡ir: ¡ ¡ ¡ ¡ ¡print ¡line ¡

slide-85
SLIDE 85

85

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ typically ¡is ¡ container ¡

is ¡ is ¡

a ¡generator ¡

always ¡is ¡ a ¡generator ¡ ¡ expression ¡ a ¡generator ¡ ¡ funcCon ¡

slide-86
SLIDE 86

86

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

a ¡generator ¡ ¡ expression ¡

slide-87
SLIDE 87

87

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

a ¡generator ¡ ¡ expression ¡ a ¡generator ¡ ¡ funcCon ¡

slide-88
SLIDE 88

88

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

a ¡generator ¡ ¡ expression ¡ a ¡generator ¡ ¡ funcCon ¡

slide-89
SLIDE 89

89

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

is ¡ is ¡

a ¡generator ¡

a ¡generator ¡ ¡ expression ¡ a ¡generator ¡ ¡ funcCon ¡

slide-90
SLIDE 90

90

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

is ¡ is ¡

a ¡generator ¡

always ¡is ¡ a ¡generator ¡ ¡ expression ¡ a ¡generator ¡ ¡ funcCon ¡

slide-91
SLIDE 91

91 is ¡

a ¡generator ¡ ¡ funcCon ¡

is ¡

a ¡generator ¡ ¡ expression ¡

a ¡generator ¡

slide-92
SLIDE 92

92 is ¡

a ¡generator ¡ ¡ expression ¡

numbers ¡= ¡[x ¡for ¡x ¡in ¡range(1, ¡10)] ¡ squares ¡= ¡[x ¡* ¡x ¡for ¡x ¡in ¡numbers] ¡ type(squares) ¡ # ¡list ¡ a ¡generator ¡

slide-93
SLIDE 93

93 is ¡

a ¡generator ¡ ¡ expression ¡

numbers ¡= ¡[x ¡for ¡x ¡in ¡range(1, ¡10)] ¡ squares ¡= ¡[x ¡* ¡x ¡for ¡x ¡in ¡numbers] ¡ type(squares) ¡ # ¡list ¡ ¡ lazy_squares ¡= ¡(x ¡* ¡x ¡for ¡x ¡in ¡numbers) ¡ lazy_squares ¡ # ¡<generator ¡object ¡<genexpr> ¡at ¡ 0x104c6da00> ¡ a ¡generator ¡

slide-94
SLIDE 94

94 is ¡

a ¡generator ¡ ¡ expression ¡

numbers ¡= ¡[x ¡for ¡x ¡in ¡range(1, ¡10)] ¡ squares ¡= ¡[x ¡* ¡x ¡for ¡x ¡in ¡numbers] ¡ type(squares) ¡ # ¡list ¡ ¡ lazy_squares ¡= ¡(x ¡* ¡x ¡for ¡x ¡in ¡numbers) ¡ lazy_squares ¡ # ¡<generator ¡object ¡<genexpr> ¡at ¡ 0x104c6da00> ¡ ¡ next(lazy_squares) ¡ # ¡1 ¡ next(lazy_squares) ¡ # ¡4 ¡ x ¡ a ¡generator ¡

slide-95
SLIDE 95

95 is ¡

a ¡generator ¡ ¡ expression ¡

numbers ¡= ¡[x ¡for ¡x ¡in ¡range(1, ¡10)] ¡ squares ¡= ¡[x ¡* ¡x ¡for ¡x ¡in ¡numbers] ¡ type(squares) ¡ # ¡list ¡ ¡ lazy_squares ¡= ¡(x ¡* ¡x ¡for ¡x ¡in ¡numbers) ¡ lazy_squares ¡ # ¡<generator ¡object ¡<genexpr> ¡at ¡ 0x104c6da00> ¡ ¡ next(lazy_squares) ¡ # ¡1 ¡ next(lazy_squares) ¡ # ¡4 ¡ ¡ lazy_squares ¡= ¡(x ¡* ¡x ¡for ¡x ¡in ¡numbers) ¡ for ¡x ¡in ¡lazy_squares: ¡ ¡print ¡x ¡ ¡ ¡ a ¡generator ¡

slide-96
SLIDE 96

96

a ¡generator ¡

is ¡

a ¡generator ¡ ¡ funcCon ¡

slide-97
SLIDE 97

97

a ¡generator ¡

def ¡fib(): ¡ ¡ ¡prev, ¡curr ¡= ¡0,1 ¡ ¡ ¡while ¡True: ¡ ¡ ¡ ¡ ¡yield ¡curr ¡ ¡ ¡ ¡ ¡prev,curr ¡= ¡curr, ¡prev+curr ¡ ¡ ¡

is ¡

a ¡generator ¡ ¡ funcCon ¡

slide-98
SLIDE 98

98

a ¡generator ¡

def ¡fib(): ¡ ¡ ¡prev, ¡curr ¡= ¡0,1 ¡ ¡ ¡while ¡True: ¡ ¡ ¡ ¡ ¡yield ¡curr ¡ ¡ ¡ ¡ ¡prev,curr ¡= ¡curr, ¡prev+curr ¡ ¡ f ¡= ¡fib() ¡ ¡ next(f) ¡ # ¡1 ¡ next(f) ¡ # ¡1 ¡ next(f) ¡ # ¡2 ¡ ¡

is ¡

a ¡generator ¡ ¡ funcCon ¡

slide-99
SLIDE 99

99

a ¡generator ¡

def ¡fib(): ¡ ¡ ¡prev, ¡curr ¡= ¡0,1 ¡ ¡ ¡while ¡True: ¡ ¡ ¡ ¡ ¡yield ¡curr ¡ ¡ ¡ ¡ ¡prev,curr ¡= ¡curr, ¡prev+curr ¡ ¡ ¡

is ¡

a ¡generator ¡ ¡ funcCon ¡

for ¡x ¡in ¡islice(fib(), ¡0,3): ¡ ¡ ¡ ¡ ¡print ¡x ¡ # 1 # 1 # 2 ¡

slide-100
SLIDE 100

100

class ¡HdfsLineSentence(object): ¡ ¡ ¡ ¡ ¡def ¡__init__(self, ¡source): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.source ¡= ¡source ¡ ¡ ¡ ¡ ¡ ¡def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡stream ¡= ¡self.source.open('r') ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡line ¡in ¡stream: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cid, ¡s ¡= ¡line.split('\t') ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡decode ¡and ¡do ¡some ¡work ¡with ¡s ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡yield ¡s ¡ ¡ sentences ¡= ¡HdfsLineSentence(...) ¡ for ¡s ¡in ¡setences: ¡ ¡ ¡ ¡ ¡print(s) ¡

slide-101
SLIDE 101

101

an ¡iterator ¡

(an) ¡iterable ¡

iter() ¡

always ¡is ¡ next() ¡

Lazily ¡produce ¡ the ¡next ¡value ¡

comprehension ¡ produces ¡ typically ¡is ¡ container ¡

is ¡ is ¡

a ¡generator ¡

always ¡is ¡ a ¡generator ¡ ¡ expression ¡ a ¡generator ¡ ¡ funcCon ¡

slide-102
SLIDE 102

What does it have to do with Data Processing? ¡

102

slide-103
SLIDE 103

Unknown amout of data ¡

103

slide-104
SLIDE 104

Not enough memory ¡

104

slide-105
SLIDE 105

Data streaming via lazy evaluation ¡

105

slide-106
SLIDE 106

Data processing pipelines through iterables ¡

106

slide-107
SLIDE 107

Chaining iterables ¡

107

slide-108
SLIDE 108

108

class ¡HdfsLineSentence(object): ¡ ¡ ¡ ¡ ¡def ¡__init__(self, ¡source): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.source ¡= ¡source ¡ ¡ ¡ ¡ ¡ ¡def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡stream ¡= ¡self.source.open('r') ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡line ¡in ¡stream: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cid, ¡s ¡= ¡line.split('\t') ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡decode ¡and ¡do ¡some ¡work ¡with ¡s ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡yield ¡s ¡ ¡ sentences ¡= ¡HdfsLineSentence(...) ¡ for ¡s ¡in ¡sentences: ¡ ¡ ¡ ¡ ¡print ¡s ¡

slide-109
SLIDE 109

109

class ¡FilterComment(object): ¡ ¡ ¡ ¡ ¡def ¡__init__(self, ¡source): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.source ¡= ¡source ¡ ¡ ¡ ¡ ¡ ¡def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡s ¡in ¡self.source: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡s[0] ¡!= ¡"#": ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡yield ¡s ¡ ¡ ¡

slide-110
SLIDE 110

110

class ¡FilterComment(object): ¡ ¡ ¡ ¡ ¡def ¡__init__(self, ¡source): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.source ¡= ¡source ¡ ¡ ¡ ¡ ¡ ¡def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡s ¡in ¡self.source: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡s[0] ¡!= ¡"#": ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡yield ¡s ¡ ¡ ¡ sents ¡= ¡FilterComment(HdfsLineSentence(source)) ¡ for ¡s ¡in ¡sents: ¡ ¡print ¡s ¡ ¡

slide-111
SLIDE 111

111

class ¡FilterComment(object): ¡ ¡ ¡ ¡ ¡def ¡__init__(self, ¡source): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.source ¡= ¡source ¡ ¡ ¡ ¡ ¡ ¡def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡s ¡in ¡self.source: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡s[0] ¡!= ¡"#": ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡yield ¡s ¡ ¡ ¡ sents ¡= ¡FilterComment(HdfsLineSentence(source)) ¡ for ¡s ¡in ¡sents: ¡ ¡print ¡s ¡ ¡

slide-112
SLIDE 112

112

class ¡FilterComment(object): ¡ ¡ ¡ ¡ ¡def ¡__init__(self, ¡source): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.source ¡= ¡source ¡ ¡ ¡ ¡ ¡ ¡def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡s ¡in ¡self.source: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡s[0] ¡!= ¡"#": ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡yield ¡s ¡ ¡ ¡ sents ¡= ¡FilterComment(HdfsLineSentence(source)) ¡ for ¡s ¡in ¡sents: ¡ ¡print ¡s ¡ ¡

slide-113
SLIDE 113

113

def ¡filter_comment(source): ¡ ¡ ¡ ¡ ¡for ¡s ¡in ¡source: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡s[0] ¡!= ¡"#": ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡yield ¡s ¡ ¡ sents ¡= ¡filter_comment(HdfsLineSentence(source)) ¡ ¡ for ¡s ¡in ¡sents: ¡ ¡print ¡s ¡ ¡

slide-114
SLIDE 114

Talks @ Europython

114

¡

  • Itera6on, ¡itera6on, ¡itera6on ¡by ¡John ¡

Sutherland ¡(Friday ¡15:45 ¡Barria ¡1) ¡

slide-115
SLIDE 115

3.

Conclusions

115

slide-116
SLIDE 116

Data Scientists / Engineers / ML Developers should learn…

116

slide-117
SLIDE 117

collections and itertools ¡ modules

117

slide-118
SLIDE 118

Iterables ¡and ¡iterators ¡for ¡data ¡ processing ¡pipelines ¡

118

slide-119
SLIDE 119

Object ¡oriented ¡programming ¡

119

slide-120
SLIDE 120

Good ¡soWware ¡engineering ¡ prac6ces ¡

120

slide-121
SLIDE 121

121 Spaguetti: https://www.flickr.com/photos/129610671@N02/16633987421/ (CC BY- NC-ND 2.0) vision.communicate Autovification: Credit: AV Dezign https://www.flickr.com/photos/91345457@N07/22666878846/ (CC BY-NC-ND 2.0) Iterators and Iterables based on work of Vincent Driessen : http://nvie.com/posts/iterators-vs-generators/ Ideas from Iterables taken from RaRe Technologies blog; http://rare-technologies.com/data-streaming- in-python-generators-iterators-iterables/

Credits

Counter image: Dean Hochman Source: https://www.flickr.com/photos/17997843@N02/24061690099/“ (CC BY-NC-ND 2.0) PMF Class based on Vik Paruchuri’s https://www.dataquest.io/blog/python-counter-class/ Cookies: Source Wikipedia https://en.wikipedia.org/wiki/File:R%C5%AFzn%C3%A9_druhy_cukrov%C3%AD_(2).jpg (CC BY 3.0) Counting things in Python: http://treyhunner.com/2015/11/counting-things-in-python/

slide-122
SLIDE 122

We are Hiring!

Visit ¡our ¡table ¡for ¡more ¡info! ¡

slide-123
SLIDE 123

Thanks!

Any ¡quesCons? ¡

You ¡can ¡find ¡me ¡at: ¡ @mfcabrera ¡ mfcabrera@gmail.com ¡ ¡

123