def test_callable_old_style(self):
     class MyTest_ok():
         def __call__(self, x):  # @UnusedVariable
             return True
     o = MyTest_ok()
     assert o('value') == True
     new_contract(cname(), o)
 def test_class_as_contract1(self):
     # This should be interpreted as a type
     # init(x,y) so it is not mistaken for a valid callable
     class NewStyleClass(object):
         def __init__(self, x, y):  # @UnusedVariable
             pass
     new_contract(cname(), NewStyleClass)
 def test_callable_5(self):
     class MyTest_ok(object):
         def f(self, x):  # @UnusedVariable
             return True
     o = MyTest_ok()
     assert o.f('value') == True
     new_contract(cname(), o.f)
Beispiel #4
0
 def test_other_pass(self):
     class Ex1(Exception):
         pass
     def invalid(x):
         raise Ex1()
     c = cname() 
     new_contract(c, invalid)
     self.assertRaises(Ex1, check, 'list(%s)' % c, [0])        
def check_valid_identifier(e):
    check_valid_identifier.__dict__['description'] = \
        'check_valid_identifier(%r)' % e

    identifier_expression.parseString(e, parseAll=True) #@UndefinedVariable

    new_contract(e, '*')

    check(e, 42)
    def test_idioms(self):
        Storage.string2contract = {}  # TODO remove this hack
        color = new_contract('color', 'list[3](number,>=0,<=1)')
        # Make sure we got it right
        color.check([0, 0, 0])
        color.check([0, 0, 1])
        color.fail([0, 0])
        color.fail([0, 0, 2])

        self.assertRaises(ValueError, color.fail, [0, 0, 1])

        # Now use ``color`` in other contracts.
        @contract
        def fill_area(inside, border):
            """ Fill the area inside the current figure.

                :type border: color
                :type inside: color
            """
            pass

        @contract
        def fill_gradient(colors):
            """ Use a gradient to fill the area.

                :type colors: list(color)
            """
            pass
Beispiel #7
0
    def __init__(cls, name, bases, namespace):
        '''I copy my class docstring if deriving class has none. I tell base classes when I derive
        a new class from them. I publish a new contract type for each new class I create.
        '''
        doc = namespace.get("__doc__", None)
        if not doc:
            for base in cls.__mro__[1:]:
                if base.__doc__:
                    doc = base.__doc__
                    break
        cls.__doc__ = doc

        # monkey a new contract into the decorator module so checking for that type at runtime can work
        dname = (cls.__module__ + '.' + name).replace('__main__.', 'main.').replace('.', '_')
        if not dname in decorators.__dict__:
            decorators.__dict__[dname] = contracts.new_contract(dname, lambda x: isinstance(x, cls))

        # all bases except object get the derived class' name appended
        for base in [b for b in bases if b != object]:
            derived = cls
            # mangle the name to the base scope
            attribute = '_%s__implementors' % base.__name__
            if hasattr(base, attribute):
                getattr(base, attribute).append(derived)
            else:
                setattr(base, attribute, [derived])
        cls.logger = logger.getLogger('{}.{}'.format(cls.__module__.replace('__main__', 'pymor'), name))
        abc.ABCMeta.__init__(cls, name, bases, namespace)
    def test_string_list(self):
        """
        Special case: a string is a sequence; and therefore might still pass
        the "sequence" test.
        """
        new_contract('string', lambda x: isinstance(x, str))

        @contract(a='[string]')
        def f(a):
            return a

        self.assertRaises(ContractError, f, 'a')
        self.assertRaises(ContractError, f, 1)
        self.assertRaises(ContractError, f, {1: 1})
        self.assertRaises(ContractError, f, [1])
        self.assertEqual(f(['a']), ['a'])
        self.assertEqual(f(['a', '']), ['a', ''])
        self.assertEqual(f([]), [])
    def setUp(self):
        def f():
            raise Exception("Unstable contract")

        new_contract("int", lambda x: isinstance(x, int))
        new_contract("not empty", lambda x: len(x) > 0)
        new_contract("unstable", lambda x: f())
Beispiel #10
0
 def wrapper(func):
     """ Decorator """
     contract_name = func.__name__
     exdesc = _parse_new_contract_args(*args, **kwargs)
     # Pass to the custom contract, via a property, only the
     # exception descriptions
     func.exdesc = dict(
         (
             value['name'],
             (
                 '[START CONTRACT MSG: {0}]{1}'
                 '[STOP CONTRACT MSG]'.format(
                     contract_name, value['msg']
                 )
             )
         ) for value in exdesc
     )
     # Register custom contract
     _register_custom_contracts(contract_name, exdesc)
     # Apply PyContracts decorator
     return contracts.new_contract(func)
Beispiel #11
0
    def __init__(cls, name, bases, namespace):
        '''Metaclass of :class:`BasicInterface`.

        I tell base classes when I derive a new class from them. I publish
        a new contract type for each new class I create. I create a logger
        for each class I create. I add an `init_args` attribute to the class.
        '''
        # monkey a new contract into the decorator module so checking for that type at runtime can work
        if HAVE_CONTRACTS:
            dname = (cls.__module__ + '.' + name).replace('__main__.', 'main.').replace('.', '_')
            if not dname in decorators.__dict__:
                decorators.__dict__[dname] = contracts.new_contract(dname, lambda x: isinstance(x, cls))

        # all bases except object get the derived class' name appended
        for base in [b for b in bases if b != object]:
            derived = cls
            # mangle the name to the base scope
            attribute = '_%s__implementors' % base.__name__
            if hasattr(base, attribute):
                getattr(base, attribute).append(derived)
            else:
                setattr(base, attribute, [derived])
        cls._logger = logger.getLogger('{}.{}'.format(cls.__module__.replace('__main__', 'pymor'), name))
        abc.ABCMeta.__init__(cls, name, bases, namespace)
    software distributed under the Apache License Version 2.0 is distributed on
    an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
    express or implied. See the Apache License Version 2.0 for the specific
    language governing permissions and limitations there under.

    Authors: Anuj More, Alex Dean, Fred Blundun
    Copyright: Copyright (c) 2013-2014 Snowplow Analytics Ltd
    License: Apache License Version 2.0
"""

from contracts import contract, new_contract

SUPPORTED_PLATFORMS = set(["pc", "tv", "mob", "cnsl", "iot"])
DEFAULT_PLATFORM = "pc"

new_contract("subject", lambda x: isinstance(x, Subject))

new_contract("supported_platform", lambda x: x in SUPPORTED_PLATFORMS)

class Subject(object):
    """
        Class for an event subject, where we view events as of the form

        (Subject) -> (Verb) -> (Object)
    """
    def __init__(self):

        self.standard_nv_pairs = {"p": DEFAULT_PLATFORM}

    @contract
    def set_platform(self, value):
Beispiel #13
0
import functools
from contracts import contract, new_contract

from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey, AssetKey
from opaque_keys.edx.locator import LibraryLocator
from opaque_keys.edx.locations import SlashSeparatedCourseKey
from xmodule.assetstore import AssetMetadata

from . import ModuleStoreWriteBase
from . import ModuleStoreEnum
from .exceptions import ItemNotFoundError, DuplicateCourseError
from .draft_and_published import ModuleStoreDraftAndPublished
from .split_migrator import SplitMigrator

new_contract('CourseKey', CourseKey)
new_contract('AssetKey', AssetKey)
new_contract('AssetMetadata', AssetMetadata)
new_contract('LibraryLocator', LibraryLocator)
new_contract('long', long)

log = logging.getLogger(__name__)


def strip_key(func):
    """
    A decorator for stripping version and branch information from return values that are, or contain, UsageKeys or
    CourseKeys.
    Additionally, the decorated function is called with an optional 'field_decorator' parameter that can be used
    to strip any location(-containing) fields, which are not directly returned by the function.
Beispiel #14
0
'''
    Contains all about rotation matrices, quaternions, and various conversions.

    conventions: q=( a + bi + cj + dk), with a>0
'''
import itertools

from contracts import contract, new_contract
from geometry.basic_utils import safe_arccos, normalize_length
from geometry.spheres import default_axis
from geometry.utils.numpy_backport import assert_allclose
import numpy as np

new_contract('unit_quaternion', 'array[4], unit_length')
new_contract('axis_angle', 'tuple(direction, float)')
# Canonically, we return a positive angle.
new_contract('axis_angle_canonical', 'tuple(direction, (float,>=0, <=pi))')


@contract(x='array[NxN],N>0')
def check_SO(x):
    ''' Checks that the given value is a rotation matrix of arbitrary size. '''
    check_orthogonal(x)
    determinant = np.linalg.det(x * 1.0)  # XXX: voodoo
    # lapack_lite.LapackError:
    # Parameter a has non-native byte order in lapack_lite.dgetrf
    assert_allclose(determinant, 1.0)


@contract(x='array[NxN],N>0')
def check_orthogonal(x):
Beispiel #15
0
    ('score', 'float'),
    ('bound', 'float'),
])


@contract(timestamp='float',
          id_track='str',
          coords='seq[2](number)',
          score='>0',
          bound='>0')
def aer_particle(timestamp, id_track, coords, score, bound):
    """ Creates a particle """
    p = np.zeros(shape=(), dtype=aer_particle_dtype)
    p['timestamp'] = timestamp
    p['id_track'] = id_track
    p['coords'][0] = coords[0]
    p['coords'][1] = coords[1]
    p['score'] = score
    p['bound'] = bound
    return p


@new_contract
def is_particle_dtype(x):
    if not x.dtype == aer_particle_dtype:
        msg = 'Invalid dtype: %r' % x.dtype
        raise ValueError(msg)


new_contract('particles_array', 'array[>=1],is_particle_dtype')
import six
from contracts import new_contract

from openedx.core.djangoapps.course_groups.cohorts import get_legacy_discussion_settings
from openedx.core.djangoapps.django_comment_common.models import (
    FORUM_ROLE_ADMINISTRATOR,
    FORUM_ROLE_COMMUNITY_TA,
    FORUM_ROLE_GROUP_MODERATOR,
    FORUM_ROLE_MODERATOR,
    FORUM_ROLE_STUDENT,
    CourseDiscussionSettings,
    Role
)
from openedx.core.lib.cache_utils import request_cached

new_contract('basestring', six.string_types[0])


class ThreadContext(object):
    """ An enumeration that represents the context of a thread. Used primarily by the comments service. """
    STANDALONE = 'standalone'
    COURSE = 'course'


STUDENT_ROLE_PERMISSIONS = ["vote", "update_thread", "follow_thread", "unfollow_thread",
                            "update_comment", "create_sub_comment", "unvote", "create_thread",
                            "follow_commentable", "unfollow_commentable", "create_comment", ]

MODERATOR_ROLE_PERMISSIONS = ["edit_content", "delete_thread", "openclose_thread",
                              "endorse_comment", "delete_comment", "see_all_cohorts"]
Beispiel #17
0
from contracts import new_contract
import numpy as np
from contracts.interface import describe_value, describe_type

__all__ = ['finite']


@new_contract
def finite(x):
    return np.isfinite(x).all()


new_contract('np_scalar_uint', 'np_uint8|np_uint16|np_uint32|np_uint64')
new_contract('np_scalar_int', 'np_int8|np_int16|np_int32|np_int64')
new_contract('np_scalar_float', 'np_float32|np_float64')
new_contract('np_scalar_type', 'np_scalar_int|np_scalar_uint|np_scalar_float')


@new_contract
def np_zeroshape_array(x):
    #     scalars = [
    #     np.int,  # Platform integer (normally either int32 or int64)
    #     np.int8,  # Byte (-128 to 127)
    #     np.int16,  # Integer (-32768 to 32767)
    #     np.int32,  # Integer (-2147483648 to 2147483647)
    #     np.int64,  # Integer (9223372036854775808 to 9223372036854775807)
    #     np.uint8,  # Unsigned integer (0 to 255)
    #     np.uint16,  # Unsigned integer (0 to 65535)
    #     np.uint32,  # Unsigned integer (0 to 4294967295)
    #     np.uint64,  # Unsigned integer (0 to 18446744073709551615)
    #     np.float,  # Shorthand for float64.
Beispiel #18
0
import copy
from contracts import contract, new_contract
from xblock.fields import Scope
from collections import namedtuple
from xblock.exceptions import InvalidScopeError
from .definition_lazy_loader import DefinitionLazyLoader
from xmodule.modulestore.inheritance import InheritanceKeyValueStore
from opaque_keys.edx.locator import BlockUsageLocator

# id is a BlockUsageLocator, def_id is the definition's guid
SplitMongoKVSid = namedtuple('SplitMongoKVSid', 'id, def_id')
new_contract('BlockUsageLocator', BlockUsageLocator)


class SplitMongoKVS(InheritanceKeyValueStore):
    """
    A KeyValueStore that maps keyed data access to one of the 3 data areas
    known to the MongoModuleStore (data, children, and metadata)
    """
    @contract(parent="BlockUsageLocator | None")
    def __init__(self,
                 definition,
                 initial_values,
                 parent,
                 field_decorator=None):
        """

        :param definition: either a lazyloader or definition id for the definition
        :param initial_values: a dictionary of the locally set values
        """
        # deepcopy so that manipulations of fields does not pollute the source
Beispiel #19
0
from .point_set import *
from .product_manifold import *
from .special_euclidean_algebra import *
from .special_euclidean_group import *
from .special_euclidean_group import *
from .special_orthogonal_algebra import *
from .special_orthogonal_group import *
from .sphere import *
from .square import *
from .tangent_bundle import *
from .torus import *
from .torus01 import *
from .translation_algebra import *
from .translation_group import *

new_contract('DifferentiableManifold', DifferentiableManifold)

# keep at the end   #@NoMove

all_manifolds = [
    SO3, SO2,
    R1, R2, R3,
    T1, T2, T3,
    Tran1, Tran2, Tran3,
    SE2, SE3,
    S1, S2,
    se2, se3,
    so2, so3,
    tran1, tran2, tran3,
    TSE2, TSE3,
    Ts1, Ts2, Ts3,
Beispiel #20
0
    def delete(self, key):
        self._raise_unless_scope_is_allowed(key)
        self._field_data_cache.delete(key)

    def has(self, key):
        self._raise_unless_scope_is_allowed(key)
        return self._field_data_cache.has(key)

    def _raise_unless_scope_is_allowed(self, key):
        """Raise an InvalidScopeError if key.scope is not in self._allowed_scopes."""
        if key.scope not in self._allowed_scopes:
            raise InvalidScopeError(key, self._allowed_scopes)


new_contract("DjangoKeyValueStore", DjangoKeyValueStore)
new_contract("DjangoKeyValueStore_Key", DjangoKeyValueStore.Key)


class DjangoOrmFieldCache(object):
    """
    Baseclass for Scope-specific field cache objects that are based on
    single-row-per-field Django ORM objects.
    """
    __metaclass__ = ABCMeta

    def __init__(self):
        self._cache = {}

    def cache_fields(self, fields, xblocks, aside_types):
        """
Beispiel #21
0
        for beta in betas:
            q = self.center_point(beta)
            res.append(q)
        return res

    @memoized_reset
    def lane_profile(self, points_per_segment: int = 5) -> List[SE2value]:
        points_left = []
        points_right = []
        n = len(self.control_points) - 1
        num = n * points_per_segment
        betas = np.linspace(0, n, num=num)
        for beta in betas:
            q = self.center_point(beta)
            delta_left = np.array([0, self.width / 2])
            delta_right = np.array([0, -self.width / 2])
            points_left.append(SE2_apply_R2(q, delta_left))
            points_right.append(SE2_apply_R2(q, delta_right))

        return points_right + list(reversed(points_left))


new_contract("LaneSegment", LaneSegment)


def get_distance_two(q0: SE2value, q1: SE2value) -> float:
    g = geo.SE2.multiply(geo.SE2.inverse(q0), q1)
    v = geo.SE2.algebra_from_group(g)
    linear, angular = geo.linear_angular_from_se2(v)
    return np.linalg.norm(linear)
Beispiel #22
0
from . import DiffeoLibrary
import numpy as np
from contracts import new_contract, contract
import functools

__all__ = ['diffeo_torus', 'diffeo_torus_reflection']

new_contract('coordinates_can', 'array[2x...],array(>=-1,<=1),shape(x)')
new_contract('coordinates_tuple', 'seq[2](>=-1,<=+1)')
new_contract('coordinates', 'coordinates_can|coordinates_tuple')


def mod1d(x):
    ''' bounds in [-1,1] '''
    return np.fmod(np.fmod(x + 1, 2) + 2, 2) - 1


@contract(X='seq[2](float)|array[2x...]', returns='coordinates_can')
def mod_toroidal(X):
    ''' Bounds in [-1,+1]x[-1,+1] '''
    if len(X) == 0:
        raise ValueError('Invalid value %s' % X)
    return np.array([mod1d(X[0]), mod1d(X[1])])


def diffeo_torus_reflection(f):
    """ Declares this is a diffeomorphis which is its own inverse. """
    def f_inv(X):
        return f(X)

    f_inv.__name__ = '%s_inv' % f.__name__
Beispiel #23
0
    from queue import Queue

from celery import Celery
import redis
import requests
from contracts import contract, new_contract

from snowplow_tracker.self_describing_json import SelfDescribingJson

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

DEFAULT_MAX_LENGTH = 10
PAYLOAD_DATA_SCHEMA = "iglu:com.snowplowanalytics.snowplow/payload_data/jsonschema/1-0-4"

new_contract("protocol", lambda x: x == "http" or x == "https")

new_contract("method", lambda x: x == "get" or x == "post")

new_contract("function", lambda x: hasattr(x, "__call__"))

new_contract("redis", lambda x: isinstance(x,
                                           (redis.Redis, redis.StrictRedis)))

try:
    # Check whether a custom Celery configuration module named "snowplow_celery_config" exists
    import snowplow_celery_config
    app = Celery()
    app.config_from_object(snowplow_celery_config)
except ImportError:
    # Otherwise configure Celery with default settings
#-*- coding: utf-8 -*-
#
# Created on Feb 28, 2013
#
# @author: Younes JAAIDI
#
# $Id: f89c74662c1a5a3d3d43f8247a030f54eb04df7a $
#

from .i_member_delegate import IMemberDelegate
from .i_naming_convention import INamingConvention
from contracts import new_contract

new_contract('INamingConvention', INamingConvention)


class AccessorDelegate(IMemberDelegate):

    _GETTER_KEY = 'getter'
    _SETTER_KEY = 'setter'

    # Mappings between accessor types and their names and methods.
    # @hack: I don't much like that.
    _NAMING_CONVENTION_ACCESSOR_NAME_METHOD_DICT = {
        _GETTER_KEY: 'getterName',
        _SETTER_KEY: 'setterName'
    }

    def __init__(self, namingConvention, getterName, setterName):
        """
    :type namingConvention: INamingConvention
Beispiel #25
0
    def delete(self, key):
        self._raise_unless_scope_is_allowed(key)
        self._field_data_cache.delete(key)

    def has(self, key):
        self._raise_unless_scope_is_allowed(key)
        return self._field_data_cache.has(key)

    def _raise_unless_scope_is_allowed(self, key):
        """Raise an InvalidScopeError if key.scope is not in self._allowed_scopes."""
        if key.scope not in self._allowed_scopes:
            raise InvalidScopeError(key, self._allowed_scopes)


new_contract("DjangoKeyValueStore", DjangoKeyValueStore)
new_contract("DjangoKeyValueStore_Key", DjangoKeyValueStore.Key)


class DjangoOrmFieldCache(object):
    """
    Baseclass for Scope-specific field cache objects that are based on
    single-row-per-field Django ORM objects.
    """
    __metaclass__ = ABCMeta

    def __init__(self):
        self._cache = {}

    def cache_fields(self, fields, xblocks, aside_types):
        """
Beispiel #26
0
# -*- coding: utf-8 -*-
#
# (c) 2013-2014 Wishtack
#
# $Id: 39c66831a985f0a7aacda487a89ac6d2762f6279 $
#
from contracts import contract, new_contract

from synthetic import synthesize_member, synthesize_constructor
from tastypie.bundle import Bundle
from tastypie.resources import Resource

new_contract('Bundle', Bundle)
new_contract('Resource', Resource)


@synthesize_member('apply_to', contract='str|None', default=None)
@synthesize_constructor()
class AbstractCondition(object):
    @contract
    def check(self, bundle, resource):
        """
        :type bundle: Bundle
        :type resource: Resource
        :return: Boolean result of condition check.
        """

        return self._check(bundle=bundle, resource=resource)

    def _object_to_compare(self, bundle, resource):
Beispiel #27
0
from .block_config import resolve_config
from .block_meta import BlockMeta, BlockMetaSugar
from .block_sugar import InputProxy, OutputProxy, StateProxy, ConfigProxy
from .exceptions import BlockWriterError, ModelWriterError, ModelExecutionError


__all__ = [
    'Block',
    'NOT_READY',
    'Generator',
]


NOT_READY = None

new_contract('num_or_id', 'int|str')

class Block(BlockMetaSugar):
    __metaclass__ = BlockMeta

    def __init__(self, name, config, library):  # @UnusedVariable
        assert isinstance(name, str), \
            'The block name must be a string, not %s' % name.__class__

        self.name = name

        list_of_config = self.__class__.config
        self.__config = resolve_config(list_of_config, config, None)

        # this is an array containing the names/ids
        # example: ["y", 1, 2]
Beispiel #28
0
from modsecurity_exception_factory.correlation.correlation_engine import \
    CorrelationEngine
from modsecurity_exception_factory.correlation.correlation_progress_listener_console import \
    CorrelationProgressListenerConsole
from modsecurity_exception_factory.modsecurity_audit_data_source import \
    IModsecurityAuditDataSource, ModsecurityAuditDataSourceSQL
from modsecurity_exception_factory.modsecurity_audit_log_parser import \
    ModsecurityAuditLogParser
from modsecurity_exception_factory.utils import Config
import argparse
import contracts
import io
import sys
from modsecurity_exception_factory.modsecurity_exception_writer import ModsecurityExceptionWriter

new_contract('IModsecurityAuditDataSource', IModsecurityAuditDataSource)


class CommandModsecurityExceptionFactory:
    def main(self, argumentList):
        # Disabling contracts solves some performance issues.
        contracts.disable_all()

        argumentParser = argparse.ArgumentParser(
            description=u"Make ModSecurity exceptions.")
        argumentParser.add_argument(
            u"-i",
            u"--input",
            metavar=u"MODSEC_AUDIT_LOG_FILE",
            dest='modsecurityAuditLogPath',
            type=unicode,
from xblock.fields import ScopeIds
from opaque_keys.edx.locator import BlockUsageLocator, LocalId, CourseLocator, LibraryLocator, DefinitionLocator
from xmodule.mako_module import MakoDescriptorSystem
from xmodule.error_module import ErrorDescriptor
from xmodule.errortracker import exc_info_to_str
from xmodule.modulestore.edit_info import EditInfoRuntimeMixin
from xmodule.modulestore.exceptions import ItemNotFoundError
from xmodule.modulestore.inheritance import inheriting_field_data, InheritanceMixin
from xmodule.modulestore.split_mongo import BlockKey, CourseEnvelope
from xmodule.modulestore.split_mongo.id_manager import SplitMongoIdManager
from xmodule.modulestore.split_mongo.definition_lazy_loader import DefinitionLazyLoader
from xmodule.modulestore.split_mongo.split_mongo_kvs import SplitMongoKVS

log = logging.getLogger(__name__)

new_contract('BlockUsageLocator', BlockUsageLocator)
new_contract('CourseLocator', CourseLocator)
new_contract('LibraryLocator', LibraryLocator)
new_contract('BlockKey', BlockKey)
new_contract('CourseEnvelope', CourseEnvelope)


class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
    """
    A system that has a cache of a course version's json that it will use to load modules
    from, with a backup of calling to the underlying modulestore for more data.

    Computes the settings (nee 'metadata') inheritance upon creation.
    """
    @contract(course_entry=CourseEnvelope)
    def __init__(self, modulestore, course_entry, default_class, module_data, lazy, **kwargs):
Beispiel #30
0
triharmonic operators in 1D and 2D.
"""

import numpy as np
from scipy.ndimage.filters import convolve

# https://andreacensi.github.io/contracts/
from contracts import contract, new_contract


@contract(x='int')
def odd_integer_contract(x):
    return x % 2 == 1


new_contract('odd', odd_integer_contract)


@contract(name='str', stencil_size='int,>0,odd|None')
def operator(name, stencil_size=None):
    """ Return 1 or 2 dimensional central finite difference stencil
    for laplace, biharmonic and triharmonic operators.

    >>> print(operator("laplace_1d"))
    [ 1 -2  1]

    >>> print(operator("biharmonic_1d"))
    [ 1 -4  6 -4  1]

    >>> print(operator("triharmonic_1d"))
    [  1  -6  15 -20  15  -6   1]
Beispiel #31
0
#
#   RateItSeven is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   RateItSeven is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with RateItSeven. If not, see <http://www.gnu.org/licenses/>.
#
from contracts import new_contract
from requests.cookies import RequestsCookieJar
from synthetic import synthesize_constructor
from synthetic import synthesize_property

new_contract("RequestsCookieJar", RequestsCookieJar)


@synthesize_constructor()
@synthesize_property('session_cookies', contract="RequestsCookieJar|None")
@synthesize_property('email', contract=str)
@synthesize_property('password', contract=str)
@synthesize_property('username', contract=str)
class User(object):
    def __init__(self):
        pass
class Tracker:

    new_contract("not_none", lambda s: s is not None)

    new_contract("non_empty_string",
                 lambda s: isinstance(s, six.string_types) and len(s) > 0)
    new_contract(
        "string_or_none", lambda s:
        (isinstance(s, six.string_types) and len(s) > 0) or s is None)
    new_contract("payload", lambda s: isinstance(s, payload.Payload))

    new_contract("tracker", lambda s: isinstance(s, Tracker))

    new_contract("emitter", lambda s: hasattr(s, "input"))

    new_contract("self_describing_json",
                 lambda s: isinstance(s, SelfDescribingJson))

    new_contract("context_array", "list(self_describing_json)")

    new_contract("form_node_name", lambda s: s in FORM_NODE_NAMES)

    new_contract("form_type", lambda s: s.lower() in FORM_TYPES)

    new_contract("timestamp", lambda x: (isinstance(x, Timestamp)))

    new_contract("form_element", lambda x: Tracker.check_form_element(x))

    @contract
    def __init__(self,
                 emitters,
                 subject=None,
                 namespace=None,
                 app_id=None,
                 encode_base64=DEFAULT_ENCODE_BASE64):
        """
            :param emitters:         Emitters to which events will be sent
            :type  emitters:         list[>0](emitter) | emitter
            :param subject:          Subject to be tracked
            :type  subject:          subject | None
            :param namespace:        Identifier for the Tracker instance
            :type  namespace:        string_or_none
            :param app_id:           Application ID
            :type  app_id:           string_or_none
            :param encode_base64:    Whether JSONs in the payload should be base-64 encoded
            :type  encode_base64:    bool
        """
        if subject is None:
            subject = _subject.Subject()

        if type(emitters) is list:
            self.emitters = emitters
        else:
            self.emitters = [emitters]

        self.subject = subject
        self.encode_base64 = encode_base64

        self.standard_nv_pairs = {
            "tv": VERSION,
            "tna": namespace,
            "aid": app_id
        }
        self.timer = None

    @staticmethod
    @contract
    def get_uuid():
        """
            Set transaction ID for the payload once during the lifetime of the
            event.

            :rtype:           string
        """
        return str(uuid.uuid4())

    @staticmethod
    @contract
    def get_timestamp(tstamp=None):
        """
            :param tstamp:    User-input timestamp or None
            :type  tstamp:    int | float | None
            :rtype:           int
        """
        if tstamp is None:
            return int(time.time() * 1000)
        elif isinstance(tstamp, (
                int,
                float,
        )):
            return int(tstamp)

    """
    Tracking methods
    """

    @contract
    def track(self, pb):
        """
            Send the payload to a emitter

            :param  pb:              Payload builder
            :type   pb:              payload
            :rtype:                  tracker
        """
        for emitter in self.emitters:
            emitter.input(pb.nv_pairs)
        return self

    @contract
    def complete_payload(self, pb, context, tstamp):
        """
            Called by all tracking events to add the standard name-value pairs
            to the Payload object irrespective of the tracked event.

            :param  pb:              Payload builder
            :type   pb:              payload
            :param  context:         Custom context for the event
            :type   context:         context_array | None
            :param  tstamp:          Optional user-provided timestamp for the event
            :type   tstamp:          timestamp | int | float | None
            :rtype:                  tracker
        """
        pb.add("eid", Tracker.get_uuid())

        if isinstance(tstamp, TrueTimestamp):
            pb.add("ttm", tstamp.value)
        if isinstance(tstamp, DeviceTimestamp):
            pb.add("dtm", Tracker.get_timestamp(tstamp.value))
        elif isinstance(tstamp, (int, float, type(None))):
            pb.add("dtm", Tracker.get_timestamp(tstamp))

        if context is not None:
            context_jsons = list(map(lambda c: c.to_json(), context))
            context_envelope = SelfDescribingJson(CONTEXT_SCHEMA,
                                                  context_jsons).to_json()
            pb.add_json(context_envelope, self.encode_base64, "cx", "co")

        pb.add_dict(self.standard_nv_pairs)

        pb.add_dict(self.subject.standard_nv_pairs)

        return self.track(pb)

    @contract
    def track_page_view(self,
                        page_url,
                        page_title=None,
                        referrer=None,
                        context=None,
                        tstamp=None):
        """
            :param  page_url:       URL of the viewed page
            :type   page_url:       non_empty_string
            :param  page_title:     Title of the viewed page
            :type   page_title:     string_or_none
            :param  referrer:       Referrer of the page
            :type   referrer:       string_or_none
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :param  tstamp:         Optional user-provided timestamp for the event
            :type   tstamp:         timestamp | int | float | None
            :rtype:                 tracker
        """
        pb = payload.Payload()
        pb.add("e", "pv")  # pv: page view
        pb.add("url", page_url)
        pb.add("page", page_title)
        pb.add("refr", referrer)

        return self.complete_payload(pb, context, tstamp)

    @contract
    def track_page_ping(self,
                        page_url,
                        page_title=None,
                        referrer=None,
                        min_x=None,
                        max_x=None,
                        min_y=None,
                        max_y=None,
                        context=None,
                        tstamp=None):
        """
            :param  page_url:       URL of the viewed page
            :type   page_url:       non_empty_string
            :param  page_title:     Title of the viewed page
            :type   page_title:     string_or_none
            :param  referrer:       Referrer of the page
            :type   referrer:       string_or_none
            :param  min_x:          Minimum page x offset seen in the last ping period
            :type   min_x:          int | None
            :param  max_x:          Maximum page x offset seen in the last ping period
            :type   max_x:          int | None
            :param  min_y:          Minimum page y offset seen in the last ping period
            :type   min_y:          int | None
            :param  max_y:          Maximum page y offset seen in the last ping period
            :type   max_y:          int | None
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :param  tstamp:         Optional user-provided timestamp for the event
            :type   tstamp:         timestamp | int | float | None
            :rtype:                 tracker
        """
        pb = payload.Payload()
        pb.add("e", "pp")  # pp: page ping
        pb.add("url", page_url)
        pb.add("page", page_title)
        pb.add("refr", referrer)
        pb.add("pp_mix", min_x)
        pb.add("pp_max", max_x)
        pb.add("pp_miy", min_y)
        pb.add("pp_may", max_y)

        return self.complete_payload(pb, context, tstamp)

    @contract
    def track_link_click(self,
                         target_url,
                         element_id=None,
                         element_classes=None,
                         element_target=None,
                         element_content=None,
                         context=None,
                         tstamp=None):
        """
            :param  target_url:     Target URL of the link
            :type   target_url:     non_empty_string
            :param  element_id:     ID attribute of the HTML element
            :type   element_id:     string_or_none
            :param  element_classes:    Classes of the HTML element
            :type   element_classes:    list(str) | tuple(str,*) | None
            :param  element_content:    The content of the HTML element
            :type   element_content:    string_or_none
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :param  tstamp:         Optional user-provided timestamp for the event
            :type   tstamp:         timestamp | int | float | None
            :rtype:                 tracker
        """
        properties = {}
        properties["targetUrl"] = target_url
        if element_id is not None:
            properties["elementId"] = element_id
        if element_classes is not None:
            properties["elementClasses"] = element_classes
        if element_target is not None:
            properties["elementTarget"] = element_target
        if element_content is not None:
            properties["elementContent"] = element_content

        event_json = SelfDescribingJson(
            "%s/link_click/%s/1-0-1" % (BASE_SCHEMA_PATH, SCHEMA_TAG),
            properties)

        return self.track_unstruct_event(event_json, context, tstamp)

    @contract
    def track_add_to_cart(self,
                          sku,
                          quantity,
                          name=None,
                          category=None,
                          unit_price=None,
                          currency=None,
                          context=None,
                          tstamp=None):
        """
            :param  sku:            Item SKU or ID
            :type   sku:            non_empty_string
            :param  quantity:       Number added to cart
            :type   quantity:       int
            :param  name:           Item's name
            :type   name:           string_or_none
            :param  category:       Item's category
            :type   category:       string_or_none
            :param  unit_price:     Item's price
            :type   unit_price:     int | float | None
            :param  currency:       Type of currency the price is in
            :type   currency:       string_or_none
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :param  tstamp:         Optional user-provided timestamp for the event
            :type   tstamp:         timestamp | int | float | None
            :rtype:                 tracker
        """
        properties = {}
        properties["sku"] = sku
        properties["quantity"] = quantity
        if name is not None:
            properties["name"] = name
        if category is not None:
            properties["category"] = category
        if unit_price is not None:
            properties["unitPrice"] = unit_price
        if currency is not None:
            properties["currency"] = currency

        event_json = SelfDescribingJson(
            "%s/add_to_cart/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG),
            properties)

        return self.track_unstruct_event(event_json, context, tstamp)

    @contract
    def track_remove_from_cart(self,
                               sku,
                               quantity,
                               name=None,
                               category=None,
                               unit_price=None,
                               currency=None,
                               context=None,
                               tstamp=None):
        """
            :param  sku:            Item SKU or ID
            :type   sku:            non_empty_string
            :param  quantity:       Number added to cart
            :type   quantity:       int
            :param  name:           Item's name
            :type   name:           string_or_none
            :param  category:       Item's category
            :type   category:       string_or_none
            :param  unit_price:     Item's price
            :type   unit_price:     int | float | None
            :param  currency:       Type of currency the price is in
            :type   currency:       string_or_none
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :param  tstamp:         Optional user-provided timestamp for the event
            :type   tstamp:         timestamp | int | float | None
            :rtype:                 tracker
        """
        properties = {}
        properties["sku"] = sku
        properties["quantity"] = quantity
        if name is not None:
            properties["name"] = name
        if category is not None:
            properties["category"] = category
        if unit_price is not None:
            properties["unitPrice"] = unit_price
        if currency is not None:
            properties["currency"] = currency

        event_json = SelfDescribingJson(
            "%s/remove_from_cart/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG),
            properties)

        return self.track_unstruct_event(event_json, context, tstamp)

    @contract
    def track_form_change(self,
                          form_id,
                          element_id,
                          node_name,
                          value,
                          type_=None,
                          element_classes=None,
                          context=None,
                          tstamp=None):
        """
        :param  form_id:        ID attribute of the HTML form
        :type   form_id:        non_empty_string
        :param  element_id:     ID attribute of the HTML element
        :type   element_id:     string_or_none
        :param  node_name:      Type of input element
        :type   node_name:      form_node_name
        :param  value:          Value of the input element
        :type   value:          string_or_none
        :param  type_:          Type of data the element represents
        :type   type_:          non_empty_string, form_type
        :param  element_classes:    Classes of the HTML element
        :type   element_classes:    list(str) | tuple(str,*) | None
        :param  context:        Custom context for the event
        :type   context:        context_array | None
        :param  tstamp:         Optional user-provided timestamp for the event
        :type   tstamp:         timestamp | int | float | None
        :rtype:                 tracker
        """
        properties = dict()
        properties["formId"] = form_id
        properties["elementId"] = element_id
        properties["nodeName"] = node_name
        properties["value"] = value
        if type_ is not None:
            properties["type"] = type_
        if element_classes is not None:
            properties["elementClasses"] = element_classes

        event_json = SelfDescribingJson(
            "%s/change_form/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG),
            properties)

        return self.track_unstruct_event(event_json, context, tstamp)

    @contract
    def track_form_submit(self,
                          form_id,
                          form_classes=None,
                          elements=None,
                          context=None,
                          tstamp=None):
        """
            :param  form_id:        ID attribute of the HTML form
            :type   form_id:        non_empty_string
            :param  form_classes:   Classes of the HTML form
            :type   form_classes:   list(str) | tuple(str,*) | None
            :param  elements:       Classes of the HTML form
            :type   elements:       list(form_element) | None
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :param  tstamp:         Optional user-provided timestamp for the event
            :type   tstamp:         timestamp | int | float | None
            :rtype:                 tracker
        """

        properties = dict()
        properties['formId'] = form_id
        if form_classes is not None:
            properties['formClasses'] = form_classes
        if elements is not None and len(elements) > 0:
            properties['elements'] = elements

        event_json = SelfDescribingJson(
            "%s/submit_form/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG),
            properties)

        return self.track_unstruct_event(event_json, context, tstamp)

    @contract
    def track_site_search(self,
                          terms,
                          filters=None,
                          total_results=None,
                          page_results=None,
                          context=None,
                          tstamp=None):
        """
            :param  terms:          Search terms
            :type   terms:          seq[>=1](str)
            :param  filters:        Filters applied to the search
            :type   filters:        dict(str:str|bool) | None
            :param  total_results:  Total number of results returned
            :type   total_results:  int | None
            :param  page_results:   Total number of pages of results
            :type   page_results:   int | None
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :param  tstamp:         Optional user-provided timestamp for the event
            :type   tstamp:         timestamp | int | float | None
            :rtype:                 tracker
        """
        properties = {}
        properties["terms"] = terms
        if filters is not None:
            properties["filters"] = filters
        if total_results is not None:
            properties["totalResults"] = total_results
        if page_results is not None:
            properties["pageResults"] = page_results

        event_json = SelfDescribingJson(
            "%s/site_search/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG),
            properties)

        return self.track_unstruct_event(event_json, context, tstamp)

    @contract
    def track_ecommerce_transaction_item(self,
                                         order_id,
                                         sku,
                                         price,
                                         quantity,
                                         name=None,
                                         category=None,
                                         currency=None,
                                         context=None,
                                         tstamp=None):
        """
            This is an internal method called by track_ecommerce_transaction.
            It is not for public use.

            :param  order_id:    Order ID
            :type   order_id:    non_empty_string
            :param  sku:         Item SKU
            :type   sku:         non_empty_string
            :param  price:       Item price
            :type   price:       int | float
            :param  quantity:    Item quantity
            :type   quantity:    int
            :param  name:        Item name
            :type   name:        string_or_none
            :param  category:    Item category
            :type   category:    string_or_none
            :param  currency:    The currency the price is expressed in
            :type   currency:    string_or_none
            :param  context:     Custom context for the event
            :type   context:     context_array | None
            :rtype:              tracker
        """
        pb = payload.Payload()
        pb.add("e", "ti")
        pb.add("ti_id", order_id)
        pb.add("ti_sk", sku)
        pb.add("ti_nm", name)
        pb.add("ti_ca", category)
        pb.add("ti_pr", price)
        pb.add("ti_qu", quantity)
        pb.add("ti_cu", currency)

        return self.complete_payload(pb, context, tstamp)

    @contract
    def track_ecommerce_transaction(self,
                                    order_id,
                                    total_value,
                                    affiliation=None,
                                    tax_value=None,
                                    shipping=None,
                                    city=None,
                                    state=None,
                                    country=None,
                                    currency=None,
                                    items=None,
                                    context=None,
                                    tstamp=None):
        """
            :param  order_id:       ID of the eCommerce transaction
            :type   order_id:       non_empty_string
            :param  total_value: Total transaction value
            :type   total_value: int | float
            :param  affiliation: Transaction affiliation
            :type   affiliation: string_or_none
            :param  tax_value:   Transaction tax value
            :type   tax_value:   int | float | None
            :param  shipping:    Delivery cost charged
            :type   shipping:    int | float | None
            :param  city:        Delivery address city
            :type   city:        string_or_none
            :param  state:       Delivery address state
            :type   state:       string_or_none
            :param  country:     Delivery address country
            :type   country:     string_or_none
            :param  currency:    The currency the price is expressed in
            :type   currency:    string_or_none
            :param  items:          The items in the transaction
            :type   items:          list(dict(str:*))
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :rtype:                 tracker
        """
        pb = payload.Payload()
        pb.add("e", "tr")
        pb.add("tr_id", order_id)
        pb.add("tr_tt", total_value)
        pb.add("tr_af", affiliation)
        pb.add("tr_tx", tax_value)
        pb.add("tr_sh", shipping)
        pb.add("tr_ci", city)
        pb.add("tr_st", state)
        pb.add("tr_co", country)
        pb.add("tr_cu", currency)

        tstamp = Tracker.get_timestamp(tstamp)

        self.complete_payload(pb, context, tstamp)

        for item in items:
            item["tstamp"] = tstamp
            item["order_id"] = order_id
            item["currency"] = currency
            self.track_ecommerce_transaction_item(**item)

        return self

    @contract
    def track_screen_view(self,
                          name=None,
                          id_=None,
                          context=None,
                          tstamp=None):
        """
            :param  name:           The name of the screen view event
            :type   name:           string_or_none
            :param  id_:            Screen view ID
            :type   id_:            string_or_none
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :rtype:                 tracker
        """
        screen_view_properties = {}
        if name is not None:
            screen_view_properties["name"] = name
        if id_ is not None:
            screen_view_properties["id"] = id_

        event_json = SelfDescribingJson(
            "%s/screen_view/%s/1-0-0" % (BASE_SCHEMA_PATH, SCHEMA_TAG),
            screen_view_properties)

        return self.track_unstruct_event(event_json, context, tstamp)

    @contract
    def track_struct_event(self,
                           category,
                           action,
                           label=None,
                           property_=None,
                           value=None,
                           context=None,
                           tstamp=None):
        """
            :param  category:       Category of the event
            :type   category:       non_empty_string
            :param  action:         The event itself
            :type   action:         non_empty_string
            :param  label:          Refer to the object the action is
                                    performed on
            :type   label:          string_or_none
            :param  property_:      Property associated with either the action
                                    or the object
            :type   property_:      string_or_none
            :param  value:          A value associated with the user action
            :type   value:          int | float | None
            :param  context:        Custom context for the event
            :type   context:        context_array | None
            :rtype:                 tracker
        """
        pb = payload.Payload()
        pb.add("e", "se")
        pb.add("se_ca", category)
        pb.add("se_ac", action)
        pb.add("se_la", label)
        pb.add("se_pr", property_)
        pb.add("se_va", value)

        return self.complete_payload(pb, context, tstamp)

    @contract
    def track_unstruct_event(self, event_json, context=None, tstamp=None):
        """
            :param  event_json:      The properties of the event. Has two field:
                                     A "data" field containing the event properties and
                                     A "schema" field identifying the schema against which the data is validated
            :type   event_json:      self_describing_json
            :param  context:         Custom context for the event
            :type   context:         context_array | None
            :param  tstamp:          User-set timestamp
            :type   tstamp:          timestamp | int | None
            :rtype:                  tracker
        """

        envelope = SelfDescribingJson(UNSTRUCT_EVENT_SCHEMA,
                                      event_json.to_json()).to_json()

        pb = payload.Payload()

        pb.add("e", "ue")
        pb.add_json(envelope, self.encode_base64, "ue_px", "ue_pr")

        return self.complete_payload(pb, context, tstamp)

    # Alias
    track_self_describing_event = track_unstruct_event

    @contract
    def flush(self, asynchronously=False):
        """
            Flush the emitter

            :param  asynchronously:  Whether the flush is done asynchronously. Default is False
            :type   asynchronously:  bool
            :rtype:         tracker
        """
        for emitter in self.emitters:
            if asynchronously:
                emitter.flush()
            else:
                emitter.sync_flush()
        return self

    @contract
    def set_subject(self, subject):
        """
            Set the subject of the events fired by the tracker

            :param subject: Subject to be tracked
            :type  subject: subject | None
            :rtype:         tracker
        """
        self.subject = subject
        return self

    @contract
    def add_emitter(self, emitter):
        """
            Add a new emitter to which events should be passed

            :param emitter: New emitter
            :type  emitter: emitter
            :rtype:         tracker
        """
        self.emitters.append(emitter)
        return self

    @staticmethod
    def check_form_element(element):
        """
            PyContracts helper method to check that dictionary conforms element
            in sumbit_form and change_form schemas
        """
        all_present = isinstance(
            element, dict
        ) and 'name' in element and 'value' in element and 'nodeName' in element
        try:
            if element['type'] in FORM_TYPES:
                type_valid = True
            else:
                type_valid = False
        except KeyError:
            type_valid = True
        return all_present and element[
            'nodeName'] in FORM_NODE_NAMES and type_valid
Beispiel #33
0
"""
A baseclass for a generic client for accessing XBlock Scope.user_state field data.
"""

from abc import abstractmethod
from collections import namedtuple
from datetime import datetime

from contracts import contract, new_contract, ContractsMeta
from opaque_keys.edx.keys import UsageKey, DefinitionKey
from xblock.fields import Scope, ScopeBase

new_contract('UsageKey', UsageKey)
new_contract('DefinitionKey', DefinitionKey)
new_contract('basestring', str)
new_contract('datetime', datetime)
new_contract('block_key', 'UsageKey|DefinitionKey|str|NoneType')


class XBlockUserState(
        namedtuple('_XBlockUserState',
                   ['username', 'block_key', 'state', 'updated', 'scope'])):
    """
    The current state of a single XBlock.

    Arguments:
        username: The username of the user that stored this state.
        block_key: The key identifying the scoped state. Depending on the :class:`~xblock.fields.BlockScope` of

                  ``scope``, this may take one of several types:
from xmodule.errortracker import exc_info_to_str
from xmodule.library_tools import LibraryToolsService
from xmodule.mako_module import MakoDescriptorSystem
from xmodule.modulestore import BlockData
from xmodule.modulestore.edit_info import EditInfoRuntimeMixin
from xmodule.modulestore.exceptions import ItemNotFoundError
from xmodule.modulestore.inheritance import InheritanceMixin, inheriting_field_data
from xmodule.modulestore.split_mongo import BlockKey, CourseEnvelope
from xmodule.modulestore.split_mongo.definition_lazy_loader import DefinitionLazyLoader
from xmodule.modulestore.split_mongo.id_manager import SplitMongoIdManager
from xmodule.modulestore.split_mongo.split_mongo_kvs import SplitMongoKVS
from xmodule.x_module import XModuleMixin

log = logging.getLogger(__name__)

new_contract('BlockUsageLocator', BlockUsageLocator)
new_contract('CourseLocator', CourseLocator)
new_contract('LibraryLocator', LibraryLocator)
new_contract('BlockKey', BlockKey)
new_contract('BlockData', BlockData)
new_contract('CourseEnvelope', CourseEnvelope)
new_contract('XBlock', XBlock)


class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
    """
    A system that has a cache of a course version's json that it will use to load modules
    from, with a backup of calling to the underlying modulestore for more data.

    Computes the settings (nee 'metadata') inheritance upon creation.
    """
Beispiel #35
0
from contracts import contract, new_contract

class Foo(object):
    pass

new_contract('foo', Foo)

@contract(foo_list='list[>1](foo)')
def foo_list_func(foo_list):
    pass
    
# OK
foo_list_func([Foo(), Foo()])

# raises ContractNotRespected
foo_list_func([Foo(), 42])
import itertools
from operator import attrgetter

try:
    import simplejson as json
except ImportError:
    import json

from xblock.fields import Scope, ScopeBase
from xblock_user_state.interface import XBlockUserStateClient
from courseware.models import StudentModule
from contracts import contract, new_contract
from opaque_keys.edx.keys import UsageKey

new_contract('UsageKey', UsageKey)


class DjangoXBlockUserStateClient(XBlockUserStateClient):
    """
    An interface that uses the Django ORM StudentModule as a backend.
    """

    class ServiceUnavailable(XBlockUserStateClient.ServiceUnavailable):
        """
        This error is raised if the service backing this client is currently unavailable.
        """
        pass

    class PermissionDenied(XBlockUserStateClient.PermissionDenied):
        """
try:
    import numpy
except ImportError:
    pass
else:

    import unittest

    from contracts import decorate, new_contract, ContractNotRespected

    new_contract('rgb', 'array[HxWx3],H>0,W>0')
    new_contract('rgba', 'array[HxWx4],H>0,W>0')

    def blend_function(image1, image2, bug=False):
        """
            Blends two RGB or RGBA images together.

             :param image1: The first image to blend.
             :type image1: (rgb|rgba),array[HxWx*]
             :param image2: The second image to blend.
             :type image2: (rgb|rgba),array[HxWx*]
             :param bug: Introduce a bug to check the contracts.
             :type bug: bool

             :return: The blended image.
             :rtype: rgb,array[HxWx3]
        """
        H, W = image1.shape[0], image1.shape[1]

        if bug:
            # if we want to show a bug, return a different shape
from contextlib import contextmanager
from time import time

# Import this just to export it
from pymongo.errors import DuplicateKeyError  # pylint: disable=unused-import
from django.core.cache import get_cache, InvalidCacheBackendError
import dogstats_wrapper as dog_stats_api

from contracts import check, new_contract
from mongodb_proxy import autoretry_read, MongoProxy
from xmodule.exceptions import HeartbeatFailure
from xmodule.modulestore import BlockData
from xmodule.modulestore.split_mongo import BlockKey


new_contract('BlockData', BlockData)


def round_power_2(value):
    """
    Return value rounded up to the nearest power of 2.
    """
    if value == 0:
        return 0

    return math.pow(2, math.ceil(math.log(value, 2)))


class Tagger(object):
    """
    An object used by :class:`QueryTimer` to allow timed code blocks
Beispiel #39
0
from contracts import new_contract

try:
    import numpy
except ImportError:  # pragma: no cover
    pass
else:
    from .numpy_specific import *

from .numbers import *

new_contract('bytes', bytes)
# @author: Younes JAAIDI
#
# $Id: d941a9f394b3536a5c5a66092ca2b9b77e104708 $
#

from ..modsecurity_audit_entry import ModsecurityAuditEntry
from .i_modsecurity_audit_data_source import IModsecurityAuditDataSource
from .modsecurity_audit_item_dict_iterable_sql import ModsecurityAuditItemDictIterableSQL
from .sql_base import SQLBase
from .sql_modsecurity_audit_entry_message import SQLModsecurityAuditEntryMessage
from contextlib import closing
from contracts import contract, new_contract
from sqlalchemy.engine import create_engine
from sqlalchemy.orm.session import sessionmaker

new_contract('ModsecurityAuditEntry', ModsecurityAuditEntry)
new_contract('SQLModsecurityAuditEntryMessage',
             SQLModsecurityAuditEntryMessage)


class ModsecurityAuditDataSourceSQL(IModsecurityAuditDataSource):
    _DATA_INSERTION_BUFFER_SIZE = 100

    @contract
    def __init__(self, dataBaseUrl):
        """
        :type dataBaseUrl: unicode
"""
        self._dataBaseUrl = dataBaseUrl
        self._sqlEngine = create_engine(dataBaseUrl)
        self._sessionMaker = sessionmaker(bind=self._sqlEngine)
# 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 contracts import new_contract

import collections
import datetime
import libvirt
import sqlalchemy
import neat.db

# import novaclient


new_contract("deque", collections.deque)
new_contract("function", lambda x: hasattr(x, "__call__"))
new_contract("datetime", datetime.datetime)
new_contract("virConnect", libvirt.virConnect)
new_contract("virDomain", libvirt.virDomain)
new_contract("Table", sqlalchemy.Table)
new_contract("Database", neat.db.Database)
# new_contract('Client', novaclient.v1_1.client.Client)
Beispiel #42
0
from utils import IncorrectParamValue

# noinspection PyUnresolvedReferences,PyPep8Naming
from diagnostic_context import (
    measure,
    measured,
    measured_total,
    measure_total,
    DEFAULT_CONTEXT_TOTAL as context_total,
)

DELIMITER = '___'
SPECIAL_NAME = 'special_name'
GROWTH_NODE_NAME = 'growth_node'

figures_values_contract = new_contract('figures_values',
                                       'dict(str: dict(str: float))')

number_pattern = re.compile(r'-?[ ]?\d+\.?\d*')

empty_dict = types.MappingProxyType({})


@contract(base_name='str', object_name='str', returns='str')
def compose_full_name(base_name: str, object_name: str) -> str:
    return f'{base_name}{DELIMITER}{object_name}'


@contract(full_name='str', returns='list[2](str)')
def split_full_name(full_name: str) -> (str, str):
    return full_name.split(DELIMITER, maxsplit=2)
# $Id: 788bf39b6c9bfefa5bfe7df5012ba9034e3256f7 $
#

from contextlib import closing
from contracts import contract, new_contract
from modsecurity_exception_factory.correlation.i_item_iterable import IItemIterable
from modsecurity_exception_factory.modsecurity_audit_data_source.sql_filter import SQLFilter
from modsecurity_exception_factory.modsecurity_audit_data_source.sql_filter_condition import SQLFilterCondition
from modsecurity_exception_factory.modsecurity_audit_data_source.sql_modsecurity_audit_entry_message import \
    SQLModsecurityAuditEntryMessage
from sqlalchemy.sql.expression import union, literal, desc, not_, and_, or_
from sqlalchemy.sql.functions import count
from synthetic.decorators import synthesizeMember, synthesizeConstructor
import copy

new_contract('SQLFilterCondition', SQLFilterCondition)

class EmptyVariableNameListError(Exception):
    def __init__(self):
        super(EmptyVariableNameListError, self).__init__(u"Variable name list can't be empty.")

class ModsecurityAuditItemDictIterableSQL(IItemIterable):
    
    _VARIABLE_NAME_KEY = 'variableName'
    _VARIABLE_VALUE_KEY = 'variableValue'
    _VARIABLE_VALUE_COUNT_KEY = 'variableValueCountKey'

    @contract
    def __init__(self, sessionMaker, variableNameList, distinct = False, sqlFilterConditionListDict = None):
        """
    :type variableNameList: list(str)
Beispiel #44
0
from contextlib import contextmanager
from time import time

# Import this just to export it
from pymongo.errors import DuplicateKeyError  # pylint: disable=unused-import
from django.core.cache import get_cache, InvalidCacheBackendError
import dogstats_wrapper as dog_stats_api

from contracts import check, new_contract
from mongodb_proxy import autoretry_read, MongoProxy
from xmodule.exceptions import HeartbeatFailure
from xmodule.modulestore import BlockData
from xmodule.modulestore.split_mongo import BlockKey


new_contract('BlockData', BlockData)


def round_power_2(value):
    """
    Return value rounded up to the nearest power of 2.
    """
    if value == 0:
        return 0

    return math.pow(2, math.ceil(math.log(value, 2)))


class Tagger(object):
    """
    An object used by :class:`QueryTimer` to allow timed code blocks
Beispiel #45
0
def check_valid_code_spec(x):
    if not isinstance(x, list):
        raise BadConfig(x, 'A code spec must be a list.')

    if len(x) != 2:
        msg = 'A code spec must be a list of exactly two elements.'
        raise BadConfig(x, msg)

    name = x[0]
    params = x[1]
    if not isinstance(name, str):
        raise BadConfig(x, 'The code must be given as a string.')
    if not isinstance(params, dict):
        raise BadConfig(x, 'The params must be given as a dictionary.')

new_contract('check_valid_code_spec', check_valid_code_spec)

def check_valid_code_spec_contract(x):
    """ Note that otherwise BadConfig is thrown --- 
    while it should be ValueError for PyContracts. """
    try:
        check_valid_code_spec(x)
    except BadConfig as e:
        raise ValueError(e)

new_contract('code_spec', check_valid_code_spec_contract)

@contract(code_spec='code_spec')
def instantiate_spec(code_spec):
    ''' code_spec must be a sequence  [string, dictionary], giving
        the python function (or class) to instantiate, along
Beispiel #46
0
from contextlib import contextmanager

import six
from contracts import contract, new_contract
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import AssetKey, CourseKey
from opaque_keys.edx.locator import LibraryLocator

from xmodule.assetstore import AssetMetadata

from . import XMODULE_FIELDS_WITH_USAGE_KEYS, ModuleStoreEnum, ModuleStoreWriteBase
from .draft_and_published import ModuleStoreDraftAndPublished
from .exceptions import DuplicateCourseError, ItemNotFoundError
from .split_migrator import SplitMigrator

new_contract('CourseKey', CourseKey)
new_contract('AssetKey', AssetKey)
new_contract('AssetMetadata', AssetMetadata)
new_contract('LibraryLocator', LibraryLocator)
if six.PY2:
    new_contract('long', long)
else:
    new_contract('long', int)

log = logging.getLogger(__name__)


def strip_key(func):
    """
    A decorator for stripping version and branch information from return values that are, or contain, UsageKeys or
    CourseKeys.
Beispiel #47
0
from contracts import contract, new_contract

import numpy as np

from .basic_utils import safe_arccos, normalize_length
from .utils import assert_allclose


@new_contract
@contract(x='array[N],N>0')
def unit_length(x):
    ''' Checks that the value is a 1D vector with unit length in the 2 norm.'''
    assert_allclose(1, np.linalg.norm(x), rtol=1e-5)  # XXX:


new_contract('direction', 'array[3], unit_length')

new_contract('S1', 'array[2],unit_length')
new_contract('S2', 'array[3],unit_length')


@new_contract
@contract(X='array[KxN],K>0,N>0')
def directions(X):
    ''' Checks that every column has unit length. '''
    norm = (X * X).sum(axis=0)
    assert_allclose(1, norm, rtol=1e-5)  # XXX:


@contract(s='array[K],K>=2', v='array[K]')
def assert_orthogonal(s, v):
Beispiel #48
0
# Created on Jan 3, 2013
#
# @author: Younes JAAIDI
#
# $Id: 873bc3172ff50c02ca942865a9a993dad1542849 $
#

from .i_naming_convention import INamingConvention
from .synthetic_comparison_factory import SyntheticComparisonFactory
from .synthetic_constructor_factory import SyntheticConstructorFactory
from .synthetic_member import SyntheticMember
from .synthetic_meta_data import SyntheticMetaData
from contracts import contract, new_contract
import inspect

new_contract('INamingConvention', INamingConvention)
new_contract('SyntheticMember', SyntheticMember)


class SyntheticClassController:
    def __init__(self, cls):
        self._constructorFactory = SyntheticConstructorFactory()
        self._comparisonFactory = SyntheticComparisonFactory()
        self._class = cls

    @contract
    def addSyntheticMember(self, syntheticMember):
        """
    :type syntheticMember: SyntheticMember
"""
        # Inserting this member at the beginning of the member list of synthesization data attribute
Beispiel #49
0
"""
Classes representing asset & asset thumbnail metadata.
"""

from datetime import datetime
import pytz
from contracts import contract, new_contract
from opaque_keys.edx.keys import CourseKey, AssetKey

new_contract('AssetKey', AssetKey)
new_contract('datetime', datetime)
new_contract('basestring', basestring)


class IncorrectAssetIdType(Exception):
    """
    Raised when the asset ID passed-in to create an AssetMetadata or
    AssetThumbnailMetadata is of the wrong type.
    """
    pass


class AssetMetadata(object):
    """
    Stores the metadata associated with a particular course asset. The asset metadata gets stored
    in the modulestore.
    """

    TOP_LEVEL_ATTRS = ['basename', 'internal_name', 'locked', 'contenttype', 'md5']
    EDIT_INFO_ATTRS = ['curr_version', 'prev_version', 'edited_by', 'edited_on']
    ALLOWED_ATTRS = TOP_LEVEL_ATTRS + EDIT_INFO_ATTRS
Beispiel #50
0
#-*- coding: utf-8 -*-
#
# Created on Feb 28, 2013
#
# @author: Younes JAAIDI
#
# $Id: 88c25f7f70d67f765b00f438cc7618eb2a6eb321 $
#

from .exceptions import SyntheticError
from .i_member_delegate import IMemberDelegate
from contracts import contract, new_contract

new_contract('IMemberFactory', IMemberDelegate)


class InvalidPropertyOverrideError(SyntheticError):
    @contract
    def __init__(self, memberName, className):
        """
    :type memberName: str
    :type className: str
"""
        super(InvalidPropertyOverrideError, self).__init__("Member '%s' for class '%s' must be overridden with a property." \
                                                           % (memberName, className))


class PropertyDelegate(IMemberDelegate):

    _KEY_PROPERTY_GET = 'fget'
    _KEY_PROPERTY_SET = 'fset'
#   RateItSeven is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with RateItSeven. If not, see <http://www.gnu.org/licenses/>.
#
import logging

from contracts import contract, new_contract
from lxml import html
from lxml.html import HtmlElement
from requests import Response

new_contract('HtmlElement', HtmlElement)
new_contract('Response', Response)


class ScrapperMixin(object):

    @contract
    def parse_html(self, response):
        """
        :type response: Response
        :rtype: HtmlElement
        """

        try:
            return html.fromstring(response.text)
        except Exception as e:
Beispiel #52
0
"""
Classes representing asset metadata.
"""

from datetime import datetime
import dateutil.parser
import pytz
import json
from contracts import contract, new_contract
from opaque_keys.edx.keys import CourseKey, AssetKey
from lxml import etree

new_contract('AssetKey', AssetKey)
new_contract('CourseKey', CourseKey)
new_contract('datetime', datetime)
new_contract('basestring', basestring)
new_contract('AssetElement',
             lambda x: isinstance(x, etree._Element) and x.tag == "asset")  # pylint: disable=protected-access, no-member
new_contract('AssetsElement',
             lambda x: isinstance(x, etree._Element) and x.tag == "assets")  # pylint: disable=protected-access, no-member


class AssetMetadata(object):
    """
    Stores the metadata associated with a particular course asset. The asset metadata gets stored
    in the modulestore.
    """

    TOP_LEVEL_ATTRS = [
        'pathname', 'internal_name', 'locked', 'contenttype', 'thumbnail',
        'fields'
Beispiel #53
0
import copy
from contracts import contract, new_contract
from xblock.fields import Scope
from collections import namedtuple
from xblock.exceptions import InvalidScopeError
from .definition_lazy_loader import DefinitionLazyLoader
from xmodule.modulestore.inheritance import InheritanceKeyValueStore
from opaque_keys.edx.locator import BlockUsageLocator

# id is a BlockUsageLocator, def_id is the definition's guid
SplitMongoKVSid = namedtuple("SplitMongoKVSid", "id, def_id")
new_contract("BlockUsageLocator", BlockUsageLocator)


class SplitMongoKVS(InheritanceKeyValueStore):
    """
    A KeyValueStore that maps keyed data access to one of the 3 data areas
    known to the MongoModuleStore (data, children, and metadata)
    """

    @contract(parent="BlockUsageLocator | None")
    def __init__(self, definition, initial_values, parent, field_decorator=None):
        """

        :param definition: either a lazyloader or definition id for the definition
        :param initial_values: a dictionary of the locally set values
        """
        # deepcopy so that manipulations of fields does not pollute the source
        super(SplitMongoKVS, self).__init__(copy.deepcopy(initial_values))
        self._definition = definition  # either a DefinitionLazyLoader or the db id of the definition.
        # if the db id, then the definition is presumed to be loaded into _fields
Beispiel #54
0
from contracts import contract, new_contract
from xblock.plugin import default_select

from .exceptions import InvalidLocationError, InsufficientSpecificationError
from xmodule.errortracker import make_error_tracker
from xmodule.assetstore import AssetMetadata
from opaque_keys.edx.keys import CourseKey, UsageKey, AssetKey
from opaque_keys.edx.locations import Location  # For import backwards compatibility
from opaque_keys import InvalidKeyError
from opaque_keys.edx.locations import SlashSeparatedCourseKey
from xblock.runtime import Mixologist
from xblock.core import XBlock

log = logging.getLogger('edx.modulestore')

new_contract('CourseKey', CourseKey)
new_contract('AssetKey', AssetKey)
new_contract('AssetMetadata', AssetMetadata)


class ModuleStoreEnum(object):
    """
    A class to encapsulate common constants that are used with the various modulestores.
    """
    class Type(object):
        """
        The various types of modulestores provided
        """
        split = 'split'
        mongo = 'mongo'
        xml = 'xml'
from bootstrapping_olympics import BootWithInternalLog
from collections import namedtuple
from contracts import contract, new_contract


PureCommandsLast = namedtuple('PureCommandsLast',
                              'y0 y1 delta commands commands_index queue_len')

new_contract('PureCommandsLast', PureCommandsLast)


class PureCommands(BootWithInternalLog):
    """ 
        Converts a stream of observations/commands pairs to
        discrete (y0, y1, commands), provided that the commands
        were held for more than delta. 
        
        Note that in case of ``U U U U``, this will return [U U], [U U U], [U U U...]. 
        unless new_behavior=True is given.
        
    """
    def __init__(self, delta, new_behavior=False):
        """ :param delta: minimum length for commands to be the same """
        self.delta = delta
        self.cmdstr2index = {}
        self.reset()
        self.new_behavior = new_behavior
        
    def reset(self):
        self.q = []
        self.cmd = None
Beispiel #56
0
import warnings

from contracts import contract, new_contract

import numpy as np

from .constants import GeometryConstants

new_contract('R1', 'array[1]')
new_contract('R2', 'array[2]')
new_contract('R3', 'array[3]')

try:

    @new_contract
    @contract(x='array')
    def finite(x):
        # TODO: make into standard thing
        return np.isfinite(x).all()

except:
    pass


@contract(s='array')
def normalize_length(s, norm=2):
    ''' Normalize an array such that it has unit length in the given norm. '''
    sn = np.linalg.norm(s, norm)
    if np.allclose(sn, 0, atol=GeometryConstants.atol_zero_norm):
        raise ValueError('Norm is zero')
    else:
Beispiel #57
0
Phase 1: Checks to see if an asset's metadata can be found in the course's modulestore.
    If not found, fails over to access the asset from the contentstore.
    At first, the asset metadata will never be found, since saving isn't implemented yet.
Note: Hotfix (PLAT-734) No asset calls find_asset_metadata, and directly accesses from contentstore.

"""

from __future__ import absolute_import

from contracts import contract, new_contract
from opaque_keys.edx.keys import AssetKey

from xmodule.contentstore.django import contentstore

new_contract('AssetKey', AssetKey)


class AssetException(Exception):
    """
    Base exception class for all exceptions related to assets.
    """
    pass


class AssetMetadataNotFound(AssetException):
    """
    Thrown when no asset metadata is present in the course modulestore for the particular asset requested.
    """
    pass
Beispiel #58
0
            raise ValueError(msg)
        for k in attrs:
            if k in value:
                msg = (
                    "The same field %r is found in both key and value. \n"
                    "  key: %s \n"
                    "value: %s" % (k, attrs, value)
                )
                raise ValueError(msg)
        super(StoreResultsDict, self).__setitem__(attrs, value)

    def field_or_value_field(self, field):
        """
            Returns all values for field, which can be either in the
            key or in the value dict.
        """
        for k, v in self.items():
            if field in k:
                yield k[field]
            elif field in v:
                yield v[field]
            else:
                msg = (
                    "Could not find value of %r neither in key or value. "
                    "Key: %s Value: %s" % (field, k, v)
                )
                raise ValueError(msg)


new_contract("StoreResultsDict", StoreResultsDict)
import copy
from contracts import contract, new_contract
from xblock.fields import Scope
from collections import namedtuple
from xblock.exceptions import InvalidScopeError
from .definition_lazy_loader import DefinitionLazyLoader
from xmodule.modulestore.inheritance import InheritanceKeyValueStore
from opaque_keys.edx.locator import BlockUsageLocator
from xblock.core import XBlockAside

# id is a BlockUsageLocator, def_id is the definition's guid
SplitMongoKVSid = namedtuple('SplitMongoKVSid', 'id, def_id')
new_contract('BlockUsageLocator', BlockUsageLocator)


class SplitMongoKVS(InheritanceKeyValueStore):
    """
    A KeyValueStore that maps keyed data access to one of the 3 data areas
    known to the MongoModuleStore (data, children, and metadata)
    """

    VALID_SCOPES = (Scope.parent, Scope.children, Scope.settings, Scope.content)

    @contract(parent="BlockUsageLocator | None")
    def __init__(self, definition, initial_values, default_values, parent, aside_fields=None, field_decorator=None):
        """

        :param definition: either a lazyloader or definition id for the definition
        :param initial_values: a dictionary of the locally set values
        :param default_values: any Scope.settings field defaults that are set locally
            (copied from a template block with copy_from_template)
    http://www.apache.org/licenses/LICENSE-2.0.

    Unless required by applicable law or agreed to in writing,
    software distributed under the Apache License Version 2.0 is distributed on
    an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
    express or implied. See the Apache License Version 2.0 for the specific
    language governing permissions and limitations there under.

    Authors: Anuj More, Alex Dean, Fred Blundun, Anton Parkhomenko
    Copyright: Copyright (c) 2013-2014 Snowplow Analytics Ltd
    License: Apache License Version 2.0
"""

from contracts import contract, new_contract

new_contract("ts_type", lambda x: x == "ttm" or x == "dtm")


class Timestamp(object):
    @contract
    def __init__(self, ts_type, value):
        """
            Construct base timestamp type

            :param ts_type: one of possible timestamp types, according to
                            tracker protocol
            :type ts_type:  ts_type
            :param value:   timestamp value in milliseconds
            :type value:    int
        """
        self.ts_type = ts_type