Why Python by Marilyn Davis, Marakana

download Why Python by Marilyn Davis, Marakana

If you can't read please download the document

Transcript of Why Python by Marilyn Davis, Marakana



One line to make it importable

if __name__ == '__main__': Test()

Desirable Attributes for Programs

Programmer Friendly

Correct

Efficient

Portable?

Why Python?

by Marilyn Davis, Ph.D.

[email protected]

Marakana Open Source Trainingwww.marakana.com

Programmer Friendly

Pleasant to look at.

Easy to read and modify.

Manages complication.

Invites collaboration.

"""pig.py word [word2 ...]Prints the word(s) in Pig Latin.

The rules for forming Pig Latin words are:

o If the word begins with a vowel, add "way" to the end of the word. o If the word begins with a consonant, extract consonants up to the first vowel, move those consonants to the end of the word, and add "ay"."""def Pigify(word): """Return the word translated to piglatin.""" vowels = "aeiouyAEIOUY"

if word[0] in vowels: return word + 'way' # concatonation

o If the word begins with a consonant, extract consonants up to the first vowel, move those consonants to the end of the word, and add "ay"."""def Pigify(word): """Return the word translated to piglatin.""" # code blocks are delimited by indentation vowels = "aeiouyAEIOUY"

if word[0] in vowels: return word + 'way' # concatonation

# loop through a sequence, with index for i, char in enumerate(word): if char in vowels: break return word[i:] + word[:i] + 'ay' # slicing

def Test(): """Test a few words."""

for word in "chocolate", "nuts", "cherry": # comma suppresses the new line print Pigify(word), print # just prints a new line

Test()

OUTPUT:$ pig.pyocolatechay utsnay errychay$"""

$ pig.pyocolatechay utsnay errychay$

#!/usr/bin/env python (Top line in unix)

import math

def GetCircleAttributes(radius): try: radius = float(radius) except ValueError: return None circumference = math.pi * 2 * radius area = math.pi * radius * radius return circumference, area radius = raw_input("radius: ")answer = GetCircleAttributes(radius)if answer: circumference, area = answer print "Circumference =", circumference print "Area =", areaelse: print "That wasn't a number!"

A Program File is a Module is a Namespace

__main__

The module being run is __main__ (magic main).

Each object in the module is another namespace

Function Object

str Object

float Object

__main__

Each import brings a neighboring namespace
into your code.

math

__main__

Introspection

$ pythonPython 2.6 (r26:66714, Dec 31 2008, 14:19:00) [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> dir()['__builtins__', '__doc__', '__name__', '__package__']

>>> __name__'__main__'

>>> a_string = "Hi">>> dir()['__builtins__', '__doc__', '__name__', '__package__', 'a_string']

>>> dir(a_string)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

>>> a_string.swapcase()'hI'

The dot operator travels into a Namespace.

>>> import math

>>> dir()['__builtins__', '__doc__', '__name__', '__package__', 'a_string', 'math']

>>> dir(math)['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees','e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

>>> import pig

>>> dir()['__builtins__', '__doc__', '__name__', '__package__', 'a_string', math', 'pig']

>>> dir(pig)['Pigify', 'Test', '__builtins__', '__doc__', '__file__', '__name__', '__package__']

>>>

>>> help(pig)Help on module pig:

NAME pig

FILE /home/marilyn/python/Why_Python/pig.py

DESCRIPTION pig.py word [word2 ...] Prints the word(s) in Pig Latin. The rules for forming Pig Latin words are: o If the word begins with a vowel, add "way" to the end of the word.

o If the word begins with a consonant, extract consonants up to the first vowel, move those consonants to the end of the word, and add "ay".

FUNCTIONS Pigify(word) Return the word translated to piglatin. Test() Test a few words.

>>> pig.Pigify("jalapenos") 'alapenosjay'

import pig

def PigLatin(): """Prompts for English, prints pig latin. """ line = raw_input(Tell me something good: ) for word in line.split(): print pig.Pigify(word), print

PigLatin()


Pythonic
Thinking

object.attribute

Namespaces!

menu.lunch.salad = 'caesar'

Object-Oriented Programming

A class is:

A blueprint for a namespace.

An object is:

A namespace constructed from the blueprint.

class Rock:

def SetType(self, rock_type): self.type = rock_type

def __str__(self): return "Rock object: " + self.type

rock_object = Rock()rock_object.SetType("Pet")print rock_object

"""OUTPUT:Rock object: Pet"""

>>> a_list = [3, 8, 1]>>> a_list[1] = 88>>> a_list.pop()1>>> a_list[3, 88]

Data Structures (types)

Lists

>>> a_dict = {}>>> a_dict["Mary"] = "678-8823">>> a_dict["Zoe"] = "555-1234">>> a_dict[3] = "12">>> a_dict{3: '12', 'Mary': '678-8823','Zoe':'555-1234'}>>> for each_key in a_dict:... print each_key, a_dict[each_key]... 3 12Mary 678-8823Zoe 555-1234>>>

Dictionaries (hash table, associative array)

More Iterating

file_obj = open("ram_tzu")for line in file_obj: print line,"""Ram Tzu knows this:When God wants you to do something,You think it's your idea."""

Swapping

this = 1that = 100

this, that = that, this

def GoToThePark(name, best_friend="Jose"): print name, 'and', best_friend print 'go to the park' print 'play hide-and-seek' print 'till long after dark' print

GoToThePark('Charlie', 'Jacqueline')GoToThePark('Judi')

Default Arguments

Keyword Arguments

def Greet(first, last): print 'Hello', first, last

Greet('Rocky','The Squirrel')

Greet(last='Moose', first='Bullwinkle')

>>> [(x, x**2) for x in range(1, 10, 2)][(1, 1), (3, 9), (5, 25), (7, 49), (9, 81)]

>>> reduce(lambda x,y: x + y, range(1,11))55>>>

Functional Programming

Regular expressions plus

r"a string

'r' prepended to a string makes a raw string The backslashes are literal.

import rep = re.compile(r\b((?P\w+) ?(?P\w?\.?)) Jones)m = p.search("John J. Jones")

>>> m.groups(2)'John'

>>> m.group('first')'John'

Named groups!

String formatting plus

>>> print "The %s eats some %s before %s time." \ % (a_dict["animal"], a_dict["food"], a_dict["hobby"])

The cat eats some cheese before sleeping time.

Dictionary Replacement

>>> print "The %(animal)s eats some \ %(food)s before %(hobby)s time." \ % a_dict

The cat eats some cheese before sleeping time.

http://www.iolanguage.com/about/simplicity/

Programmer Friendly

Pleasant to look at.

Easy to read and modify.

Manages complication.

Invites collaboration.

Desirable Attributes for Programs

Programmer Friendly

Correct

Efficient

Portable?

OS Module

>>> import os>>> os.linesep'\n' '\r\n'>>> os.path.join("some", "path")'some/path' 'some\\path'>>>

Linux Windows

Runs on:

Linux/Unix

Mac

Windows

OS/2

Amiga

Nokia Series 60 cell phones

Any OS with a C compiler!

The same source code will run unchanged across all implementations.

Python can integrate with COM, .NET, and CORBA objects.

Python is also supported for the Internet Communications Engine (ICE) and many other integration technologies.

SWIG/Boost C/C++ wrapping tools to bring C/C++ libraries into the Python interpreter so you can import myCstuff.

Jython For seamless integration of Java libraries on JVM.

IronPython Microsoft's implementation for .NET.

You can also go the opposite route and embed Python in your own application.

Desirable Attributes for Programs

Programmer Friendly

Correct

Efficient

Portable?

Correct

The easier it is to read the code, and the more the language handles the tedious details, like data-typing, memory management, and loop set-up, the easier it is to make the program correct.

An empirical comparison of C, C++, Java, Perl, Python, Rexx, and Tcl for asearch/string-processing programLutz Prechelt ([email protected]) Fakultat fur Informatik Universitat Karlsruhe D-76128 Karlsruhe, Germany March 10, 2000

page.mi.fu-berlin.de/~prechelt/Biblio/jccpprtTR.pdf

Efficient

Quick to write.

Efficient at run time.

Fast-running.

Conserves memory.

http://www.osnews.comstory.php?news_id=5602&page=3

Speed

/

http://scutigena.sourceforge.net/

http://shootout.alioth.debian.org/

What about
execution speed ?

Modern hardware processors generally make language speed a non-issue.

Most applications are limited by speed of database or network connection, not programming language.

Steven Ferg, Bureau of Labor Statistics, www.ferg.org

Here is a slightly abridged version of Ian Bicking's excellent blog entry on the subject of speed (http://blog.colorstudy.com/ianb/weblog/2003/10/19.html#P14)

"Any reader of the comp.lang.python is used to queries about performance. The typical answer is that if Python's performance is problematic, you can code key portions in C.

I don't think this is a very good answer, because it's not the answer that most Python programmers come to. Extension programming is a sort of release valve -- it means there's always a way out if you need it. But it really is only there in case of emergency.

I think the real answer to these people should be: Python is fast enough just as it is. Your application may not be fast enough when you first write it in Python, but you can almost always make Python -- just Python -- perform sufficiently. This isn't true for every domain, but it's true for the most common programs.

C is good for one kind of performance -- running code really fast. But this is only one kind of performance, and now that CPUs are so fast it's not the most important kind of performance. Programs have bottlenecks, and nothing but the bottleneck matters. CPUs are not the bottleneck!

Two applications that reminded me of this: Evolution and Mail.app. At least for my usage, Mail.app runs much faster than Evolution. This isn't because of language or runtime differences. Mail.app runs faster because it is smarter. I have big IMAP mailboxes, and Mail.app caches messages intelligently, checks those caches intelligently, and does its best to queue IMAP queries for quick response to what I'm most interested in.

Mail.app isn't written in Python, but it shows what good optimization is -- it's not just executing code fast, it's executing only as much code as necessary, and being responsive.

So, I think when someone asks if Python performance is an issue, tell them no, and don't apologize for Python by pointing to the release valve of C extensions. If you can write a full-featured Python program twice as fast as C (probably more like 10x, but for argument...), that leaves the Python programmer a lot of time to think about how to make the program more responsive. Some of that time spent in optimization could be used to mitigate Python's relatively slow computation, but more likely it will be spent avoiding expensive operations with disks and networks. In the end, I would expect the pure Python program to be faster than the C program (given an equal amount of programmer time)."

Productivity =
Lines of code

1 machine instruction for Assembly Language

3 to 7 machine instructions for C/C++ and Java

100's to 1000's of instructions for Perl and Python

A programmer can produce roughly the same number of lines of code per year regardless of the language. But, one line of code translates to:

"Scripting: Higher Level Programming for the 21st Century"

by John K. Ousterhout IEEE Computer magazine, March 1998

Productivity
= Features
implemented
= Fun

Note: A day when you decrease the number of linesof code while maintaining the same functionality is a very productive day.

Your code is readable, modifiable, and extendable.

The behavior matches the specification.

More Instructions/Line of Code

Perl and Python programs do more at run time:

Dynamic data typing.

Automatic memory management.

High level data structure manipulation.

Perl and Python programmers have less to do at development time.

No data type declarations.

No memory management.

Much data manipulation is built in.

Python Programmers

Have less to do when it's time to modify and enhance the code.

Python code is easy to read.

Developer
Reports

I find that I'm able to program about three times faster in Python than I could in Java, and three times faster in Java than I could in C.

Andy Hertzfeld

Eckel was comparing Python to Java and C++.

Andy Hertzfeld is one of the programmers working on Chandler, the new open-source PIM (personal information manager). The quote is fromhttp://wiki.osafoundation.org/bin/view/Main/DemoTranscript2003-04-23

Probably the original estimate of 5-10 times productivity was made (for scripting languages in general) by John Ousterhout in his 1998 article on scripting languages. Subsequent experience and anecdotal evidence seem to confirm this estimate.

When a 20,000 line project went to approximately 3,000 lines overnight, and came out being more flexible and robust ... I realized I was on to something really good.

-- Matthew "Glyph" Lefkowitz

Eckel was comparing Python to Java and C++.

Andy Hertzfeld is one of the programmers working on Chandler, the new open-source PIM (personal information manager). The quote is fromhttp://wiki.osafoundation.org/bin/view/Main/DemoTranscript2003-04-23

Probably the original estimate of 5-10 times productivity was made (for scripting languages in general) by John Ousterhout in his 1998 article on scripting languages. Subsequent experience and anecdotal evidence seem to confirm this estimate.

...the lines of Python code were 10% of the equivalent C++ code.

-- Greg Stein, Google

Eckel was comparing Python to Java and C++.

Andy Hertzfeld is one of the programmers working on Chandler, the new open-source PIM (personal information manager). The quote is fromhttp://wiki.osafoundation.org/bin/view/Main/DemoTranscript2003-04-23

Probably the original estimate of 5-10 times productivity was made (for scripting languages in general) by John Ousterhout in his 1998 article on scripting languages. Subsequent experience and anecdotal evidence seem to confirm this estimate.

Who is using Python?
What are they doing with it?

Industrial Light and Magic: Every CG image we create has involved Python somewhere in the process," ...Philip Peterson, Principal Engineer, Research & Development

Most organizations don't want to be caught out in the cold, being the only one using a particular language. These slides show that that there is nothing to worry about with Python, on that front. They also illustrate Python's flexibility, which I referred to earlier.

I got most of these references from the Pythonology web site - http://www.pythonology.org/

Disney Feature Length Animation uses Python for its animation production applications.

Most organizations don't want to be caught out in the cold, being the only one using a particular language. These slides show that that there is nothing to worry about with Python, on that front. They also illustrate Python's flexibility, which I referred to earlier.

I got most of these references from the Pythonology web site - http://www.pythonology.org/

"Python has been an important part of Google since the beginning, and remains so as the system grows and evolves. ..Peter Norvig, Director of Search Quality

Yahoo bought Inktomi in December 2002:

Mergers & Acquisitions
Yahoo! Buys Inktomi For $235 Million
Arik Hesseldahl, 12.23.02
NEW YORK - Two of the oldest names in the Web-searching business are combining into one. Sunnyvale, Calif-based Web portal Yahoo! says it will pay $235 million for Inktomi, another Web outfit that started out as a search engine, but has more recently gotten into the business of caching Web content to speed its delivery.

Inktomi, which is based in Foster City, Calif., was founded in 1996 out of parallel computing technology based on a research project conducted at the University of California at Berkeley to find information on the then-exploding Web. Searching remains Inktomi's core competency.

"Python enabled us to create EVE Online, a massive multiplayer game, in record time. The EVE Online server cluster runs over 25,000 simultaneous players in a shared space simulation, most of which is created in Python. The flexibilities of Python have enabled us to quickly improve the game experience based on player feedback," ... Hilmar Veigar Petursson of CCP Games.

Python has provided us with a measurable productivity gain that allows us to stay competitive in the online travel space," said Michael Engelhart, CTO of EZTrip.com.

Python is of tremendous value throughout an embedded system's lifecycle...Thomas Major, Product Development Manager

Carmanah

LED lighting and solar photovoltaic systems

Yahoo uses Python for its groups site, and in its Inktomi search engine.

Yahoo bought Inktomi in December 2002:

Mergers & Acquisitions
Yahoo! Buys Inktomi For $235 Million
Arik Hesseldahl, 12.23.02
NEW YORK - Two of the oldest names in the Web-searching business are combining into one. Sunnyvale, Calif-based Web portal Yahoo! says it will pay $235 million for Inktomi, another Web outfit that started out as a search engine, but has more recently gotten into the business of caching Web content to speed its delivery.

Inktomi, which is based in Foster City, Calif., was founded in 1996 out of parallel computing technology based on a research project conducted at the University of California at Berkeley to find information on the then-exploding Web. Searching remains Inktomi's core competency.

The Philips (formerly IBM) Fishkill semiconductor manufacturing facility uses Linux and Python for factory tool control.

Yahoo bought Inktomi in December 2002:

Mergers & Acquisitions
Yahoo! Buys Inktomi For $235 Million
Arik Hesseldahl, 12.23.02
NEW YORK - Two of the oldest names in the Web-searching business are combining into one. Sunnyvale, Calif-based Web portal Yahoo! says it will pay $235 million for Inktomi, another Web outfit that started out as a search engine, but has more recently gotten into the business of caching Web content to speed its delivery.

Inktomi, which is based in Foster City, Calif., was founded in 1996 out of parallel computing technology based on a research project conducted at the University of California at Berkeley to find information on the then-exploding Web. Searching remains Inktomi's core competency.

NASA Python has met or exceeded every requirement we've had" ... Steve Waterbury, Software Group Leader, NASA STEP Testbed

"Python is fast enough for our site and allows us to produce maintainablefeatures in record times, with a minimum of developers," Cuong Do, Software Architect, YouTube.com.

The National Institute of Health

and

Case Western Reserve University are building cutting-edge genetic analysis software with Python. .

The National Weather Service uses Python to prepare weather forecasts.

Lawrence Livermore National Laboratories is basing a new numerical engineering environment on Python.

The Theoretical Physics Division at Los Alamos National Laboratory uses Python to control large-scale simulations on massively parallel supercomputers.

US Navy uses Python & Zope for a web based workflow system.

Rear Admiral Grace Murray Hopper

A more detailed discussion of 8 success stories can be found in O'Reilly's Python Success Stories Find it at:http://www.onlamp.com/pub/wlg/3198

US Department of Agriculture uses Python & Zope for massive collaboration

A more detailed discussion of 8 success stories can be found in O'Reilly's Python Success Stories Find it at:http://www.onlamp.com/pub/wlg/3198

US Department of Labor uses Python & Zope for the Workforce Connections learning management system.

A more detailed discussion of 8 success stories can be found in O'Reilly's Python Success Stories Find it at:http://www.onlamp.com/pub/wlg/3198

The point of a programming languageis to communicate with other engineers in a language that also the computer understands.

Number of Keywordsfuck

Python28

Java53

PHP53

Java Script59

C++63

C#77

Perl180