classad – ClassAd reference¶
This page is an exhaustive reference of the API exposed by the classad
module. It is not meant to be a tutorial for new users but rather a helpful
guide for those who already understand the basic usage of the module.
This reference covers the following:
- Module-Level Functions: The module-level
classadfunctions. ClassAd: Representation of a ClassAd.ExprTree: Representation of a ClassAd expression.- Useful Enumerations: Useful enumerations.
Module-Level Functions¶
-
classad.quote(input)¶ Converts the Python string into a ClassAd string literal; this handles all the quoting rules for the ClassAd language. For example:
>>> classad.quote('hello"world') '"hello\\"world"'
This allows one to safely handle user-provided strings to build expressions. For example:
>>> classad.ExprTree("Foo =?= %s" % classad.quote('hello"world')) Foo is "hello\"world"
Parameters: input (str) – Input string to quote. Returns: The corresponding string literal as a Python string. Return type: str
-
classad.unquote(input)¶ Converts a ClassAd string literal, formatted as a string, back into a python string. This handles all the quoting rules for the ClassAd language.
Parameters: input (str) – Input string to unquote. Returns: The corresponding Python string for a string literal. Return type: str
-
classad.parseAds(input, parser=Auto)¶ Parse the input as a series of ClassAds.
Parameters: Returns: An iterator that produces
ClassAd.
-
classad.parseNext(input, parser=Auto)¶ Parse the next ClassAd in the input string. Advances the
inputto point after the consumed ClassAd.Parameters: Returns: An iterator that produces
ClassAd.
-
classad.parseOne(input, parser=Auto)¶ Parse the entire input into a single
ClassAdobject.In the presence of multiple ClassAds or blank lines in the input, continue to merge ClassAds together until the entire input is consumed.
Parameters: Returns: Corresponding
ClassAdobject.Return type:
-
classad.version()¶ Return the version of the linked ClassAd library.
-
classad.lastError()¶ Return the string representation of the last error to occur in the ClassAd library.
As the ClassAd language has no concept of an exception, this is the only mechanism to receive detailed error messages from functions.
-
classad.Attribute(name)¶ Given an attribute name, construct an
ExprTreeobject which is a reference to that attribute.Note
This may be used to build ClassAd expressions easily from python. For example, the ClassAd expression
foo == 1can be constructed by the python codeAttribute("foo") == 1.Parameters: name (str) – Name of attribute to reference. Returns: Corresponding expression consisting of an attribute reference. Return type: ExprTree
-
classad.Function(name, arg1, arg2, ...)¶ Given function name name, and zero-or-more arguments, construct an
ExprTreewhich is a function call expression. The function is not evaluated.For example, the ClassAd expression
strcat("hello ", "world")can be constructed by the pythonFunction("strcat", "hello ", "world").Returns: Corresponding expression consisting of a function call. Return type: ExprTree
-
classad.Literal(obj)¶ Convert a given python object to a ClassAd literal.
Python strings, floats, integers, and booleans have equivalent literals in the ClassAd language.
Parameters: obj – Python object to convert to an expression. Returns: Corresponding expression consising of a literal. Return type: ExprTree
-
classad.register(function, name=None)¶ Given the python function, register it as a function in the ClassAd language. This allows the invocation of the python function from within a ClassAd evaluation context.
Parameters: - function – A callable object to register with the ClassAd runtime.
- name (str) – Provides an alternate name for the function within the ClassAd library.
The default,
None, indicates to use the built in function name.
-
classad.registerLibrary(path)¶ Given a file system path, attempt to load it as a shared library of ClassAd functions. See the upstream documentation for configuration variable
CLASSAD_USER_LIBSfor more information about loadable libraries for ClassAd functions.Parameters: path (str) – The library to load.
Module Classes¶
-
class
classad.ClassAd¶ The
ClassAdobject is the python representation of a ClassAd. Where possible, theClassAdattempts to mimic a python dictionary. When attributes are referenced, they are converted to python values if possible; otherwise, they are represented by aExprTreeobject.The
ClassAdobject is iterable (returning the attributes) and implements the dictionary protocol. Theitems,keys,values,get,setdefault, andupdatemethods have the same semantics as a dictionary.-
__init__(ad)¶ Create a new ClassAd object; can be initialized via a string (which is parsed as an ad) or a dictionary-like object.
Note
Where possible, we recommend using the dedicated parsing functions (
parseOne(),parseNext(), orparseAds()) instead of using the constructor.Parameters: ad (str or dict) – Initial values for this object.
-
eval(attr)¶ Evaluate an attribute to a python object. The result will not be an
ExprTreebut rather an built-in type such as a string, integer, boolean, etc.Parameters: attr (str) – Attribute to evaluate. Returns: The Python object corresponding to the evaluated ClassAd attribute Raises: ValueError – if unable to evaluate the object.
-
lookup(attr)¶ Look up the
ExprTreeobject associated with attribute.No attempt will be made to convert to a Python object.
Parameters: attr (str) – Attribute to evaluate. Returns: The ExprTreeobject referenced byattr.
-
printOld()¶ Serialize the ClassAd in the old ClassAd format.
Returns: The “old ClassAd” representation of the ad. Return type: str
-
flatten(expression)¶ Given ExprTree object expression, perform a partial evaluation. All the attributes in expression and defined in this ad are evaluated and expanded. Any constant expressions, such as
1 + 2, are evaluated; undefined attributes are not evaluated.Parameters: expression ( ExprTree) – The expression to evaluate in the context of this ad.Returns: The partially-evaluated expression. Return type: ExprTree
-
matches(ad)¶ Lookup the
Requirementsattribute of givenadreturnTrueif theRequirementsevaluate toTruein our context.Parameters: ad ( ClassAd) – ClassAd whoseRequirementswe will evaluate.Returns: Trueif we satisfyad’s requirements;Falseotherwise.Return type: bool
-
symmetricMatch(ad)¶ Check for two-way matching between given ad and ourselves.
Equivalent to
self.matches(ad) and ad.matches(self).Parameters: ad ( ClassAd) – ClassAd to check for matching.Returns: Trueif both ads’ requirements are satisfied.Return type: bool
-
externalRefs(expr)¶ Returns a python list of external references found in
expr.An external reference is any attribute in the expression which is not defined by the ClassAd object.
Parameters: expr ( ExprTree) – Expression to examine.Returns: A list of external attribute references. Return type: list[str]
-
-
class
classad.ExprTree¶ The
ExprTreeclass represents an expression in the ClassAd language.As with typical ClassAd semantics, lazy-evaluation is used. So, the expression
"foo" + 1does not produce an error until it is evaluated with a call tobool()or theExprTree.eval()method.Note
The python operators for ExprTree have been overloaded so, if
e1ande2areExprTreeobjects, thene1 + e2is also an :class:ExprTreeobject. However, Python short-circuit evaluation semantics fore1 && e2causee1to be evaluated. In order to get the “logical and” of the two expressions without evaluating, usee1.and_(e2). Similarly,e1.or_(e2)results in the “logical or”.-
__init__(expr)¶ Parse the string
expras a ClassAd expression.Parameters: expr (str) – Initial expression, serialized as a string.
-
__str__()¶ Represent and return the ClassAd expression as a string.
Returns: Expression represented as a string. Return type: str
-
__int__()¶ Converts expression to an integer (evaluating as necessary).
-
__float__()¶ Converts expression to a float (evaluating as necessary).
-
and_(expr2)¶ Return a new expression, formed by
self && expr2.Parameters: expr2 ( ExprTree) – Right-hand-side expression to “and”Returns: A new expression, defined to be self && expr2.Return type: ExprTree
-
or_(expr2)¶ Return a new expression, formed by
self || expr2.Parameters: expr2 ( ExprTree) – Right-hand-side expression to “or”Returns: A new expression, defined to be self || expr2.Return type: ExprTree
-
is_(expr2)¶ Logical comparison using the “meta-equals” operator.
Parameters: expr2 ( ExprTree) – Right-hand-side expression to=?=operator.Returns: A new expression, formed by self =?= expr2.Return type: ExprTree
-
isnt_(expr2)¶ Logical comparison using the “meta-not-equals” operator.
Parameters: expr2 ( ExprTree) – Right-hand-side expression to=!=operator.Returns: A new expression, formed by self =!= expr2.Return type: ExprTree
-
sameAs(expr2)¶ Returns
Trueif givenExprTreeis same as this one.Parameters: expr2 ( ExprTree) – Expression to compare against.Returns: Trueif and only ifexpr2is equivalent to this object.Return type: bool
-
eval()¶ Evaluate the expression and return as a ClassAd value, typically a Python object.
Returns: The evaluated expression as a Python object.
-
Useful Enumerations¶
Deprecated Functions¶
The functions in this section are deprecated; new code should not use them and existing code should be rewritten to use their replacements.