Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- arg :: ToPy a => a -> Arg
- class FromPy a where
- fromPy :: SomeObject -> IO a
- data PyCastException = PyCastException String
- class ToPy a where
- toPy :: a -> IO SomeObject
- call :: FromPy a => Text -> Text -> [Arg] -> [(Text, Arg)] -> IO a
- easyFromPy :: (Concrete p, Typeable h) => (p -> IO h) -> Proxy h -> SomeObject -> IO h
- easyToPy :: Object p => (h -> IO p) -> h -> IO SomeObject
- getAttribute :: FromPy a => Text -> Text -> IO a
- importModule :: Text -> IO Module
- initialize :: IO ()
- setAttribute :: ToPy a => Text -> Text -> a -> IO ()
Documentation
arg :: ToPy a => a -> Arg Source #
Builds a Python argument from any Haskell type with a ToPy
instance
FromPy
instances indicate that a type can be marshalled from Python to Haskell automatically
For example, FromPy Integer
indicates that we know how to take some Python object and convert
it into a Haskell Integer. If the Python object is int
, then we can cast properly. Failed casts throw a PyCastException
fromPy :: SomeObject -> IO a Source #
Takes some Python object, and converts it to the corresponding Haskell type by going over FFI. Might throw a PyCastException
Generally you'll only need to call fromPy
manually on some type when writing your own FromPy
instances for another type
Instances
data PyCastException Source #
An exception representing a failed cast from a Python object to Haskell value, usually because the expected type of the Python object was not correct.
Carries a String
which represents the name of the expected Haskell type which caused a failed cast. If using easyFromPy
, this String
is found with typeRep
Instances
Exception PyCastException Source # | |
Defined in CPython.Simple.Instances | |
Show PyCastException Source # | |
Defined in CPython.Simple.Instances showsPrec :: Int -> PyCastException -> ShowS # show :: PyCastException -> String # showList :: [PyCastException] -> ShowS # |
ToPy
instances indicate that a type can be marshalled from Haskell to Python automatically
For example, ToPy Integer
indicates that we know how to take a Haskell Integer
and convert
it into a Python int
object
toPy :: a -> IO SomeObject Source #
Instances
ToPy Text Source # | |
Defined in CPython.Simple.Instances | |
ToPy String Source # | |
Defined in CPython.Simple.Instances | |
ToPy Integer Source # | |
Defined in CPython.Simple.Instances | |
ToPy Bool Source # | |
Defined in CPython.Simple.Instances | |
ToPy Char Source # | |
Defined in CPython.Simple.Instances | |
ToPy Double Source # | |
Defined in CPython.Simple.Instances | |
ToPy a => ToPy (Maybe a) Source # | |
Defined in CPython.Simple.Instances | |
ToPy a => ToPy [a] Source # | |
Defined in CPython.Simple.Instances toPy :: [a] -> IO SomeObject Source # | |
(ToPy a, ToPy b) => ToPy (a, b) Source # | |
Defined in CPython.Simple.Instances toPy :: (a, b) -> IO SomeObject Source # | |
(ToPy a, ToPy b, ToPy c) => ToPy (a, b, c) Source # | |
Defined in CPython.Simple.Instances toPy :: (a, b, c) -> IO SomeObject Source # | |
(ToPy a, ToPy b, ToPy c, ToPy d) => ToPy (a, b, c, d) Source # | |
Defined in CPython.Simple.Instances toPy :: (a, b, c, d) -> IO SomeObject Source # |
:: FromPy a | |
=> Text | Python module name |
-> Text | Python function name |
-> [Arg] | Python function's arguments |
-> [(Text, Arg)] | Python function's keyword arguments (kwargs) as |
-> IO a |
The most common use case of Simple
is calling some Python function
For example, if we wanted to wrap Python's random.randint(low, high)
, we could write this:
randint :: Integer -> Integer -> IO Integer randint low high = call "random" "randint" [arg low, arg high] []
Because of the FromPy
instance in call
's type signature, we can infer what to do to convert a Python value back into Haskell, if given the type. In this example using random.uniform
, although we use a similar definition as for randint
, we correct cast to Double
instead of Integer
uniform :: Integer -> Integer -> IO Double uniform low high = call "random" "uniform" [arg low, arg high] []
We can also use the TypeApplications
language extension to tell call
what type to use, if needed
call @Double "random" "uniform" [arg low, arg high] []
Calling a function with mixed positional and keyword arguments is also fairly straightforward.
The example is equivalent to calling pyautogui.moveTo(x, y, duration=seconds)
moveToDuration :: Integer -> Integer -> Double -> IO () moveToDuration x y seconds = call "pyautogui" "moveTo" [arg x, arg y] [("duration", arg seconds)]
:: (Concrete p, Typeable h) | |
=> (p -> IO h) | python from- conversion, e.g. Py.fromFloat |
-> Proxy h | proxy for the type being converted to |
-> SomeObject | python object to cast from |
-> IO h | Haskell value |
Helper that takes a conversion function and a Python object, and casts the Python object into a Haskell value.
Lets you define fromPy
with just a Python conversion function
We use Proxy
to infer the type name for use in case of a failed cast. In the context of defining an instance, this type will be inferrable, so you can just provide a Proxy
value
:: Object p | |
=> (h -> IO p) | python to- conversion, e.g. Py.toFloat |
-> h | haskell type being converted |
-> IO SomeObject | Python object |
Helper that lets you convert a Haskell value to a Python object by providing both a Python conversion function (from the Haskell type, over FFI, to some Python Object) as well as the Haskell value
Lets you define toPy
with just a Python conversion function
Get the value of an attribute of some Python module
This example is equivalent to getting random.BPF
in Python
getBpf :: IO Integer getBpf = getAttribute "random" "BPF"
importModule :: Text -> IO Module Source #
Given a Python module name as text, imports it as a Module
Throws an exception if e.g. the module name was misspelled, or isn't installed
initialize :: IO () Source #
Kicks off talking to Python, and will need to be called before using other functions