SlideShare une entreprise Scribd logo
1  sur  37
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à py25

Advanced Bash Scripting Guide 2002
Advanced Bash Scripting Guide 2002Advanced Bash Scripting Guide 2002
Advanced Bash Scripting Guide 2002duquoi
 
How to write LaTeX package
How to write LaTeX packageHow to write LaTeX package
How to write LaTeX packageRuini Xue
 
How to make DSL
How to make DSLHow to make DSL
How to make DSLYukio Goto
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9tomaspavelka
 
IPW2008 - my.opera.com scalability
IPW2008 - my.opera.com scalabilityIPW2008 - my.opera.com scalability
IPW2008 - my.opera.com scalabilityCosimo Streppone
 
Getting started erlang
Getting started erlangGetting started erlang
Getting started erlangKwanzoo 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 TutorialTom Croucher
 
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 BatsovMichael 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, DockerDocker, Inc.
 
All bugfixes are incompatibilities
All bugfixes are incompatibilitiesAll bugfixes are incompatibilities
All bugfixes are incompatibilitiesnagachika t
 
Slides Aquarium Paris 2008
Slides Aquarium Paris 2008Slides Aquarium Paris 2008
Slides Aquarium Paris 2008julien.ponge
 
Upgrading to Rails 3
Upgrading to Rails 3Upgrading to Rails 3
Upgrading to Rails 3juliangiuca
 
Profiling blueprints
Profiling blueprintsProfiling blueprints
Profiling blueprintsbergel
 

Similaire à 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
 

Plus de webuploader

Michael_Hulme_Banff_Social_Networking
Michael_Hulme_Banff_Social_NetworkingMichael_Hulme_Banff_Social_Networking
Michael_Hulme_Banff_Social_Networkingwebuploader
 
cyberSecurity_Milliron
cyberSecurity_MillironcyberSecurity_Milliron
cyberSecurity_Millironwebuploader
 
LiveseyMotleyPresentation
LiveseyMotleyPresentationLiveseyMotleyPresentation
LiveseyMotleyPresentationwebuploader
 
FairShare_Morningstar_022607
FairShare_Morningstar_022607FairShare_Morningstar_022607
FairShare_Morningstar_022607webuploader
 
3_System_Requirements_and_Scaling
3_System_Requirements_and_Scaling3_System_Requirements_and_Scaling
3_System_Requirements_and_Scalingwebuploader
 
ScalabilityAvailability
ScalabilityAvailabilityScalabilityAvailability
ScalabilityAvailabilitywebuploader
 
scale_perf_best_practices
scale_perf_best_practicesscale_perf_best_practices
scale_perf_best_practiceswebuploader
 
7496_Hall 070204 Research Faculty Summit
7496_Hall 070204 Research Faculty Summit7496_Hall 070204 Research Faculty Summit
7496_Hall 070204 Research Faculty Summitwebuploader
 
FreeBSD - LinuxExpo
FreeBSD - LinuxExpoFreeBSD - LinuxExpo
FreeBSD - LinuxExpowebuploader
 

Plus de 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
 

Dernier

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

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