introduciton to python

51
Creating Big Data: Introduction to Python Moshe Kaplan [email protected]

Upload: moshe-kaplan

Post on 16-Apr-2017

177 views

Category:

Technology


0 download

TRANSCRIPT

Creating Big Data: Introduction to Python

Moshe [email protected]

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Moshe Kaplan> Scale Hacker:Architecture (Sys and Data)R&DSolving Problems> Blogs:http://top-performance.blogspot.comhttp://blogs.microsoft.co.il/vprnd

2

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

About Me: It’s all About Scale

3

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

INTRODUCTIONBackground

4

http://geekandpoke.typepad.com/geekandpoke/2008/07/one-day-in-the.html

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Wny Python?Simple and readable

Indentation: hard to messOOP and functionalDynamic and auto memory managementCan be packagedCross platformWide number of librariesWorks great w/ Java, C, .Net

5

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Who is Behind?Python Software Foundation~1M USD annual budget Created at 1989Top 10 from 2008

6

https://en.wikipedia.org/wiki/Python_(programming_language)

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Who is Using?

7

http://www.dhruvb.com/blog/images/slides/python/using-python.png

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Python Version2.X Existing Projects3.X New ProjectsNot so backwards compatible

8

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Materials

9

https://www.tocode.co.il10% DiscountCoupon Code: PyApplied

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

IDEsYou can use Notepad++

10

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

HELLO WORLDBackground

11

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com12

http://www.dhruvb.com/blog/presentations/python/#/2

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com13

http://www.dhruvb.com/blog/presentations/python/#/2

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

VARIABLES, NUMBERS AND STRINGSPython

14

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

C:\>PythonType "help", "copyright", "credits" or "license" for more information.>>> a = "hello">>> print(a)hello>>> print(type(a))<class 'str'>>>> a = 1>>> print(type(a))<class 'int'>>>>

Variables

15

Auto Declaration

Auto type assignment

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

NumbersInteger (int)FloatingComplex (a+ib)

16

http://www.slideshare.net/TahaniAlManie/python-3-programming-language

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

StringsImmutable (cannot modify)Char is a single char stringStarts w/ 0 and support –N

17

>>> a = "hello">>> a[0]'h'>>> a[-1]'o'

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Strings Methods

18

http://www.slideshare.net/TahaniAlManie/python-3-programming-language

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Strings Operators

19

http://www.slideshare.net/TahaniAlManie/python-3-programming-language

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

LISTS AND TUPLESPython

20

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Lists BasicsMutableSupport multi typesAgain 0–N

21

>>> l = [1, 'b', 'ccc']>>> print(l[0])1>>> print(l[1:2]);['b']>>> l[2] = 3>>> print(l)>>> del(l[0])>>> print(l)['b', 3]

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Lists MagicSupports list in a listFor each:

22

>>> a = [1, 2, 3]>>> [x ** 2 for x in a][1, 4, 9]

http://www.slideshare.net/TahaniAlManie/python-3-programming-language

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

TuplesSimilar functionality to listsImmutable (cannot modify)Faster than listsPerfect for functional programming

23

>>> t = (1, 'b', 'ccc')>>> t[0] = 2Traceback (most recent call last):File "<stdin>", line 1, in <module>

TypeError: 'tuple' object does not support item assignment

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Dictionaries BasicsKey/Value pairs (JSON like)Mutable (cannot modify)

24

>>> d = {'name':'moshe', 'course':'PySpark'}>>> print(d['name'])moshe>>> print(d.keys())dict_keys(['name', 'course'])>>> print(d.values())dict_values(['moshe', 'PySpark'])>>> print(d.items())dict_items([('name', 'moshe'), ('course', 'PySpark')])

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Dictionaries Methods

25

http://www.slideshare.net/TahaniAlManie/python-3-programming-language

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

FUNCTIONSPython

26

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Functions BasicsFlexible parameters:

27

>>> def a(name = "moshe", course = "pycourse"):... print(name + ": " + course)...>>> a()moshe: pycourse>>> a("roni")roni: pycourse>>> a(course = "spark")moshe: spark

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Variable Length Parameters and RefsNot ByVal and not ByRef:

Mutuable: Object Reference (array)Immutable: Change pointer each time (string)

28

>>> def f(*tup):... for t in tup:... print(t)...>>>>>>>>> f(1, "b", "ccc")1bccc

is vs ==

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

IF STATEMENTS AND LOOPSPython

29

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

ifTrue or False conditions (no null)Non Null TrueNon Zero True

30

>>> b = True>>> if b:... print(1)... elif b != True:... print(2)... else:... print(3)1>>> x = 'correct' if b == True else 'wrong'

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

For Loops: Strings

31

>>> for letter in 'python':... print(letter)python

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

For Loops: For Each

32

>>> for x in range(1, 5):... print(x)...1234

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Whilecontinue: Well, continuebreak: Well, breakpass: Void, due to indentation

33

>>> while (x < 5):... print(x)... x = x + 101234

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

INPUT AND BASIC TERMINAL APPSPython

34

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Filesf = open(“file_name” [, access_mode] [,buffering])

“r” read“w” recreate“a” append“b” add for binary reading

i = f.read() All at onceline = f.readline() Single linelines = f.readlines() All at once to a listf.write(string)f.close()

35

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

I/OImmutable (cannot modify)Char is a single char stringStarts w/ 0 and support –N

36

shuffelBlocks = (raw_input("Shuffel Blocks (y/n): ") == 'y')print(shuffelBlocks)assert(type(a) == type(1))

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

CLASSES AND ENTERPRISE LEVEL PROGRAMMINGPython

37

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Classes

38

> Python class.pyMoshe is earning too muchRoni is earning 100Total Employees: 2

Static variableConstructor

Object var

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

InheritanceBy default Everything public__attribute Turn to privateReflection++:Multiple inheritance is supported

39

class shape:...

class Circle(shape):...

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Modules and ImportsUse 3rd party code:

FunctionsClassesAttributes

40

>>> import module1 [, module2]>>> from module3 import name1 [, name2]

Only specific attributes

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Creating ModulesSimple headerAnd we are done…

41

"""file my_module.pyModule 'my_module': my special module"""def my_func(a):if __name__ == '__main__':

print my_func(1)

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Packaging

42

def is_number(s):try:

int(s)return True

except ValueError:return False

if __name__ == '__main__':COLOR_BREAK = 'blue'COLOR_HEADER_FOOTER = 'darkCyan';

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Python CompilerSee also .pyc file in your stack

43

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Python Integration

44

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

EXCEPTIONSPython

45

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Try and ExceptCommon:

NameErrorTypeErrorIndexErrorKeyError

46

try:print(getattr(moshe, "course"))

except NameError:print("no attribute1")

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

TESTINGPython

47

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

How to Do that?

48

import my_moduleimport unittestclass my_module_test(unittest.TestCase):

def test_method1(self):self.assertFalse(method1(1))

if __name__ == '__main__':unittest.main()

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

PREPARE FOR SPARKPython

49

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Functions are Objects

50

# Evil Code…>> log, exp = exp, log>> r = [ -1, 0, 1]>> map(abs, r)[1, 0, 1]>> def sum(x, y): return x+y>> reduce(sum, r)0>> def large(x): return x > 0>> filter(large, r)[1]

© All rights reserved: Moshe Kaplanhttp://top-performance.blogspot.com

Thank You !

Moshe [email protected]

054-2291978