Upload
kiran-jonnalagadda
View
662
Download
3
Tags:
Embed Size (px)
DESCRIPTION
A presentation I made in May 2004 introducing the Python language and Zope application server. I can no longer recall where this was presented.
Citation preview
What kind of a language is Python?
3
High Level & Low Level
Low level languages give you more power, but take very long to write code in.
High level languages emphasise developer productivity; get things done fast.
High Level
Lisp
Perl
Python
Java, C#
C++
C
Assembler
Low Level
Powe
r — P
rodu
ctiv
ity
4
Why Productivity?
Computers get faster each year.
Humans don’t get faster.
Language that can make programmer faster makes sense.
Drop in program’s speed is offset by a faster computer.
5
Types of Languages
How does the language treat variables?
Type is Strong or Weak
Declaration is Static or Dynamic
6
Strong Typed
Type of variable is explicit.
Type of variable does not change depending on usage.
Examples:In C++, Java or C#: int n;n = 0; // validn = 0.6; // invalid
In Python: a = 1b = “hello”print a + b # invalid
7
Weak Typed
Type of variable is NOT explicit.
Type of variable depends on the operation.
Examples:In shell script (bash): A=1B=2echo $A+$B # 1+2echo $((A+B)) # 3
In PHP: $a = 1;$b = 2;echo($a + $b); # 3echo($a . $b); # 12
8
Static Typed
Variable is declared before use.
Using a variable without declaring it is a compile-time error.
Type of variable cannot be changed at run-time.
Examples:In C, C++, Java, C#: int n;n = 1; // validm = 1; // invalid
9
Dynamic TypedVariables need not be declared before use.
However, variables do not exist until assigned a value.
Reading a non-existent variable is a run-time error (varies by language).
Examples:In shell script (bash):A=1echo $A # 1echo $B # (blank)
In Python: a = 1print a # 1print b # (error)
Language Type Matrix
Static Typed Dynamic Typed
Weak Typed C Perl, PHP,Shell Script
Strong Typed C++, Java, C# Python
Capability MatrixOS Level GUI Web Portable
Perl Yes Yes Yes
Python Yes Yes Yes
Java, C# Yes Yes Yes
VB Yes Yes
PHP Yes Yes
C++ Yes Yes Yes Yes
C Yes Yes Yes Yes
Features MatrixOO GC Introspection ADT*
Perl Partial Yes Partial Yes
Python Yes Yes Yes Yes
Java, C# Yes Yes Partial Yes
VB Partial Yes Partial
PHP Partial Yes Partial
C++ Partial
C* Advanced Data Types: strings, lists, dictionaries, complex numbers, etc.
13
What is Introspection?
Concept introduced by Lisp.
Code can treat code as data.
Can rewrite parts of itself at run-time.
Very powerful if used well.
Python takes it to the extreme.
14
Highlights of Python
No compile or link steps.
No type declarations.
Object oriented.
High-level data types and operations.
Automatic memory management.
Highly scalable.
Interactive interpreter.
Python is all about
Rapid Application Development
Example CodeUsing the Python Interactive Interpreter: >>> myStr = "Rapid Coils">>> myStr.split(" ")[0][:-2].lower() * 3'rapraprap'
>>> myLst = [1, 2, 3, 4]>>> myLst.append(5)>>> "|".join([str(x) for x in myLst])'1|2|3|4|5'>>>
Example CodeDictionaries or hash tables or associative arrays: >>> myDict = {1:['a','b','c'],2:"abc"}>>> myDict[3] = ('a','b','c')>>> myDict.keys()[1, 2, 3]>>> myDict['foobar'] = 'barfoo'>>> myDict.keys()[1, 2, 3, 'foobar']>>> myDict{1: ['a', 'b', 'c'], 2: 'abc', 3: ('a', 'b', 'c'), 'foobar': 'barfoo'}>>>
Python ClassesClasses are defined using the “class” keyword: >>> class foo:... def __init__(self, text):... self.data = text... def read(self):... return self.data.upper()... >>> f = foo("Some text")>>> f.data'Some text'>>> f.read()'SOME TEXT'>>>
InheritanceClasses can derive from other classes: class Pretty: def prettyPrint(self, data): print data.title().strip()
class Names(Pretty): def __init__(self, value): self.name = value def cleanName(self): self.prettyPrint(self.name)
Multiple inheritance is allowed.
Operator Overloadingclass Complex: def __init__ (self, part1, part2): self.real = part1 self.im = part2
def __add__ (self, other): return Complex(self.real + other.real, self.im + other.im)
>>> a = Complex(1, 2)>>> b = Complex(2, 3)>>> c = a + b>>> print c.real, c.im3 5>>>
Container Model>>> class foo:... pass... >>> class bar:... pass... >>> f = foo()>>> b = bar()>>> dir(f)['__doc__', '__module__']>>> f.boo = b >>> dir(f)['__doc__', '__module__', 'boo']
Observe how items are added to containers.
So that is Python. What is Zope?
23
Zope is...An application built using Python.
Provides a Web server,
A database engine,
A search engine,
A page template language,
Another page template language,
And several standard modules.
What Visual Studio is to
Windows software development,
Zope is to the Web.
Zope is a Web Application Server.A framework for building applications with Web-based interfaces. Zope provides both development and run-
time environments.
25
Web Server: ZServer
Uses ZServer; Apache not needed.
But Apache can be used in front.
ZPublisher maps URLs to objects.
ZServer does multiple protocols:
HTTP, WebDAV and XML-RPC.
FTP and Secure FTP (SFTP in CVS).
26
Database Engine: ZODB
Zope Object Database.
Object oriented database.
Can be used independent of Zope.
Fully transparent object persistence.
May be used for either relational or hierarchical databases, but Zope forces hierarchical with single-parent.
Hierarchical Data Access
Python:Object.SubObj.Function()
ZServer URL:site.com/Object/SubObj/Function
The only way to get to “Function” is via “Object” and “SubObj.”
Introducing Acquisition...
Object
SubObj
Function
How Acquisition Works
Container.SubObj.SubSubObj.Template is the same thing as Container.Template, but context differs.
Container
Template
SubObj
SubSubObj
What Inheritance is to
Classes,
Acquisition is to Instances
and Containers.
30
ZODB FeaturesCode need not be database aware.
Includes transactions, unlimited undo.
Storage backend is plug-in driven.
Default: FileStorage.
Others: Directory and BerkeleyDB.
May also be an SQL backend.
31
ZODB with ZEOZEO is Zope Enterprise Objects.
One ZODB with multiple Zopes.
Processor usage is usually in logic and presentation, not database.
ZEO allows load to be distributed across multiple servers.
Database replication itself is not open source currently.
32
Search Engine: ZCatalog
ZCatalog maintains an index of objects in database.
Is highly configurable.
Multiple ZCatalog instances can be used together.
No query language; just function calls.
33
Document Template ML
DTML uses <dtml-command> tags inserted into HTML.
Common commands: var, if, with, in.
Extensions can add new commands.
DTML is deprecated: difficult to edit with WYSIWYG editors.
34
Zope Page Templates
ZPT uses XML namespaces.
Is compatible with WYSIWYG editors like DreamWeaver.
Enforces separation between logic and presentation: no code in templates.
Example:<span tal:replace="here/title”>Title comes here</span>
Zope Page TemplatesBoxSlot
Main BodySlot
Templates define macros and slots using XML namespaces. Macros fill slots in other templates.
File-system Layout Zope/ The base folder doc/ Documentation Extensions/ Individual Python scripts import/ For importing objects lib/ Libraries python/ Zope’s extensions to Python Products/ Extensions to Zope var/ Data folder Data.fs The database file ZServer/ Web server
37
Example Extension: Formulator
HTML form construction framework.
Form widgets ⇔ Formulator objects.
Widgets can have validation rules.
Automatic form construction.
Or plugged into a ZPT template.
Painless data validation.
Supported Platforms
Windows Linux FreeBSD Mac OS XSolaris
Red Hat Debian Mandrake SuSE Gentoo
OpenBSD
Supported Operating Systems
Supported Linux Distributions
Resources
Python: www.python.orgZope: www.zope.org
The Indian Zope and Python User’s Groupgroups.yahoo.com/group/izpug