<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import properties
import numpy as np
import unittest
import pickle


class NumPrimitive(properties.HasProperties):
    mycomplex = properties.Complex("its complicated")
    myfloat = properties.Float("something that floats", default=1)
    myint = properties.Integer("an integer")


class BoolPrimitive(properties.HasProperties):
    abool = properties.Bool("True or False", default=True)
    athing = properties.Union("", (
        properties.String("a string"),
        properties.Bool("temp")
    ))


class StrPrimitive(properties.HasProperties):
    anystr = properties.String("a string!")
    stripstr = properties.String("a string!", strip=' ')
    lowerstr = properties.String("a string!", change_case='lower')
    upperstr = properties.String("a string!", change_case='upper')


class StrChoicePrimitive(properties.HasProperties):
    abc = properties.StringChoice("a, b or c", choices=['A', 'B', 'C'])
    vowel = properties.StringChoice("vowels", choices={
        'vowel': ('a', 'e', 'i', 'o', 'u'),
        'maybe': 'y'
    })


class APrimitive(properties.HasProperties):
    opacity = properties.Float(
        "My range",
        min=0.,
        max=1.,
        required=True
    )
    color = properties.Float("Not a color!")


class AnotherPrimitive(properties.HasProperties):
    myrangeint = properties.Integer(
        'int range',
        default=0,
        min=0,
        max=10
    )


class Location2(properties.HasProperties):
    loc = properties.Vector2("My location")
    unit = properties.Vector2("My location", length=1)


class Location3(properties.HasProperties):
    loc = properties.Vector3("My location")
    unit = properties.Vector3("My location", length=1)

    @properties.observe('loc')
    def _on_loc_change(self, change):
        self._last_change = change


class SomeOptions(APrimitive):
    color = properties.Color("My color", default='blue')


class ReqOptions(APrimitive):
    color = properties.Color("My color", required=True)


class ReqDefOptions(APrimitive):
    color = properties.Color("My color", required=True)


class DefaultColorOptions(APrimitive):
    color = properties.Color("This color is random", default='random')


class ThingWithOptions(properties.HasProperties):
    opts = properties.Instance("My options", SomeOptions, auto_create=True)
    opts2 = properties.Instance("My options", SomeOptions, auto_create=True)
    moreopts = properties.List(
        "List of options",
        SomeOptions
    )


class ThingWithDefaults(ThingWithOptions):
    _defaults = dict(
        opts=SomeOptions(),  # this is bad practice, but works!
        opts2=SomeOptions,
        moreopts=lambda: [SomeOptions()]
    )


class ThingWithInheritedDefaults(ThingWithDefaults):
    @properties.defaults
    def _defaults(self):
        return dict(
            opts2=lambda: SomeOptions(color='green'),
        )


class MyArray(properties.HasProperties):
    int_array = properties.Array(
        'some ints',
        shape=('*',),
        dtype=int
    )
    float_array = properties.Array(
        'some floats',
        shape=('*',),
        dtype=float
    )
    flexible_array = properties.Array(
        'some numbers',
        shape=('*',),
        dtype=(float, int)
    )
    int_matrix = properties.Array(
        '3x3x3 matrix',
        shape=(3, 3, 3),
        dtype=int
    )


class TestBasic(unittest.TestCase):

    def test_color(self):

        opts = ReqOptions()
        self.assertRaises(ValueError, opts.validate)

        opts = ReqDefOptions(
            color='red',
            opacity=0
        )
        opts.validate()

        opts = SomeOptions(color='red')

        # Test options
        assert opts.color == (255, 0, 0)
        opts.color = 'darkred'
        assert opts.color == (139, 0, 0)
        opts.color = '#FFF'
        assert opts.color == (255, 255, 255)
        opts.color = [50, 50, 50]
        assert opts.color == (50, 50, 50)
        opts.color = np.r_[50, 50, 50]
        assert opts.color == (50, 50, 50)
        opts.color = 'random'
        assert len(opts.color) == 3
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', 'SunburnRed'))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', '#00112233'))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', '#CDEFGH'))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', 5))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', (1.3, 5.3, 100.6)))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', [5, 100]))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', [-10, 0, 0]))

        opts = DefaultColorOptions()
        assert len(opts.color) == 3

    def test_range(self):

        opts = SomeOptions(opacity=0.3)
        assert opts.opacity == 0.3

        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'opacity', 5))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'opacity', -1))
        self.assertRaises((ValueError, TypeError),
                          lambda: setattr(opts, 'opacity', [.25, .75]))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'opacity', 'See-through'))
<commit_msg>Will work on both python 2/3<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import properties
import numpy as np
import unittest
import pickle


class NumPrimitive(properties.HasProperties):
    mycomplex = properties.Complex("its complicated")
    myfloat = properties.Float("something that floats", default=1)
    myint = properties.Integer("an integer")


class BoolPrimitive(properties.HasProperties):
    abool = properties.Bool("True or False", default=True)
    athing = properties.Union("", (
        properties.String("a string"),
        properties.Bool("temp")
    ))


class StrPrimitive(properties.HasProperties):
    anystr = properties.String("a string!")
    stripstr = properties.String("a string!", strip=' ')
    lowerstr = properties.String("a string!", change_case='lower')
    upperstr = properties.String("a string!", change_case='upper')


class StrChoicePrimitive(properties.HasProperties):
    abc = properties.StringChoice("a, b or c", choices=['A', 'B', 'C'])
    vowel = properties.StringChoice("vowels", choices={
        'vowel': ('a', 'e', 'i', 'o', 'u'),
        'maybe': 'y'
    })


class APrimitive(properties.HasProperties):
    opacity = properties.Float(
        "My range",
        min=0.,
        max=1.,
        required=True
    )
    color = properties.Float("Not a color!")


class AnotherPrimitive(properties.HasProperties):
    myrangeint = properties.Integer(
        'int range',
        default=0,
        min=0,
        max=10
    )


class Location2(properties.HasProperties):
    loc = properties.Vector2("My location")
    unit = properties.Vector2("My location", length=1)


class Location3(properties.HasProperties):
    loc = properties.Vector3("My location")
    unit = properties.Vector3("My location", length=1)

    @properties.observe('loc')
    def _on_loc_change(self, change):
        self._last_change = change


class SomeOptions(APrimitive):
    color = properties.Color("My color", default='blue')


class ReqOptions(APrimitive):
    color = properties.Color("My color", required=True)


class ReqDefOptions(APrimitive):
    color = properties.Color("My color", required=True)


class DefaultColorOptions(APrimitive):
    color = properties.Color("This color is random", default='random')


class ThingWithOptions(properties.HasProperties):
    opts = properties.Instance("My options", SomeOptions, auto_create=True)
    opts2 = properties.Instance("My options", SomeOptions, auto_create=True)
    moreopts = properties.List(
        "List of options",
        SomeOptions
    )


class ThingWithDefaults(ThingWithOptions):
    _defaults = dict(
        opts=SomeOptions(),  # this is bad practice, but works!
        opts2=SomeOptions,
        moreopts=lambda: [SomeOptions()]
    )


class ThingWithInheritedDefaults(ThingWithDefaults):
    @properties.defaults
    def _defaults(self):
        return dict(
            opts2=lambda: SomeOptions(color='green'),
        )


class MyArray(properties.HasProperties):
    int_array = properties.Array(
        'some ints',
        shape=('*',),
        dtype=int
    )
    float_array = properties.Array(
        'some floats',
        shape=('*',),
        dtype=float
    )
    flexible_array = properties.Array(
        'some numbers',
        shape=('*',),
        dtype=(float, int)
    )
    int_matrix = properties.Array(
        '3x3x3 matrix',
        shape=(3, 3, 3),
        dtype=int
    )


class TestBasic(unittest.TestCase):

    def test_color(self):

        opts = ReqOptions()
        self.assertRaises(ValueError, opts.validate)

        opts = ReqDefOptions(
            color='red',
            opacity=0
        )
        opts.validate()

        opts = SomeOptions(color='red')

        # Test options
        assert opts.color == (255, 0, 0)
        opts.color = 'darkred'
        assert opts.color == (139, 0, 0)
        opts.color = '#FFF'
        assert opts.color == (255, 255, 255)
        opts.color = [50, 50, 50]
        assert opts.color == (50, 50, 50)
        opts.color = np.r_[50, 50, 50]
        assert opts.color == (50, 50, 50)
        opts.color = 'random'
        assert len(opts.color) == 3
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', 'SunburnRed'))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', '#00112233'))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', '#CDEFGH'))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', 5))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', (1.3, 5.3, 100.6)))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', [5, 100]))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'color', [-10, 0, 0]))

        opts = DefaultColorOptions()
        assert len(opts.color) == 3

    def test_range(self):

        opts = SomeOptions(opacity=0.3)
        assert opts.opacity == 0.3

        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'opacity', 5))
        self.assertRaises(ValueError,
                          lambda: setattr(opts, 'opacity', -1))
        self.assertRaises((ValueError, TypeError),
                          lambda: setattr(opts, 'opacity', [.25, .75]))
        self.assertRaises((ValueError, TypeError),
                          lambda: setattr(opts, 'opacity', 'See-through'))

{'fraction_non_alphanumeric': 0.12624113475177304, 'fraction_numerical': 0.01879432624113475, 'mean_word_length': 2.5474842767295596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '726008', 'n_tokens_mistral': 3404, 'n_tokens_neox': 3226, 'n_words': 734}