<commit_before>'''
Convertaion rules
=================

Can be converted into:

integer
-------

#. Any int or long value
#. Any suitable string/unicode
#. Boolean value

number
-------

#. Any float or int or long value
#. Any suitable string/unicode
#. Boolean value

string
------

#. Any suitable string/unicode
#. Any int or long value

boolean
-------

#. Boolean value
#. 0 or 1
#. '0' or '1'
#. u'0' or u'1'

array
-----

#. Any iterable value(collections.Iterable)

dictionary
----------

#. Any mapping value(collections.Mapping)

'''
from __future__ import absolute_import

import collections

import schematec.exc as exc
import schematec.abc as abc

class Integer(abc.Converter):
    def __call__(self, value):
        if value is None:
            raise exc.ConvertationError(value)

        if isinstance(value, bool):
            return int(value)

        if isinstance(value, (int, long)):
            return int(value)

        if isinstance(value, basestring):
            try:
                return int(value)
            except ValueError:
                raise exc.ConvertationError(value)

        raise exc.ConvertationError(value)

integer = Integer()

class Number(abc.Converter):
    def __call__(self, value):
        if value is None:
            raise exc.ConvertationError(value)

        if isinstance(value, bool):
            return float(value)

        if isinstance(value, (float, int, long)):
            return float(value)

        if isinstance(value, basestring):
            try:
                return float(value)
            except ValueError:
                raise exc.ConvertationError(value)

        raise exc.ConvertationError(value)

number = Number()

class String(abc.Converter):
    def __call__(self, value):
        if value is None:
            raise exc.ConvertationError(value)

        if isinstance(value, unicode):
            return value

        if isinstance(value, bool):
            raise exc.ConvertationError(value)

        if isinstance(value, (int, long)):
            return unicode(value)

        if isinstance(value, str):
            try:
                return unicode(value)
            except UnicodeDecodeError:
                raise exc.ConvertationError(value)

        raise exc.ConvertationError(value)

string = String()

class Boolean(abc.Converter):
    def __call__(self, value):
        if value is None:
            raise exc.ConvertationError(value)

        if isinstance(value, bool):
            return value

        if isinstance(value, (int, long)) and value in (0, 1):
            return bool(value)

        if isinstance(value, basestring) and value in (u'0', u'1'):
            return bool(int(value))

        raise exc.ConvertationError(value)

boolean = Boolean()

class Array(abc.Converter):
    TYPE = collections.Iterable

    def __call__(self, value):
        if isinstance(value, self.TYPE):
            return list(value)

        raise exc.ConvertationError(value)

array = Array()

class Dictionary(abc.Converter):
    TYPE = collections.Mapping

    def __call__(self, value):
        if isinstance(value, self.TYPE):
            return dict(value)

        raise exc.ConvertationError(value)

dictionary = Dictionary()
<commit_msg>drop type definition for converters<commit_after>'''
Convertaion rules
=================

Can be converted into:

integer
-------

#. Any int or long value
#. Any suitable string/unicode
#. Boolean value

number
-------

#. Any float or int or long value
#. Any suitable string/unicode
#. Boolean value

string
------

#. Any suitable string/unicode
#. Any int or long value

boolean
-------

#. Boolean value
#. 0 or 1
#. '0' or '1'
#. u'0' or u'1'

array
-----

#. Any iterable value(collections.Iterable)

dictionary
----------

#. Any mapping value(collections.Mapping)

'''
from __future__ import absolute_import

import collections

import schematec.exc as exc
import schematec.abc as abc

class Integer(abc.Converter):
    def __call__(self, value):
        if value is None:
            raise exc.ConvertationError(value)

        if isinstance(value, bool):
            return int(value)

        if isinstance(value, (int, long)):
            return int(value)

        if isinstance(value, basestring):
            try:
                return int(value)
            except ValueError:
                raise exc.ConvertationError(value)

        raise exc.ConvertationError(value)

integer = Integer()

class Number(abc.Converter):
    def __call__(self, value):
        if value is None:
            raise exc.ConvertationError(value)

        if isinstance(value, bool):
            return float(value)

        if isinstance(value, (float, int, long)):
            return float(value)

        if isinstance(value, basestring):
            try:
                return float(value)
            except ValueError:
                raise exc.ConvertationError(value)

        raise exc.ConvertationError(value)

number = Number()

class String(abc.Converter):
    def __call__(self, value):
        if value is None:
            raise exc.ConvertationError(value)

        if isinstance(value, unicode):
            return value

        if isinstance(value, bool):
            raise exc.ConvertationError(value)

        if isinstance(value, (int, long)):
            return unicode(value)

        if isinstance(value, str):
            try:
                return unicode(value)
            except UnicodeDecodeError:
                raise exc.ConvertationError(value)

        raise exc.ConvertationError(value)

string = String()

class Boolean(abc.Converter):
    def __call__(self, value):
        if value is None:
            raise exc.ConvertationError(value)

        if isinstance(value, bool):
            return value

        if isinstance(value, (int, long)) and value in (0, 1):
            return bool(value)

        if isinstance(value, basestring) and value in (u'0', u'1'):
            return bool(int(value))

        raise exc.ConvertationError(value)

boolean = Boolean()

class Array(abc.Converter):
    def __call__(self, value):
        if isinstance(value, collections.Iterable):
            return list(value)

        raise exc.ConvertationError(value)

array = Array()

class Dictionary(abc.Converter):
    def __call__(self, value):
        if isinstance(value, collections.Mapping):
            return dict(value)

        raise exc.ConvertationError(value)

dictionary = Dictionary()
<|endoftext|>


<commit_before>from flask import Flask
from flask import session
from flask import Response
from flask import request
from flask import redirect, url_for
from flask import render_template

from api.core import app, db
from api.models.user import User
from api.models.page import Page
from gensim import corpora, models, similarities

import urllib
import json
import datetime
import pprint

@app.route('/nlp/sentiment')
def nlp_sentiment():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json'
    }
    params = urllib.urlencode(params)

    sentiment = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetTextSentiment?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': sentiment
    }), mimetype='application/json')

@app.route('/nlp/rank_keywords')
def npl_rank_keywords():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json',
        'sentiment':  1
    }
    params = urllib.urlencode(params)

    ranked = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetRankedKeywords?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': ranked
    }), mimetype='application/json')

@app.route('/nlp/similar')
def nlp_similar():
    documents = [
        "Human machine interface for lab abc computer applications",
        "A survey of user opinion of computer system response time",
        "The EPS user interface management system",
        "System and human system engineering testing of EPS",
        "Relation of user perceived response time to error measurement",
        "The generation of random binary unordered trees",
        "The intersection graph of paths in trees",
        "Graph minors IV Widths of trees and well quasi ordering",
        "Graph minors A survey"]

    # remove common words and tokenize
    stoplist = set('for a of the and to in by from on with as a '.split())
    texts = [[word for word in document.lower().split() if word not in stoplist] for document in documents]

    # Create dictionary
    dictionary = corpora.Dictionary(texts)

    # Define corpus
    corpus = [dictionary.doc2bow(text) for text in texts]

    # Define LSI space
    lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=10)

    # Get similarity of teh doc vs documents
    doc = "Human computer interaction"
    vector = dictionary.doc2bow(doc.lower().split())
    vector_lsi = lsi[vector]

    index = similarities.MatrixSimilarity(lsi[corpus])
    sims = index[vector_lsi]

    return Response(json.dumps(sims), mimetype='application/json')<commit_msg>Update NLP thingie<commit_after>from flask import Flask
from flask import session
from flask import Response
from flask import request
from flask import redirect, url_for
from flask import render_template

from api.core import app, db
from api.models.user import User
from api.models.page import Page
from gensim import corpora, models, similarities

import urllib
import json
import datetime
import pprint

@app.route('/nlp/sentiment')
def nlp_sentiment():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json'
    }
    params = urllib.urlencode(params)

    sentiment = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetTextSentiment?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': sentiment
    }), mimetype='application/json')

@app.route('/nlp/rank_keywords')
def npl_rank_keywords():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json',
        'sentiment':  1
    }
    params = urllib.urlencode(params)

    ranked = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetRankedKeywords?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': ranked
    }), mimetype='application/json')

@app.route('/nlp/similar')
def nlp_similar():
    documents = [
        "This is not good at all"
        "Human machine interface for lab abc computer applications",
        "A survey of user opinion of computer system response time",
        "The EPS user interface management system",
        "System and human system engineering testing of EPS",
        "Relation of user perceived response time to error measurement",
        "The generation of random binary unordered trees",
        "The intersection graph of paths in trees",
        "Graph minors IV Widths of trees and well quasi ordering",
        "Graph minors A survey"]

    # remove common words and tokenize
    stoplist = set('for a of the and to in by from on with as a '.split())
    texts = [[word for word in document.lower().split() if word not in stoplist] for document in documents]

    # Create dictionary
    dictionary = corpora.Dictionary(texts)

    # Define corpus
    corpus = [dictionary.doc2bow(text) for text in texts]

    # Define LSI space
    lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=10)

    # Get similarity of teh doc vs documents
    doc = "Human computer interaction"
    vector = dictionary.doc2bow(doc.lower().split())
    vector_lsi = lsi[vector]

    index = similarities.MatrixSimilarity(lsi[corpus])
    sims = index[vector_lsi]

    return Response(json.dumps(sims), mimetype='application/json')<|endoftext|>


<commit_before>'''Class SigFig for repressenting digits of a measurement that
   convey meaningful inormation (i.e. are significant).
'''

from __future__ import absolute_import

from decimal import Decimal
from collections import defaultdict

from hlab.lexing import Lexer, LexicalError
from hlab.bases import AutoRepr

class SigFig(AutoRepr):

    def __init__(self, arg):
        if isinstance(arg, unicode):
            arg = str(arg)
        elif isinstance(arg, Decimal):
            sign, digits, exp = arg.as_tuple()
            arg = (sign, digits, exp - 1 + len(digits))
        if isinstance(arg, str):
            arg = parse_string(arg)
        elif isinstance(arg, (int,long)):
            arg = parse_string(str(arg))
        elif isinstance(arg, SigFig):
            arg = (arg.sign, arg.digits, arg.power)

        (self.sign,
         self.digits,
         self.power
         ) = arg

    def repr_args(self):
        return [str(self)]

    #legacy method
    def as_scientific(self):
        return self

    def as_decimal(self):
        return Decimal((self.sign, self.digits, self.least_significant_place))

    @property
    def sigfigs(self):
        if self.digits[0] != 0:
            return len(self.digits)
        return max(1, len(self.digits) - 1)

    @property
    def most_significant_place(self):
        return self.power

    @property
    def least_significant_place(self):
        return 1 + self.power - len(self.digits)

    def round_to_sigfigs(self, n):
        if n <= 0:
            raise ValueError("rounding %s to invalid number of sig figs %d" % (self, n))
        return self.round_at_index(n)

    def round_to_place(self, n):
        #print 'round to', n
        return self.round_at_index(self.power - n + 1)

    def round_at_index(self, i):
        #print 'round', self, self.sign, self.digits, self.power, 'at', i
        if i < 0:
            return self.__class__('0')

        if i >= len(self.digits):
            return self.shift_sigfigs(-(i - len(self.digits)))

        digits = list(self.digits)
        power = self.power

        if i==0:
            digits.insert(0, 0)
            power += 1
            i += 1

        round_dig = digits[i]

        if (round_dig > 5) or (round_dig == 5 and digits[i-1]%2):
            digits[i-1] += 1

        # carray
        for j in xrange(len(digits) - 1, 0, -1):
            if digits[j] == 10:
                digits[j] = 0
                digits[j-1] += 1

        digits = digits[:i:]

        if digits[0] == 10:
            digits[0] = 0
            digits.insert(0, 1)
            power += 1

        if digits[0] == 0:
            del digits[0]
            power -= 1

        if not digits:
            return SigFig('0')

        return self.__class__((self.sign, digits, power))

    def shift_sigfigs(self, sigfigs=1):
        if sigfigs == 0:
            return self

        if sigfigs > 0:
            digits = self.digits[:-sigfigs:]
            if not digits:
                return self.__class__(0)
        else:
            digits = self.digits + (0,) * -sigfigs

        return self.__class__((self.sign, digits, self.power))

    def __str__(self):
        base, exp = self.get_format_args()
        if exp is None:
            return base
        return '%se%s' % (base, exp)

    def get_format_args(self, min_exp_power=3):
        if self.digits[0] == 0:
            return self._get_zero_format_args()

        sigfigs = self.sigfigs
        power = self.power

        if min_exp_power!=None and abs(power) > max(sigfigs, min_exp_power):
            return self.get_exp_format_args()

        digs = map(str, self.digits)

        #deal with trailing zeros
        if sigfigs > power:
            insignificant_zeros = 0
            #prevet trailing decimal, i.e. 10.
            if power > 0 and len(digs) == power+1 and digs[-1] == '0':
                return self.get_exp_format_args()
        else:
            #check for significant trailing zeros
            if self.digits[-1::] == (0,):
                return self.get_exp_format_args()
            #add insignificant trailing zeros
            insignificant_zeros = (1 + power - sigfigs)
        digs += ['0'] * insignificant_zeros

        #special case to prevent decimal points in zero
        if digs == ['0']:
            return '0', None

        #place deicmal point
        if power >= 0:
            if not insignificant_zeros and power+1 < len(digs):
                digs.insert(1+power, '.')
        else:
            digs = ['0.'] + ['0'] * (-1 - power) + digs

        return ['%s%s' % ('-' if self.sign else '', ''.join(digs)),
                None]

    def _get_zero_format_args(self):
        assert set(self.digits) == set([0])
        if self.power > 0:
            return self.get_exp_format_args()
        assert self.digits == (0,)
        if self.power == 0:
            return '0', None
        return '0.' + '0' * -self.power, None

    def get_exp_format_args(self):
        return ['%s%d%s' % ('-' if self.sign else '',
                            self.digits[0],
                            '.' + ''.join(map(str, self.digits[1::])) if
                            len(self.digits) > 1 else ''),
                self.power]

    def __pos__(self):
        return self

    def __neg__(self):
        return self.__class__((1 if self.sign==0 else 0,
                               self.digits, self.power))

    def __nonzero__(self):
        return self.as_decimal() != 0

    def perform_binary_operation(self, other, func, rule):
        if not isinstance(other, (SigFig, Decimal, int, long)):
            return NotImplemented

        selfd = self.as_decimal()
        otherd = other.as_decimal() if isinstance(other, SigFig) else other
        valued = func(selfd, otherd)
        value = self.__class__(valued)

        sigfigs = (min(self.sigfigs, other.sigfigs)
                   if isinstance(other, SigFig) else
                   self.sigfigs)

        if rule=='mul':
            if valued == 0:
                value.digits = (0,) * sigfigs
            else:
                value = value.round_to_sigfigs(min(self.sigfigs, other.sigfigs)
                                               if isinstance(other, SigFig) else
                                               self.sigfigs)
        elif rule=='add':
            lsp = (max(self.least_significant_place, other.least_significant_place)
                   if isinstance(other, SigFig) else
                   self.least_significant_place)
            value = value.round_to_place(lsp)
#            if value.digits == (0,):
#                value.digits = (0,) * max(1, sigfigs)

        else:
            raise ValueError("bad sigfig rule %r" % (rule,))

        return value

    def __mul__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a*b, 'mul')
    def __rmul__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b*a, 'mul')
    def __div__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a/b, 'mul')
    def __rdiv__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b/a, 'mul')
    def __mod__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a%b, 'mul')
    def __rmod__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b%a, 'mul')
    __truediv__ = __div__
    __rtruediv__ = __rdiv__

    def __add__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a+b, 'add')
    def __radd__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b+a, 'add')
    def __sub__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a-b, 'add')
    def __rsub__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b-a, 'add')

    def __gt__(self, other):
        return (self-other).as_decimal() > 0
    def __ge__(self, other):
        return (self-other).as_decimal() >= 0
    def __eq__(self, other):
        if not isinstance(other, (int,long,Decimal,SigFig)):
            return NotImplemented
        try:
            return not (abs((self-other).as_decimal()) > 0)
        except TypeError,e:
            return False
    def __ne__(self, other):
        return not (self == other)
    def __le__(self, other):
        return (self-other).as_decimal() <= 0
    def __lt__(self, other):
        return (self-other).as_decimal() < 0

    def __pow__(self, op):
        if not isinstance(op, (int,long)):
            raise TypeError("pow not implemented for non-integer type %r" % (op,))
        if op < 0:
            return 1 / self**-op
        if op == 0:
            return self / self
        return (self.as_decimal() ** op).round_to_sigfigs(self.sigfigs)

    def sqrt(self):
        return self.__class__(self.as_decimal().sqrt()).round_to_sigfigs(self.sigfigs)

class SigFigBuilder(object):

    def __init__(self):
        self.digit_powers =  defaultdict(int)
        self.sign = 0

    def set_digit(self, power, digit):
        self.digit_powers[power] = digit

    def add_digit(self, power, digit):
        self.digit_powers[power] += digit
        e, self.digit_powers[power] = divmod(self.digit_powers[power], 10)
        if e:
            self.add_digit(power+1, e)

    def clear_upper_zeros(self):
        while self.digit_powers and self.digit_powers[max(self.digit_powers)] == 0:
            del self.digit_powers[max(self.digit_powers)]

    def fix_sign(self):
        swap_sign = self.digit_powers[max(self.digit_powers)] < 0
        if swap_sign:
            self.sign = 0 if self.sign else 1
            for i in self.digit_powers:
                self.digit_powers[i] *= -1

def parse_string(bytes):
    lex = Lexer(bytes.strip())
    [pm, digs_pre_dot, dot, digs_post_dot, exp, exp_power
     ] = lex.pulls(r'[+-]', r'\d+', r'\.', r'\d+', r'[eE]', r'[+-]?\d+')
    if not lex.eof or (exp and not exp_power):
        raise LexicalError("bad sigfig literal %r" % (bytes,))

    sign = 1 if pm == '-' else 0
    power = int(exp_power) if exp_power else 0
    digs_pre_dot = map(int, digs_pre_dot)
    digs_post_dot = map(int, digs_post_dot)

    #remove insignificant trailing zeros
    if not dot:
        if set(digs_pre_dot) > set([0]):
            while digs_pre_dot and digs_pre_dot[-1] == 0:
                digs_pre_dot.pop(-1)
                power += 1

    if not digs_pre_dot:
        digs_pre_dot.append(0)

    #make scientific by shifting digits and power accordingly
    while len(digs_pre_dot) > 1:
        digs_post_dot.insert(0, digs_pre_dot.pop(-1))
        power += 1
    assert len(digs_pre_dot) == 1

    digits = digs_pre_dot + digs_post_dot

    #remove insignificant leading zeros
    while len(digits) > 1 and digits[0] == 0:
        digits.pop(0)
        power -= 1

    digits = tuple(digits)

    return sign, digits, power

#sf = SigFig('120')
#print sf.least_significant_place
#m = sf - 120
#print m
# print SigFig('0.00000') - 0

# sf = SigFig('22.534')
# print sf
# print sf.round_to_sigfigs(4)
# print SigFig(sf.as_decimal())
# print
# print SigFig('90.8513423') + SigFig('12.6')
# print SigFig('90.8513423') * SigFig('1.343e-8')

print SigFig('10') + SigFig('-5')
<commit_msg>Added input assertion safe guards in SigFig initializer<commit_after>'''Class SigFig for repressenting digits of a measurement that
   convey meaningful inormation (i.e. are significant).
'''

from __future__ import absolute_import

from decimal import Decimal
from collections import defaultdict

from hlab.lexing import Lexer, LexicalError
from hlab.bases import AutoRepr

valid_digits = tuple(range(10))

class SigFig(AutoRepr):

    def __init__(self, arg):
        if isinstance(arg, unicode):
            arg = str(arg)
        elif isinstance(arg, Decimal):
            sign, digits, exp = arg.as_tuple()
            arg = (sign, digits, exp - 1 + len(digits))
        if isinstance(arg, str):
            arg = parse_string(arg)
        elif isinstance(arg, (int,long)):
            arg = parse_string(str(arg))
        elif isinstance(arg, SigFig):
            arg = (arg.sign, arg.digits, arg.power)

        sign, digits, power = arg
        digits = tuple(digits)

        assert sign in (0,1)
        assert all(isinstance(digit, (int,long)) and digit in valid_digits
                   for digit in digits)
        assert isinstance(power, (int,long))

        self.sign = sign
        self.digits = digits
        self.power = power

    def repr_args(self):
        return [str(self)]

    #legacy method
    def as_scientific(self):
        return self

    def as_decimal(self):
        return Decimal((self.sign, self.digits, self.least_significant_place))

    @property
    def sigfigs(self):
        if self.digits[0] != 0:
            return len(self.digits)
        return max(1, len(self.digits) - 1)

    @property
    def most_significant_place(self):
        return self.power

    @property
    def least_significant_place(self):
        return 1 + self.power - len(self.digits)

    def round_to_sigfigs(self, n):
        if n <= 0:
            raise ValueError("rounding %s to invalid number of sig figs %d" % (self, n))
        return self.round_at_index(n)

    def round_to_place(self, n):
        #print 'round to', n
        return self.round_at_index(self.power - n + 1)

    def round_at_index(self, i):
        #print 'round', self, self.sign, self.digits, self.power, 'at', i
        if i < 0:
            return self.__class__('0')

        if i >= len(self.digits):
            return self.shift_sigfigs(-(i - len(self.digits)))

        digits = list(self.digits)
        power = self.power

        if i==0:
            digits.insert(0, 0)
            power += 1
            i += 1

        round_dig = digits[i]

        if (round_dig > 5) or (round_dig == 5 and digits[i-1]%2):
            digits[i-1] += 1

        # carray
        for j in xrange(len(digits) - 1, 0, -1):
            if digits[j] == 10:
                digits[j] = 0
                digits[j-1] += 1

        digits = digits[:i:]

        if digits[0] == 10:
            digits[0] = 0
            digits.insert(0, 1)
            power += 1

        if digits[0] == 0:
            del digits[0]
            power -= 1

        if not digits:
            return SigFig('0')

        return self.__class__((self.sign, digits, power))

    def shift_sigfigs(self, sigfigs=1):
        if sigfigs == 0:
            return self

        if sigfigs > 0:
            digits = self.digits[:-sigfigs:]
            if not digits:
                return self.__class__(0)
        else:
            digits = self.digits + (0,) * -sigfigs

        return self.__class__((self.sign, digits, self.power))

    def __str__(self):
        base, exp = self.get_format_args()
        if exp is None:
            return base
        return '%se%s' % (base, exp)

    def get_format_args(self, min_exp_power=3):
        if self.digits[0] == 0:
            return self._get_zero_format_args()

        sigfigs = self.sigfigs
        power = self.power

        if min_exp_power!=None and abs(power) > max(sigfigs, min_exp_power):
            return self.get_exp_format_args()

        digs = map(str, self.digits)

        #deal with trailing zeros
        if sigfigs > power:
            insignificant_zeros = 0
            #prevet trailing decimal, i.e. 10.
            if power > 0 and len(digs) == power+1 and digs[-1] == '0':
                return self.get_exp_format_args()
        else:
            #check for significant trailing zeros
            if self.digits[-1::] == (0,):
                return self.get_exp_format_args()
            #add insignificant trailing zeros
            insignificant_zeros = (1 + power - sigfigs)
        digs += ['0'] * insignificant_zeros

        #special case to prevent decimal points in zero
        if digs == ['0']:
            return '0', None

        #place deicmal point
        if power >= 0:
            if not insignificant_zeros and power+1 < len(digs):
                digs.insert(1+power, '.')
        else:
            digs = ['0.'] + ['0'] * (-1 - power) + digs

        return ['%s%s' % ('-' if self.sign else '', ''.join(digs)),
                None]

    def _get_zero_format_args(self):
        assert set(self.digits) == set([0])
        if self.power > 0:
            return self.get_exp_format_args()
        assert self.digits == (0,)
        if self.power == 0:
            return '0', None
        return '0.' + '0' * -self.power, None

    def get_exp_format_args(self):
        return ['%s%d%s' % ('-' if self.sign else '',
                            self.digits[0],
                            '.' + ''.join(map(str, self.digits[1::])) if
                            len(self.digits) > 1 else ''),
                self.power]

    def __pos__(self):
        return self

    def __neg__(self):
        return self.__class__((1 if self.sign==0 else 0,
                               self.digits, self.power))

    def __nonzero__(self):
        return self.as_decimal() != 0

    def perform_binary_operation(self, other, func, rule):
        if not isinstance(other, (SigFig, Decimal, int, long)):
            return NotImplemented

        selfd = self.as_decimal()
        otherd = other.as_decimal() if isinstance(other, SigFig) else other
        valued = func(selfd, otherd)
        value = self.__class__(valued)

        sigfigs = (min(self.sigfigs, other.sigfigs)
                   if isinstance(other, SigFig) else
                   self.sigfigs)

        if rule=='mul':
            if valued == 0:
                value.digits = (0,) * sigfigs
            else:
                value = value.round_to_sigfigs(min(self.sigfigs, other.sigfigs)
                                               if isinstance(other, SigFig) else
                                               self.sigfigs)
        elif rule=='add':
            lsp = (max(self.least_significant_place, other.least_significant_place)
                   if isinstance(other, SigFig) else
                   self.least_significant_place)
            value = value.round_to_place(lsp)
#            if value.digits == (0,):
#                value.digits = (0,) * max(1, sigfigs)

        else:
            raise ValueError("bad sigfig rule %r" % (rule,))

        return value

    def __mul__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a*b, 'mul')
    def __rmul__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b*a, 'mul')
    def __div__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a/b, 'mul')
    def __rdiv__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b/a, 'mul')
    def __mod__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a%b, 'mul')
    def __rmod__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b%a, 'mul')
    __truediv__ = __div__
    __rtruediv__ = __rdiv__

    def __add__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a+b, 'add')
    def __radd__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b+a, 'add')
    def __sub__(self, other):
        return self.perform_binary_operation(other, lambda a,b: a-b, 'add')
    def __rsub__(self, other):
        return self.perform_binary_operation(other, lambda a,b: b-a, 'add')

    def __gt__(self, other):
        return (self-other).as_decimal() > 0
    def __ge__(self, other):
        return (self-other).as_decimal() >= 0
    def __eq__(self, other):
        if not isinstance(other, (int,long,Decimal,SigFig)):
            return NotImplemented
        try:
            return not (abs((self-other).as_decimal()) > 0)
        except TypeError,e:
            return False
    def __ne__(self, other):
        return not (self == other)
    def __le__(self, other):
        return (self-other).as_decimal() <= 0
    def __lt__(self, other):
        return (self-other).as_decimal() < 0

    def __pow__(self, op):
        if not isinstance(op, (int,long)):
            raise TypeError("pow not implemented for non-integer type %r" % (op,))
        if op < 0:
            return 1 / self**-op
        if op == 0:
            return self / self
        return (self.as_decimal() ** op).round_to_sigfigs(self.sigfigs)

    def sqrt(self):
        return self.__class__(self.as_decimal().sqrt()).round_to_sigfigs(self.sigfigs)

class SigFigBuilder(object):

    def __init__(self):
        self.digit_powers =  defaultdict(int)
        self.sign = 0

    def set_digit(self, power, digit):
        self.digit_powers[power] = digit

    def add_digit(self, power, digit):
        self.digit_powers[power] += digit
        e, self.digit_powers[power] = divmod(self.digit_powers[power], 10)
        if e:
            self.add_digit(power+1, e)

    def clear_upper_zeros(self):
        while self.digit_powers and self.digit_powers[max(self.digit_powers)] == 0:
            del self.digit_powers[max(self.digit_powers)]

    def fix_sign(self):
        swap_sign = self.digit_powers[max(self.digit_powers)] < 0
        if swap_sign:
            self.sign = 0 if self.sign else 1
            for i in self.digit_powers:
                self.digit_powers[i] *= -1

def parse_string(bytes):
    lex = Lexer(bytes.strip())
    [pm, digs_pre_dot, dot, digs_post_dot, exp, exp_power
     ] = lex.pulls(r'[+-]', r'\d+', r'\.', r'\d+', r'[eE]', r'[+-]?\d+')
    if not lex.eof or (exp and not exp_power):
        raise LexicalError("bad sigfig literal %r" % (bytes,))

    sign = 1 if pm == '-' else 0
    power = int(exp_power) if exp_power else 0
    digs_pre_dot = map(int, digs_pre_dot)
    digs_post_dot = map(int, digs_post_dot)

    #remove insignificant trailing zeros
    if not dot:
        if set(digs_pre_dot) > set([0]):
            while digs_pre_dot and digs_pre_dot[-1] == 0:
                digs_pre_dot.pop(-1)
                power += 1

    if not digs_pre_dot:
        digs_pre_dot.append(0)

    #make scientific by shifting digits and power accordingly
    while len(digs_pre_dot) > 1:
        digs_post_dot.insert(0, digs_pre_dot.pop(-1))
        power += 1
    assert len(digs_pre_dot) == 1

    digits = digs_pre_dot + digs_post_dot

    #remove insignificant leading zeros
    while len(digits) > 1 and digits[0] == 0:
        digits.pop(0)
        power -= 1

    digits = tuple(digits)

    return sign, digits, power

#sf = SigFig('120')
#print sf.least_significant_place
#m = sf - 120
#print m
# print SigFig('0.00000') - 0

# sf = SigFig('22.534')
# print sf
# print sf.round_to_sigfigs(4)
# print SigFig(sf.as_decimal())
# print
# print SigFig('90.8513423') + SigFig('12.6')
# print SigFig('90.8513423') * SigFig('1.343e-8')

print SigFig('10') + SigFig('-5')
<|endoftext|>


<commit_before>from flask import Flask
from flask import session
from flask import Response
from flask import request
from flask import redirect, url_for
from flask import render_template

from api.core import app, db
from api.models.user import User
from api.models.page import Page
from gensim import corpora, models, similarities

import urllib
import json
import datetime

@app.route('/nlp/sentiment')
def nlp_sentiment():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json'
    }
    params = urllib.urlencode(params)

    sentiment = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetTextSentiment?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': sentiment
    }), mimetype='application/json')

@app.route('/nlp/rank_keywords')
def npl_rank_keywords():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json',
        'sentiment':  1
    }
    params = urllib.urlencode(params)

    ranked = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetRankedKeywords?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': ranked
    }), mimetype='application/json')

@app.route('/nlp/similar')
def nlp_similar():
    documents = [
        "Human machine interface for lab abc computer applications",
        "A survey of user opinion of computer system response time",
        "The EPS user interface management system",
        "System and human system engineering testing of EPS",
        "Relation of user perceived response time to error measurement",
        "The generation of random binary unordered trees",
        "The intersection graph of paths in trees",
        "Graph minors IV Widths of trees and well quasi ordering",
        "Graph minors A survey"]

    # remove common words and tokenize
    stoplist = set('for a of the and to in by from on with as a '.split())
    texts = [[word for word in document.lower().split() if word not in stoplist] for document in documents]

    # Create dictionary
    dictionary = corpora.Dictionary(texts)

    # Define corpus
    corpus = [dictionary.doc2bow(text) for text in texts]

    # Define LSI space
    lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=10)

    # Get similarity of teh doc vs documents
    doc = "Human computer interaction"
    vector = dictionary.doc2bow(doc.lower().split())
    vector_lsi = lsi[vector]

    index = similarities.MatrixSimilarity(lsi[corpus])
    sims = index[vector_lsi]

    return Response(sims, mimetype = 'application/json')
<commit_msg>Update NLP thingie<commit_after>from flask import Flask
from flask import session
from flask import Response
from flask import request
from flask import redirect, url_for
from flask import render_template

from api.core import app, db
from api.models.user import User
from api.models.page import Page
from gensim import corpora, models, similarities

import urllib
import json
import datetime

@app.route('/nlp/sentiment')
def nlp_sentiment():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json'
    }
    params = urllib.urlencode(params)

    sentiment = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetTextSentiment?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': sentiment
    }), mimetype='application/json')

@app.route('/nlp/rank_keywords')
def npl_rank_keywords():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json',
        'sentiment':  1
    }
    params = urllib.urlencode(params)

    ranked = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetRankedKeywords?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': ranked
    }), mimetype='application/json')

@app.route('/nlp/similar')
def nlp_similar():
    documents = [
        "Human machine interface for lab abc computer applications",
        "A survey of user opinion of computer system response time",
        "The EPS user interface management system",
        "System and human system engineering testing of EPS",
        "Relation of user perceived response time to error measurement",
        "The generation of random binary unordered trees",
        "The intersection graph of paths in trees",
        "Graph minors IV Widths of trees and well quasi ordering",
        "Graph minors A survey"]

    # remove common words and tokenize
    stoplist = set('for a of the and to in by from on with as a '.split())
    texts = [[word for word in document.lower().split() if word not in stoplist] for document in documents]

    # Create dictionary
    dictionary = corpora.Dictionary(texts)

    # Define corpus
    corpus = [dictionary.doc2bow(text) for text in texts]

    # Define LSI space
    lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=10)

    # Get similarity of teh doc vs documents
    doc = "Human computer interaction"
    vector = dictionary.doc2bow(doc.lower().split())
    vector_lsi = lsi[vector]

    index = similarities.MatrixSimilarity(lsi[corpus])
    sims = index[vector_lsi]

    return Response(type(sims), mimetype='application/json')
<|endoftext|>


<commit_before>from numerictypes import character, string, unicode_, obj2dtype, integer
from numeric import ndarray, broadcast, empty
import sys

# special sub-class for character arrays (string and unicode_)
# This adds equality testing and methods of str and unicode types
#  which operate on an element-by-element basis

class ndchararray(ndarray):
    def __new__(subtype, shape, itemlen=1, unicode=False, buffer=None,
                offset=0, strides=None, swap=0, fortran=0):

        if unicode:
            dtype = unicode_
        else:
            dtype = string
            swap = 0

        if buffer is None:
            self = ndarray.__new__(subtype, shape, dtype, itemlen, fortran=fortran)
        else:
            self = ndarray.__new__(subtype, shape, dtype, itemlen, buffer=buffer,
                                   offset=offset, strides=strides,
                                   swap=swap, fortran=fortran)
        return self

    def __reduce__(self):
        pass

    def _richcmpfunc(self, other, op):
        b = broadcast(self, other)
        result = empty(b.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(b):
            r1 = val[0].strip('\x00')
	    r2 = val[1]
	    res[k] = eval("r1 %s r2" % op, {'r1':r1,'r2':r2})
        return result

    # these should probably be moved to C
    def __eq__(self, other):
        return self._richcmpfunc(other, '==')

    def __ne__(self, other):
        return self._richcmpfunc(other, '!=')

    def __ge__(self, other):
        return self._richcmpfunc(other, '>=')

    def __le__(self, other):
        return self._richcmpfunc(other, '<=')

    def __gt__(self, other):
        return self._richcmpfunc(other, '>')

    def __lt__(self, other):
        return self._richcmpfunc(other, '<')

    def __add__(self, other):
        b = broadcast(self, other)
        arr = b.iters[1].base
        outitem = self.itemsize + arr.itemsize
        result = ndchararray(b.shape, outitem, self.dtype is unicode_)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] + val[1])
        return result

    def __radd__(self, other):
        b = broadcast(other, self)
        outitem = b.iters[0].base.itemsize + \
                  b.iters[1].base.itemsize
        result = ndchararray(b.shape, outitem, self.dtype is unicode_)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] + val[1])
        return result

    def __mul__(self, other):
        b = broadcast(self, other)
        arr = b.iters[1].base
        if not issubclass(arr.dtype, integer):
            raise ValueError, "Can only multiply by integers"
        outitem = b.iters[0].base.itemsize * arr.max()
        result = ndchararray(b.shape, outitem, self.dtype is unicode_)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = val[0]*val[1]
        return result

    def __rmul__(self, other):
        b = broadcast(self, other)
        arr = b.iters[1].base
        if not issubclass(arr.dtype, integer):
            raise ValueError, "Can only multiply by integers"
        outitem = b.iters[0].base.itemsize * arr.max()
        result = ndchararray(b.shape, outitem, self.dtype is unicode_)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = val[0]*val[1]
        return result

    def __mod__(self, other):
        b = broadcast(self, other)
        res = [None]*b.size
        maxsize = -1
        for k,val in enumerate(b):
            newval = val[0] % val[1]
            maxsize = max(len(newval), maxsize)
            res[k] = newval
        newarr = ndchararray(b.shape, maxsize, self.dtype is unicode_)
        nearr[:] = res
        return newarr

    def __rmod__(self, other):
        return NotImplemented

    def _generalmethod(self, name, myiter):
        res = [None]*myiter.size
        maxsize = -1
        for k, val in enumerate(myiter):
            newval = []
            for chk in val[1:]:
                if chk is None:
                    break
                newval.append(chk)
            newitem = getattr(val[0],name)(*newval)
            maxsize = max(len(newitem), maxsize)
            res[k] = newval
        newarr = ndchararray(myiter.shape, maxsize, self.dtype is unicode_)
        newarr[:] = res
        return newarr

    def _typedmethod(self, name, myiter, dtype):
        result = empty(myiter.shape, dtype=dtype)
        res = result.flat
        for k, val in enumerate(myiter):
            newval = []
            for chk in val[1:]:
                if chk is None:
                    break
                newval.append(chk)
            newitem = getattr(val[0],name)(*newval)
            res[k] = newval
        return res

    def _samemethod(self, name):
        result = self.copy()
        res = result.flat
        for k, val in enumerate(self.flat):
            res[k] = getattr(val, name)()
        return result

    def capitalize(self):
        return self._samemethod('capitalize')

    if sys.version[:3] >= '2.4':
        def center(self, width, fillchar=' '):
            return self._generalmethod('center', broadcast(self, width, fillchar))
        def ljust(self, width, fillchar=' '):
            return self._generalmethod('ljust', broadcast(self, width, fillchar))
        def rjust(self, width, fillchar=' '):
            return self._generalmethod('rjust', broadcast(self, width, fillchar))
        def rsplit(self, sep=None, maxsplit=None):
            return self._generalmethod2('rsplit', broadcast(self, sep, maxsplit))
    else:
        def ljust(self, width):
            return self._generalmethod('ljust', broadcast(self, width))
        def rjust(self, width):
            return self._generalmethod('rjust', broadcast(self, width))
        def center(self, width):
            return self._generalmethod('center', broadcast(self, width))

    def count(self, sub, start=None, end=None):
        return self._typedmethod('count', broadcast(self, sub, start, end), int)

    def decode(self,encoding=None,errors=None):
        return self._generalmethod('decode', broadcast(self, encoding, errors))

    def encode(self,encoding=None,errors=None):
        return self._generalmethod('encode', broadcast(self, encoding, errors))

    def endswith(self, suffix, start=None, end=None):
        return self._typedmethod('endswith', broadcast(self, suffix, start, end), bool)

    def expandtabs(self, tabsize=None):
        return self._generalmethod('endswith', broadcast(self, tabsize))

    def find(self, sub, start=None, end=None):
        return self._typedmethod('find', broadcast(self, sub, start, end), int)

    def index(self, sub, start=None, end=None):
        return self._typedmethod('index', broadcast(self, sub, start, end), int)

    def _ismethod(self, name):
        result = empty(self.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(self.flat):
            res[k] = getattr(val, name)()
        return result

    def isalnum(self):
        return self._ismethod('isalnum')

    def isalpha(self):
        return self._ismethod('isalpha')

    def isdigit(self):
        return self._ismethod('isdigit')

    def islower(self):
        return self._ismethod('islower')

    def isspace(self):
        return self._ismethod('isspace')

    def istitle(self):
        return self._ismethod('istitle')

    def isupper(self):
        return self._ismethod('isupper')

    def join(self, seq):
        return self._generalmethod('join', broadcast(self, seq))

    def lower(self):
        return self._samemethod('lower')

    def lstrip(self, chars):
        return self._generalmethod('lstrip', broadcast(self, chars))

    def replace(self, old, new, count=None):
        return self._generalmethod('replace', broadcast(self, old, new, count))

    def rfind(self, sub, start=None, end=None):
        return self._typedmethod('rfind', broadcast(self, sub, start, end), int)

    def rindex(self, sub, start=None, end=None):
        return self._typedmethod('rindex', broadcast(self, sub, start, end), int)

    def rstrip(self, chars=None):
        return self._generalmethod('rstrip', broadcast(self, chars))

    def split(self, sep=None, maxsplit=None):
        return self._typedmethod('split', broadcast(self, sep, maxsplit), object)

    def splitlines(self, keepends=None):
        return self._typedmethod('splitlines', broadcast(self, keepends), object)

    def startswith(self, prefix, start=None, end=None):
        return self._typedmethod('startswith', broadcast(self, prefix, start, end), bool)

    def strip(self, chars=None):
        return self._generalmethod('strip', broadcast(self, chars))

    def swapcase(self):
        return self._samemethod('swapcase')

    def title(self):
        return self._samemethod('title')

    #deletechars not accepted for unicode objects
    def translate(self, table, deletechars=None):
        if self.dtype is unicode_:
            return self._generalmethod('translate', broadcast(self, table))
        else:
            return self._generalmethod('translate', broadcast(self, table, deletechars))

    def upper(self):
        return self._samemethod('upper')

    def zfill(self, width):
        return self._generalmethod('zfill', broadcast(self, width))

def chararray(obj, itemlen=7, copy=True, unicode=False, fortran=False):

    if isinstance(obj, charndarray):
        if copy or (itemlen != obj.itemlen) \
           or (not unicode and obj.dtype == unicode_) \
           or (unicode and obj.dtype == string):
            return obj.astype(obj.dtypestr[1:])
        else:
            return obj

    if isinstance(obj, ndarray) and (obj.dtype in [unicode_, string]):
        copied = 0

        if unicode:
            dtype = 'U%d' % obj.itemlen
            if obj.dtype == string:
                obj = obj.astype(dtype)
                copied = 1
        else:
            dtype = 'S%d' % obj.itemlen
            if obj.dtype == unicode_:
                obj = obj.astype(dtype)
                copied = 1

        if copy and not copied:
            obj = obj.copy()

        return ndarray.__new__(chararray, obj.shape)

    if unicode:
        dtype = "U%d" % itemlen
    else:
        dtype = "S%d" % itemlen

    val = asarray(obj).astype(dtype)

    return ndchararray(val.shape, itemlen, unicode, buffer=val,
                       strides=val.strides, fortran=fortran)

def aschararray(obj):
    return chararray(obj, copy=False)
<commit_msg>Improvements to chararray.<commit_after>from numerictypes import character, string, unicode_, \
     obj2dtype, integer, object_
from numeric import ndarray, broadcast, empty, array
import sys

# special sub-class for character arrays (string and unicode_)
# This adds equality testing and methods of str and unicode types
#  which operate on an element-by-element basis

class ndchararray(ndarray):
    def __new__(subtype, shape, itemlen=1, unicode=False, buffer=None,
                offset=0, strides=None, swap=0, fortran=0):

        if unicode:
            dtype = unicode_
        else:
            dtype = string
            swap = 0

        if buffer is None:
            self = ndarray.__new__(subtype, shape, dtype, itemlen,
                                   fortran=fortran)
        else:
            self = ndarray.__new__(subtype, shape, dtype, itemlen, buffer=buffer,
                                   offset=offset, strides=strides,
                                   swap=swap, fortran=fortran)
        return self

    def __reduce__(self):
        pass

    def _richcmpfunc(self, other, op):
        b = broadcast(self, other)
        result = empty(b.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(b):
            r1 = val[0].rstrip('\x00')
	    r2 = val[1]
	    res[k] = eval("r1 %s r2" % op, {'r1':r1,'r2':r2})
        return result

    # these should probably be moved to C
    def __eq__(self, other):
        return self._richcmpfunc(other, '==')

    def __ne__(self, other):
        return self._richcmpfunc(other, '!=')

    def __ge__(self, other):
        return self._richcmpfunc(other, '>=')

    def __le__(self, other):
        return self._richcmpfunc(other, '<=')

    def __gt__(self, other):
        return self._richcmpfunc(other, '>')

    def __lt__(self, other):
        return self._richcmpfunc(other, '<')

    def __add__(self, other):
        b = broadcast(self, other)
        arr = b.iters[1].base
        outitem = self.itemsize + arr.itemsize
        result = ndchararray(b.shape, outitem, self.dtype is unicode_)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] + val[1])
        return result

    def __radd__(self, other):
        b = broadcast(other, self)
        outitem = b.iters[0].base.itemsize + \
                  b.iters[1].base.itemsize
        result = ndchararray(b.shape, outitem, self.dtype is unicode_)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] + val[1])
        return result

    def __mul__(self, other):
        b = broadcast(self, other)
        arr = b.iters[1].base
        if not issubclass(arr.dtype, integer):
            raise ValueError, "Can only multiply by integers"
        outitem = b.iters[0].base.itemsize * arr.max()
        result = ndchararray(b.shape, outitem, self.dtype is unicode_)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = val[0]*val[1]
        return result

    def __rmul__(self, other):
        b = broadcast(self, other)
        arr = b.iters[1].base
        if not issubclass(arr.dtype, integer):
            raise ValueError, "Can only multiply by integers"
        outitem = b.iters[0].base.itemsize * arr.max()
        result = ndchararray(b.shape, outitem, self.dtype is unicode_)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = val[0]*val[1]
        return result

    def __mod__(self, other):
        b = broadcast(self, other)
        res = [None]*b.size
        maxsize = -1
        for k,val in enumerate(b):
            newval = val[0] % val[1]
            maxsize = max(len(newval), maxsize)
            res[k] = newval
        newarr = ndchararray(b.shape, maxsize, self.dtype is unicode_)
        nearr[:] = res
        return newarr

    def __rmod__(self, other):
        return NotImplemented

    def _generalmethod(self, name, myiter):
        res = [None]*myiter.size
        maxsize = -1
        for k, val in enumerate(myiter):
            newval = []
            for chk in val[1:]:
                if chk.dtype is object_ and chk.toscalar() is None:
                    break
                newval.append(chk)
            newitem = getattr(val[0],name)(*newval)
            maxsize = max(len(newitem), maxsize)
            res[k] = newitem
        newarr = ndchararray(myiter.shape, maxsize, self.dtype is unicode_)
	print res, maxsize
        newarr[:] = res
        return newarr

    def _typedmethod(self, name, myiter, dtype):
        result = empty(myiter.shape, dtype=dtype)
        res = result.flat
        for k, val in enumerate(myiter):
            newval = []
            for chk in val[1:]:
                if chk.dtype is object_ and chk.toscalar() is None:
                    break
                newval.append(chk)
            this_str = val[0].rstrip('\x00')
            newitem = getattr(this_str,name)(*newval)
            res[k] = newitem
        return result

    def _samemethod(self, name):
        result = self.copy()
        res = result.flat
        for k, val in enumerate(self.flat):
            res[k] = getattr(val, name)()
        return result

    def capitalize(self):
        return self._samemethod('capitalize')

    if sys.version[:3] >= '2.4':
        def center(self, width, fillchar=' '):
            return self._generalmethod('center',
                                       broadcast(self, width, fillchar))
        def ljust(self, width, fillchar=' '):
            return self._generalmethod('ljust',
                                       broadcast(self, width, fillchar))
        def rjust(self, width, fillchar=' '):
            return self._generalmethod('rjust',
                                       broadcast(self, width, fillchar))
        def rsplit(self, sep=None, maxsplit=None):
            return self._typedmethod('rsplit', broadcast(self, sep, maxsplit),
                                     object)
    else:
        def ljust(self, width):
            return self._generalmethod('ljust', broadcast(self, width))
        def rjust(self, width):
            return self._generalmethod('rjust', broadcast(self, width))
        def center(self, width):
            return self._generalmethod('center', broadcast(self, width))

    def count(self, sub, start=None, end=None):
        return self._typedmethod('count', broadcast(self, sub, start, end), int)

    def decode(self,encoding=None,errors=None):
        return self._generalmethod('decode', broadcast(self, encoding, errors))

    def encode(self,encoding=None,errors=None):
        return self._generalmethod('encode', broadcast(self, encoding, errors))

    def endswith(self, suffix, start=None, end=None):
        return self._typedmethod('endswith', broadcast(self, suffix, start, end), bool)

    def expandtabs(self, tabsize=None):
        return self._generalmethod('endswith', broadcast(self, tabsize))

    def find(self, sub, start=None, end=None):
        return self._typedmethod('find', broadcast(self, sub, start, end), int)

    def index(self, sub, start=None, end=None):
        return self._typedmethod('index', broadcast(self, sub, start, end), int)

    def _ismethod(self, name):
        result = empty(self.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(self.flat):
            item = val.rstrip('\x00')
            res[k] = getattr(item, name)()
        return result

    def isalnum(self):
        return self._ismethod('isalnum')

    def isalpha(self):
        return self._ismethod('isalpha')

    def isdigit(self):
        return self._ismethod('isdigit')

    def islower(self):
        return self._ismethod('islower')

    def isspace(self):
        return self._ismethod('isspace')

    def istitle(self):
        return self._ismethod('istitle')

    def isupper(self):
        return self._ismethod('isupper')

    def join(self, seq):
        return self._generalmethod('join', broadcast(self, seq))

    def lower(self):
        return self._samemethod('lower')

    def lstrip(self, chars):
        return self._generalmethod('lstrip', broadcast(self, chars))

    def replace(self, old, new, count=None):
        return self._generalmethod('replace', broadcast(self, old, new, count))

    def rfind(self, sub, start=None, end=None):
        return self._typedmethod('rfind', broadcast(self, sub, start, end), int)

    def rindex(self, sub, start=None, end=None):
        return self._typedmethod('rindex', broadcast(self, sub, start, end), int)

    def rstrip(self, chars=None):
        return self._generalmethod('rstrip', broadcast(self, chars))

    def split(self, sep=None, maxsplit=None):
        return self._typedmethod('split', broadcast(self, sep, maxsplit), object)

    def splitlines(self, keepends=None):
        return self._typedmethod('splitlines', broadcast(self, keepends), object)

    def startswith(self, prefix, start=None, end=None):
        return self._typedmethod('startswith', broadcast(self, prefix, start, end), bool)

    def strip(self, chars=None):
        return self._generalmethod('strip', broadcast(self, chars))

    def swapcase(self):
        return self._samemethod('swapcase')

    def title(self):
        return self._samemethod('title')

    def translate(self, table, deletechars=None):
        if self.dtype is unicode_:
            return self._generalmethod('translate', broadcast(self, table))
        else:
            return self._generalmethod('translate', broadcast(self, table, deletechars))

    def upper(self):
        return self._samemethod('upper')

    def zfill(self, width):
        return self._generalmethod('zfill', broadcast(self, width))

def chararray(obj, itemlen=7, copy=True, unicode=False, fortran=False):

    if isinstance(obj, ndchararray):
        if copy or (itemlen != obj.itemlen) \
           or (not unicode and obj.dtype == unicode_) \
           or (unicode and obj.dtype == string):
            return obj.astype("%s%d" % (obj.dtypechar, itemlen))
        else:
            return obj

    if isinstance(obj, ndarray) and (obj.dtype in [unicode_, string]):
        copied = 0
        if unicode:
            dtype = 'U%d' % obj.itemlen
            if obj.dtype == string:
                obj = obj.astype(dtype)
                copied = 1
        else:
            dtype = 'S%d' % obj.itemlen
            if obj.dtype == unicode_:
                obj = obj.astype(dtype)
                copied = 1

        if copy and not copied:
            obj = obj.copy()

        return ndarray.__new__(ndchararray, obj.shape, dtype, itemlen,
                               buffer=obj, offset=0, swap=obj.flags.swapped,
                               fortran=obj.flags['FNC'])

    if unicode:
        dtype = "U%d" % itemlen
    else:
        dtype = "S%d" % itemlen

    val = array(obj, dtype=dtype, fortran=fortran, subok=1)

    return ndchararray(val.shape, itemlen, unicode, buffer=val,
                       strides=val.strides, swap=val.flags.swapped,
                       fortran=fortran)

def aschararray(obj, itemlen=7, unicode=False, fortran=False):
    return chararray(obj, itemlen, copy=False,
                     unicode=unicode, fortran=fortran)
<|endoftext|>


<commit_before>from __future__ import division

import pyglet
import rabbyt

from ..image import load_all
from . import sprite
from . import glyph

CLEAR_COLOR_DEFAULT = (0.1, 0.3, 0.2)
WORLD_BOUNDS = (-24 * 16, -14 * 16, 24 * 16, 14 * 16)
VIEWPORT_ASPECT = 48 / 30
HUD_SIZE = 2 / 30

def get_viewport(win_width, win_height):
    win_aspect = win_width / win_height
    width, height = win_width, win_height
    if win_aspect < VIEWPORT_ASPECT:
        # window is too tall
        height *= win_aspect / VIEWPORT_ASPECT
    else:
        # window is too wide
        width /= win_aspect / VIEWPORT_ASPECT

    hud_height = height * HUD_SIZE

    return (
        (win_width - width) / 2, (win_height - height) / 2,
        (win_width + width) / 2, (win_height + height) / 2 - hud_height
    )

def init(world, options):

    window = pyglet.window.Window(
        fullscreen=options.fullscreen,
        vsync=options.vsync,
        resizable=not options.fullscreen,
        caption='Tanks',
    )

    rabbyt.set_default_attribs()

    draw_glyphs = glyph.init(window, world)
    draw_sprites = sprite.init(window, world, load_all('data/images'))

    @window.event
    def on_draw():
        rabbyt.clear(rgba=CLEAR_COLOR_DEFAULT)
        rabbyt.set_viewport(
            get_viewport(window.width, window.height),
            WORLD_BOUNDS
        )
        draw_glyphs()
        draw_sprites()

    return window

<commit_msg>one last replacement<commit_after>from __future__ import division

import pyglet
import rabbyt

from ..image import load_all
from . import sprite
from . import glyph

CLEAR_COLOR_DEFAULT = (0.1, 0.3, 0.2)
WORLD_BOUNDS = (-24 * 16, -14 * 16, 24 * 16, 14 * 16)
VIEWPORT_ASPECT = 48 / 30
HUD_SIZE = 2 / 30

def get_viewport(win_width, win_height):
    win_aspect = win_width / win_height
    width, height = win_width, win_height
    if win_aspect < VIEWPORT_ASPECT:
        # window is too tall
        height *= win_aspect / VIEWPORT_ASPECT
    else:
        # window is too wide
        width /= win_aspect / VIEWPORT_ASPECT

    hud_height = height * HUD_SIZE

    return (
        (win_width - width) / 2, (win_height - height) / 2,
        (win_width + width) / 2, (win_height + height) / 2 - hud_height
    )

def init(world, options):

    window = pyglet.window.Window(
        fullscreen=options.fullscreen,
        vsync=options.vsync,
        resizable=not options.fullscreen,
        caption='Zerkcom',
    )

    rabbyt.set_default_attribs()

    draw_glyphs = glyph.init(window, world)
    draw_sprites = sprite.init(window, world, load_all('data/images'))

    @window.event
    def on_draw():
        rabbyt.clear(rgba=CLEAR_COLOR_DEFAULT)
        rabbyt.set_viewport(
            get_viewport(window.width, window.height),
            WORLD_BOUNDS
        )
        draw_glyphs()
        draw_sprites()

    return window

<|endoftext|>


<commit_before># BEGIN_COPYRIGHT
#
# Copyright 2009-2016 CRS4.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# END_COPYRIGHT

import unittest
import itertools as it
from pydoop.mapreduce.streams import get_key_value_stream
from pydoop.mapreduce.streams import get_key_values_stream

STREAM_1_DATA = [
    ('mapItem', 'key1', 'val1'),
    ('mapItem', 'key2', 'val2'),
    ('mapItem', 'key3', 'val3'),
    ('close',),
    ('mapItem', 'key3', 'val3'),  # should not get here
]

STREAM_2_DATA = [
    ('reduceKey', 'key1'),
    ('reduceValue', 'val11'),
    ('reduceValue', 'val12'),
    ('reduceValue', 'val13'),
    ('reduceKey', 'key2'),
    ('reduceValue', 'val21'),
    ('reduceValue', 'val22'),
    ('reduceValue', 'val23'),
    ('close',),
    ('reduceValue', 'val24'),  # should not get here
]

key = None

def keyfunc(t):
    global key
    if t[0] == 'reduceKey':
        key = t[1]
    return key

def get_stream(data):
    for args in data:
        yield (args[0], tuple(args[1:]))

class TestStream(unittest.TestCase):

    def test_get_key_value_stream(self):
        stream = get_stream(STREAM_1_DATA)
        kv_stream = get_key_value_stream(stream)
        for ((k, v), (cmd, k1, v1)) in it.izip(kv_stream, STREAM_1_DATA):
            self.assertEqual(k, k1)
            self.assertEqual(v, v1)

    def test_get_key_values_stream(self):
        stream = get_stream(STREAM_2_DATA)
        kvs_stream = get_key_values_stream(stream, private_encoding=False)
        kvs2_stream = it.groupby(STREAM_2_DATA, keyfunc)

        for (k1, vals1), (k2, itx) in it.izip(kvs_stream, kvs2_stream):
            self.assertEqual(k1, k2)
            vals2 = it.imap(lambda t: t[1],
                            it.ifilter(lambda t: t[0] == 'reduceValue', itx))
            for v1, v2 in it.izip(vals1, vals2):
                self.assertEqual(v1, v2)

    def test_get_key_values_stream2(self):
        stream = get_stream(STREAM_2_DATA)
        kvs_stream = get_key_values_stream(stream, private_encoding=False)
        kvs2_stream = it.groupby(STREAM_2_DATA, keyfunc)
        for (k1, vals1), (k2, itx) in it.izip(kvs_stream, kvs2_stream):
            self.assertEqual(k1, k2)
            if k1 == 'key2':
                vals2 = it.imap(lambda t: t[1],
                                it.ifilter(lambda t: t[0] == 'reduceValue',
                                           itx))
                for v1, v2 in it.izip(vals1, vals2):
                    self.assertEqual(v1, v2)

def suite():
    suite_ = unittest.TestSuite()
    suite_.addTest(TestStream('test_get_key_value_stream'))
    suite_.addTest(TestStream('test_get_key_values_stream'))
    suite_.addTest(TestStream('test_get_key_values_stream2'))
    return suite_

if __name__ == '__main__':
    _RUNNER = unittest.TextTestRunner(verbosity=2)
    _RUNNER.run((suite()))
<commit_msg>py3.5-ized mapreduce/test_streams.py<commit_after># BEGIN_COPYRIGHT
#
# Copyright 2009-2016 CRS4.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# END_COPYRIGHT

import unittest
import itertools as it

import pydoop.mapreduce.streams as streams
from pydoop.mapreduce.streams import get_key_value_stream
from pydoop.mapreduce.streams import get_key_values_stream
from pydoop.utils.py3compat import czip, cmap, cfilter

STREAM_1_DATA = [
    (streams.MAP_ITEM, 'key1', 'val1'),
    (streams.MAP_ITEM, 'key2', 'val2'),
    (streams.MAP_ITEM, 'key3', 'val3'),
    (streams.CLOSE,),
    (streams.MAP_ITEM, 'key3', 'val3'),  # should not get here
]

STREAM_2_DATA = [
    (streams.REDUCE_KEY, 'key1'),
    (streams.REDUCE_VALUE, 'val11'),
    (streams.REDUCE_VALUE, 'val12'),
    (streams.REDUCE_VALUE, 'val13'),
    (streams.REDUCE_KEY, 'key2'),
    (streams.REDUCE_VALUE, 'val21'),
    (streams.REDUCE_VALUE, 'val22'),
    (streams.REDUCE_VALUE, 'val23'),
    (streams.CLOSE,),
    (streams.REDUCE_VALUE, 'val24'),  # should not get here
]

key = None

def keyfunc(t):
    global key
    if t[0] == streams.REDUCE_KEY:
        key = t[1]
    return key

def get_stream(data):
    for args in data:
        yield (args[0], tuple(args[1:]))

class TestStream(unittest.TestCase):

    def test_get_key_value_stream(self):
        stream = get_stream(STREAM_1_DATA)
        kv_stream = get_key_value_stream(stream)
        for ((k, v), (cmd, k1, v1)) in czip(kv_stream, STREAM_1_DATA):
            self.assertEqual(k, k1)
            self.assertEqual(v, v1)

    def test_get_key_values_stream(self):
        stream = get_stream(STREAM_2_DATA)
        kvs_stream = get_key_values_stream(stream, private_encoding=False)
        kvs2_stream = it.groupby(STREAM_2_DATA, keyfunc)

        for (k1, vals1), (k2, itx) in czip(kvs_stream, kvs2_stream):
            self.assertEqual(k1, k2)
            vals2 = cmap(lambda t: t[1],
                         cfilter(lambda t: t[0] == streams.REDUCE_VALUE,
                                 itx))
            for v1, v2 in czip(vals1, vals2):
                self.assertEqual(v1, v2)

    def test_get_key_values_stream2(self):
        stream = get_stream(STREAM_2_DATA)
        kvs_stream = get_key_values_stream(stream, private_encoding=False)
        kvs2_stream = it.groupby(STREAM_2_DATA, keyfunc)
        for (k1, vals1), (k2, itx) in czip(kvs_stream, kvs2_stream):
            self.assertEqual(k1, k2)
            if k1 == 'key2':
                vals2 = cmap(lambda t: t[1],
                             cfilter(lambda t: t[0] == streams.REDUCE_VALUE,
                                     itx))
                for v1, v2 in czip(vals1, vals2):
                    self.assertEqual(v1, v2)

def suite():
    suite_ = unittest.TestSuite()
    suite_.addTest(TestStream('test_get_key_value_stream'))
    suite_.addTest(TestStream('test_get_key_values_stream'))
    suite_.addTest(TestStream('test_get_key_values_stream2'))
    return suite_

if __name__ == '__main__':
    _RUNNER = unittest.TextTestRunner(verbosity=2)
    _RUNNER.run((suite()))
<|endoftext|>


<commit_before>from __future__ import absolute_import

from django.conf import settings
from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
from typing import List, Optional, Set, Text

from zerver.decorator import authenticated_json_post_view
from zerver.lib.actions import do_invite_users, do_refer_friend, \
    get_default_subs, internal_send_message
from zerver.lib.request import REQ, has_request_variables, JsonableError
from zerver.lib.response import json_success, json_error
from zerver.lib.streams import access_stream_by_name
from zerver.lib.validator import check_string, check_list
from zerver.models import PreregistrationUser, Stream, UserProfile

import re

@authenticated_json_post_view
@has_request_variables
def json_invite_users(request, user_profile,
                      invitee_emails_raw=REQ("invitee_emails"),
                      body=REQ("custom_body", default=None)):
    # type: (HttpRequest, UserProfile, str, Optional[str]) -> HttpResponse
    if not invitee_emails_raw:
        return json_error(_("You must specify at least one email address."))
    if body == '':
        body = None

    invitee_emails = get_invitee_emails_set(invitee_emails_raw)

    stream_names = request.POST.getlist('stream')
    if not stream_names:
        return json_error(_("You must specify at least one stream for invitees to join."))

    # We unconditionally sub you to the notifications stream if it
    # exists and is public.
    notifications_stream = user_profile.realm.notifications_stream  # type: Optional[Stream]
    if notifications_stream and not notifications_stream.invite_only:
        stream_names.append(notifications_stream.name)

    streams = [] # type: List[Stream]
    for stream_name in stream_names:
        try:
            (stream, recipient, sub) = access_stream_by_name(user_profile, stream_name)
        except JsonableError:
            return json_error(_("Stream does not exist: %s. No invites were sent.") % (stream_name,))
        streams.append(stream)

    ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams, body)

    if ret_error is not None:
        return json_error(data=error_data, msg=ret_error)
    else:
        return json_success()

def get_invitee_emails_set(invitee_emails_raw):
    # type: (str) -> Set[str]
    invitee_emails_list = set(re.split(r'[,\n]', invitee_emails_raw))
    invitee_emails = set()
    for email in invitee_emails_list:
        is_email_with_name = re.search(r'<(?P<email>.*)>', email)
        if is_email_with_name:
            email = is_email_with_name.group('email')
        invitee_emails.add(email.strip())
    return invitee_emails

@has_request_variables
def bulk_invite_users(request, user_profile,
                      invitee_emails_list=REQ('invitee_emails',
                                              validator=check_list(check_string))):
    # type: (HttpRequest, UserProfile, List[str]) -> HttpResponse
    invitee_emails = set(invitee_emails_list)
    streams = get_default_subs(user_profile)

    ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams)

    if ret_error is not None:
        return json_error(data=error_data, msg=ret_error)
    else:
        # Report bulk invites to internal Zulip.
        invited = PreregistrationUser.objects.filter(referred_by=user_profile)
        internal_message = "%s <`%s`> invited %d people to Zulip." % (
            user_profile.full_name, user_profile.email, invited.count())
        internal_send_message(user_profile.realm, settings.NEW_USER_BOT, "stream",
                              "signups", user_profile.realm.string_id, internal_message)
        return json_success()

@authenticated_json_post_view
@has_request_variables
def json_refer_friend(request, user_profile, email=REQ()):
    # type: (HttpRequest, UserProfile, str) -> HttpResponse
    if not email:
        return json_error(_("No email address specified"))
    if user_profile.invites_granted - user_profile.invites_used <= 0:
        return json_error(_("Insufficient invites"))

    do_refer_friend(user_profile, email)

    return json_success()
<commit_msg>pep8: Add compliance with rule E261 to zerver/views/invite.py.<commit_after>from __future__ import absolute_import

from django.conf import settings
from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
from typing import List, Optional, Set, Text

from zerver.decorator import authenticated_json_post_view
from zerver.lib.actions import do_invite_users, do_refer_friend, \
    get_default_subs, internal_send_message
from zerver.lib.request import REQ, has_request_variables, JsonableError
from zerver.lib.response import json_success, json_error
from zerver.lib.streams import access_stream_by_name
from zerver.lib.validator import check_string, check_list
from zerver.models import PreregistrationUser, Stream, UserProfile

import re

@authenticated_json_post_view
@has_request_variables
def json_invite_users(request, user_profile,
                      invitee_emails_raw=REQ("invitee_emails"),
                      body=REQ("custom_body", default=None)):
    # type: (HttpRequest, UserProfile, str, Optional[str]) -> HttpResponse
    if not invitee_emails_raw:
        return json_error(_("You must specify at least one email address."))
    if body == '':
        body = None

    invitee_emails = get_invitee_emails_set(invitee_emails_raw)

    stream_names = request.POST.getlist('stream')
    if not stream_names:
        return json_error(_("You must specify at least one stream for invitees to join."))

    # We unconditionally sub you to the notifications stream if it
    # exists and is public.
    notifications_stream = user_profile.realm.notifications_stream  # type: Optional[Stream]
    if notifications_stream and not notifications_stream.invite_only:
        stream_names.append(notifications_stream.name)

    streams = []  # type: List[Stream]
    for stream_name in stream_names:
        try:
            (stream, recipient, sub) = access_stream_by_name(user_profile, stream_name)
        except JsonableError:
            return json_error(_("Stream does not exist: %s. No invites were sent.") % (stream_name,))
        streams.append(stream)

    ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams, body)

    if ret_error is not None:
        return json_error(data=error_data, msg=ret_error)
    else:
        return json_success()

def get_invitee_emails_set(invitee_emails_raw):
    # type: (str) -> Set[str]
    invitee_emails_list = set(re.split(r'[,\n]', invitee_emails_raw))
    invitee_emails = set()
    for email in invitee_emails_list:
        is_email_with_name = re.search(r'<(?P<email>.*)>', email)
        if is_email_with_name:
            email = is_email_with_name.group('email')
        invitee_emails.add(email.strip())
    return invitee_emails

@has_request_variables
def bulk_invite_users(request, user_profile,
                      invitee_emails_list=REQ('invitee_emails',
                                              validator=check_list(check_string))):
    # type: (HttpRequest, UserProfile, List[str]) -> HttpResponse
    invitee_emails = set(invitee_emails_list)
    streams = get_default_subs(user_profile)

    ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams)

    if ret_error is not None:
        return json_error(data=error_data, msg=ret_error)
    else:
        # Report bulk invites to internal Zulip.
        invited = PreregistrationUser.objects.filter(referred_by=user_profile)
        internal_message = "%s <`%s`> invited %d people to Zulip." % (
            user_profile.full_name, user_profile.email, invited.count())
        internal_send_message(user_profile.realm, settings.NEW_USER_BOT, "stream",
                              "signups", user_profile.realm.string_id, internal_message)
        return json_success()

@authenticated_json_post_view
@has_request_variables
def json_refer_friend(request, user_profile, email=REQ()):
    # type: (HttpRequest, UserProfile, str) -> HttpResponse
    if not email:
        return json_error(_("No email address specified"))
    if user_profile.invites_granted - user_profile.invites_used <= 0:
        return json_error(_("Insufficient invites"))

    do_refer_friend(user_profile, email)

    return json_success()
<|endoftext|>