Web Hosting Philippines, Offshore Programming, Offshore SEO Philippines, Cheap Webhosting Manila Philippines
Home -> Resources -> Spyce Development Tips and Techniques

What is Spyce?

Spyce is a mature Python server pages technology and one of the most versatile around. It works analogous to PHP except the language used is Python. Unlike Python frameworks and web stacks such as Django and TurboGears, Spyce requires very little additional knowledge to get started with and lets you utilize your existing knowledge of Python libraries without much additional fuss.

Python's innate expressiveness means that even without resorting to a fancy framework, one can creating a complex web application in html-embedded script style and still retain conciseness that promotes code manageability. Spyce includes a handful of minimalistic but powerful mechanisms such as custom tags and Spyce lambdas that imbue the straightforward server pages development style with more power and enable it to tackle demanding tasks that would probably already require a framework under PHP.

Another important benefit of using Spyce over more complex Python web technology like Django/TurboGears/etc... is that it will be much easier to find programmers to inherit your codebase if you ever find yourself needing to do this. Instead of looking for a Django, TurboGears, CherryPy, etc... expert, you only need to look for someone versed in Python itself.


1. Use <% and %> instead of [[ and ]]

The Spyce documentation teaches usage of [[ and ]] as server script body delimiters, but Spyce will happily support the more familiar ASP/JSP style <% and %> delimiters which most people might be more comfortable with. For our development work here at Neotitans, we use this convention because it lets our favorite text editor, SciTE, color highlight the Python code within our Spyce scripts very nicely as shown below.

Spyce color syntax highlighting in Scite

2. Accessing POST and GET variables in Spyce (.spy) scripts

Every Spyce script contains the implicit module request which contains the calls necessary for accessing the POST and GET variables sent by the browser. The simplest way to access these are via the calls request.get1("varname"), request.post1("varname"), request.getpost1("varname") and request.postget1("varname"). These return a string containing the value of the GET/POST variable.

request.getpost1("varname") will return the value of GET variable varname if that is present or alternatively, the value of the POST variable varname if that is the one present. request.postget1("varname") works in the other way.

The calls request.get("varname"), request.post("varname"), request.getpost("varname") and request.postget("varname") work exactly like the above but return the values of the GET/POST variables within a list. Use these when you are expecting multiple values within a single GET/POST variable.

<%\

req.get1("userid")     # returns value of GET variable userid as a string
req.post1("userid")    # returns value of POST variable userid as a string

req.getpost1("userid") # returns value of GET variable userid if present or
                       # of POST variable otherwise
req.postget1("userid") # like the last example, but checks in reverse order

req.get("userid")      # Works like the above calls, but returns the values
req.post("userid")     # of the GET/POST variables as a list of strings.
req.getpost("userid")  # Useful if you are expecting multiple values within
req.postget("userid")  # a single GET or POST variable.

%>

See Spyce request module reference page for more detailed info.


3. How to exit a Spyce script in the middle of code without showing an exception message

When you want to terminate your Spyce script execution somewhere in the middle of the code, you might be tempted to use sys.exit(). This will raise a SystemExit exception and thus lead to an exception message, which is usually not what you want. The correct way to exit a spyce script cleanly in the middle of the code is to either raise spyceDone or just do a plain return.

<html><body>
<%\
userid=req.getpost1("userid")

if userid:
  print "Hello ",userid
else:
  return    # will not print closing body and html tag below
%>
</body></html>

See http://spyce.sourceforge.net/docs/docs-runtime_except.html for more detailed info.



© 2020 by Andy Sy
last updated: 2008-Jul-17

NeoTitans- Offshore Outsourced Corporate Web and Software Development Internet Marketing SEO

Web Development / Rich Internet Applications (RIA) Development

Programming Languages

Platforms

Database Development


Offshore Outsourced Corporate Web and Software Development Internet Marketing SEO
Small Business Web Design Philippines Corporate Web Development
   © 2003-2019 Neotitans Technologies Inc.