11---
22layout : default
33title : Executing code
4- nav_order : 5
4+ nav_order : 7
55---
66
77The [ MarkLogic REST service extension] ( https://docs.marklogic.com/REST/client/service-extension ) supports the
88execution of custom code, whether via an inline script or an existing module in your application's modules database.
9- The MarkLogic Python client simplifies execution of custom code both by managing some of the complexity of submitting
10- and custom code and also converting the multipart response into more useful Python data types.
9+ The MarkLogic Python client supports execution of custom code by simplifying the submission of custom code
10+ and converting the multipart response into more useful Python data types.
1111
1212## Setup
1313
@@ -26,14 +26,14 @@ The [v1/eval REST endpoint](https://docs.marklogic.com/REST/POST/v1/eval) suppor
2626and XQuery queries. Each type of query can be easily submitted via the client:
2727
2828```
29- client.eval.javascript( "fn.currentDateTime()")
30- client.eval.xquery( "fn:current-dateTime()")
29+ client.eval(javascript= "fn.currentDateTime()")
30+ client.eval(xquery= "fn:current-dateTime()")
3131```
3232
3333Variables can optionally be provided via a ` dict ` :
3434
3535```
36- results = client.eval.javascript( 'Sequence.from([{"hello": myValue}])', vars={"myValue": "world"})
36+ results = client.eval(javascript= 'Sequence.from([{"hello": myValue}])', vars={"myValue": "world"})
3737assert "world" == results[0]["hello"]
3838```
3939
@@ -48,9 +48,16 @@ and XQuery main modules that have been deployed to your application's modules da
4848the client in the following manner:
4949
5050```
51+ # Set the input to the URI of the module you wish to invoke in your application's
52+ # modules database.
5153client.invoke("/path/to/module.sjs")
5254```
5355
56+ You can provide variables to your module in the same fashion as when evaluating custom code:
57+
58+ ```
59+ client.invoke("/path/to/module.sjs", vars={"my_var1": "value1"})
60+ ```
5461
5562## Conversion of data types
5663
@@ -59,15 +66,15 @@ value having MarkLogic-specific type information. The client will use this type
5966an appropriate Python data type. For example, each JSON object into the example below is converted into a ` dict ` :
6067
6168```
62- results = client.eval.javascript( 'Sequence.from([{"doc": 1}, {"doc": 2}])')
69+ results = client.eval(javascript= 'Sequence.from([{"doc": 1}, {"doc": 2}])')
6370assert len(results) == 2
6471assert results[0]["doc"] == 1
6572assert results[1]["doc"] == 2
6673```
6774
6875The following table describes how each MarkLogic type is associated with a Python data type. For any
69- MarkLogic type not listed in the table, such as ` hexBinary ` and ` base64Binary ` , the value is not converted and will be
70- of type ` bytes ` .
76+ MarkLogic type not listed in the table, such as ` hexBinary ` and ` base64Binary ` , the value is not converted and will
77+ remain of type ` bytes ` .
7178
7279| MarkLogic type | Python type |
7380| --- | --- |
@@ -92,3 +99,9 @@ the multipart `X-URI` header. Otherwise, a value of type `dict`, `str`, or `byte
9299Each ` client.eval ` method and ` client.invoke ` accept a ` return_response ` argument. When that
93100argument is set to ` True ` , the original response is returned. This can be useful for custom
94101processing of the response or debugging requests.
102+
103+ ## Referencing a transaction
104+
105+ The ` client.eval ` and ` client.invoke ` functions both support referencing a
106+ [ REST API transaction] ( https://docs.marklogic.com/REST/client/transaction-management ) via the ` tx `
107+ argument. See [ the guide on transactions] ( transactions.md ) for further information.
0 commit comments