Presentation is loading. Please wait.

Presentation is loading. Please wait.

Zope Concepts - Python scripts Valentin Baciu Finsiel Romania Tirana, 6 June 2005.

Similar presentations


Presentation on theme: "Zope Concepts - Python scripts Valentin Baciu Finsiel Romania Tirana, 6 June 2005."— Presentation transcript:

1 Zope Concepts - Python scripts Valentin Baciu Finsiel Romania Tirana, 6 June 2005

2 References for Python “Python has been an important part of Google since the beginning, and remains so as the system grows and evolves. Today dozens of Google engineers use Python…” (Peter Norvig, director of search quality at Google, Inc. )Google, Inc. Name ‘Python ’comes from the Monty Python Flying Circus, no snake involved Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault.

3 Usage of Python Google (various projects) NASA (several projects) NYSE (one of only three languages "on the floor") Industrial Light & Magic (everything) Yahoo! (Yahoo mail & groups) RealNetworks (function and load testing) RedHat (Linux installation tools) LLNL, Fermilab (steering scientific applications) Zope Corporation (content management) ObjectDomain (embedded Jython in UML tool) Alice project at CMU (accessible 3D graphics) More success stories at www.pythonology.com

4 Python overview it is an interpreted OO programming language it has modules, classes, exceptions, very high level dynamic data types, and dynamic typing it is portable : it runs on many brands of UNIX, on Windows, OS/2, Mac and many other platforms rich set of modules and packages most of Zope is written in Python many resources available at http://www.python.orghttp://www.python.org

5 Example of usage Here's a simple function written in Python, which sorts a list first for last name and then for first name (double key sort): def sortList(namesList): namesList.sort(lambda x,y: (cmp(x.split()[-1],y.split()[-1]) or cmp(x,y) )) >>> li = [“Elizabeth taylor”,”Bette Davis”,”Hugh Grant”,”C. Grant”] >>> sortList(li) >>> print li  ['Bette Davis', 'C. Grant', 'Hugh Grant', 'Elizabeth taylor']

6 Zope scripting A script is treated like a method of an object Context – the environment in which the script executes Scripts can be called –from the web, by visiting an URL –from any other type of object

7 When to use them Python scripts vs. DTML/ZPT: Complicate computations Keep presentation apart from logic Cleaner code When the task becomes complex think about moving to External Methods or Python products The only strong point of Python Scripts is the fast access to them from ZMI Python Scripts have restricted access to some Python modules

8 Python Scripts product The Python Scripts product provides support for execution of Python scripts, exposing them as callable objects within the Zope When you create a Python script, from Select type to add… select Script (Python) you can specify: Id Title Parameter list Bound Names Textarea that contains python code

9

10 Binding variables (I) Bound names – set of variables that are created whenever the script is called used to access other Zope objects and scripts and are only available in Python scripts the names of the variables are set by default and should not be changed

11 Binding variables (II) Context refers to the object (environment) in which the script is called - context Container refers to the object (environment) in which the script is called – container Script refers to the object itself – script Namespace is left blank by default Subpath an advanced variable used when the script is traversed - traverse_subpath

12 External Methods When security constraints imposed by scripts, DTML, ZPT come in your way use External Methods Use them if you want –Access to the file system or network –Use Python packages that are not available to restricted scripts (regular expressions) They are stored on the file system (you need access) Reside in the /Extensions directory of the Zope server Must be instantiated in Zope using an External Method form the ‘Select type to add’ list

13 External Methods in ZMI

14 External Methods product The External Methods have the following attributes: –Id – the id that will be internally used by Zope –Title – title, optional –Module names – name of the module in the filesystem –Function name – the name of the function from the module Call this function form your DTML, ZPT, Python Scripts methods and make use of the results.


Download ppt "Zope Concepts - Python scripts Valentin Baciu Finsiel Romania Tirana, 6 June 2005."

Similar presentations


Ads by Google