Successfully reported this slideshow.

Python: an introduction for PHP webdevelopers

5

Share

Upcoming SlideShare
Introduce Django
Introduce Django
Loading in …3
×
1 of 84
1 of 84

Python: an introduction for PHP webdevelopers

5

Share

Download to read offline

A python introduction that I gave to PHP developers. It handles the differences between Python and PHP v5.3

A python introduction that I gave to PHP developers. It handles the differences between Python and PHP v5.3

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Python: an introduction for PHP webdevelopers

  1. 1. PYTHON: AN INTRODUCTION FOR PHP DEVELOPERS
  2. 2. What will we talk about? Some general information about Python Some code - enough to be able to read and understand python code Some minimal practical examples of python code and that based on things that I picked up @ Dx-Solutions. Any questions ? Feel free to interrupt !
  3. 3. What is python ? Python is a widely used general- purpose, high-level programming language. Its design philosophy emphasises code readability. Python supports multiple programming models, including object-oriented, procedural and functional programming. Has a large and comprehensive standard library. Python can be run on many operating systems and (embedded) platforms.
  4. 4. Things that rule It’s easy, flexible and powerful. Good to have in your toolbox! String handling is fantastic A wide range of good third party libraries (because it has a close relationship with C) It is extremely versatile (web, gui apps, arduino yun, scripting …) Great prototype language It’s really fun
  5. 5. HTTP://XKCD.COM/353/
  6. 6. Things that can bite Uses whitespace (tabs) to delimit program blocks (personal preference) It isn’t the fastest language (but projects like cython, pypy, … counters that) Python 3 is not backward compatible unfortunately. In this presentation we will focus on Python 2.x. Package management could be better and isn’t that good like in node.js or PHP
  7. 7. Who uses it ?
  8. 8. In Belgium Not a lot of big players Mobile Vikings Belgacom BIC GDF Suez Belfius … Lot of misconceptions (toy language, only for experiments, small startups, …) Starting to get some traction in higher education (university of Gent, Leuven, Antwerp)
  9. 9. Some web frameworks https://github.com/vinta/awesome-python BeautifulSoup
  10. 10. Work project: Ghelamco CSV fields -> doctrine generator. Really speeded up the process to create entities. SQL generator to automatically generate dump queries Tool to dump table column names (for example to use in certain queries) RAW dump importer (that served as a prototype for a PHP symfony command)
  11. 11. From CSV data
  12. 12. To generate doctrine entity fields … now imagine if you need to do that manually… Average runtime: less than a second…
  13. 13. Personal project: WebGL baking tool http://www.simplicity.be/improving-realism-in-webgl-scenes-by-using-texture-baking/
  14. 14. Now for the code stuff…
  15. 15. IDE Python comes with a builtin IDE “Idle” which feels a bit dated and isn’t that good PHPStorm lovers will be glad to know that jetbrains also has a python IDE called PyCharm Community edition is free But the web support isn’t in the free edition But in reality every text editor will suffice
  16. 16. PyCharm CE
  17. 17. Running python • Through the interactive python shell $ python Python 2.7.9 (default, Dec 19 2014, 06:00:59) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print "hello world" hello world >>>
  18. 18. Running python • By running a .py file hello.py print "hello world" $ python hello.py hello world command
  19. 19. 2 very handy methods dir list attributes and methods help shows builtin help
  20. 20. dir (example) >>> dir("dx-solutions") ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
  21. 21. help (example) >>> help(“dx-solutions”.upper) Help on built-in function upper: upper(...) S.upper() -> string Return a copy of the string S converted to uppercase. (END)
  22. 22. Comments A comment in python start with a hash (#) symbol No multiline comments use multiple #
  23. 23. Comments (examples) // hello world /** * hello * world **/ PHP # hello world # hello # world Python
  24. 24. Variables Python is a dynamically typed language. So you do not need to declare variables before using them or declare their type (like PHP) Every variable in Python is an object… …that has an id …that is of a certain standard data type …that has a mutable or immutable value
  25. 25. Mutable vs immutable Mutable: you can change an object without changing its identity. So in other words when you alter an object its id will be the same. Immutable: you can not change an object without changing its identity. So when you alter an object its id will change!
  26. 26. Mutable vs immutable (examples) >>> myval = "dxsolutions" >>> id(myval) 4538483456 >>> myval = "dx-solutions" >>> id(myval) 4538475888 Immutable >>> mylist = [] >>> id(mylist) 4538197848 >>> mylist.append("hello") >>> id(mylist) 4538197848 Mutable
  27. 27. Standard data types Number integer float long complex String List Tuple Dictionary
  28. 28. Variables declarations (examples) $var_1 = 1; // Integer $var_2 = 3.14; // Float $var_3 = "hello"; // String $var_4 = 'hello'; // String PHP var_1 = 1 # Integer var_2 = 3.14 # Float var_3 = "hello" # String var_4 = 'hello' # String var_1 = 'abc' # Is now a string Python
  29. 29. Math operators Arithmetic operators are the same as PHP Notation Description + add - subtract * multiply / division % modulo ** power operator* * the ** operator is introduced in PHP 5.6
  30. 30. Assignment operators Notation Description = simple assignment += add and assign -= subtract and assign *= multiply and assign /= divide and assign %= modulo and assign **= exponent and assign* //= floor division and assign* * not available in PHP
  31. 31. Comparison operators Notation Description == is equal* != not equal <> not equal > bigger than < smaller than >= bigger than or equal <= smaller than or equal * doesn’t have the === operator
  32. 32. Logical operators Notation Description and and or or not not
  33. 33. Chained comparisons # chained if 5 < x < 7: print 6 # same as if x > 5 and x < 7: print 6
  34. 34. Strings (examples) $var_1 = 'Thunderstruck'; $var_2 = "Ain't no sunshine"; $var_3 = 'Ain't no sunshine'; $var_3 = "this has multiple lines"; PHP Python var_1 = 'Thunderstruck' var_2 = "Ain't no sunshine" var_3 = 'Ain't no sunshine' var_3 = """this has multiple lines"""
  35. 35. Print print ('Thunderstruck'); echo 'Thunderstruck'; PHP print ('Thunderstruck'); # python 3.x print 'Thunderstruck'; # python 2.x Python
  36. 36. String formatting (examples) >>> "Hello %s %s" % ("Glenn", "De Backer") 'Hello Glenn De Backer' C syntax >>> "Hello {0}, {1}".format("Glenn", "De Backer") 'Hello Glenn, De Backer’ >>> "Hello {firstname}, {name}".format(firstname="Glenn", name="De Backer") 'Hello Glenn, De Backer' Advanced string formatting (PEP 3101) (PEP = Python Enhancement Proposals)
  37. 37. Slices Strings are a sequence of characters A subsequence of a sequence is called a slice The operation that extracts a subsequence is called slicing Notation Description a[start:end] items start through end-1 a[start:] items start through the rest of the array a[:end] items from the beginning through end-1 a[:] a copy of the whole array a[start:end:step] start through not past end, by step
  38. 38. Slices a[-1] # last item in the array a[-2:] # last two items in the array a[:-2] # everything except the last two items FROM HTTP://WWW.NLTK.ORG/BOOK/CH03.HTML
  39. 39. None None is the pythonic way of defining NULL. Evaluates to False Is an object (NoneType) Because it is an object, we cannot use it to check if a variable exist.
  40. 40. None (example) db_con = None # Try to connect try: db_con = MysqlDB(db_host, db_user, db_password, db_database) db_con = database.connect() except DatabaseException: pass if db_con is None: print('Could not connect') else: # do dbase stuff
  41. 41. Booleans Simple same as in PHP but case sensitive! True False 1 0
  42. 42. Booleans (examples) $a = True; $b = FALSE; if ($a === true) { echo "Party!"; } PHP a = True b = False if a is True: print “party" if a == True: print "party" Python
  43. 43. Sequences Difficult to compare as PHP only has associative arrays ( hashmap ) List: is just a list of items Tuples: are like lists but you cannot change their values. months blood types … Dictionary: hashmap / associative arrays Also supports slices and slicing
  44. 44. List (example 1/2) >>> mylist = [] >>> mylist.append(123) >>> mylist.append('dxsolutions') >>> mylist.append(2) >>> print mylist [123, ‘dxsolutions’, 2] >>> mylist.sort() >>> print mylist [2, 123, ‘dxsolutions'] >>> mylist.reverse() >>> print mylist ['dxsolutions', 123, 2]
  45. 45. List (example 2/2) >>> mylist.pop() 2 >>> print mylist ['dxsolutions', 123] >>> mylist.extend([1,2,3]) >>> print mylist ['dxsolutions', 123, 1, 2, 3] >>> mylist.remove(2) >>> print mylist ['dxsolutions', 123, 1, 3]
  46. 46. Tuples (example) >>> days = (‘monday’,'tuesday','wednesday','thursday', 'friday','saterday','sunday') >>> days[2] 'wednesday' >>> days[2:] ('wednesday', 'thursday', 'friday', 'saterday', 'sunday') >>> days[:3] ('monday', 'tuesday', ‘wednesday') >>> days[0:2] ('monday', 'tuesday')
  47. 47. Dictionaries person = {'Name': 'Tom', 'Age': 27}; print person['Name']; # Tom print person['Age']; # 27 Python $person = array('Name' => 'Tom', 'Age' => 27); print $person['Name']; print $person['Age']; PHP
  48. 48. Membership operators Notation Description in Evaluates to true if in sequence not in Evaluates to true if not in sequence Python has membership operators, which test for membership in a sequence, such as strings, lists, or tuples
  49. 49. Membership operators (example) >>> a = (1,2,4,8) >>> 1 in a True >>> 3 not in a True >>> 3 in a False >>> word = "boat" >>> 'e' in word False >>> 'a' in word True
  50. 50. Add / multiply operators and sequences >>> a = [ 1, 2 ] >>> b = [ 3, 4 ] >>> a + b [1, 2, 3, 4] >>> a * 4 [1, 2, 1, 2, 1, 2, 1, 2]
 
 >>> a = "hello " >>> b = "world" >>> a + b 'hello world' >>> a * 4 'hello hello hello hello '
  51. 51. Whitespace - indentation Instead of using curly braces ( { ) to delimit program blocks, Python uses indentation It is mandatory, no way around it It is a very controversial feature that some really hate… … but it does improve readability… … and after a while you totally forget about it.
  52. 52. Conditional: if…else…el(se)if (example) if (statement) { // do stuff } elseif (other_statement) { // do other stuff } else { // do other stuff } PHP if (statement): # do stuff elif (other_statement): # do other stuff else: # do other stuff Python
  53. 53. Iterations: for loop Python for statement has a lot in common with PHP foreach Supports break and continue which works more or less the same as in PHP You can loop over strings, lists, dictionaries, … as they are sequences.
  54. 54. Iterations: for loop (example) $words = array('cat', 'window', 'door'); foreach ($words as $word) { echo $word; } PHP words = ['cat', 'window', 'door'] for word in words: print word >>> cat, window, door for word in words[1:3]: print word >>> window, door Python
  55. 55. Iterations: for loop (example) $persons = array('name' => 'glenn', 'job' => 'developer'); foreach ($persons as $key => $value) { // process person } PHP persons = {'name': 'glenn', 'job': 'developer'} for key in persons.keys(): # process key for value in persons.values(): # process value for key, value in persons.items():
 # process value Python
  56. 56. Iterations: range If you do need to iterate over a sequence of numbers, you can use the range() method. You have also xrange: if you do range(1, 10000) it will create a list of 10000 elements in memory when using xrange you create a sequence which evaluates lazy. This is faster and uses less memory. In Python 3.x range() -> xrange()
  57. 57. Iterations: range (example) // array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) $arr = range(1,10); // array(1, 2, 3, 4, 5, 6,) foreach (range(1, 6) as $number) {     echo $number; } PHP >>> range(10) # python 2.x >>> list(range(10)) # python 3.x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for number in range(1,6): print number >>> 1, 2, 3, 4, 5 Python
  58. 58. Iterations: enumerate (example) >>> # bad example! >>> words = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i in range(len(words)): print i, words[i] Bad >>> # if you need indices use enumerate >>> words = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for index, value in enumerate(words): print index, value Good For example: when you want index -> value from a list.
  59. 59. Functions Function blocks begin with the keyword def followed by the function name and parentheses (( )) The code block within every function starts with a colon (:) and is indented. Functions can have docstring. Accessible through function.__doc__ or help(function)
  60. 60. Functions (example) function hello() {     echo "hello world"; } PHP def hello(): "Prints hello world"
 echo "hello world" >>> help(hello) Help on function hello in module __main__: hello() Prints hello world Python
  61. 61. Function arguments You can call a function by using the following types of formal arguments: Required arguments Default arguments Keyword arguments Variable-length arguments (Variadic function) PHP >= 5.6 splat operator (…) Like PHP it doesn’t support function overloading
  62. 62. Required argument (example) >>> def hello(name): print "hello" + name >>> hello("glenn") glenn Python function hello($name) {     echo "hello" . $name; } hello("glenn"); PHP
  63. 63. Required argument (example) >>> def hello(name): print "hello" + name >>> hello() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: hello() takes exactly 1 argument (0 given)
  64. 64. Default argument (example) >>> def hello(name="glenn"): print "hello" + name >>> hello() hello glenn Python function hello($name="glenn") {     echo “hello” . $name; } hello(); >>> hello glenn PHP
  65. 65. Keyword argument (example) >>> def sum(a=1, b=2): print a + b >>> sum() 3 >>> sum(a=5) 7 >>> sum(b=4) 5 >>> sum(a=2, b=4) 6 Python ( There isn’t a PHP equivalent )
  66. 66. Variable length argument (example) def manyArgs(*arg): print "I was called with", len(arg), "arguments:", arg >>> manyArgs(1) I was called with 1 arguments: (1,) >>> manyArgs(1, 2, 3) I was called with 3 arguments: (1, 2, 3) Python function manyArgs(...$arg) { echo "I was called with " . count($arg) . " arguments: " implode($arg); } hello(1); // I was called with 1 arguments: 1
 hello(1, 2, 3); // I was called with 3 arguments: 1,2,3 PHP >= 5.6 using the . . . (splat) operator
  67. 67. Classes In Python everything is an object (again) Class blocks begin with the keyword class followed by the class name and a colon (:) Doesn’t have private / public concept There is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public… …but it is still accessible Classes can also have docstring.
  68. 68. Classes (example) class Employee: def __init__(self, name, salary): self.name = name self.salary = salary e = Employee("mike", "1500") Python class Employee { function __construct($name, $salary) { $this->name = $name; $this->salary = $salary; } } $e = new Employee("mike", "1500"); PHP
  69. 69. Classes: inheritance Supports multiple inheritance (PHP lacks that) Method overloading works the same as in PHP You can (optionally) make your base class inherit from the class object. Matter of style.
  70. 70. Classes: inheritance (example) class Vehicle { // vehicle methods and members } class Car extends Vehicle { // car methods and members } PHP class Vehicle: # vehicle methods and members class Car(Vehicle): # car methods and members Python
  71. 71. Classes: overloading 1/2 (example) class Parent(object): def __init__(self): self.value = 5 def get_value(self): return self.value class Child(Parent): pass # is a null operation >>> c = Child() >>> c.get_value() 5
  72. 72. Classes: overloading 2/2 (example) class Parent(object): def __init__(self): self.value = 5 def get_value(self): return self.value class Child(Parent): def get_value(self): return self.value + 1 >>> c = Child() >>> c.get_value() 6
  73. 73. __init__ /__keyword__ The __keyword__ are also called dunder methods (thunder in West Flemish or double under) They are also sometimes called “magic” methods They are roughly the same as inheriting things In the case of __init__ in the background Python will do automatically the calls to init and new They also make it possible (but are not limited) to change how operators (for example +, / , ..) behave.
  74. 74. from __future__ import division (example) # in python 2.x if you divide an integer you # will get an integer back >> 1 / 2 0 # In python 2.7 future = Python 3.x from __future__ import division >> 1 / 2 0.5 >> 1 // 2 # you will need the special // division # operator to get an integer back
  75. 75. File I/O (example) # Reading a file with open('myfile.txt', 'r') as f: for line in f: print f # Writing a file with open('myfile.txt', 'w') as f: f.write('Hello')
  76. 76. Lambda functions # Simple lambda example f = lambda x, y : x + y >> f(2,4) 6 # Map -> lambda (divide by 2) >>> a = [2, 4, 8, 16] >>> map ( lambda x: x/2, a) [1, 2, 4, 8] # Filter -> lambda (even numbers) >>> a = [1, 2, 3, 4, 5, 6 , 7, 8, 9, 10] >>> filter (lambda x: x % 2 == 0, a) [2, 4, 6, 8, 10]
  77. 77. The proof is in the pudding…
  78. 78. Minimal flask (example) from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" @app.route("/name/<name>") def hello(name=None): return “Hello “ + name if __name__ == "__main__": app.run() HTTP://FLASK.POCOO.ORG
  79. 79. Minimal scraper 1/2 (example) <div title="buyer-name">Carson Busses</div> <span class="item-price">$29.95</span> HTML from lxml import html import requests page = requests.get('http://econpy.pythonanywhere.com/ex/001.html') tree = html.fromstring(page.text) #This will create a list of buyers: buyers = tree.xpath('//div[@title="buyer-name"]/text()') #This will create a list of prices prices = tree.xpath('//span[@class="item-price"]/text()') print 'Buyers: ', buyers print 'Prices: ', prices Python
  80. 80. Minimal scraper 2/2 (example) Buyers: ['Carson Busses', 'Earl E. Byrd', 'Patty Cakes', 'Derri Anne Connecticut', 'Moe Dess', 'Leda Doggslife', 'Dan Druff', 'Al Fresco', 'Ido Hoe', 'Howie Kisses', 'Len Lease', 'Phil Meup', 'Ira Pent', 'Ben D. Rules', 'Ave Sectomy', 'Gary Shattire', 'Bobbi Soks', 'Sheila Takya', 'Rose Tattoo', 'Moe Tell'] Prices: ['$29.95', '$8.37', '$15.26', '$19.25', '$19.25', '$13.99', '$31.57', '$8.49', '$14.47', '$15.86', '$11.11', '$15.98', '$16.27', '$7.50', '$50.85', '$14.26', '$5.68', '$15.00', '$114.07', '$10.09'] Result HTTP://DOCS.PYTHON-GUIDE.ORG/EN/LATEST/SCENARIOS/SCRAPE/
  81. 81. Bayesian text classifier from sklearn.feature_extraction.text import CountVectorizer from sklearn.pipeline import Pipeline from sklearn.naive_bayes import MultinomialNB import numpy # docs docs = numpy.array(["Buy viagra", "Hello frank", "The servers are down", "Do you want a rolex", "Cheap airplane tickets"]) # labels 1: spam, 0: ham labels = numpy.array([1,0,0,1,1]) # pipeline steps = [('vectorizer', CountVectorizer()), ('clf', MultinomialNB())] pipe = Pipeline(steps) pipe.fit(docs, labels) # test data test_data = numpy.array([ "Hello koen", "Cheap viagra"]) print pipe.predict(test_data) HTTP://SCIKIT-LEARN.ORG/STABLE/
  82. 82. Automate with fabric # Import Fabric's API module from fabric.api import * env.hosts = [‘server.domain.tld’, ‘another_server.domain.tld’] def update_upgrade(): """ Update the default OS installation's basic default tools. """ run("aptitude update") run("aptitude -y upgrade") def install_memcached(): """ Download and install memcached. """ run("aptitude install -y memcached") def update_install(): # Update update_upgrade() # Install install_memcached() HTTP://WWW.FABFILE.ORG/
  83. 83. Some interesting resources Dive into python -http://www.diveintopython.net/ The standard python library - http://effbot.org/zone/librarybook- index.htm How to think like a computer scientist with python - http:// www.greenteapress.com/thinkpython/thinkCSpy.pdf PyVideo (indexes a lot of python talks) - http://pyvideo.org/ 80 best python resources - http://www.fromdev.com/2014/03/ python-tutorials-resources.html 10 myths of enterprise python - https://www.paypal- engineering.com/2014/12/10/10-myths-of-enterprise-python/ Python success stories - https://www.python.org/about/success/
  84. 84. Thank you for listening… (any questions ?)

×