39
Python and Zope An Introduction Kiran Jonnalagadda <[email protected]> http://jace.seacrow.com/

Python and Zope: An introduction (May 2004)

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

Page 1: Python and Zope: An introduction (May 2004)

Pythonand Zope

An IntroductionKiran Jonnalagadda<[email protected]>

http://jace.seacrow.com/

Page 2: Python and Zope: An introduction (May 2004)

What kind of a language is Python?

Page 3: Python and Zope: An introduction (May 2004)

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

Page 4: Python and Zope: An introduction (May 2004)

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.

Page 5: Python and Zope: An introduction (May 2004)

5

Types of Languages

How does the language treat variables?

Type is Strong or Weak

Declaration is Static or Dynamic

Page 6: Python and Zope: An introduction (May 2004)

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

Page 7: Python and Zope: An introduction (May 2004)

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

Page 8: Python and Zope: An introduction (May 2004)

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

Page 9: Python and Zope: An introduction (May 2004)

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)

Page 10: Python and Zope: An introduction (May 2004)

Language Type Matrix

Static Typed Dynamic Typed

Weak Typed C Perl, PHP,Shell Script

Strong Typed C++, Java, C# Python

Page 11: Python and Zope: An introduction (May 2004)

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

Page 12: Python and Zope: An introduction (May 2004)

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.

Page 13: Python and Zope: An introduction (May 2004)

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.

Page 14: Python and Zope: An introduction (May 2004)

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.

Page 15: Python and Zope: An introduction (May 2004)

Python is all about

Rapid Application Development

Page 16: Python and Zope: An introduction (May 2004)

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'>>>

Page 17: Python and Zope: An introduction (May 2004)

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'}>>>

Page 18: Python and Zope: An introduction (May 2004)

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'>>>

Page 19: Python and Zope: An introduction (May 2004)

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.

Page 20: Python and Zope: An introduction (May 2004)

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

Page 21: Python and Zope: An introduction (May 2004)

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.

Page 22: Python and Zope: An introduction (May 2004)

So that is Python. What is Zope?

Page 23: Python and Zope: An introduction (May 2004)

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.

Page 24: Python and Zope: An introduction (May 2004)

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.

Page 25: Python and Zope: An introduction (May 2004)

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).

Page 26: Python and Zope: An introduction (May 2004)

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.

Page 27: Python and Zope: An introduction (May 2004)

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

Page 28: Python and Zope: An introduction (May 2004)

How Acquisition Works

Container.SubObj.SubSubObj.Template is the same thing as Container.Template, but context differs.

Container

Template

SubObj

SubSubObj

Page 29: Python and Zope: An introduction (May 2004)

What Inheritance is to

Classes,

Acquisition is to Instances

and Containers.

Page 30: Python and Zope: An introduction (May 2004)

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.

Page 31: Python and Zope: An introduction (May 2004)

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.

Page 32: Python and Zope: An introduction (May 2004)

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.

Page 33: Python and Zope: An introduction (May 2004)

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.

Page 34: Python and Zope: An introduction (May 2004)

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>

Page 35: Python and Zope: An introduction (May 2004)

Zope Page TemplatesBoxSlot

Main BodySlot

Templates define macros and slots using XML namespaces. Macros fill slots in other templates.

Page 36: Python and Zope: An introduction (May 2004)

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

Page 37: Python and Zope: An introduction (May 2004)

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.

Page 38: Python and Zope: An introduction (May 2004)

Supported Platforms

Windows Linux FreeBSD Mac OS XSolaris

Red Hat Debian Mandrake SuSE Gentoo

OpenBSD

Supported Operating Systems

Supported Linux Distributions

Page 39: Python and Zope: An introduction (May 2004)

Resources

Python: www.python.orgZope: www.zope.org

The Indian Zope and Python User’s Groupgroups.yahoo.com/group/izpug