Previous Chapter | Up | Next Section | Contents

The special namespace variable, _


A special variable, _ , is defined for use in expr expressions. The _ variable provides access to the DTML "namespace," which is an object used to look up variables when rendering DTML. The _ variable can be used to look up names directly:

<dtml-if "_['sequence-length'] > 20">

The _ variable is especially useful for accessing variables with names that contain special characters, like dashes in the case above.

Available attributes in the special variable, _

Name

Description

abs(X)

Return the absolute value of a number.

chr(I)

Return a string of one character whose ASCII code is integer I , e.g., chr(97) returns the string 'a' . This is the inverse of ord() . The argument must be in the range 0-255, inclusive.

DateTime()

Create a DateTime object from zero or more arguments. See "Appendix A, Date-time data" on page 41.

divmod(A,B)

Take two numbers as arguments and return a pair of integers consisting of their integer quotient and remainder. With mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as (A/B,A%B) . For floating point numbers the result is the same as (math.floor(A/B),A%B) .

float(X)

Convert a number to floating point. The argument may be a plain or long integer or a floating point number.

getattr(O,name)

Get the named attribute from an object.

hasattr(O,name)

Test whether the name can be found in the namespace.

getitem(name,flag)

Lookup a name in the namespace. If the value is callable and the flag is true, then the result of calling the value is returned, otherwise the value is returned. flag defaults to false.

hash(O)

Return the 32-bit integer hash value of the object.

hex(X)

Convert an integer to a hexadecimal string.

int(X)

Convert a number to an integer.

len(S)

Return the length (the number of items) of a collection of items.

math

The math module (table 3), which defines mathematical functions.

max(S)

Return the largest item of a non-empty sequence.

min(S)

Return the smallest item of a non-empty sequence.

namespace(name1=value1,
name2=value2,...)

The namespace function can be used with the in tag to assign variables for use within a section of DTML. The function accepts any number of "keyword" arguments, which are given as name=value pairs.

None

A special value that means "nothing".

oct(X)

Convert an integer.

ord(C)

Return the ASCII value of a string of one character. E.g., ord('a') returns the integer 97 . This is the inverse of chr() .

pow(X,Y)

Return X to the power Y . The arguments must have numeric types. With mixed operand types, the rules for binary arithmetic operators apply. The effective operand type is also the type of the result; if the result is not expressible in this type, the function raises an exception, e.g. pow(2,-1) and pow(2,35000) raise exceptions.

random

The random module (table 4), which defines various random-number generating functions.

round(X,N)

Return the floating point value X rounded to N digits after the decimal point. If N is omitted, the default value is zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus N; if two multiples are equally close, rounding is done away from 0 (so e.g. round(0.5) is 1.0 and round(-0.5) is -1.0 ).

str(O)

Return a string containing a representation of an object.

string

The string module, which defines string functions.

whrandom

The whrandom module (Figure 6, "Attributes defined by the whrandom module," on page 23), which defines random-number generating functions using the Wichmann-Hill pseudo-random number generator.

Attributes defined by the math module

Name

Description

acos(X)

Compute the inverse cosine, in radians, of X.

asin(X)

Compute the inverse sine, in radians, of X.

atan(X)

Compute the inverse tangent, in radians, of X.

atan2(X,Y)

Compute the inverse tangent, in radians, of the quotient of X and Y.

ceil(X)

Return the smallest integer that is greater than X.

cos(X)

Compute the cosine of X, which is in radians.

cosh(X)

Compute the hyperbolic cosine of X.

e

The base of the natural logarithms.

exp(X)

Compute the exponential function of X, or e to the power X, where e is the base of the natural logarithms.

fabs(X)

Compute a floating-point absolute value of the number, X.

floor(X)

Return the largest integer less than X.

fmod(X,Y)

Return the remainder of the division of X by Y.

frexp(X)

Return the mantissa and exponent in base 2 of the floating-point value of X, such that absolute value mantissa is between 0.5 and 1.0 or is 0.

hypot(X,Y)

Compute the hypotenuse of a right triangle with sides X and Y.

ldexp(X,Y)

Return X times two to the power of Y.

log(X)

Compute the natural (base e) logarithm of X.

log10(X)

Compute the common (base 10) logarithm of X.

modf(X)

Break a number into its whole and fractional parts.

pi

The mathematical constant, pi

pow(X,Y)

Raise X to the power Y.

sin(X)

Compute the sine of X.

sinh(X)

Compute the hyperbolic sine of X.

sqrt(X)

Compute the square-root of X.

tan(X)

Compute the tangent of X.

tanh(X)

Compute the hyperbolic tangent of X.

Attributes defined by the random module

Name

Description

betavariate (alpha, beta)

Beta distribution. Conditions on the parameters are alpha >- 1 and beta > -1. Returned values will range between 0 and 1.

choice (seq)

Choose a random element from the non-empty sequence seq and return it.

cunifvariate (mean, arc)

Circular uniform distribution. mean is the mean angle, and arc is the range of the distribution, centered around the mean angle. Both values must be expressed in radians, and can range between 0 and . Returned values will range between mean - arc/2 and mean + arc/2.

expovariate (lambd)

Exponential distribution. lambd is 1.0 divided by the desired mean. (The parameter would be called "lambda", but that is a reserved word in Python.) Returned values will range from 0 to positive infinity.

gamma (alpha, beta)

Gamma distribution. (Not the gamma function!) Conditions on the parameters are alpha > -1 and beta > 0.

gauss (mu, sigma)

Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function defined below.

lognormvariate (mu, sigma)

Log normal distribution. If you take the natural logarithm of this distribution, you'll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.

normalvariate (mu, sigma)

Normal distribution. mu is the mean, and sigma is the standard deviation.

paretovariate (alpha)

Pareto distribution. alpha is the shape parameter.

randint (a, b)

Return a random integer N, such that a<=N<=b

random()

Return a random real number N, such that 0<=N<1.

uniform (a, b)

Return a random real number N, such that a<=N<b.

vonmisesvariate (mu, kappa)

mu is the mean angle, expressed in radians between 0 and pi, and kappa is the concentration parameter, which must be greater then or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to .

weibullvariate (alpha, beta)

Weibull distribution. alpha is the scale parameter and beta is the shape parameter.

Attributes defined by the string module

Name

Description

digits

The string '0123456789'.

hexdigits

The string `0123456789abcdefABCDEF'.

letters

The concatenation of the strings lowercase' and uppercase' described below.

lowercase

A string containing all the characters that are considered lowercase letters. On most systems this is the string `abcdefghijklmnopqrstuvwxyz'.

octdigits

The string `01234567'.

uppercase

A string containing all the characters that are considered uppercase letters. On most systems this is the string `ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

whitespace

A string containing all characters that are considered "white space". On most systems this includes the characters space, tab, line feed, return, form feed, and vertical tab.

atof(S)

Convert a string to a floating point number.

atoi(S[, BASE])

Convert string S to an integer in the given BASE. The string must consist of one or more digits, optionally preceded by a sign (+' or -'). The BASE defaults to 10. If the base is 0, a default base is chosen depending on the leading characters of the string (after stripping the sign): '0x' or '0X' means 16, '0' means 8, anything else means 10. If BASE is 16, a leading '0x' or '0X' is always accepted.

capitalize(W)

Capitalize the first character of the argument.

capwords(S)

Convert sequences of spaces, tabs, new-line characters, and returns to single spaces and convert every lower-case letter at the beginning of the string or that follows space to an uppercase letter.

find(S, SUB[, START])

Return the lowest index in S not smaller than START where the sub-string SUB is found. Return -1 when SUB does not occur as a sub-string of S with index at least START. If START is omitted, the default value is 0. If START is negative, then it is added to the length of the string.

rfind(S, SUB[, START])

Like find but find the highest index.

index(S, SUB[, START])

Like find but raise a ValueError exception when the substring is not found.

rindex(S, SUB[, START])

Like rfind but raise ValueError exception when the substring is not found.

count(S, SUB[, START])

Return the number of (non-overlapping) occurrences of substring SUB in string S with index at least START. If START is omitted, the default value is 0. If START is negative, then it is added to the length of the string.

lower(S)

Convert letters to lower case.

maketrans(FROM, TO)

Return a translation table suitable for passing to string.translate, that will map each character in FROM into the character at the same position in TO; FROM and TO must have the same length.

split(S[, SEP[, MAX]])

Return a list of the words of the string S. If the optional second argument SEP is absent or None, the words are separated by arbitrary strings of white-space characters (space, tab, new line, return, form feed). If the second argument SEP is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more items than the number of non-overlapping occurrences of the separator in the string. The optional third argument MAX defaults to 0. If it is nonzero, at most MAX number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most MAX+1 elements).

join(WORDS[, SEP])

Concatenate a list or tuple of words with intervening occurrences of SEP. The default value for SEP is a single space character. It is always true that string.join(string.split(S, SEP), SEP) equals S.

lstrip(S)

Remove leading white space from string S.

rstrip(S)

Remove trailing white space from string S.

strip(S)

Remove leading and trailing white space from string S.

swapcase(S)

Convert lower case letters to upper case and vice versa.

translate(S, TABLE[, DELS])

Delete all characters from S that are in DELS (if present), and then translate the characters using TABLE, which is a 256-character string giving the translation for each character value, indexed by its ordinal.

upper(S)

Convert letters to upper case.

ljust(S, WIDTH)

rjust(S, WIDTH)

center(S, WIDTH)

These functions respectively left-justify, right-justify and center a string in a field of given width. They return a string that is at least WIDTH characters wide, created by padding the string S with spaces until the given width on the right, left or both sides. The string is never truncated.

zfill(S, WIDTH)

Pad a numeric string on the left with zero digits until the given width is reached. Strings starting with a sign are handled properly.

Attributes defined by the whrandom module

Name

Description

choice (seq)

Choose a random element from the non-empty sequence seq and return it.

randint (a, b)

Return a random integer N, such that a<=N<=b

random()

Return a random real number N, such that 0<=N<1.

seed(X, Y, Z)

Initialize the random number generator from the integers X, Y and Z.

uniform (a, b)

Return a random real number N, such that a<=N<b.

 

The _ variable has a method, has_key, which can be used to check whether or not a variable is in the namespace:

<dtml-if "_.has_key('sequence-length')">

Note that when a callable value is looked up with the _ variable, it is called automatically, as is done with the name attribute. In the case where it is undesirable to have the value called automatically, the g etitem method of the _ variable is used to look up the value. The getitem method accepts two arguments, the name to be looked up and a flag indicating whether or not the value is to be returned:

<dtml-var "_.getitem(name, 0)">

 

Previous Chapter | Up | Next Section | Contents