<commit_before>from __future__ import unicode_literals

from django.contrib.auth.models import User
from django.core import management
from django.test import TestCase
from django.utils.six import StringIO

from .models import (
    Car, CarDriver, Driver, Group, Membership, Person, UserMembership,
)

class M2MThroughTestCase(TestCase):

    def test_everything(self):
        bob = Person.objects.create(name="Bob")
        jim = Person.objects.create(name="Jim")

        rock = Group.objects.create(name="Rock")
        roll = Group.objects.create(name="Roll")

        frank = User.objects.create_user("frank", "frank@example.com", "password")
        jane = User.objects.create_user("jane", "jane@example.com", "password")

        Membership.objects.create(person=bob, group=rock)
        Membership.objects.create(person=bob, group=roll)
        Membership.objects.create(person=jim, group=rock)

        self.assertQuerysetEqual(
            bob.group_set.all(), [
                "<Group: Rock>",
                "<Group: Roll>",
            ],
            ordered=False
        )

        self.assertQuerysetEqual(
            roll.members.all(), [
                "<Person: Bob>",
            ]
        )

        self.assertRaises(AttributeError, setattr, bob, "group_set", [])
        self.assertRaises(AttributeError, setattr, roll, "members", [])

        self.assertRaises(AttributeError, rock.members.create, name="Anne")
        self.assertRaises(AttributeError, bob.group_set.create, name="Funk")

        UserMembership.objects.create(user=frank, group=rock)
        UserMembership.objects.create(user=frank, group=roll)
        UserMembership.objects.create(user=jane, group=rock)

        self.assertQuerysetEqual(
            frank.group_set.all(), [
                "<Group: Rock>",
                "<Group: Roll>",
            ],
            ordered=False
        )

        self.assertQuerysetEqual(
            roll.user_members.all(), [
                "<User: frank>",
            ]
        )

    def test_serialization(self):
        "m2m-through models aren't serialized as m2m fields. Refs #8134"

        p = Person.objects.create(name="Bob")
        g = Group.objects.create(name="Roll")
        m = Membership.objects.create(person=p, group=g)

        pks = {"p_pk": p.pk, "g_pk": g.pk, "m_pk": m.pk}

        out = StringIO()
        management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
        self.assertJSONEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks)

        out = StringIO()
        management.call_command("dumpdata", "m2m_through_regress", format="xml",
            indent=2, stdout=out)
        self.assertXMLEqual(out.getvalue().strip(), """
<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
  <object pk="%(m_pk)s" model="m2m_through_regress.membership">
    <field to="m2m_through_regress.person" name="person" rel="ManyToOneRel">%(p_pk)s</field>
    <field to="m2m_through_regress.group" name="group" rel="ManyToOneRel">%(g_pk)s</field>
    <field type="IntegerField" name="price">100</field>
  </object>
  <object pk="%(p_pk)s" model="m2m_through_regress.person">
    <field type="CharField" name="name">Bob</field>
  </object>
  <object pk="%(g_pk)s" model="m2m_through_regress.group">
    <field type="CharField" name="name">Roll</field>
  </object>
</django-objects>
        """.strip() % pks)

    def test_join_trimming(self):
        "Check that we don't involve too many copies of the intermediate table when doing a join. Refs #8046, #8254"
        bob = Person.objects.create(name="Bob")
        jim = Person.objects.create(name="Jim")

        rock = Group.objects.create(name="Rock")
        roll = Group.objects.create(name="Roll")

        Membership.objects.create(person=bob, group=rock)
        Membership.objects.create(person=jim, group=rock, price=50)
        Membership.objects.create(person=bob, group=roll, price=50)

        self.assertQuerysetEqual(
            rock.members.filter(membership__price=50), [
                "<Person: Jim>",
            ]
        )

        self.assertQuerysetEqual(
            bob.group_set.filter(membership__price=50), [
                "<Group: Roll>",
            ]
        )

class ToFieldThroughTests(TestCase):
    def setUp(self):
        self.car = Car.objects.create(make="Toyota")
        self.driver = Driver.objects.create(name="Ryan Briscoe")
        CarDriver.objects.create(car=self.car, driver=self.driver)
        # We are testing if wrong objects get deleted due to using wrong
        # field value in m2m queries. So, it is essential that the pk
        # numberings do not match.
        # Create one intentionally unused driver to mix up the autonumbering
        self.unused_driver = Driver.objects.create(name="Barney Gumble")
        # And two intentionally unused cars.
        self.unused_car1 = Car.objects.create(make="Trabant")
        self.unused_car2 = Car.objects.create(make="Wartburg")

    def test_to_field(self):
        self.assertQuerysetEqual(
            self.car.drivers.all(),
            ["<Driver: Ryan Briscoe>"]
        )

    def test_to_field_reverse(self):
        self.assertQuerysetEqual(
            self.driver.car_set.all(),
            ["<Car: Toyota>"]
        )

    def test_to_field_clear_reverse(self):
        self.driver.car_set.clear()
        self.assertQuerysetEqual(
            self.driver.car_set.all(), [])

    def test_to_field_clear(self):
        self.car.drivers.clear()
        self.assertQuerysetEqual(
            self.car.drivers.all(), [])

    # Low level tests for _add_items and _remove_items. We test these methods
    # because .add/.remove aren't available for m2m fields with through, but
    # through is the only way to set to_field currently. We do want to make
    # sure these methods are ready if the ability to use .add or .remove with
    # to_field relations is added some day.
    def test_add(self):
        self.assertQuerysetEqual(
            self.car.drivers.all(),
            ["<Driver: Ryan Briscoe>"]
        )
        # Yikes - barney is going to drive...
        self.car.drivers._add_items('car', 'driver', self.unused_driver)
        self.assertQuerysetEqual(
            self.car.drivers.all(),
            ["<Driver: Barney Gumble>", "<Driver: Ryan Briscoe>"]
        )

    def test_add_null(self):
        nullcar = Car.objects.create(make=None)
        with self.assertRaises(ValueError):
            nullcar.drivers._add_items('car', 'driver', self.unused_driver)

    def test_add_related_null(self):
        nulldriver = Driver.objects.create(name=None)
        with self.assertRaises(ValueError):
            self.car.drivers._add_items('car', 'driver', nulldriver)

    def test_add_reverse(self):
        car2 = Car.objects.create(make="Honda")
        self.assertQuerysetEqual(
            self.driver.car_set.all(),
            ["<Car: Toyota>"]
        )
        self.driver.car_set._add_items('driver', 'car', car2)
        self.assertQuerysetEqual(
            self.driver.car_set.all(),
            ["<Car: Toyota>", "<Car: Honda>"],
            ordered=False
        )

    def test_add_null_reverse(self):
        nullcar = Car.objects.create(make=None)
        with self.assertRaises(ValueError):
            self.driver.car_set._add_items('driver', 'car', nullcar)

    def test_add_null_reverse_related(self):
        nulldriver = Driver.objects.create(name=None)
        with self.assertRaises(ValueError):
            nulldriver.car_set._add_items('driver', 'car', self.car)

    def test_remove(self):
        self.assertQuerysetEqual(
            self.car.drivers.all(),
            ["<Driver: Ryan Briscoe>"]
        )
        self.car.drivers._remove_items('car', 'driver', self.driver)
        self.assertQuerysetEqual(
            self.car.drivers.all(), [])

    def test_remove_reverse(self):
        self.assertQuerysetEqual(
            self.driver.car_set.all(),
            ["<Car: Toyota>"]
        )
        self.driver.car_set._remove_items('driver', 'car', self.car)
        self.assertQuerysetEqual(
            self.driver.car_set.all(), [])

class ThroughLoadDataTestCase(TestCase):
    fixtures = ["m2m_through"]

    def test_sequence_creation(self):
        "Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107"
        out = StringIO()
        management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
        self.assertJSONEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""")
<commit_msg>Refs #18586 -- Split up tests.m2m_through_regress<commit_after>from __future__ import unicode_literals

from django.contrib.auth.models import User
from django.core import management
from django.test import TestCase
from django.utils.six import StringIO

from .models import (
    Car, CarDriver, Driver, Group, Membership, Person, UserMembership,
)

class M2MThroughTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.bob = Person.objects.create(name="Bob")
        cls.jim = Person.objects.create(name="Jim")

        cls.rock = Group.objects.create(name="Rock")
        cls.roll = Group.objects.create(name="Roll")

        cls.frank = User.objects.create_user("frank", "frank@example.com", "password")
        cls.jane = User.objects.create_user("jane", "jane@example.com", "password")

        # normal intermediate model
        cls.bob_rock = Membership.objects.create(person=cls.bob, group=cls.rock)
        cls.bob_roll = Membership.objects.create(person=cls.bob, group=cls.roll, price=50)
        cls.jim_rock = Membership.objects.create(person=cls.jim, group=cls.rock, price=50)

        # intermediate model with custom id column
        cls.frank_rock = UserMembership.objects.create(user=cls.frank, group=cls.rock)
        cls.frank_roll = UserMembership.objects.create(user=cls.frank, group=cls.roll)
        cls.jane_rock = UserMembership.objects.create(user=cls.jane, group=cls.rock)

    def test_retrieve_reverse_m2m_items(self):
        self.assertQuerysetEqual(
            self.bob.group_set.all(), [
                "<Group: Rock>",
                "<Group: Roll>",
            ],
            ordered=False
        )

    def test_retrieve_forward_m2m_items(self):
        self.assertQuerysetEqual(
            self.roll.members.all(), [
                "<Person: Bob>",
            ]
        )

    def test_cannot_use_setattr_on_reverse_m2m_with_intermediary_model(self):
        self.assertRaises(AttributeError, setattr, self.bob, "group_set", [])

    def test_cannot_use_setattr_on_forward_m2m_with_intermediary_model(self):
        self.assertRaises(AttributeError, setattr, self.roll, "members", [])

    def test_cannot_use_create_on_m2m_with_intermediary_model(self):
        self.assertRaises(AttributeError, self.rock.members.create, name="Anne")

    def test_cannot_use_create_on_reverse_m2m_with_intermediary_model(self):
        self.assertRaises(AttributeError, self.bob.group_set.create, name="Funk")

    def test_retrieve_reverse_m2m_items_via_custom_id_intermediary(self):
        self.assertQuerysetEqual(
            self.frank.group_set.all(), [
                "<Group: Rock>",
                "<Group: Roll>",
            ],
            ordered=False
        )

    def test_retrieve_forward_m2m_items_via_custom_id_intermediary(self):
        self.assertQuerysetEqual(
            self.roll.user_members.all(), [
                "<User: frank>",
            ]
        )

    def test_join_trimming_forwards(self):
        "Check that we don't involve too many copies of the intermediate table when doing a join. Refs #8046, #8254"
        self.assertQuerysetEqual(
            self.rock.members.filter(membership__price=50), [
                "<Person: Jim>",
            ]
        )

    def test_join_trimming_reverse(self):
        self.assertQuerysetEqual(
            self.bob.group_set.filter(membership__price=50), [
                "<Group: Roll>",
            ]
        )

class M2MThroughSerializationTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.bob = Person.objects.create(name="Bob")
        cls.roll = Group.objects.create(name="Roll")
        cls.bob_roll = Membership.objects.create(person=cls.bob, group=cls.roll)

    def test_serialization(self):
        "m2m-through models aren't serialized as m2m fields. Refs #8134"
        pks = {"p_pk": self.bob.pk, "g_pk": self.roll.pk, "m_pk": self.bob_roll.pk}

        out = StringIO()
        management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
        self.assertJSONEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks)

        out = StringIO()
        management.call_command("dumpdata", "m2m_through_regress", format="xml",
            indent=2, stdout=out)
        self.assertXMLEqual(out.getvalue().strip(), """
<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
  <object pk="%(m_pk)s" model="m2m_through_regress.membership">
    <field to="m2m_through_regress.person" name="person" rel="ManyToOneRel">%(p_pk)s</field>
    <field to="m2m_through_regress.group" name="group" rel="ManyToOneRel">%(g_pk)s</field>
    <field type="IntegerField" name="price">100</field>
  </object>
  <object pk="%(p_pk)s" model="m2m_through_regress.person">
    <field type="CharField" name="name">Bob</field>
  </object>
  <object pk="%(g_pk)s" model="m2m_through_regress.group">
    <field type="CharField" name="name">Roll</field>
  </object>
</django-objects>
        """.strip() % pks)

class ToFieldThroughTests(TestCase):
    def setUp(self):
        self.car = Car.objects.create(make="Toyota")
        self.driver = Driver.objects.create(name="Ryan Briscoe")
        CarDriver.objects.create(car=self.car, driver=self.driver)
        # We are testing if wrong objects get deleted due to using wrong
        # field value in m2m queries. So, it is essential that the pk
        # numberings do not match.
        # Create one intentionally unused driver to mix up the autonumbering
        self.unused_driver = Driver.objects.create(name="Barney Gumble")
        # And two intentionally unused cars.
        self.unused_car1 = Car.objects.create(make="Trabant")
        self.unused_car2 = Car.objects.create(make="Wartburg")

    def test_to_field(self):
        self.assertQuerysetEqual(
            self.car.drivers.all(),
            ["<Driver: Ryan Briscoe>"]
        )

    def test_to_field_reverse(self):
        self.assertQuerysetEqual(
            self.driver.car_set.all(),
            ["<Car: Toyota>"]
        )

    def test_to_field_clear_reverse(self):
        self.driver.car_set.clear()
        self.assertQuerysetEqual(
            self.driver.car_set.all(), [])

    def test_to_field_clear(self):
        self.car.drivers.clear()
        self.assertQuerysetEqual(
            self.car.drivers.all(), [])

    # Low level tests for _add_items and _remove_items. We test these methods
    # because .add/.remove aren't available for m2m fields with through, but
    # through is the only way to set to_field currently. We do want to make
    # sure these methods are ready if the ability to use .add or .remove with
    # to_field relations is added some day.
    def test_add(self):
        self.assertQuerysetEqual(
            self.car.drivers.all(),
            ["<Driver: Ryan Briscoe>"]
        )
        # Yikes - barney is going to drive...
        self.car.drivers._add_items('car', 'driver', self.unused_driver)
        self.assertQuerysetEqual(
            self.car.drivers.all(),
            ["<Driver: Barney Gumble>", "<Driver: Ryan Briscoe>"]
        )

    def test_add_null(self):
        nullcar = Car.objects.create(make=None)
        with self.assertRaises(ValueError):
            nullcar.drivers._add_items('car', 'driver', self.unused_driver)

    def test_add_related_null(self):
        nulldriver = Driver.objects.create(name=None)
        with self.assertRaises(ValueError):
            self.car.drivers._add_items('car', 'driver', nulldriver)

    def test_add_reverse(self):
        car2 = Car.objects.create(make="Honda")
        self.assertQuerysetEqual(
            self.driver.car_set.all(),
            ["<Car: Toyota>"]
        )
        self.driver.car_set._add_items('driver', 'car', car2)
        self.assertQuerysetEqual(
            self.driver.car_set.all(),
            ["<Car: Toyota>", "<Car: Honda>"],
            ordered=False
        )

    def test_add_null_reverse(self):
        nullcar = Car.objects.create(make=None)
        with self.assertRaises(ValueError):
            self.driver.car_set._add_items('driver', 'car', nullcar)

    def test_add_null_reverse_related(self):
        nulldriver = Driver.objects.create(name=None)
        with self.assertRaises(ValueError):
            nulldriver.car_set._add_items('driver', 'car', self.car)

    def test_remove(self):
        self.assertQuerysetEqual(
            self.car.drivers.all(),
            ["<Driver: Ryan Briscoe>"]
        )
        self.car.drivers._remove_items('car', 'driver', self.driver)
        self.assertQuerysetEqual(
            self.car.drivers.all(), [])

    def test_remove_reverse(self):
        self.assertQuerysetEqual(
            self.driver.car_set.all(),
            ["<Car: Toyota>"]
        )
        self.driver.car_set._remove_items('driver', 'car', self.car)
        self.assertQuerysetEqual(
            self.driver.car_set.all(), [])

class ThroughLoadDataTestCase(TestCase):
    fixtures = ["m2m_through"]

    def test_sequence_creation(self):
        "Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107"
        out = StringIO()
        management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
        self.assertJSONEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""")
<|endoftext|>


<commit_before>"""
TESTS is a dict with all you tests.
Keys for this will be categories' names.
Each test is dict with
    "input" -- input data for user function
    "answer" -- your right answer
    "explanation" -- not necessary key, it's using for additional info in animation.
"""

TESTS = {
    "X+Y": [
        {
            "f":lambda x,y:x+y,
            "g":lambda x,y:(x**2-y**2)/(x-y),
            "args": (1,3),
            "kwargs": {},
            "result": (4,'same')
        }
    ]
}
<commit_msg>Update tests.py<commit_after>"""
TESTS is a dict with all you tests.
Keys for this will be categories' names.
Each test is dict with
    "input" -- input data for user function
    "answer" -- your right answer
    "explanation" -- not necessary key, it's using for additional info in animation.
"""

TESTS = {
    "X+Y": [
        {
            "input":[lambda x,y:x+y,lambda x,y:(x**2-y**2)/(x-y)],
            "answer":{"args": (1,3),"kwargs": {},"result": (4,'same')},
            "explanation":""
        }
    ]
}
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown Copyright 2017 Met Office.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""
This module defines the utilities required for Ensemble Copula Coupling
plugins.

"""
import copy
import numpy as np
import random

import cf_units as unit
import iris
from iris.exceptions import CoordinateNotFoundError

def create_percentiles(no_of_percentiles, sampling="quantile"):
    """
    Function to create percentiles.

    Parameters
    ----------
    no_of_percentiles : Int
        Number of percentiles.
    sampling : String
        Type of sampling of the distribution to produce a set of
        percentiles e.g. quantile or random.
        Accepted options for sampling are:
        Quantile: A regular set of equally-spaced percentiles aimed
                  at dividing a Cumulative Distribution Function into
                  blocks of equal probability.
        Random: A random set of ordered percentiles.

    For further details, Flowerdew, J., 2014.
    Calibrating ensemble reliability whilst preserving spatial structure.
    Tellus, Series A: Dynamic Meteorology and Oceanography, 66(1), pp.1-20.
    Schefzik, R., Thorarinsdottir, T.L. & Gneiting, T., 2013.
    Uncertainty Quantification in Complex Simulation Models Using Ensemble
    Copula Coupling.
    Statistical Science, 28(4), pp.616-640.

    Returns
    -------
    percentiles : List
        Percentiles calculated using the sampling technique specified.

    """
    if sampling in ["quantile"]:
        percentiles = np.linspace(
            1/float(1+no_of_percentiles),
            no_of_percentiles/float(1+no_of_percentiles),
            no_of_percentiles).tolist()
    elif sampling in ["random"]:
        percentiles = []
        for _ in range(no_of_percentiles):
            percentiles.append(
                random.uniform(
                    1/float(1+no_of_percentiles),
                    no_of_percentiles/float(1+no_of_percentiles)))
        percentiles = sorted(percentiles)
    else:
        msg = "The {} sampling option is not yet implemented.".format(
            sampling)
        raise ValueError(msg)
    return percentiles

def create_cube_with_percentiles(percentiles, template_cube, cube_data):
    """
    Create a cube with a percentile coordinate based on a template cube.
    The resulting cube will have an extra percentile coordinate compared with
    the input cube. The shape of the cube_data should be the shape of the
    desired output cube.

    Parameters
    ----------
    percentiles : List
        Ensemble percentiles. There should be the same number of percentiles
        as the first dimension of cube_data.
    template_cube : Iris cube
        Cube to copy all coordinates from.
        Metadata is also copied from this cube.
    cube_data : Numpy array
        Data to insert into the template cube.
        The shape of the cube_data, excluding the dimension associated with
        the percentile coordinate, should be the same as the shape of
        template_cube.
        For example, template_cube shape is (3, 3, 3), whilst the cube_data
        is (10, 3, 3, 3), where there are 10 percentiles.

    Returns
    -------
    result : Iris.cube.Cube
        Cube containing a percentile coordinate as the zeroth dimension
        coordinate in addition to the coordinates and metadata from the
        template cube.

    """
    percentile_coord = iris.coords.DimCoord(
        np.float32(percentiles), long_name="percentile",
        units=unit.Unit("1"), var_name="percentile")

    metadata_dict = copy.deepcopy(template_cube.metadata._asdict())
    result = iris.cube.Cube(cube_data, **metadata_dict)
    result.add_dim_coord(percentile_coord, 0)

    # For the dimension coordinates, the dimensions are incremented by one,
    # as the percentile coordinate has been added as the zeroth coordinate.
    # The dimension associated with the auxiliary and derived coordinates
    # has also been incremented by one.
    for coord in template_cube.dim_coords:
        dim, = template_cube.coord_dims(coord)
        result.add_dim_coord(coord.copy(), dim+1)
    for coord in template_cube.aux_coords:
        dims = template_cube.coord_dims(coord)
        dims = tuple([dim+1 for dim in dims])
        result.add_aux_coord(coord.copy(), dims)
    for coord in template_cube.derived_coords:
        dims = template_cube.coord_dims(coord)
        dims = tuple([dim+1 for dim in dims])
        result.add_aux_coord(coord.copy(), dims)
    return result
<commit_msg>Pylint edit to ensemble_copula_coupling_utilities.py.<commit_after># -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown Copyright 2017 Met Office.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""
This module defines the utilities required for Ensemble Copula Coupling
plugins.

"""
import copy
import numpy as np
import random

import cf_units as unit
import iris

def create_percentiles(no_of_percentiles, sampling="quantile"):
    """
    Function to create percentiles.

    Parameters
    ----------
    no_of_percentiles : Int
        Number of percentiles.
    sampling : String
        Type of sampling of the distribution to produce a set of
        percentiles e.g. quantile or random.
        Accepted options for sampling are:
        Quantile: A regular set of equally-spaced percentiles aimed
                  at dividing a Cumulative Distribution Function into
                  blocks of equal probability.
        Random: A random set of ordered percentiles.

    For further details, Flowerdew, J., 2014.
    Calibrating ensemble reliability whilst preserving spatial structure.
    Tellus, Series A: Dynamic Meteorology and Oceanography, 66(1), pp.1-20.
    Schefzik, R., Thorarinsdottir, T.L. & Gneiting, T., 2013.
    Uncertainty Quantification in Complex Simulation Models Using Ensemble
    Copula Coupling.
    Statistical Science, 28(4), pp.616-640.

    Returns
    -------
    percentiles : List
        Percentiles calculated using the sampling technique specified.

    """
    if sampling in ["quantile"]:
        percentiles = np.linspace(
            1/float(1+no_of_percentiles),
            no_of_percentiles/float(1+no_of_percentiles),
            no_of_percentiles).tolist()
    elif sampling in ["random"]:
        percentiles = []
        for _ in range(no_of_percentiles):
            percentiles.append(
                random.uniform(
                    1/float(1+no_of_percentiles),
                    no_of_percentiles/float(1+no_of_percentiles)))
        percentiles = sorted(percentiles)
    else:
        msg = "The {} sampling option is not yet implemented.".format(
            sampling)
        raise ValueError(msg)
    return percentiles

def create_cube_with_percentiles(percentiles, template_cube, cube_data):
    """
    Create a cube with a percentile coordinate based on a template cube.
    The resulting cube will have an extra percentile coordinate compared with
    the input cube. The shape of the cube_data should be the shape of the
    desired output cube.

    Parameters
    ----------
    percentiles : List
        Ensemble percentiles. There should be the same number of percentiles
        as the first dimension of cube_data.
    template_cube : Iris cube
        Cube to copy all coordinates from.
        Metadata is also copied from this cube.
    cube_data : Numpy array
        Data to insert into the template cube.
        The shape of the cube_data, excluding the dimension associated with
        the percentile coordinate, should be the same as the shape of
        template_cube.
        For example, template_cube shape is (3, 3, 3), whilst the cube_data
        is (10, 3, 3, 3), where there are 10 percentiles.

    Returns
    -------
    result : Iris.cube.Cube
        Cube containing a percentile coordinate as the zeroth dimension
        coordinate in addition to the coordinates and metadata from the
        template cube.

    """
    percentile_coord = iris.coords.DimCoord(
        np.float32(percentiles), long_name="percentile",
        units=unit.Unit("1"), var_name="percentile")

    metadata_dict = copy.deepcopy(template_cube.metadata._asdict())
    result = iris.cube.Cube(cube_data, **metadata_dict)
    result.add_dim_coord(percentile_coord, 0)

    # For the dimension coordinates, the dimensions are incremented by one,
    # as the percentile coordinate has been added as the zeroth coordinate.
    # The dimension associated with the auxiliary and derived coordinates
    # has also been incremented by one.
    for coord in template_cube.dim_coords:
        dim, = template_cube.coord_dims(coord)
        result.add_dim_coord(coord.copy(), dim+1)
    for coord in template_cube.aux_coords:
        dims = template_cube.coord_dims(coord)
        dims = tuple([dim+1 for dim in dims])
        result.add_aux_coord(coord.copy(), dims)
    for coord in template_cube.derived_coords:
        dims = template_cube.coord_dims(coord)
        dims = tuple([dim+1 for dim in dims])
        result.add_aux_coord(coord.copy(), dims)
    return result
<|endoftext|>


<commit_before># Copyright (C) Osama Khalid 2011. Released under AGPLv3+.
# Please wirte your feedbacks to [[User_talk:OsamaK]].

# This script updates Alexa rankings depending on a list on
# [[User:OsamaK/AlexaBot.js]]. The syntax of the list is:
#     "Example (website) example.com"
# It could optionally include the "local" flag to fetch the local
# Alexa ranking (the one beside the 'Global ranking'):
#     "Example (website) example.com local"

import re
import urllib
import shelve
import time
from datetime import datetime

import wikipedia

class alexaBot:
    def __init__(self):
        self.database = shelve.open('alexa_rankings.db')
        self.now = datetime.now()
        self.month_names = ['January', 'February', 'March', 'April', 'May',
                       'June', 'July', 'August', 'September',
                       'October', 'November', 'December']
        self.site = wikipedia.getSite()

    def get_article_list(self):
        list_regex = '"(.+)" ([^ \n]+)[ ]?(local)?'
        list_page = wikipedia.Page(self.site,'User:OsamaK/AlexaBot.js').get()
        articles_list = re.findall(list_regex, list_page)

        #print articles_list #FIXME: REMOVE
        return articles_list

    def get_alexa_ranking(self, alexa_url, article):
        ranking_regex  = '([\d,]+)[ \t]+\
\n\
Global Rank'
        local_ranking_regex = '([\d,]+)[ \t]+\
\n\
' \
                              'Rank in\n\<a href=\'[^\']+\' title="([\w ]+)"'
        title_regex = '\<title\>(.+)\</title\>'

        print "Fetching", alexa_url
        while True:
            try:
                alexa_text = urllib.urlopen(alexa_url).read()
                break
            except IOError:
                print "Error fetching Alexa page. Retyring in 10" \
                      " seconds."
                time.sleep(10)
                continue

        alexa_ranking = re.findall(ranking_regex, alexa_text)[0]
        alexa_title = re.findall(title_regex, alexa_text)[0]
        if 'local' in article:
            alexa_local_ranking, alexa_local_country = re.findall(
                              local_ranking_regex, alexa_text)[0]
            local_ranking_text = "; {{Flagicon|%(country)s}} %(ranking)s" % \
                                 {"country": alexa_local_country,
                                  "ranking": alexa_local_ranking}
        else:
            local_ranking_text = ""

        new_ranking = int(alexa_ranking.replace(',', ''))
        difference = self.find_difference(str(article[1]), new_ranking)

        ranking_text = "%(diff)s%(g_ranking)s%(l_ranking)s" % \
                      {"diff": difference, "g_ranking": alexa_ranking,
                       "l_ranking": local_ranking_text}

        return ranking_text, alexa_title, new_ranking

    def find_difference(self, article_url, new_ranking):
        try:
            old_ranking = self.database[article_url]
        except KeyError: # If the website is newly added.
            old_ranking = 0

        print "New Alexa ranking is", new_ranking, "old was", old_ranking

        if old_ranking == 0:
            difference = ""
        elif old_ranking > new_ranking:
            difference = "{{increase}} "
        elif old_ranking < new_ranking:
            difference = "{{decrease}} "
        elif old_ranking == new_ranking:
            difference = "{{steady}} "

        return difference

    def save_article(self, article_object, article_text, article_url,
                       old_alexa_field, new_alexa_field, new_ranking):
        article_text = article_text.replace(old_alexa_field, new_alexa_field)
        article_object.put(article_text, comment="Bot: Updating" \
                          "Alexa ranking ([[User talk:OsamaK/" \
                          "AlexaBot.js|Help get more pages covered]]")
        time.sleep(10)
        self.database[article_url] = new_ranking

    def run(self):
        alexa_field_regex = "\|[ ]*alexa[ ]*=[ ]*.+[\|\n]"
        old_ranking_regex = "\|[ ]*alexa[ ]*=[ ]*(.+)[\|\n]"
        reference_regex = "(\<references|\{\{(reference|refs|re|listaref" \
                          "|ref-list|reflist|footnotesmall|reference list" \
                          "|ref list))"

        print "Fetching articles list.."
        articles_list = self.get_article_list()

        if self.database == {}: # If this is the first time.
            print "This seems to be the first time. No difference templete" \
                  " will be added."
            for article in articles_list:
                self.database[str(article[1])] = 0

        for article in articles_list:
            article_name = article[0]
            article_url = str(article[1])
            alexa_url = "http://www.alexa.com/siteinfo/" + article_url
            article_object = wikipedia.Page(self.site, article_name)

            print "Fetching %s page on Wikipedia.." % article_name
            try:
                article_text = article_object.get()
            except wikipedia.pywikibot.exceptions.NoPage:
                print "Page %s does not exist." % article_name
                continue

            if not re.search(reference_regex, article_text, flags=re.IGNORECASE):
                print "No refereence list in", article_name
                continue

            try:
                old_alexa_field = re.findall(alexa_field_regex, article_text)[0]
            except IndexError:
                print "No alexa field in", article_name
                continue

            ranking_text, alexa_title, new_ranking = self.get_alexa_ranking(
                                                   alexa_url, article)

            old_field_ranking = re.findall(old_ranking_regex, old_alexa_field)[0]
            new_field_ranking = "%(ranking_text)s ({{as of|%(year)d|%(month)d|%(day)d" \
                                "|alt=%(month_name)s %(year)d}})<ref name=\"alexa\">" \
                                "{{cite web|url= %(url)s |title= %(title)s " \
                                "| publisher= [[Alexa Internet]] " \
                                "|accessdate= %(year)d-%(month)02d-%(day)02d }}</ref>" \
                                "" % \
                             {"ranking_text": ranking_text, "title": alexa_title,
                              "url": alexa_url, "year": self.now.year,
                              "month": self.now.month, "day": self.now.day,
                              "month_name": self.month_names[self.now.month-1]}

            new_alexa_field = old_alexa_field.replace(old_field_ranking, new_field_ranking)
            #print new_alexa_field #FIXME: REMOVE

            self.save_article(article_object, article_text, article_url,
                              old_alexa_field, new_alexa_field,
                              new_ranking)

        self.database.close()

if __name__ == '__main__':
    try:
        bot = alexaBot()
        bot.run()
    finally:
        wikipedia.stopme()
<commit_msg>Adding 'Couldn't find any ranking data on' error message<commit_after># Copyright (C) Osama Khalid 2011. Released under AGPLv3+.
# Please wirte your feedbacks to [[User_talk:OsamaK]].

# This script updates Alexa rankings depending on a list on
# [[User:OsamaK/AlexaBot.js]]. The syntax of the list is:
#     "Example (website) example.com"
# It could optionally include the "local" flag to fetch the local
# Alexa ranking (the one beside the 'Global ranking'):
#     "Example (website) example.com local"

import re
import urllib
import shelve
import time
from datetime import datetime

import wikipedia

class alexaBot:
    def __init__(self):
        self.database = shelve.open('alexa_rankings.db')
        self.now = datetime.now()
        self.month_names = ['January', 'February', 'March', 'April', 'May',
                       'June', 'July', 'August', 'September',
                       'October', 'November', 'December']
        self.site = wikipedia.getSite()

    def get_article_list(self):
        list_regex = '"(.+)" ([^ \n]+)[ ]?(local)?'
        list_page = wikipedia.Page(self.site,'User:OsamaK/AlexaBot.js').get()
        articles_list = re.findall(list_regex, list_page)

        #print articles_list #FIXME: REMOVE
        return articles_list

    def get_alexa_ranking(self, alexa_url, article):
        ranking_regex  = '([\d,]+)[ \t]+\
\n\
Global Rank'
        local_ranking_regex = '([\d,]+)[ \t]+\
\n\
' \
                              'Rank in\n\<a href=\'[^\']+\' title="([\w ]+)"'
        title_regex = '\<title\>(.+)\</title\>'

        print "Fetching", alexa_url
        while True:
            try:
                alexa_text = urllib.urlopen(alexa_url).read()
                break
            except IOError:
                print "Error fetching Alexa page. Retyring in 10" \
                      " seconds."
                time.sleep(10)
                continue

        alexa_ranking = re.findall(ranking_regex, alexa_text)[0]
        alexa_title = re.findall(title_regex, alexa_text)[0]
        if 'local' in article:
            alexa_local_ranking, alexa_local_country = re.findall(
                              local_ranking_regex, alexa_text)[0]
            local_ranking_text = "; {{Flagicon|%(country)s}} %(ranking)s" % \
                                 {"country": alexa_local_country,
                                  "ranking": alexa_local_ranking}
        else:
            local_ranking_text = ""

        new_ranking = int(alexa_ranking.replace(',', ''))
        difference = self.find_difference(str(article[1]), new_ranking)

        ranking_text = "%(diff)s%(g_ranking)s%(l_ranking)s" % \
                      {"diff": difference, "g_ranking": alexa_ranking,
                       "l_ranking": local_ranking_text}

        return ranking_text, alexa_title, new_ranking

    def find_difference(self, article_url, new_ranking):
        try:
            old_ranking = self.database[article_url]
        except KeyError: # If the website is newly added.
            old_ranking = 0

        print "New Alexa ranking is", new_ranking, "old was", old_ranking

        if old_ranking == 0:
            difference = ""
        elif old_ranking > new_ranking:
            difference = "{{increase}} "
        elif old_ranking < new_ranking:
            difference = "{{decrease}} "
        elif old_ranking == new_ranking:
            difference = "{{steady}} "

        return difference

    def save_article(self, article_object, article_text, article_url,
                       old_alexa_field, new_alexa_field, new_ranking):
        article_text = article_text.replace(old_alexa_field, new_alexa_field)
        article_object.put(article_text, comment="Bot: Updating " \
                          "Alexa ranking ([[User talk:OsamaK/" \
                          "AlexaBot.js|Help get more pages covered]]")
        time.sleep(10)
        self.database[article_url] = new_ranking

    def run(self):
        alexa_field_regex = "\|[ ]*alexa[ ]*=[ ]*.+[\|\n]"
        old_ranking_regex = "\|[ ]*alexa[ ]*=[ ]*(.+)[\|\n]"
        reference_regex = "(\<references|\{\{(reference|refs|re|listaref" \
                          "|ref-list|reflist|footnotesmall|reference list" \
                          "|ref list))"

        print "Fetching articles list.."
        articles_list = self.get_article_list()

        if self.database == {}: # If this is the first time.
            print "This seems to be the first time. No difference templete" \
                  " will be added."
            for article in articles_list:
                self.database[str(article[1])] = 0

        for article in articles_list:
            article_name = article[0]
            article_url = str(article[1])
            alexa_url = "http://www.alexa.com/siteinfo/" + article_url
            article_object = wikipedia.Page(self.site, article_name)

            print "Fetching %s page on Wikipedia.." % article_name
            try:
                article_text = article_object.get()
            except wikipedia.pywikibot.exceptions.NoPage:
                print "Page %s does not exist." % article_name
                continue

            if not re.search(reference_regex, article_text, flags=re.IGNORECASE):
                print "No refereence list in", article_name
                continue

            try:
                old_alexa_field = re.findall(alexa_field_regex, article_text)[0]
            except IndexError:
                print "No alexa field in", article_name
                continue

            try:
                ranking_text, alexa_title, new_ranking = self.get_alexa_ranking(
                                                   alexa_url, article)
            except IndexError:
                print "Couldn't find any ranking data on", alexa_url
                continue

            old_field_ranking = re.findall(old_ranking_regex, old_alexa_field)[0]
            new_field_ranking = "%(ranking_text)s ({{as of|%(year)d|%(month)d|%(day)d" \
                                "|alt=%(month_name)s %(year)d}})<ref name=\"alexa\">" \
                                "{{cite web|url= %(url)s |title= %(title)s " \
                                "| publisher= [[Alexa Internet]] " \
                                "|accessdate= %(year)d-%(month)02d-%(day)02d }}</ref>" \
                                "" % \
                             {"ranking_text": ranking_text, "title": alexa_title,
                              "url": alexa_url, "year": self.now.year,
                              "month": self.now.month, "day": self.now.day,
                              "month_name": self.month_names[self.now.month-1]}

            new_alexa_field = old_alexa_field.replace(old_field_ranking, new_field_ranking)
            #print new_alexa_field #FIXME: REMOVE

            self.save_article(article_object, article_text, article_url,
                              old_alexa_field, new_alexa_field,
                              new_ranking)

        self.database.close()

if __name__ == '__main__':
    try:
        bot = alexaBot()
        bot.run()
    finally:
        wikipedia.stopme()
<|endoftext|>


<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2013 OpenStack Foundation
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#

"""nvp_mac_learning

Revision ID: 3cbf70257c28
Revises: 176a85fc7d79
Create Date: 2013-05-15 10:15:50.875314

"""

# revision identifiers, used by Alembic.
revision = '3cbf70257c28'
down_revision = '176a85fc7d79'

# Change to ['*'] if this migration applies to all plugins

migration_for_plugins = [
    'quantum.plugins.nicira.QuantumPlugin.NvpPluginV2'
]

from alembic import op
import sqlalchemy as sa

from quantum.db import migration

def upgrade(active_plugin=None, options=None):
    if not migration.should_run(active_plugin, migration_for_plugins):
        return

    op.create_table(
        'maclearningstates',
        sa.Column('port_id', sa.String(length=36), nullable=False),
        sa.Column('mac_learning_enabled', sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
        ['port_id'], ['ports.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('port_id'))

def downgrade(active_plugin=None, options=None):
    if not migration.should_run(active_plugin, migration_for_plugins):
        return

    op.drop_table('maclearningstates')
<commit_msg>Fix branch in db migration scripts<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2013 OpenStack Foundation
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#

"""nvp_mac_learning

Revision ID: 3cbf70257c28
Revises: 5ac71e65402c
Create Date: 2013-05-15 10:15:50.875314

"""

# revision identifiers, used by Alembic.
revision = '3cbf70257c28'
down_revision = '5ac71e65402c'

# Change to ['*'] if this migration applies to all plugins

migration_for_plugins = [
    'quantum.plugins.nicira.QuantumPlugin.NvpPluginV2'
]

from alembic import op
import sqlalchemy as sa

from quantum.db import migration

def upgrade(active_plugin=None, options=None):
    if not migration.should_run(active_plugin, migration_for_plugins):
        return

    op.create_table(
        'maclearningstates',
        sa.Column('port_id', sa.String(length=36), nullable=False),
        sa.Column('mac_learning_enabled', sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
        ['port_id'], ['ports.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('port_id'))

def downgrade(active_plugin=None, options=None):
    if not migration.should_run(active_plugin, migration_for_plugins):
        return

    op.drop_table('maclearningstates')
<|endoftext|>


<commit_before>from __future__ import print_function
import os

import pandas as pd
import numpy as np
from ggplot import *

meat = pd.read_csv(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ggplot', 'exampledata', 'meat.csv'))
meat['date'] = pd.to_datetime(meat.date)

df = pd.DataFrame({
    "x": np.arange(0, 100),
    "y": np.arange(0, 100),
    "z": np.arange(0, 100)
})

df['cat'] = np.where(df.x*2 > 50, 'blah', 'blue')
df['cat'] = np.where(df.y > 50, 'hello', df.cat)
df['cat2'] = np.where(df.y < 15, 'one', 'two')
df['y'] = np.sin(df.y)

gg = ggplot(aes(x="x", y="z", color="cat", alpha=0.2), data=df)
gg = ggplot(aes(x="x", color="c"), data=pd.DataFrame({"x": np.random.normal(0, 1, 10000), "c": ["blue" if i%2==0 else "red" for i in range(10000)]}))
#print gg + geom_density() + xlab("x label") + ylab("y label")
gg = ggplot(aes(x="x", y="y", shape="cat2", color="cat"), data=df)
#print gg + geom_point() + facet_wrap(x="cat", y="cat2")
#print gg + geom_point() + facet_wrap(y="cat2") + ggtitle("My Single Facet")
#print gg + stat_smooth(color="blue") + ggtitle("My Smoothed Chart")
#print gg + geom_hist() + ggtitle("My Histogram")
#print gg + geom_point() + geom_vline(x=50, ymin=-10, ymax=10)
#print gg + geom_point() + geom_hline(y=50, xmin=-10, xmax=10)
df['z'] = df['y'] + 100
gg = ggplot(aes(x='x', ymax='y', ymin='z'), data=df)
#print gg + geom_bar() + facet_wrap(x="cat2")
#print gg + geom_area() + facet_wrap(x="cat2")
gg = ggplot(aes(x='x', ymax='y', ymin='z', color="cat2"), data=df)
#print gg + geom_area()
df['x'] = np.random.randint(0, 10, 100)
df['y'] = np.random.randint(0, 10, 100)
gg = ggplot(aes(x='x', y='y', shape='cat', color='cat2'), data=df)
#print df.head()
#print gg + geom_point()

#print gg + stat_bin2d()

#print ggplot(aes(x='mpg', fill=True, alpha=0.3), data=mtcars) + \
 #       geom_density()
#plt.show(block=True)

#p = ggplot(mtcars, aes(x='wt', y='mpg', colour='factor(cyl)', size='mpg', linetype='factor(cyl)'))
#print p + geom_line() + geom_point()

# p + geom_point() + geom_line(color='lightblue') + ggtitle("Beef: It's What's for Dinner") + xlab("Date") + ylab("Head of Cattle Slaughtered")

meat_lng = pd.melt(meat[['date', 'beef', 'broilers', 'pork']], id_vars=['date'])
meat_lng = pd.melt(meat, id_vars=['date'])

p = ggplot(aes(x='date', y='value', colour='variable', fill=True, alpha=0.3), data=meat_lng)
#print p + geom_density() + facet_wrap("variable")
print(p + geom_line() + facet_wrap("variable"))
plt.show(1)
# ggsave(p + geom_density(), "densityplot.png")

p = ggplot(aes(x="date", y="value", colour="variable", shape="variable"), meat_lng)
#print p + geom_point() + facet_grid(y="variable")
p = p + stat_smooth(se=False) + geom_point()

p = ggplot(aes(x='date', y='beef'), data=meat)
# print p + geom_point() + stat_smooth(se=True)

#p = ggplot(aes(x='x', y='y', colour='z'), data=diamonds.head(4))
#print p + geom_point() + \
#    scale_colour_gradient(low="white", high="red") + \
#    facet_wrap("cut")
#plt.show(block=True)

#p = ggplot(aes(x='x', y='y', colour='z'), data=diamonds.head(1000))
#print p + geom_point() + \
#    scale_colour_gradient(low="white", high="red") + \
#    facet_grid("cut", "clarity")
#plt.show(block=True)

#p = ggplot(aes(x='date', y='beef'), data=meat)
#print p + geom_point() + scale_x_continuous("This is the X") + scale_y_continuous("Squared", limits=[0, 1500])
#print p + geom_point() + ylim(0, 1500)
#gg = ggplot(aes(x='date', y='beef'), data=meat)
#print gg + stat_smooth(se=True)

#print ggplot(aes(x='date', y='beef'), data=meat) + geom_line() + \
#    scale_x_date(labels="%Y-%m-%d")
#plt.show(block=True)

#p = ggplot(aes(x='carat'), data=diamonds)
#print p + geom_now_its_art()
#print p + geom_density() + facet_grid("cut", "clarity")
#plt.show(block=True)

p = ggplot(aes(x='factor(cyl)'), data=mtcars)
print(p + geom_bar())
plt.show(block=True)
#ggsave(p + geom_bar(), "public/img/mtcars_geom_bar_cyl.png")

p = ggplot(aes(x='date_hour', y='pageviews'), data=pageviews)
print(p + geom_point())
plt.show(1)

<commit_msg>make example.py code runable under ipython<commit_after>from __future__ import print_function
import os

import pandas as pd
import numpy as np
from ggplot import *

import ggplot as gg
os.path.join(os.path.dirname(gg.__file__),   'exampledata', 'meat.csv')
meat = pd.read_csv(os.path.join(os.path.dirname(gg.__file__),   'exampledata', 'meat.csv'))
meat['date'] = pd.to_datetime(meat.date)

df = pd.DataFrame({
    "x": np.arange(0, 100),
    "y": np.arange(0, 100),
    "z": np.arange(0, 100)
})

df['cat'] = np.where(df.x*2 > 50, 'blah', 'blue')
df['cat'] = np.where(df.y > 50, 'hello', df.cat)
df['cat2'] = np.where(df.y < 15, 'one', 'two')
df['y'] = np.sin(df.y)

gg = ggplot(aes(x="x", y="z", color="cat", alpha=0.2), data=df)
gg = ggplot(aes(x="x", color="c"), data=pd.DataFrame({"x": np.random.normal(0, 1, 10000), "c": ["blue" if i%2==0 else "red" for i in range(10000)]}))
#print gg + geom_density() + xlab("x label") + ylab("y label")
gg = ggplot(aes(x="x", y="y", shape="cat2", color="cat"), data=df)
#print gg + geom_point() + facet_wrap(x="cat", y="cat2")
#print gg + geom_point() + facet_wrap(y="cat2") + ggtitle("My Single Facet")
#print gg + stat_smooth(color="blue") + ggtitle("My Smoothed Chart")
#print gg + geom_hist() + ggtitle("My Histogram")
#print gg + geom_point() + geom_vline(x=50, ymin=-10, ymax=10)
#print gg + geom_point() + geom_hline(y=50, xmin=-10, xmax=10)
df['z'] = df['y'] + 100
gg = ggplot(aes(x='x', ymax='y', ymin='z'), data=df)
#print gg + geom_bar() + facet_wrap(x="cat2")
#print gg + geom_area() + facet_wrap(x="cat2")
gg = ggplot(aes(x='x', ymax='y', ymin='z', color="cat2"), data=df)
#print gg + geom_area()
df['x'] = np.random.randint(0, 10, 100)
df['y'] = np.random.randint(0, 10, 100)
gg = ggplot(aes(x='x', y='y', shape='cat', color='cat2'), data=df)
#print df.head()
#print gg + geom_point()

#print gg + stat_bin2d()

#print ggplot(aes(x='mpg', fill=True, alpha=0.3), data=mtcars) + \
 #       geom_density()
#plt.show(block=True)

#p = ggplot(mtcars, aes(x='wt', y='mpg', colour='factor(cyl)', size='mpg', linetype='factor(cyl)'))
#print p + geom_line() + geom_point()

# p + geom_point() + geom_line(color='lightblue') + ggtitle("Beef: It's What's for Dinner") + xlab("Date") + ylab("Head of Cattle Slaughtered")

meat_lng = pd.melt(meat[['date', 'beef', 'broilers', 'pork']], id_vars=['date'])
meat_lng = pd.melt(meat, id_vars=['date'])

p = ggplot(aes(x='date', y='value', colour='variable', fill=True, alpha=0.3), data=meat_lng)
#print p + geom_density() + facet_wrap("variable")
print(p + geom_line() + facet_wrap("variable"))
plt.show(1)
# ggsave(p + geom_density(), "densityplot.png")

p = ggplot(aes(x="date", y="value", colour="variable", shape="variable"), meat_lng)
#print p + geom_point() + facet_grid(y="variable")
p = p + stat_smooth(se=False) + geom_point()

p = ggplot(aes(x='date', y='beef'), data=meat)
# print p + geom_point() + stat_smooth(se=True)

#p = ggplot(aes(x='x', y='y', colour='z'), data=diamonds.head(4))
#print p + geom_point() + \
#    scale_colour_gradient(low="white", high="red") + \
#    facet_wrap("cut")
#plt.show(block=True)

#p = ggplot(aes(x='x', y='y', colour='z'), data=diamonds.head(1000))
#print p + geom_point() + \
#    scale_colour_gradient(low="white", high="red") + \
#    facet_grid("cut", "clarity")
#plt.show(block=True)

#p = ggplot(aes(x='date', y='beef'), data=meat)
#print p + geom_point() + scale_x_continuous("This is the X") + scale_y_continuous("Squared", limits=[0, 1500])
#print p + geom_point() + ylim(0, 1500)
#gg = ggplot(aes(x='date', y='beef'), data=meat)
#print gg + stat_smooth(se=True)

#print ggplot(aes(x='date', y='beef'), data=meat) + geom_line() + \
#    scale_x_date(labels="%Y-%m-%d")
#plt.show(block=True)

#p = ggplot(aes(x='carat'), data=diamonds)
#print p + geom_now_its_art()
#print p + geom_density() + facet_grid("cut", "clarity")
#plt.show(block=True)

p = ggplot(aes(x='factor(cyl)'), data=mtcars)
print(p + geom_bar())
try:
    __IPYTHON__
    plt.show()
except:
    plt.show(block=True)
#ggsave(p + geom_bar(), "public/img/mtcars_geom_bar_cyl.png")

p = ggplot(aes(x='date_hour', y='pageviews'), data=pageviews)
print(p + geom_point())
plt.show(1)

<|endoftext|>


<commit_before>from __future__ import unicode_literals
from debugger import debugger_helpers_all_init

import cherrypy

import sideboard.server

if __name__ == '__main__':
    #print("running debug server2...", flush=True)
    #pydevd.settrace('10.0.0.29', port=5000, stdoutToServer=True, stderrToServer=True)

    debugger_helpers_all_init()

    cherrypy.engine.start()
    cherrypy.engine.block()
<commit_msg>more debugging junk<commit_after>from __future__ import unicode_literals
from debugger import debugger_helpers_all_init

import cherrypy

import sideboard.server

if __name__ == '__main__':
    # import pydevd
    # print("running debug server2...", flush=True)
    # pydevd.settrace('10.0.0.29', port=5000, stdoutToServer=True, stderrToServer=True)

    debugger_helpers_all_init()

    cherrypy.engine.start()
    cherrypy.engine.block()
<|endoftext|>


<commit_before>from observer import Observable, Event

# http://www.dsc.ufcg.edu.br/~jacques/cursos/map/html/arqu/observer.htm
# https://www.safaribooksonline.com/library/view/learning-javascript-design/9781449334840/ch09s05.html
# http://api.jquery.com/trigger/
# http://api.jquery.com/trigger/
# http://api.jquery.com/on/
# https://code.jquery.com/jquery-2.1.4.js
# http://stackoverflow.com/questions/12627443/jquery-click-vs-onclick
# http://stackoverflow.com/questions/9122078/difference-between-onclick-vs-click
# http://stackoverflow.com/questions/15594905/difference-between-observer-pub-sub-and-data-binding
# http://stackoverflow.com/questions/11857325/publisher-subscriber-vs-observer
# http://stackoverflow.com/questions/8065305/whats-the-difference-between-on-and-live-or-bind
# http://www.javaworld.com/article/2077444/learn-java/speaking-on-the-observer-pattern.html
# http://c2.com/cgi/wiki?SoftwareDesignPatternsIndex

# TODO: multiplos subscribers/listeners por topico/evento Observable#add
# TODO: mecanismo para parar a propagacao de uma mensagem em topico
#    (.stopPropagation or return False)
# TODO? permitir que um evento faca link de outros eventos e.on(e2).on(e3)
# TODO? permitir event namespaces? http://api.jquery.com/on/#event-names
# TODO? subject.trigger(event)

# Event == Topic
# Observable == Subject == Source == Event Source == Provider
# Observer == Listener == Subscriber

def clicked1():
    print('clicked1.')

def clicked2():
    print('clicked2.')

def clicked3():
    print('clicked3.')

def mouseentered():
    print('mouseentered')

document = Observable()
document.click = Event()
document.click.on(clicked1)
document.click.on(clicked2)
document.click.on(clicked3)
document.mouseenter = Event(mouseentered)
document.click.trigger()
document.mouseenter.trigger()

document = Observable()
document.on('click', [clicked1, clicked2, clicked3])  # multiple handlers
document.on('mouseenter', mouseentered)
document.click()
document.mouseenter()

document = Observable()
document.on({
    'click': [clicked1, clicked2, clicked3],
    'mouseenter': mouseentered})
document.trigger(['click', 'mouseenter'])

document = Observable()
document.on('click', [clicked1, clicked2, clicked3])
document.on('click2', [clicked1, clicked2, clicked3])  # contains same handlers
document.on('mouseenter', mouseentered)
document.trigger(['click', 'mouseenter'])

document = Observable()
document.on(['click', 'click2'], [clicked1, clicked2, clicked3])
document.on('mouseenter', mouseentered)
document.trigger(['click', 'mouseenter'])

document = Observable()
document.on('click click2', [clicked1, clicked2, clicked3])
document.on('mouseenter', mouseentered)
document.trigger(['click', 'mouseenter'])

document = Observable()
document.on({
    'click click2': [clicked1, clicked2, clicked3],
    'mouseenter': mouseentered})
document.trigger(['click', 'mouseenter'])

# TODO? document.trigger('click mouseenter')

# TODO? descritor para adicionar a test a w.events automaticamente

class Handler:

    def __call__(self, i, j, a, b):
        assert([i, j, a, b] == [1, 2, 3, 4])

def one_handler(*args, **kargs):
    assert(kargs == {'a': 3, 'b': 4})

two_handler = Handler()  # any callable object can be a handler
three_handler = Handler()

subject = Observable()
subject.on('one', one_handler)
subject.on('two', two_handler)
subject.on('three', three_handler)
subject.on('many', [one_handler, two_handler])  # event with handlers
subject.many.on(three_handler)  # add another handler
assert(subject.events['one'] is subject.one)
assert(subject.events['two'] is subject.two)
assert(subject.events['three'] is subject.three)
assert(subject.events['many'] is subject.many)
assert(one_handler in subject.one.handlers)
assert(two_handler in subject.two.handlers)
assert(one_handler in subject.many.handlers)
assert(two_handler in subject.many.handlers)
assert(three_handler in subject.many.handlers)

subject.events['one'].trigger(1, 2, a=3, b=4)  # trigger an event with message
subject.two.trigger(1, 2, a=3, b=4)  # trigger ...
subject.many(1, 2, a=3, b=4)  # trigger ...

subject.off('many', one_handler)  # remove an handler from subject event
subject.many.off(two_handler)  # remove ...
assert(one_handler not in subject.many.handlers)
assert(two_handler not in subject.many.handlers)

subject.off('many')  # remove an event from subject
assert('many' not in subject.events)
try:
    subject.many
    assert(False)
except:
    assert(True)

subject.on('two2', subject.two)  # creating alias for the event
assert(subject.two2 == subject.two)

class ThreeEvent(Event):

    def __init__(self):
        self.on(one_handler)  # add a handler
        self.on(two_handler)  # add another

subject.on('three', ThreeEvent())  # add a subject event with a event object
assert(isinstance(subject.three, ThreeEvent))
assert(one_handler in subject.three.handlers)
assert(two_handler in subject.three.handlers)

subject.on('one', ThreeEvent())  # update a existing event with a event object
assert(ThreeEvent() != ThreeEvent())

subject = Observable()
subject.on({  # setting events of an observable with dictionary
    'one': one_handler,
    'two': two_handler,
    'three': ThreeEvent(),
    'many': [one_handler, two_handler]})

subject = Observable()
subject.on(['two', 'two2'], two_handler)  # events with same handler
assert(subject.two is not subject.two2)
assert(two_handler in subject.two.handlers)
assert(two_handler in subject.two2.handlers)

subject = Observable()
subject.on(['many', 'many2'], [one_handler, two_handler])  # with same handlers
assert(subject.many is not subject.many2)
assert(one_handler in subject.many.handlers)
assert(two_handler in subject.many.handlers)
assert(one_handler in subject.many2.handlers)
assert(two_handler in subject.many2.handlers)

subject = Observable()
three_event = ThreeEvent()
subject.on(['three', 'three_alias'], three_event)  # events with same reference
assert(three_event is subject.three)
assert(subject.three is subject.three_alias)
<commit_msg>Descrevendo alguns exemplos.<commit_after>from observer import Observable, Event

# http://www.dsc.ufcg.edu.br/~jacques/cursos/map/html/arqu/observer.htm
# https://www.safaribooksonline.com/library/view/learning-javascript-design/9781449334840/ch09s05.html
# http://api.jquery.com/trigger/
# http://api.jquery.com/trigger/
# http://api.jquery.com/on/
# https://code.jquery.com/jquery-2.1.4.js
# http://stackoverflow.com/questions/12627443/jquery-click-vs-onclick
# http://stackoverflow.com/questions/9122078/difference-between-onclick-vs-click
# http://stackoverflow.com/questions/15594905/difference-between-observer-pub-sub-and-data-binding
# http://stackoverflow.com/questions/11857325/publisher-subscriber-vs-observer
# http://stackoverflow.com/questions/8065305/whats-the-difference-between-on-and-live-or-bind
# http://www.javaworld.com/article/2077444/learn-java/speaking-on-the-observer-pattern.html
# http://c2.com/cgi/wiki?SoftwareDesignPatternsIndex

# TODO: multiplos subscribers/listeners por topico/evento Observable#add
# TODO: mecanismo para parar a propagacao de uma mensagem em topico
#    (.stopPropagation or return False)
# TODO? permitir que um evento faca link de outros eventos e.on(e2).on(e3)
# TODO? permitir event namespaces? http://api.jquery.com/on/#event-names
# TODO? subject.trigger(event)

def clicked(a, b, c=None):
    print('clicked ', a, b, c)

def clicked1():
    print('clicked1.')

def clicked2():
    print('clicked2.')

def clicked3():
    print('clicked3.')

print('A document event (click) with a handler:')
document = Observable()
document.click = Event()
document.click.on(clicked)  # add a handler to the event
document.click.trigger(1, 2, c=3)  # trigger event handler with arguments

print('A document event (click) with a handler:')
document = Observable()
document.on('click', clicked)  # create event dynamically
document.click.trigger(1, 2, c=3)

print('A document event (click) with many handlers:')
document = Observable()
document.click = Event()
document.click.on(clicked1)  # add a handler to the event
document.click.on(clicked2)  # add another
document.click.on(clicked3)  # ...
document.click.trigger()  # trigger all event handlers

print('A document event (click) with many handlers:')
document = Observable()
document.on('click', [clicked1, clicked2, clicked3])  # create it dynamically
document.click.trigger()

print('Three document events each one with its handler:')
document = Observable()
document.on({
    'click1': clicked1,
    'click2': clicked2,
    'click3': clicked3})
document.click1.trigger()  # different ways to trigger an event
document.click2()
document.trigger('click3')
print('Trigger many events at once:')
document.trigger(['click1', 'click2', 'click3'])  # trigger many events at once

print('Two document events that contains the same handlers:')
document = Observable()
document.on('clicka', [clicked1, clicked2, clicked3])
document.on('clickb', [clicked1, clicked2, clicked3])
document.trigger(['clicka', 'clickb'])

print('Two document events that contains the same handlers:')
document = Observable()
document.on(['clicka', 'clickb'], [clicked1, clicked2, clicked3])
document.trigger(['clicka', 'clickb'])

print('Two document events that contains the same handlers:')
document = Observable()
document.on('clicka clickb', [clicked1, clicked2, clicked3])
document.trigger(['clicka', 'clickb'])

class ClickEvent(Event):

    def __init__(self):
        self.on(self.clicked1)
        self.on(self.clicked2)
        self.on(self.clicked3)

    def clicked1(self):
        print('clicked1.')

    def clicked2(self):
        print('clicked2.')

    def clicked3(self):
        print('clicked3.')

click_event = ClickEvent()
click_event2 = ClickEvent()

document = Observable()
document.on('click', click_event)  # create document event with event instance
document.on('click', click_event2)  # replace document event behaviour
document.on('click click_alias', click_event)  # events have the same reference

print('Create many document events with a dictionary:')
document = Observable()
document.on({
    'click click_alias': click_event,
    'clicka clickb': [clicked1, clicked2, clicked3],
    'click1': clicked1,
    'click2': clicked2,
    'click3': clicked3})
document.click.trigger()
document.clicka()
document.trigger('click1')
document.trigger(['click2', 'click3'])

class Clicked:

    def __init__(self, i):
        self.msg = 'Clicked{}.'.format(i)

    def __call__(self):
        print(self.msg)

clicked1 = Clicked(1)
clicked2 = Clicked(2)
clicked3 = Clicked(3)
document = Observable()
document.on('click', [clicked1, clicked2, clicked3])
document.trigger('click')

# TODO? document.trigger('click mouseenter')
# TODO? descritor para adicionar a test a w.events automaticamente

class Handler:

    def __call__(self, i, j, a, b):
        assert([i, j, a, b] == [1, 2, 3, 4])

def one_handler(*args, **kargs):
    assert(kargs == {'a': 3, 'b': 4})

two_handler = Handler()  # any callable object can be a handler
three_handler = Handler()

subject = Observable()
subject.on('one', one_handler)
subject.on('two', two_handler)
subject.on('three', three_handler)
subject.on('many', [one_handler, two_handler])  # event with handlers
subject.many.on(three_handler)  # add another handler
assert(subject.events['one'] is subject.one)
assert(subject.events['two'] is subject.two)
assert(subject.events['three'] is subject.three)
assert(subject.events['many'] is subject.many)
assert(one_handler in subject.one.handlers)
assert(two_handler in subject.two.handlers)
assert(one_handler in subject.many.handlers)
assert(two_handler in subject.many.handlers)
assert(three_handler in subject.many.handlers)

subject.events['one'].trigger(1, 2, a=3, b=4)  # trigger an event with message
subject.two.trigger(1, 2, a=3, b=4)  # trigger ...
subject.many(1, 2, a=3, b=4)  # trigger ...

subject.off('many', one_handler)  # remove an handler from subject event
subject.many.off(two_handler)  # remove ...
assert(one_handler not in subject.many.handlers)
assert(two_handler not in subject.many.handlers)

subject.off('many')  # remove an event from subject
assert('many' not in subject.events)
try:
    subject.many
    assert(False)
except:
    assert(True)

subject.on('two2', subject.two)  # creating alias for the event
assert(subject.two2 == subject.two)

class ThreeEvent(Event):

    def __init__(self):
        self.on(one_handler)  # add a handler
        self.on(two_handler)  # add another

subject.on('three', ThreeEvent())  # add a subject event with a event object
assert(isinstance(subject.three, ThreeEvent))
assert(one_handler in subject.three.handlers)
assert(two_handler in subject.three.handlers)

subject.on('one', ThreeEvent())  # update a existing event with a event object
assert(ThreeEvent() != ThreeEvent())

subject = Observable()
subject.on({  # setting events of an observable with dictionary
    'one': one_handler,
    'two': two_handler,
    'three': ThreeEvent(),
    'many': [one_handler, two_handler]})

subject = Observable()
subject.on(['two', 'two2'], two_handler)  # events with same handler
assert(subject.two is not subject.two2)
assert(two_handler in subject.two.handlers)
assert(two_handler in subject.two2.handlers)

subject = Observable()
subject.on(['many', 'many2'], [one_handler, two_handler])  # with same handlers
assert(subject.many is not subject.many2)
assert(one_handler in subject.many.handlers)
assert(two_handler in subject.many.handlers)
assert(one_handler in subject.many2.handlers)
assert(two_handler in subject.many2.handlers)

subject = Observable()
three_event = ThreeEvent()
subject.on(['three', 'three_alias'], three_event)  # events with same reference
assert(three_event is subject.three)
assert(subject.three is subject.three_alias)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# JN 2016-01-12
# JN 2014-10-28

from __future__ import division, absolute_import, print_function
import datetime
import tables

def make_blocks(N, bs=100000):
    """
    creates block starts and stops of length bs
    """
    starts = list(range(0, N, bs))

    if N - starts[-1] < bs/4:
        del starts[-1]

    stops = starts[1:] + [N]

    return zip(starts, stops)

def initfile(h5name, ncsf, q_down, include_times=True):
    """
    initializes a h5 file to store converted data
    """
    adbitvolts = ncsf.header['ADBitVolts']
    timestep = ncsf.timestep

    chname = ncsf.header['AcqEntName']

    h5f = tables.open_file(h5name, 'w')

    h5f.create_group('/', 'data')
    h5f.create_earray('/data', 'rawdata', tables.Int16Atom(), [0])
    h5f.root.data.rawdata.set_attr('ADBitVolts', adbitvolts)
    h5f.root.data.rawdata.set_attr('timestep', timestep)
    h5f.root.data.rawdata.set_attr('Q', q_down)
    h5f.root.data.rawdata.set_attr('AcqEntName', chname)

    if include_times:
        h5f.create_earray('/', 'time', tables.UInt64Atom(), [0])

    return h5f

def parse_datetime(fname):
    """
    read datetime file
    returns ts_start_nlx: start time in milliseconds
    ts_start_mpl: start time as datetime object
    """
    with open(fname, 'r') as fid:
            lines = [line.strip() for line in fid.readlines()]

    for line in lines:
        if line[0] == '#':
            continue
        fields = line.split()
        if fields[0] == 'start_recording':
            dtime, micro = fields[2].split('.')
            dstr = fields[1] + ' ' + dtime
            dfmt = '%Y-%m-%d %H:%M:%S'
            start_date = datetime.datetime.strptime(dstr, dfmt)
            start_date += datetime.timedelta(microseconds=int(micro))
            break

    ts_start_nlx = float(fields[3])/1000
    return ts_start_nlx, start_date

if __name__ == "__main__":
    """
    a small test case
    """
    from combinato import NcsFile
    import sys
    Q = 16
    ncsfname = sys.argv[1]
    fid = NcsFile(ncsfname)
    h5f = initfile('out.h5', fid, Q)
    print(h5f)
<commit_msg>Update signalviewer<commit_after># -*- coding: utf-8 -*-
# JN 2016-01-12
# JN 2014-10-28

from __future__ import division, absolute_import, print_function
import datetime
import tables

def make_blocks(N, bs=100000):
    """
    creates block starts and stops of length bs
    """
    starts = list(range(0, N, bs))

    if N - starts[-1] < bs/4:
        del starts[-1]

    stops = starts[1:] + [N]

    return zip(starts, stops)

def initfile(h5name, ncsf, q_down, include_times=True):
    """
    initializes a h5 file to store converted data
    """
    adbitvolts = ncsf.header['ADBitVolts']
    timestep = ncsf.timestep

    chname = ncsf.header['AcqEntName']

    h5f = tables.open_file(h5name, 'w')

    h5f.create_group('/', 'data')
    h5f.create_earray('/data', 'rawdata', tables.Int16Atom(), [0])
    h5f.root.data.rawdata.set_attr('ADBitVolts', adbitvolts)
    h5f.root.data.rawdata.set_attr('timestep', timestep)
    h5f.root.data.rawdata.set_attr('Q', q_down)
    h5f.root.data.rawdata.set_attr('AcqEntName', chname)

    if include_times:
        h5f.create_earray('/', 'time', tables.UInt64Atom(), [0])

    return h5f

def parse_datetime(fname, mode='start'):
    """
    read datetime file
    returns ts_start_nlx: start time in milliseconds
    start_date: start time as datetime object

    the same for mode='stop'
    """
    if mode == 'start':
        target = 'start_recording'
    elif mode == 'stop':
        target = 'stop_recording'
    else:
        raise Warning('Unknown parse_datetime mode: {}'.format(mode))

    with open(fname, 'r') as fid:
            lines = [line.strip() for line in fid.readlines()]

    for line in lines:
        if line[0] == '#':
            continue
        fields = line.split()
        if fields[0] == target:
            dtime, micro = fields[2].split('.')
            dstr = fields[1] + ' ' + dtime
            dfmt = '%Y-%m-%d %H:%M:%S'
            start_date = datetime.datetime.strptime(dstr, dfmt)
            start_date += datetime.timedelta(microseconds=int(micro))
            break

    ts_start_nlx = float(fields[3])/1000
    return ts_start_nlx, start_date

if __name__ == "__main__":
    """
    a small test case
    """
    from combinato import NcsFile
    import sys
    Q = 16
    ncsfname = sys.argv[1]
    fid = NcsFile(ncsfname)
    h5f = initfile('out.h5', fid, Q)
    print(h5f)
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import time
import contextlib
import subprocess
import dhcpctl

def generate_leases(server, port, subnet, start, end, ping):
    with contextlib.closing(dhcpctl.Connection(server, port)) as dhcp:
        print("IN")

        for i in range(start, end):
            ip, ttl, name = dhcp.get_lease("%s.%d" % (subnet, i))

            if not ttl:
                continue

            status, output = subprocess.getstatusoutput(
                ping % {"ip": ip}
            )

            if not status:
                diff = int(time.time()) - ttl
                hours = int((diff / 60) / 60)
                mins = int((diff / 60) % 60)

                yield "ip=%s, uptime=(%dh, %dm), name=%s" % (
                    ip,
                    hours,
                    mins,
                    name
                )

if __name__ == "__main__":
    server = "192.168.1.2"
    port = 20999
    subnet = "192.168.1"
    start = 50
    end = 200
    ping = "ping -q -c 1 %(ip)s &>/dev/null"

    for lease in generate_leases(server, port, subnet, start, end, ping):
        print(lease)
<commit_msg>Uses cleaner integer division instead of casting.<commit_after>#!/usr/bin/env python3

import time
import contextlib
import subprocess
import dhcpctl

def generate_leases(server, port, subnet, start, end, ping):
    with contextlib.closing(dhcpctl.Connection(server, port)) as dhcp:
        print("IN")

        for i in range(start, end):
            ip, ttl, name = dhcp.get_lease("%s.%d" % (subnet, i))

            if not ttl:
                continue

            status, output = subprocess.getstatusoutput(
                ping % {"ip": ip}
            )

            if not status:
                diff = int(time.time()) - ttl
                hours = (diff // 60) // 60
                mins = (diff // 60) % 60

                yield "ip=%s, uptime=(%dh, %dm), name=%s" % (
                    ip,
                    hours,
                    mins,
                    name
                )

if __name__ == "__main__":
    server = "192.168.1.2"
    port = 20999
    subnet = "192.168.1"
    start = 50
    end = 200
    ping = "ping -q -c 1 %(ip)s &>/dev/null"

    for lease in generate_leases(server, port, subnet, start, end, ping):
        print(lease)
<|endoftext|>