Пример #1
0
def test_hypergauge_hybrid1():
    # hybrid 1: same velocity of `g` and `g.max_gauge`.
    # (suggested by @hybrid0)
    g = Gauge(0, Gauge(1, 5, at=0), at=0)
    g.add_momentum(+1)
    g.max_gauge.add_momentum(+1, since=1)
    assert g.determination == [(0, 0), (1, 1), (5, 5)]
Пример #2
0
    def __init__(self, id, inputHandler, parent, *groups):
        super().__init__(groups)

        # rocket sprite from https://opengameart.org/content/rocket
        self.image = pygame.image.load("assets/cohete_bw.png")
        self.image = pygame.transform.scale(self.image, config.player_size)
        self.image.fill(config.player_color[id], special_flags=pygame.BLEND_MULT)
        self.originalImage = self.image.copy()
        self.rect = self.image.get_rect()

        self.id = id
        self.parent = parent
        self.keymap = config.keymaps[id]
        self.inputHandler = inputHandler

        self.angle = 0
        self.shootDelay = config.shoot_delay
        self.direction = pygame.Vector2(0, -1)
        self.position = pygame.Vector2(config.starting_positions[id])
        self.rect.center = self.position
        self.velocity = pygame.Vector2(0, 0)
        self.acceleration = pygame.Vector2(0, 0)

        self.fuel = config.starting_fuel
        self.fuelBar = Gauge(self.fuel, self)
        self.fuelBar.add(self.groups())
Пример #3
0
def test_case1():
    g = Gauge(0, 5, at=0)
    g.add_momentum(+1)
    g.add_momentum(-2, since=1, until=3)
    g.add_momentum(+1, since=5, until=7)
    assert g.determination == [
        (0, 0), (1, 1), (2, 0), (3, 0), (5, 2), (6.5, 5), (7, 5)]
Пример #4
0
def test_hypergauge_hybrid1():
    # hybrid 1: same velocity of `g` and `g.max_gauge`.
    # (suggested by @hybrid0)
    g = Gauge(0, Gauge(1, 5, at=0), at=0)
    g.add_momentum(+1)
    g.max_gauge.add_momentum(+1, since=1)
    assert g.determination == [(0, 0), (1, 1), (5, 5)]
Пример #5
0
def test_intersection_of_vertical_segment_reversed():
    f = FakeGauge([(0, 0), (1e-309, -1)])
    g = Gauge(-2.5, 0, f, at=-1)
    g.add_momentum(+2)
    g.add_momentum(-1)
    assert \
        round_determination(g.determination, precision=1) == \
        [(-1, -2.5), (0, -0.5), (0.5, 0)]
Пример #6
0
def test_intersection_of_vertical_segment_reversed():
    f = FakeGauge([(0, 0), (1e-309, -1)])
    g = Gauge(-2.5, 0, f, at=-1)
    g.add_momentum(+2)
    g.add_momentum(-1)
    assert \
        round_determination(g.determination, precision=1) == \
        [(-1, -2.5), (0, -0.5), (0.5, 0)]
Пример #7
0
def test_hypergauge_case2():
    g = Gauge(12, 100, at=0)
    g.add_momentum(+1, since=1, until=6)
    g.add_momentum(-1, since=3, until=8)
    g.set_max(Gauge(15, 15, at=0), at=0)
    g.max_gauge.add_momentum(-1, until=4)
    g.max_gauge.add_momentum(+1, since=4, until=6)
    assert g.determination == [
        (0, 12), (1, 12), (2, 13), (3, 12), (4, 11), (6, 11), (8, 9)]
Пример #8
0
def test_over_max_on_hypergauge():
    g = Gauge(1, Gauge(10, 20, at=0), at=0)
    g.max_gauge.add_momentum(+1)
    with pytest.raises(ValueError):
        g.set(20, at=0)
    g.set(20, at=0, outbound=OK)
    assert g.get(at=0) == 20
    g.set(20, at=10)
    assert g.get(at=10) == 20
    assert g.get(at=0) == 20  # past was forgot
Пример #9
0
def test_decr_max_hyper():
    g = Gauge(0, Gauge(10, 100, at=0), at=0)
    g.add_momentum(+2)
    g.add_momentum(-1)
    assert g.base[TIME] == 0
    assert g.get(10) == 10
    g.max_gauge.decr(5, at=10)
    assert g.base[TIME] == 10
    assert g.get(10) == 5
    assert g.get(20) == 5
Пример #10
0
def test_pickle():
    g = Gauge(0, 10, at=0)
    r = Random(17171771)
    for x in range(10000):
        since = r.randrange(1000)
        until = since + 1 + r.randrange(1000)
        g.add_momentum(r.uniform(-10, +10), since=since, until=until)
    data = pickle.dumps(g)
    g2 = pickle.loads(data)
    assert g.determination == g2.determination
Пример #11
0
def test_pickle():
    g = Gauge(0, 10, at=0)
    r = Random(17171771)
    for x in range(10000):
        since = r.randrange(1000)
        until = since + 1 + r.randrange(1000)
        g.add_momentum(r.uniform(-10, +10), since=since, until=until)
    data = pickle.dumps(g)
    g2 = pickle.loads(data)
    assert g.determination == g2.determination
Пример #12
0
class Equilibrium(object):

    gauge = None
    speed = None

    def __init__(self, medium, speed):
        self.speed = speed
        self.medium = medium

    @property
    def medium(self):
        return self._medium

    @medium.setter
    def medium(self, medium, at=None):
        at = now_or(at)
        self._medium = medium
        if self.gauge is None:
            self.gauge = Gauge(medium, medium, medium)
            return
        value = self.gauge.clear_momenta(at)
        if value == medium:
            self.gauge._set_limits(medium, medium, at=at)
            return
        if value < medium:
            self.gauge.set_max(medium, at=at)
            velocity = +self.speed
        elif value > medium:
            self.gauge.set_min(medium, at=at)
            velocity = -self.speed
        self.gauge.add_momentum(velocity, since=at)
Пример #13
0
class Equilibrium(object):

    gauge = None
    speed = None

    def __init__(self, medium, speed):
        self.speed = speed
        self.medium = medium

    @property
    def medium(self):
        return self._medium

    @medium.setter
    def medium(self, medium, at=None):
        at = now_or(at)
        self._medium = medium
        if self.gauge is None:
            self.gauge = Gauge(medium, medium, medium)
            return
        value = self.gauge.clear_momenta(at)
        if value == medium:
            self.gauge._set_limits(medium, medium, at=at)
            return
        if value < medium:
            self.gauge.set_max(medium, at=at)
            velocity = +self.speed
        elif value > medium:
            self.gauge.set_min(medium, at=at)
            velocity = -self.speed
        self.gauge.add_momentum(velocity, since=at)
Пример #14
0
def test_same_determination():
    g = Gauge(10, 100, at=0)
    g.add_momentum(+1, since=5, until=10)
    g.add_momentum(+1, since=20, until=30)
    g.add_momentum(-2, since=50, until=60)
    fg = FrozenGauge(g)
    assert fg.get(0) == g.get(0) == 10
    assert fg.get(10) == g.get(10) == 15
    assert fg.get(30) == g.get(30) == 25
    assert fg.get(60) == g.get(60) == 5
    assert fg.get(100) == g.get(100) == 5
Пример #15
0
def bidir():
    g = Gauge(5, Gauge(10, 10, at=0), Gauge(0, 10, at=0), at=0)
    g.add_momentum(+1, since=0, until=3)
    g.add_momentum(-1, since=3, until=6)
    g.add_momentum(+1, since=6, until=9)
    g.add_momentum(-1, since=9, until=12)
    g.max_gauge.add_momentum(-1, since=0, until=4)
    g.max_gauge.add_momentum(+1, since=6, until=7)
    g.min_gauge.add_momentum(+1, since=1, until=6)
    g.min_gauge.add_momentum(-1, since=6, until=8)
    return g
Пример #16
0
def test_decr_max_before_base_time():
    # decr max earlier than the gauge's base time.
    g = Gauge(0, Gauge(10, 100, at=10), at=5)
    g.add_momentum(+1)
    assert g.determination == [(5, 0), (15, 10)]

    with pytest.raises(ValueError):
        g.max_gauge.decr(5, at=0)
    assert g.determination == [(5, 0), (15, 10)]

    g.max_gauge.incr(10, at=10)
    assert g.determination == [(10, 5), (25, 20)]
Пример #17
0
def test_decr_max_before_base_time():
    # decr max earlier than the gauge's base time.
    g = Gauge(0, Gauge(10, 100, at=10), at=5)
    g.add_momentum(+1)
    assert g.determination == [(5, 0), (15, 10)]

    with pytest.raises(ValueError):
        g.max_gauge.decr(5, at=0)
    assert g.determination == [(5, 0), (15, 10)]

    g.max_gauge.incr(10, at=10)
    assert g.determination == [(10, 5), (25, 20)]
Пример #18
0
def zigzag():
    g = Gauge(1, Gauge(2, 3, 2, at=0), Gauge(1, 1, 0, at=0), at=0)
    for x in range(6):
        g.max_gauge.add_momentum(+1, since=x * 2, until=x * 2 + 1)
        g.max_gauge.add_momentum(-1, since=x * 2 + 1, until=x * 2 + 2)
        g.min_gauge.add_momentum(-1, since=x * 2, until=x * 2 + 1)
        g.min_gauge.add_momentum(+1, since=x * 2 + 1, until=x * 2 + 2)
    for x in range(3):
        t = sum(y * 2 for y in range(x + 1))
        g.add_momentum(+1, since=t, until=t + (x + 1))
        g.add_momentum(-1, since=t + (x + 1), until=t + 2 * (x + 1))
    return g
Пример #19
0
def test_intersection_of_vertical_segment():
    assert 0 != 1e-309
    assert math.isinf(1 / 1e-309)
    f = FakeGauge([(0, 0), (1e-309, 1)])
    assert f.get(0.000000000000000000000) == 0
    assert f.get(0.000000000000000000001) == 1
    g = Gauge(2.5, f, at=-1)
    g.add_momentum(-2)
    g.add_momentum(+1)
    assert \
        round_determination(g.determination, precision=1) == \
        [(-1, 2.5), (0, 0.5), (0.5, 0)]
Пример #20
0
def test_decr_max_normal():
    g = Gauge(0, 10, at=0)
    g.add_momentum(+2)
    g.add_momentum(-1)
    assert g.base[TIME] == 0
    assert g.get(10) == 10
    g.set_max(5, at=10)
    g.set(10, outbound=OK, at=10)
    assert g.base[TIME] == 10
    assert g.get(10) == 10
    assert g.get(15) == 5
    assert g.get(20) == 5
Пример #21
0
def test_intersection_of_vertical_segment():
    assert 0 != 1e-309
    assert math.isinf(1 / 1e-309)
    f = FakeGauge([(0, 0), (1e-309, 1)])
    assert f.get(0.000000000000000000000) == 0
    assert f.get(0.000000000000000000001) == 1
    g = Gauge(2.5, f, at=-1)
    g.add_momentum(-2)
    g.add_momentum(+1)
    assert \
        round_determination(g.determination, precision=1) == \
        [(-1, 2.5), (0, 0.5), (0.5, 0)]
Пример #22
0
def shift_gauge(gauge, delta=0):
    """Adds the given delta to a gauge."""
    if gauge.max_gauge is None:
        max_ = gauge.max_value + delta
    else:
        max_ = shift_gauge(gauge.max_gauge, delta)
    if gauge.min_gauge is None:
        min_ = gauge.min_value + delta
    else:
        min_ = shift_gauge(gauge.min_gauge, delta)
    g = Gauge(gauge.base[VALUE] + delta, max_, min_, gauge.base[TIME])
    for momentum in gauge.momenta:
        g.add_momentum(momentum)
    return g
Пример #23
0
def test_in_range():
    g = Gauge(20, 10, at=0)
    assert not g.in_range(0)
    assert not g.in_range(20)
    g.add_momentum(-1)
    assert not g.in_range(0)
    assert g.in_range(20)
Пример #24
0
def shift_gauge(gauge, delta=0):
    """Adds the given delta to a gauge."""
    if gauge.max_gauge is None:
        max_ = gauge.max_value + delta
    else:
        max_ = shift_gauge(gauge.max_gauge, delta)
    if gauge.min_gauge is None:
        min_ = gauge.min_value + delta
    else:
        min_ = shift_gauge(gauge.min_gauge, delta)
    g = Gauge(gauge.base[VALUE] + delta, max_, min_, gauge.base[TIME])
    for momentum in gauge.momenta:
        g.add_momentum(momentum)
    return g
Пример #25
0
def test_hypergauge_zigzag2():
    g = Gauge(2, Gauge(3, 5, 3, at=0), Gauge(2, 2, 0, at=0), at=0)
    for x in range(5):
        g.max_gauge.add_momentum(+1, since=x * 4, until=x * 4 + 2)
        g.max_gauge.add_momentum(-1, since=x * 4 + 2, until=x * 4 + 4)
        g.min_gauge.add_momentum(-1, since=x * 4, until=x * 4 + 2)
        g.min_gauge.add_momentum(+1, since=x * 4 + 2, until=x * 4 + 4)
    for x in range(4):
        t = sum(y * 2 for y in range(x + 1))
        g.add_momentum(+1, since=t, until=t + (x + 1))
        g.add_momentum(-1, since=t + (x + 1), until=t + 2 * (x + 1))
    assert g.determination == [
        (0, 2), (1, 3), (2, 2), (3.5, 3.5), (4, 3), (6, 1), (8, 3), (9, 4),
        (11.5, 1.5), (12, 2), (14.5, 4.5), (16, 3), (18.5, 0.5), (20, 2)]
Пример #26
0
def test_same_determination():
    g = Gauge(10, 100, at=0)
    g.add_momentum(+1, since=5, until=10)
    g.add_momentum(+1, since=20, until=30)
    g.add_momentum(-2, since=50, until=60)
    fg = FrozenGauge(g)
    assert fg.get(0) == g.get(0) == 10
    assert fg.get(10) == g.get(10) == 15
    assert fg.get(30) == g.get(30) == 25
    assert fg.get(60) == g.get(60) == 5
    assert fg.get(100) == g.get(100) == 5
Пример #27
0
def test_set_range():
    g = Gauge(0, 100, at=0)
    g.add_momentum(+1)
    assert g.determination == [(0, 0), (100, 100)]
    g.set_range(Gauge(100, 100, at=0), Gauge(0, 100, at=0), at=0)
    g.max_gauge.add_momentum(-1, until=40)
    g.min_gauge.add_momentum(+1, until=40)
    assert g.determination == [(0, 0), (60, 60)]
    g.clear_momenta(at=30)
    g.add_momentum(-1)
    assert g.determination == [(30, 30), (40, 40)]
Пример #28
0
def test_hypergauge_case3():
    g = Gauge(12, 100, at=0)
    g.add_momentum(+1, since=1, until=6)
    g.add_momentum(-1, since=3, until=8)
    g.set_max(10, at=0)
    g.set(12, outbound=OK, at=0)
    assert g.determination == [
        (0, 12), (1, 12), (3, 12), (5, 10), (6, 10), (8, 8)]
    g.set_max(Gauge(10, 100, at=0), at=0)
    assert g.determination == [
        (0, 12), (1, 12), (3, 12), (5, 10), (6, 10), (8, 8)]
Пример #29
0
def test_case8_simple():
    max_ = Gauge(10, 10, at=0)
    max_.add_momentum(-1)

    g = Gauge(10, max_, at=0)

    max_.forget_past(at=2)

    with pytest.raises(ValueError):
        max_.forget_past(at=1)  # forget older past.

    assert g.get(99999) == approx(0)
Пример #30
0
def test_hyper_hypergauge(zigzag, bidir):
    # under zigzag 1
    g = Gauge(1, zigzag, at=0)
    g.add_momentum(+0.5)
    assert round_determination(g.determination, precision=2) == [
        (0, 1), (1.33, 1.67), (2, 1), (4, 2), (5.5, 0.5), (9.5, 2.5),
        (10, 2), (11.5, 0.5), (12.5, 1)]
    # between zigzag 1 ~ bidirectional hyper-gauge
    g = Gauge(3, bidir, zigzag, at=0)
    g.add_momentum(+3, since=0, until=3)
    g.add_momentum(-3, since=3, until=6)
    g.add_momentum(+3, since=6, until=9)
    g.add_momentum(-3, since=9, until=12)
    assert round_determination(g.determination, precision=2) == [
        (0, 3), (1, 6), (2.5, 7.5), (3, 7), (5, 1), (5.5, 0.5), (6, 1),
        (8, 7), (9, 7), (11, 1), (11.5, 0.5), (12, 1)]
Пример #31
0
def test_hypergauge_with_different_base_time():
    g = Gauge(0, Gauge(10, 100, at=100), at=0)
    g.add_momentum(+1)
    assert g.max_gauge.get(0) == 10
    assert g.get(10) == 10
    g = Gauge(0, Gauge(10, 100, at=0), at=100)
    g.add_momentum(+1)
    assert g.max_gauge.get(100) == 10
    assert g.get(110) == 10
Пример #32
0
def test_thin_momenta():
    g = Gauge(0, 100, at=0)
    for x in range(1000):
        g.add_momentum(+1000000000, since=x, until=x + 1e-10)
    assert_all_in_range(g)
    assert g.get(0) == 0
    assert g.get(1001) == 100
    for x, y in zip(range(9999), range(1, 10000)):
        assert 0 <= g.get(x / 10.) <= g.get(y / 10.) <= 100
Пример #33
0
def random_gauge1(random=random, far=10, near=3, until=20):
    # (-far ~ -near) <= g <= (near ~ far)
    g_max = Gauge(random.uniform(near, far), far, near, at=0)
    g_min = Gauge(random.uniform(-far, -near), -near, -far, at=0)
    value = random.uniform(g_min.min_value, g_max.max_value)
    g = Gauge(value, g_max, g_min, at=0)
    for x in range(0, until, 5):
        g_max.add_momentum(random.uniform(-far, +far), since=x, until=x + 5)
    for x in range(0, until, 2):
        g.add_momentum(random.uniform(-far, +far), since=x, until=x + 2)
    for x in range(0, until, 1):
        g_min.add_momentum(random.uniform(-far, +far), since=x, until=x + 1)
    return g
Пример #34
0
 def medium(self, medium, at=None):
     at = now_or(at)
     self._medium = medium
     if self.gauge is None:
         self.gauge = Gauge(medium, medium, medium)
         return
     value = self.gauge.clear_momenta(at)
     if value == medium:
         self.gauge._set_limits(medium, medium, at=at)
         return
     if value < medium:
         self.gauge.set_max(medium, at=at)
         velocity = +self.speed
     elif value > medium:
         self.gauge.set_min(medium, at=at)
         velocity = -self.speed
     self.gauge.add_momentum(velocity, since=at)
Пример #35
0
    def getUI(self):
        """get the ui element"""
        result = Gauge()
        skin = sm.getSkin('gauge', self.asset)
        result.size = sm.getControlSize(skin, self.asset)
        result.file_gauge = os.path.join(skin['path'], skin['gauge'])
        result.file_needle = os.path.join(skin['path'], skin['needle'])
        #self.min = sm.getMinimum('gauge', self.value, self._typeInfo)
        #self.max = sm.getMaximum('gauge', self.value, self._typeInfo)
        self.min = 0                                        #temp fix, gauge needs to be updated so it can handle values better
        self.max = 100
        if self.value:
            result.value = (self.value / (self.max - self.min)) * 100 # need to convert into % cause the gauge can only process from 0 to 100

        self.uiEl = result
        self.prepareUiElement()
        return result
Пример #36
0
def test_repr():
    g = Gauge(0, 10, at=0)
    assert repr(g) == '<Gauge 0.00/10.00>'
    g.set_min(-10, at=0)
    assert repr(g) == '<Gauge 0.00 between -10.00~10.00>'
    g.set_max(Gauge(10, 10), at=0)
    assert repr(g) == '<Gauge 0.00 between -10.00~<Gauge 10.00/10.00>>'
    m = Momentum(+100, since=10, until=20)
    assert repr(m) == '<Momentum +100.00/s 10.00~20.00>'
    m = Momentum(+100, since=10)
    assert repr(m) == '<Momentum +100.00/s 10.00~>'
    m = Momentum(+100, until=20)
    assert repr(m) == '<Momentum +100.00/s ~20.00>'
    h = Horizon(10, 20, 30)
    assert repr(h) == '<Line[HORIZON] 30.00 for 10.00~20.00>'
    r = Ray(10, 20, 30, 40)
    assert repr(r) == '<Line[RAY] 30.00+40.00/s for 10.00~20.00>'
Пример #37
0
def test_pickle_hypergauge():
    # case 1 from :func:`test_hypergauge`.
    g = Gauge(12, 100, at=0)
    g.add_momentum(+1, since=1, until=6)
    g.add_momentum(-1, since=3, until=8)
    g.set_max(Gauge(15, 15, at=0), at=0)
    g.max_gauge.add_momentum(-1, until=5)
    assert g.determination == [
        (0, 12), (1, 12), (2, 13), (3, 12), (5, 10), (6, 10), (8, 8)]
    assert g.max_gauge.determination == [(0, 15), (5, 10)]
    data = pickle.dumps(g)
    g2 = pickle.loads(data)
    assert g2.max_gauge is not None
    assert g2.determination == [
        (0, 12), (1, 12), (2, 13), (3, 12), (5, 10), (6, 10), (8, 8)]
    assert g2.max_gauge.determination == [(0, 15), (5, 10)]
    assert g2 in g2.max_gauge.limited_gauges()
Пример #38
0
def random_gauge2(random=random, far=1000, near=1, until=20):
    # 0 <= g <= (near ~ far)
    g_max = Gauge(random.uniform(near, far), far, near, at=0)
    value = random.uniform(0, g_max.max_value)
    g = Gauge(value, g_max, at=0)
    for x in range(0, until, 5):
        g_max.add_momentum(random.uniform(-far, +far), since=x, until=x + 5)
    for x in range(0, until, 2):
        g.add_momentum(random.uniform(-far, +far), since=x, until=x + 2)
    return g
Пример #39
0
def test_in_range():
    g = Gauge(20, 10, at=0)
    assert not g.in_range(0)
    assert not g.in_range(20)
    g.add_momentum(-1)
    assert not g.in_range(0)
    assert g.in_range(20)
Пример #40
0
def test_limited_gauges():
    max_g = Gauge(10, 100, at=0)
    g = Gauge(0, max_g, at=0)
    assert g in max_g.limited_gauges()
    g.set_max(10, at=0)
    assert g not in max_g.limited_gauges()
    # clear dead links.
    g.set_max(max_g, at=0)
    assert len(max_g.limited_gauges()) == 1
    del g
    # NOTE: Weak references could not be collected by GC immediately in PyPy.
    for x in range(10):
        try:
            assert len(max_g.limited_gauges()) == 0
        except AssertionError:
            continue
        else:
            break
Пример #41
0
def test_make_momentum():
    g = Gauge(0, 10, at=0)
    m = g.add_momentum(+1)
    assert isinstance(m, Momentum)
    with pytest.raises(TypeError):
        g.add_momentum(m, since=1)
    with pytest.raises(TypeError):
        g.add_momentum(m, until=2)
Пример #42
0
def test_clear_momentum_events():
    g = Gauge(0, 10, at=0)
    m = g.add_momentum(+1, since=10, until=20)
    assert list(g.momentum_events()) == \
        [(0, NONE, None), (10, ADD, m), (20, REMOVE, m), (+inf, NONE, None)]
    # assert len(g._events) == 2
    g.remove_momentum(m)
    assert list(g.momentum_events()) == [(0, NONE, None), (+inf, NONE, None)]
Пример #43
0
def zigzag():
    g = Gauge(1, Gauge(2, 3, 2, at=0), Gauge(1, 1, 0, at=0), at=0)
    for x in range(6):
        g.max_gauge.add_momentum(+1, since=x * 2, until=x * 2 + 1)
        g.max_gauge.add_momentum(-1, since=x * 2 + 1, until=x * 2 + 2)
        g.min_gauge.add_momentum(-1, since=x * 2, until=x * 2 + 1)
        g.min_gauge.add_momentum(+1, since=x * 2 + 1, until=x * 2 + 2)
    for x in range(3):
        t = sum(y * 2 for y in range(x + 1))
        g.add_momentum(+1, since=t, until=t + (x + 1))
        g.add_momentum(-1, since=t + (x + 1), until=t + 2 * (x + 1))
    return g
Пример #44
0
def test_hypergauge_case2():
    g = Gauge(12, 100, at=0)
    g.add_momentum(+1, since=1, until=6)
    g.add_momentum(-1, since=3, until=8)
    g.set_max(Gauge(15, 15, at=0), at=0)
    g.max_gauge.add_momentum(-1, until=4)
    g.max_gauge.add_momentum(+1, since=4, until=6)
    assert g.determination == [(0, 12), (1, 12), (2, 13), (3, 12), (4, 11),
                               (6, 11), (8, 9)]
Пример #45
0
def test_thin_momenta():
    g = Gauge(0, 100, at=0)
    for x in range(1000):
        g.add_momentum(+1000000000, since=x, until=x + 1e-10)
    assert_all_in_range(g)
    assert g.get(0) == 0
    assert g.get(1001) == 100
    for x, y in zip(range(9999), range(1, 10000)):
        assert 0 <= g.get(x / 10.) <= g.get(y / 10.) <= 100
Пример #46
0
def test_decr_max_hyper():
    g = Gauge(0, Gauge(10, 100, at=0), at=0)
    g.add_momentum(+2)
    g.add_momentum(-1)
    assert g.base[TIME] == 0
    assert g.get(10) == 10
    g.max_gauge.decr(5, at=10)
    assert g.base[TIME] == 10
    assert g.get(10) == 5
    assert g.get(20) == 5
Пример #47
0
def test_case1():
    g = Gauge(0, 5, at=0)
    g.add_momentum(+1)
    g.add_momentum(-2, since=1, until=3)
    g.add_momentum(+1, since=5, until=7)
    assert g.determination == [(0, 0), (1, 1), (2, 0), (3, 0), (5, 2),
                               (6.5, 5), (7, 5)]
Пример #48
0
def test_hypergauge_zigzag2():
    g = Gauge(2, Gauge(3, 5, 3, at=0), Gauge(2, 2, 0, at=0), at=0)
    for x in range(5):
        g.max_gauge.add_momentum(+1, since=x * 4, until=x * 4 + 2)
        g.max_gauge.add_momentum(-1, since=x * 4 + 2, until=x * 4 + 4)
        g.min_gauge.add_momentum(-1, since=x * 4, until=x * 4 + 2)
        g.min_gauge.add_momentum(+1, since=x * 4 + 2, until=x * 4 + 4)
    for x in range(4):
        t = sum(y * 2 for y in range(x + 1))
        g.add_momentum(+1, since=t, until=t + (x + 1))
        g.add_momentum(-1, since=t + (x + 1), until=t + 2 * (x + 1))
    assert g.determination == [(0, 2), (1, 3), (2, 2), (3.5, 3.5), (4, 3),
                               (6, 1), (8, 3), (9, 4), (11.5, 1.5), (12, 2),
                               (14.5, 4.5), (16, 3), (18.5, 0.5), (20, 2)]
Пример #49
0
def bidir():
    g = Gauge(5, Gauge(10, 10, at=0), Gauge(0, 10, at=0), at=0)
    g.add_momentum(+1, since=0, until=3)
    g.add_momentum(-1, since=3, until=6)
    g.add_momentum(+1, since=6, until=9)
    g.add_momentum(-1, since=9, until=12)
    g.max_gauge.add_momentum(-1, since=0, until=4)
    g.max_gauge.add_momentum(+1, since=6, until=7)
    g.min_gauge.add_momentum(+1, since=1, until=6)
    g.min_gauge.add_momentum(-1, since=6, until=8)
    return g
Пример #50
0
def test_make_momentum():
    g = Gauge(0, 10, at=0)
    m = g.add_momentum(+1)
    assert isinstance(m, Momentum)
    with pytest.raises(TypeError):
        g.add_momentum(m, since=1)
    with pytest.raises(TypeError):
        g.add_momentum(m, until=2)
Пример #51
0
    def build(self):
        num_page = 2
        root = Accordion()
        pages = list()

        for page in range(num_page):
            item = AccordionItem(title=f'Title{page}')
            grid = GridLayout(cols=3, spacing=20, padding=10)
            # with grid.canvas:
            #     Color(0.5,0.6,0.7,0.8)
            #     Rectangle(size=grid.size,pos=grid.pos)
            item.add_widget(grid)
            root.add_widget(item)
            pages.append(grid)
        #!!! this must be get from serial port !!!#
        Buttons_text = list(map(lambda num: 'Button ' + str(num), range(1, 6)))
        Buttons_status0 = [
            'open', 'close', 'deactive', 'open', 'close', 'deactive'
        ]
        Buttons_status = dict()
        for name, status in zip(Buttons_text, Buttons_status0):
            Buttons_status[name] = status
        # predefined constants
        Buttons_color = {
            'open': [random(), random(), random(), 0.9],
            'close': [1, 0, 0, 0.9],
            'deactive': [0.5, 0.5, 0.5, 0.9]
        }

        # add Buttons to page 0
        Buttons = list(
            map(
                lambda name: Button(text=name,
                                    background_color=Buttons_color[
                                        Buttons_status[name]]), Buttons_text))
        # Clock.schedule_interval(Buttons[0]., 1.0 / 60.0)
        # Buttons[0].bind(on_press=print('hello world'))
        _ = list(map(lambda button: pages[0].add_widget(button), Buttons))

        # add Gauges to page 1
        Gauges = list(
            map(lambda gauge: Gauge(value=50, size_gauge=200, size_text=19),
                Buttons_text))
        _ = list(map(lambda gauge: pages[1].add_widget(gauge), Gauges))

        ### defines events
        _ = list(
            map(
                lambda button: button.bind(on_press=lambda button: self.
                                           ErrorWindow()), Buttons))
        return root
Пример #52
0
def test_clear_momentum_events():
    g = Gauge(0, 10, at=0)
    m = g.add_momentum(+1, since=10, until=20)
    assert list(g.momentum_events()) == \
        [(0, NONE, None), (10, ADD, m), (20, REMOVE, m), (+inf, NONE, None)]
    # assert len(g._events) == 2
    g.remove_momentum(m)
    assert list(g.momentum_events()) == [(0, NONE, None), (+inf, NONE, None)]
Пример #53
0
def test_immutability():
    import pytest
    fg = FrozenGauge(Gauge(10, 100, at=0))
    with pytest.raises(AssertionError):
        fg.invalidate()
    with pytest.raises(TypeError):
        fg.incr(10, at=100)
    with pytest.raises(TypeError):
        fg.decr(10, at=100)
    with pytest.raises(TypeError):
        fg.set(10, at=100)
    with pytest.raises(TypeError):
        fg.add_momentum(+1, since=10, until=20)
    with pytest.raises(TypeError):
        fg.remove_momentum(+1, since=10, until=20)
    with pytest.raises(TypeError):
        fg.forget_past(at=10)
Пример #54
0
def test_pickle_hypergauge():
    # case 1 from :func:`test_hypergauge`.
    g = Gauge(12, 100, at=0)
    g.add_momentum(+1, since=1, until=6)
    g.add_momentum(-1, since=3, until=8)
    g.set_max(Gauge(15, 15, at=0), at=0)
    g.max_gauge.add_momentum(-1, until=5)
    assert g.determination == [(0, 12), (1, 12), (2, 13), (3, 12), (5, 10),
                               (6, 10), (8, 8)]
    assert g.max_gauge.determination == [(0, 15), (5, 10)]
    data = pickle.dumps(g)
    g2 = pickle.loads(data)
    assert g2.max_gauge is not None
    assert g2.determination == [(0, 12), (1, 12), (2, 13), (3, 12), (5, 10),
                                (6, 10), (8, 8)]
    assert g2.max_gauge.determination == [(0, 15), (5, 10)]
    assert g2 in g2.max_gauge.limited_gauges()
Пример #55
0
def test_repr():
    g = Gauge(0, 10, at=0)
    assert repr(g) == '<Gauge 0.00/10.00>'
    g.set_min(-10, at=0)
    assert repr(g) == '<Gauge 0.00 between -10.00~10.00>'
    g.set_max(Gauge(10, 10), at=0)
    assert repr(g) == '<Gauge 0.00 between -10.00~<Gauge 10.00/10.00>>'
    m = Momentum(+100, since=10, until=20)
    assert repr(m) == '<Momentum +100.00/s 10.00~20.00>'
    m = Momentum(+100, since=10)
    assert repr(m) == '<Momentum +100.00/s 10.00~>'
    m = Momentum(+100, until=20)
    assert repr(m) == '<Momentum +100.00/s ~20.00>'
    h = Horizon(10, 20, 30)
    assert repr(h) == '<Line[HORIZON] 30.00 for 10.00~20.00>'
    r = Ray(10, 20, 30, 40)
    assert repr(r) == '<Line[RAY] 30.00+40.00/s for 10.00~20.00>'
Пример #56
0
    def getUI(self):
        """get the ui element"""
        result = Gauge()
        skin = sm.getSkin('gauge', self.asset)
        result.size = sm.getControlSize(skin, self.asset)
        result.file_gauge = os.path.join(skin['path'], skin['gauge'])
        result.file_needle = os.path.join(skin['path'], skin['needle'])
        #self.min = sm.getMinimum('gauge', self.value, self._typeInfo)
        #self.max = sm.getMaximum('gauge', self.value, self._typeInfo)
        self.min = 0  #temp fix, gauge needs to be updated so it can handle values better
        self.max = 100
        if self.value:
            result.value = (
                self.value / (self.max - self.min)
            ) * 100  # need to convert into % cause the gauge can only process from 0 to 100

        self.uiEl = result
        self.prepareUiElement()
        return result
Пример #57
0
def g(request):
    length = request.param
    g = Gauge(0, 10, at=0)
    for x in range(length):
        add_random_momentum(g)
    return g
Пример #58
0
import board
from adafruit_clue import clue
import displayio
import time
from gauge import Gauge

display = board.DISPLAY

colour_fade = [
    0x00FF00, 0x00FF00, 0x00FF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00,
    0xFF0000, 0xFF0000, 0xFF0000
]
gauge = Gauge(-10, 10, 120, 120, value_label="x:")
y_gauge = Gauge(-10,
                10,
                120,
                120,
                value_label="y:",
                arc_colour=colour_fade,
                colour_fade=True)

y_gauge.x = 120

group = displayio.Group(scale=1)

group.append(gauge)
group.append(y_gauge)

display.show(group)
board.DISPLAY.auto_refresh = True
while True:
Пример #59
0
def test_hypergauge_past_bugs(zigzag, bidir):
    """Regression testing for hyper-gauge."""
    # just one momentum
    g1 = Gauge(5, Gauge(5, 10, at=0), Gauge(5, 10, at=0), at=0)
    g1.max_gauge.add_momentum(+1)
    g1.min_gauge.add_momentum(-1)
    assert g1.determination == [(0, 5)]
    g1.add_momentum(+0.1, until=100)
    assert g1.determination == [(0, 5), (50, 10), (100, 10)]
    # floating-point inaccuracy problem 1
    g1 = Gauge(3, bidir, zigzag, at=0)
    g1.add_momentum(+6, since=0, until=1)
    g1.add_momentum(-6, since=1, until=2)
    g1.add_momentum(+6, since=2, until=3)
    g1.add_momentum(-6, since=3, until=4)
    g1.add_momentum(+6, since=4, until=5)
    g1.add_momentum(-6, since=5, until=6)
    g1.add_momentum(+6, since=6, until=7)
    g1.add_momentum(-6, since=7, until=8)
    g1.add_momentum(+6, since=8, until=9)
    g1.add_momentum(-6, since=9, until=10)
    g1.add_momentum(+6, since=10, until=11)
    g1.add_momentum(-6, since=11, until=12)
    assert round_determination(g1.determination, precision=2) == [
        (0, 3), (0.4, 5.4), (1, 6), (1.8, 1.2), (2, 1), (3, 7), (3.8, 2.2),
        (4, 2), (4.57, 5.43), (5, 5), (5.71, 0.71), (6, 1), (6.8, 5.8), (7, 6),
        (7.6, 2.4), (8, 2), (8.83, 7), (9, 7), (9.8, 2.2), (10, 2),
        (10.57, 5.43), (11, 5), (11.71, 0.71), (12, 1)
    ]
    # float problem 2
    g2 = Gauge(0, Gauge(1, 1, at=0), at=0)
    for x in range(10):
        g2.add_momentum(+0.1, since=x, until=x + 1)
    g2.max_gauge.add_momentum(-0.1, since=0, until=6)
    g2.max_gauge.add_momentum(+0.5, since=6, until=10)
    assert round(g2.get(5), 1) == 0.5
    assert round(g2.get(6), 1) == 0.4
    assert round(g2.get(7), 1) == 0.5
    assert round(g2.get(8), 1) == 0.6
    assert round(g2.get(9), 1) == 0.7
    assert round(g2.get(10), 1) == 0.8
    # float problem 3
    g3_max_max = Gauge(3, bidir, zigzag, at=0)
    g3_max_max.add_momentum(+6, since=0, until=1)
    g3_max_max.add_momentum(-6, since=1, until=2)
    g3_max_max.add_momentum(+6, since=2, until=3)
    g3_max_max.add_momentum(-6, since=3, until=4)
    g3_max_max.add_momentum(+6, since=4, until=5)
    g3_max_max.add_momentum(-6, since=5, until=6)
    g3_max_max.add_momentum(+6, since=6, until=7)
    g3_max_max.add_momentum(-6, since=7, until=8)
    g3_max_max.add_momentum(+6, since=8, until=9)
    g3_max_max.add_momentum(-6, since=9, until=10)
    g3_max_max.add_momentum(+6, since=10, until=11)
    g3_max_max.add_momentum(-6, since=11, until=12)
    g3_max = Gauge(0, g3_max_max, at=0)
    for x in range(10):
        g3_max.add_momentum(+0.1, since=x)
    r = random.Random(10)
    g3 = Gauge(0, shift_gauge(zigzag, +3), g3_max, at=0)
    for x in range(10):
        g3.add_momentum(r.uniform(-10, 10), since=x, until=x + 1)
    assert round(g3.get(9), 1) == 2.9  # not 2.4133871928
    # bound at first
    g4 = Gauge(0, 10, Gauge(0, 10, at=1), at=0)
    g4.min_gauge.add_momentum(+1, until=11)
    g4.add_momentum(-1, until=10)
    assert g4.get(10) == 9  # not -10
    assert g4.determination == [(0, 0), (1, 0), (10, 9), (11, 10)]
    # floor is dense than ceil
    r = random.Random(2810856076715324514)
    g5 = Gauge(0, shift_gauge(zigzag, +3), g3, at=0)
    for x in range(4):
        g5.add_momentum(r.uniform(-10, 10), since=x, until=x + 1)
    assert round(g5.get(4), 1) == 5.0  # not 11.8
Пример #60
0
                        gauge.set_min(gauge.get_min(at) - 10, at)
                    elif pressed[K_BACKQUOTE]:
                        gauge.forget_past(at=at)
                    elif pressed[K_SPACE]:
                        pdb.set_trace()
                    elif pressed[K_EQUALS]:
                        do = Gauge.incr
                    elif pressed[K_MINUS]:
                        do = Gauge.decr
            # draw gauges
            screen.fill(BASE3)
            surf = None
            if do is not None:
                do(disp.gauge, 10, False, at=at)
            if surf is None:
                surf = disp.render(at)
            left = (screen.get_width() - surf.get_width()) / 2
            screen.blit(surf, (left, 20))
            graph_surf = graph.render(at)
            screen.blit(graph_surf, (left - 50, 60))
            # end of loop
            first = False
    except KeyboardInterrupt:
        pass


if __name__ == '__main__':
    g = Gauge(50, 100)
    g.add_momentum(+1)
    main(g, 5)