python utan-stodhjul-motorsag
TRANSCRIPT
![Page 1: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/1.jpg)
Utan stödhjul och motorsåg
![Page 2: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/2.jpg)
Vad är Python?
● Ett dynamiskt typat språk● Designat för enkelhet och tydlighet
![Page 3: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/3.jpg)
It is the only language co-designed for the most advanced and the complete novice.
[David Goodger]
![Page 4: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/4.jpg)
Plattformar
● Python = CPython● Jython (JVM)● IronPython (.NET, Mono)● PyPy (Python in Python)
![Page 5: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/6.jpg)
Scripta verktyg
Grafik (Gimp, Inkscape, Blender, ...), LibreOffice, ...
Spel (Battlefield 2+, Civ IV, EVE-Online, ...)
DVCS: Mercurial, Bazaar...
![Page 7: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/7.jpg)
Användare
![Page 8: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/8.jpg)
![Page 9: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/9.jpg)
![Page 10: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/10.jpg)
![Page 11: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/11.jpg)
.. Sun, Microsoft, Canonical, O'Reilly, Lucasfilm, Nasa, Sourceforge, ...
![Page 12: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/12.jpg)
Årets språk enligt Tiobe
Bästa tillväxten
20072010
![Page 13: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/13.jpg)
Moget
Utveckling & drift: doctest, nosetests, distribute, pip, virtualenv, ...
Paket för allt: data, sysadmin, webb, grafik, matematik, parallellism...
![Page 14: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/14.jpg)
Design
![Page 15: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/15.jpg)
Vad är viktigt?
Läsbarhet!
Namngivning!
![Page 16: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/16.jpg)
Programming The Way Guido Indented It
![Page 17: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/18.jpg)
"In soviet russia, braces scope you"
from __future__ import braces
...SyntaxError: not a chance
![Page 19: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/21.jpg)
Generella behållare
● list() == []● dict() == {}● tuple() == ()● set()
![Page 22: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/22.jpg)
Python är interaktivt
$ pythonPython 2.7.1 [...]Type "help", "copyright", "credits" or "license" for more information.>>>
![Page 23: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/23.jpg)
>>> 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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/24.jpg)
Mekanik
![Page 25: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/25.jpg)
"""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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/26.jpg)
__protokoll__
Betyder används under ytan
![Page 27: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/27.jpg)
Tydliga sammanhang
Börjar bukta när en dålig idé växer
● state är explicit (self)
![Page 28: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/28.jpg)
"""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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/29.jpg)
Allt är objekt
● primitiver● collections● funktioner, klasser● moduler, paket
![Page 30: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/30.jpg)
Ingen motorsåg..
![Page 31: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/33.jpg)
Reduce boilerplate..
try: f = open('file1.txt') for line in f: print(line)
finally: f.close()
![Page 34: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/34.jpg)
.. with contexts
with open('file1.txt') as f:
for line in f: print(line)
![Page 35: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/36.jpg)
Projicera data
![Page 37: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/37.jpg)
Maskinellt procedurell
publicfiles = []
for fname in os.listdir('.'):
if not fname.startswith('.'): publicfiles.append(open(fname))
![Page 38: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/38.jpg)
Tillståndslös spaghetti
publicfiles = map(lambda fname: open(fname), filter(lambda fname: not fname.startswith('.'), os.listdir('.')))
![Page 39: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/39.jpg)
List comprehensions
publicfiles = [open(fname) for fname in os.listdir('.') if not fname.startswith('.')]
![Page 40: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/40.jpg)
Generator comprehensions
publicfiles = (open(fname) for fname in os.listdir('.') if not fname.startswith('.'))
![Page 41: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/41.jpg)
process(open(fname) for fname in os.listdir('.') if not fname.startswith('.'))
![Page 42: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/42.jpg)
Myter
![Page 43: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/43.jpg)
"My way or the highway"
![Page 44: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/44.jpg)
Sanningar
![Page 45: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/45.jpg)
There should be one — and preferably only one — obvious
way to do it.
![Page 46: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/46.jpg)
Although that way may not be obvious at first unless you're
Dutch.
![Page 47: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/47.jpg)
Python is humanism
![Page 48: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/48.jpg)
Python ger dig
● Enkel men kraftfull kod● Fokus på lösningen
![Page 49: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/49.jpg)
Tack!
![Page 50: Python utan-stodhjul-motorsag](https://reader034.vdocuments.mx/reader034/viewer/2022052321/5564af3cd8b42a3e618b475f/html5/thumbnails/50.jpg)
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."