Python utan-stodhjul-motorsag

50
Utan stödhjul och motorsåg

Transcript of Python utan-stodhjul-motorsag

Page 1: Python utan-stodhjul-motorsag

 

Utan stödhjul och motorsåg

Page 2: Python utan-stodhjul-motorsag

Vad är Python?

● Ett dynamiskt typat språk● Designat för enkelhet och tydlighet

Page 3: Python utan-stodhjul-motorsag

It is the only language co-designed for the most advanced and the complete novice.

[David Goodger]

Page 4: Python utan-stodhjul-motorsag

Plattformar

● Python = CPython● Jython (JVM)● IronPython (.NET, Mono)● PyPy (Python in Python)

Page 5: Python utan-stodhjul-motorsag

Användningsområden

admin, byggen och driftsättning

webbapps, webbtjänster/+klienter

verktygsklister & GUIn (vanligt på linux)

dataskyffling/-konvertering/-analys

Page 6: Python utan-stodhjul-motorsag

Scripta verktyg

Grafik (Gimp, Inkscape, Blender, ...), LibreOffice, ...

Spel (Battlefield 2+, Civ IV, EVE-Online, ...)

DVCS: Mercurial, Bazaar...

Page 7: Python utan-stodhjul-motorsag

Användare

Page 8: Python utan-stodhjul-motorsag
Page 9: Python utan-stodhjul-motorsag
Page 10: Python utan-stodhjul-motorsag
Page 11: Python utan-stodhjul-motorsag

.. Sun, Microsoft, Canonical, O'Reilly, Lucasfilm, Nasa, Sourceforge, ...

Page 12: Python utan-stodhjul-motorsag

Årets språk enligt Tiobe

Bästa tillväxten

20072010

Page 13: Python utan-stodhjul-motorsag

Moget

Utveckling & drift: doctest, nosetests, distribute, pip, virtualenv, ...

Paket för allt: data, sysadmin, webb, grafik, matematik, parallellism...

Page 14: Python utan-stodhjul-motorsag

Design

Page 15: Python utan-stodhjul-motorsag

Vad är viktigt?

Läsbarhet!

Namngivning!

Page 16: Python utan-stodhjul-motorsag

Programming The Way Guido Indented It

Page 17: Python utan-stodhjul-motorsag

def to_roman(num):

assert num > 0 and num < 4000

result = []

for d, r in NUMERALS:

while num >= d: result.append(r) num -= d

return ''.join(result)

Page 18: Python utan-stodhjul-motorsag

"In soviet russia, braces scope you"

from __future__ import braces

...SyntaxError: not a chance

Page 19: Python utan-stodhjul-motorsag

Effektivt

tokens = ['a', 'b', 'c', 'B', 'dd', 'C', 'DD']

similar = {}

for token in tokens: key = token.lower() similar.setdefault(key, []).append(token)

Page 20: Python utan-stodhjul-motorsag

Lättanvänt

for key, tokens in similar.items():

if len(tokens) > 1: print("Similar (like %s):" % key)

for token in tokens: print(token)

Page 21: Python utan-stodhjul-motorsag

Generella behållare

● list() == []● dict() == {}● tuple() == ()● set()

Page 22: Python utan-stodhjul-motorsag

Python är interaktivt

$ pythonPython 2.7.1 [...]Type "help", "copyright", "credits" or "license" for more information.>>>

Page 23: Python utan-stodhjul-motorsag

>>> def add(a, b=1):... return a + b...>>> add(1)2>>> add(1, 2)3>>> add(2, b=3)5>>> add(b=3, a=2)5

Page 24: Python utan-stodhjul-motorsag

Mekanik

Page 25: Python utan-stodhjul-motorsag

"""Usage:

>>> usr = Path('/usr') >>> bin = usr.segment('local').segment('bin') >>> str(bin) '/usr/local/bin'"""

class Path: def __init__(self, base): self.base = base

def segment(self, name): return Path(self.base + '/' + name)

def __str__(self): return self.base

Page 26: Python utan-stodhjul-motorsag

__protokoll__

Betyder används under ytan

Page 27: Python utan-stodhjul-motorsag

Tydliga sammanhang

Börjar bukta när en dålig idé växer

● state är explicit (self)

Page 28: Python utan-stodhjul-motorsag

"""Usage:

>>> usr = Path('/usr') >>> bin = usr.segment('local').segment('bin') >>> str(bin) '/usr/local/bin'"""

class Path(str): def segment(self, name): return Path(self + '/' + name)

Page 29: Python utan-stodhjul-motorsag

Allt är objekt

● primitiver● collections● funktioner, klasser● moduler, paket

Page 30: Python utan-stodhjul-motorsag

Ingen motorsåg..

Page 31: Python utan-stodhjul-motorsag

Closures"""Usage:

>>> mkpasswd = salter("@secret#salt") >>> mkpasswd("qwerty") '4ef6056fb6f424f2e848705fd5e40602'"""

from hashlib import md5

def salter(salt):

def salted_encrypt(value): return md5(salt + value).hexdigest()

return salted_encrypt

 

Page 32: Python utan-stodhjul-motorsag

Funktionell @dekoration

from django.views.decorators.cache import cache_page

@cache_page(15 * 60)def slashdot_index(request): result = resource_intensive_operation(request) return view_for(result)

Page 33: Python utan-stodhjul-motorsag

Reduce boilerplate..

try: f = open('file1.txt') for line in f: print(line)

finally: f.close()

Page 34: Python utan-stodhjul-motorsag

.. with contexts

with open('file1.txt') as f:

for line in f: print(line)

Page 35: Python utan-stodhjul-motorsag

Generators

def public_files(dirname): for fname in os.listdir(dirname): if not fname.startswith('.'): yield fname

for fname in public_files('.'): open(fname)

Page 36: Python utan-stodhjul-motorsag

Projicera data

Page 37: Python utan-stodhjul-motorsag

Maskinellt procedurell

publicfiles = []

for fname in os.listdir('.'):

if not fname.startswith('.'): publicfiles.append(open(fname))

Page 38: Python utan-stodhjul-motorsag

Tillståndslös spaghetti

publicfiles = map(lambda fname: open(fname), filter(lambda fname: not fname.startswith('.'), os.listdir('.')))

Page 39: Python utan-stodhjul-motorsag

List comprehensions

publicfiles = [open(fname) for fname in os.listdir('.') if not fname.startswith('.')]

Page 40: Python utan-stodhjul-motorsag

Generator comprehensions

publicfiles = (open(fname) for fname in os.listdir('.') if not fname.startswith('.'))

Page 41: Python utan-stodhjul-motorsag

process(open(fname) for fname in os.listdir('.') if not fname.startswith('.'))

Page 42: Python utan-stodhjul-motorsag

Myter

Page 43: Python utan-stodhjul-motorsag

"My way or the highway"

Page 44: Python utan-stodhjul-motorsag

Sanningar

Page 45: Python utan-stodhjul-motorsag

There should be one — and preferably only one — obvious 

way to do it.

Page 46: Python utan-stodhjul-motorsag

Although that way may not be obvious at first unless you're 

Dutch.

Page 47: Python utan-stodhjul-motorsag

Python is humanism

Page 48: Python utan-stodhjul-motorsag

Python ger dig

● Enkel men kraftfull kod● Fokus på lösningen

Page 49: Python utan-stodhjul-motorsag

Tack!

Page 50: Python utan-stodhjul-motorsag

Image Tributes (CC)

Python, Google, YouTube, Spotify logos & Amazon Python Day Poster photo courtesy of respective organization.

Night train

"And so thy path shall be a track of light"

"Documents Reveal Django Pony, Caught In Tail Of Lies." / _why

"inch by inch"

"Wasserglas"

"I am Jack, hear me lumber!"

"Bend me, Shape me, any way you want me."