Python for Humans

download Python for Humans

of 69

Transcript of Python for Humans

  • 7/30/2019 Python for Humans

    1/69

    Python For Humans

    Kenneth Reitz

  • 7/30/2019 Python for Humans

    2/69

  • 7/30/2019 Python for Humans

    3/69

    @kennethreitz

    http://twitter.com/kennethreitzhttp://twitter.com/kennethreitz
  • 7/30/2019 Python for Humans

    4/69

  • 7/30/2019 Python for Humans

    5/69

    Open Source

  • 7/30/2019 Python for Humans

    6/69

    RequestsHTTP for Humans

  • 7/30/2019 Python for Humans

    7/69

    Httpbin.org$curl http://httpbin.org/get?test=1{"url": "http://httpbin.org/get","headers": {

    "Content-Length": "","Connection": "keep-alive","Accept": "*/*","User-Agent": "curl/7.21.4 ...","Host": "httpbin.org","Content-Type": ""

    },"args": {test: 1

    },"origin": "67.163.102.42"

    }

    http://httpbin.org/gethttp://httpbin.org/gethttp://httpbin.org/gethttp://httpbin.org/gethttp://httpbin.org/get
  • 7/30/2019 Python for Humans

    8/69

    Et Cetera Legit: Git Workow for Humans

    Envoy: Subprocess for Humans

    Tablib: Tabular Data for Humans

    Clint: CLI App Toolkit

    Autoenv: Magic Shell Environments

    OSX-GCC-Installer: Provokes Lawyers

  • 7/30/2019 Python for Humans

    9/69

    Open Source

    All The Things!

  • 7/30/2019 Python for Humans

    10/69

    Build for Open Source

    Components become concise & decoupled.

    Concerns separate themselves.

    Best practices emerge (e.g. no creds in code).

    Documentation and tests become crucial.

    Code can be released at any time.

  • 7/30/2019 Python for Humans

    11/69

    Philosophy

  • 7/30/2019 Python for Humans

    12/69

    We share a dark past.

    Perl, Java, PHP, ColdFusion,

    Classic ASP, &c.

  • 7/30/2019 Python for Humans

    13/69

  • 7/30/2019 Python for Humans

    14/69

    Beautiful is betterthan ugly.

  • 7/30/2019 Python for Humans

    15/69

    Explicit is betterthan implicit.

  • 7/30/2019 Python for Humans

    16/69

    Simple is betterthan complex.

  • 7/30/2019 Python for Humans

    17/69

    Complex is betterthan complicated.

  • 7/30/2019 Python for Humans

    18/69

    If the implementation is hardto explain, its a bad idea.

    (except PyPy)

  • 7/30/2019 Python for Humans

    19/69

  • 7/30/2019 Python for Humans

    20/69

    Welcome toParadise

  • 7/30/2019 Python for Humans

    21/69

  • 7/30/2019 Python for Humans

    22/69

    We know Ruby...require 'net/http'require 'uri'

    uri = URI.parse('https://api.github.com/user')

    http = Net::HTTP.new(uri.host, uri.port)http.use_ssl = true

    req = Net::HTTP::Get.new(uri.request_uri)

    req.basic_auth('username', 'password')

    r = http.request(req)

    puts r

  • 7/30/2019 Python for Humans

    23/69

    Pythons net/http?http/url/lib/2

  • 7/30/2019 Python for Humans

    24/69

    Several hours later...

  • 7/30/2019 Python for Humans

    25/69

    import urllib2

    gh_url = 'https://api.github.com/user'

    req = urllib2.Request(gh_url)

    password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()password_manager.add_password(None, gh_url, 'user', 'pass')

    auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)opener = urllib2.build_opener(auth_manager)

    urllib2.install_opener(opener)

    handler = urllib2.urlopen(req)

    print handler.read()

    https://api.github.com/user'https://api.github.com/user'https://api.github.com/user'
  • 7/30/2019 Python for Humans

    26/69

    import re

    class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):

    auth_header = 'Authorization'rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'

    'realm=(["\'])(.*?)\\2', re.I)

    def __init__(self, *args, **kwargs):HTTPBasicAuthHandler.__init__(self, *args, **kwargs)

    def http_error_401(self, req, fp, code, msg, headers):url = req.get_full_url()

    response = self._http_error_auth_reqed('www-authenticate', url, req, headers)

    self.reset_retry_count()return response

    http_error_404 = http_error_401

  • 7/30/2019 Python for Humans

    27/69

    Admit it.Youd leave and never come back.

  • 7/30/2019 Python for Humans

    28/69

    The Problem.

    Unclear which module to use in the rst

    place.

    Prognosis seems to be urllib2, but the docs

    are useless.

    Worst API ever.

  • 7/30/2019 Python for Humans

    29/69

    This is a serious problem.

    HTTP should be as simple

    as a print statement.

  • 7/30/2019 Python for Humans

    30/69

    The Solution is Simple.

    Build elegant tools to

    perform these tasks.

  • 7/30/2019 Python for Humans

    31/69

    Python needs morePragmatic Packages.

  • 7/30/2019 Python for Humans

    32/69

    pragmatic |pragmatik|, adj:

    Dealing with things sensibly and realistically in

    a way that is based on practical rather than

    theoretical considerations

  • 7/30/2019 Python for Humans

    33/69

    Python For Humans

  • 7/30/2019 Python for Humans

    34/69

    Lets Break it Down.

    A small set of methods with

    consistent parameters.

    HEAD, GET, POST, PUSH, PUT,

    PATCH, DELETE, &c.

    They all accept Headers, URL

    Parameters, Body/Form Data.

    What is HTTP at its core?

  • 7/30/2019 Python for Humans

    35/69

    Urllib2 is Toxic. Heavily over-engineered.

    Abolishes most of PEP20.

    Docs are impossible to read.

    HTTP is simple. Urllib2 is not.

    Scares people away from Python.

  • 7/30/2019 Python for Humans

    36/69

    Enter Requests.

  • 7/30/2019 Python for Humans

    37/69

    HTTP for Humans.

  • 7/30/2019 Python for Humans

    38/69

    import requests

    url = 'https://api.github.com/user'auth = ('username', 'password')

    r = requests.get(url, auth=auth)print r.content

    https://api.github.com/user'https://api.github.com/user'
  • 7/30/2019 Python for Humans

    39/69

  • 7/30/2019 Python for Humans

    40/69

    Achievement

    Unlocked! A small set of methods with

    consistent parameters.

    HEAD, GET, POST, PUSH, PUT,

    PATCH, DELETE, &c.

    They all accept Headers, URL

    Parameters, Body/Form Data.

  • 7/30/2019 Python for Humans

    41/69

    Do this.

  • 7/30/2019 Python for Humans

    42/69

    The Litmus Test

    If you have to refer to the documentation

    every time you use a module, nd (or

    build) a new module.

  • 7/30/2019 Python for Humans

    43/69

    Fit the 90% Use Case.

  • 7/30/2019 Python for Humans

    44/69

    The API is all that matters.Everything else is secondary.

  • 7/30/2019 Python for Humans

    45/69

    I Mean Everything.

    Features.

    Eciency.

    Performance.

    Corner-cases.

    Everything.

  • 7/30/2019 Python for Humans

    46/69

    Pivot!

    At rst, Requests was far from powerful.

    But, it deeply resonated with people.

    Features grew over time, but the API was

    never compromised.

  • 7/30/2019 Python for Humans

    47/69

  • 7/30/2019 Python for Humans

    48/69

    Cool Story, Bro.

    We need better APIs.

    We want better APIs.

    Its worth your time as a developer.

    Its worth everyones time as users.

  • 7/30/2019 Python for Humans

    49/69

    Barriers to Entry

  • 7/30/2019 Python for Humans

    50/69

    File and System

    Operations

    sys | shutils | os | os.path | io modules

    Really dicult to run external commands.

    Blocks dev+ops folks from adopting Python.

  • 7/30/2019 Python for Humans

    51/69

    Installing Python

    Just use the system Python?

    Python 2 or Python 3?

    Installer from Python.org?

    32bit or 64bit?

    Build from source? Unix or Framework build?

  • 7/30/2019 Python for Humans

    52/69

    There should be oneand

    preferably only oneobviousway to do it.

  • 7/30/2019 Python for Humans

    53/69

    XML Hell

    etree annoys people.

    lxml is awesome, but can be dicult to install.

  • 7/30/2019 Python for Humans

    54/69

    Packaging & Depedencies

    Pip or easy_install?

    No easy_uninstall?

    Distribute vs Setuptools?

    Setuptools appears to be built into Python.

    Broken setup.py les. Released packages not in the Cheeseshop.

  • 7/30/2019 Python for Humans

    55/69

    Date[time]s.

    Which module to use? Datetime? Date?

    Time? Calendar? Dateutil? 1.5?

    Timezones.

    The stdlib can generate but not parse

    ISO8601 dates.

  • 7/30/2019 Python for Humans

    56/69

    Unicode.

  • 7/30/2019 Python for Humans

    57/69

    Testing.

  • 7/30/2019 Python for Humans

    58/69

  • 7/30/2019 Python for Humans

    59/69

    The HitchhikersGuide to Python.

    http://python-guide.org

    http://python-guide.org/http://python-guide.org/
  • 7/30/2019 Python for Humans

    60/69

  • 7/30/2019 Python for Humans

    61/69

    Python-Guide.org

    Documented best practices.

    Guidebook for newcomers.

    Reference for seasoned veterans.

    Dont panic & always carry a towel.

    The Hitchhikers Guide to Python

  • 7/30/2019 Python for Humans

    62/69

    Best Practices

    Recommends distribute, pip, and

    virtualenv out of the box. Explicit

    installation directions for every OS.

    Instills a resistance to doctest.

    Teaches the use of datetime.utcnow()

  • 7/30/2019 Python for Humans

    63/69

    Theres only one rule...

  • 7/30/2019 Python for Humans

    64/69

    There should be oneand

    preferably only oneobviousway to do it.

  • 7/30/2019 Python for Humans

    65/69

    This Fixes...

    Makes Python more accessible,

    lowering the barrier to entry.

    Sets developers on the right path.

    Great reference guide for seasoned

    veterans.

    Practice what we preach.

  • 7/30/2019 Python for Humans

    66/69

  • 7/30/2019 Python for Humans

    67/69

    Simplify terrible APIs.

  • 7/30/2019 Python for Humans

    68/69

    Document ourbest-practices.

  • 7/30/2019 Python for Humans

    69/69

    github.com/kennethreitzQuestions?

    https://github.com/kennethreitzhttps://github.com/kennethreitzhttps://github.com/kennethreitz