<commit_before># -*- coding: utf-8 -*-

import json
from datetime import datetime

import flask
from flask_socketio import emit
from future.utils import with_metaclass
from eventlet.event import Event


def json_conversion(obj):

    if isinstance(obj, datetime):
        return obj.isoformat()
    raise TypeError('Not sure how to serialize {} of type {}'.format(obj, type(obj)))


def jdumps(data):
    return json.dumps(data, default=json_conversion)


def make_event(event):

    @property
    def actualevent(self):
        name = event.__name__[3:]
        return '{uuid}#{event}'.format(uuid=self._uuid, event=name)

    # TODO fix this for python 2
    # actualevent.__doc__ = event.__doc__

    return actualevent


def is_event(attribute):
    return attribute.startswith('on_')

def make_command(command):

    def actualcommand(self, data):
        name = command.__name__[3:]
        signal = '{uuid}#{event}'.format(uuid=self._uuid, event=name)
        if flask.has_request_context():
            return emit(signal, jdumps(data))
        else:
            sio = flask.current_app.extensions['socketio']
            return sio.emit(signal, jdumps(data))

    actualcommand.__doc__ = command.__doc__

    return actualcommand


def is_command(attribute):
    return attribute.startswith('do_')

class _Maker(type):
    def __new__(cls, name, parents, dct):
        for k in dct:
            if is_event(k):
                dct[k] = make_event(dct[k])
            if is_command(k):
                dct[k] = make_command(dct[k])
        return super(_Maker, cls).__new__(cls, name, parents, dct)


class Component(with_metaclass(_Maker, object)):
    _NEXT_UUID = 0

    @classmethod
    def _next_uuid(cls):
        cls._NEXT_UUID += 1
        return cls._NEXT_UUID

    def __init__(self):
        # TODO wanted to put "self" instead of "Component"
        # was surprised that didn't work
        self._uuid = Component._next_uuid()
        super(Component, self).__init__()

    def get(self, block=True, timeout=None):
        event = Event()
        emit('{}#get'.format(self._uuid), callback=lambda x: event.send(x))
        return event.wait()<commit_msg>also check context when getting<commit_after># -*- coding: utf-8 -*-

import json
from datetime import datetime

import flask
from flask_socketio import emit
from future.utils import with_metaclass
from eventlet.event import Event


def json_conversion(obj):

    if isinstance(obj, datetime):
        return obj.isoformat()
    raise TypeError('Not sure how to serialize {} of type {}'.format(obj, type(obj)))


def jdumps(data):
    return json.dumps(data, default=json_conversion)


def make_event(event):

    @property
    def actualevent(self):
        name = event.__name__[3:]
        return '{uuid}#{event}'.format(uuid=self._uuid, event=name)

    # TODO fix this for python 2
    # actualevent.__doc__ = event.__doc__

    return actualevent


def is_event(attribute):
    return attribute.startswith('on_')

def make_command(command):

    def actualcommand(self, data):
        name = command.__name__[3:]
        signal = '{uuid}#{event}'.format(uuid=self._uuid, event=name)
        if flask.has_request_context():
            return emit(signal, jdumps(data))
        else:
            sio = flask.current_app.extensions['socketio']
            return sio.emit(signal, jdumps(data))

    actualcommand.__doc__ = command.__doc__

    return actualcommand


def is_command(attribute):
    return attribute.startswith('do_')

class _Maker(type):
    def __new__(cls, name, parents, dct):
        for k in dct:
            if is_event(k):
                dct[k] = make_event(dct[k])
            if is_command(k):
                dct[k] = make_command(dct[k])
        return super(_Maker, cls).__new__(cls, name, parents, dct)


class Component(with_metaclass(_Maker, object)):
    _NEXT_UUID = 0

    @classmethod
    def _next_uuid(cls):
        cls._NEXT_UUID += 1
        return cls._NEXT_UUID

    def __init__(self):
        # TODO wanted to put "self" instead of "Component"
        # was surprised that didn't work
        self._uuid = Component._next_uuid()
        super(Component, self).__init__()

    def get(self, block=True, timeout=None):
        event = Event()
        if flask.has_request_context():
            emit('{}#get'.format(self._uuid), callback=lambda x: event.send(x))
        else:
            sio = flask.current_app.extensions['socketio']
            sio.emit('{}#get'.format(self._uuid), callback=lambda x: event.send(x))
        return event.wait()
{'fraction_non_alphanumeric': 0.11648784760236479, 'fraction_numerical': 0.0026275454346398074, 'mean_word_length': 2.8646362098138747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '308108', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1389, 'n_words': 344}