SlideShare a Scribd company logo
Audrey Roy
                          audreyr@cartwheelweb.com
                         http://www.audreymroy.com
                                          @audreyr
PYCON PHILIPPINES 2012




PYTHON TRICKS THAT
YOU CAN’T LIVE
WITHOUT
ABOUT ME

• Principal at Cartwheel Web
• Massachusetts Institute of
  Technology EECS    (winter 2005)


• Filipina-American and very
  proud to be here


                                     flickr.com/photos/chrisjrn/6102009780/
I ♥ PYTHON
• OpenComparison core dev, and contributor to
    various open-source projects

• Co-founded PyLadies
• Helped organize #pyconph
• Python Software Foundation member
•   I even met my fiancé Daniel Greenfeld at PyCon!
•   I even met my fiancé Daniel Greenfeld at PyCon!
•
                                                                                Audrey Roy
                                                                                 @audreyr

                         http://www.flickr.com/photos/47628826@N05/4374285165/
OVERVIEW


• Code readability
• Linters and code checkers
• Where to find free reusable Python libraries
• How to package your code for reuse

                                            Audrey Roy
                                             @audreyr
CODE READABILITY
The #1 trick to being a great Python developer is
      writing clear, understandable code.
CODE READABILITY


• The best Python code is compact, but not
  too compact

• Write self-documenting code
 • And document it anyway :)

                                         Audrey Roy
                                          @audreyr
CODE READABILITY
Can this be made cleaner?
def is_even(x):
    if x % 2 == 0:
        return True
    else:
        return False
CODE READABILITY
Can this be made even cleaner?
def is_even(x):
    if x % 2 == 0:
        return True
    return False
CODE READABILITY
That’s better, but what’s missing?
def is_even(x):
    return x % 2 == 0
CODE READABILITY
Don’t forget your docstrings
def is_even(x):
    """ Returns True if x is even, and
        False if x is odd. """

    return x % 2 == 0
ZEN OF PYTHON
Keep in mind Python’s philosophy as you code.

>>> import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
...
PEP8


• Python style guide
 • 4 spaces. No tabs!
 • Blank lines between function & class defs
 • Much more...

                                          Audrey Roy
                                           @audreyr
TOOLS FOR CODE
  READABILITY
Kind of like spell check, but for code
SUBLIME TEXT 2 + PLUGINS
SublimeLinter

Highlights lines of
code that are not
PEP8-compliant.

Catches potential
style issues or errors.



(also for CSS, JS, PHP,
Ruby, etc.)
SUBLIME TEXT 2 + PLUGINS

  By the way, Sublime Text 2 plugins are
  simple Python files



  To write a plugin, you put a Python file in
  Sublime’s “Packages” directory
PEP8.PY
         A command-line PEP8 checker.


$ pep8 test2.py
test2.py:13:1: E302 expected 2 blank lines, found 1
test2.py:20:1: W391 blank line at end of file




               http://pypi.python.org/pypi/pep8
PYLINT
        Advanced Python source code analyzer.
$ pylint test2.py
No config file found, using default configuration
************* Module test2
C: 1,0: Missing docstring
F: 1,0: Unable to import 'django.db.models'
C: 3,0: Invalid name "compa2lookup" (should match (([A-Z_][A-
Z0-9_]*)|(__.*__))$)
C: 13,0:p_expression_ID: Invalid name "p_expression_ID" (should
match [a-z_][a-z0-9_]{2,30}$)
C: 13,0:p_expression_ID: Invalid name "p" (should match [a-z_][a-
z0-9_]{2,30}$)
C: 13,20:p_expression_ID: Invalid name "p" (should match [a-z_][a-
z0-9_]{2,30}$)
C: 18,4:p_expression_ID: Invalid name "d" (should match [a-z_][a-
z0-9_]{2,30}$)
W: 19,11:p_expression_ID: Used * or ** magic


                   http://pypi.python.org/pypi/pylint
PYLINT
        Advanced Python source code analyzer.
Report
======
8 statements analysed.

Messages by category
--------------------
+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |6       |NC      |NC         |
+-----------+-------+---------+-----------+
|refactor   |0       |NC      |NC         |
+-----------+-------+---------+-----------+
|warning    |1       |NC      |NC         |
+-----------+-------+---------+-----------+

                   http://pypi.python.org/pypi/pylint
FINDING PYTHON
    LIBRARIES
 “Free stuff for Python developers!”
FINDING CODE TO REUSE
Where to get FREE reusable Python libraries:
  1. Python Standard Library
   •   Many great essentials, already on your system!

   •   http://docs.python.org/library/index.html

  2. Python Package Index
   •   21,000+ packages to download!

   •   http://pypi.python.org/


                                                   Audrey Roy
                                                    @audreyr
WHY REUSE CODE?



• Python helps you avoid reinventing the
  wheel

 • “Not Invented Here” syndrome

                                           Audrey Roy
                                            @audreyr
MORE ABOUT THE
    PYTHON STDLIB

A collection of highly useful modules

• No need to install
• Just import and start using them!


                                        Audrey Roy
                                         @audreyr
STDLIB EXAMPLE: MATH
>>> import math
>>> math.ceil(2.03)
3.0
>>> math.floor(2.99)
2.0
>>> math.log(32,2)
5.0
>>> math.erf(0.5)
0.5204998778130465



Mathematical functions defined by the C standard
STDLIB EXAMPLE: RANDOM
>>> import random
>>> random.random()
0.12863367604888531
>>> random.uniform(0,100)
25.374019279313988
>>> math.floor(random.uniform(0,100))
77.0
>>> random.randrange(0,100)
69
MORE ABOUT PYPI


• PyPI is “Python Package Index”
• 21,000+ packages
 • All created by community members like you
• http://pypi.python.org

                                        Audrey Roy
                                         @audreyr
PYPI EXAMPLES


• You saw some great examples already
  from PyPI (Python Package Index)

 • pep8: Simple PEP8 syntax checker
 • pylint: Advanced source code analyzer

                                           Audrey Roy
                                            @audreyr
STDLIB VS. PYPI


• The stdlib is conservative
 • Few additions/changes/deprecations
• On PyPI, anything goes!

                                        Audrey Roy
                                         @audreyr
STDLIB VS. PYPI


• Sometimes PyPI packages are better than
  the equivalent stdlib ones

 • e.g. requests is better than urllib2
• If in doubt, ask around

                                          Audrey Roy
                                           @audreyr
INSTALLING
PYTHON PACKAGES
  The wrong way, and the right way
THE WRONG WAY



• Systemwide installation of Python
  libraries is generally bad

• You can make a mess of your system
                                       Audrey Roy
                                        @audreyr
THE RIGHT WAY

You really should be using these 2 tools:

• pip - a good package installer
• virtualenv - create isolated Python envs

I strongly recommend virtualenvwrapper too.


                                            Audrey Roy
                                             @audreyr
THE RIGHT WAY:
          VIRTUALENV
Create isolated virtualenvs for different projects.

$ workon consumer_io
(consumer_io) $ cd consumer_io/proj/
(consumer_io) $ python manage.py runserver
(consumer_io) $ ...
(consumer_io) $ deactivate
$ cd ../../experiments
$ workon experiments
(experiments) $ python somethingelse.py
(experiments) $ ...
THE RIGHT WAY: PIP

Use pip to install packages into virtualenvs.

(experiments) $ pip install Django==1.4




pip is like easy_install, but much better.
THE RIGHT WAY:
        PIP+VIRTUALENV
SCENARIO:
You use Django 1.3 for work, but you want to
experiment with Django 1.4.

With pip and virtualenv, you can switch between
1.3 and 1.4 on the same computer.
PIP REQUIREMENTS FILES
You should pin your dependencies in requirements.txt!
 $ pip install -r requirements.txt

 # Your requirements.txt file
 Flask==0.8
 glue==0.2.5
 Pillow==1.7.7
 Django==1.4


Use pip install PackageName==1.0.4 for
experimentation only.
AFTER INSTALLATION?
Once installed, you can import Python code
from modules:
from collections import deque


Or from submodules:
from os.path import abspath
WRITING REUSABLE
     CODE
  How code reuse works in Python
MODULES
A module is a file containing Python
definitions and statements.

Like this:

# divisible.py

def is_even(x):
    """ Returns True if x is even, and
        False if x is odd. """

    return x % 2 == 0
PACKAGES
A Python package is a collection of modules.
sound/
    __init__.py
    formats/
        __init__.py
        wav.py
        aiff.py
    effects/
        __init__.py
        echo.py
        surround.py
PACKAGES
A sample import from this package:
from sound.formats.wav import read_wav


sound/
    __init__.py
    formats/
        __init__.py
        wav.py
        aiff.py
    effects/
        __init__.py
        echo.py
        surround.py
INTRA-PACKAGE IMPORTS
Relative imports work between submodules of
a package:
from . import echo
from .. import formats
from ..filters import equalizer
INTRA-PACKAGE IMPORTS
Absolute imports work between submodules of
a package:
# Use this from anywhere in the package
from sound.effects import echo
          package root
IMPORTING FROM
    OUTSIDE A PACKAGE
• Can’t use absolute/relative imports
• What to do? One of these:
   • Good: Add the package to PYTHONPATH
     [edit env var or use sys.path.append()]

   • Better: Install the package into your active
     virtualenv.

                                               Audrey Roy
                                                @audreyr
BETTER PACKAGING

• Recommended reading: “The Hitchhiker’s
  Guide To Packaging”

 • http://guide.python-distribute.org
 • Learn to make packages that are
    downloadable & installable from PyPI


                                           Audrey Roy
                                            @audreyr
THANK YOU


• Find me if you have questions
• Introduce yourself - I’d love to meet you!
• Twitter: @audreyr
• Email: audreyr@cartwheelweb.com

                                           Audrey Roy
                                            @audreyr

More Related Content

What's hot (20)

PDF
Introduction to Pandas and Time Series Analysis [PyCon DE]
Alexander Hendorf
 
PPTX
Python oop third class
Aleksander Fabijan
 
PDF
Data Analysis and Visualization using Python
Chariza Pladin
 
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
PPTX
Object oriented programming with python
Arslan Arshad
 
PPT
Introduction to Python
Nowell Strite
 
PDF
Python list
Mohammed Sikander
 
PPTX
Introduction to OOP in Python
Aleksander Fabijan
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
Object Oriented Programming in Python
Sujith Kumar
 
PDF
What Are Python Modules? Edureka
Edureka!
 
PPTX
Object oriented programming in python
nitamhaske
 
PDF
What is Python JSON | Edureka
Edureka!
 
PPTX
Python-Classes.pptx
Karudaiyar Ganapathy
 
DOCX
Python Interview Questions For Experienced
zynofustechnology
 
PDF
Python Tutorial
AkramWaseem
 
PPTX
Class, object and inheritance in python
Santosh Verma
 
PDF
Feature Engineering
Sri Ambati
 
PPTX
Introduction to numpy Session 1
Jatin Miglani
 
Introduction to Pandas and Time Series Analysis [PyCon DE]
Alexander Hendorf
 
Python oop third class
Aleksander Fabijan
 
Data Analysis and Visualization using Python
Chariza Pladin
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Object oriented programming with python
Arslan Arshad
 
Introduction to Python
Nowell Strite
 
Python list
Mohammed Sikander
 
Introduction to OOP in Python
Aleksander Fabijan
 
Python: Modules and Packages
Damian T. Gordon
 
Object Oriented Programming in Python
Sujith Kumar
 
What Are Python Modules? Edureka
Edureka!
 
Object oriented programming in python
nitamhaske
 
What is Python JSON | Edureka
Edureka!
 
Python-Classes.pptx
Karudaiyar Ganapathy
 
Python Interview Questions For Experienced
zynofustechnology
 
Python Tutorial
AkramWaseem
 
Class, object and inheritance in python
Santosh Verma
 
Feature Engineering
Sri Ambati
 
Introduction to numpy Session 1
Jatin Miglani
 

Viewers also liked (13)

PDF
Python 2 vs. Python 3
Pablo Enfedaque
 
PPTX
Advance OOP concepts in Python
Sujith Kumar
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPT
Introduction to Python
amiable_indian
 
PDF
Python的50道陰影
Tim (文昌)
 
PPTX
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
Cicilia Lee
 
PDF
常用內建模組
Justin Lin
 
PDF
型態與運算子
Justin Lin
 
PDF
進階主題
Justin Lin
 
PDF
Python 起步走
Justin Lin
 
PDF
[系列活動] Python 程式語言起步走
台灣資料科學年會
 
PDF
[系列活動] Python爬蟲實戰
台灣資料科學年會
 
PDF
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
台灣資料科學年會
 
Python 2 vs. Python 3
Pablo Enfedaque
 
Advance OOP concepts in Python
Sujith Kumar
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Introduction to Python
amiable_indian
 
Python的50道陰影
Tim (文昌)
 
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
Cicilia Lee
 
常用內建模組
Justin Lin
 
型態與運算子
Justin Lin
 
進階主題
Justin Lin
 
Python 起步走
Justin Lin
 
[系列活動] Python 程式語言起步走
台灣資料科學年會
 
[系列活動] Python爬蟲實戰
台灣資料科學年會
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
台灣資料科學年會
 
Ad

Similar to Python Tricks That You Can't Live Without (20)

PPTX
Intro to Python for C# Developers
Sarah Dutkiewicz
 
PDF
The quality of the python ecosystem - and how we can protect it!
Bruno Rocha
 
PDF
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
gustyyrauan
 
PDF
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
benhurmaarup
 
PDF
Python Foundation – A programmer's introduction to Python concepts & style
Kevlin Henney
 
PDF
Intro to Python
Daniel Greenfeld
 
PPT
Introduction to the intermediate Python - v1.1
Andrei KUCHARAVY
 
PPTX
Austin Python Learners Meetup - Everything you need to know about programming...
Danny Mulligan
 
PDF
First Steps in Python Programming
Dozie Agbo
 
PPTX
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Devloper
 
PPT
Introduction to Python For Diploma Students
SanjaySampat1
 
PPTX
2024-25 TYBSC(CS)-PYTHON_PROG_ControlStructure.pptx
sangeeta borde
 
PDF
Using Python Libraries.pdf
SoumyadityaDey
 
PPTX
Python-Development (1).pptx PYTHON PROGRAMMING
DrNeetuSharma5
 
PDF
From Basics to Advanced: A Comprehensive Python Programming Guide
pallavichauhan2525
 
PPTX
Complete python toolbox for modern developers
Jan Giacomelli
 
PDF
Python+Deep+Dive+1(Python Course ).pdf
interesadoenhablar
 
PDF
Python_AdvancedUnit - 3.pdf about the python
gpsign134
 
PDF
Welcome to Python
Elena Williams
 
Intro to Python for C# Developers
Sarah Dutkiewicz
 
The quality of the python ecosystem - and how we can protect it!
Bruno Rocha
 
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
gustyyrauan
 
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
benhurmaarup
 
Python Foundation – A programmer's introduction to Python concepts & style
Kevlin Henney
 
Intro to Python
Daniel Greenfeld
 
Introduction to the intermediate Python - v1.1
Andrei KUCHARAVY
 
Austin Python Learners Meetup - Everything you need to know about programming...
Danny Mulligan
 
First Steps in Python Programming
Dozie Agbo
 
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Devloper
 
Introduction to Python For Diploma Students
SanjaySampat1
 
2024-25 TYBSC(CS)-PYTHON_PROG_ControlStructure.pptx
sangeeta borde
 
Using Python Libraries.pdf
SoumyadityaDey
 
Python-Development (1).pptx PYTHON PROGRAMMING
DrNeetuSharma5
 
From Basics to Advanced: A Comprehensive Python Programming Guide
pallavichauhan2525
 
Complete python toolbox for modern developers
Jan Giacomelli
 
Python+Deep+Dive+1(Python Course ).pdf
interesadoenhablar
 
Python_AdvancedUnit - 3.pdf about the python
gpsign134
 
Welcome to Python
Elena Williams
 
Ad

Recently uploaded (20)

PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Python basic programing language for automation
DanialHabibi2
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Python basic programing language for automation
DanialHabibi2
 

Python Tricks That You Can't Live Without

  • 1. Audrey Roy [email protected] http://www.audreymroy.com @audreyr PYCON PHILIPPINES 2012 PYTHON TRICKS THAT YOU CAN’T LIVE WITHOUT
  • 2. ABOUT ME • Principal at Cartwheel Web • Massachusetts Institute of Technology EECS (winter 2005) • Filipina-American and very proud to be here flickr.com/photos/chrisjrn/6102009780/
  • 3. I ♥ PYTHON • OpenComparison core dev, and contributor to various open-source projects • Co-founded PyLadies • Helped organize #pyconph • Python Software Foundation member • I even met my fiancé Daniel Greenfeld at PyCon! • I even met my fiancé Daniel Greenfeld at PyCon! • Audrey Roy @audreyr http://www.flickr.com/photos/47628826@N05/4374285165/
  • 4. OVERVIEW • Code readability • Linters and code checkers • Where to find free reusable Python libraries • How to package your code for reuse Audrey Roy @audreyr
  • 5. CODE READABILITY The #1 trick to being a great Python developer is writing clear, understandable code.
  • 6. CODE READABILITY • The best Python code is compact, but not too compact • Write self-documenting code • And document it anyway :) Audrey Roy @audreyr
  • 7. CODE READABILITY Can this be made cleaner? def is_even(x): if x % 2 == 0: return True else: return False
  • 8. CODE READABILITY Can this be made even cleaner? def is_even(x): if x % 2 == 0: return True return False
  • 9. CODE READABILITY That’s better, but what’s missing? def is_even(x): return x % 2 == 0
  • 10. CODE READABILITY Don’t forget your docstrings def is_even(x): """ Returns True if x is even, and False if x is odd. """ return x % 2 == 0
  • 11. ZEN OF PYTHON Keep in mind Python’s philosophy as you code. >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. ...
  • 12. PEP8 • Python style guide • 4 spaces. No tabs! • Blank lines between function & class defs • Much more... Audrey Roy @audreyr
  • 13. TOOLS FOR CODE READABILITY Kind of like spell check, but for code
  • 14. SUBLIME TEXT 2 + PLUGINS SublimeLinter Highlights lines of code that are not PEP8-compliant. Catches potential style issues or errors. (also for CSS, JS, PHP, Ruby, etc.)
  • 15. SUBLIME TEXT 2 + PLUGINS By the way, Sublime Text 2 plugins are simple Python files To write a plugin, you put a Python file in Sublime’s “Packages” directory
  • 16. PEP8.PY A command-line PEP8 checker. $ pep8 test2.py test2.py:13:1: E302 expected 2 blank lines, found 1 test2.py:20:1: W391 blank line at end of file http://pypi.python.org/pypi/pep8
  • 17. PYLINT Advanced Python source code analyzer. $ pylint test2.py No config file found, using default configuration ************* Module test2 C: 1,0: Missing docstring F: 1,0: Unable to import 'django.db.models' C: 3,0: Invalid name "compa2lookup" (should match (([A-Z_][A- Z0-9_]*)|(__.*__))$) C: 13,0:p_expression_ID: Invalid name "p_expression_ID" (should match [a-z_][a-z0-9_]{2,30}$) C: 13,0:p_expression_ID: Invalid name "p" (should match [a-z_][a- z0-9_]{2,30}$) C: 13,20:p_expression_ID: Invalid name "p" (should match [a-z_][a- z0-9_]{2,30}$) C: 18,4:p_expression_ID: Invalid name "d" (should match [a-z_][a- z0-9_]{2,30}$) W: 19,11:p_expression_ID: Used * or ** magic http://pypi.python.org/pypi/pylint
  • 18. PYLINT Advanced Python source code analyzer. Report ====== 8 statements analysed. Messages by category -------------------- +-----------+-------+---------+-----------+ |type |number |previous |difference | +===========+=======+=========+===========+ |convention |6 |NC |NC | +-----------+-------+---------+-----------+ |refactor |0 |NC |NC | +-----------+-------+---------+-----------+ |warning |1 |NC |NC | +-----------+-------+---------+-----------+ http://pypi.python.org/pypi/pylint
  • 19. FINDING PYTHON LIBRARIES “Free stuff for Python developers!”
  • 20. FINDING CODE TO REUSE Where to get FREE reusable Python libraries: 1. Python Standard Library • Many great essentials, already on your system! • http://docs.python.org/library/index.html 2. Python Package Index • 21,000+ packages to download! • http://pypi.python.org/ Audrey Roy @audreyr
  • 21. WHY REUSE CODE? • Python helps you avoid reinventing the wheel • “Not Invented Here” syndrome Audrey Roy @audreyr
  • 22. MORE ABOUT THE PYTHON STDLIB A collection of highly useful modules • No need to install • Just import and start using them! Audrey Roy @audreyr
  • 23. STDLIB EXAMPLE: MATH >>> import math >>> math.ceil(2.03) 3.0 >>> math.floor(2.99) 2.0 >>> math.log(32,2) 5.0 >>> math.erf(0.5) 0.5204998778130465 Mathematical functions defined by the C standard
  • 24. STDLIB EXAMPLE: RANDOM >>> import random >>> random.random() 0.12863367604888531 >>> random.uniform(0,100) 25.374019279313988 >>> math.floor(random.uniform(0,100)) 77.0 >>> random.randrange(0,100) 69
  • 25. MORE ABOUT PYPI • PyPI is “Python Package Index” • 21,000+ packages • All created by community members like you • http://pypi.python.org Audrey Roy @audreyr
  • 26. PYPI EXAMPLES • You saw some great examples already from PyPI (Python Package Index) • pep8: Simple PEP8 syntax checker • pylint: Advanced source code analyzer Audrey Roy @audreyr
  • 27. STDLIB VS. PYPI • The stdlib is conservative • Few additions/changes/deprecations • On PyPI, anything goes! Audrey Roy @audreyr
  • 28. STDLIB VS. PYPI • Sometimes PyPI packages are better than the equivalent stdlib ones • e.g. requests is better than urllib2 • If in doubt, ask around Audrey Roy @audreyr
  • 29. INSTALLING PYTHON PACKAGES The wrong way, and the right way
  • 30. THE WRONG WAY • Systemwide installation of Python libraries is generally bad • You can make a mess of your system Audrey Roy @audreyr
  • 31. THE RIGHT WAY You really should be using these 2 tools: • pip - a good package installer • virtualenv - create isolated Python envs I strongly recommend virtualenvwrapper too. Audrey Roy @audreyr
  • 32. THE RIGHT WAY: VIRTUALENV Create isolated virtualenvs for different projects. $ workon consumer_io (consumer_io) $ cd consumer_io/proj/ (consumer_io) $ python manage.py runserver (consumer_io) $ ... (consumer_io) $ deactivate $ cd ../../experiments $ workon experiments (experiments) $ python somethingelse.py (experiments) $ ...
  • 33. THE RIGHT WAY: PIP Use pip to install packages into virtualenvs. (experiments) $ pip install Django==1.4 pip is like easy_install, but much better.
  • 34. THE RIGHT WAY: PIP+VIRTUALENV SCENARIO: You use Django 1.3 for work, but you want to experiment with Django 1.4. With pip and virtualenv, you can switch between 1.3 and 1.4 on the same computer.
  • 35. PIP REQUIREMENTS FILES You should pin your dependencies in requirements.txt! $ pip install -r requirements.txt # Your requirements.txt file Flask==0.8 glue==0.2.5 Pillow==1.7.7 Django==1.4 Use pip install PackageName==1.0.4 for experimentation only.
  • 36. AFTER INSTALLATION? Once installed, you can import Python code from modules: from collections import deque Or from submodules: from os.path import abspath
  • 37. WRITING REUSABLE CODE How code reuse works in Python
  • 38. MODULES A module is a file containing Python definitions and statements. Like this: # divisible.py def is_even(x): """ Returns True if x is even, and False if x is odd. """ return x % 2 == 0
  • 39. PACKAGES A Python package is a collection of modules. sound/ __init__.py formats/ __init__.py wav.py aiff.py effects/ __init__.py echo.py surround.py
  • 40. PACKAGES A sample import from this package: from sound.formats.wav import read_wav sound/ __init__.py formats/ __init__.py wav.py aiff.py effects/ __init__.py echo.py surround.py
  • 41. INTRA-PACKAGE IMPORTS Relative imports work between submodules of a package: from . import echo from .. import formats from ..filters import equalizer
  • 42. INTRA-PACKAGE IMPORTS Absolute imports work between submodules of a package: # Use this from anywhere in the package from sound.effects import echo package root
  • 43. IMPORTING FROM OUTSIDE A PACKAGE • Can’t use absolute/relative imports • What to do? One of these: • Good: Add the package to PYTHONPATH [edit env var or use sys.path.append()] • Better: Install the package into your active virtualenv. Audrey Roy @audreyr
  • 44. BETTER PACKAGING • Recommended reading: “The Hitchhiker’s Guide To Packaging” • http://guide.python-distribute.org • Learn to make packages that are downloadable & installable from PyPI Audrey Roy @audreyr
  • 45. THANK YOU • Find me if you have questions • Introduce yourself - I’d love to meet you! • Twitter: @audreyr • Email: [email protected] Audrey Roy @audreyr

Editor's Notes