SlideShare a Scribd company logo
Better, faster, smarter



   Python: yesterday, today... and tomorrow




©2006 Alex Martelli aleax@google.com
                                              1
Outline of this talk
a short reflection on Python evolution
    2.2 -> 2.3 -> 2.4 -> ...
... -> highlights of Python 2.5
    the “with” statement (RAII)
    other language changes
    additions to the standard library
    optimizations
Q&A
    Qs are also welcome during the talk!-)


                    2
                                             2
1 lang, many versions
Jython (pending 2.2/2.3 release)
IronPython (1.0, 8/06, Microsoft ~ CPython 2.4)
pypy (0.9, 6/06 ~ CPython 2.4, but “beta”)
CPython (Classic Python) timeline
  2.2: 12/01 (...2.2.3: 5/03) major new stuff
  2.3: 7/03 (...2.3.5: 2/05) ~30% faster
  2.4: 11/04 (...2.4.4: 9/06) ~5% faster yet
  2.5: 9/06 (...?) ~10% (?) faster yet



                     3
                                                  3
2.2               2.2.2
Dec 2001          Oct 2002
2.2    2.2.1                 2.2.3
       Apr 2002              Mar 2003




Apr 2002   Oct 2002   Apr 2003     Oct 2003   Apr 2004     Oct 2004   Apr 2005       Oct 2005   Apr 2006

                                 2.3
                                   2.3
                                   Jul 2003

                                        2.3.1
                                        Sep 2003

                                         2.3.2
                                         Oct 2003

                                              2.3.3
                                              Dec 2003

                                                         2.3.4            2.3.5
                                                         May 2004         Feb 2005



                                                                    2.4
Apr 2002   Oct 2002   Apr 2003     Oct 2003   Apr 2004     Oct 2004 Nov 2004
                                                                       Apr 2005      Oct 2005   Apr 2006
                                                                                                           2.5
                                                                    2.4    2.4.1         2.4.2
                                                                                         Sep 2005
                                                                                                    2.4.3
                                                                           Mar 2005                 Mar 2006




Apr 2002   Oct 2002   Apr 2003     Oct 2003   Apr 2004     Oct 2004   Apr 2005       Oct 2005   Apr 2006
                                                    4
                                                                                                               4
The 2.2 “revolution”
2.2 was a “backwards-compatible” revolution
  new-style object model, descriptors,
  custom metaclasses...
  iterators and generators
  nested scopes
  int/long merge, new division, bool (2.2.3)
  standard library: XML/RPC (clients and
  servers), IPv6 support, email, UCS-4, ...
nothing THAT big since, plus, new rule:
  2.N.* has NO extra features wrt 2.N

                    5
                                           5
2.2 highlights
class Newy(object): ...
__metaclass__ = ...

def funmaker(...):
  def madefun(...): ...
  return madefun

def gen1(item):
  yield item

for item in iter(f, sentinel): ...

                    6
                                     6
2.3: stable evolution
no changes to the language “proper”
many, MANY optimizations/tweaks/fixes
  import-from-ZIP, ever-more-amazing
  sort, Karatsuba multiplication, pymalloc,
  interned strs gc’able ...
builtins: sum, enumerate, extended slices,
enhancements to str, dict, list, file, ...
stdlib, many new modules: bz2, csv,
datetime, heapq, itertools, logging, optparse,
platform, sets, tarfile, textwrap, timeit
  & many enhancements: socket timeouts, ...
                    7
                                             7
2.3 highlights
sys.path.append(‘some.zip’)

sum([x**2 for x in xs if x%2])

for i, x in enumerate(xs): ...

print ‘ciao’[::-1]

for line in open(fn, ‘U’): ...

...and MANY new stdlib modules...!

                     8
                                     8
2.4: mostly evolution
“small” new language features:
  genexps, decorators
  many “peephole-level” optimizations
builtins: sorted, reversed; enhancements to
sort, str; set becomes built-in
stdlib, new modules: collections, cookielib,
decimal, subprocess
  string.Template, faster bisect & heapq,
  operator itemgetter & attrgetter,
  os.urandom, threading.local, ...

                    9
                                               9
2.4 language changes
sum(x**2 for x in xs if x%2)
like sum([x**2 for x in xs if x%2])
(without actually building the list!)

class Foo(object):
  @classmethod
  def bar(cls): return cls.__name__
print Foo().bar(), Foo.bar()
emits: Foo Foo


                     10
                                        10
2.4 new built-ins
for item in sorted(sequence): ...
(does not alter sequence in-place!)

for item in reversed(sequence): ...
(does not alter sequence in-place; like...
for item in sequence[::-1]: ...
...but, more readable!-)

set   and frozenzet become built-in types


                       11
                                             11
2.4 built-ins additions
print ‘foo’.center(7, ‘+’)
emits: ++foo++

print ‘foo+bar+baz’.rsplit(‘+’,1)[-1]
emits: baz

print sorted(‘abc d ef g’.split(), key=len)
emits: [‘d’, ‘g’, ‘ef’, ‘abc’]



                    12
                                          12
2.4 new stdlib modules
collections.deque
double-ended queue -- methods: append,
appendleft, extend, extendleft, pop,
popleft, rotate

decimal.Decimal
specified-precision decimal floating point
number, IEEE-754 compliant

subprocess.Popen
spawn and control a sub-process
                     13
                                           13
2.4 stdlib additions
list2d.sort(key=operator.itemgetter(1))

os.urandom(n) -> n crypto-strong byte str

threading.local() -> TLS (attrs bundle)

heapq.nlargest(n, sequence)
also .nsmallest (whole module much faster)



                     14
                                             14
2.5: evolution... plus!
several language changes:
  full support for “RAII”-style programming
    new “with” statement, new contextlib
    module, generator enhancements...
  absolute/relative imports, unified “try/
  except/finally” statement, “if/else”
  operator, exceptions are new-style
new builtins: any/all, dict.__missing__
new stdlib modules: ctypes, xml.etree,
functools, hashlib, sqlite3, wsgiref, ...

                   15
                                          15
2.5: many optimizations
sets/frozensets recoded from scratch
many speed-ups to string operations
substantial speed-ups in struct
new-style exceptions are faster
and many minor optimization tweaks
  smaller and phantom frames in calls
  re uses Python allocator
  some constant-folding at compile time
  fewer system calls during importing
  ...
                   16
                                          16
Resource Allocation Is Initialization
# in 2.4 and earlier, Java-like...:
resource = ...allocate it...
try:
  ...use the resource...
finally:
  ...free the resource...

# in 2.5, much “slimmer”...:
with ...allocate it... as resource:
  ...use the resource...
with automatic “freeing” at block exit!
                      17
                                           17
Many “with”-ready types
with open(filename) as f:
   ...work with file f...
# auto f.close() on block exit

somelock = threading.Lock()

with somelock:
  # auto somelock.acquire() on block entry
  ...work guarded by somelock...
# auto somelock.release() on block exit


                    18
                                         18
The “with” statement
from __future__ import with_statement
with <expr> as var: <with-block>

# makes and uses a *context manager*
_context = <expr>
var = _context.__enter__()
try: <with-block>
except: _context.__exit__(*sys.exc_info())
else: _context.__exit__(None, None, None)

Better than C++: can distinguish exception
exits from normal ones!
                      19
                                             19
Your own context mgrs
roll-your-own: write a wrapper class
   usually __init__ for initialization
   __enter__(self) returns useful “var”
   __exit__(self, ext, exv, tbv) performs the
   needed termination operations (exit is
   “normal” iff args are all None)
extremely general
slightly clunky/boilerplatey


                    20
                                            20
“with” for transactions
class Transaction(object):
 def __init__(self, c): self.c = c
 def __enter__(self): return self.c.cursor()
 def __exit__(self, ext, exv, tbv):
   if ext is None: self.c.commit()
   else: self.c.rollback()

with Transaction(connection) as cursor:
   cursor.execute(...)
   ...and more processing as needed...


                     21
                                           21
Your own context mgrs
contextlib module can help in many ways
decorator contextlib.contextmanager lets
you write a context mgr as a generator
  yield the desired result of __enter__
  within a try/finally or try/except/else
  re-raise exception in try/except case
function contextlib.nested “nests” context
managers without needing special syntax
function contextlib.closing(x) just returns x
and calls x.close() at block exit

                    22
                                                22
Transaction w/contextlib
import contextlib

@contextlib.contextmanager
def Transaction(c):
  cursor = c.cursor()
  try: yield cursor
  except:
    c.rollback()
    raise
  else:
    c.commit()

                     23
                              23
Other uses of contextlib
# syntax-free “nesting”
# e.g., a locked transaction:
with contextlib.nested(thelock,
  Transaction(c)) as (locked, cursor): ...
# auto commit or rollback, AND auto
# thelock.release, on block exit

# when all you need is closing, e.g:
with contextlib.closing(
         urllib.urlopen(...)) as f:
  ...work with pseudo-file object f...
# auto f.close() on block exit
                    24
                                         24
Generator enhancements
yield can be inside a try-clause
yield is now an expression
  x = g.send(value) gives yield’s value
  x = g.next() is like x = g.send(None)
  preferred syntax: value = (yield result)
g.throw(type [,value [,traceback]])
  g.close() is like g.throw(GeneratorExit)
automatic g.close() when g is garbage-
collected
  this is what ensures try/finally works!

                    25
                                             25
Absolute/relative imports
from __future__ import absolute_import
  means: import X finds X in sys.path
  you can import .X to find X in the
  current package
  also import ..X to find X in the
  package containing the current one, etc
important “future” simplification of imports




                    26
                                              26
try/except/finally
try: <body>
except <spec>: <handler>
else: <ebody> # else-clause is optional
finally: <finalizer>
becomes equivalent to:
try:
  try: <body>
  except <spec>: <handler>
  else: <ebody>
finally: <finalizer>

                     27
                                          27
if/else ternary operator
result = (whentrue if cond else whenfalse)
becomes equivalent to:
if cond:
  result = whentrue
else:
  result = whenfalse

  the parentheses are technically optional (!)
  meant to help with lambda & the like
  somewhat-controversial syntax...:-)

                       28
                                                 28
Exceptions are new-style
BaseException
    KeyboardInterrupt
    Exception
        GeneratorExit
        StandardError
            ArithmeticError
            EnvironmentError
            LookupError
            # other “true” error classes
        StopIteration
        SystemExit
        Warning
    SystemExit

                    29
                                           29
any and all
def any(seq):
  for item in seq:
    if item: return True
  return False

def all(seq):
  for item in seq:
    if not item: return False
  return True

note: RIGHT behavior for empty sequence!
                    30
                                           30
dict.__missing__
 hook method called by __setitem__ if the
 key is missing (==not in the dict)
 default implementation in dict itself:
def __missing__(self, key):
  raise KeyError(key)
 meant to be overridden by subclasses
 collections.defaultdict subclasses dict:
def __missing__(self, key):
  return self.default_factory()
   default_factory optionally set at
   __init__ (default None == raise)
                      31
                                            31
ctypes
load any shared library / DLL with
ctypes.CDLL(<complete name of library>)
call any function as a method of the CDLL
automatically converts to int and char*
other conversions explicit with c_int,
c_float, create_string_buffer, ...
also accesses Python’s C API
essentially: a general-purpose Python FFI !
dangerous: any programer mistake or
oversight can easily crash Python!

                   32
                                              32
Element-Tree
new package xml.etree with modules
ElementTree, ElementPath, ElementInclude
highly Pythonic in-memory representation
of XML document as tree, much slimmer
(and faster!) than the DOM
each XML element is a bit like a list of its
children merged with a dict of its attrs
scalable to large documents with included C
accelerators and .iterparse incremental
parsing (a bit like pulldom, but simpler, and
keeps subtrees by default)

                    33
                                            33
functools
functools.partial for “partial
application” (AKA “currying”)
functools.update_wrapper for proper
setting of metadata for functions that wrap
other functions
functools.wraps: decorator equivalent of
functools.update_wrapper




                   34
                                          34
hashlib
replaces md5 and sha modules (which
become wrappers to it!)
adds SHA-224, SHA-256, SHA-384, SHA-512
optionally uses OpenSSL as accelerator (but
can be pure-Python if necessary!)




                   35
                                          35
sqlite3
wrapper for the SQLite embedded DB
DB-API-2 compliant interface
  except that SQLite is “typeless” (!)
  some extensions: optional timeout on
  connect, isolation level, detect_type and
  type converters, executemany on
  iterators, executescript method, ...
great way to “get started” on small app,
can later migrate to PostgreSQL or other
relational DB (MySQL, Oracle, whatever)


                    36
                                              36
wsgiref
Web Server Gateway Interface
standard “middleware” interface between
HTTP servers and Python web frameworks
  goal: any framework, any server
  non-goal: direct use by web applications!
  already widely supported by frameworks
  http://www.wsgi.org/wsgi for more!
stdlib now includes a “reference
implementation” of WSGI (wsgiref)
  includes basic HTTP server for debugging
  WSGI applications and interfaces
                   37
                                              37

More Related Content

Similar to py25

Advanced Bash Scripting Guide 2002
Advanced Bash Scripting Guide 2002Advanced Bash Scripting Guide 2002
Advanced Bash Scripting Guide 2002
duquoi
 
How to write LaTeX package
How to write LaTeX packageHow to write LaTeX package
How to write LaTeX package
Ruini Xue
 
rails_tutorial
rails_tutorialrails_tutorial
rails_tutorial
tutorialsruby
 
rails_tutorial
rails_tutorialrails_tutorial
rails_tutorial
tutorialsruby
 
How to make DSL
How to make DSLHow to make DSL
How to make DSL
Yukio Goto
 
Es04
Es04Es04
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9
tomaspavelka
 
Lu solr 20100709
Lu solr 20100709Lu solr 20100709
Lu solr 20100709
Koji Sekiguchi
 
IPW2008 - my.opera.com scalability
IPW2008 - my.opera.com scalabilityIPW2008 - my.opera.com scalability
IPW2008 - my.opera.com scalability
Cosimo Streppone
 
Getting started erlang
Getting started erlangGetting started erlang
Getting started erlang
Kwanzoo Dev
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
bobmcwhirter
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js Tutorial
Tom Croucher
 
AES Proposal
AES ProposalAES Proposal
AES Proposal
rodrigovmoraes
 
Te xworks manual
Te xworks manualTe xworks manual
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar BatsovRuby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Michael Kimathi
 
What’s New in Docker - Victor Vieux, Docker
What’s New in Docker - Victor Vieux, DockerWhat’s New in Docker - Victor Vieux, Docker
What’s New in Docker - Victor Vieux, Docker
Docker, Inc.
 
All bugfixes are incompatibilities
All bugfixes are incompatibilitiesAll bugfixes are incompatibilities
All bugfixes are incompatibilities
nagachika t
 
Slides Aquarium Paris 2008
Slides Aquarium Paris 2008Slides Aquarium Paris 2008
Slides Aquarium Paris 2008
julien.ponge
 
Upgrading to Rails 3
Upgrading to Rails 3Upgrading to Rails 3
Upgrading to Rails 3
juliangiuca
 
Profiling blueprints
Profiling blueprintsProfiling blueprints
Profiling blueprints
bergel
 

Similar to py25 (20)

Advanced Bash Scripting Guide 2002
Advanced Bash Scripting Guide 2002Advanced Bash Scripting Guide 2002
Advanced Bash Scripting Guide 2002
 
How to write LaTeX package
How to write LaTeX packageHow to write LaTeX package
How to write LaTeX package
 
rails_tutorial
rails_tutorialrails_tutorial
rails_tutorial
 
rails_tutorial
rails_tutorialrails_tutorial
rails_tutorial
 
How to make DSL
How to make DSLHow to make DSL
How to make DSL
 
Es04
Es04Es04
Es04
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9
 
Lu solr 20100709
Lu solr 20100709Lu solr 20100709
Lu solr 20100709
 
IPW2008 - my.opera.com scalability
IPW2008 - my.opera.com scalabilityIPW2008 - my.opera.com scalability
IPW2008 - my.opera.com scalability
 
Getting started erlang
Getting started erlangGetting started erlang
Getting started erlang
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js Tutorial
 
AES Proposal
AES ProposalAES Proposal
AES Proposal
 
Te xworks manual
Te xworks manualTe xworks manual
Te xworks manual
 
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar BatsovRuby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
 
What’s New in Docker - Victor Vieux, Docker
What’s New in Docker - Victor Vieux, DockerWhat’s New in Docker - Victor Vieux, Docker
What’s New in Docker - Victor Vieux, Docker
 
All bugfixes are incompatibilities
All bugfixes are incompatibilitiesAll bugfixes are incompatibilities
All bugfixes are incompatibilities
 
Slides Aquarium Paris 2008
Slides Aquarium Paris 2008Slides Aquarium Paris 2008
Slides Aquarium Paris 2008
 
Upgrading to Rails 3
Upgrading to Rails 3Upgrading to Rails 3
Upgrading to Rails 3
 
Profiling blueprints
Profiling blueprintsProfiling blueprints
Profiling blueprints
 

More from webuploader

Michael_Hulme_Banff_Social_Networking
Michael_Hulme_Banff_Social_NetworkingMichael_Hulme_Banff_Social_Networking
Michael_Hulme_Banff_Social_Networking
webuploader
 
socialpref
socialprefsocialpref
socialpref
webuploader
 
cyberSecurity_Milliron
cyberSecurity_MillironcyberSecurity_Milliron
cyberSecurity_Milliron
webuploader
 
PJO-3B
PJO-3BPJO-3B
PJO-3B
webuploader
 
LiveseyMotleyPresentation
LiveseyMotleyPresentationLiveseyMotleyPresentation
LiveseyMotleyPresentation
webuploader
 
FairShare_Morningstar_022607
FairShare_Morningstar_022607FairShare_Morningstar_022607
FairShare_Morningstar_022607
webuploader
 
saito_porcupine
saito_porcupinesaito_porcupine
saito_porcupine
webuploader
 
3_System_Requirements_and_Scaling
3_System_Requirements_and_Scaling3_System_Requirements_and_Scaling
3_System_Requirements_and_Scaling
webuploader
 
ScalabilityAvailability
ScalabilityAvailabilityScalabilityAvailability
ScalabilityAvailability
webuploader
 
scale_perf_best_practices
scale_perf_best_practicesscale_perf_best_practices
scale_perf_best_practices
webuploader
 
7496_Hall 070204 Research Faculty Summit
7496_Hall 070204 Research Faculty Summit7496_Hall 070204 Research Faculty Summit
7496_Hall 070204 Research Faculty Summit
webuploader
 
Chapter5
Chapter5Chapter5
Chapter5
webuploader
 
Mak3
Mak3Mak3
visagie_freebsd
visagie_freebsdvisagie_freebsd
visagie_freebsd
webuploader
 
freebsd-watitis
freebsd-watitisfreebsd-watitis
freebsd-watitis
webuploader
 
BPotter-L1-05
BPotter-L1-05BPotter-L1-05
BPotter-L1-05
webuploader
 
FreeBSD - LinuxExpo
FreeBSD - LinuxExpoFreeBSD - LinuxExpo
FreeBSD - LinuxExpo
webuploader
 
CLI313
CLI313CLI313
CLI313
webuploader
 
CFInterop
CFInteropCFInterop
CFInterop
webuploader
 
WCE031_WH06
WCE031_WH06WCE031_WH06
WCE031_WH06
webuploader
 

More from webuploader (20)

Michael_Hulme_Banff_Social_Networking
Michael_Hulme_Banff_Social_NetworkingMichael_Hulme_Banff_Social_Networking
Michael_Hulme_Banff_Social_Networking
 
socialpref
socialprefsocialpref
socialpref
 
cyberSecurity_Milliron
cyberSecurity_MillironcyberSecurity_Milliron
cyberSecurity_Milliron
 
PJO-3B
PJO-3BPJO-3B
PJO-3B
 
LiveseyMotleyPresentation
LiveseyMotleyPresentationLiveseyMotleyPresentation
LiveseyMotleyPresentation
 
FairShare_Morningstar_022607
FairShare_Morningstar_022607FairShare_Morningstar_022607
FairShare_Morningstar_022607
 
saito_porcupine
saito_porcupinesaito_porcupine
saito_porcupine
 
3_System_Requirements_and_Scaling
3_System_Requirements_and_Scaling3_System_Requirements_and_Scaling
3_System_Requirements_and_Scaling
 
ScalabilityAvailability
ScalabilityAvailabilityScalabilityAvailability
ScalabilityAvailability
 
scale_perf_best_practices
scale_perf_best_practicesscale_perf_best_practices
scale_perf_best_practices
 
7496_Hall 070204 Research Faculty Summit
7496_Hall 070204 Research Faculty Summit7496_Hall 070204 Research Faculty Summit
7496_Hall 070204 Research Faculty Summit
 
Chapter5
Chapter5Chapter5
Chapter5
 
Mak3
Mak3Mak3
Mak3
 
visagie_freebsd
visagie_freebsdvisagie_freebsd
visagie_freebsd
 
freebsd-watitis
freebsd-watitisfreebsd-watitis
freebsd-watitis
 
BPotter-L1-05
BPotter-L1-05BPotter-L1-05
BPotter-L1-05
 
FreeBSD - LinuxExpo
FreeBSD - LinuxExpoFreeBSD - LinuxExpo
FreeBSD - LinuxExpo
 
CLI313
CLI313CLI313
CLI313
 
CFInterop
CFInteropCFInterop
CFInterop
 
WCE031_WH06
WCE031_WH06WCE031_WH06
WCE031_WH06
 

Recently uploaded

Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 

Recently uploaded (20)

Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 

py25

  • 1. Better, faster, smarter Python: yesterday, today... and tomorrow ©2006 Alex Martelli aleax@google.com 1
  • 2. Outline of this talk a short reflection on Python evolution 2.2 -> 2.3 -> 2.4 -> ... ... -> highlights of Python 2.5 the “with” statement (RAII) other language changes additions to the standard library optimizations Q&A Qs are also welcome during the talk!-) 2 2
  • 3. 1 lang, many versions Jython (pending 2.2/2.3 release) IronPython (1.0, 8/06, Microsoft ~ CPython 2.4) pypy (0.9, 6/06 ~ CPython 2.4, but “beta”) CPython (Classic Python) timeline 2.2: 12/01 (...2.2.3: 5/03) major new stuff 2.3: 7/03 (...2.3.5: 2/05) ~30% faster 2.4: 11/04 (...2.4.4: 9/06) ~5% faster yet 2.5: 9/06 (...?) ~10% (?) faster yet 3 3
  • 4. 2.2 2.2.2 Dec 2001 Oct 2002 2.2 2.2.1 2.2.3 Apr 2002 Mar 2003 Apr 2002 Oct 2002 Apr 2003 Oct 2003 Apr 2004 Oct 2004 Apr 2005 Oct 2005 Apr 2006 2.3 2.3 Jul 2003 2.3.1 Sep 2003 2.3.2 Oct 2003 2.3.3 Dec 2003 2.3.4 2.3.5 May 2004 Feb 2005 2.4 Apr 2002 Oct 2002 Apr 2003 Oct 2003 Apr 2004 Oct 2004 Nov 2004 Apr 2005 Oct 2005 Apr 2006 2.5 2.4 2.4.1 2.4.2 Sep 2005 2.4.3 Mar 2005 Mar 2006 Apr 2002 Oct 2002 Apr 2003 Oct 2003 Apr 2004 Oct 2004 Apr 2005 Oct 2005 Apr 2006 4 4
  • 5. The 2.2 “revolution” 2.2 was a “backwards-compatible” revolution new-style object model, descriptors, custom metaclasses... iterators and generators nested scopes int/long merge, new division, bool (2.2.3) standard library: XML/RPC (clients and servers), IPv6 support, email, UCS-4, ... nothing THAT big since, plus, new rule: 2.N.* has NO extra features wrt 2.N 5 5
  • 6. 2.2 highlights class Newy(object): ... __metaclass__ = ... def funmaker(...): def madefun(...): ... return madefun def gen1(item): yield item for item in iter(f, sentinel): ... 6 6
  • 7. 2.3: stable evolution no changes to the language “proper” many, MANY optimizations/tweaks/fixes import-from-ZIP, ever-more-amazing sort, Karatsuba multiplication, pymalloc, interned strs gc’able ... builtins: sum, enumerate, extended slices, enhancements to str, dict, list, file, ... stdlib, many new modules: bz2, csv, datetime, heapq, itertools, logging, optparse, platform, sets, tarfile, textwrap, timeit & many enhancements: socket timeouts, ... 7 7
  • 8. 2.3 highlights sys.path.append(‘some.zip’) sum([x**2 for x in xs if x%2]) for i, x in enumerate(xs): ... print ‘ciao’[::-1] for line in open(fn, ‘U’): ... ...and MANY new stdlib modules...! 8 8
  • 9. 2.4: mostly evolution “small” new language features: genexps, decorators many “peephole-level” optimizations builtins: sorted, reversed; enhancements to sort, str; set becomes built-in stdlib, new modules: collections, cookielib, decimal, subprocess string.Template, faster bisect & heapq, operator itemgetter & attrgetter, os.urandom, threading.local, ... 9 9
  • 10. 2.4 language changes sum(x**2 for x in xs if x%2) like sum([x**2 for x in xs if x%2]) (without actually building the list!) class Foo(object): @classmethod def bar(cls): return cls.__name__ print Foo().bar(), Foo.bar() emits: Foo Foo 10 10
  • 11. 2.4 new built-ins for item in sorted(sequence): ... (does not alter sequence in-place!) for item in reversed(sequence): ... (does not alter sequence in-place; like... for item in sequence[::-1]: ... ...but, more readable!-) set and frozenzet become built-in types 11 11
  • 12. 2.4 built-ins additions print ‘foo’.center(7, ‘+’) emits: ++foo++ print ‘foo+bar+baz’.rsplit(‘+’,1)[-1] emits: baz print sorted(‘abc d ef g’.split(), key=len) emits: [‘d’, ‘g’, ‘ef’, ‘abc’] 12 12
  • 13. 2.4 new stdlib modules collections.deque double-ended queue -- methods: append, appendleft, extend, extendleft, pop, popleft, rotate decimal.Decimal specified-precision decimal floating point number, IEEE-754 compliant subprocess.Popen spawn and control a sub-process 13 13
  • 14. 2.4 stdlib additions list2d.sort(key=operator.itemgetter(1)) os.urandom(n) -> n crypto-strong byte str threading.local() -> TLS (attrs bundle) heapq.nlargest(n, sequence) also .nsmallest (whole module much faster) 14 14
  • 15. 2.5: evolution... plus! several language changes: full support for “RAII”-style programming new “with” statement, new contextlib module, generator enhancements... absolute/relative imports, unified “try/ except/finally” statement, “if/else” operator, exceptions are new-style new builtins: any/all, dict.__missing__ new stdlib modules: ctypes, xml.etree, functools, hashlib, sqlite3, wsgiref, ... 15 15
  • 16. 2.5: many optimizations sets/frozensets recoded from scratch many speed-ups to string operations substantial speed-ups in struct new-style exceptions are faster and many minor optimization tweaks smaller and phantom frames in calls re uses Python allocator some constant-folding at compile time fewer system calls during importing ... 16 16
  • 17. Resource Allocation Is Initialization # in 2.4 and earlier, Java-like...: resource = ...allocate it... try: ...use the resource... finally: ...free the resource... # in 2.5, much “slimmer”...: with ...allocate it... as resource: ...use the resource... with automatic “freeing” at block exit! 17 17
  • 18. Many “with”-ready types with open(filename) as f: ...work with file f... # auto f.close() on block exit somelock = threading.Lock() with somelock: # auto somelock.acquire() on block entry ...work guarded by somelock... # auto somelock.release() on block exit 18 18
  • 19. The “with” statement from __future__ import with_statement with <expr> as var: <with-block> # makes and uses a *context manager* _context = <expr> var = _context.__enter__() try: <with-block> except: _context.__exit__(*sys.exc_info()) else: _context.__exit__(None, None, None) Better than C++: can distinguish exception exits from normal ones! 19 19
  • 20. Your own context mgrs roll-your-own: write a wrapper class usually __init__ for initialization __enter__(self) returns useful “var” __exit__(self, ext, exv, tbv) performs the needed termination operations (exit is “normal” iff args are all None) extremely general slightly clunky/boilerplatey 20 20
  • 21. “with” for transactions class Transaction(object): def __init__(self, c): self.c = c def __enter__(self): return self.c.cursor() def __exit__(self, ext, exv, tbv): if ext is None: self.c.commit() else: self.c.rollback() with Transaction(connection) as cursor: cursor.execute(...) ...and more processing as needed... 21 21
  • 22. Your own context mgrs contextlib module can help in many ways decorator contextlib.contextmanager lets you write a context mgr as a generator yield the desired result of __enter__ within a try/finally or try/except/else re-raise exception in try/except case function contextlib.nested “nests” context managers without needing special syntax function contextlib.closing(x) just returns x and calls x.close() at block exit 22 22
  • 23. Transaction w/contextlib import contextlib @contextlib.contextmanager def Transaction(c): cursor = c.cursor() try: yield cursor except: c.rollback() raise else: c.commit() 23 23
  • 24. Other uses of contextlib # syntax-free “nesting” # e.g., a locked transaction: with contextlib.nested(thelock, Transaction(c)) as (locked, cursor): ... # auto commit or rollback, AND auto # thelock.release, on block exit # when all you need is closing, e.g: with contextlib.closing( urllib.urlopen(...)) as f: ...work with pseudo-file object f... # auto f.close() on block exit 24 24
  • 25. Generator enhancements yield can be inside a try-clause yield is now an expression x = g.send(value) gives yield’s value x = g.next() is like x = g.send(None) preferred syntax: value = (yield result) g.throw(type [,value [,traceback]]) g.close() is like g.throw(GeneratorExit) automatic g.close() when g is garbage- collected this is what ensures try/finally works! 25 25
  • 26. Absolute/relative imports from __future__ import absolute_import means: import X finds X in sys.path you can import .X to find X in the current package also import ..X to find X in the package containing the current one, etc important “future” simplification of imports 26 26
  • 27. try/except/finally try: <body> except <spec>: <handler> else: <ebody> # else-clause is optional finally: <finalizer> becomes equivalent to: try: try: <body> except <spec>: <handler> else: <ebody> finally: <finalizer> 27 27
  • 28. if/else ternary operator result = (whentrue if cond else whenfalse) becomes equivalent to: if cond: result = whentrue else: result = whenfalse the parentheses are technically optional (!) meant to help with lambda & the like somewhat-controversial syntax...:-) 28 28
  • 29. Exceptions are new-style BaseException KeyboardInterrupt Exception GeneratorExit StandardError ArithmeticError EnvironmentError LookupError # other “true” error classes StopIteration SystemExit Warning SystemExit 29 29
  • 30. any and all def any(seq): for item in seq: if item: return True return False def all(seq): for item in seq: if not item: return False return True note: RIGHT behavior for empty sequence! 30 30
  • 31. dict.__missing__ hook method called by __setitem__ if the key is missing (==not in the dict) default implementation in dict itself: def __missing__(self, key): raise KeyError(key) meant to be overridden by subclasses collections.defaultdict subclasses dict: def __missing__(self, key): return self.default_factory() default_factory optionally set at __init__ (default None == raise) 31 31
  • 32. ctypes load any shared library / DLL with ctypes.CDLL(<complete name of library>) call any function as a method of the CDLL automatically converts to int and char* other conversions explicit with c_int, c_float, create_string_buffer, ... also accesses Python’s C API essentially: a general-purpose Python FFI ! dangerous: any programer mistake or oversight can easily crash Python! 32 32
  • 33. Element-Tree new package xml.etree with modules ElementTree, ElementPath, ElementInclude highly Pythonic in-memory representation of XML document as tree, much slimmer (and faster!) than the DOM each XML element is a bit like a list of its children merged with a dict of its attrs scalable to large documents with included C accelerators and .iterparse incremental parsing (a bit like pulldom, but simpler, and keeps subtrees by default) 33 33
  • 34. functools functools.partial for “partial application” (AKA “currying”) functools.update_wrapper for proper setting of metadata for functions that wrap other functions functools.wraps: decorator equivalent of functools.update_wrapper 34 34
  • 35. hashlib replaces md5 and sha modules (which become wrappers to it!) adds SHA-224, SHA-256, SHA-384, SHA-512 optionally uses OpenSSL as accelerator (but can be pure-Python if necessary!) 35 35
  • 36. sqlite3 wrapper for the SQLite embedded DB DB-API-2 compliant interface except that SQLite is “typeless” (!) some extensions: optional timeout on connect, isolation level, detect_type and type converters, executemany on iterators, executescript method, ... great way to “get started” on small app, can later migrate to PostgreSQL or other relational DB (MySQL, Oracle, whatever) 36 36
  • 37. wsgiref Web Server Gateway Interface standard “middleware” interface between HTTP servers and Python web frameworks goal: any framework, any server non-goal: direct use by web applications! already widely supported by frameworks http://www.wsgi.org/wsgi for more! stdlib now includes a “reference implementation” of WSGI (wsgiref) includes basic HTTP server for debugging WSGI applications and interfaces 37 37