<commit_before># coding: utf-8
import datetime

from django.conf import settings
from django.db import models
from django.utils import tzinfo

CHOICES = (
    (1, 'first'),
    (2, 'second'),
)

class Article(models.Model):
    headline = models.CharField(max_length=100, default='Default headline')
    pub_date = models.DateTimeField()
    status = models.IntegerField(blank=True, null=True, choices=CHOICES)
    misc_data = models.CharField(max_length=100, blank=True)
    article_text = models.TextField()

    class Meta:
        ordering = ('pub_date','headline')
        # A utf-8 verbose name (Ångström's Articles) to test they are valid.
        verbose_name = "\xc3\x85ngstr\xc3\xb6m's Articles"

    def __unicode__(self):
        return self.headline

class Movie(models.Model):
    #5218: Test models with non-default primary keys / AutoFields
    movie_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=60)

class Party(models.Model):
    when = models.DateField(null=True)

class Event(models.Model):
    when = models.DateTimeField()

class Department(models.Model):
    id = models.PositiveIntegerField(primary_key=True)
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class Worker(models.Model):
    department = models.ForeignKey(Department)
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class BrokenUnicodeMethod(models.Model):
    name = models.CharField(max_length=7)

    def __unicode__(self):
        # Intentionally broken (trying to insert a unicode value into a str
        # object).
        return 'Názov: %s' % self.name

__test__ = {'API_TESTS': """
(NOTE: Part of the regression test here is merely parsing the model
declaration. The verbose_name, in particular, did not always work.)

An empty choice field should return None for the display name.

>>> from datetime import datetime
>>> a = Article(headline="Look at me!", pub_date=datetime.now())
>>> a.save()
>>> a.get_status_display() is None
True

Empty strings should be returned as Unicode
>>> a2 = Article.objects.get(pk=a.id)
>>> a2.misc_data
u''

# TextFields can hold more than 4000 characters (this was broken in Oracle).
>>> a3 = Article(headline="Really, really big", pub_date=datetime.now())
>>> a3.article_text = "ABCDE" * 1000
>>> a3.save()
>>> a4 = Article.objects.get(pk=a3.id)
>>> len(a4.article_text)
5000

# Regression test for #659
>>> import datetime
>>> p = Party.objects.create(when = datetime.datetime(1999, 12, 31))
>>> p = Party.objects.create(when = datetime.datetime(1998, 12, 31))
>>> p = Party.objects.create(when = datetime.datetime(1999, 1, 1))
>>> [p.when for p in Party.objects.filter(when__month=2)]
[]
>>> [p.when for p in Party.objects.filter(when__month=1)]
[datetime.date(1999, 1, 1)]
>>> [p.when for p in Party.objects.filter(when__month=12)]
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
>>> [p.when for p in Party.objects.filter(when__year=1998)]
[datetime.date(1998, 12, 31)]

# Regression test for #8510
>>> [p.when for p in Party.objects.filter(when__day='31')]
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
>>> [p.when for p in Party.objects.filter(when__month='12')]
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
>>> [p.when for p in Party.objects.filter(when__year='1998')]
[datetime.date(1998, 12, 31)]

# Date filtering was failing with NULL date values in SQLite (regression test
# for #3501, amongst other things).
>>> _ = Party.objects.create()
>>> p = Party.objects.filter(when__month=1)[0]
>>> p.when
datetime.date(1999, 1, 1)
>>> l = Party.objects.filter(pk=p.pk).dates("when", "month")
>>> l[0].month == 1
True

# Check that get_next_by_FIELD and get_previous_by_FIELD don't crash when we
# have usecs values stored on the database
#
# [It crashed after the Field.get_db_prep_* refactor, because on most backends
#  DateTimeFields supports usecs, but DateTimeField.to_python didn't recognize
#  them. (Note that Model._get_next_or_previous_by_FIELD coerces values to
#  strings)]
#
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 16, 0, 0))
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 6, 1, 1))
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 13, 1, 1))
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 12, 0, 20, 24))
>>> e.get_next_by_when().when
datetime.datetime(2000, 1, 1, 13, 1, 1)
>>> e.get_previous_by_when().when
datetime.datetime(2000, 1, 1, 6, 1, 1)

# Check Department and Worker
>>> d = Department(id=10, name='IT')
>>> d.save()
>>> w = Worker(department=d, name='Full-time')
>>> w.save()
>>> w
<Worker: Full-time>

# Models with broken unicode methods should still have a printable repr
>>> b = BrokenUnicodeMethod(name="Jerry")
>>> b.save()
>>> BrokenUnicodeMethod.objects.all()
[<BrokenUnicodeMethod: [Bad Unicode data]>]

"""}

if settings.DATABASE_ENGINE != "mysql":
    __test__["non-mysql-tests"] = """
# Saving an updating with timezone-aware datetime Python objects. Regression
# test for #10443.

# The idea is that all these creations and saving should work without crashing.
# It's not rocket science.
>>> Article.objects.all().delete()
>>> dt1 = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(600))
>>> dt2 = datetime.datetime(2008, 8, 31, 17, 20, tzinfo=tzinfo.FixedOffset(600))
>>> obj = Article.objects.create(headline="A headline", pub_date=dt1, article_text="foo")

>>> obj.pub_date = dt2
>>> obj.save()
>>> Article.objects.filter(headline="A headline").update(pub_date=dt1)
1

"""

<commit_msg>Refs #10443: Added Oracle to the #10443 regression test exemption, since we don't yet support it.<commit_after># coding: utf-8
import datetime

from django.conf import settings
from django.db import models
from django.utils import tzinfo

CHOICES = (
    (1, 'first'),
    (2, 'second'),
)

class Article(models.Model):
    headline = models.CharField(max_length=100, default='Default headline')
    pub_date = models.DateTimeField()
    status = models.IntegerField(blank=True, null=True, choices=CHOICES)
    misc_data = models.CharField(max_length=100, blank=True)
    article_text = models.TextField()

    class Meta:
        ordering = ('pub_date','headline')
        # A utf-8 verbose name (Ångström's Articles) to test they are valid.
        verbose_name = "\xc3\x85ngstr\xc3\xb6m's Articles"

    def __unicode__(self):
        return self.headline

class Movie(models.Model):
    #5218: Test models with non-default primary keys / AutoFields
    movie_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=60)

class Party(models.Model):
    when = models.DateField(null=True)

class Event(models.Model):
    when = models.DateTimeField()

class Department(models.Model):
    id = models.PositiveIntegerField(primary_key=True)
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class Worker(models.Model):
    department = models.ForeignKey(Department)
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class BrokenUnicodeMethod(models.Model):
    name = models.CharField(max_length=7)

    def __unicode__(self):
        # Intentionally broken (trying to insert a unicode value into a str
        # object).
        return 'Názov: %s' % self.name

__test__ = {'API_TESTS': """
(NOTE: Part of the regression test here is merely parsing the model
declaration. The verbose_name, in particular, did not always work.)

An empty choice field should return None for the display name.

>>> from datetime import datetime
>>> a = Article(headline="Look at me!", pub_date=datetime.now())
>>> a.save()
>>> a.get_status_display() is None
True

Empty strings should be returned as Unicode
>>> a2 = Article.objects.get(pk=a.id)
>>> a2.misc_data
u''

# TextFields can hold more than 4000 characters (this was broken in Oracle).
>>> a3 = Article(headline="Really, really big", pub_date=datetime.now())
>>> a3.article_text = "ABCDE" * 1000
>>> a3.save()
>>> a4 = Article.objects.get(pk=a3.id)
>>> len(a4.article_text)
5000

# Regression test for #659
>>> import datetime
>>> p = Party.objects.create(when = datetime.datetime(1999, 12, 31))
>>> p = Party.objects.create(when = datetime.datetime(1998, 12, 31))
>>> p = Party.objects.create(when = datetime.datetime(1999, 1, 1))
>>> [p.when for p in Party.objects.filter(when__month=2)]
[]
>>> [p.when for p in Party.objects.filter(when__month=1)]
[datetime.date(1999, 1, 1)]
>>> [p.when for p in Party.objects.filter(when__month=12)]
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
>>> [p.when for p in Party.objects.filter(when__year=1998)]
[datetime.date(1998, 12, 31)]

# Regression test for #8510
>>> [p.when for p in Party.objects.filter(when__day='31')]
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
>>> [p.when for p in Party.objects.filter(when__month='12')]
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
>>> [p.when for p in Party.objects.filter(when__year='1998')]
[datetime.date(1998, 12, 31)]

# Date filtering was failing with NULL date values in SQLite (regression test
# for #3501, amongst other things).
>>> _ = Party.objects.create()
>>> p = Party.objects.filter(when__month=1)[0]
>>> p.when
datetime.date(1999, 1, 1)
>>> l = Party.objects.filter(pk=p.pk).dates("when", "month")
>>> l[0].month == 1
True

# Check that get_next_by_FIELD and get_previous_by_FIELD don't crash when we
# have usecs values stored on the database
#
# [It crashed after the Field.get_db_prep_* refactor, because on most backends
#  DateTimeFields supports usecs, but DateTimeField.to_python didn't recognize
#  them. (Note that Model._get_next_or_previous_by_FIELD coerces values to
#  strings)]
#
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 16, 0, 0))
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 6, 1, 1))
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 13, 1, 1))
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 12, 0, 20, 24))
>>> e.get_next_by_when().when
datetime.datetime(2000, 1, 1, 13, 1, 1)
>>> e.get_previous_by_when().when
datetime.datetime(2000, 1, 1, 6, 1, 1)

# Check Department and Worker
>>> d = Department(id=10, name='IT')
>>> d.save()
>>> w = Worker(department=d, name='Full-time')
>>> w.save()
>>> w
<Worker: Full-time>

# Models with broken unicode methods should still have a printable repr
>>> b = BrokenUnicodeMethod(name="Jerry")
>>> b.save()
>>> BrokenUnicodeMethod.objects.all()
[<BrokenUnicodeMethod: [Bad Unicode data]>]

"""}

if settings.DATABASE_ENGINE not in ("mysql", "oracle"):
    __test__["timezone-tests"] = """
# Saving an updating with timezone-aware datetime Python objects. Regression
# test for #10443.

# The idea is that all these creations and saving should work without crashing.
# It's not rocket science.
>>> Article.objects.all().delete()
>>> dt1 = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(600))
>>> dt2 = datetime.datetime(2008, 8, 31, 17, 20, tzinfo=tzinfo.FixedOffset(600))
>>> obj = Article.objects.create(headline="A headline", pub_date=dt1, article_text="foo")

>>> obj.pub_date = dt2
>>> obj.save()
>>> Article.objects.filter(headline="A headline").update(pub_date=dt1)
1

"""

<|endoftext|>


<commit_before>import openpnm as op
import scipy as sp
import pytest

class GenericTransportTest:

    def setup_class(self):
        self.net = op.network.Cubic(shape=[9, 9, 9])
        self.geo = op.geometry.StickAndBall(network=self.net,
                                            pores=self.net.Ps,
                                            throats=self.net.Ts)
        self.phase = op.phases.Air(network=self.net)
        self.phase['pore.mole_fraction'] = 0
        self.phys = op.physics.GenericPhysics(network=self.net,
                                              phase=self.phase,
                                              geometry=self.geo)
        self.phys['throat.diffusive_conductance'] = 1.0

    def test_results(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        with pytest.raises(Exception):
            alg.results()

    def test_set_solver(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        # Store old values
        family = alg.settings["solver_family"]
        stype = alg.settings["solver_type"]
        tol = alg.settings["solver_tol"]
        atol = alg.settings["solver_atol"]
        rtol = alg.settings["solver_rtol"]
        # Set solver settings, but don't provide any arguments
        alg.set_solver()
        # Make sure nothing was changed
        assert alg.settings["solver_family"] == family
        assert alg.settings["solver_type"] == stype
        assert alg.settings["solver_tol"] == tol
        assert alg.settings["solver_atol"] == atol
        assert alg.settings["solver_rtol"] == rtol
        # Set solver settings, this time change everything
        alg.set_solver(solver_family="petsc", solver_type="gmres",
                       preconditioner="ilu", tol=1e-3, atol=1e-12, rtol=1e-2)
        # Make changes went through
        assert alg.settings["solver_family"] == "petsc"
        assert alg.settings["solver_type"] == "gmres"
        assert alg.settings["solver_tol"] == 1e-3
        assert alg.settings["solver_atol"] == 1e-12
        assert alg.settings["solver_rtol"] == 1e-2

    def test_remove_boundary_conditions(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.set_value_BC(pores=self.net.pores('top'), values=1)
        alg.set_value_BC(pores=self.net.pores('bottom'), values=0)
        assert sp.sum(sp.isfinite(alg['pore.bc_value'])) > 0
        alg.remove_BC(pores=self.net.pores('top'))
        assert sp.sum(sp.isfinite(alg['pore.bc_value'])) > 0
        alg.remove_BC(pores=self.net.pores('bottom'))
        assert sp.sum(sp.isfinite(alg['pore.bc_value'])) == 0

    def test_generic_transport(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_value_BC(pores=self.net.pores('top'), values=1)
        alg.set_value_BC(pores=self.net.pores('bottom'), values=0)
        alg.run()

    def test_two_value_conditions(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_value_BC(pores=self.net.pores('top'), values=1)
        alg.set_value_BC(pores=self.net.pores('bottom'), values=0)
        alg.run()
        x = [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]
        y = sp.unique(sp.around(alg['pore.mole_fraction'], decimals=3))
        assert sp.all(x == y)

    def test_two_value_conditions_cg(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_value_BC(pores=self.net.pores('top'), values=1)
        alg.set_value_BC(pores=self.net.pores('bottom'), values=0)
        alg.settings['solver'] = 'cg'
        alg.run()
        x = [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]
        y = sp.unique(sp.around(alg['pore.mole_fraction'], decimals=3))
        assert sp.all(x == y)

    def test_one_value_one_rate(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=self.net.pores('bottom'), values=1)
        alg.set_value_BC(pores=self.net.pores('top'), values=0)
        alg.run()
        x = [0., 1., 2., 3., 4., 5., 6., 7., 8.]
        y = sp.unique(sp.around(alg['pore.mole_fraction'], decimals=3))
        assert sp.all(x == y)

    def test_set_iterative_props(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                              phase=self.phase)
        assert len(alg.settings["iterative_props"]) == 0
        alg.set_iterative_props(propnames="pore.pressure")
        assert "pore.pressure" in alg.settings["iterative_props"]
        # Ensure each prop is only added once
        alg.set_iterative_props(propnames="pore.pressure")
        assert len(alg.settings["iterative_props"]) == 1
        alg.set_iterative_props(propnames=["pore.temperature", "pore.pressure"])
        assert len(alg.settings["iterative_props"]) == 2
        assert "pore.pressure" in alg.settings["iterative_props"]
        assert "pore.temperature" in alg.settings["iterative_props"]

    def test_cache_A(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=self.net.pores('bottom'), values=1)
        alg.set_value_BC(pores=self.net.pores('top'), values=0)
        alg.settings["cache_A"] = True
        alg._build_A()
        x = alg._A.mean()
        self.phys["throat.diffusive_conductance"][1] = 50.0
        alg._build_A()
        y = alg._A.mean()
        # When cache_A is True, A is not recomputed, hence x == y
        assert x == y
        alg.settings["cache_A"] = False
        alg._build_A()
        y = alg._A.mean()
        # When cache_A is False, A must be recomputed, hence x!= y
        assert x != y
        # Revert back changes to objects
        self.setup_class()

    def test_rate_single(self):
        alg = op.algorithms.ReactiveTransport(network=self.net,
                                              phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=self.net.pores("left"), values=1.235)
        alg.set_value_BC(pores=self.net.pores("right"), values=0.0)
        alg.run()
        rate = alg.rate(pores=self.net.pores("right"))[0]
        assert sp.isclose(rate, -1.235*self.net.pores("right").size)
        # Net rate must always be zero at steady state conditions
        assert sp.isclose(alg.rate(pores=self.net.Ps), 0.0)

    def test_rate_multiple(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=[0, 1, 2, 3], values=1.235)
        # Note that pore = 0 is assigned two rate values (rate = sum(rates))
        alg.set_rate_BC(pores=[5, 6, 19, 35, 0], values=3.455)
        alg.set_value_BC(pores=[50, 51, 52, 53], values=0.0)
        alg.run()
        rate = alg.rate(pores=[50, 51, 52, 53])[0]
        assert sp.isclose(rate, -(1.235*4 + 3.455*5))   # 4, 5 are number of pores
        # Net rate must always be zero at steady state conditions
        assert sp.isclose(alg.rate(pores=self.net.Ps), 0.0)

    def test_rate_Nt_by_2_conductance(self):
        net = op.network.Cubic(shape=[1, 6, 1])
        geom = op.geometry.StickAndBall(network=net)
        air = op.phases.Air(network=net)
        water = op.phases.Water(network=net)
        m = op.phases.MultiPhase(phases=[air, water], project=net.project)
        m.set_occupancy(phase=air, pores=[0, 1, 2])
        m.set_occupancy(phase=water, pores=[3, 4, 5])
        const = op.models.misc.constant
        K_water_air = 0.5
        m.set_binary_partition_coef(propname="throat.partition_coef",
                                    phases=[water, air],
                                    model=const, value=K_water_air)
        m._set_automatic_throat_occupancy()
        _ = op.physics.Standard(network=net, phase=m, geometry=geom)
        alg = op.algorithms.GenericTransport(network=net, phase=m)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=0, values=1.235)
        alg.set_value_BC(pores=5, values=0.0)
        alg.run()
        rate = alg.rate(pores=5)[0]
        assert sp.isclose(rate, -1.235)
        # Rate at air-water interface throat (#2) must match imposed rate
        rate = alg.rate(throats=2)[0]
        assert sp.isclose(rate, 1.235)
        # Rate at interface pores (#2 @ air-side, #3 @ water-side) must be 0
        rate_air_side = alg.rate(pores=2)[0]
        rate_water_side = alg.rate(pores=3)[0]
        assert sp.isclose(rate_air_side, 0.0)
        assert sp.isclose(rate_water_side, 0.0)
        # Net rate must always be zero at steady state conditions
        assert sp.isclose(alg.rate(pores=net.Ps), 0.0)

    def test_reset(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=self.net.pores('bottom'), values=1)
        alg.set_value_BC(pores=self.net.pores('top'), values=0)
        alg.run()
        assert ~sp.all(sp.isnan(alg['pore.bc_value']))
        assert ~sp.all(sp.isnan(alg['pore.bc_rate']))
        assert 'pore.mole_fraction' in alg.keys()
        alg.reset(bcs=True, results=False)
        assert sp.all(sp.isnan(alg['pore.bc_value']))
        assert sp.all(sp.isnan(alg['pore.bc_rate']))
        assert 'pore.mole_fraction' in alg.keys()
        alg.reset(bcs=True, results=True)
        assert 'pore.mole_fraction' not in alg.keys()
        alg.set_rate_BC(pores=self.net.pores('bottom'), values=1)
        alg.set_value_BC(pores=self.net.pores('top'), values=0)
        alg.run()

    def teardown_class(self):
        ws = op.Workspace()
        ws.clear()

if __name__ == '__main__':

    t = GenericTransportTest()
    t.setup_class()
    for item in t.__dir__():
        if item.startswith('test'):
            print('running test: '+item)
            t.__getattribute__(item)()
    self = t
<commit_msg>PEP8 fix, forgot to turn flake8 on, really a time saver!<commit_after>import openpnm as op
import scipy as sp
import pytest

class GenericTransportTest:

    def setup_class(self):
        self.net = op.network.Cubic(shape=[9, 9, 9])
        self.geo = op.geometry.StickAndBall(network=self.net,
                                            pores=self.net.Ps,
                                            throats=self.net.Ts)
        self.phase = op.phases.Air(network=self.net)
        self.phase['pore.mole_fraction'] = 0
        self.phys = op.physics.GenericPhysics(network=self.net,
                                              phase=self.phase,
                                              geometry=self.geo)
        self.phys['throat.diffusive_conductance'] = 1.0

    def test_results(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        with pytest.raises(Exception):
            alg.results()

    def test_set_solver(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        # Store old values
        family = alg.settings["solver_family"]
        stype = alg.settings["solver_type"]
        tol = alg.settings["solver_tol"]
        atol = alg.settings["solver_atol"]
        rtol = alg.settings["solver_rtol"]
        # Set solver settings, but don't provide any arguments
        alg.set_solver()
        # Make sure nothing was changed
        assert alg.settings["solver_family"] == family
        assert alg.settings["solver_type"] == stype
        assert alg.settings["solver_tol"] == tol
        assert alg.settings["solver_atol"] == atol
        assert alg.settings["solver_rtol"] == rtol
        # Set solver settings, this time change everything
        alg.set_solver(solver_family="petsc", solver_type="gmres",
                       preconditioner="ilu", tol=1e-3, atol=1e-12, rtol=1e-2)
        # Make changes went through
        assert alg.settings["solver_family"] == "petsc"
        assert alg.settings["solver_type"] == "gmres"
        assert alg.settings["solver_tol"] == 1e-3
        assert alg.settings["solver_atol"] == 1e-12
        assert alg.settings["solver_rtol"] == 1e-2

    def test_remove_boundary_conditions(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.set_value_BC(pores=self.net.pores('top'), values=1)
        alg.set_value_BC(pores=self.net.pores('bottom'), values=0)
        assert sp.sum(sp.isfinite(alg['pore.bc_value'])) > 0
        alg.remove_BC(pores=self.net.pores('top'))
        assert sp.sum(sp.isfinite(alg['pore.bc_value'])) > 0
        alg.remove_BC(pores=self.net.pores('bottom'))
        assert sp.sum(sp.isfinite(alg['pore.bc_value'])) == 0

    def test_generic_transport(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_value_BC(pores=self.net.pores('top'), values=1)
        alg.set_value_BC(pores=self.net.pores('bottom'), values=0)
        alg.run()

    def test_two_value_conditions(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_value_BC(pores=self.net.pores('top'), values=1)
        alg.set_value_BC(pores=self.net.pores('bottom'), values=0)
        alg.run()
        x = [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]
        y = sp.unique(sp.around(alg['pore.mole_fraction'], decimals=3))
        assert sp.all(x == y)

    def test_two_value_conditions_cg(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_value_BC(pores=self.net.pores('top'), values=1)
        alg.set_value_BC(pores=self.net.pores('bottom'), values=0)
        alg.settings['solver'] = 'cg'
        alg.run()
        x = [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]
        y = sp.unique(sp.around(alg['pore.mole_fraction'], decimals=3))
        assert sp.all(x == y)

    def test_one_value_one_rate(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=self.net.pores('bottom'), values=1)
        alg.set_value_BC(pores=self.net.pores('top'), values=0)
        alg.run()
        x = [0., 1., 2., 3., 4., 5., 6., 7., 8.]
        y = sp.unique(sp.around(alg['pore.mole_fraction'], decimals=3))
        assert sp.all(x == y)

    def test_set_iterative_props(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                              phase=self.phase)
        assert len(alg.settings["iterative_props"]) == 0
        alg.set_iterative_props(propnames="pore.pressure")
        assert "pore.pressure" in alg.settings["iterative_props"]
        # Ensure each prop is only added once
        alg.set_iterative_props(propnames="pore.pressure")
        assert len(alg.settings["iterative_props"]) == 1
        alg.set_iterative_props(propnames=["pore.temperature", "pore.pressure"])
        assert len(alg.settings["iterative_props"]) == 2
        assert "pore.pressure" in alg.settings["iterative_props"]
        assert "pore.temperature" in alg.settings["iterative_props"]

    def test_cache_A(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=self.net.pores('bottom'), values=1)
        alg.set_value_BC(pores=self.net.pores('top'), values=0)
        alg.settings["cache_A"] = True
        alg._build_A()
        x = alg._A.mean()
        self.phys["throat.diffusive_conductance"][1] = 50.0
        alg._build_A()
        y = alg._A.mean()
        # When cache_A is True, A is not recomputed, hence x == y
        assert x == y
        alg.settings["cache_A"] = False
        alg._build_A()
        y = alg._A.mean()
        # When cache_A is False, A must be recomputed, hence x!= y
        assert x != y
        # Revert back changes to objects
        self.setup_class()

    def test_rate_single(self):
        alg = op.algorithms.ReactiveTransport(network=self.net,
                                              phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=self.net.pores("left"), values=1.235)
        alg.set_value_BC(pores=self.net.pores("right"), values=0.0)
        alg.run()
        rate = alg.rate(pores=self.net.pores("right"))[0]
        assert sp.isclose(rate, -1.235*self.net.pores("right").size)
        # Net rate must always be zero at steady state conditions
        assert sp.isclose(alg.rate(pores=self.net.Ps), 0.0)

    def test_rate_multiple(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=[0, 1, 2, 3], values=1.235)
        # Note that pore = 0 is assigned two rate values (rate = sum(rates))
        alg.set_rate_BC(pores=[5, 6, 19, 35, 0], values=3.455)
        alg.set_value_BC(pores=[50, 51, 52, 53], values=0.0)
        alg.run()
        rate = alg.rate(pores=[50, 51, 52, 53])[0]
        assert sp.isclose(rate, -(1.235*4 + 3.455*5))   # 4, 5 are number of pores
        # Net rate must always be zero at steady state conditions
        assert sp.isclose(alg.rate(pores=self.net.Ps), 0.0)

    def test_rate_Nt_by_2_conductance(self):
        net = op.network.Cubic(shape=[1, 6, 1])
        geom = op.geometry.StickAndBall(network=net)
        air = op.phases.Air(network=net)
        water = op.phases.Water(network=net)
        m = op.phases.MultiPhase(phases=[air, water], project=net.project)
        m.set_occupancy(phase=air, pores=[0, 1, 2])
        m.set_occupancy(phase=water, pores=[3, 4, 5])
        const = op.models.misc.constant
        K_water_air = 0.5
        m.set_binary_partition_coef(propname="throat.partition_coef",
                                    phases=[water, air],
                                    model=const, value=K_water_air)
        m._set_automatic_throat_occupancy()
        _ = op.physics.Standard(network=net, phase=m, geometry=geom)
        alg = op.algorithms.GenericTransport(network=net, phase=m)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=0, values=1.235)
        alg.set_value_BC(pores=5, values=0.0)
        alg.run()
        rate = alg.rate(pores=5)[0]
        assert sp.isclose(rate, -1.235)
        # Rate at air-water interface throat (#2) must match imposed rate
        rate = alg.rate(throats=2)[0]
        assert sp.isclose(rate, 1.235)
        # Rate at interface pores (#2 @ air-side, #3 @ water-side) must be 0
        rate_air_side = alg.rate(pores=2)[0]
        rate_water_side = alg.rate(pores=3)[0]
        assert sp.isclose(rate_air_side, 0.0)
        assert sp.isclose(rate_water_side, 0.0)
        # Net rate must always be zero at steady state conditions
        assert sp.isclose(alg.rate(pores=net.Ps), 0.0)

    def test_reset(self):
        alg = op.algorithms.GenericTransport(network=self.net,
                                             phase=self.phase)
        alg.settings['conductance'] = 'throat.diffusive_conductance'
        alg.settings['quantity'] = 'pore.mole_fraction'
        alg.set_rate_BC(pores=self.net.pores('bottom'), values=1)
        alg.set_value_BC(pores=self.net.pores('top'), values=0)
        alg.run()
        assert ~sp.all(sp.isnan(alg['pore.bc_value']))
        assert ~sp.all(sp.isnan(alg['pore.bc_rate']))
        assert 'pore.mole_fraction' in alg.keys()
        alg.reset(bcs=True, results=False)
        assert sp.all(sp.isnan(alg['pore.bc_value']))
        assert sp.all(sp.isnan(alg['pore.bc_rate']))
        assert 'pore.mole_fraction' in alg.keys()
        alg.reset(bcs=True, results=True)
        assert 'pore.mole_fraction' not in alg.keys()
        alg.set_rate_BC(pores=self.net.pores('bottom'), values=1)
        alg.set_value_BC(pores=self.net.pores('top'), values=0)
        alg.run()

    def teardown_class(self):
        ws = op.Workspace()
        ws.clear()

if __name__ == '__main__':

    t = GenericTransportTest()
    t.setup_class()
    for item in t.__dir__():
        if item.startswith('test'):
            print('running test: '+item)
            t.__getattribute__(item)()
    self = t
<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-
from abjad.tools import abctools
from abjad.tools import mathtools

class SegmentSession(abctools.AbjadValueObject):
    r'''A segment-maker session.
    '''

    ### CLASS VARIABLES ###

    __slots__ = (
        'attack_point_map',
        'meters',
        'score',
        'segment_maker',
        'voicewise_timespans',
        )

    ### INITIALIZER ###

    def __init__(
        self,
        segment_maker=None,
        ):
        self.attack_point_map = None
        self.meters = None
        self.score = None
        self.segment_maker = segment_maker
        self.voicewise_timespans = None

    ### PRIVATE PROPERTIES ###

    @property
    def _storage_format_specification(self):
        from abjad.tools import systemtools
        return systemtools.StorageFormatSpecification(
            self,
            keyword_argument_names=None,
            )

    ### PUBLIC PROPERTIES ###

    @property
    def measure_offsets(self):
        measure_durations = [x.duration for x in self.time_signatures]
        measure_offsets = mathtools.cumulative_sums(measure_durations)
        return measure_offsets

    @property
    def segment_duration(self):
        return sum(x.duration for x in self.time_signatures)

    @property
    def time_signatures(self):
        return (
            meter.implied_time_signature
            for meter in self.meters
            )<commit_msg>Removed unused SegmentSession.<commit_after><|endoftext|>


<commit_before>from rest_framework import serializers
from .models import Contact, ContactType, Company, CCRelation

class ContactTypeSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        contact_role = ContactType.objects.create(**validated_data)
        contact_role.save()
        return contact_role

    def update(self, instance, validated_data):
        instance.type_name = validated_data.get('type_name', instance.type_name)
        instance.is_valid = validated_data.get('is_valid', instance.is_valid)
        instance.save()
        return instance

    class Meta:
        model = ContactType
        fields = ('id', 'type_name', 'is_valid', 'creation_date')
        extra_kwargs = {'id': {'read_only': True}}

class ContactSerializer(serializers.ModelSerializer):

    role = serializers.DictField()

    def __init__(self, *args, **kwargs):
        remove_fields = kwargs.pop('remove_fields', None)
        super(ContactSerializer, self).__init__(*args, **kwargs)

        if remove_fields:
            for field_name in remove_fields:
                self.fields.pop(field_name)

    def create(self, validated_data):
        contact_role_data = validated_data.pop('role')
        contact = Contact.objects.create(**validated_data)
        for item in contact_role_data['relations']:
            try:
                company = Company.objects.get(company_custom_id=item['company']['company_custom_id'])
                contact_type = ContactType.objects.get(type_name=item['role'])
                relationship = CCRelation.objects.create(company=company, contact_type=contact_type, contact=contact)
            except:
                contact.delete()
                raise serializers.ValidationError({'company_type': ["Invalid role"]})

            contact.save()
            relationship.save()
            return contact

    def update(self,  instance, validated_data):

        contact_role_data = validated_data.pop('role')

        instance.contact_username = validated_data.get('contact_username', instance.contact_username)
        instance.contact_first_name = validated_data.get('contact_first_name', instance.contact_first_name)
        instance.contact_last_name = validated_data.get('contact_last_name', instance.contact_last_name)
        instance.contact_email = validated_data.get('contact_email', instance.contact_email)
        instance.contact_email_secondary = validated_data.get('contact_email_secondary', instance.contact_email_secondary)
        instance.contact_phone = validated_data.get('contact_phone', instance.contact_phone)
        instance.contact_phone_secondary = validated_data.get('contact_phone_secondary', instance.contact_phone_secondary)
        instance.contact_notes = validated_data.get('contact_notes', instance.contact_notes)

        for item in contact_role_data['relations']:
            try:
                relationship = CCRelation.objects.filter(company__company_custom_id=item['company']['company_custom_id'], contact_type__type_name=item['role'], contact=instance)
                if not relationship:
                    try:
                        company = Company.objects.get(company_custom_id=item['company']['company_custom_id'])
                        contact_type = ContactType.objects.get(type_name=item['role'])
                        CCRelation.objects.create(company=company, contact_type=contact_type, contact=instance)
                    except:
                        raise serializers.ValidationError({'Validation Error': ["Invalid company and/or invalid contact_type"]})
            except:
                raise serializers.ValidationError({'Validation Error': ["Invalid company and/or invalid contact_type"]})
        instance.save()
        return instance

    class Meta:
        model = Contact
        fields = ('id', 'contact_username', 'contact_first_name', 'contact_last_name', 'contact_email', 'contact_email_secondary',
                  'contact_phone', 'contact_phone_secondary', 'contact_notes', 'role')<commit_msg>fixing contact creation<commit_after>from rest_framework import serializers
from .models import Contact, ContactType, Company, CCRelation

class ContactTypeSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        contact_role = ContactType.objects.create(**validated_data)
        contact_role.save()
        return contact_role

    def update(self, instance, validated_data):
        instance.type_name = validated_data.get('type_name', instance.type_name)
        instance.is_valid = validated_data.get('is_valid', instance.is_valid)
        instance.save()
        return instance

    class Meta:
        model = ContactType
        fields = ('id', 'type_name', 'is_valid', 'creation_date')
        extra_kwargs = {'id': {'read_only': True}}

class ContactSerializer(serializers.ModelSerializer):

    role = serializers.DictField()

    def __init__(self, *args, **kwargs):
        remove_fields = kwargs.pop('remove_fields', None)
        super(ContactSerializer, self).__init__(*args, **kwargs)

        if remove_fields:
            for field_name in remove_fields:
                self.fields.pop(field_name)

    def create(self, validated_data):
        contact_role_data = validated_data.pop('role')
        contact = Contact.objects.create(**validated_data)
        for item in contact_role_data['relations']:
            try:
                company = Company.objects.get(id=item['company']['id'])
                contact_type = ContactType.objects.get(type_name=item['role'])
                relationship = CCRelation.objects.create(company=company, contact_type=contact_type, contact=contact)
            except:
                contact.delete()
                raise serializers.ValidationError({'company_type': ["Invalid role"]})

            contact.save()
            relationship.save()
            return contact

    def update(self,  instance, validated_data):

        contact_role_data = validated_data.pop('role')

        instance.contact_username = validated_data.get('contact_username', instance.contact_username)
        instance.contact_first_name = validated_data.get('contact_first_name', instance.contact_first_name)
        instance.contact_last_name = validated_data.get('contact_last_name', instance.contact_last_name)
        instance.contact_email = validated_data.get('contact_email', instance.contact_email)
        instance.contact_email_secondary = validated_data.get('contact_email_secondary', instance.contact_email_secondary)
        instance.contact_phone = validated_data.get('contact_phone', instance.contact_phone)
        instance.contact_phone_secondary = validated_data.get('contact_phone_secondary', instance.contact_phone_secondary)
        instance.contact_notes = validated_data.get('contact_notes', instance.contact_notes)

        for item in contact_role_data['relations']:
            try:
                relationship = CCRelation.objects.filter(company__id=item['company']['id'], contact_type__type_name=item['role'], contact=instance)
                if not relationship:
                    try:
                        company = Company.objects.get(id=item['company']['id'])
                        contact_type = ContactType.objects.get(type_name=item['role'])
                        CCRelation.objects.create(company=company, contact_type=contact_type, contact=instance)
                    except:
                        raise serializers.ValidationError({'Validation Error': ["Invalid company and/or invalid contact_type"]})
            except:
                raise serializers.ValidationError({'Validation Error': ["Invalid company and/or invalid contact_type"]})
        instance.save()
        return instance

    class Meta:
        model = Contact
        fields = ('id', 'contact_username', 'contact_first_name', 'contact_last_name', 'contact_email', 'contact_email_secondary',
                  'contact_phone', 'contact_phone_secondary', 'contact_notes', 'role')<|endoftext|>


<commit_before>
__author__ = "Ryan Faulkner and Aaron Halfaker"
__date__ = "October 3rd, 2012"
__license__ = "GPL (version 2 or later)"

import user_metric as um

class Blocks(um.UserMetric):
    """
        Adapted from Aaron Hafaker's implementation -- uses the logging table to count blocks.  This is a user quality
        metric used to assess whether a user went on to do damage.

        As a UserMetric type this class utilizes the process() function attribute to produce an internal list of metrics by
        user handle (typically ID but user names may also be specified). The execution of process() produces a nested list that
        stores in each element:

            * User ID
            * Block count
            * Date of first block
            * Date of Last block
            * Date of ban

        The process method for this metric breaks conformity with other metrics in that it expects usernames, and not
        user IDs, by default (see example below).

        Example: ::

            >>> import src.metrics.blocks as b
            >>> block_obj = b.Blocks(date_start='2011-01-01 00:00:00')
            >>> for r in block_obj.process(['Wesley Mouse', 'Nickyp88']).__iter__(): print r
            ...
            ['Nickyp88', 1L, '20110809143215', '20110809143215', -1]
            ['Wesley_Mouse', 2L, '20110830010835', '20120526192657', -1]
    """

    def __init__(self,
                 date_start='2012-01-01 00:00:00',
                 project='enwiki',
                 **kwargs):

        self._date_start_ = date_start
        self._project_ = project
        um.UserMetric.__init__(self, project=project, **kwargs)

    @staticmethod
    def header(): return ['user_id', 'block_count', 'block_first', 'block_last', 'ban']

    def process(self, user_handle, is_id=False, **kwargs):
        """
            Process method for the "blocks" metric.  Computes a list of block and ban events for users.

            Parameters:
                - **user_handle** - List.  List of user names (or IDs).
                - **is_id** - Boolean.  Defaults to False.

            Return:
                - UserMetric::Blocks (self).

        """
        rowValues = {}

        if not hasattr(user_handle, '__iter__'): user_handle = [user_handle] # ensure the handles are iterable
        for i in xrange(len(user_handle)):
            try:
                user_handle[i] = user_handle[i].encode('utf-8').replace(" ", "_")
            except UnicodeDecodeError:
                user_handle[i] = user_handle[i].replace(" ", "_")
            rowValues[user_handle[i]] = {'block_count' : 0, 'block_first' : -1, 'block_last' : -1, 'ban' : -1}

        user_handle_str = um.dl.DataLoader().format_comma_separated_list(user_handle)

        cursor = self._data_source_._cur_
        sql = """
				SELECT
				    log_title as user_name,
					IF(log_params LIKE "%%indefinite%%", "ban", "block") as type,
					count(*) as count,
					min(log_timestamp) as first,
					max(log_timestamp) as last
				FROM %(wiki)s.logging
				WHERE log_type = "block"
				AND log_action = "block"
				AND log_title in (%(usernames)s)
				AND log_timestamp >= (%(timestamp)s)
				GROUP BY 1, 2
			""" % {
            'timestamp': self._date_start_,
            'usernames': user_handle_str,
            'wiki' : self._project_
            }

        sql = " ".join(sql.strip().split())
        cursor.execute(sql)

        for row in cursor:

            username = row[0]
            type = row[1]
            count = row[2]
            first = row[3]
            last = row[4]

            if type == "block":
                rowValues[username]['block_count'] = count
                rowValues[username]['block_first'] = first
                rowValues[username]['block_last'] = last

            elif type == "ban":
                rowValues[username][type] = first

        self._results = [[user, rowValues.get(user)['block_count'], rowValues.get(user)['block_first'], rowValues.get(user)['block_last'], rowValues.get(user)['ban']] for user in rowValues.keys()]
        return self<commit_msg>FIX: handle cases where both user IDs and names are passed.  Default to expect IDs.<commit_after>
__author__ = "Ryan Faulkner and Aaron Halfaker"
__date__ = "October 3rd, 2012"
__license__ = "GPL (version 2 or later)"

import user_metric as um

class Blocks(um.UserMetric):
    """
        Adapted from Aaron Hafaker's implementation -- uses the logging table to count blocks.  This is a user quality
        metric used to assess whether a user went on to do damage.

        As a UserMetric type this class utilizes the process() function attribute to produce an internal list of metrics by
        user handle (typically ID but user names may also be specified). The execution of process() produces a nested list that
        stores in each element:

            * User ID
            * Block count
            * Date of first block
            * Date of Last block
            * Date of ban

        The process method for this metric breaks conformity with other metrics in that it expects usernames, and not
        user IDs, by default (see example below).

        Example: ::

            >>> import src.metrics.blocks as b
            >>> block_obj = b.Blocks(date_start='2011-01-01 00:00:00')
            >>> for r in block_obj.process(['Wesley Mouse', 'Nickyp88']).__iter__(): print r
            ...
            ['Nickyp88', 1L, '20110809143215', '20110809143215', -1]
            ['Wesley_Mouse', 2L, '20110830010835', '20120526192657', -1]
    """

    def __init__(self,
                 date_start='2012-01-01 00:00:00',
                 project='enwiki',
                 **kwargs):

        self._date_start_ = um.UserMetric._get_timestamp(date_start)
        self._project_ = project
        um.UserMetric.__init__(self, project=project, **kwargs)

    @staticmethod
    def header(): return ['user_id', 'block_count', 'block_first', 'block_last', 'ban']

    def process(self, user_handle, is_id=True, **kwargs):
        """
            Process method for the "blocks" metric.  Computes a list of block and ban events for users.

            Parameters:
                - **user_handle** - List.  List of user names (or IDs).
                - **is_id** - Boolean.  Defaults to False.

            Return:
                - UserMetric::Blocks (self).

        """
        rowValues = {}

        if not hasattr(user_handle, '__iter__'): user_handle = [user_handle] # ensure the handles are iterable
        for i in xrange(len(user_handle)):
            if hasattr(user_handle[i], 'encode'):
                try:
                    user_handle[i] = user_handle[i].encode('utf-8').replace(" ", "_")
                except UnicodeDecodeError:
                    user_handle[i] = user_handle[i].replace(" ", "_")
            rowValues[user_handle[i]] = {'block_count' : 0, 'block_first' : -1, 'block_last' : -1, 'ban' : -1}

        # Prepare condition on users based on whether numeric IDs or string usernames are passed
        if is_id:
            user_handle = um.dl.DataLoader().cast_elems_to_string(user_handle)
            user_handle_str = 'AND log_user in (' + um.dl.DataLoader().format_comma_separated_list(
                user_handle, include_quotes=False) + ')'
        else:
            user_handle_str = 'AND log_title in (' + um.dl.DataLoader().format_comma_separated_list(user_handle) + ')'

        cursor = self._data_source_._cur_
        sql = """
				SELECT
				    log_title as user_name,
					IF(log_params LIKE "%%indefinite%%", "ban", "block") as type,
					count(*) as count,
					min(log_timestamp) as first,
					max(log_timestamp) as last
				FROM %(wiki)s.logging
				WHERE log_type = "block"
				AND log_action = "block"
				%(user_cond)s
				AND log_timestamp >= "%(timestamp)s"
				GROUP BY 1, 2
			""" % {
            'timestamp': self._date_start_,
            'user_cond': user_handle_str,
            'wiki' : self._project_
            }

        sql = " ".join(sql.strip().split())
        print sql
        cursor.execute(sql)

        for row in cursor:

            username = row[0]
            type = row[1]
            count = row[2]
            first = row[3]
            last = row[4]

            if type == "block":
                rowValues[username]['block_count'] = count
                rowValues[username]['block_first'] = first
                rowValues[username]['block_last'] = last

            elif type == "ban":
                rowValues[username][type] = first

        self._results = [[user, rowValues.get(user)['block_count'], rowValues.get(user)['block_first'], rowValues.get(user)['block_last'], rowValues.get(user)['ban']] for user in rowValues.keys()]
        return self<|endoftext|>


<commit_before>import os, os.path, subprocess, shutil, re

try:
    from urllib.request import urlopen
except ImportError:
    from urllib import urlopen

python_versions = ['2.4.6', '2.5.6', '2.6.8', '2.7.5', '3.1.5', '3.2.5', '3.3.5', '3.4.1']
libcurl_versions = ['7.19.0', '7.37.0']

# http://bsdpower.com/building-python-24-with-zlib/
patch_python_for_zlib = "sed -e 's/^#zlib/zlib/g' Modules/Setup >Modules/Setup.patched && mv Modules/Setup.patched Modules/Setup"
patch_python_for_ssl = "sed -e '/^#SSL/,/^$/s/^#//' -e 's/^#\*shared\*/*shared*/' Modules/Setup >Modules/Setup.patched && mv Modules/Setup.patched Modules/Setup"

python_meta = {
    '2.4.6': {
        'post-configure': [patch_python_for_zlib, patch_python_for_ssl],
    },
    '2.5.6': {
        'patches': ['python25.patch'],
        'post-configure': [patch_python_for_zlib, patch_python_for_ssl],
    },
    '3.0.1': {
        'patches': ['python25.patch', 'python30.patch'],
    },
}

libcurl_meta = {
    '7.19.0': {
        'patches': [
            'curl-7.19.0-sslv2-c66b0b32fba-modified.patch',
            #'curl-7.19.0-sslv2-2b0e09b0f98.patch',
        ],
    },
}

root = os.path.abspath(os.path.dirname(__file__))

class in_dir:
    def __init__(self, dir):
        self.dir = dir

    def __enter__(self):
        self.oldwd = os.getcwd()
        os.chdir(self.dir)

    def __exit__(self, type, value, traceback):
        os.chdir(self.oldwd)

def subprocess_check_call(cmd, **kwargs):
    try:
        subprocess.check_call(cmd, **kwargs)
    except OSError as exc:
        message = exc.args[0]
        message = '%s while trying to execute %s' % (message, str(cmd))
        args = tuple([message] + exc.args[1:])
        raise type(exc)(args)

def fetch(url, archive=None):
    if archive is None:
        archive = os.path.basename(url)
    if not os.path.exists(archive):
        sys.stdout.write("Fetching %s\n" % url)
        io = urlopen(url)
        with open('.tmp.%s' % archive, 'wb') as f:
            while True:
                chunk = io.read(65536)
                if len(chunk) == 0:
                    break
                f.write(chunk)
        os.rename('.tmp.%s' % archive, archive)

def build(archive, dir, prefix, meta=None):
    if not os.path.exists(dir):
        sys.stdout.write("Building %s\n" % archive)
        subprocess_check_call(['tar', 'xf', archive])
        with in_dir(dir):
            if meta and 'patches' in meta:
                for patch in meta['patches']:
                    patch_path = os.path.join(root, 'matrix', patch)
                    subprocess_check_call(['patch', '-p1', '-i', patch_path])
            subprocess_check_call(['./configure', '--prefix=%s' % prefix])
            if 'post-configure' in meta:
                for cmd in meta['post-configure']:
                    subprocess_check_call(cmd, shell=True)
            subprocess_check_call(['make'])
            subprocess_check_call(['make', 'install'])

def patch_pycurl_for_24():
    # change relative imports to old syntax as python 2.4 does not
    # support relative imports
    for root, dirs, files in os.walk('tests'):
        for file in files:
            if file.endswith('.py'):
                path = os.path.join(root, file)
                with open(path, 'r') as f:
                    contents = f.read()
                contents = re.compile(r'^(\s*)from \. import', re.M).sub(r'\1import', contents)
                contents = re.compile(r'^(\s*)from \.(\w+) import', re.M).sub(r'\1from \2 import', contents)
                with open(path, 'w') as f:
                    f.write(contents)

def run_matrix(python_versions, libcurl_versions):
    for python_version in python_versions:
        url = 'http://www.python.org/ftp/python/%s/Python-%s.tgz' % (python_version, python_version)
        archive = os.path.basename(url)
        fetch(url, archive)

        dir = archive.replace('.tgz', '')
        prefix = os.path.abspath('i/%s' % dir)
        build(archive, dir, prefix, meta=python_meta.get(python_version))

    for libcurl_version in libcurl_versions:
        url = 'http://curl.haxx.se/download/curl-%s.tar.gz' % libcurl_version
        archive = os.path.basename(url)
        fetch(url, archive)

        dir = archive.replace('.tar.gz', '')
        prefix = os.path.abspath('i/%s' % dir)
        build(archive, dir, prefix, meta=libcurl_meta.get(libcurl_version))

    fetch('https://raw.github.com/pypa/virtualenv/1.7/virtualenv.py', 'virtualenv-1.7.py')
    fetch('https://raw.github.com/pypa/virtualenv/1.9.1/virtualenv.py', 'virtualenv-1.9.1.py')

    if not os.path.exists('venv'):
        os.mkdir('venv')

    for python_version in python_versions:
        python_version_pieces = [int(piece) for piece in python_version.split('.')[:2]]
        for libcurl_version in libcurl_versions:
            python_prefix = os.path.abspath('i/Python-%s' % python_version)
            libcurl_prefix = os.path.abspath('i/curl-%s' % libcurl_version)
            venv = os.path.abspath('venv/Python-%s-curl-%s' % (python_version, libcurl_version))
            if os.path.exists(venv):
                shutil.rmtree(venv)
            if python_version_pieces >= [2, 5]:
                fetch('https://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c11-py2.5.egg')
                fetch('https://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg')
                fetch('https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg')
                # I had virtualenv 1.8.2 installed systemwide which
                # did not work with python 3.0:
                # http://stackoverflow.com/questions/14224361/why-am-i-getting-this-error-related-to-pip-and-easy-install-when-trying-to-set
                # so, use known versions everywhere
                # md5=89e68df89faf1966bcbd99a0033fbf8e
                fetch('https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz')
                subprocess_check_call(['python', 'virtualenv-1.9.1.py', venv, '-p', '%s/bin/python%d.%d' % (python_prefix, python_version_pieces[0], python_version_pieces[1]), '--no-site-packages', '--never-download'])
            else:
                # md5=bd639f9b0eac4c42497034dec2ec0c2b
                fetch('https://pypi.python.org/packages/2.4/s/setuptools/setuptools-0.6c11-py2.4.egg')
                # md5=6afbb46aeb48abac658d4df742bff714
                fetch('https://pypi.python.org/packages/source/p/pip/pip-1.4.1.tar.gz')
                subprocess_check_call(['python', 'virtualenv-1.7.py', venv, '-p', '%s/bin/python' % python_prefix, '--no-site-packages', '--never-download'])
            curl_config_path = os.path.join(libcurl_prefix, 'bin/curl-config')
            curl_lib_path = os.path.join(libcurl_prefix, 'lib')
            with in_dir('pycurl'):
                extra_patches = []
                extra_env = []
                if python_version_pieces >= [2, 6]:
                    deps_cmd = 'pip install -r requirements-dev.txt'
                elif python_version_pieces >= [2, 5]:
                    deps_cmd = 'pip install -r requirements-dev-2.5.txt'
                else:
                    deps_cmd = 'easy_install nose simplejson==2.1.0'
                    patch_pycurl_for_24()
                    extra_patches.append('(cd %s/lib/python2.4/site-packages/nose-* && patch -p1) <tests/matrix/nose-1.3.0-python24.patch' % venv)
                    extra_env.append('PYCURL_STANDALONE_APP=yes')
                extra_patches = ' && '.join(extra_patches)
                extra_env = ' '.join(extra_env)
                cmd = '''
                    make clean &&
                    . %(venv)s/bin/activate &&
                    %(deps_cmd)s && %(extra_patches)s
                    python -V &&
                    LD_LIBRARY_PATH=%(curl_lib_path)s PYCURL_CURL_CONFIG=%(curl_config_path)s %(extra_env)s make test
                ''' % dict(
                    venv=venv,
                    deps_cmd=deps_cmd,
                    extra_patches=extra_patches,
                    curl_lib_path=curl_lib_path,
                    curl_config_path=curl_config_path,
                    extra_env=extra_env
                )
                print(cmd)
                subprocess_check_call(cmd, shell=True)

if __name__ == '__main__':
    import sys

    def main():
        import optparse

        parser = optparse.OptionParser()
        parser.add_option('-p', '--python', help='Specify python version to test against')
        parser.add_option('-c', '--curl', help='Specify libcurl version to test against')
        options, args = parser.parse_args()
        if options.python:
            chosen_python_versions = [options.python]
        else:
            chosen_python_versions = python_versions
        if options.curl:
            chosen_libcurl_versions = [options.curl]
        else:
            chosen_libcurl_versions = libcurl_versions
        run_matrix(chosen_python_versions, chosen_libcurl_versions)

    if len(sys.argv) > 1 and sys.argv[1] == 'patch-24':
        patch_pycurl_for_24()
    else:
        main()
<commit_msg>Permit python version prefixes<commit_after>import os, os.path, subprocess, shutil, re

try:
    from urllib.request import urlopen
except ImportError:
    from urllib import urlopen

python_versions = ['2.4.6', '2.5.6', '2.6.8', '2.7.5', '3.1.5', '3.2.5', '3.3.5', '3.4.1']
libcurl_versions = ['7.19.0', '7.37.0']

# http://bsdpower.com/building-python-24-with-zlib/
patch_python_for_zlib = "sed -e 's/^#zlib/zlib/g' Modules/Setup >Modules/Setup.patched && mv Modules/Setup.patched Modules/Setup"
patch_python_for_ssl = "sed -e '/^#SSL/,/^$/s/^#//' -e 's/^#\*shared\*/*shared*/' Modules/Setup >Modules/Setup.patched && mv Modules/Setup.patched Modules/Setup"

python_meta = {
    '2.4.6': {
        'post-configure': [patch_python_for_zlib, patch_python_for_ssl],
    },
    '2.5.6': {
        'patches': ['python25.patch'],
        'post-configure': [patch_python_for_zlib, patch_python_for_ssl],
    },
    '3.0.1': {
        'patches': ['python25.patch', 'python30.patch'],
    },
}

libcurl_meta = {
    '7.19.0': {
        'patches': [
            'curl-7.19.0-sslv2-c66b0b32fba-modified.patch',
            #'curl-7.19.0-sslv2-2b0e09b0f98.patch',
        ],
    },
}

root = os.path.abspath(os.path.dirname(__file__))

class in_dir:
    def __init__(self, dir):
        self.dir = dir

    def __enter__(self):
        self.oldwd = os.getcwd()
        os.chdir(self.dir)

    def __exit__(self, type, value, traceback):
        os.chdir(self.oldwd)

def subprocess_check_call(cmd, **kwargs):
    try:
        subprocess.check_call(cmd, **kwargs)
    except OSError as exc:
        message = exc.args[0]
        message = '%s while trying to execute %s' % (message, str(cmd))
        args = tuple([message] + exc.args[1:])
        raise type(exc)(args)

def fetch(url, archive=None):
    if archive is None:
        archive = os.path.basename(url)
    if not os.path.exists(archive):
        sys.stdout.write("Fetching %s\n" % url)
        io = urlopen(url)
        with open('.tmp.%s' % archive, 'wb') as f:
            while True:
                chunk = io.read(65536)
                if len(chunk) == 0:
                    break
                f.write(chunk)
        os.rename('.tmp.%s' % archive, archive)

def build(archive, dir, prefix, meta=None):
    if not os.path.exists(dir):
        sys.stdout.write("Building %s\n" % archive)
        subprocess_check_call(['tar', 'xf', archive])
        with in_dir(dir):
            if meta and 'patches' in meta:
                for patch in meta['patches']:
                    patch_path = os.path.join(root, 'matrix', patch)
                    subprocess_check_call(['patch', '-p1', '-i', patch_path])
            subprocess_check_call(['./configure', '--prefix=%s' % prefix])
            if 'post-configure' in meta:
                for cmd in meta['post-configure']:
                    subprocess_check_call(cmd, shell=True)
            subprocess_check_call(['make'])
            subprocess_check_call(['make', 'install'])

def patch_pycurl_for_24():
    # change relative imports to old syntax as python 2.4 does not
    # support relative imports
    for root, dirs, files in os.walk('tests'):
        for file in files:
            if file.endswith('.py'):
                path = os.path.join(root, file)
                with open(path, 'r') as f:
                    contents = f.read()
                contents = re.compile(r'^(\s*)from \. import', re.M).sub(r'\1import', contents)
                contents = re.compile(r'^(\s*)from \.(\w+) import', re.M).sub(r'\1from \2 import', contents)
                with open(path, 'w') as f:
                    f.write(contents)

def run_matrix(python_versions, libcurl_versions):
    for python_version in python_versions:
        url = 'http://www.python.org/ftp/python/%s/Python-%s.tgz' % (python_version, python_version)
        archive = os.path.basename(url)
        fetch(url, archive)

        dir = archive.replace('.tgz', '')
        prefix = os.path.abspath('i/%s' % dir)
        build(archive, dir, prefix, meta=python_meta.get(python_version))

    for libcurl_version in libcurl_versions:
        url = 'http://curl.haxx.se/download/curl-%s.tar.gz' % libcurl_version
        archive = os.path.basename(url)
        fetch(url, archive)

        dir = archive.replace('.tar.gz', '')
        prefix = os.path.abspath('i/%s' % dir)
        build(archive, dir, prefix, meta=libcurl_meta.get(libcurl_version))

    fetch('https://raw.github.com/pypa/virtualenv/1.7/virtualenv.py', 'virtualenv-1.7.py')
    fetch('https://raw.github.com/pypa/virtualenv/1.9.1/virtualenv.py', 'virtualenv-1.9.1.py')

    if not os.path.exists('venv'):
        os.mkdir('venv')

    for python_version in python_versions:
        python_version_pieces = [int(piece) for piece in python_version.split('.')[:2]]
        for libcurl_version in libcurl_versions:
            python_prefix = os.path.abspath('i/Python-%s' % python_version)
            libcurl_prefix = os.path.abspath('i/curl-%s' % libcurl_version)
            venv = os.path.abspath('venv/Python-%s-curl-%s' % (python_version, libcurl_version))
            if os.path.exists(venv):
                shutil.rmtree(venv)
            if python_version_pieces >= [2, 5]:
                fetch('https://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c11-py2.5.egg')
                fetch('https://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg')
                fetch('https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg')
                # I had virtualenv 1.8.2 installed systemwide which
                # did not work with python 3.0:
                # http://stackoverflow.com/questions/14224361/why-am-i-getting-this-error-related-to-pip-and-easy-install-when-trying-to-set
                # so, use known versions everywhere
                # md5=89e68df89faf1966bcbd99a0033fbf8e
                fetch('https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz')
                subprocess_check_call(['python', 'virtualenv-1.9.1.py', venv, '-p', '%s/bin/python%d.%d' % (python_prefix, python_version_pieces[0], python_version_pieces[1]), '--no-site-packages', '--never-download'])
            else:
                # md5=bd639f9b0eac4c42497034dec2ec0c2b
                fetch('https://pypi.python.org/packages/2.4/s/setuptools/setuptools-0.6c11-py2.4.egg')
                # md5=6afbb46aeb48abac658d4df742bff714
                fetch('https://pypi.python.org/packages/source/p/pip/pip-1.4.1.tar.gz')
                subprocess_check_call(['python', 'virtualenv-1.7.py', venv, '-p', '%s/bin/python' % python_prefix, '--no-site-packages', '--never-download'])
            curl_config_path = os.path.join(libcurl_prefix, 'bin/curl-config')
            curl_lib_path = os.path.join(libcurl_prefix, 'lib')
            with in_dir('pycurl'):
                extra_patches = []
                extra_env = []
                if python_version_pieces >= [2, 6]:
                    deps_cmd = 'pip install -r requirements-dev.txt'
                elif python_version_pieces >= [2, 5]:
                    deps_cmd = 'pip install -r requirements-dev-2.5.txt'
                else:
                    deps_cmd = 'easy_install nose simplejson==2.1.0'
                    patch_pycurl_for_24()
                    extra_patches.append('(cd %s/lib/python2.4/site-packages/nose-* && patch -p1) <tests/matrix/nose-1.3.0-python24.patch' % venv)
                    extra_env.append('PYCURL_STANDALONE_APP=yes')
                extra_patches = ' && '.join(extra_patches)
                extra_env = ' '.join(extra_env)
                cmd = '''
                    make clean &&
                    . %(venv)s/bin/activate &&
                    %(deps_cmd)s && %(extra_patches)s
                    python -V &&
                    LD_LIBRARY_PATH=%(curl_lib_path)s PYCURL_CURL_CONFIG=%(curl_config_path)s %(extra_env)s make test
                ''' % dict(
                    venv=venv,
                    deps_cmd=deps_cmd,
                    extra_patches=extra_patches,
                    curl_lib_path=curl_lib_path,
                    curl_config_path=curl_config_path,
                    extra_env=extra_env
                )
                print(cmd)
                subprocess_check_call(cmd, shell=True)

if __name__ == '__main__':
    import sys

    def main():
        import optparse

        parser = optparse.OptionParser()
        parser.add_option('-p', '--python', help='Specify python version to test against')
        parser.add_option('-c', '--curl', help='Specify libcurl version to test against')
        options, args = parser.parse_args()
        if options.python:
            python_version = options.python
            if python_version in python_versions:
                chosen_python_versions = [python_version]
            else:
                chosen_python_versions = [v for v in python_versions if v.startswith(python_version)]
                if len(chosen_python_versions) != 1:
                    raise Exception('Bogus python version requested: %s' % python_version)
        else:
            chosen_python_versions = python_versions
        if options.curl:
            chosen_libcurl_versions = [options.curl]
        else:
            chosen_libcurl_versions = libcurl_versions
        run_matrix(chosen_python_versions, chosen_libcurl_versions)

    if len(sys.argv) > 1 and sys.argv[1] == 'patch-24':
        patch_pycurl_for_24()
    else:
        main()
<|endoftext|>


<commit_before>from pydojo import *

# inizializzo
pygame.init()
width = 800
height = 600
SCREEN(width, height)

bianco = [255, 255, 255]

gamepad_control = False
try:
    # creo un oggetto Joystick
    pad = pygame.joystick.Joystick(0)
    # inizializzo il joystick
    pad.init()
    gamepad_control = True
except:
    print("no GamePad found...")

gufo = Actor('example_library/gufo.png')
gufo.goto(width/2, height/2)

uno = Arduino('COM5')

rightPwm = uno.get_pin('d:10:p')
leftPwm = uno.get_pin('d:5:p')
RF = uno.get_pin('d:9:o')
RB = uno.get_pin('d:8:o')
LF = uno.get_pin('d:7:o')
LB = uno.get_pin('d:6:o')

RF.write(True)
RB.write(False)
LF.write(True)
LB.write(False)

direction = "forward"

def shutdown():
    RF.write(False)
    RB.write(False)
    LF.write(False)
    LB.write(False)
    rightPwm.write(0)
    leftPwm.write(0)

while True:

    # KEYBOARD CONTROL
    if keydown(ESCAPE):
        shutdown()
        quit()

    if keydown(RIGHT):
        if direction == "forward":
            RF.write(True)
            RB.write(False)
        elif direction == "back":
            RF.write(False)
            RB.write(True)
        rightPwm.write(1)
    if keyup(RIGHT):
        rightPwm.write(0)
        RF.write(False)
        RB.write(False)

    if keydown(LEFT):
        if direction == "forward":
            LF.write(True)
            LB.write(False)
        elif direction == "back":
            LF.write(False)
            LB.write(True)
        leftPwm.write(1)
    if keyup(LEFT):
        leftPwm.write(0)
        LF.write(False)
        LB.write(False)

    if keydown(UP):
        direction = "forward"
    if keydown(DOWN):
        direction = "back"

    # GAMEPAD CONTROL
    if gamepad_control:
        horizontal = pad.get_axis(0)

        if horizontal > 0.2:
            rightPwm.write(0.9)
            gufo.x += int(horizontal * 10)
        if horizontal < -0.2:
            leftPwm.write(0.9)
            gufo.x += int(horizontal * 10)

    fill(bianco)
    gufo.draw()
    sleep(0.01)
    UPDATE()
<commit_msg>inverted key-motor bindings<commit_after>from pydojo import *

# inizializzo
pygame.init()
width = 800
height = 600
SCREEN(width, height)

bianco = [255, 255, 255]

gamepad_control = False
try:
    # creo un oggetto Joystick
    pad = pygame.joystick.Joystick(0)
    # inizializzo il joystick
    pad.init()
    gamepad_control = True
except:
    print("no GamePad found...")

gufo = Actor('example_library/gufo.png')
gufo.goto(width/2, height/2)

uno = Arduino('/dev/rfcomm0')
#uno = Arduino('/dev/ttyACM0')

rightPwm = uno.get_pin('d:5:p')
leftPwm = uno.get_pin('d:10:p')
RF = uno.get_pin('d:7:o')
RB = uno.get_pin('d:6:o')
LF = uno.get_pin('d:9:o')
LB = uno.get_pin('d:8:o')

RF.write(True)
RB.write(False)
LF.write(True)
LB.write(False)

direction = "forward"

def shutdown():
    RF.write(False)
    RB.write(False)
    LF.write(False)
    LB.write(False)
    rightPwm.write(0)
    leftPwm.write(0)

while True:

    # KEYBOARD CONTROL
    if keydown(ESCAPE):
        shutdown()
        quit()

    if keydown(RIGHT):
        if direction == "forward":
            RF.write(True)
            RB.write(False)
        elif direction == "back":
            RF.write(False)
            RB.write(True)
        rightPwm.write(1)
    if keyup(RIGHT):
        rightPwm.write(0)
        RF.write(False)
        RB.write(False)

    if keydown(LEFT):
        if direction == "forward":
            LF.write(True)
            LB.write(False)
        elif direction == "back":
            LF.write(False)
            LB.write(True)
        leftPwm.write(1)
    if keyup(LEFT):
        leftPwm.write(0)
        LF.write(False)
        LB.write(False)

    if keydown(UP):
        direction = "forward"
    if keydown(DOWN):
        direction = "back"

    # GAMEPAD CONTROL
    if gamepad_control:
        horizontal = pad.get_axis(0)

        if horizontal > 0.2:
            rightPwm.write(0.9)
            gufo.x += int(horizontal * 10)
        if horizontal < -0.2:
            leftPwm.write(0.9)
            gufo.x += int(horizontal * 10)

    fill(bianco)
    gufo.draw()
    sleep(0.01)
    UPDATE()
<|endoftext|>


<commit_before>#!/usr/bin/python

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import threading
import os
import logging
import modules.utils

wss = []

target_locations = []
boundary_locations = []

class WSHandler(tornado.websocket.WebSocketHandler):

    """ Creates the web socket server
    """

    def check_origin(self, origin):
        return True

    def open(self):
        logging.info('New connection established.')

        for marker in target_locations:
            location_data = {'category': 'marker', 'type': 'target', 'location': marker}
            self.write_message(modules.utils.getJSON(location_data))

        for marker in boundary_locations:
            location_data = {'category': 'marker', 'type': 'boundary', 'location': marker}
            self.write_message(modules.utils.getJSON(location_data))

        if self not in wss:
            wss.append(self)

    def on_message(self, message):
        logging.info('Received message: %s' % message)

    def on_close(self):
        logging.info('Connection closed.')
        if self in wss:
            wss.remove(self)

class IndexHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous
    def get(self):
        self.render('../web/index.html')

class Application(tornado.web.Application):

    def __init__(self):
        handlers = [(r'/ws', WSHandler), (r'/', IndexHandler)]
        settings = {'debug': True,
                    'static_path': os.path.join(os.path.dirname(__file__),
                    '../web')}
        tornado.web.Application.__init__(self, handlers, **settings)

application = Application()

class ServerThread(threading.Thread):

    """ Creates thread which runs the web socket server
    """

    def send_data(self, message):
        for ws in wss:

            # do not log any data here, doing so would create an infinite loop

            try:
                ws.write_message(message)
                break
            except TypeError:
                logging.error('Tried to send invalid value.')

    def close_sockets(self):
        logging.info('Closing all connections....')
        for ws in wss:
            ws.close()

    def run(self):
        global target_locations
        global boundary_locations

        logging.info('Starting server.')

        # defining the locations array
        target_locations = self._kwargs['target_locations']
        boundary_locations = self._kwargs['boundary_locations']

        try:
            http_server = tornado.httpserver.HTTPServer(application)
            http_server.listen(self._kwargs['port'])

            main_loop = tornado.ioloop.IOLoop.instance()

            logging.info('The web server successfully bound to port %d'
                         % self._kwargs['port'])

            # starts the main IO loop

            main_loop.start()
        except OSError:
            logging.critical('The web server failed to bind to the port!')
        except Exception:
            logging.critical('The web server failed to start!')

<commit_msg>Minor optimizations to web server<commit_after>#!/usr/bin/python

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import threading
import os
import logging
import modules.utils

wss = []

target_locations = []
boundary_locations = []

class WSHandler(tornado.websocket.WebSocketHandler):

    """ Creates the web socket server
    """

    def check_origin(self, origin):
        return True

    def open(self):
        logging.info('New connection established.')

        for marker in target_locations:
            location_data = {'category': 'marker', 'type': 'target', 'location': marker}
            self.write_message(modules.utils.getJSON(location_data))

        for marker in boundary_locations:
            location_data = {'category': 'marker', 'type': 'boundary', 'location': marker}
            self.write_message(modules.utils.getJSON(location_data))

        if self not in wss:
            wss.append(self)

    def on_message(self, message):
        logging.info('Received message: %s' % message)

    def on_close(self):
        logging.info('Connection closed.')
        if self in wss:
            wss.remove(self)

class IndexHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous
    def get(self):
        self.render('../web/index.html')

class Application(tornado.web.Application):

    def __init__(self):
        handlers = [(r'/ws', WSHandler), (r'/', IndexHandler)]
        settings = {'debug': True,
                    'static_path': os.path.join(os.path.dirname(__file__),
                    '../web')}
        tornado.web.Application.__init__(self, handlers, **settings)

application = Application()

class ServerThread(threading.Thread):

    """ Creates thread which runs the web socket server
    """

    def send_data(self, message):
        for ws in wss:

            # do not log any data here, doing so would create an infinite loop

            try:
                ws.write_message(message)
                break
            except TypeError:
                logging.error('Tried to send invalid value.')

    def close_sockets(self):
        logging.info('Closing all connections....')
        for ws in wss:
            ws.close()

    def run(self):
        global target_locations, boundary_locations

        logging.info('Starting server.')

        # defining the locations array
        target_locations = self._kwargs['target_locations']
        boundary_locations = self._kwargs['boundary_locations']

        try:
            http_server = tornado.httpserver.HTTPServer(application)
            http_server.listen(self._kwargs['port'])

            main_loop = tornado.ioloop.IOLoop.instance()

            logging.info('The web server successfully bound to port %d'
                         % self._kwargs['port'])

            # starts the main IO loop
            main_loop.start()

        except OSError:
            logging.critical('The web server failed to bind to the port!')
        except Exception:
            logging.critical('The web server failed to start!')

<|endoftext|>


<commit_before># This file is a minimal clang-format vim-integration. To install:
# - Change 'binary' if clang-format is not on the path (see below).
# - Add to your .vimrc:
#
#   map <C-I> :pyf <path-to-this-file>/clang-format.py<cr>
#   imap <C-I> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
#
# The first line enables clang-format for NORMAL and VISUAL mode, the second
# line adds support for INSERT mode. Change "C-I" to another binding if you
# need clang-format on a different key (C-I stands for Ctrl+i).
#
# With this integration you can press the bound key and clang-format will
# format the current line in NORMAL and INSERT mode or the selected region in
# VISUAL mode. The line or region is extended to the next bigger syntactic
# entity.
#
# You can also pass in the variable "l:lines" to choose the range for
# formatting. This variable can either contain "<start line>:<end line>" or
# "all" to format the full file. So, to format the full file, write a function
# like:
# :function FormatFile()
# :  let l:lines="all"
# :  pyf <path-to-this-file>/clang-format.py
# :endfunction
#
# It operates on the current, potentially unsaved buffer and does not create
# or save any files. To revert a formatting, just undo.
from __future__ import print_function

import difflib
import json
import platform
import subprocess
import sys
import vim

# set g:clang_format_path to the path to clang-format if it is not on the path
# Change this to the full path if clang-format is not on the path.
binary = 'clang-format'
if vim.eval('exists("g:clang_format_path")') == "1":
  binary = vim.eval('g:clang_format_path')

# Change this to format according to other formatting styles. See the output of
# 'clang-format --help' for a list of supported styles. The default looks for
# a '.clang-format' or '_clang-format' file to indicate the style that should be
# used.
style = 'file'
fallback_style = None
if vim.eval('exists("g:clang_format_fallback_style")') == "1":
  fallback_style = vim.eval('g:clang_format_fallback_style')

def get_buffer(encoding):
  if platform.python_version_tuple()[0] == '3':
    return vim.current.buffer
  return [ line.decode(encoding) for line in vim.current.buffer ]

def main():
  # Get the current text.
  encoding = vim.eval("&encoding")
  buf = get_buffer(encoding)
  text = '\n'.join(buf)

  # Determine range to format.
  if vim.eval('exists("l:lines")') == '1':
    lines = ['-lines', vim.eval('l:lines')]
  elif vim.eval('exists("l:formatdiff")') == '1':
    with open(vim.current.buffer.name, 'r') as f:
      ondisk = f.read().splitlines();
    sequence = difflib.SequenceMatcher(None, ondisk, vim.current.buffer)
    lines = []
    for op in reversed(sequence.get_opcodes()):
      if op[0] not in ['equal', 'delete']:
        lines += ['-lines', '%s:%s' % (op[3] + 1, op[4])]
    if lines == []:
      return
  else:
    lines = ['-lines', '%s:%s' % (vim.current.range.start + 1,
                                  vim.current.range.end + 1)]

  # Determine the cursor position.
  cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
  if cursor < 0:
    print('Couldn\'t determine cursor position. Is your file empty?')
    return

  # Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
  startupinfo = None
  if sys.platform.startswith('win32'):
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    startupinfo.wShowWindow = subprocess.SW_HIDE

  # Call formatter.
  command = [binary, '-style', style, '-cursor', str(cursor)]
  if lines != 'all':
    command += lines
  if fallback_style:
    command.extend(['-fallback-style', fallback_style])
  if vim.current.buffer.name:
    command.extend(['-assume-filename', vim.current.buffer.name])
  p = subprocess.Popen(command,
                       stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                       stdin=subprocess.PIPE, startupinfo=startupinfo)
  stdout, stderr = p.communicate(input=text.encode(encoding))

  # If successful, replace buffer contents.
  if stderr:
    print(stderr)

  if not stdout:
    print(
        'No output from clang-format (crashed?).\n'
        'Please report to bugs.llvm.org.'
    )
  else:
    lines = stdout.decode(encoding).split('\n')
    output = json.loads(lines[0])
    lines = lines[1:]
    sequence = difflib.SequenceMatcher(None, buf, lines)
    for op in reversed(sequence.get_opcodes()):
      if op[0] is not 'equal':
        vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
    if output.get('IncompleteFormat'):
      print('clang-format: incomplete (syntax errors)')
    vim.command('goto %d' % (output['Cursor'] + 1))

main()
<commit_msg>[clang-format] Fix lines=all case in clang-format.py<commit_after># This file is a minimal clang-format vim-integration. To install:
# - Change 'binary' if clang-format is not on the path (see below).
# - Add to your .vimrc:
#
#   map <C-I> :pyf <path-to-this-file>/clang-format.py<cr>
#   imap <C-I> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
#
# The first line enables clang-format for NORMAL and VISUAL mode, the second
# line adds support for INSERT mode. Change "C-I" to another binding if you
# need clang-format on a different key (C-I stands for Ctrl+i).
#
# With this integration you can press the bound key and clang-format will
# format the current line in NORMAL and INSERT mode or the selected region in
# VISUAL mode. The line or region is extended to the next bigger syntactic
# entity.
#
# You can also pass in the variable "l:lines" to choose the range for
# formatting. This variable can either contain "<start line>:<end line>" or
# "all" to format the full file. So, to format the full file, write a function
# like:
# :function FormatFile()
# :  let l:lines="all"
# :  pyf <path-to-this-file>/clang-format.py
# :endfunction
#
# It operates on the current, potentially unsaved buffer and does not create
# or save any files. To revert a formatting, just undo.
from __future__ import print_function

import difflib
import json
import platform
import subprocess
import sys
import vim

# set g:clang_format_path to the path to clang-format if it is not on the path
# Change this to the full path if clang-format is not on the path.
binary = 'clang-format'
if vim.eval('exists("g:clang_format_path")') == "1":
  binary = vim.eval('g:clang_format_path')

# Change this to format according to other formatting styles. See the output of
# 'clang-format --help' for a list of supported styles. The default looks for
# a '.clang-format' or '_clang-format' file to indicate the style that should be
# used.
style = 'file'
fallback_style = None
if vim.eval('exists("g:clang_format_fallback_style")') == "1":
  fallback_style = vim.eval('g:clang_format_fallback_style')

def get_buffer(encoding):
  if platform.python_version_tuple()[0] == '3':
    return vim.current.buffer
  return [ line.decode(encoding) for line in vim.current.buffer ]

def main():
  # Get the current text.
  encoding = vim.eval("&encoding")
  buf = get_buffer(encoding)
  text = '\n'.join(buf)

  # Determine range to format.
  if vim.eval('exists("l:lines")') == '1':
    lines = ['-lines', vim.eval('l:lines')]
  elif vim.eval('exists("l:formatdiff")') == '1':
    with open(vim.current.buffer.name, 'r') as f:
      ondisk = f.read().splitlines();
    sequence = difflib.SequenceMatcher(None, ondisk, vim.current.buffer)
    lines = []
    for op in reversed(sequence.get_opcodes()):
      if op[0] not in ['equal', 'delete']:
        lines += ['-lines', '%s:%s' % (op[3] + 1, op[4])]
    if lines == []:
      return
  else:
    lines = ['-lines', '%s:%s' % (vim.current.range.start + 1,
                                  vim.current.range.end + 1)]

  # Determine the cursor position.
  cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
  if cursor < 0:
    print('Couldn\'t determine cursor position. Is your file empty?')
    return

  # Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
  startupinfo = None
  if sys.platform.startswith('win32'):
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    startupinfo.wShowWindow = subprocess.SW_HIDE

  # Call formatter.
  command = [binary, '-style', style, '-cursor', str(cursor)]
  if lines != ['-lines', 'all']:
    command += lines
  if fallback_style:
    command.extend(['-fallback-style', fallback_style])
  if vim.current.buffer.name:
    command.extend(['-assume-filename', vim.current.buffer.name])
  p = subprocess.Popen(command,
                       stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                       stdin=subprocess.PIPE, startupinfo=startupinfo)
  stdout, stderr = p.communicate(input=text.encode(encoding))

  # If successful, replace buffer contents.
  if stderr:
    print(stderr)

  if not stdout:
    print(
        'No output from clang-format (crashed?).\n'
        'Please report to bugs.llvm.org.'
    )
  else:
    lines = stdout.decode(encoding).split('\n')
    output = json.loads(lines[0])
    lines = lines[1:]
    sequence = difflib.SequenceMatcher(None, buf, lines)
    for op in reversed(sequence.get_opcodes()):
      if op[0] is not 'equal':
        vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
    if output.get('IncompleteFormat'):
      print('clang-format: incomplete (syntax errors)')
    vim.command('goto %d' % (output['Cursor'] + 1))

main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from __future__ import division, unicode_literals

import logging
import os
import subprocess

from spreads.plugin import HookPlugin
from spreads.util import MissingDependencyException, find_in_path

if not find_in_path('pdfbeads'):
    raise MissingDependencyException("Could not find executable `pdfbeads` in"
                                     " $PATH. Please install the appropriate"
                                     " package(s)!")

logger = logging.getLogger('spreadsplug.pdfbeads')

class PDFBeadsPlugin(HookPlugin):
    __name__ = 'pdfbeads'

    def output(self, path):
        logger.info("Assembling PDF.")
        img_dir = path / 'done'
        pdf_file = path / 'out' / "{0}.pdf".format(path.name)
        img_files = [unicode(x) for x in sorted(img_dir.glob('*.tif'))]
        cmd = ["pdfbeads", "-d"] + img_files + ["-o", unicode(pdf_file)]
        logger.debug("Running " + " ".join(cmd))
        # NOTE: pdfbeads only finds *html files for the text layer in the
        #       working directory...
        os.chdir(unicode(img_dir))
        _ = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
<commit_msg>pdfbeads: Fix path handling<commit_after># -*- coding: utf-8 -*-

from __future__ import division, unicode_literals

import logging
import os
import subprocess

from spreads.plugin import HookPlugin
from spreads.util import MissingDependencyException, find_in_path

if not find_in_path('pdfbeads'):
    raise MissingDependencyException("Could not find executable `pdfbeads` in"
                                     " $PATH. Please install the appropriate"
                                     " package(s)!")

logger = logging.getLogger('spreadsplug.pdfbeads')

class PDFBeadsPlugin(HookPlugin):
    __name__ = 'pdfbeads'

    def output(self, path):
        logger.info("Assembling PDF.")
        img_dir = path / 'done'
        pdf_file = path / 'out' / "{0}.pdf".format(path.name)
        img_files = [unicode(x.name) for x in sorted(img_dir.glob('*.tif'))]
        cmd = ["pdfbeads", "-d"] + img_files + ["-o", unicode(pdf_file)]
        logger.debug("Running " + " ".join(cmd))
        # NOTE: pdfbeads only finds *html files for the text layer in the
        #       working directory...
        os.chdir(unicode(img_dir))
        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        logger.debug("Output:\n{0}".format(output))
<|endoftext|>


<commit_before>from spyvm import model_display, model
from spyvm.error import PrimitiveFailedError
from spyvm.storage import AbstractCachingShadow
from spyvm.plugins.plugin import Plugin

from rpython.rlib import jit, objectmodel
from rpython.rlib.rarithmetic import r_uint, intmask

BitBltPlugin = Plugin()

def send(interp, s_frame, w_selector):
    from spyvm.interpreter import Return
    from spyvm.error import MethodNotFound
    try:
        s_frame._sendSelfSelector(w_selector, 0, interp)
    except Return, ret:
        return True
    except MethodNotFound:
        return False
    return False

def image_side_copy_bits(interp, s_frame, w_rcvr, w_method):
    if interp.image.w_simulateCopyBits is not interp.space.w_nil:
        return send(interp, s_frame, interp.image.w_simulateCopyBits)
    elif (interp.image.w_copyBitsSimulated is not interp.space.w_nil and
          "copyBitsAgain" == w_method.lookup_selector):
        return send(interp, s_frame, interp.image.w_copyBitsSimulated)
    else:
        raise PrimitiveFailedError("BitBlt not implemented in the VM")

def maybe_flush_screen(interp, w_rcvr):
    import spyvm.model_display
    w_dest_form = w_rcvr.fetch(interp.space, 0)
    w_display = interp.space.objtable['w_display']
    if w_dest_form.is_same_object(w_display):
        w_bitmap = w_display.fetch(interp.space, 0)
        assert isinstance(w_bitmap, model_display.W_DisplayBitmap)
        w_bitmap.flush_to_screen()

@BitBltPlugin.expose_primitive(unwrap_spec=None, clean_stack=True, compiled_method=True)
def primitiveCopyBits(interp, s_frame, argcount, w_method):
    if argcount == 0:
        w_rcvr = s_frame.peek(0)
    else:
        assert argcount == 1
        w_rcvr = s_frame.peek(1)

    if not isinstance(w_rcvr, model.W_PointersObject) or w_rcvr.size() < 15:
        raise PrimitiveFailedError("BitBlt primitive not called in BitBlt object!")

    image_side_copy_bits(interp, s_frame, w_rcvr, w_method)
    maybe_flush_screen(interp, w_rcvr)
    return w_rcvr
<commit_msg>don't force flush<commit_after>from spyvm import model_display, model
from spyvm.error import PrimitiveFailedError
from spyvm.storage import AbstractCachingShadow
from spyvm.plugins.plugin import Plugin

from rpython.rlib import jit, objectmodel
from rpython.rlib.rarithmetic import r_uint, intmask

BitBltPlugin = Plugin()

def send(interp, s_frame, w_selector):
    from spyvm.interpreter import Return
    from spyvm.error import MethodNotFound
    try:
        s_frame._sendSelfSelector(w_selector, 0, interp)
    except Return, ret:
        return True
    except MethodNotFound:
        return False
    return False

def image_side_copy_bits(interp, s_frame, w_rcvr, w_method):
    if interp.image.w_simulateCopyBits is not interp.space.w_nil:
        return send(interp, s_frame, interp.image.w_simulateCopyBits)
    elif (interp.image.w_copyBitsSimulated is not interp.space.w_nil and
          "copyBitsAgain" == w_method.lookup_selector):
        return send(interp, s_frame, interp.image.w_copyBitsSimulated)
    else:
        raise PrimitiveFailedError("BitBlt not implemented in the VM")

@BitBltPlugin.expose_primitive(unwrap_spec=None, clean_stack=True, compiled_method=True)
def primitiveCopyBits(interp, s_frame, argcount, w_method):
    if argcount == 0:
        w_rcvr = s_frame.peek(0)
    else:
        assert argcount == 1
        w_rcvr = s_frame.peek(1)

    if not isinstance(w_rcvr, model.W_PointersObject) or w_rcvr.size() < 15:
        raise PrimitiveFailedError("BitBlt primitive not called in BitBlt object!")

    image_side_copy_bits(interp, s_frame, w_rcvr, w_method)
    return w_rcvr
<|endoftext|>