Package: faunadb
Python driver forFaunaDB.Note: This driver supports an older version of the Fauna API. The latest version of the official
Fauna Python Driver is located [here](https://pypi.org/project/faunadb/) (we encourage all new
development to use this new version where possible).Installation$pipinstallfaunadbCompatibilityThe following versions of Python are supported:Python 3.6Python 3.7Python 3.8Python 3.9Python 3.10DocumentationDriver documentation is available athttps://fauna.github.io/faunadb-python/4.5.0/api/.See theFaunaDB Documentationfor a complete API reference, or look intestsfor more examples.Basic Usagefromfaunadbimportqueryasqfromfaunadb.objectsimportReffromfaunadb.clientimportFaunaClientclient=FaunaClient(secret="your-secret-here")indexes=client.query(q.paginate(q.indexes()))print(indexes)Document StreamingFauna supports document streaming, where changes to a streamed document are pushed to all clients subscribing to that document.The following section provides an example for managing a document stream.The streaming API is blocking by default, the choice and mechanism for handling concurrent streams is left to the application developer:fromfaunadbimportqueryasqfromfaunadb.objectsimportReffromfaunadb.clientimportFaunaClientclient=FaunaClient(secret="your-secret-here")coll=client.query(q.create_collection({"name":"sc"}))doc=client.query(q.create(coll["ref"],{"data":{"x":0}}))stream=Nonedefon_start(event):print("started stream at%s"%(event.txn))client.query(q.update(doc["ref"],{"data":{"x":"updated"}}))defon_version(event):print("on_version event at%s"%(event.txn))print("    event:%s"%(event.event))stream.close()defon_error(event):print("Received error event%s"%(event))options={"fields":["document","diff"]}stream=client.stream(doc["ref"],options,on_start,on_error,on_version)stream.start()Observing MetricsIts possible to observe each query’s metrics by providing an “observer” callback.More information on query metrics is available in theFaunaDB Documentation.Here is a simple example:fromfaunadbimportqueryasqfromfaunadb.clientimportFaunaClientfromfaunadb.errorsimportFaunaError# The observer callback, which takes the HTTP response for a querydefobserve(response):h=response.response_headersprint('bytesOut:',h["x-compute-ops"])print('queryTime:',h["x-query-time"])print('readOps:',h["x-byte-read-ops"])print('writeOps:',h["x-byte-write-ops"])print('retries:',h["x-txn-retries"])# Connect to a local Fauna Dev instanceclient=FaunaClient(secret="secret",domain="localhost",scheme="http",port=8443,observer=observe)try:result=client.query(q.paginate(q.collections()))exceptFaunaErroraserr:print("err: ",err)else:print(result)Building it yourselfSetup$virtualenvvenv$sourcevenv/bin/activate$pipinstall.TestingTo run the tests you must have a FaunaDB database available.
Then set the environment variableFAUNA_ROOT_KEYto your database’s root key.
If you use FaunaDB cloud, this is the password you log in with.Tip: Setting theFAUNA_QUERY_TIMEOUT_MSenvironment variable will
set a timeout in milliseconds for all queries.Then runmake test.
To test a single test, use e.g.python-munittest tests.test_client.ClientTest.test_ping.Tests can also be run via a Docker container withFAUNA_ROOT_KEY="your-cloud-secret"makedocker-test(an alternate Alpine-based Python image can be provided viaRUNTIME_IMAGE).CoverageTo run the tests with coverage, install the coverage dependencies withpip install .[coverage],
and then runmake coverage. A summary will be displayed to the terminal, and a detailed coverage report
will be available athtmlcov/index.html.ContributeGitHub pull requests are very welcome.LicenseCopyright 2020Fauna, Inc.Licensed under the Mozilla Public License, Version 2.0 (the
“License”); you may not use this software except in compliance with
the License. You may obtain a copy of the License athttp://mozilla.org/MPL/2.0/Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.