pyccel.ast.builtins module#
The Python interpreter has a number of built-in functions and types that are always available.
In this module we implement some of them in alphabetical order.
- class pyccel.ast.builtins.Lambda(variables, expr)[source]#
Bases:
PyccelAstNode
Represents a call to Python’s lambda for temporary functions.
Represents a call to Python’s built-in function lambda for temporary functions.
- Parameters:
variables (tuple of symbols) – The arguments to the lambda expression.
expr (TypedAstNode) – The expression carried out when the lambda function is called.
- property expr#
The expression carried out when the lambda function is called
- property variables#
The arguments to the lambda function
- class pyccel.ast.builtins.PythonAbs(x)[source]#
Bases:
PyccelFunction
Represents a call to Python abs for code generation.
Represents a call to Python’s built-in function abs.
- Parameters:
x (TypedAstNode) – The argument passed to the function.
- property arg#
The argument passed to the abs function.
The argument passed to the abs function.
- name = 'abs'#
- class pyccel.ast.builtins.PythonBool(arg)[source]#
Bases:
PyccelFunction
Represents a call to Python’s native bool() function.
Represents a call to Python’s native bool() function which casts an argument to a boolean.
- Parameters:
arg (TypedAstNode) – The argument passed to the function.
- property arg#
Get the argument which was passed to the function.
Get the argument which was passed to the function.
- name = 'bool'#
- class pyccel.ast.builtins.PythonComplex(arg0, arg1=Literal(0.0))[source]#
Bases:
PyccelFunction
Represents a call to Python’s native complex() function.
Represents a call to Python’s native complex() function which casts an argument to a complex number.
- Parameters:
arg0 (TypedAstNode) – The first argument passed to the function (either a real or a complex).
arg1 (TypedAstNode, default=0) – The second argument passed to the function (the imaginary part).
- property imag#
Returns the imaginary part of the complex
- property internal_var#
When the complex call is a cast, returns the variable being cast.
When the complex call is a cast, returns the variable being cast. This property should only be used when handling a cast.
- property is_cast#
Indicates if the function is casting or assembling a complex
- name = 'complex'#
- property real#
Returns the real part of the complex
- class pyccel.ast.builtins.PythonComplexProperty(arg)[source]#
Bases:
PyccelFunction
Represents a call to the .real or .imag property.
Represents a call to a property of a complex number. The relevant properties are the .real and .imag properties.
e.g: >>> a = 1+2j >>> a.real 1.0
- Parameters:
arg (TypedAstNode) – The object which the property is called from.
- property internal_var#
Return the variable on which the function was called
- class pyccel.ast.builtins.PythonConjugate(arg)[source]#
Bases:
PyccelFunction
Represents a call to the .conjugate() function.
Represents a call to the conjugate function which is a member of the builtin types int, float, complex. The conjugate function is called from Python as follows:
>>> a = 1+2j >>> a.conjugate() 1-2j
- Parameters:
arg (TypedAstNode) – The variable/expression which was passed to the conjugate function.
- property internal_var#
Return the variable on which the function was called
- name = 'conjugate'#
- class pyccel.ast.builtins.PythonDict(keys, values)[source]#
Bases:
PyccelFunction
Class representing a call to Python’s {} function.
Class representing a call to Python’s {} function which generates a literal Python dict. This operator does not handle **a expressions.
- Parameters:
keys (iterable[TypedAstNode]) – The keys of the new dictionary.
values (iterable[TypedAstNode]) – The values of the new dictionary.
- property keys#
The keys of the new dictionary.
The keys of the new dictionary.
- property values#
The values of the new dictionary.
The values of the new dictionary.
- class pyccel.ast.builtins.PythonDictFunction(*args, **kwargs)[source]#
Bases:
PyccelFunction
Class representing a call to the dict function.
Class representing a call to the dict function. This is different to the {} syntax as it is either a cast function or it uses arguments to create the dictionary. In the case of a cast function an instance of PythonDict is returned to express this concept. In the case of a copy this class stores the description of the copy operator.
- Parameters:
*args (TypedAstNode) – The arguments passed to the function call. If args are provided then only one argument should be provided. This object is copied unless it is a temporary PythonDict in which case it is returned directly.
**kwargs (dict[TypedAstNode]) – The keyword arguments passed to the function call. If kwargs are provided then no args should be provided and a PythonDict object will be created.
- property copied_obj#
The object being copied.
The object being copied to create a new dict instance.
- name = 'dict'#
- class pyccel.ast.builtins.PythonEnumerate(arg, start=None)[source]#
Bases:
Iterable
Represents a call to Python’s native enumerate() function.
Represents a call to Python’s native enumerate() function.
- Parameters:
arg (TypedAstNode) – The argument passed to the function.
start (TypedAstNode) – The start value of the enumeration index.
- property element#
Get the object which is being enumerated.
Get the object which is being enumerated.
- get_assign_targets()[source]#
Get objects that should be assigned to variables to use the enumerate targets.
Get objects that should be assigned to variables to use the enumerate targets. If the start of the enumerate is 0 then the only object that needs to be assigned is the element of the variable, however if the indexing is offset compared to the variable then both the index and the variable need to be created.
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_python_iterable_item()[source]#
Get the item of the iterable that will be saved to the loop targets.
Returns two objects that could be elements of the enumerate.
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_range()[source]#
Get a range that can be used to iterate over the enumerate iterable.
Get a range that can be used to iterate over the enumerate iterable.
- Returns:
A range that can be used to iterate over the enumerate iterable.
- Return type:
- name = 'enumerate'#
- property start#
Returns the value from which the indexing starts
- class pyccel.ast.builtins.PythonFloat(arg)[source]#
Bases:
PyccelFunction
Represents a call to Python’s native float() function.
Represents a call to Python’s native float() function which casts an argument to a floating point number.
- Parameters:
arg (TypedAstNode) – The argument passed to the function.
- property arg#
Get the argument which was passed to the function.
Get the argument which was passed to the function.
- name = 'float'#
- class pyccel.ast.builtins.PythonImag(arg)[source]#
Bases:
PythonComplexProperty
Represents a call to the .imag property.
Represents a call to the .imag property of an object with a complex type. e.g: >>> a = 1+2j >>> a.imag 1.0
- Parameters:
arg (TypedAstNode) – The object on which the property is called.
- name = 'imag'#
- class pyccel.ast.builtins.PythonInt(arg)[source]#
Bases:
PyccelFunction
Represents a call to Python’s native int() function.
Represents a call to Python’s native int() function which casts an argument to an integer.
- Parameters:
arg (TypedAstNode) – The argument passed to the function.
- property arg#
Get the argument which was passed to the function.
Get the argument which was passed to the function.
- name = 'int'#
- class pyccel.ast.builtins.PythonIsInstance(obj, class_or_tuple)[source]#
Bases:
PyccelFunction
Represents a call to Python’s isinstance function.
Represents a call to Python’s isinstance function which checks if an object has a specified type. This class exists to find a definition of isinstance in builtin_functions_dict but it should not be instantiated.
- Parameters:
obj (TypedAstNode) – The object whose type should be checked.
class_or_tuple (TypedAstNode) – A class or a tuple of classes describing the acceptable types for the object.
- class pyccel.ast.builtins.PythonLen(arg)[source]#
Bases:
PyccelFunction
Represents a len expression in the code.
Represents a call to the function len which calculates the length (aka the first element of the shape) of an object. This can usually be calculated in the generated code, but in an inhomogeneous object the integer value of the shape must be returned.
If the shape is unknown and cannot be determined at compile time then the first element of the shape is a PyccelArrayShapeElement which can be used to generate the equivalent C code using the STC library.
- Parameters:
arg (TypedAstNode) – The argument whose length is being examined.
- class pyccel.ast.builtins.PythonList(*args)[source]#
Bases:
TypedAstNode
Class representing a call to Python’s [,] function.
Class representing a call to Python’s [,] function which generates a literal Python list.
- Parameters:
*args (tuple of TypedAstNodes) – The arguments passed to the operator.
See also
FunctionalFor
The [] function when it describes a comprehension.
- property args#
Arguments of the list.
The arguments that were used to initialise the list.
- property is_homogeneous#
Indicates whether the list is homogeneous or inhomogeneous.
Indicates whether all elements of the list have the same dtype, rank, etc (homogenous) or if these values can vary (inhomogeneous). Lists are always homogeneous.
- class pyccel.ast.builtins.PythonListFunction(arg=None)[source]#
Bases:
PyccelFunction
Class representing a call to the list function.
Class representing a call to the list function. This is different to the [,] syntax as it only takes one argument and unpacks any variables.
- Parameters:
arg (TypedAstNode) – The argument passed to the function call.
- property copied_obj#
The object being copied.
The object being copied to create a new list instance.
- name = 'list'#
- class pyccel.ast.builtins.PythonMap(func, func_args)[source]#
Bases:
Iterable
Class representing a call to Python’s builtin map function.
Class representing a call to Python’s builtin map function.
- Parameters:
func (FunctionDef) – The function to be applied to the elements.
func_args (TypedAstNode) – The arguments to which the function will be applied.
- property func#
Arguments of the map
- property func_args#
Arguments of the function
- get_assign_targets()[source]#
Get objects that should be assigned to variables to use the map target.
Get objects that should be assigned to variables to use the map targets. This is the function calling the element of the variable that is found at the index.
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_python_iterable_item()[source]#
Get the item of the iterable that will be saved to the loop targets.
This is the function calling the element of the variable that is found at the index.
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_range()[source]#
Get a range that can be used to iterate over the map iterable.
Get a range that can be used to iterate over the map iterable.
- Returns:
A range that can be used to iterate over the map iterable.
- Return type:
- name = 'map'#
- class pyccel.ast.builtins.PythonMax(*x)[source]#
Bases:
PyccelFunction
Represents a call to Python’s built-in max function.
Represents a call to Python’s built-in max function.
- Parameters:
*x (list, tuple, PythonTuple, PythonList) – The arguments passed to the function.
- name = 'max'#
- class pyccel.ast.builtins.PythonMin(*x)[source]#
Bases:
PyccelFunction
Represents a call to Python’s built-in min function.
Represents a call to Python’s built-in min function.
- Parameters:
*x (list, tuple, PythonTuple, PythonList) – The arguments passed to the function.
- name = 'min'#
- class pyccel.ast.builtins.PythonPrint(expr, file='stdout')[source]#
Bases:
PyccelAstNode
Represents a call to the print function in the code.
Represents a call to the built-in Python function print in the code.
- Parameters:
expr (TypedAstNode) – The expression to print.
file (str, default='stdout') – One of [stdout,stderr].
- property expr#
The expression that should be printed.
The expression that should be printed.
- property file#
returns the output unit (stdout or stderr)
- name = 'print'#
- class pyccel.ast.builtins.PythonRange(*args)[source]#
Bases:
Iterable
Class representing a range.
Class representing a call to the built-in Python function range. This function is parametrised by an interval (described by a start element and a stop element) and a step. The step describes the number of elements between subsequent elements in the range.
- Parameters:
*args (tuple of TypedAstNodes) – The arguments passed to the range. If one argument is passed then it represents the end of the interval. If two arguments are passed then they represent the start and end of the interval. If three arguments are passed then they represent the start, end and step of the interval.
- get_assign_targets()[source]#
Get objects that should be assigned to variables to use the range.
This method is used to allow this class to be handled like other iterables which can be converted to PythonRange objects.
- Returns:
An empty list.
- Return type:
list[TypedAstNode]
- get_python_iterable_item()[source]#
Get the item of the iterable that will be saved to the loop targets.
Returns an element of the range indexed with the iterators previously provided via the set_loop_counters method (useful to determine the dtype etc of the loop iterator).
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_range()[source]#
Get this range.
Get this range. This method is used to allow this class to be handled like other iterables which can be converted to PythonRange objects.
- Returns:
This object.
- Return type:
- name = 'range'#
- property start#
Get the start of the interval.
Get the start of the interval which the range iterates over.
- property step#
Get the step between subsequent elements in the range.
Get the step between subsequent elements in the range.
- property stop#
Get the end of the interval.
Get the end of the interval which the range iterates over. The interval does not include this value.
- class pyccel.ast.builtins.PythonReal(arg)[source]#
Bases:
PythonComplexProperty
Represents a call to the .real property.
e.g: >>> a = 1+2j >>> a.real 1.0
- Parameters:
arg (TypedAstNode) – The object which the property is called from.
- name = 'real'#
- class pyccel.ast.builtins.PythonRound(number, ndigits=None)[source]#
Bases:
PyccelFunction
Class representing a call to Python’s native round() function.
Class representing a call to Python’s native round() function which rounds a float or integer to a given number of decimals.
- Parameters:
number (TypedAstNode) – The number to be rounded.
ndigits (TypedAstNode, optional) – The number of digits to round to.
- property arg#
The number to be rounded.
The number to be rounded.
- name = 'round'#
- property ndigits#
The number of digits to which the argument is rounded.
The number of digits to which the argument is rounded.
- class pyccel.ast.builtins.PythonSet(*args)[source]#
Bases:
TypedAstNode
Class representing a call to Python’s {,} function.
Class representing a call to Python’s {,} function which generates a literal Python Set.
- Parameters:
*args (tuple of TypedAstNodes) – The arguments passed to the operator.
- property args#
Arguments of the set.
The arguments that were used to initialise the set.
- property is_homogeneous#
Indicates whether the set is homogeneous or inhomogeneous.
Indicates whether all elements of the Set have the same dtype, precision, rank, etc (homogenous) or if these values can vary (inhomogeneous). sets are always homogeneous.
- class pyccel.ast.builtins.PythonSetFunction(copied_obj)[source]#
Bases:
PyccelFunction
Class representing a call to the set function.
Class representing a call to the set function. This is different to the {,} syntax as it only takes one argument and unpacks any variables.
- Parameters:
copied_obj (TypedAstNode) – The argument passed to the function call.
- name = 'set'#
- class pyccel.ast.builtins.PythonStr(arg)[source]#
Bases:
PyccelFunction
Represents a call to Python’s str function.
Represents a call to Python’s str function which describes a string cast.
- Parameters:
arg (TypedAstNode) – The argument that is cast to a string.
- name = 'str'#
- class pyccel.ast.builtins.PythonSum(arg)[source]#
Bases:
PyccelFunction
Represents a call to Python sum for code generation.
Represents a call to Python’s built-in function sum.
- Parameters:
arg (TypedAstNode) – The argument passed to the function.
- property arg#
The argument passed to the sum function.
The argument passed to the sum function.
- name = 'sum'#
- class pyccel.ast.builtins.PythonTuple(*args, prefer_inhomogeneous=False)[source]#
Bases:
TypedAstNode
Class representing a call to Python’s native (,) function which creates tuples.
Class representing a call to Python’s native (,) function which initialises a literal tuple.
- Parameters:
*args (tuple of TypedAstNode) – The arguments passed to the tuple function.
prefer_inhomogeneous (bool, default=False) – A boolean that can be used to ensure that the tuple is stocked as an inhomogeneous object even if it could be homogeneous.
- property args#
Arguments of the tuple.
The arguments that were used to initialise the tuple.
- property is_homogeneous#
Indicates whether the tuple is homogeneous or inhomogeneous.
Indicates whether all elements of the tuple have the same dtype, rank, etc (homogenous) or if these values can vary (inhomogeneous).
- class pyccel.ast.builtins.PythonTupleFunction[source]#
Bases:
TypedAstNode
Class representing a call to the tuple function.
Class representing a call to the tuple function. This is different to the (,) syntax as it only takes one argument and unpacks any variables.
This class should not be used to create an instance, it is simply a place-holder to indicate the class to the semantic parser.
- class pyccel.ast.builtins.PythonType(obj)[source]#
Bases:
PyccelFunction
Represents a call to the Python builtin type function.
The use of type in code is usually for one of two purposes. Firstly it is useful for debugging. In this case the print_string property is useful to obtain the underlying type. It is equally useful to provide datatypes to objects in templated functions. This double usage should be considered when using this class.
- Parameters:
obj (TypedAstNode) – The object whose type we wish to investigate.
- property arg#
Returns the object for which the type is determined
- property print_string#
Return a LiteralString describing the type.
Constructs a LiteralString containing the message usually printed by Python to describe this type. This string can then be easily printed in each language.
- class pyccel.ast.builtins.PythonZip(*args)[source]#
Bases:
Iterable
Represents a call to Python zip for code generation.
Represents a call to Python’s built-in function zip.
- Parameters:
*args (tuple of TypedAstNode) – The arguments passed to the function.
- property args#
The arguments passed to the function.
Tuple containing all the arguments passed to the function call.
- get_assign_targets()[source]#
Get objects that should be assigned to variables to use the zip targets.
Get objects that should be assigned to variables to use the zip targets.
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_python_iterable_item()[source]#
Get the item of the iterable that will be saved to the loop targets.
Returns an element of the zip indexed with the iterators previously provided via the set_loop_counters method (useful to determine the dtype etc of the loop iterator).
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_range()[source]#
Get a range that can be used to iterate over the zip iterable.
Get a range that can be used to iterate over the zip iterable.
- Returns:
A range that can be used to iterate over the zip iterable.
- Return type:
- name = 'zip'#
- class pyccel.ast.builtins.VariableIterator(var)[source]#
Bases:
Iterable
Represents a call to Python’s iter function on a variable.
Represents a call to Python’s iter function on a variable. This is useful for for loops as this function is called implicitly.
- Parameters:
var (Variable) – The Variable that is iterated over.
- get_assign_targets()[source]#
Get objects that should be assigned to variables to use the variable iterator targets.
Returns an element of the variable indexed with the iterators previously provided via the set_loop_counters method.
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_python_iterable_item()[source]#
Get the item of the iterable that will be saved to the loop targets.
Returns an element of the variable indexed with the iterators previously provided via the set_loop_counters method (useful to determine the dtype etc of the loop iterator).
- Returns:
A list of objects that should be assigned to variables.
- Return type:
list[TypedAstNode]
- get_range()[source]#
Get a range that can be used to iterate over the variable.
Get a range that can be used to iterate over the variable.
- Returns:
A range that can be used to iterate over the enumerate iterable.
- Return type:
- property variable#
The variable being iterated over.
The variable being iterated over.