<commit_before>from __future__ import absolute_import, division, print_function

from collections import Iterator
from flask import Flask, request, jsonify, json
from functools import partial, wraps

from .index import parse_index

class Server(object):
    __slots__ = 'app', 'datasets'

    def __init__(self, name='Blaze-Server', datasets=None):
        app = self.app = Flask(name)
        self.datasets = datasets or dict()

        for args, kwargs, func in routes:
            func2 = wraps(func)(partial(func, self.datasets))
            app.route(*args, **kwargs)(func2)

    def __getitem__(self, key):
        return self.datasets[key]

    def __setitem__(self, key, value):
        self.datasets[key] = value
        return value


routes = list()

def route(*args, **kwargs):
    def f(func):
        routes.append((args, kwargs, func))
        return func
    return f


@route('/datasets.json')
def dataset(datasets):
    return jsonify(dict((k, str(v.dshape)) for k, v in datasets.items()))


@route('/data/<name>.json', methods=['POST'])
def data(datasets, name):
    """ Basic indexing API

    Allows remote indexing of datasets.  Takes indexing data as JSON

    Takes requests like
    Example
    -------

    For the following array:

    [['Alice', 100],
     ['Bob', 200],
     ['Charlie', 300]]

    schema = '{name: string, amount: int32}'

    And the following

    url: /data/table-name.json
    POST-data: {'index': [{'start': 0, 'step': 3}, 'name']}

    and returns responses like

    {"name": "table-name",
     "index": [0, "name"],
     "datashape": "3 * string",
     "data": ["Alice", "Bob", "Charlie"]}
     """

    if request.headers['content-type'] != 'application/json':
        return ("Expected JSON data", 404)
    try:
        data = json.loads(request.data)
    except ValueError:
        return ("Bad JSON.  Got %s " % request.data, 404)

    try:
        dset = datasets[name]
    except KeyError:
        return ("Dataset %s not found" % name, 404)

    try:
        index = parse_index(data['index'])
    except ValueError:
        return ("Bad index", 404)

    rv = dset.py.__getitem__(index)
    if isinstance(rv, Iterator):
        rv = list(rv)

    return jsonify({'name': name,
                    'index': data['index'],
                    'datashape': str(dset.dshape.subshape.__getitem__(index)),
                    'data': rv})
<commit_msg>serve: replace getitem calls with []<commit_after>from __future__ import absolute_import, division, print_function

from collections import Iterator
from flask import Flask, request, jsonify, json
from functools import partial, wraps

from .index import parse_index

class Server(object):
    __slots__ = 'app', 'datasets'

    def __init__(self, name='Blaze-Server', datasets=None):
        app = self.app = Flask(name)
        self.datasets = datasets or dict()

        for args, kwargs, func in routes:
            func2 = wraps(func)(partial(func, self.datasets))
            app.route(*args, **kwargs)(func2)

    def __getitem__(self, key):
        return self.datasets[key]

    def __setitem__(self, key, value):
        self.datasets[key] = value
        return value


routes = list()

def route(*args, **kwargs):
    def f(func):
        routes.append((args, kwargs, func))
        return func
    return f


@route('/datasets.json')
def dataset(datasets):
    return jsonify(dict((k, str(v.dshape)) for k, v in datasets.items()))


@route('/data/<name>.json', methods=['POST'])
def data(datasets, name):
    """ Basic indexing API

    Allows remote indexing of datasets.  Takes indexing data as JSON

    Takes requests like
    Example
    -------

    For the following array:

    [['Alice', 100],
     ['Bob', 200],
     ['Charlie', 300]]

    schema = '{name: string, amount: int32}'

    And the following

    url: /data/table-name.json
    POST-data: {'index': [{'start': 0, 'step': 3}, 'name']}

    and returns responses like

    {"name": "table-name",
     "index": [0, "name"],
     "datashape": "3 * string",
     "data": ["Alice", "Bob", "Charlie"]}
     """

    if request.headers['content-type'] != 'application/json':
        return ("Expected JSON data", 404)
    try:
        data = json.loads(request.data)
    except ValueError:
        return ("Bad JSON.  Got %s " % request.data, 404)

    try:
        dset = datasets[name]
    except KeyError:
        return ("Dataset %s not found" % name, 404)

    try:
        index = parse_index(data['index'])
    except ValueError:
        return ("Bad index", 404)

    rv = dset.py[index]
    if isinstance(rv, Iterator):
        rv = list(rv)

    return jsonify({'name': name,
                    'index': data['index'],
                    'datashape': str(dset.dshape.subshape[index]),
                    'data': rv})

{'fraction_non_alphanumeric': 0.14398504051527114, 'fraction_numerical': 0.012050696031581135, 'mean_word_length': 2.5925373134328358, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3670544', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1455, 'n_words': 431}