<commit_before>"""
Django settings for authapi project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

import djcelery
djcelery.setup_loader()

# Celery config
BROKER_URL = "amqp://guest:guest@localhost:5672//"

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'zct2c=hlij$^0xu0i8o6c^phjc!=m)r(%h90th0yyx9r5dm))+'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # custom
    'api',
    'authmethods',
    'captcha',

    #3rd party
    'corsheaders',
    'djcelery',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'authapi.urls'

WSGI_APPLICATION = 'authapi.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# cors
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
        'localhost:9001',
)

ENABLE_CAPTCHA = True

AVAILABLE_PACKS = [
    {
        "id": 0,
        "name": "basic",
        "num_credits": 500,
        "price": {
            "quote": "250",
            "unit": "EURO"
        }
    },
    {
        "id": 1,
        "name": "premium",
        "num_credits": 5000,
        "price": {
            "quote": "2500",
            "unit": "EURO"
        }
    }
]

AVAILABLE_PAYMENT_METHODS = ["paypal"]

SMS_PROVIDER = "console"
MAX_AUTH_MSG_SIZE = {
  "sms": 120,
  "emails": 10000
}

SMS_BASE_TEMPLATE = "%s . Using Agora Voting"

EMAIL_BASE_TEMPLATE = "%s\n\n -- Agora Voting https://agoravoting.com"

SMS_AUTH_CODE_URL = "https://agoravoting.example.com/#/election/%(authid)s/auth_code"
EMAIL_AUTH_CODE_URL = "https://agoravoting.example.com/#/election/%(authid)s/auth_code/%(code)s"

# Auth api settings
from auth_settings import *

try:
    from custom_settings import *
except:
    pass
<commit_msg>fix celery-related unit tests<commit_after>"""
Django settings for authapi project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

import djcelery
djcelery.setup_loader()

# Celery config
BROKER_URL = "amqp://guest:guest@localhost:5672//"

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'zct2c=hlij$^0xu0i8o6c^phjc!=m)r(%h90th0yyx9r5dm))+'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # custom
    'api',
    'authmethods',
    'captcha',

    #3rd party
    'corsheaders',
    'djcelery',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

# change the test runner to the one provided by celery so that the tests that
# make use of celery work when ./manage.py test is executed
TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'

ROOT_URLCONF = 'authapi.urls'

WSGI_APPLICATION = 'authapi.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# cors
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
        'localhost:9001',
)

ENABLE_CAPTCHA = True

AVAILABLE_PACKS = [
    {
        "id": 0,
        "name": "basic",
        "num_credits": 500,
        "price": {
            "quote": "250",
            "unit": "EURO"
        }
    },
    {
        "id": 1,
        "name": "premium",
        "num_credits": 5000,
        "price": {
            "quote": "2500",
            "unit": "EURO"
        }
    }
]

AVAILABLE_PAYMENT_METHODS = ["paypal"]

SMS_PROVIDER = "console"
MAX_AUTH_MSG_SIZE = {
  "sms": 120,
  "emails": 10000
}

SMS_BASE_TEMPLATE = "%s . Using Agora Voting"

EMAIL_BASE_TEMPLATE = "%s\n\n -- Agora Voting https://agoravoting.com"

SMS_AUTH_CODE_URL = "https://agoravoting.example.com/#/election/%(authid)s/auth_code"
EMAIL_AUTH_CODE_URL = "https://agoravoting.example.com/#/election/%(authid)s/auth_code/%(code)s"

# Auth api settings
from auth_settings import *

try:
    from custom_settings import *
except:
    pass
<|endoftext|>


<commit_before># * coding: utf8
"""

"""
# This file is part of zasim. zasim is licensed under the BSD 3-clause license.
# See LICENSE.txt for details.

from random import randrange
from .bases import Computation
from .utils import elementary_digits_and_values, rule_nr_to_rule_arr

import new

import numpy as np

class ElementaryCellularAutomatonBase(Computation):
    """Infer a 'Gödel numbering' from the used `Neighbourhood` and
    create a computation that corresponds to the rule'th possible combination
    of values for the neighbourhood cells.

    This works with any number of dimensions."""

    rule = 0
    """The elementary cellular automaton rule to use.

    See :meth:`visit` for details on how it's used."""

    digits_and_values = []
    """This list stores a list of dictionaries that for each combination of
    values for the neighbourhood cells stores the 'result_value', too."""

    def __init__(self, rule=None, **kwargs):
        """Create the computation.

        Supply None as the rule to get a random one."""
        super(ElementaryCellularAutomatonBase, self).__init__(**kwargs)
        self.rule = rule

    def visit(self):
        """Get the rule'th cellular automaton for the given neighbourhood.

        First, find out, how many possible combinations there are.
        That's simply the nuber of cells in the neighbourhood as the exponent
        of the number of `possible_values`.
        Then, normalise the neighbourhood cells by sorting their positions
        first by X, then by Y axis.
        Finally, create code, that sums up all the values and looks up the
        target value from the rule lookup array.
        """
        super(ElementaryCellularAutomatonBase, self).visit()

        self.neigh = zip(self.code.neigh.offsets, self.code.neigh.names)
        self.digits = len(self.neigh)

        self.base = len(self.code.possible_values)
        # the numbers in the possible values list have to start at 0 and go
        # all the way up to base-1.
        assert self.code.possible_values == tuple(range(self.base))

        if self.rule is None:
            self.rule = randrange(0, self.base ** (self.base ** self.digits))

        if self.rule >= self.base ** (self.base ** self.digits):
            self.rule = self.rule % (self.base ** (self.base ** self.digits))

        compute_code = ["result = 0;"]
        compute_py = ["result = 0"]
        self.code.attrs.append("rule")

        for digit_num, (offset, name) in zip(range(len(self.neigh) - 1, -1, -1), self.neigh):
            code = "result += %s * %d" % (name, self.base ** digit_num)
            compute_code.append(code + ";")
            compute_py.append(code)

        compute_code.append("result = rule(result);")
        compute_py.append("result = self.target.rule[int(result)]")

        self.code.add_weave_code("compute", "\n".join(compute_code))
        self.code.add_py_code("compute", "\n".join(compute_py))

    def init_once(self):
        """Generate the rule lookup array and a pretty printer."""
        super(ElementaryCellularAutomatonBase, self).init_once()
        entries = self.base ** self.digits
        self.target.rule = np.zeros(entries, np.dtype("i"))
        rule = self.rule

        self.target.rule = np.array(rule_nr_to_rule_arr(rule, self.digits, self.base))

        # and now do some heavy work to generate a pretty-printer!
        bbox = self.code.neigh.bounding_box()
        offsets = self.code.neigh.offsets
        offset_to_name = dict(self.neigh)

        if len(bbox) == 1:
            h = 3
            y_offset = None
        elif len(bbox) == 2:
            h = bbox[1][1] - bbox[1][0] + 3
            y_offset = bbox[1][0]
        else:
            # for higher dimensions, just fall back to the dummy pretty-printer
            return
        protolines = [[] for i in range(h)]
        lines = [line[:] for line in protolines]
        w = bbox[0][1] + 1 - bbox[0][0]

        for y in range(h):
            for x in range(bbox[0][0], bbox[0][1] + 1):
                if h == 3 and (x,) in offsets and y == 0:
                    lines[y].append("%(" + offset_to_name[(x,)]
                               + ")d")
                elif h > 3 and (x, y + y_offset) in offsets:
                    lines[y].append("%(" + offset_to_name[(x, y + y_offset)]
                               + ")d")
                else:
                    lines[y].append(" ")
            lines[y] = "".join(lines[y]) + "  "

        lines[-1] = ("X".center(w) + "  ").replace("X", "%(result_value)d")

        template = [line[:] for line in lines]

        self.digits_and_values = \
                elementary_digits_and_values(self.code.neigh, self.base, self.target.rule)

        def pretty_printer(self):
            lines = [line[:] for line in protolines]
            for thedict in self.digits_and_values:
                for line, tmpl_line in zip(lines, template):
                    line.append(tmpl_line % thedict)

            return "\n".join(["".join(line) for line in lines])

        self.pretty_print = new.instancemethod(pretty_printer, self, self.__class__)

    def pretty_print(self):
        """This method is generated upon init_once and pretty-prints the rules
        that this elementary cellular automaton uses for local steps."""
        return ["pretty printer is available only after the StepFunc "
                "object has been put together. also: cannot pretty-print "
                "with neighbourhoods of more than two dimensions"]

    def build_name(self, parts):
        if self.rule <= 255:
            mingle = str
        elif self.rule > 0xffffff:
            def mingle(value):
                raw = hex(value)
                chunks, raw = [raw[0:6]], raw[6:]
                for idx in range(len(raw)/4):
                    chunks.append(raw[idx * 4:idx * 4 + 4])
                return " ".join(chunks)
        else:
            mingle = hex
        parts.append("calculating rule %s" % (mingle(self.rule)))

class CountBasedComputationBase(Computation):
    """This base class counts the amount of nonzero neighbours excluding the
    center cell and offers the result as a local variable called
    nonzerocount of type int.

    The name of the central neighbour will be provided as self.central_name.
    """

    def visit(self):
        """Generate code that calculates nonzerocount from all neighbourhood
        values."""
        super(CountBasedComputationBase, self).visit()
        names = list(self.code.neigh.names)
        offsets = self.code.neigh.offsets

        # kick out the center cell, if any.
        zero_offset = tuple([0] * len(offsets[0]))
        zero_position = offsets.index(zero_offset)
        if zero_position != -1:
            self.central_name = names.pop(zero_position)
        else:
            self.central_name = None

        self.code.add_weave_code("localvars", "int nonzerocount;")
        if self.code.possible_values == (0, 1):
            single_values = names
        else:
            single_values = ["int(%d != 0)" % name for name in names]
        code = "nonzerocount = %s" % (" + ".join(single_values))

        self.code.add_weave_code("compute", code + ";")
        self.code.add_py_code("compute", code)

class LifeCellularAutomatonBase(CountBasedComputationBase):
    """This computation base is useful for any game-of-life-like step function
    in which the number of ones in the neighbourhood of a cell are counted to
    decide wether to change a 0 into a 1 or the other way around."""

    def __init__(self, reproduce_min=3, reproduce_max=3,
                 stay_alive_min=2, stay_alive_max=3, **kwargs):
        """:param reproduce_min: The minimal number of alive cells needed to
                                 reproduce to this cell.
           :param reproduce_max: The maximal number of alive cells that still
                                 cause a reproduction.
           :param stay_alive_min: The minimal number of alive neighbours needed
                                  for a cell to survive.
           :param stay_alive_max: The maximal number of alive neighbours that
                                  still allow the cell to survive."""
        super(LifeCellularAutomatonBase, self).__init__(**kwargs)
        self.params = dict(reproduce_min = reproduce_min,
                reproduce_max = reproduce_max,
                stay_alive_min = stay_alive_min,
                stay_alive_max = stay_alive_max)

    def visit(self):
        """Generates the code that turns a 0 into a 1 if nonzerocount exceeds
        reproduce_min and doesn't exceed reproduce_max and turns a 1 into a 0
        if nonzerocount is lower than stay_alive_min or higher than
        stay_alive_max."""
        super(LifeCellularAutomatonBase, self).visit()
        assert self.central_name is not None, "Need a neighbourhood with a named zero offset"
        self.params.update(central_name=self.central_name)
        self.code.add_weave_code("compute",
                """
    result = %(central_name)s;
    if (%(central_name)s == 0) {
      if (nonzerocount >= %(reproduce_min)d && nonzerocount <= %(reproduce_max)d) {
        result = 1;
    }} else {
      if (nonzerocount < %(stay_alive_min)d || nonzerocount > %(stay_alive_max)d) {
        result = 0;
      }}""" % self.params)
        self.code.add_py_code("compute","""
            result = %(central_name)s
            if %(central_name)s == 0:
                if %(reproduce_min)d <= nonzerocount <= %(reproduce_max)d:
                  result = 1
            else:
                if not (%(stay_alive_min)d <= nonzerocount <= %(stay_alive_max)d):
                  result = 0""" % self.params)

    def build_name(self, parts):
        if self.params != dict(reproduce_min=3, reproduce_max=3,
                stay_alive_min=2, stay_alive_max=3, central_name=self.params["central_name"]):
            parts.append("calculating life - reproduce "\
                    "[%(reproduce_min)s:%(reproduce_max)s], "\
                    "stay alive [%(stay_alive_min)s: %(stay_alive_max)s]" % self.params)
        else:
            parts.append("calculating game of life")
<commit_msg>a computation to just paste in strings<commit_after># * coding: utf8
"""

"""
# This file is part of zasim. zasim is licensed under the BSD 3-clause license.
# See LICENSE.txt for details.

from random import randrange
from .bases import Computation
from .utils import elementary_digits_and_values, rule_nr_to_rule_arr
from .compatibility import no_weave_code, no_python_code

import new

import numpy as np

class ElementaryCellularAutomatonBase(Computation):
    """Infer a 'Gödel numbering' from the used `Neighbourhood` and
    create a computation that corresponds to the rule'th possible combination
    of values for the neighbourhood cells.

    This works with any number of dimensions."""

    rule = 0
    """The elementary cellular automaton rule to use.

    See :meth:`visit` for details on how it's used."""

    digits_and_values = []
    """This list stores a list of dictionaries that for each combination of
    values for the neighbourhood cells stores the 'result_value', too."""

    def __init__(self, rule=None, **kwargs):
        """Create the computation.

        Supply None as the rule to get a random one."""
        super(ElementaryCellularAutomatonBase, self).__init__(**kwargs)
        self.rule = rule

    def visit(self):
        """Get the rule'th cellular automaton for the given neighbourhood.

        First, find out, how many possible combinations there are.
        That's simply the nuber of cells in the neighbourhood as the exponent
        of the number of `possible_values`.
        Then, normalise the neighbourhood cells by sorting their positions
        first by X, then by Y axis.
        Finally, create code, that sums up all the values and looks up the
        target value from the rule lookup array.
        """
        super(ElementaryCellularAutomatonBase, self).visit()

        self.neigh = zip(self.code.neigh.offsets, self.code.neigh.names)
        self.digits = len(self.neigh)

        self.base = len(self.code.possible_values)
        # the numbers in the possible values list have to start at 0 and go
        # all the way up to base-1.
        assert self.code.possible_values == tuple(range(self.base))

        if self.rule is None:
            self.rule = randrange(0, self.base ** (self.base ** self.digits))

        if self.rule >= self.base ** (self.base ** self.digits):
            self.rule = self.rule % (self.base ** (self.base ** self.digits))

        compute_code = ["result = 0;"]
        compute_py = ["result = 0"]
        self.code.attrs.append("rule")

        for digit_num, (offset, name) in zip(range(len(self.neigh) - 1, -1, -1), self.neigh):
            code = "result += %s * %d" % (name, self.base ** digit_num)
            compute_code.append(code + ";")
            compute_py.append(code)

        compute_code.append("result = rule(result);")
        compute_py.append("result = self.target.rule[int(result)]")

        self.code.add_weave_code("compute", "\n".join(compute_code))
        self.code.add_py_code("compute", "\n".join(compute_py))

    def init_once(self):
        """Generate the rule lookup array and a pretty printer."""
        super(ElementaryCellularAutomatonBase, self).init_once()
        entries = self.base ** self.digits
        self.target.rule = np.zeros(entries, np.dtype("i"))
        rule = self.rule

        self.target.rule = np.array(rule_nr_to_rule_arr(rule, self.digits, self.base))

        # and now do some heavy work to generate a pretty-printer!
        bbox = self.code.neigh.bounding_box()
        offsets = self.code.neigh.offsets
        offset_to_name = dict(self.neigh)

        if len(bbox) == 1:
            h = 3
            y_offset = None
        elif len(bbox) == 2:
            h = bbox[1][1] - bbox[1][0] + 3
            y_offset = bbox[1][0]
        else:
            # for higher dimensions, just fall back to the dummy pretty-printer
            return
        protolines = [[] for i in range(h)]
        lines = [line[:] for line in protolines]
        w = bbox[0][1] + 1 - bbox[0][0]

        for y in range(h):
            for x in range(bbox[0][0], bbox[0][1] + 1):
                if h == 3 and (x,) in offsets and y == 0:
                    lines[y].append("%(" + offset_to_name[(x,)]
                               + ")d")
                elif h > 3 and (x, y + y_offset) in offsets:
                    lines[y].append("%(" + offset_to_name[(x, y + y_offset)]
                               + ")d")
                else:
                    lines[y].append(" ")
            lines[y] = "".join(lines[y]) + "  "

        lines[-1] = ("X".center(w) + "  ").replace("X", "%(result_value)d")

        template = [line[:] for line in lines]

        self.digits_and_values = \
                elementary_digits_and_values(self.code.neigh, self.base, self.target.rule)

        def pretty_printer(self):
            lines = [line[:] for line in protolines]
            for thedict in self.digits_and_values:
                for line, tmpl_line in zip(lines, template):
                    line.append(tmpl_line % thedict)

            return "\n".join(["".join(line) for line in lines])

        self.pretty_print = new.instancemethod(pretty_printer, self, self.__class__)

    def pretty_print(self):
        """This method is generated upon init_once and pretty-prints the rules
        that this elementary cellular automaton uses for local steps."""
        return ["pretty printer is available only after the StepFunc "
                "object has been put together. also: cannot pretty-print "
                "with neighbourhoods of more than two dimensions"]

    def build_name(self, parts):
        if self.rule <= 255:
            mingle = str
        elif self.rule > 0xffffff:
            def mingle(value):
                raw = hex(value)
                chunks, raw = [raw[0:6]], raw[6:]
                for idx in range(len(raw)/4):
                    chunks.append(raw[idx * 4:idx * 4 + 4])
                return " ".join(chunks)
        else:
            mingle = hex
        parts.append("calculating rule %s" % (mingle(self.rule)))

class CountBasedComputationBase(Computation):
    """This base class counts the amount of nonzero neighbours excluding the
    center cell and offers the result as a local variable called
    nonzerocount of type int.

    The name of the central neighbour will be provided as self.central_name.
    """

    def visit(self):
        """Generate code that calculates nonzerocount from all neighbourhood
        values."""
        super(CountBasedComputationBase, self).visit()
        names = list(self.code.neigh.names)
        offsets = self.code.neigh.offsets

        # kick out the center cell, if any.
        zero_offset = tuple([0] * len(offsets[0]))
        zero_position = offsets.index(zero_offset)
        if zero_position != -1:
            self.central_name = names.pop(zero_position)
        else:
            self.central_name = None

        self.code.add_weave_code("localvars", "int nonzerocount;")
        if self.code.possible_values == (0, 1):
            single_values = names
        else:
            single_values = ["int(%d != 0)" % name for name in names]
        code = "nonzerocount = %s" % (" + ".join(single_values))

        self.code.add_weave_code("compute", code + ";")
        self.code.add_py_code("compute", code)

class LifeCellularAutomatonBase(CountBasedComputationBase):
    """This computation base is useful for any game-of-life-like step function
    in which the number of ones in the neighbourhood of a cell are counted to
    decide wether to change a 0 into a 1 or the other way around."""

    def __init__(self, reproduce_min=3, reproduce_max=3,
                 stay_alive_min=2, stay_alive_max=3, **kwargs):
        """:param reproduce_min: The minimal number of alive cells needed to
                                 reproduce to this cell.
           :param reproduce_max: The maximal number of alive cells that still
                                 cause a reproduction.
           :param stay_alive_min: The minimal number of alive neighbours needed
                                  for a cell to survive.
           :param stay_alive_max: The maximal number of alive neighbours that
                                  still allow the cell to survive."""
        super(LifeCellularAutomatonBase, self).__init__(**kwargs)
        self.params = dict(reproduce_min = reproduce_min,
                reproduce_max = reproduce_max,
                stay_alive_min = stay_alive_min,
                stay_alive_max = stay_alive_max)

    def visit(self):
        """Generates the code that turns a 0 into a 1 if nonzerocount exceeds
        reproduce_min and doesn't exceed reproduce_max and turns a 1 into a 0
        if nonzerocount is lower than stay_alive_min or higher than
        stay_alive_max."""
        super(LifeCellularAutomatonBase, self).visit()
        assert self.central_name is not None, "Need a neighbourhood with a named zero offset"
        self.params.update(central_name=self.central_name)
        self.code.add_weave_code("compute",
                """
    result = %(central_name)s;
    if (%(central_name)s == 0) {
      if (nonzerocount >= %(reproduce_min)d && nonzerocount <= %(reproduce_max)d) {
        result = 1;
    }} else {
      if (nonzerocount < %(stay_alive_min)d || nonzerocount > %(stay_alive_max)d) {
        result = 0;
      }}""" % self.params)
        self.code.add_py_code("compute","""
            result = %(central_name)s
            if %(central_name)s == 0:
                if %(reproduce_min)d <= nonzerocount <= %(reproduce_max)d:
                  result = 1
            else:
                if not (%(stay_alive_min)d <= nonzerocount <= %(stay_alive_max)d):
                  result = 0""" % self.params)

    def build_name(self, parts):
        if self.params != dict(reproduce_min=3, reproduce_max=3,
                stay_alive_min=2, stay_alive_max=3, central_name=self.params["central_name"]):
            parts.append("calculating life - reproduce "\
                    "[%(reproduce_min)s:%(reproduce_max)s], "\
                    "stay alive [%(stay_alive_min)s: %(stay_alive_max)s]" % self.params)
        else:
            parts.append("calculating game of life")

class PasteComputation(Computation):
    def __init__(self, c_code=None, py_code=None, name=None):
        if c_code is None:
            self.provides_features.append(no_weave_code)
        self.c_code = c_code
        if py_code is None:
            self.provides_features.append(no_python_code)
        self.py_code = py_code

    def visit(self):
        if self.py_code is not None:
            self.code.add_py_code(self.py_code)
        if self.c_code is not None:
            self.code.add_weave_code(self.c_code)
<|endoftext|>


<commit_before># -----------------
# normal decorators
# -----------------

def decorator(func):
    def wrapper(*args):
        return func(1, *args)
    return wrapper

@decorator
def decorated(a,b):
    return a,b

exe = decorated(set, '')

#? set
exe[1]

#? int()
exe[0]

# more complicated with args/kwargs
def dec(func):
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

@dec
def fu(a, b, c, *args, **kwargs):
    return a, b, c, args, kwargs

exe = fu(list, c=set, b=3, d='')

#? list()
exe[0]
#? int()
exe[1]
#? set
exe[2]
#? []
exe[3][0]
#? str()
exe[4]['d']

exe = fu(list, set, 3, '', d='')

#? str()
exe[3][0]

# -----------------
# multiple decorators
# -----------------
def dec2(func2):
    def wrapper2(first_arg, *args2, **kwargs2):
        return func2(first_arg, *args2, **kwargs2)
    return wrapper2

@dec2
@dec
def fu2(a, b, c, *args, **kwargs):
    return a, b, c, args, kwargs

exe = fu2(list, c=set, b=3, d='str')

#? list()
exe[0]
#? int()
exe[1]
#? set
exe[2]
#? []
exe[3][0]
#? str()
exe[4]['d']

# -----------------
# Decorator is a class
# -----------------
class Decorator(object):
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        return self.func(1, *args, **kwargs)

@Decorator
def nothing(a,b,c):
    return a,b,c

#? int()
nothing("")[0]
#? str()
nothing("")[1]

@Decorator
def nothing(a,b,c):
    return a,b,c

class MethodDecoratorAsClass():
    class_var = 3
    @Decorator
    def func_without_self(arg, arg2):
        return arg, arg2

    @Decorator
    def func_with_self(self, arg):
        return self.class_var

#? int()
MethodDecoratorAsClass().func_without_self('')[0]
#? str()
MethodDecoratorAsClass().func_without_self('')[1]
#?
MethodDecoratorAsClass().func_with_self(1)

# -----------------
# not found decorators (are just ignored)
# -----------------
@not_found_decorator
def just_a_func():
    return 1

#? int()
just_a_func()

#? ['__closure__']
just_a_func.__closure__

class JustAClass:
    @not_found_decorator2
    def a(self):
        return 1

#? ['__closure__']
JustAClass().a.__closure__
#? int()
JustAClass().a()
#? ['__closure__']
JustAClass.a.__closure__
#? int()
JustAClass.a()

# -----------------
# illegal decorators
# -----------------

class DecoratorWithoutCall():
    def __init__(self, func):
        self.func = func

@DecoratorWithoutCall
def f():
    return 1

# cannot be resolved - should be ignored
@DecoratorWithoutCall(None)
def g():
    return 1

#?
f()
#? int()
g()

# -----------------
# method decorators
# -----------------

def dec(f):
    def wrapper(s):
        return f(s)
    return wrapper

class MethodDecorators():
    _class_var = 1
    def __init__(self):
        self._method_var = ''

    @dec
    def constant(self):
        return 1.0

    @dec
    def class_var(self):
        return self._class_var

    @dec
    def method_var(self):
        return self._method_var

#? float()
MethodDecorators().constant()
#? int()
MethodDecorators().class_var()
#? str()
MethodDecorators().method_var()

class Base():
    @not_existing
    def __init__(self):
        pass
    @not_existing
    def b(self):
        return ''
    @dec
    def c(self):
        return 1

class MethodDecoratorDoesntExist(Base):
    """#272 github: combination of method decorators and super()"""
    def a(self):
        #?
        super().__init__()
        #? str()
        super().b()
        #? int()
        super().c()
        #? float()
        self.d()

    @doesnt_exist
    def d(self):
        return 1.0

# -----------------
# others
# -----------------
def memoize(function):
        def wrapper(*args):
            if 1:
                pass
            else:
                rv = function(*args)
                return rv
        return wrapper

@memoize
def follow_statement(stmt):
    return stmt

# here we had problems with the else clause, because the parent was not right.
#? int()
follow_statement(1)

# -----------------
# class decorators
# -----------------

# class decorators should just be ignored
@should_ignore
class A():
    def ret(self):
        return 1

#? int()
A().ret()
<commit_msg>tests for init decorator problem, see #247<commit_after># -----------------
# normal decorators
# -----------------

def decorator(func):
    def wrapper(*args):
        return func(1, *args)
    return wrapper

@decorator
def decorated(a,b):
    return a,b

exe = decorated(set, '')

#? set
exe[1]

#? int()
exe[0]

# more complicated with args/kwargs
def dec(func):
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

@dec
def fu(a, b, c, *args, **kwargs):
    return a, b, c, args, kwargs

exe = fu(list, c=set, b=3, d='')

#? list()
exe[0]
#? int()
exe[1]
#? set
exe[2]
#? []
exe[3][0]
#? str()
exe[4]['d']

exe = fu(list, set, 3, '', d='')

#? str()
exe[3][0]

# -----------------
# multiple decorators
# -----------------
def dec2(func2):
    def wrapper2(first_arg, *args2, **kwargs2):
        return func2(first_arg, *args2, **kwargs2)
    return wrapper2

@dec2
@dec
def fu2(a, b, c, *args, **kwargs):
    return a, b, c, args, kwargs

exe = fu2(list, c=set, b=3, d='str')

#? list()
exe[0]
#? int()
exe[1]
#? set
exe[2]
#? []
exe[3][0]
#? str()
exe[4]['d']

# -----------------
# Decorator is a class
# -----------------
class Decorator(object):
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        return self.func(1, *args, **kwargs)

@Decorator
def nothing(a,b,c):
    return a,b,c

#? int()
nothing("")[0]
#? str()
nothing("")[1]

@Decorator
def nothing(a,b,c):
    return a,b,c

class MethodDecoratorAsClass():
    class_var = 3
    @Decorator
    def func_without_self(arg, arg2):
        return arg, arg2

    @Decorator
    def func_with_self(self, arg):
        return self.class_var

#? int()
MethodDecoratorAsClass().func_without_self('')[0]
#? str()
MethodDecoratorAsClass().func_without_self('')[1]
#?
MethodDecoratorAsClass().func_with_self(1)

class SelfVars():
    """Init decorator problem as an instance, #247"""
    @Decorator
    def __init__(self):
        """
        init decorators should be ignored when looking up variables in the
        class.
        """
        self.c = list

    @Decorator
    def shouldnt_expose_var(not_self):
        """
        Even though in real Python this shouldn't expose the variable, in this
        case Jedi exposes the variable, because these kind of decorators are
        normally descriptors, which SHOULD be exposed (at least 90%).
        """
        not_self.b = 1.0

    def other_method(self):
        #? float()
        self.b
        #? list
        self.c

# -----------------
# not found decorators (are just ignored)
# -----------------
@not_found_decorator
def just_a_func():
    return 1

#? int()
just_a_func()

#? ['__closure__']
just_a_func.__closure__

class JustAClass:
    @not_found_decorator2
    def a(self):
        return 1

#? ['__closure__']
JustAClass().a.__closure__
#? int()
JustAClass().a()
#? ['__closure__']
JustAClass.a.__closure__
#? int()
JustAClass.a()

# -----------------
# illegal decorators
# -----------------

class DecoratorWithoutCall():
    def __init__(self, func):
        self.func = func

@DecoratorWithoutCall
def f():
    return 1

# cannot be resolved - should be ignored
@DecoratorWithoutCall(None)
def g():
    return 1

#?
f()
#? int()
g()

# -----------------
# method decorators
# -----------------

def dec(f):
    def wrapper(s):
        return f(s)
    return wrapper

class MethodDecorators():
    _class_var = 1
    def __init__(self):
        self._method_var = ''

    @dec
    def constant(self):
        return 1.0

    @dec
    def class_var(self):
        return self._class_var

    @dec
    def method_var(self):
        return self._method_var

#? float()
MethodDecorators().constant()
#? int()
MethodDecorators().class_var()
#? str()
MethodDecorators().method_var()

class Base():
    @not_existing
    def __init__(self):
        pass
    @not_existing
    def b(self):
        return ''
    @dec
    def c(self):
        return 1

class MethodDecoratorDoesntExist(Base):
    """#272 github: combination of method decorators and super()"""
    def a(self):
        #?
        super().__init__()
        #? str()
        super().b()
        #? int()
        super().c()
        #? float()
        self.d()

    @doesnt_exist
    def d(self):
        return 1.0

# -----------------
# others
# -----------------
def memoize(function):
        def wrapper(*args):
            if 1:
                pass
            else:
                rv = function(*args)
                return rv
        return wrapper

@memoize
def follow_statement(stmt):
    return stmt

# here we had problems with the else clause, because the parent was not right.
#? int()
follow_statement(1)

# -----------------
# class decorators
# -----------------

# class decorators should just be ignored
@should_ignore
class A():
    def ret(self):
        return 1

#? int()
A().ret()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import vim
import os
import os.path
from leaderf.utils import *
from leaderf.explorer import *
from leaderf.manager import *
from leaderf.mru import *

#*****************************************************
# MruExplorer
#*****************************************************
class MruExplorer(Explorer):
    @showRelativePath
    def getContent(self, *args, **kwargs):
        with lfOpen(mru.getCacheFileName(), 'r+', errors = 'ignore') as f:
            lines = f.readlines()
            lines[:] = [name for name in lines if os.path.exists(name.rstrip())]
            f.seek(0)
            f.truncate(0)
            f.writelines(lines)
            return lines

    def acceptSelection(self, *args, **kwargs):
        if len(args) == 0:
            return
        file = args[0]
        vim.command("hide edit %s" % escSpecial(file))

    def getStlFunction(self):
        return 'Mru'

    def getStlCurDir(self):
        return escQuote(lfEncode(os.getcwd()))

    def supportsMulti(self):
        return True

    def supportsNameOnly(self):
        return True

    def delFromCache(self, name):
        with lfOpen(mru.getCacheFileName(), 'r+', errors = 'ignore') as f:
            lines = f.readlines()
            lines.remove(name + '\n')
            f.seek(0)
            f.truncate(0)
            f.writelines(lines)

#*****************************************************
# MruExplManager
#*****************************************************
class MruExplManager(Manager):
    def _getExplClass(self):
        return MruExplorer

    def _defineMaps(self):
        vim.command("call g:LfMruExplMaps()")

    def _createHelp(self):
        help = []
        help.append('" <CR>/<double-click>/o : open file under cursor')
        help.append('" x : open file under cursor in a horizontally split window')
        help.append('" v : open file under cursor in a vertically split window')
        help.append('" t : open file under cursor in a new tabpage')
        help.append('" d : remove from mru list')
        help.append('" i : switch to input mode')
        help.append('" s : select multiple files')
        help.append('" a : select all files')
        help.append('" c : clear all selections')
        help.append('" q : quit')
        help.append('" <F1> : toggle this help')
        help.append('" ---------------------------------------------------------')
        return help

    def deleteMru(self):
        if vim.current.window.cursor[0] <= self._help_length:
            return
        vim.command("setlocal modifiable")
        line = vim.current.line
        self._explorer.delFromCache(line)
        if len(self._content) > 0:
            self._content.remove(line + '\n')
        del vim.current.line
        vim.command("setlocal nomodifiable")

#*****************************************************
# mruExplManager is a singleton
#*****************************************************
mruExplManager = MruExplManager()

__all__ = ['mruExplManager']
<commit_msg>fix an issue about multibyte when invoke "LeaderfMru"<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import vim
import os
import os.path
from leaderf.utils import *
from leaderf.explorer import *
from leaderf.manager import *
from leaderf.mru import *

#*****************************************************
# MruExplorer
#*****************************************************
class MruExplorer(Explorer):
    @showRelativePath
    def getContent(self, *args, **kwargs):
        with lfOpen(mru.getCacheFileName(), 'r+', errors = 'ignore') as f:
            lines = f.readlines()
            lines = [name for name in lines if os.path.exists(lfDecode(name.rstrip()))]
            f.seek(0)
            f.truncate(0)
            f.writelines(lines)
            return lines

    def acceptSelection(self, *args, **kwargs):
        if len(args) == 0:
            return
        file = args[0]
        vim.command("hide edit %s" % escSpecial(file))

    def getStlFunction(self):
        return 'Mru'

    def getStlCurDir(self):
        return escQuote(lfEncode(os.getcwd()))

    def supportsMulti(self):
        return True

    def supportsNameOnly(self):
        return True

    def delFromCache(self, name):
        with lfOpen(mru.getCacheFileName(), 'r+', errors = 'ignore') as f:
            lines = f.readlines()
            lines.remove(name + '\n')
            f.seek(0)
            f.truncate(0)
            f.writelines(lines)

#*****************************************************
# MruExplManager
#*****************************************************
class MruExplManager(Manager):
    def _getExplClass(self):
        return MruExplorer

    def _defineMaps(self):
        vim.command("call g:LfMruExplMaps()")

    def _createHelp(self):
        help = []
        help.append('" <CR>/<double-click>/o : open file under cursor')
        help.append('" x : open file under cursor in a horizontally split window')
        help.append('" v : open file under cursor in a vertically split window')
        help.append('" t : open file under cursor in a new tabpage')
        help.append('" d : remove from mru list')
        help.append('" i : switch to input mode')
        help.append('" s : select multiple files')
        help.append('" a : select all files')
        help.append('" c : clear all selections')
        help.append('" q : quit')
        help.append('" <F1> : toggle this help')
        help.append('" ---------------------------------------------------------')
        return help

    def deleteMru(self):
        if vim.current.window.cursor[0] <= self._help_length:
            return
        vim.command("setlocal modifiable")
        line = vim.current.line
        self._explorer.delFromCache(line)
        if len(self._content) > 0:
            self._content.remove(line + '\n')
        del vim.current.line
        vim.command("setlocal nomodifiable")

#*****************************************************
# mruExplManager is a singleton
#*****************************************************
mruExplManager = MruExplManager()

__all__ = ['mruExplManager']
<|endoftext|>


<commit_before>from typing import Any, Callable, Dict, Iterable, List, Tuple
from unittest import mock

import ujson
from django.test import override_settings

from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.utils import statsd

def fix_params(raw_params: Dict[str, Any]) -> Dict[str, str]:
    # A few of our few legacy endpoints need their
    # individual parameters serialized as JSON.
    return {k: ujson.dumps(v) for k, v in raw_params.items()}

class StatsMock:
    def __init__(self, settings: Callable[..., Any]) -> None:
        self.settings = settings
        self.real_impl = statsd
        self.func_calls: List[Tuple[str, Iterable[Any]]] = []

    def __getattr__(self, name: str) -> Callable[..., Any]:
        def f(*args: Any) -> None:
            with self.settings(STATSD_HOST=''):
                getattr(self.real_impl, name)(*args)
            self.func_calls.append((name, args))

        return f

class TestReport(ZulipTestCase):
    def test_send_time(self) -> None:
        self.login('hamlet')

        params = dict(
            time=5,
            received=6,
            displayed=7,
            locally_echoed='true',
            rendered_content_disparity='true',
        )

        stats_mock = StatsMock(self.settings)
        with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
            result = self.client_post("/json/report/send_times", params)
        self.assert_json_success(result)

        expected_calls = [
            ('timing', ('endtoend.send_time.zulip', 5)),
            ('timing', ('endtoend.receive_time.zulip', 6)),
            ('timing', ('endtoend.displayed_time.zulip', 7)),
            ('incr', ('locally_echoed',)),
            ('incr', ('render_disparity',)),
        ]
        self.assertEqual(stats_mock.func_calls, expected_calls)

    def test_narrow_time(self) -> None:
        self.login('hamlet')

        params = dict(
            initial_core=5,
            initial_free=6,
            network=7,
        )

        stats_mock = StatsMock(self.settings)
        with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
            result = self.client_post("/json/report/narrow_times", params)
        self.assert_json_success(result)

        expected_calls = [
            ('timing', ('narrow.initial_core.zulip', 5)),
            ('timing', ('narrow.initial_free.zulip', 6)),
            ('timing', ('narrow.network.zulip', 7)),
        ]
        self.assertEqual(stats_mock.func_calls, expected_calls)

    def test_unnarrow_time(self) -> None:
        self.login('hamlet')

        params = dict(
            initial_core=5,
            initial_free=6,
        )

        stats_mock = StatsMock(self.settings)
        with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
            result = self.client_post("/json/report/unnarrow_times", params)
        self.assert_json_success(result)

        expected_calls = [
            ('timing', ('unnarrow.initial_core.zulip', 5)),
            ('timing', ('unnarrow.initial_free.zulip', 6)),
        ]
        self.assertEqual(stats_mock.func_calls, expected_calls)

    @override_settings(BROWSER_ERROR_REPORTING=True)
    def test_report_error(self) -> None:
        user = self.example_user('hamlet')
        self.login_user(user)
        self.make_stream('errors', user.realm)

        params = fix_params(dict(
            message='hello',
            stacktrace='trace',
            ui_message=True,
            user_agent='agent',
            href='href',
            log='log',
            more_info=dict(foo='bar', draft_content="**draft**"),
        ))

        publish_mock = mock.patch('zerver.views.report.queue_json_publish')
        subprocess_mock = mock.patch(
            'zerver.views.report.subprocess.check_output',
            side_effect=KeyError('foo'),
        )
        with publish_mock as m, subprocess_mock:
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)

        report = m.call_args[0][1]['report']
        for k in set(params) - {'ui_message', 'more_info'}:
            self.assertEqual(report[k], params[k])

        self.assertEqual(report['more_info'], dict(foo='bar', draft_content="'**xxxxx**'"))
        self.assertEqual(report['user_email'], user.delivery_email)

        # Teset with no more_info
        del params['more_info']
        with publish_mock as m, subprocess_mock:
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)

        with self.settings(BROWSER_ERROR_REPORTING=False):
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)

        # If js_source_map is present, then the stack trace should be annotated.
        # DEVELOPMENT=False and TEST_SUITE=False are necessary to ensure that
        # js_source_map actually gets instantiated.
        with \
                self.settings(DEVELOPMENT=False, TEST_SUITE=False), \
                mock.patch('zerver.lib.unminify.SourceMap.annotate_stacktrace') as annotate:
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)
        # fix_params (see above) adds quotes when JSON encoding.
        annotate.assert_called_once_with('"trace"')

        # Now test without authentication.
        self.logout()
        with \
                self.settings(DEVELOPMENT=False, TEST_SUITE=False), \
                mock.patch('zerver.lib.unminify.SourceMap.annotate_stacktrace') as annotate:
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)

    def test_report_csp_violations(self) -> None:
        fixture_data = self.fixture_data('csp_report.json')
        result = self.client_post("/report/csp_violations", fixture_data, content_type="application/json")
        self.assert_json_success(result)
<commit_msg>test_report: Use assertLogs to avoid spam in test-backend output.<commit_after>from typing import Any, Callable, Dict, Iterable, List, Tuple
from unittest import mock

import ujson
from django.test import override_settings

from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.utils import statsd

def fix_params(raw_params: Dict[str, Any]) -> Dict[str, str]:
    # A few of our few legacy endpoints need their
    # individual parameters serialized as JSON.
    return {k: ujson.dumps(v) for k, v in raw_params.items()}

class StatsMock:
    def __init__(self, settings: Callable[..., Any]) -> None:
        self.settings = settings
        self.real_impl = statsd
        self.func_calls: List[Tuple[str, Iterable[Any]]] = []

    def __getattr__(self, name: str) -> Callable[..., Any]:
        def f(*args: Any) -> None:
            with self.settings(STATSD_HOST=''):
                getattr(self.real_impl, name)(*args)
            self.func_calls.append((name, args))

        return f

class TestReport(ZulipTestCase):
    def test_send_time(self) -> None:
        self.login('hamlet')

        params = dict(
            time=5,
            received=6,
            displayed=7,
            locally_echoed='true',
            rendered_content_disparity='true',
        )

        stats_mock = StatsMock(self.settings)
        with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
            result = self.client_post("/json/report/send_times", params)
        self.assert_json_success(result)

        expected_calls = [
            ('timing', ('endtoend.send_time.zulip', 5)),
            ('timing', ('endtoend.receive_time.zulip', 6)),
            ('timing', ('endtoend.displayed_time.zulip', 7)),
            ('incr', ('locally_echoed',)),
            ('incr', ('render_disparity',)),
        ]
        self.assertEqual(stats_mock.func_calls, expected_calls)

    def test_narrow_time(self) -> None:
        self.login('hamlet')

        params = dict(
            initial_core=5,
            initial_free=6,
            network=7,
        )

        stats_mock = StatsMock(self.settings)
        with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
            result = self.client_post("/json/report/narrow_times", params)
        self.assert_json_success(result)

        expected_calls = [
            ('timing', ('narrow.initial_core.zulip', 5)),
            ('timing', ('narrow.initial_free.zulip', 6)),
            ('timing', ('narrow.network.zulip', 7)),
        ]
        self.assertEqual(stats_mock.func_calls, expected_calls)

    def test_unnarrow_time(self) -> None:
        self.login('hamlet')

        params = dict(
            initial_core=5,
            initial_free=6,
        )

        stats_mock = StatsMock(self.settings)
        with mock.patch('zerver.views.report.statsd', wraps=stats_mock):
            result = self.client_post("/json/report/unnarrow_times", params)
        self.assert_json_success(result)

        expected_calls = [
            ('timing', ('unnarrow.initial_core.zulip', 5)),
            ('timing', ('unnarrow.initial_free.zulip', 6)),
        ]
        self.assertEqual(stats_mock.func_calls, expected_calls)

    @override_settings(BROWSER_ERROR_REPORTING=True)
    def test_report_error(self) -> None:
        user = self.example_user('hamlet')
        self.login_user(user)
        self.make_stream('errors', user.realm)

        params = fix_params(dict(
            message='hello',
            stacktrace='trace',
            ui_message=True,
            user_agent='agent',
            href='href',
            log='log',
            more_info=dict(foo='bar', draft_content="**draft**"),
        ))

        publish_mock = mock.patch('zerver.views.report.queue_json_publish')
        subprocess_mock = mock.patch(
            'zerver.views.report.subprocess.check_output',
            side_effect=KeyError('foo'),
        )
        with publish_mock as m, subprocess_mock:
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)

        report = m.call_args[0][1]['report']
        for k in set(params) - {'ui_message', 'more_info'}:
            self.assertEqual(report[k], params[k])

        self.assertEqual(report['more_info'], dict(foo='bar', draft_content="'**xxxxx**'"))
        self.assertEqual(report['user_email'], user.delivery_email)

        # Teset with no more_info
        del params['more_info']
        with publish_mock as m, subprocess_mock:
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)

        with self.settings(BROWSER_ERROR_REPORTING=False):
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)

        # If js_source_map is present, then the stack trace should be annotated.
        # DEVELOPMENT=False and TEST_SUITE=False are necessary to ensure that
        # js_source_map actually gets instantiated.
        with \
                self.settings(DEVELOPMENT=False, TEST_SUITE=False), \
                mock.patch('zerver.lib.unminify.SourceMap.annotate_stacktrace') as annotate, \
                self.assertLogs(level='INFO') as info_logs:
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)
        # fix_params (see above) adds quotes when JSON encoding.
        annotate.assert_called_once_with('"trace"')
        self.assertEqual(info_logs.output, [
            'INFO:root:Processing traceback with type browser for None'
        ])

        # Now test without authentication.
        self.logout()
        with \
                self.settings(DEVELOPMENT=False, TEST_SUITE=False), \
                mock.patch('zerver.lib.unminify.SourceMap.annotate_stacktrace') as annotate, \
                self.assertLogs(level='INFO') as info_logs:
            result = self.client_post("/json/report/error", params)
        self.assert_json_success(result)
        self.assertEqual(info_logs.output, [
            'INFO:root:Processing traceback with type browser for None'
        ])

    def test_report_csp_violations(self) -> None:
        fixture_data = self.fixture_data('csp_report.json')
        with self.assertLogs(level='WARNING') as warn_logs:
            result = self.client_post("/report/csp_violations", fixture_data, content_type="application/json")
        self.assert_json_success(result)
        self.assertEqual(warn_logs.output, [
            "WARNING:root:CSP Violation in Document(''). Blocked URI(''), Original Policy(''), Violated Directive(''), Effective Directive(''), Disposition(''), Referrer(''), Status Code(''), Script Sample('')"
        ])
<|endoftext|>


<commit_before>#
# Copyright 2013 Quantopian, Inc.
#
# 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.
from __future__ import division

import abc

import math

from copy import copy
from functools import partial

from six import with_metaclass

from zipline.protocol import DATASOURCE_TYPE
import zipline.utils.math_utils as zp_math

SELL = 1 << 0
BUY = 1 << 1
STOP = 1 << 2
LIMIT = 1 << 3

def check_order_triggers(order, event):
    """
    Given an order and a trade event, return a tuple of
    (stop_reached, limit_reached).
    For market orders, will return (False, False).
    For stop orders, limit_reached will always be False.
    For limit orders, stop_reached will always be False.
    For stop limit orders a Boolean is returned to flag
    that the stop has been reached.

    Orders that have been triggered already (price targets reached),
    the order's current values are returned.
    """
    if order.triggered:
        return (order.stop_reached, order.limit_reached, False)

    stop_reached = False
    limit_reached = False
    sl_stop_reached = False

    order_type = 0

    if order.amount > 0:
        order_type |= BUY
    else:
        order_type |= SELL

    if order.stop is not None:
        order_type |= STOP

    if order.limit is not None:
        order_type |= LIMIT

    if order_type == BUY | STOP | LIMIT:
        if event.price >= order.stop:
            sl_stop_reached = True
            if event.price <= order.limit:
                limit_reached = True
    elif order_type == SELL | STOP | LIMIT:
        if event.price <= order.stop:
            sl_stop_reached = True
            if event.price >= order.limit:
                limit_reached = True
    elif order_type == BUY | STOP:
        if event.price >= order.stop:
            stop_reached = True
    elif order_type == SELL | STOP:
        if event.price <= order.stop:
            stop_reached = True
    elif order_type == BUY | LIMIT:
        if event.price <= order.limit:
            limit_reached = True
    elif order_type == SELL | LIMIT:
        # This is a SELL LIMIT order
        if event.price >= order.limit:
            limit_reached = True

    return (stop_reached, limit_reached, sl_stop_reached)

def transact_stub(slippage, commission, event, open_orders):
    """
    This is intended to be wrapped in a partial, so that the
    slippage and commission models can be enclosed.
    """
    for order, transaction in slippage(event, open_orders):
        if (
            transaction
            and not
            zp_math.tolerant_equals(transaction.amount, 0)
        ):
            direction = math.copysign(1, transaction.amount)
            per_share, total_commission = commission.calculate(transaction)
            transaction.price = transaction.price + (per_share * direction)
            transaction.commission = total_commission
        yield order, transaction

def transact_partial(slippage, commission):
    return partial(transact_stub, slippage, commission)

class Transaction(object):

    def __init__(self, sid, amount, dt, price, order_id, commission=None):
        self.sid = sid
        self.amount = amount
        self.dt = dt
        self.price = price
        self.order_id = order_id
        self.commission = commission
        self.type = DATASOURCE_TYPE.TRANSACTION

    def __getitem__(self, name):
        return self.__dict__[name]

    def to_dict(self):
        py = copy(self.__dict__)
        del py['type']
        return py

def create_transaction(event, order, price, amount):

    # floor the amount to protect against non-whole number orders
    # TODO: Investigate whether we can add a robust check in blotter
    # and/or tradesimulation, as well.
    amount_magnitude = int(abs(amount))

    if amount_magnitude < 1:
        raise Exception("Transaction magnitude must be at least 1.")

    transaction = Transaction(
        sid=event.sid,
        amount=int(amount),
        dt=event.dt,
        price=price,
        order_id=order.id
    )

    return transaction

class SlippageModel(with_metaclass(abc.ABCMeta)):

    @property
    def volume_for_bar(self):
        return self._volume_for_bar

    @abc.abstractproperty
    def process_order(self, event, order):
        pass

    def simulate(self, event, current_orders):

        self._volume_for_bar = 0

        for order in current_orders:

            if zp_math.tolerant_equals(order.open_amount, 0):
                continue

            order.check_triggers(event)
            if not order.triggered:
                continue

            txn = self.process_order(event, order)

            if txn:
                self._volume_for_bar += abs(txn.amount)
                yield order, txn

    def __call__(self, event, current_orders, **kwargs):
        return self.simulate(event, current_orders, **kwargs)

class VolumeShareSlippage(SlippageModel):

    def __init__(self,
                 volume_limit=.25,
                 price_impact=0.1):

        self.volume_limit = volume_limit
        self.price_impact = price_impact

    def __repr__(self):
        return """
{class_name}(
    volume_limit={volume_limit},
    price_impact={price_impact})
""".strip().format(class_name=self.__class__.__name__,
                   volume_limit=self.volume_limit,
                   price_impact=self.price_impact)

    def process_order(self, event, order):

        max_volume = self.volume_limit * event.volume

        # price impact accounts for the total volume of transactions
        # created against the current minute bar
        remaining_volume = max_volume - self.volume_for_bar
        if remaining_volume < 1:
            # we can't fill any more transactions
            return

        # the current order amount will be the min of the
        # volume available in the bar or the open amount.
        cur_volume = int(min(remaining_volume, abs(order.open_amount)))

        if cur_volume < 1:
            return

        # tally the current amount into our total amount ordered.
        # total amount will be used to calculate price impact
        total_volume = self.volume_for_bar + cur_volume

        volume_share = min(total_volume / event.volume,
                           self.volume_limit)

        simulated_impact = (volume_share) ** 2 \
            * math.copysign(self.price_impact, order.direction) \
            * event.price

        return create_transaction(
            event,
            order,
            # In the future, we may want to change the next line
            # for limit pricing
            event.price + simulated_impact,
            math.copysign(cur_volume, order.direction)
        )

class FixedSlippage(SlippageModel):

    def __init__(self, spread=0.0):
        """
        Use the fixed slippage model, which will just add/subtract
        a specified spread spread/2 will be added on buys and subtracted
        on sells per share
        """
        self.spread = spread

    def process_order(self, event, order):
        return create_transaction(
            event,
            order,
            event.price + (self.spread / 2.0 * order.direction),
            order.amount,
        )
<commit_msg>MAINT: Removed unnecessary floating point checks in slippage<commit_after>#
# Copyright 2013 Quantopian, Inc.
#
# 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.
from __future__ import division

import abc

import math

from copy import copy
from functools import partial

from six import with_metaclass

from zipline.protocol import DATASOURCE_TYPE

SELL = 1 << 0
BUY = 1 << 1
STOP = 1 << 2
LIMIT = 1 << 3

def check_order_triggers(order, event):
    """
    Given an order and a trade event, return a tuple of
    (stop_reached, limit_reached).
    For market orders, will return (False, False).
    For stop orders, limit_reached will always be False.
    For limit orders, stop_reached will always be False.
    For stop limit orders a Boolean is returned to flag
    that the stop has been reached.

    Orders that have been triggered already (price targets reached),
    the order's current values are returned.
    """
    if order.triggered:
        return (order.stop_reached, order.limit_reached, False)

    stop_reached = False
    limit_reached = False
    sl_stop_reached = False

    order_type = 0

    if order.amount > 0:
        order_type |= BUY
    else:
        order_type |= SELL

    if order.stop is not None:
        order_type |= STOP

    if order.limit is not None:
        order_type |= LIMIT

    if order_type == BUY | STOP | LIMIT:
        if event.price >= order.stop:
            sl_stop_reached = True
            if event.price <= order.limit:
                limit_reached = True
    elif order_type == SELL | STOP | LIMIT:
        if event.price <= order.stop:
            sl_stop_reached = True
            if event.price >= order.limit:
                limit_reached = True
    elif order_type == BUY | STOP:
        if event.price >= order.stop:
            stop_reached = True
    elif order_type == SELL | STOP:
        if event.price <= order.stop:
            stop_reached = True
    elif order_type == BUY | LIMIT:
        if event.price <= order.limit:
            limit_reached = True
    elif order_type == SELL | LIMIT:
        # This is a SELL LIMIT order
        if event.price >= order.limit:
            limit_reached = True

    return (stop_reached, limit_reached, sl_stop_reached)

def transact_stub(slippage, commission, event, open_orders):
    """
    This is intended to be wrapped in a partial, so that the
    slippage and commission models can be enclosed.
    """
    for order, transaction in slippage(event, open_orders):
        if transaction and transaction.amount != 0:
            direction = math.copysign(1, transaction.amount)
            per_share, total_commission = commission.calculate(transaction)
            transaction.price += per_share * direction
            transaction.commission = total_commission
        yield order, transaction

def transact_partial(slippage, commission):
    return partial(transact_stub, slippage, commission)

class Transaction(object):

    def __init__(self, sid, amount, dt, price, order_id, commission=None):
        self.sid = sid
        self.amount = amount
        self.dt = dt
        self.price = price
        self.order_id = order_id
        self.commission = commission
        self.type = DATASOURCE_TYPE.TRANSACTION

    def __getitem__(self, name):
        return self.__dict__[name]

    def to_dict(self):
        py = copy(self.__dict__)
        del py['type']
        return py

def create_transaction(event, order, price, amount):

    # floor the amount to protect against non-whole number orders
    # TODO: Investigate whether we can add a robust check in blotter
    # and/or tradesimulation, as well.
    amount_magnitude = int(abs(amount))

    if amount_magnitude < 1:
        raise Exception("Transaction magnitude must be at least 1.")

    transaction = Transaction(
        sid=event.sid,
        amount=int(amount),
        dt=event.dt,
        price=price,
        order_id=order.id
    )

    return transaction

class SlippageModel(with_metaclass(abc.ABCMeta)):

    @property
    def volume_for_bar(self):
        return self._volume_for_bar

    @abc.abstractproperty
    def process_order(self, event, order):
        pass

    def simulate(self, event, current_orders):

        self._volume_for_bar = 0

        for order in current_orders:

            if order.open_amount == 0:
                continue

            order.check_triggers(event)
            if not order.triggered:
                continue

            txn = self.process_order(event, order)

            if txn:
                self._volume_for_bar += abs(txn.amount)
                yield order, txn

    def __call__(self, event, current_orders, **kwargs):
        return self.simulate(event, current_orders, **kwargs)

class VolumeShareSlippage(SlippageModel):

    def __init__(self,
                 volume_limit=.25,
                 price_impact=0.1):

        self.volume_limit = volume_limit
        self.price_impact = price_impact

    def __repr__(self):
        return """
{class_name}(
    volume_limit={volume_limit},
    price_impact={price_impact})
""".strip().format(class_name=self.__class__.__name__,
                   volume_limit=self.volume_limit,
                   price_impact=self.price_impact)

    def process_order(self, event, order):

        max_volume = self.volume_limit * event.volume

        # price impact accounts for the total volume of transactions
        # created against the current minute bar
        remaining_volume = max_volume - self.volume_for_bar
        if remaining_volume < 1:
            # we can't fill any more transactions
            return

        # the current order amount will be the min of the
        # volume available in the bar or the open amount.
        cur_volume = int(min(remaining_volume, abs(order.open_amount)))

        if cur_volume < 1:
            return

        # tally the current amount into our total amount ordered.
        # total amount will be used to calculate price impact
        total_volume = self.volume_for_bar + cur_volume

        volume_share = min(total_volume / event.volume,
                           self.volume_limit)

        simulated_impact = volume_share ** 2 \
            * math.copysign(self.price_impact, order.direction) \
            * event.price

        return create_transaction(
            event,
            order,
            # In the future, we may want to change the next line
            # for limit pricing
            event.price + simulated_impact,
            math.copysign(cur_volume, order.direction)
        )

class FixedSlippage(SlippageModel):

    def __init__(self, spread=0.0):
        """
        Use the fixed slippage model, which will just add/subtract
        a specified spread spread/2 will be added on buys and subtracted
        on sells per share
        """
        self.spread = spread

    def process_order(self, event, order):
        return create_transaction(
            event,
            order,
            event.price + (self.spread / 2.0 * order.direction),
            order.amount,
        )
<|endoftext|>


<commit_before>import tensorflow as tf
import NetVLAD_CG
import reader_utils
import loss
import tensorflow_datasets as tfds

import eval_util

import readers

from tensorflow.python.platform import flags

FLAGS = flags.FLAGS
NUM_EXAMPLES = 3844
NUM_VAL_EXAMPLES = 3844

if __name__ == "__main__":
	# Dataset flags.
	flags.DEFINE_string("train_dir", "~/data/train/train*.tfreco",
											"The directory to save the model files in.")
	flags.DEFINE_string(
			"train_data_pattern", "~/data/train/train*.tfrecord",
			"File glob for the training dataset. If the files refer to Frame Level "
			"features (i.e. tensorflow.SequenceExample), then set --reader_type "
			"format. The (Sequence)Examples are expected to have 'rgb' byte array "
			"sequence feature as well as a 'labels' int64 context feature.")
	flags.DEFINE_integer("num_readers", 8,
											 "How many threads to use for reading input files.")

def train(epochs=5, lr=0.01, num_clusters=64, batch_size=64, random_frames=True, num_mixtures=2, fc_units=2048, num_frames=30):
	steps_per_epoch = NUM_EXAMPLES // batch_size
	validation_steps = NUM_VAL_EXAMPLES // batch_size

	#Set up Reader and Preprocess Data
	data_reader = reader_utils.get_reader(num_samples=num_frames, random_frames=random_frames)

	train_dataset = data_reader.get_dataset('/home/conorfvedova_google_com/data/train/', batch_size=batch_size, num_workers=8)

	validation_dataset = data_reader.get_dataset('/home/conorfvedova_google_com/data/validate/', batch_size=batch_size, num_workers=8, type="validate")

	video_input_shape = (batch_size, num_frames, 1024)
	audio_input_shape = (batch_size, num_frames, 128)
	input_shape = (batch_size, num_frames, 1152)

	#Compile and train model
	model = NetVLAD_CG.VideoClassifier(num_clusters, video_input_shape, audio_input_shape, fc_units=fc_units, num_classes=data_reader.num_classes, num_mixtures=num_mixtures)

	model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lr), loss=loss.custom_crossentropy, metrics=['categorical_accuracy'])
	model.build(input_shape)
	model.summary()
	model.fit(x=train_dataset, steps_per_epoch=steps_per_epoch, validation_data=validation_dataset, validation_steps=validation_steps, epochs=epochs)

	#Evaluate model
	test_dataset = data_reader.get_dataset('/home/conorfvedova_google_com/data/test/', batch_size=batch_size, num_workers=8, type="test")
	numpy_dataset = tfds.as_numpy(test_dataset)
	evaluation_metrics = eval_util.EvaluationMetrics(data_reader.num_classes, 20)
	for i in range(3844 // batch_size):
		batch = next(numpy_dataset)
		test_input = tf.convert_to_tensor(batch[0])
		test_labels = tf.convert_to_tensor(batch[1])

		predictions = model.predict(test_input)

		loss_vals = loss.eval_loss(test_labels, predictions)

		test_labels = test_labels.numpy()
		loss_vals = loss_vals.numpy()

		evaluation_metrics.accumulate(predictions, test_labels, loss_vals)
	eval_dict = evaluation_metrics.get()

	print(eval_dict)

	#model.save_weights('/home/conorfvedova_google_com/saved_model/model-final.h5')

if __name__ == "__main__":
	train()
<commit_msg>Adding Random Frames<commit_after>import tensorflow as tf
import NetVLAD_CG
import reader_utils
import loss
import tensorflow_datasets as tfds

import eval_util

import readers

from tensorflow.python.platform import flags

FLAGS = flags.FLAGS
NUM_EXAMPLES = 3844
NUM_VAL_EXAMPLES = 3844

if __name__ == "__main__":
	# Dataset flags.
	flags.DEFINE_string("train_dir", "~/data/train/train*.tfreco",
											"The directory to save the model files in.")
	flags.DEFINE_string(
			"train_data_pattern", "~/data/train/train*.tfrecord",
			"File glob for the training dataset. If the files refer to Frame Level "
			"features (i.e. tensorflow.SequenceExample), then set --reader_type "
			"format. The (Sequence)Examples are expected to have 'rgb' byte array "
			"sequence feature as well as a 'labels' int64 context feature.")
	flags.DEFINE_integer("num_readers", 8,
											 "How many threads to use for reading input files.")

def train(epochs=5, lr=0.01, num_clusters=64, batch_size=64, random_frames=True, num_mixtures=2, fc_units=1024, num_frames=30):
	steps_per_epoch = NUM_EXAMPLES // batch_size
	validation_steps = NUM_VAL_EXAMPLES // batch_size

	#Set up Reader and Preprocess Data
	data_reader = reader_utils.get_reader(num_samples=num_frames, random_frames=random_frames)

	train_dataset = data_reader.get_dataset('/home/conorfvedova_google_com/data/train/', batch_size=batch_size, num_workers=8)

	validation_dataset = data_reader.get_dataset('/home/conorfvedova_google_com/data/validate/', batch_size=batch_size, num_workers=8, type="validate")

	video_input_shape = (batch_size, num_frames, 1024)
	audio_input_shape = (batch_size, num_frames, 128)
	input_shape = (batch_size, num_frames, 1152)

	#Compile and train model
	model = NetVLAD_CG.VideoClassifier(num_clusters, video_input_shape, audio_input_shape, fc_units=fc_units, num_classes=data_reader.num_classes, num_mixtures=num_mixtures)

	model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lr), loss=loss.custom_crossentropy, metrics=['categorical_accuracy'])
	model.build(input_shape)
	model.summary()
	model.fit(x=train_dataset, steps_per_epoch=steps_per_epoch, validation_data=validation_dataset, validation_steps=validation_steps, epochs=epochs)

	#Evaluate model
	test_dataset = data_reader.get_dataset('/home/conorfvedova_google_com/data/test/', batch_size=batch_size, num_workers=8, type="test")
	numpy_dataset = tfds.as_numpy(test_dataset)
	evaluation_metrics = eval_util.EvaluationMetrics(data_reader.num_classes, 20)
	for i in range(3844 // batch_size):
		batch = next(numpy_dataset)
		test_input = tf.convert_to_tensor(batch[0])
		test_labels = tf.convert_to_tensor(batch[1])

		predictions = model.predict(test_input)

		loss_vals = loss.eval_loss(test_labels, predictions)

		test_labels = test_labels.numpy()
		loss_vals = loss_vals.numpy()

		evaluation_metrics.accumulate(predictions, test_labels, loss_vals)
	eval_dict = evaluation_metrics.get()

	print(eval_dict)

	#model.save_weights('/home/conorfvedova_google_com/saved_model/model-final.h5')

if __name__ == "__main__":
	train()
<|endoftext|>


<commit_before>"""
Simple set of classes that roughly map one-to-one to the operations that can
performed in the django ORM.

Each class must provide a `clean' method that checks a given `value' is of
the right length and in some cases, type.
"""
import re

from avocado.utils.iter import ins

__all__ = ('exact', 'iexact', 'contains', 'inlist', 'lt', 'gt', 'lte', 'gte',
    'between', 'null', 'notbetween', 'notexact', 'notiexact', 'doesnotcontain',
    'notinlist', 'notnull', 'MODEL_FIELD_MAP')

FIELD_LOOKUPS = re.compile(r'(i?exact|i?contains|in|gte?|lte?|i?startswith'\
    '|i?endswith|range|year|month|day|week_day|isnull|search|i?regex)')

class Operator(object):
    short_name = ''
    verbose_name = ''
    operator = ''
    negated = False

    def __str__(self):
        if self.negated:
            return '%s (~%s)' % (self.short_name, self.operator)
        return '%s (%s)' % (self.short_name, self.operator)

    def __unicode__(self):
        return u'%s' % str(self)

    def __repr__(self):
        return str(self.__class__)

    def _get_uid(self):
        if self.negated:
            return '-%s' % self.operator
        return '%s' % self.operator
    uid = property(_get_uid)

    def check(self, value):
        "Cleans and verifies `value' can be used for this operator."
        raise NotImplementedError

class PrimitiveOperator(Operator):
    def check(self, value):
        if not ins(value):
            return True
        return False

class SequenceOperator(Operator):
    def check(self, value):
        if ins(value):
            return True
        return False

class Exact(PrimitiveOperator):
    "Only used with boolean fields. Use `iexact' otherwise."
    short_name = '='
    verbose_name = 'is equal to'
    operator = 'exact'
exact = Exact()

class iExact(PrimitiveOperator):
    short_name = '='
    verbose_name = 'is equal to'
    operator = 'iexact'
iexact = iExact()

class Contains(PrimitiveOperator):
    short_name = 'contains'
    verbose_name = 'contains'
    operator = 'icontains'
contains = Contains()

class LessThan(PrimitiveOperator):
    short_name = '<'
    verbose_name = 'is less than'
    operator = 'lt'
lt = LessThan()

class GreaterThan(PrimitiveOperator):
    short_name = '>'
    verbose_name = 'is greater than'
    operator = 'gt'
gt = GreaterThan()

class LessThanOrEqual(PrimitiveOperator):
    short_name = '<='
    verbose_name = 'is less than or equal to'
    operator = 'lte'
lte = LessThanOrEqual()

class GreaterThanOrEqual(PrimitiveOperator):
    short_name = '>='
    verbose_name = 'is greater than or equal to'
    operator = 'gte'
gte = GreaterThanOrEqual()

class Null(PrimitiveOperator):
    short_name = 'is null'
    verbose_name = 'is null'
    operator = 'isnull'
null = Null()

class InList(SequenceOperator):
    short_name = 'in list'
    verbose_name = 'is in list'
    operator = 'in'
inlist = InList()

class Between(SequenceOperator):
    short_name = 'between'
    verbose_name = 'is between'
    operator = 'range'

    def check(self, value):
        if ins(value) and len(value) == 2:
            return True
        return False
between = Between()

class NotBetween(Between):
    short_name = 'not between'
    verbose_name = 'is not between'
    negated = True
notbetween = NotBetween()

class NotExact(Exact):
    short_name = '!='
    verbose_name = 'is not equal to'
    negated = True
notexact = NotExact()

class NotiExact(iExact):
    short_name = '!='
    verbose_name = 'is not equal to'
    negated = True
notiexact = NotiExact()

class DoesNotContain(Contains):
    short_name = 'does not contain'
    verbose_name = 'does not contain'
    negated = True
doesnotcontain = DoesNotContain()

class NotInList(InList):
    short_name = 'not in list'
    verbose_name = 'is not in list'
    negated = True
notinlist = NotInList()

class NotNull(Null):
    short_name = 'not null'
    verbose_name = 'is not null'
    negated = True
notnull = NotNull()

CHAR_OPERATORS = (iexact, notiexact, contains, doesnotcontain, inlist,
    notinlist, null, notnull)

NUMERIC_OPERATORS = (exact, notexact, lt, lte, gt, gte, between,
    notbetween, null, notnull)

MODEL_FIELD_MAP = {
    'AutoField': (exact, notexact, inlist, notinlist),
    'CharField': CHAR_OPERATORS,
    'IntegerField': NUMERIC_OPERATORS,
    'FloatField': NUMERIC_OPERATORS,
    'DecimalField': NUMERIC_OPERATORS,
    'DateField': NUMERIC_OPERATORS,
    'DateTimeField': NUMERIC_OPERATORS,
    'TimeField': NUMERIC_OPERATORS,
    'BooleanField': (exact, notexact),
    'NullBooleanField': (exact, notexact, null, notnull),
}
<commit_msg>added operators to char types<commit_after>"""
Simple set of classes that roughly map one-to-one to the operations that can
performed in the django ORM.

Each class must provide a `clean' method that checks a given `value' is of
the right length and in some cases, type.
"""
import re

from avocado.utils.iter import ins

__all__ = ('exact', 'iexact', 'contains', 'inlist', 'lt', 'gt', 'lte', 'gte',
    'between', 'null', 'notbetween', 'notexact', 'notiexact', 'doesnotcontain',
    'notinlist', 'notnull', 'MODEL_FIELD_MAP')

FIELD_LOOKUPS = re.compile(r'(i?exact|i?contains|in|gte?|lte?|i?startswith'\
    '|i?endswith|range|year|month|day|week_day|isnull|search|i?regex)')

class Operator(object):
    short_name = ''
    verbose_name = ''
    operator = ''
    negated = False

    def __str__(self):
        if self.negated:
            return '%s (~%s)' % (self.short_name, self.operator)
        return '%s (%s)' % (self.short_name, self.operator)

    def __unicode__(self):
        return u'%s' % str(self)

    def __repr__(self):
        return str(self.__class__)

    def _get_uid(self):
        if self.negated:
            return '-%s' % self.operator
        return '%s' % self.operator
    uid = property(_get_uid)

    def check(self, value):
        "Cleans and verifies `value' can be used for this operator."
        raise NotImplementedError

class PrimitiveOperator(Operator):
    def check(self, value):
        if not ins(value):
            return True
        return False

class SequenceOperator(Operator):
    def check(self, value):
        if ins(value):
            return True
        return False

class Exact(PrimitiveOperator):
    "Only used with boolean fields. Use `iexact' otherwise."
    short_name = '='
    verbose_name = 'is equal to'
    operator = 'exact'
exact = Exact()

class iExact(PrimitiveOperator):
    short_name = '='
    verbose_name = 'is equal to'
    operator = 'iexact'
iexact = iExact()

class Contains(PrimitiveOperator):
    short_name = 'contains'
    verbose_name = 'contains'
    operator = 'icontains'
contains = Contains()

class LessThan(PrimitiveOperator):
    short_name = '<'
    verbose_name = 'is less than'
    operator = 'lt'
lt = LessThan()

class GreaterThan(PrimitiveOperator):
    short_name = '>'
    verbose_name = 'is greater than'
    operator = 'gt'
gt = GreaterThan()

class LessThanOrEqual(PrimitiveOperator):
    short_name = '<='
    verbose_name = 'is less than or equal to'
    operator = 'lte'
lte = LessThanOrEqual()

class GreaterThanOrEqual(PrimitiveOperator):
    short_name = '>='
    verbose_name = 'is greater than or equal to'
    operator = 'gte'
gte = GreaterThanOrEqual()

class Null(PrimitiveOperator):
    short_name = 'is null'
    verbose_name = 'is null'
    operator = 'isnull'
null = Null()

class InList(SequenceOperator):
    short_name = 'in list'
    verbose_name = 'is in list'
    operator = 'in'
inlist = InList()

class Between(SequenceOperator):
    short_name = 'between'
    verbose_name = 'is between'
    operator = 'range'

    def check(self, value):
        if ins(value) and len(value) == 2:
            return True
        return False
between = Between()

class NotBetween(Between):
    short_name = 'not between'
    verbose_name = 'is not between'
    negated = True
notbetween = NotBetween()

class NotExact(Exact):
    short_name = '!='
    verbose_name = 'is not equal to'
    negated = True
notexact = NotExact()

class NotiExact(iExact):
    short_name = '!='
    verbose_name = 'is not equal to'
    negated = True
notiexact = NotiExact()

class DoesNotContain(Contains):
    short_name = 'does not contain'
    verbose_name = 'does not contain'
    negated = True
doesnotcontain = DoesNotContain()

class NotInList(InList):
    short_name = 'not in list'
    verbose_name = 'is not in list'
    negated = True
notinlist = NotInList()

class NotNull(Null):
    short_name = 'not null'
    verbose_name = 'is not null'
    negated = True
notnull = NotNull()

CHAR_OPERATORS = (exact, notexact, iexact, notiexact, contains,
    doesnotcontain, inlist, notinlist, null, notnull)

NUMERIC_OPERATORS = (exact, notexact, lt, lte, gt, gte, between,
    notbetween, null, notnull)

MODEL_FIELD_MAP = {
    'AutoField': (exact, notexact, inlist, notinlist),
    'CharField': CHAR_OPERATORS,
    'IntegerField': NUMERIC_OPERATORS,
    'FloatField': NUMERIC_OPERATORS,
    'DecimalField': NUMERIC_OPERATORS,
    'DateField': NUMERIC_OPERATORS,
    'DateTimeField': NUMERIC_OPERATORS,
    'TimeField': NUMERIC_OPERATORS,
    'BooleanField': (exact, notexact),
    'NullBooleanField': (exact, notexact, null, notnull),
}
<|endoftext|>