47
SEMANTIC WEAPONS PHP Conf 2016 HELLO STRANGER

PHP for Python Developers

Embed Size (px)

Citation preview

Page 1: PHP for Python Developers

SEMANTIC WEAPONS

PHP Conf 2016

HELLO STRANGER

Page 2: PHP for Python Developers

SEMANTIC WEAPONS

$ id -un

[email protected]

– tweet me @cvences

– Founded a 4yo development shop

– semanticweapons.com

#phpconmx

Page 3: PHP for Python Developers

and we’re hiring!

HELLO STRANGER

SEMANTIC WEAPONS

Page 4: PHP for Python Developers

SEMANTIC WEAPONS

PHP for Python Developers

Page 5: PHP for Python Developers

SEMANTIC WEAPONS

PHP for Python Developers

Web Development & General Scripting

Page 6: PHP for Python Developers

SEMANTIC WEAPONS

$ membership operator _

$inArray = in_array(5, range(0, 10)) === TRUE;

Page 7: PHP for Python Developers

SEMANTIC WEAPONS

$ membership operator _

in_array = 5 in range(10) == True

Page 8: PHP for Python Developers

SEMANTIC WEAPONS

$ membership operator _

in_array = 5 in range(10) == True >>> in_array False

Page 9: PHP for Python Developers

SEMANTIC WEAPONS

$ default values _

# counting dictionary keys from collections import defaultdict

log = ['warn', 'error', 'debug', 'info', 'warn', 'debug', 'warn'] stats = defaultdict(int)

for entry in log: stats[entry] += 1

Page 10: PHP for Python Developers

SEMANTIC WEAPONS

$ default values _

// counting elements $log = ['warn', 'error', 'debug', 'info', 'warn', 'debug', ‘warn'];

$stats = []; foreach ($log as $entry){ $stats[$entry] = $stats[$entry]++ ?: 0; }

Page 11: PHP for Python Developers

SEMANTIC WEAPONS

$ default values _

// counting elements $log = ['warn', 'error', 'debug', 'info', 'warn', 'debug', ‘warn'];

$stats = []; foreach ($log as $entry){ $stats[$entry]++; }

Page 12: PHP for Python Developers

SEMANTIC WEAPONS

$ default values _

// counting elements $log = ['warn', 'error', 'debug', 'info', 'warn', 'debug', ‘warn'];

$stats = []; foreach ($log as $entry){ $stats[$entry]++; }

# counting dictionary keys from collections import defaultdict

log = ['warn', 'error', 'debug', 'info', 'warn', 'debug', ‘warn']

stats = defaultdict(int) for entry in log: stats[entry] += 1

Page 13: PHP for Python Developers

SEMANTIC WEAPONS

Memory consumption

– C’s pointers

– SQL’s LIMIT statement

– strcmp, strcpy, strcat

– strncmp, strncpy, strncat

– buffer overflow

Page 14: PHP for Python Developers

SEMANTIC WEAPONS

Memory consumption

– C’s pointers

– SQL’s LIMIT statement

– strcmp, strcpy, strcat

– strncmp, strncpy, strncat

– buffer overflow

¿Quién agregó la clausula LIMIT a mini SQL 1.x?

Page 15: PHP for Python Developers

SEMANTIC WEAPONS

$ Array

function myRange($max = 10) { $numeros = []; $num = 1;

while ($num < $max) { array_push($numeros, $num); $num++; }

return $numeros; }

function sum($max = 10) { $sumatoria = 0;

foreach (myRange($max) as $num) { $sumatoria += $num; }

echo $sumatoria; }

sum(1000000);

Page 16: PHP for Python Developers

SEMANTIC WEAPONS

$ Iterator

class NumberIter implements Iterator { private $position = 0; private $array = [];

public function __construct($array = []) { $this->position = 0; $this->array = $array; } public function rewind() { $this->position = 0; }

public function current() { return $this->array[$this->position]; }

public function key() { return $this->position; }

public function next() { ++$this->position; }

public function valid() { return isset($this->array[$this->position]); } }

function sum($max = 10) { $sumatoria = 0; $numberIter = new NumberIter($array = range(1, ($max - 1)));

foreach ($numberIter as $num) { $sumatoria += $num; }

echo $sumatoria; }

sum(1000000);

Page 17: PHP for Python Developers

SEMANTIC WEAPONS

$ PHP Arrays vs Iterators

foreach (myRange($max) as $num) { $sumatoria += $num; }

foreach ($numberIter as $num) { $sumatoria += $num; }

Page 18: PHP for Python Developers

SEMANTIC WEAPONS

Entering Generators

Page 19: PHP for Python Developers

SEMANTIC WEAPONS

$ Generators

function myRange($max = 10) { // $numeros = [];

$num = 1;

while ($num < $max) { yield $num; $num++; }

// return $numeros; }

function sum($max = 10) { $sumatoria = 0;

foreach (myRange($max) as $num) { $sumatoria += $num; }

echo $sumatoria; }

sum(1000000);

Page 20: PHP for Python Developers

SEMANTIC WEAPONS

Generators

– Implement Iterators

– DRY

– Can traverse with foreach

– foreach does not use IAP (>7.0.0)

– Uses hashtable iterators now

– Does not build an array in-memory

– Also speeds time to generate

Note: – Calling range(0, 1000000) will result in well over 100 MB of memory being used.

Page 21: PHP for Python Developers

SEMANTIC WEAPONS

$ Iterators, Generators and Arrays

class NumberIter implements Iterator { private $position = 0; private $array = [];

public function __construct($array = []) { $this->position = 0; $this->array = $array; } public function rewind() { $this->position = 0; }

public function current() { return $this->array[$this->position]; }

public function key() { return $this->position; }

public function next() { ++$this->position; }

public function valid() { return isset($this->array[$this->position]); } }

function myRange($max = 10) { $num = 1;

while ($num < $max) { yield $num; $num++; } }

array range ($start, $end, $step)

Page 22: PHP for Python Developers

SEMANTIC WEAPONS

$ Iterators, Generators and Arrays

class NumberIter implements Iterator { private $position = 0; private $array = [];

public function __construct($array = []) { $this->position = 0; $this->array = $array; } public function rewind() { $this->position = 0; }

public function current() { return $this->array[$this->position]; }

public function key() { return $this->position; }

public function next() { ++$this->position; }

public function valid() { return isset($this->array[$this->position]); } }

function myRange($max = 10) { $num = 1;

while ($num < $max) { yield $num; $num++; } }

Page 23: PHP for Python Developers

SEMANTIC WEAPONS

Case of Study

Page 24: PHP for Python Developers

SEMANTIC WEAPONS

$ PHP 7.1.0 RC2_

Page 25: PHP for Python Developers

SEMANTIC WEAPONS

Using Generators

Page 26: PHP for Python Developers

SEMANTIC WEAPONS

$ PHP 7.1.0 RC2_

Page 27: PHP for Python Developers

SEMANTIC WEAPONS

Using Iterators

Page 28: PHP for Python Developers

SEMANTIC WEAPONS

HELLO STRANGER

PHP 7.1.0 RC2

Page 29: PHP for Python Developers

SEMANTIC WEAPONS

$ time

Page 30: PHP for Python Developers

SEMANTIC WEAPONS

To be or not to be

– Using iterators is great

– foreach in 7.0.0 rocks!

– lists are fat, sequences evaluate lazily

– Lazy is good

Page 31: PHP for Python Developers

SEMANTIC WEAPONS

Warning: sort() expects parameter 1 to be array, object given

Page 32: PHP for Python Developers

SEMANTIC WEAPONS

But…

– Python iterators introduced in 2.2 (2001)

– range -> xrange -> range

– PHP yield introduced in 5.5.0+ (2013)

– Iterator name space may clash

– What’s wrong with iterator_to_array?

– PHP ArrayAccess

Blog Post: SPL Iterators against the performance

PHP Manual: /language.generators.overview.php

Page 33: PHP for Python Developers

SEMANTIC WEAPONS

Readability

Page 34: PHP for Python Developers

SEMANTIC WEAPONS

$ unpacking a list _

sum_of_numbers = sum([x**3 for x in range(10000)])

Page 35: PHP for Python Developers

SEMANTIC WEAPONS

$ unpacking an array _

$sumOfNumbers = 0;

foreach (range(1, 10000) as $num) { $sumOfNumbers += pow($num, 3); }

Page 36: PHP for Python Developers

SEMANTIC WEAPONS

$ unpacking an array _

// second try $processArray = function($method, $array) { return array_map($method, $array); };

$sumOfNumbers = array_sum( $processArray( $method = function($x){return pow($x, 3);}, $array = range(0, 10000) ) );

Page 37: PHP for Python Developers

SEMANTIC WEAPONS

One liners

– means something in English

– easy to understand at first glance

Page 38: PHP for Python Developers

SEMANTIC WEAPONS

$ unpacking an array _

$sumOfNumbers = 0;

foreach (range(1, 10000) as $value) { $sumOfNumbers += pow($value, 3); }

// second try $processArray = function($method, $array) { return array_map($method, $array); };

$sumOfNumbers = array_sum( $processArray( $method = function($x){return pow($x, 3);}, $array = range(0, 10000) ) );

// third try $sumOfNumbers = array_sum(array_map(function($x){return pow($x, 3);}, range(0, 10000)));

Page 39: PHP for Python Developers

SEMANTIC WEAPONS

$ readability _

sum_of_numbers = sum([x**3 for x in range(10000)])

$y = array_sum(array_map(function($x){return pow($x, 3);}, range(0, 10000)));

Page 40: PHP for Python Developers

SEMANTIC WEAPONS

$ looping over two collections _

speakers = ['Javier', 'Joe', 'Rasmus', 'Carlos'] talks = ['Ansible', 'Loopless', 'Deploy', 'Python']

for speaker, talk in zip(speakers, talks): print(speaker, talk)

Page 41: PHP for Python Developers

SEMANTIC WEAPONS

$ looping over two collections _

$speakers = ['Javier', 'Joe', 'Rasmus', 'Carlos']; $talks = ['Ansible', 'Loopless', 'Deploy', 'Python'];

$max = min(count($speakers), count($talks));

foreach(range(0, $max-1) as $i){ echo($speakers[$i] .' '. $talks[$i]); }

Page 42: PHP for Python Developers

SEMANTIC WEAPONS

$ looping over two collections _

// second approach $speakers = ['Javier', 'Joe', 'Rasmus', 'Carlos']; $talks = ['Ansible', 'Loopless', 'Deploy', 'Python'];

$meetupTalks = array_combine($speakers, $talks);

foreach($meetupTalks as $speaker => $talk){ echo "$speaker $talk"; }

Page 43: PHP for Python Developers

SEMANTIC WEAPONS

$ looping over two collections _

for speaker, talk in zip(speakers, talks): print(speaker, talk)

foreach(array_combine($speakers, $talks) as $speaker => $talk) echo "$speaker $talk";

Page 44: PHP for Python Developers

SEMANTIC WEAPONS

$ python iterators magic, tweet .@cvences

# dado un array asociativo, elimina todos los nombres que empiecen con la letra “c”meetup_info = { 'Javier': 'Ansible', 'Joe': 'Loopsless', 'Rasmus': 'Deploy', 'Carlos': 'Python', } meetup_info = {key: meetup_info[key] for key in meetup_info if not key.lower().startswith('c')}

# dado un modelo con relationships, filtra los blogs cuyos autores tengan o no # un nombre asignado. El título no incluye la palabra python y ordénalos por fecha # de creación descendente. Blog.objects .filter(entry__authors__isnull=False, entry__authors__name__isnull=True) .exclude(entry__title__icontains=‘python’) .order_by(‘-created’)

Page 45: PHP for Python Developers

SEMANTIC WEAPONS

$ python iterators magic

def fib(n): return (4 << n * (3 + n)) // ((4 << 2 * n) - (2 << n) - 1) & ((2 << n) - 1)

def get_fibonacci_by_position(pos): """ Returns the fibonacci number at pos """ a, b = 1, 1 for _ in range(pos): a, b = a + b, a return b

Page 46: PHP for Python Developers
Page 47: PHP for Python Developers

[email protected]

HELLO STRANGER

SEMANTIC WEAPONS