Giter Site home page Giter Site logo

algorithmia-python's Introduction

Algorithmia Common Library (python)

Python client library for accessing the Algorithmia API For API documentation, see the PythonDocs

Install from PyPi

pip install algorithmia

Install from source

Build algorithmia client wheel:

python setup.py bdist_wheel

Install a wheel manually:

pip install --user --upgrade dist/algorithmia-*.whl

Use

First create an Algorithmia client:

import Algorithmia

apiKey = '{{Your API key here}}'

client = Algorithmia.client(apiKey)

Now you can call an algorithm:

result = client.algo('util/echo').pipe('Hello, world!')

The result of a call is either an AlgoException (indicating an error) or an AlgoResponse:

print result.result    # Hello, world!
print result.metadata  # Metadata(content_type='text',duration=0.0002127)
print result.metadata.duration # 0.0002127
client.algo('util/whoopsWrongAlgo').pipe('Hello, world!')  # Algorithmia.algo_response.AlgoException: algorithm algo://util/whoopsWrongAlgo not found

You can also set options (query paramters in the spec):

from Algorithmia.algorithm import OutputType
client.algo('util/echo').set_options(timeout=60, stdout=False, output=OutputType.raw)

Working with data

After creating a client you can store data within Algorithmia as well

# Create a directory "foo"
foo = client.dir("data://.my/foo")
foo.create();

# You can also set the ACL for the directory on creation or update it
from Algorithmia.acl import ReadAcl, AclType
foo.create(ReadAcl.public)   # ReadAcl.public is a wrapper for Acl(AclType.public) to make things easier

acl = foo.get_permissions()  # Acl object
acl.read_acl == AclType.public  # True

foo.update_permissions(ReadAcl.private)
foo.get_permissions().read_acl == AclType.private # True


# Upload files to "foo" directory
foo.file("sample.txt").put("sample text contents")
foo.file("binary_file").put(some_binary_data)
foo.putFile(open("/path/to/myfile"))

# List files in "foo"
for file in foo.files():
    print file.path " at URL: " + file.url + " last modified " + file.last_modified

# Note nested directories are not currently supported by the API, but there are an analogous .dirs and .list methods

# Get contents of files
sampleText = foo.file("sample.txt").getString()  # String object
binaryContent = foo.file("binary_file").getBytes()  # Binary data
tempFile = foo.file("myfile").getFile()   # Open file descriptor

# Delete files and directories
foo.file("sample.txt").delete()
foo.delete(true) // true implies force deleting the directory and its contents

Upgrading from 0.9.x

The main backwards incompatibility between 0.9.x and 1.0.0 is the result of an algorithm call. In 0.9.x the result of an algorithm call is just the algorithm's output (which is not the full spec returned by the API)

result = client.algo('util/echo').pipe('Hello, world!')
print result   # Hello, world!

In 1.0.x the result of an algorithm matches the API specification. The algorithm's output is nested in an attribute 'result', and metadata can be accessed via the 'metadata' attribute.

result = client.algo('util/echo').pipe('Hello, world!')
print result.result     # Hello, world!
print result.metadata   # content_type, duration etc

Aside from that you should be able to drop in the newest version of the client. Another advantage of using the newest client is full access to the entire Data API specification.

Running tests

export ALGORITHMIA_API_KEY={{Your API key here}}
cd Test
python datadirectorytest.py
python util.py

algorithmia-python's People

Contributors

httpdss avatar jamesatha avatar platypii avatar pmcq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.