next | previous | forward | backward | up | top | index | toc | Macaulay2 website
Python :: addPyToM2Function

addPyToM2Function -- convenience function for adding value hooks

Synopsis

Description

Most of the hooks used by value have the same general format: if the python object has a particular type, then use a particular function to convert it to a corresponding Macaulay2 thing. This function simplifies the process of adding such a hook.

For example, suppose we would like to convert Fraction objects from the Python fractions module to QQ objects. Without adding a hook, value will do nothing with these objects.

i1 : fractions = import "fractions"

o1 = <module 'fractions' from '/usr/lib/python3.9/fractions.py'>

o1 : PythonObject of class module
i2 : x = fractions@@"Fraction"(2, 3)

o2 = 2/3

o2 : PythonObject of class fractions.Fraction
i3 : value x

o3 = 2/3

o3 : PythonObject of class fractions.Fraction

So we write a function to do the conversion and then install the hook using addPyToM2Function.

i4 : toQQ = x -> value x@@"numerator" / value x@@"denominator";
i5 : addPyToM2Function("Fraction", toQQ, "Fraction -> QQ");
i6 : value x

     2
o6 = -
     3

o6 : QQ
i7 : hooks value

o7 = {0 => (value, PythonObject, Strategy => unknown -> PythonObject)    }
     {1 => (value, PythonObject, Strategy => function -> FunctionClosure)}
     {2 => (value, PythonObject, Strategy => Counter -> Tally)           }
     {3 => (value, PythonObject, Strategy => dict -> HashTable)          }
     {4 => (value, PythonObject, Strategy => set -> Set)                 }
     {5 => (value, PythonObject, Strategy => list -> List)               }
     {6 => (value, PythonObject, Strategy => tuple -> Sequence)          }
     {7 => (value, PythonObject, Strategy => str -> String)              }
     {8 => (value, PythonObject, Strategy => complex -> CC)              }
     {9 => (value, PythonObject, Strategy => float -> RR)                }
     {10 => (value, PythonObject, Strategy => int -> ZZ)                 }
     {11 => (value, PythonObject, Strategy => bool -> Boolean)           }
     {12 => (value, PythonObject, Strategy => Fraction -> QQ)            }

o7 : NumberedVerticalList

Ways to use addPyToM2Function :

For the programmer

The object addPyToM2Function is a method function.