This example shows the 5 steps from http://www.analytica.com/learn/5steps/ using the REST interface through a Python binding.
Server Installation
First, you need to install the Analytica server. Please download the installer for your operating system and install the server. Once installed, start it up. In the following we assume that the server is running on localhost.
Python Binding
A Python binding is implemented for this example using Python 3.3. It consists of two files:
- web.py (web.txt – please rename from web.txt to web.py)
- rest.py (rest.txt – please rename from rest.txt to rest.py)
web.py contains a few helper functions, and rest.py contains the Python binding of a few REST interfaces that are relevant to the 5 Steps example.
The code is not generic and not meant to be production code. The goal is to show a minimal Python binding as a starting point that shows the core concepts and main functionalities.
Example
The calls of the 5 Steps example are coded in a file called fivesteps.py (fivesteps.txt – please rename from fivesteps.txt to fivesteps.py) and the calls in this file correspond to the REST calls in http://www.analytica.com/learn/analytica-rest-interface-in-5-steps/
The directory structure is
./analytica/rest.py ./analytica/web.py ./examples/fivesteps.py
fivesteps.py
The contents of fivesteps.py is as follows.
import sys
sys.path.append('../analytica')
from rest import *
response = None
# Check Connectivity
print("-->")
response = ping()
print("Success: ", response['success'])
# Connecting the Sample Database
print("-->")
response = connect("so", "datasets.analytica.com", "43657", "analytica", "analytica", "MongoDB")
print("Success: ", response['success'])
# 0. Exploring the Types
print("-->")
response = describe("so")
print("Result: ", response['result'])
print("-->")
response = describe("so.users")
print("Result: ", response['result'])
# 1. What are the maximum, minimum, and average reputation scores for users? [Calculating properties for a collection]
print("-->")
response = set("so.maxreputation", "max(users.Reputation)")
print("Success: ", response['success'])
print("-->")
response = get("so.maxreputation")
print("Result: ", response['result'])
print("-->")
response = describe("so.maxreputation")
print("Result: ", response['result'])
print("-->")
response = set("so.avgreputation", "average(users.Reputation)")
print("Success: ", response['success'])
print("-->")
response = set("so.minreputation", "min(users.Reputation)")
print("Success: ", response['success'])
# 2. Displaying data about our users
print("-->")
response = get("so.maxreputation")
print("Result: ", response['result'])
print("-->")
response = get("so.users.DisplayName")
print("Result: ", response['result'])
print("-->")
response = describe("so.maxreputation")
print("Result: ", response['result'])
# 3. Are users grumpy?! [Adding a property to each document in a collection]
print("-->")
response = set("so.users.grumpiness", "UpVotes-DownVotes")
print("Success: ", response['success'])
print("-->")
response = get("so.users.grumpiness")
print("Result: ", response['result'])
print("-->")
response = set("so.avggrumpiness", "average(users.grumpiness)")
print("Success: ", response['success'])
print("-->")
response = get("so.avggrumpiness")
print("Result: ", response['result'])
# 4. Where are our users located? [Grouping and ordering]
print("-->")
response = set("so.bylocation", "group(users.by(Location))")
print("Success: ", response['success'])
print("-->")
response = set("so.bylocation.numusers", "count(users)")
print("Success: ", response['success'])
print("-->")
response = set("so.toplocations", "orderdesc(bylocation.by(numusers))")
print("Success: ", response['success'])
print("-->")
response = get("so.toplocations")
print("Result: ", response['result'])
print("-->")
response = get("so.toplocations.numusers")
print("Result: ", response['result'])
# 5. Analyzing subsets of data
print("-->")
response = set("so.namedusers", "select(users.where(not(contains(DisplayName,\"user\"))))")
print("Success: ", response['success'])
print("-->")
response = set("so.anonusercount", "count(select(users.where(contains(DisplayName,\"user\"))))")
print("Success: ", response['success'])
print("-->")
response = get("so.anonusercount")
print("Result: ", response['result'])
# Final Step: disconnect from datasource
print("-->")
response = disconnect("so", "datasets.analytica.com", "43657", "MongoDB")
print("Success: ", response['success'])
