Example projects#

columnprinter.py#

This example prints the columns of a table one at a time.

#!/usr/bin/env python

import py1010
import sys

gateway = "http://www2.1010data.com/cgi-bin/gw"
tablename = "pub.demo.weather.stations"
if len(sys.argv) < 3:
    print("Usage: {} USERNAME PASSWORD".format(sys.argv[0]))
    exit(1)
password = sys.argv.pop()
username = sys.argv.pop()

try:
    with py1010.Session(gateway, username, password, py1010.POSSESS) as session:
        query = session.query(tablename, '<sel value="(elev>10)"/>')
        query.run()
        for col in query.cols:
            print("Column {}:".format(col.name))
            for x in col:
                print("\t{}".format(x))
except py1010.TentenException as e:
    print("Error: {}".format(e))

dictslice.py#

This example exports results to a pandas DataFrame.

import py1010
import sys
import pandas

gateway = "http://www2.1010data.com/cgi-bin/gw"
tablename = "pub.demo.weather.hourly90"
ops="""<base table="pub.demo.weather.hourly90" trs_timeline_="1"/>
<sel simple="1" value="(id=3103)" trs_timeline_="1"/>
<colord cols="date,hour,temp" trs_timeline_="1"/>
<sort cols="date,hour" trs_timeline_="1"/>
"""
if len(sys.argv) < 3:
    print("Usage: {} USERNAME PASSWORD".format(sys.argv[0]))
    exit(1)
password = sys.argv.pop()
username = sys.argv.pop()

try:
    with py1010.Session(gateway, username, password, py1010.POSSESS) as session:
        query=session.query(tablename, ops)
        query.run()
        df=pandas.DataFrame(query.dictslice(0,24*3))
        print(df)
except py1010.TentenException as e:
    print("Error: {}".format(e))

rowprinter.py#

This example prints the rows of a table one at a time.

#!/usr/bin/env python

import py1010
import sys

gateway = "http://www2.1010data.com/cgi-bin/gw"
tablename = "pub.demo.weather.stations"
if len(sys.argv) < 3:
    print("Usage: {} USERNAME PASSWORD".format(sys.argv[0]))
    exit(1)
password = sys.argv.pop()
username = sys.argv.pop()

try:
    with py1010.Session(gateway, username, password, py1010.POSSESS) as session:
        query = session.query(tablename, '<sel value="(elev>10)"/>')
        query.run()
        for r in query.rows:
            print("\t".join([str(x) for x in r]))
except py1010.TentenException as e:
    print("Error: {}".format(e))

server.py#

This example displays a complex web server example using multiple threads.

#!/usr/bin/env python
import py1010
import SocketServer
import BaseHTTPServer
import threading
import cgi
import sys

# Replace with your IP address if you want to access it from elsewhere.
server_address = ("127.0.0.1", 8000)
server_url = "http://127.0.0.1:8000/"

class ThreadingHTTPServer(SocketServer.ThreadingMixIn,
                          BaseHTTPServer.HTTPServer):
    pass

class TentenHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    formtext="""<form method="POST" action="/">
    <p>Table Path: <input type="text" name="table"/></p>
    <p>Query ops: <textarea name="ops"></textarea></p>
    <p>Row number to display: <input type="text" name="rowno" value="0"/></p>
    <p><input type="submit"/></p>
    """

    gateway = "http://www2.1010data.com/cgi-bin/gw"
    username = None
    groupname = None
    password = None

    @staticmethod
    def nicerow(row, index="?"):
        rv="Row[{}]:\n".format(index)
        for k,v in row.iteritems():
            rv += "\t{}:\t{}\n".format(k,v)
        print(rv)
        return rv

    def process_dblookup(self, table, ops, row):
        try:
            with py1010.Session(self.gateway, self.username,
                                self.password, py1010.POSSESS,
                                group=self.groupname) as session:
                print("Logged into group {}, user {}".format(self.groupname,
                                                             session.username))
                query=session.query(table, ops)
                query.run()
                res=self.nicerow(query.rowasdict(row), row)
                self.send_response(200)
                self.send_header("Content-type", "text/html")
                self.end_headers()
                self.wfile.write("""<h1>Query run</h1>
<p>Here is your data from userid {}:</p>
<pre>{}</pre>
<hr/>
{}""".format(session.username, cgi.escape(str(res)),
             self.formtext))
        except py1010.TentenException as e:
            self.send_response(500)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write("""<h1>Tenten Error</h1>
<p>Error from the 1010 side.  Here's the message:</p>
<pre>{}</pre>
<hr/>
<p><a href="{}"><i>Back</i></a></p>
""".format(cgi.escape(str(e)), server_url))
        except Exception as e:
            self.send_response(500)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write("""<h1>Error</h1>
<p>Seems to be an error not necessarily in 1010 functioning.  Here's the message:</p>
<pre>{}</pre>
<hr/>
<p><a href="{}"><i>Back</i></a></p>
""".format(cgi.escape(str(e)), server_url))


    def do_GET(self):
        print(self)
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(self.formtext)

    def do_POST(self):
        print(str(self.headers))
        indata=self.rfile.read(int(self.headers['Content-Length']))
        print('')
        data = cgi.parse_qs(indata, keep_blank_values=True)
        print(str(data))
        table = data['table'].pop()
        ops = data['ops'].pop().lstrip('-')
        rowno = int(data['rowno'].pop())
        self.process_dblookup(table, ops, rowno)

if len(sys.argv) < 4:
    print("Usage: {} USERNAME PASSWORD GROUPNAME".format(sys.argv[0]))
    exit(1)

TentenHandler.groupname = sys.argv.pop()
TentenHandler.password = sys.argv.pop()
TentenHandler.username = sys.argv.pop()

server = ThreadingHTTPServer(server_address, TentenHandler)
servthread = threading.Thread(target=server.serve_forever)
servthread.start()

simplethread.py#

This example uses two threads to create two session objects, retrieves information from two tables, and downloads the data into two separate files.

#!/usr/bin/env python

import threading
import py1010
import sys

gateway = "http://www2.1010data.com/cgi-bin/gw"
tablename = "pub.demo.weather.stations"
if len(sys.argv) < 4:
    print("Usage: {} OWNERNAME GROUPNAME PASSWORD".format(sys.argv[0]))
    exit(1)
password = sys.argv.pop()
groupname = sys.argv.pop()
username = sys.argv.pop()

def doIt(table, ops, outfilename):
    try:
        with py1010.Session(gateway, username, password, py1010.POSSESS,
                            group=groupname) as session:
            print("Logged in as {}".format(session.username))
            query = session.query(table, ops)
            query.run()
            with file(outfilename, "w") as outfile:
                print("Starting to write to "+outfilename)
                for r in query.rows:
                    outfile.write("\t".join([str(x) for x in r]) + "\n")
    except py1010.TentenException as e:
        print("Error: {}".format(e))

t1=threading.Thread(target=doIt,
                    args=("pub.public_data.census.acs.all_acs_processed_tables",
                          '<sel value="(i_<100000)"/>', "Output1.txt"))
t2=threading.Thread(target=doIt,
                    args=("pub.public_data.census.acs.reference.column_names",
                          '<sel value="(i_<100000)"/>', "Output2.txt"))
t1.start()
t2.start()

t1.join()
t2.join()