<commit_before># The Hazard Library
# Copyright (C) 2014-2017 GEM Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import unittest

import numpy

from openquake.hazardlib.source import AreaSource
from openquake.hazardlib.pmf import PMF
from openquake.hazardlib.scalerel import WC1994
from openquake.hazardlib.geo import Point, NodalPlane
from openquake.hazardlib.mfd import TruncatedGRMFD
from openquake.hazardlib.tom import PoissonTOM
from openquake.hazardlib.calc.stochastic import stochastic_event_set
from openquake.hazardlib.calc import filters
from openquake.hazardlib.site import Site, SiteCollection

from openquake.hazardlib.tests.source.non_parametric_test import \
    make_non_parametric_source

class StochasticEventSetTestCase(unittest.TestCase):

    def setUp(self):
        # time span of 10 million years
        self.time_span = 10e6

        nodalplane = NodalPlane(strike=0.0, dip=90.0, rake=0.0)
        self.mfd = TruncatedGRMFD(a_val=3.5, b_val=1.0, min_mag=5.0,
                                  max_mag=6.5, bin_width=0.1)

        # area source of circular shape with radius of 100 km
        # centered at 0., 0.
        self.area1 = AreaSource(
            source_id='src_1',
            name='area source',
            tectonic_region_type='Active Shallow Crust',
            mfd=self.mfd,
            nodal_plane_distribution=PMF([(1.0, nodalplane)]),
            hypocenter_distribution=PMF([(1.0, 5.0)]),
            upper_seismogenic_depth=0.0,
            lower_seismogenic_depth=10.0,
            magnitude_scaling_relationship=WC1994(),
            rupture_aspect_ratio=1.0,
            polygon=Point(0., 0.).to_polygon(100.),
            area_discretization=9.0,
            rupture_mesh_spacing=1.0,
            temporal_occurrence_model=PoissonTOM(self.time_span)
        )

        # area source of circular shape with radius of 100 km
        # centered at 1., 1.
        self.area2 = AreaSource(
            source_id='src_1',
            name='area source',
            tectonic_region_type='Active Shallow Crust',
            mfd=self.mfd,
            nodal_plane_distribution=PMF([(1.0, nodalplane)]),
            hypocenter_distribution=PMF([(1.0, 5.0)]),
            upper_seismogenic_depth=0.0,
            lower_seismogenic_depth=10.0,
            magnitude_scaling_relationship=WC1994(),
            rupture_aspect_ratio=1.0,
            polygon=Point(5., 5.).to_polygon(100.),
            area_discretization=9.0,
            rupture_mesh_spacing=1.0,
            temporal_occurrence_model=PoissonTOM(self.time_span)
        )

        # non-parametric source
        self.np_src, _ = make_non_parametric_source()

    def _extract_rates(self, ses, time_span, bins):
        """
        Extract annual rates of occurence from stochastic event set
        """
        mags = []
        for r in ses:
            mags.append(r.mag)

        rates, _ = numpy.histogram(mags, bins=bins)
        rates = rates / time_span

        return rates

    def test_ses_generation_from_parametric_source(self):
        # generate stochastic event set (SES) from area source with given
        # magnitude frequency distribution (MFD). Check that the MFD as
        # obtained from the SES (by making an histogram of the magnitude values
        # and normalizing by the total duration of the event set) is
        # approximately equal to the original MFD.
        numpy.random.seed(123)
        ses = stochastic_event_set([self.area1])

        rates = self._extract_rates(ses, time_span=self.time_span,
                                    bins=numpy.arange(5., 6.6, 0.1))

        expect_rates = numpy.array(
            [r for m, r in self.mfd.get_annual_occurrence_rates()]
        )

        numpy.testing.assert_allclose(rates, expect_rates, rtol=0, atol=1e-4)

    def test_ses_generation_from_parametric_source_with_filtering(self):
        # generate stochastic event set (SES) from 2 area sources (area1,
        # area2). However, by including a single site co-located with the
        # area1 center, and with source site filtering of 100 km (exactly
        # the radius of area1), the second source (area2), which is centered
        # at 5., 5. (that is about 500 km from center of area1), will be
        # excluded. the MFD from the SES will be therefore approximately equal
        # to the one of area1 only.
        numpy.random.seed(123)
        sites = SiteCollection([
            Site(
                location=Point(0., 0.), vs30=760, vs30measured=True,
                z1pt0=40., z2pt5=2.
            )
        ])
        ses = stochastic_event_set(
            [self.area1, self.area2],
            sites=sites,
            source_site_filter=filters.SourceFilter(sites, 100.)
        )

        rates = self._extract_rates(ses, time_span=self.time_span,
                                    bins=numpy.arange(5., 6.6, 0.1))

        expect_rates = numpy.array(
            [r for m, r in self.mfd.get_annual_occurrence_rates()]
        )

        numpy.testing.assert_allclose(rates, expect_rates, rtol=0, atol=1e-4)

    def test_ses_generation_from_non_parametric_source(self):
        # np_src contains two ruptures: rup1 (of magnitude 5) and rup2 (of
        # magnitude 6)
        # rup1 has probability of zero occurences of 0.7 and of one
        # occurrence of 0.3
        # rup2 has probability of zero occurrence of 0.7, of one occurrence of
        # 0.2 and of two occurrences of 0.1
        # the test generate multiple SESs. From the ensamble of SES the
        # probability of 0, 1, and 2, rupture occurrences is computed and
        # compared with the expected value
        numpy.random.seed(123)
        num_sess = 10000
        sess = [stochastic_event_set([self.np_src]) for i in range(num_sess)]

        # loop over ses. For each ses count number of rupture
        # occurrences (for each magnitude)
        n_rups1 = {}
        n_rups2 = {}
        for i, ses in enumerate(sess):
            n_rups1[i] = 0
            n_rups2[i] = 0
            for rup in ses:
                if rup.mag == 5.:
                    n_rups1[i] += 1
                if rup.mag == 6.:
                    n_rups2[i] += 1

        # count how many SESs have 0,1 or 2 occurrences, and then normalize
        # by the total number of SESs generated. This gives the probability
        # of having 0, 1 or 2 occurrences
        n_occs1 = numpy.array(list(n_rups1.values()))
        n_occs2 = numpy.array(list(n_rups2.values()))

        p_occs1_0 = float(len(n_occs1[n_occs1 == 0])) / num_sess
        p_occs1_1 = float(len(n_occs1[n_occs1 == 1])) / num_sess

        p_occs2_0 = float(len(n_occs2[n_occs2 == 0])) / num_sess
        p_occs2_1 = float(len(n_occs2[n_occs2 == 1])) / num_sess
        p_occs2_2 = float(len(n_occs2[n_occs2 == 2])) / num_sess

        self.assertAlmostEqual(p_occs1_0, 0.7, places=2)
        self.assertAlmostEqual(p_occs1_1, 0.3, places=2)
        self.assertAlmostEqual(p_occs2_0, 0.7, places=2)
        self.assertAlmostEqual(p_occs2_1, 0.2, places=2)
        self.assertAlmostEqual(p_occs2_2, 0.1, places=2)
<commit_msg>Fixed stochastic_test<commit_after># The Hazard Library
# Copyright (C) 2014-2017 GEM Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import unittest

import numpy

from openquake.hazardlib.source import AreaSource
from openquake.hazardlib.pmf import PMF
from openquake.hazardlib.scalerel import WC1994
from openquake.hazardlib.geo import Point, NodalPlane
from openquake.hazardlib.mfd import TruncatedGRMFD
from openquake.hazardlib.tom import PoissonTOM
from openquake.hazardlib.calc.stochastic import stochastic_event_set
from openquake.hazardlib.calc import filters
from openquake.hazardlib.site import Site, SiteCollection

from openquake.hazardlib.tests.source.non_parametric_test import \
    make_non_parametric_source

class StochasticEventSetTestCase(unittest.TestCase):

    def setUp(self):
        # time span of 10 million years
        self.time_span = 10e6

        nodalplane = NodalPlane(strike=0.0, dip=90.0, rake=0.0)
        self.mfd = TruncatedGRMFD(a_val=3.5, b_val=1.0, min_mag=5.0,
                                  max_mag=6.5, bin_width=0.1)

        # area source of circular shape with radius of 100 km
        # centered at 0., 0.
        self.area1 = AreaSource(
            source_id='src_1',
            name='area source',
            tectonic_region_type='Active Shallow Crust',
            mfd=self.mfd,
            nodal_plane_distribution=PMF([(1.0, nodalplane)]),
            hypocenter_distribution=PMF([(1.0, 5.0)]),
            upper_seismogenic_depth=0.0,
            lower_seismogenic_depth=10.0,
            magnitude_scaling_relationship=WC1994(),
            rupture_aspect_ratio=1.0,
            polygon=Point(0., 0.).to_polygon(100.),
            area_discretization=9.0,
            rupture_mesh_spacing=1.0,
            temporal_occurrence_model=PoissonTOM(self.time_span)
        )

        # area source of circular shape with radius of 100 km
        # centered at 1., 1.
        self.area2 = AreaSource(
            source_id='src_1',
            name='area source',
            tectonic_region_type='Active Shallow Crust',
            mfd=self.mfd,
            nodal_plane_distribution=PMF([(1.0, nodalplane)]),
            hypocenter_distribution=PMF([(1.0, 5.0)]),
            upper_seismogenic_depth=0.0,
            lower_seismogenic_depth=10.0,
            magnitude_scaling_relationship=WC1994(),
            rupture_aspect_ratio=1.0,
            polygon=Point(5., 5.).to_polygon(100.),
            area_discretization=9.0,
            rupture_mesh_spacing=1.0,
            temporal_occurrence_model=PoissonTOM(self.time_span)
        )

        # non-parametric source
        self.np_src, _ = make_non_parametric_source()

    def _extract_rates(self, ses, time_span, bins):
        """
        Extract annual rates of occurence from stochastic event set
        """
        mags = []
        for r in ses:
            mags.append(r.mag)

        rates, _ = numpy.histogram(mags, bins=bins)
        rates = rates / time_span

        return rates

    def test_ses_generation_from_parametric_source(self):
        # generate stochastic event set (SES) from area source with given
        # magnitude frequency distribution (MFD). Check that the MFD as
        # obtained from the SES (by making an histogram of the magnitude values
        # and normalizing by the total duration of the event set) is
        # approximately equal to the original MFD.
        numpy.random.seed(123)
        ses = stochastic_event_set([self.area1])

        rates = self._extract_rates(ses, time_span=self.time_span,
                                    bins=numpy.arange(5., 6.6, 0.1))

        expect_rates = numpy.array(
            [r for m, r in self.mfd.get_annual_occurrence_rates()]
        )

        numpy.testing.assert_allclose(rates, expect_rates, rtol=0, atol=1e-4)

    def test_ses_generation_from_parametric_source_with_filtering(self):
        # generate stochastic event set (SES) from 2 area sources (area1,
        # area2). However, by including a single site co-located with the
        # area1 center, and with source site filtering of 100 km (exactly
        # the radius of area1), the second source (area2), which is centered
        # at 5., 5. (that is about 500 km from center of area1), will be
        # excluded. the MFD from the SES will be therefore approximately equal
        # to the one of area1 only.
        numpy.random.seed(123)
        sites = SiteCollection([
            Site(
                location=Point(0., 0.), vs30=760, vs30measured=True,
                z1pt0=40., z2pt5=2.
            )
        ])
        ses = stochastic_event_set(
            [self.area1, self.area2],
            sites=sites,
            source_site_filter=filters.SourceFilter(sites, {'default': 100.})
        )

        rates = self._extract_rates(ses, time_span=self.time_span,
                                    bins=numpy.arange(5., 6.6, 0.1))

        expect_rates = numpy.array(
            [r for m, r in self.mfd.get_annual_occurrence_rates()]
        )

        numpy.testing.assert_allclose(rates, expect_rates, rtol=0, atol=1e-4)

    def test_ses_generation_from_non_parametric_source(self):
        # np_src contains two ruptures: rup1 (of magnitude 5) and rup2 (of
        # magnitude 6)
        # rup1 has probability of zero occurences of 0.7 and of one
        # occurrence of 0.3
        # rup2 has probability of zero occurrence of 0.7, of one occurrence of
        # 0.2 and of two occurrences of 0.1
        # the test generate multiple SESs. From the ensamble of SES the
        # probability of 0, 1, and 2, rupture occurrences is computed and
        # compared with the expected value
        numpy.random.seed(123)
        num_sess = 10000
        sess = [stochastic_event_set([self.np_src]) for i in range(num_sess)]

        # loop over ses. For each ses count number of rupture
        # occurrences (for each magnitude)
        n_rups1 = {}
        n_rups2 = {}
        for i, ses in enumerate(sess):
            n_rups1[i] = 0
            n_rups2[i] = 0
            for rup in ses:
                if rup.mag == 5.:
                    n_rups1[i] += 1
                if rup.mag == 6.:
                    n_rups2[i] += 1

        # count how many SESs have 0,1 or 2 occurrences, and then normalize
        # by the total number of SESs generated. This gives the probability
        # of having 0, 1 or 2 occurrences
        n_occs1 = numpy.array(list(n_rups1.values()))
        n_occs2 = numpy.array(list(n_rups2.values()))

        p_occs1_0 = float(len(n_occs1[n_occs1 == 0])) / num_sess
        p_occs1_1 = float(len(n_occs1[n_occs1 == 1])) / num_sess

        p_occs2_0 = float(len(n_occs2[n_occs2 == 0])) / num_sess
        p_occs2_1 = float(len(n_occs2[n_occs2 == 1])) / num_sess
        p_occs2_2 = float(len(n_occs2[n_occs2 == 2])) / num_sess

        self.assertAlmostEqual(p_occs1_0, 0.7, places=2)
        self.assertAlmostEqual(p_occs1_1, 0.3, places=2)
        self.assertAlmostEqual(p_occs2_0, 0.7, places=2)
        self.assertAlmostEqual(p_occs2_1, 0.2, places=2)
        self.assertAlmostEqual(p_occs2_2, 0.1, places=2)
<|endoftext|>


<commit_before># Copyright 2013 Kylin, Inc.
#
#    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.

from django.utils.translation import ugettext_lazy as _

from horizon import tables

class QuotaFilterAction(tables.FilterAction):
    def filter(self, table, tenants, filter_string):
        q = filter_string.lower()

        def comp(tenant):
            if q in tenant.name.lower():
                return True
            return False

        return filter(comp, tenants)

class UpdateDefaultQuotas(tables.LinkAction):
    name = "update_defaults"
    verbose_name = _("Update Defaults")
    url = "horizon:admin:defaults:update_defaults"
    classes = ("ajax-modal",)
    icon = "pencil"

def get_quota_name(quota):
    QUOTA_NAMES = {
        'injected_file_content_bytes': _('Injected File Content Bytes'),
        'injected_file_path_bytes': _('Length of Injected File Path'),
        'metadata_items': _('Metadata Items'),
        'cores': _('VCPUs'),
        'instances': _('Instances'),
        'injected_files': _('Injected Files'),
        'volumes': _('Volumes'),
        'snapshots': _('Volume Snapshots'),
        'gigabytes': _('Total Size of Volumes and Snapshots (GB)'),
        'ram': _('RAM (MB)'),
        'floating_ips': _('Floating IPs'),
        'security_groups': _('Security Groups'),
        'security_group_rules': _('Security Group Rules'),
        'key_pairs': _('Key Pairs'),
        'fixed_ips': _('Fixed IPs'),
        'volumes_volume_luks': _('LUKS Volumes'),
        'snapshots_volume_luks': _('LUKS Volume Snapshots'),
        'gigabytes_volume_luks':
        _('Total Size of LUKS Volumes and Snapshots (GB)'),
        'dm-crypt': _('dm-crypt'),
        'server_group_members': _('Server Group Members'),
        'server_groups': _('Server Groups'),
        'backup_gigabytes': _('Backup Gigabytes'),
        'backups': _('Backups')
    }
    return QUOTA_NAMES.get(quota.name, quota.name.replace("_", " ").title())

class QuotasTable(tables.DataTable):
    name = tables.Column(get_quota_name, verbose_name=_('Quota Name'))
    limit = tables.Column("limit", verbose_name=_('Limit'))

    def get_object_id(self, obj):
        return obj.name

    class Meta(object):
        name = "quotas"
        verbose_name = _("Quotas")
        table_actions = (QuotaFilterAction, UpdateDefaultQuotas)
        multi_select = False
<commit_msg>Make some volume related quotas translatable<commit_after># Copyright 2013 Kylin, Inc.
#
#    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.

from django.utils.translation import ugettext_lazy as _

from horizon import tables

class QuotaFilterAction(tables.FilterAction):
    def filter(self, table, tenants, filter_string):
        q = filter_string.lower()

        def comp(tenant):
            if q in tenant.name.lower():
                return True
            return False

        return filter(comp, tenants)

class UpdateDefaultQuotas(tables.LinkAction):
    name = "update_defaults"
    verbose_name = _("Update Defaults")
    url = "horizon:admin:defaults:update_defaults"
    classes = ("ajax-modal",)
    icon = "pencil"

def get_quota_name(quota):
    QUOTA_NAMES = {
        'injected_file_content_bytes': _('Injected File Content Bytes'),
        'injected_file_path_bytes': _('Length of Injected File Path'),
        'metadata_items': _('Metadata Items'),
        'cores': _('VCPUs'),
        'instances': _('Instances'),
        'injected_files': _('Injected Files'),
        'volumes': _('Volumes'),
        'snapshots': _('Volume Snapshots'),
        'gigabytes': _('Total Size of Volumes and Snapshots (GB)'),
        'ram': _('RAM (MB)'),
        'floating_ips': _('Floating IPs'),
        'security_groups': _('Security Groups'),
        'security_group_rules': _('Security Group Rules'),
        'key_pairs': _('Key Pairs'),
        'fixed_ips': _('Fixed IPs'),
        'dm-crypt': _('dm-crypt'),
        'server_group_members': _('Server Group Members'),
        'server_groups': _('Server Groups'),
        'backup_gigabytes': _('Backup Gigabytes'),
        'backups': _('Backups'),
        'per_volume_gigabytes': _('Per Volume Size (GiB)'),
    }

    QUOTA_DYNAMIC_NAMES = {
        'volumes': _('Volumes of Type %(type)s'),
        'snapshots': _('Volume Snapshots of Type %(type)s'),
        'gigabytes':
            _('Total Size of Volumes and Snapshots (GiB) of Type %(type)s')
    }

    # NOTE(Itxaka): This quotas are dynamic and depend on the type of
    # volume types that are defined by the operator
    if quota.name.startswith(('volumes_', 'snapshots_', 'gigabytes_')):
        params = {'type': quota.name.split('_')[1]}
        return QUOTA_DYNAMIC_NAMES.get(quota.name.split('_')[0]) % params
    return QUOTA_NAMES.get(quota.name, quota.name.replace("_", " ").title())

class QuotasTable(tables.DataTable):
    name = tables.Column(get_quota_name, verbose_name=_('Quota Name'))
    limit = tables.Column("limit", verbose_name=_('Limit'))

    def get_object_id(self, obj):
        return obj.name

    class Meta(object):
        name = "quotas"
        verbose_name = _("Quotas")
        table_actions = (QuotaFilterAction, UpdateDefaultQuotas)
        multi_select = False
<|endoftext|>


<commit_before>"""Module that pre-processes the notebook for export to HTML.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import io
import hashlib

from IPython.utils import path
from IPython.utils.traitlets import Unicode
from .base import Preprocessor

class CSSHTMLHeaderPreprocessor(Preprocessor):
    """
    Preprocessor used to pre-process notebook for HTML output.  Adds IPython notebook
    front-end CSS and Pygments CSS to HTML output.
    """
    highlight_class = Unicode('.highlight', config=True,
                              help="CSS highlight class identifier")

    def __init__(self, *pargs, **kwargs):
        Preprocessor.__init__(self, *pargs, **kwargs)
        self._default_css_hash = None

    def preprocess(self, nb, resources):
        """Fetch and add CSS to the resource dictionary

        Fetch CSS from IPython and Pygments to add at the beginning
        of the html files.  Add this css in resources in the
        "inlining.css" key

        Parameters
        ----------
        nb : NotebookNode
            Notebook being converted
        resources : dictionary
            Additional resources used in the conversion process.  Allows
            preprocessors to pass variables into the Jinja engine.
        """
        resources['inlining'] = {}
        resources['inlining']['css'] = self._generate_header(resources)
        return nb, resources

    def _generate_header(self, resources):
        """
        Fills self.header with lines of CSS extracted from IPython
        and Pygments.
        """
        from pygments.formatters import HtmlFormatter
        header = []

        # Construct path to IPy CSS
        from IPython.html import DEFAULT_STATIC_FILES_PATH
        sheet_filename = os.path.join(DEFAULT_STATIC_FILES_PATH,
            'style', 'style.min.css')

        # Load style CSS file.
        with io.open(sheet_filename, encoding='utf-8') as f:
            header.append(f.read())

        # Add pygments CSS
        formatter = HtmlFormatter()
        pygments_css = formatter.get_style_defs(self.highlight_class)
        header.append(pygments_css)

        # Load the user's custom CSS and IPython's default custom CSS.  If they
        # differ, assume the user has made modifications to his/her custom CSS
        # and that we should inline it in the nbconvert output.
        profile_dir = resources['profile_dir']
        custom_css_filename = os.path.join(profile_dir, 'static', 'custom', 'custom.css')
        if os.path.isfile(custom_css_filename):
            if self._default_css_hash is None:
                self._default_css_hash = self._hash(os.path.join(DEFAULT_STATIC_FILES_PATH, 'custom', 'custom.css'))
            if self._hash(custom_css_filename) != self._default_css_hash:
                with io.open(custom_css_filename, encoding='utf-8') as f:
                    header.append(f.read())
        return header

    def _hash(self, filename):
        """Compute the hash of a file."""
        md5 = hashlib.md5()
        with open(filename) as f:
            md5.update(f.read())
        return md5.digest()
<commit_msg>py3 fix<commit_after>"""Module that pre-processes the notebook for export to HTML.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import io
import hashlib

from IPython.utils import path
from IPython.utils.traitlets import Unicode
from IPython.utils.py3compat import str_to_bytes
from .base import Preprocessor

class CSSHTMLHeaderPreprocessor(Preprocessor):
    """
    Preprocessor used to pre-process notebook for HTML output.  Adds IPython notebook
    front-end CSS and Pygments CSS to HTML output.
    """
    highlight_class = Unicode('.highlight', config=True,
                              help="CSS highlight class identifier")

    def __init__(self, *pargs, **kwargs):
        Preprocessor.__init__(self, *pargs, **kwargs)
        self._default_css_hash = None

    def preprocess(self, nb, resources):
        """Fetch and add CSS to the resource dictionary

        Fetch CSS from IPython and Pygments to add at the beginning
        of the html files.  Add this css in resources in the
        "inlining.css" key

        Parameters
        ----------
        nb : NotebookNode
            Notebook being converted
        resources : dictionary
            Additional resources used in the conversion process.  Allows
            preprocessors to pass variables into the Jinja engine.
        """
        resources['inlining'] = {}
        resources['inlining']['css'] = self._generate_header(resources)
        return nb, resources

    def _generate_header(self, resources):
        """
        Fills self.header with lines of CSS extracted from IPython
        and Pygments.
        """
        from pygments.formatters import HtmlFormatter
        header = []

        # Construct path to IPy CSS
        from IPython.html import DEFAULT_STATIC_FILES_PATH
        sheet_filename = os.path.join(DEFAULT_STATIC_FILES_PATH,
            'style', 'style.min.css')

        # Load style CSS file.
        with io.open(sheet_filename, encoding='utf-8') as f:
            header.append(f.read())

        # Add pygments CSS
        formatter = HtmlFormatter()
        pygments_css = formatter.get_style_defs(self.highlight_class)
        header.append(pygments_css)

        # Load the user's custom CSS and IPython's default custom CSS.  If they
        # differ, assume the user has made modifications to his/her custom CSS
        # and that we should inline it in the nbconvert output.
        profile_dir = resources['profile_dir']
        custom_css_filename = os.path.join(profile_dir, 'static', 'custom', 'custom.css')
        if os.path.isfile(custom_css_filename):
            if self._default_css_hash is None:
                self._default_css_hash = self._hash(os.path.join(DEFAULT_STATIC_FILES_PATH, 'custom', 'custom.css'))
            if self._hash(custom_css_filename) != self._default_css_hash:
                with io.open(custom_css_filename, encoding='utf-8') as f:
                    header.append(f.read())
        return header

    def _hash(self, filename):
        """Compute the hash of a file."""
        md5 = hashlib.md5()
        with open(filename) as f:
            md5.update(str_to_bytes(f.read()))
        return md5.digest()
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# -----------------------------------------------------------------------------
# Copyright (c) 2017 The Regents of the University of California
#
# This file is part of kevlar (http://github.com/dib-lab/kevlar) and is
# licensed under the MIT license: see LICENSE.
# -----------------------------------------------------------------------------

from os import remove
import pytest
import subprocess
import sys
from tempfile import NamedTemporaryFile
import kevlar
from kevlar.tests import data_file

@pytest.fixture
def pico_trio(request):
    mother = NamedTemporaryFile(suffix='.fq.gz', delete=False)
    father = NamedTemporaryFile(suffix='.fq.gz', delete=False)
    proband = NamedTemporaryFile(suffix='.fq.gz', delete=False)
    refr = NamedTemporaryFile(suffix='.fq.gz', delete=False)
    filenames = [mother.name, father.name, proband.name, refr.name]

    def teardown():
        for filename in filenames:
            remove(filename)
    request.addfinalizer(teardown)

    urls = [
        'https://osf.io/e8jb3/download?version=1',
        'https://osf.io/fuaty/download?version=1',
        'https://osf.io/f5trh/download?version=1',
        'https://osf.io/58rwa/download?version=1',
    ]
    for url, filename in zip(urls, filenames):
        subprocess.check_call(['curl', '-L', '-o', filename, url])
    subprocess.check_call(['ls', '-lhp'] + filenames)

    subprocess.check_call(['bwa', 'index', refr.name])
    return mother.name, father.name, proband.name, refr.name

@pytest.mark.long
def test_simplex_pico(pico_trio, capsys):
    mother, father, proband, refr = pico_trio

    with capsys.disabled():
        cases = kevlar.novel.load_samples(
            None, [[proband]], ksize=25, memory=5e6, maxfpr=0.6, numthreads=2,
            logstream=sys.stderr
        )
        controls = kevlar.novel.load_samples(
            None, [[mother], [father]], ksize=25, memory=5e6, maxfpr=0.6,
            numthreads=2, logstream=sys.stderr
        )
        mask = kevlar.filter.load_mask([refr], 25, 5e7, maxfpr=0.005,
                                       logstream=sys.stderr)

    caserecords = kevlar.multi_file_iter_screed([proband])
    workflow = kevlar.simplex.simplex(
        caserecords, cases[0], controls, refr, ksize=25, ctrlmax=0, casemin=6,
        mask=mask, filtermem=1e7, filterfpr=0.005
    )
    variants = [v for v in workflow]
    variants = sorted(variants, key=lambda v: v._pos)
    startpos = [v._pos + 1 for v in variants]
    teststartpos = [4073, 185752, 226611, 636699, 834646, 901124, 1175768,
                    1527139, 1631013, 2265795]
    assert len(variants) == 10
    assert startpos == teststartpos
<commit_msg>Disable output capture temporarily so CI build does not time out<commit_after>#!/usr/bin/env python
#
# -----------------------------------------------------------------------------
# Copyright (c) 2017 The Regents of the University of California
#
# This file is part of kevlar (http://github.com/dib-lab/kevlar) and is
# licensed under the MIT license: see LICENSE.
# -----------------------------------------------------------------------------

from os import remove
import pytest
import subprocess
import sys
from tempfile import NamedTemporaryFile
import kevlar
from kevlar.tests import data_file

@pytest.fixture
def pico_trio(request):
    mother = NamedTemporaryFile(suffix='.fq.gz', delete=False)
    father = NamedTemporaryFile(suffix='.fq.gz', delete=False)
    proband = NamedTemporaryFile(suffix='.fq.gz', delete=False)
    refr = NamedTemporaryFile(suffix='.fq.gz', delete=False)
    filenames = [mother.name, father.name, proband.name, refr.name]

    def teardown():
        for filename in filenames:
            remove(filename)
    request.addfinalizer(teardown)

    urls = [
        'https://osf.io/e8jb3/download?version=1',
        'https://osf.io/fuaty/download?version=1',
        'https://osf.io/f5trh/download?version=1',
        'https://osf.io/58rwa/download?version=1',
    ]
    for url, filename in zip(urls, filenames):
        subprocess.check_call(['curl', '-L', '-o', filename, url])
    subprocess.check_call(['ls', '-lhp'] + filenames)

    subprocess.check_call(['bwa', 'index', refr.name])
    return mother.name, father.name, proband.name, refr.name

@pytest.mark.long
def test_simplex_pico(pico_trio, capsys):
    mother, father, proband, refr = pico_trio

    with capsys.disabled():
        cases = kevlar.novel.load_samples(
            None, [[proband]], ksize=25, memory=5e6, maxfpr=0.6, numthreads=2,
            logstream=sys.stderr
        )
        controls = kevlar.novel.load_samples(
            None, [[mother], [father]], ksize=25, memory=5e6, maxfpr=0.6,
            numthreads=2, logstream=sys.stderr
        )
        mask = kevlar.filter.load_mask([refr], 25, 5e7, maxfpr=0.005,
                                       logstream=sys.stderr)

        caserecords = kevlar.multi_file_iter_screed([proband])
        workflow = kevlar.simplex.simplex(
            caserecords, cases[0], controls, refr, ksize=25, ctrlmax=0,
            casemin=6, mask=mask, filtermem=1e7, filterfpr=0.005
        )
        variants = [v for v in workflow]
    variants = sorted(variants, key=lambda v: v._pos)
    startpos = [v._pos + 1 for v in variants]
    teststartpos = [4073, 185752, 226611, 636699, 834646, 901124, 1175768,
                    1527139, 1631013, 2265795]
    assert len(variants) == 10
    assert startpos == teststartpos
<|endoftext|>


<commit_before>import json
import os

file = open('./package.json')
conf = json.load(file)
out = 'APP_NAME="' + conf['name'] + '"\n' + 'APP_VERSION="' + conf['version'] + '"\n'
try:
    out += 'APP_SRC_DIR="' + conf['app']['src']['dir'] + '"\n'
except:
    print('# Using default source dir')
try:
    out += 'DEPLOY_HOST="' + conf['app']['deploy']['host'] + '"\n' + 'DEPLOY_PORT="' + conf['app']['deploy']['port'] + '"\n' + 'DEPLOY_DIR="' + conf['app']['deploy']['dir'] + '"\n'
except:
    print('# No deploy info')
print(out)
file.close()<commit_msg>Fixed custom deploy port handling.<commit_after>import json
import os

file = open('./package.json')
conf = json.load(file)
out = 'APP_NAME="' + conf['name'] + '"\n' + 'APP_VERSION="' + conf['version'] + '"\n'
try:
    out += 'APP_SRC_DIR="' + conf['app']['src']['dir'] + '"\n'
except:
    print('# Using default source dir')
try:
    out += 'DEPLOY_HOST="' + conf['app']['deploy']['host'] + '"\n' + 'DEPLOY_PORT=' + str(conf['app']['deploy']['port']) + '\n' + 'DEPLOY_DIR="' + conf['app']['deploy']['dir'] + '"\n'
except:
    print('# No deploy info')
print(out)
file.close()<|endoftext|>


<commit_before>import copy
import inspect
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.importlib import import_module

register = template.Library()

@register.inclusion_tag('admin_shortcuts/base.html', takes_context=True)
def admin_shortcuts(context):
    if 'ADMIN_SHORTCUTS' in context:
        admin_shortcuts = copy.deepcopy(context['ADMIN_SHORTCUTS'])
    else:
        admin_shortcuts = copy.deepcopy(
            getattr(settings, 'ADMIN_SHORTCUTS', None))
    admin_shortcuts_settings = copy.deepcopy(
        getattr(settings, 'ADMIN_SHORTCUTS_SETTINGS', None))
    request = context.get('request', None)
    if not admin_shortcuts:
        return ''

    for group in admin_shortcuts:
        if not group.get('shortcuts'):
            raise ImproperlyConfigured(
                'settings.ADMIN_SHORTCUTS is improperly configured.')
        for shortcut in group.get('shortcuts'):
            if not shortcut.get('url'):
                try:
                    url_name = shortcut['url_name']
                except KeyError:
                    raise ImproperlyConfigured(
                        _('settings.ADMIN_SHORTCUTS is improperly configured. '
                          'Please supply either a "url" or a "url_name" '
                          'for each shortcut.')
                    )
                current_app = shortcut.get('app_name')
                if isinstance(url_name, list):
                    shortcut['url'] = reverse(
                        url_name[0], args=url_name[1:], current_app=current_app
                    )
                else:
                    shortcut['url'] = reverse(
                        url_name, current_app=current_app)

                shortcut['url'] += shortcut.get('url_extra', '')

            if not shortcut.get('class'):
                shortcut['class'] = get_shortcut_class(
                    shortcut.get('url_name', shortcut['url']))

            if shortcut.get('count'):
                shortcut['count'] = eval_func(shortcut['count'], request)

            if shortcut.get('count_new'):
                shortcut['count_new'] = eval_func(
                    shortcut['count_new'], request)

    return {
        'admin_shortcuts': admin_shortcuts,
        'settings': admin_shortcuts_settings,
        'context': context
    }

@register.inclusion_tag('admin_shortcuts/style.css')
def admin_shortcuts_css():
    return {
        'classes': [value for key, value in CLASS_MAPPINGS],
    }

@register.inclusion_tag('admin_shortcuts/js.html')
def admin_shortcuts_js():
    admin_shortcuts_settings = getattr(
        settings, 'ADMIN_SHORTCUTS_SETTINGS', None)
    return {
        'settings': admin_shortcuts_settings,
    }

def eval_func(func_path, request):
    try:
        module_str = '.'.join(func_path.split('.')[:-1])
        func_str = func_path.split('.')[-1:][0]
        module = import_module(module_str)
        result = getattr(module, func_str)
        if callable(result):
            args, varargs, keywords, defaults = inspect.getargspec(result)
            if 'request' in args:
                result = result(request)
            else:
                result = result()
        return result
    except:
        return func_path

@register.simple_tag
def admin_static_url():
    """
    If set, returns the string contained in the setting
    ADMIN_MEDIA_PREFIX, otherwise returns STATIC_URL + 'admin/'.
    """
    return getattr(settings, 'ADMIN_MEDIA_PREFIX', None) \
        or ''.join([settings.STATIC_URL, 'admin/'])

def get_shortcut_class(url):
    if url == '/':
        return 'home'
    for key, value in CLASS_MAPPINGS:
        if key is not None and key in url:
            return value
    return 'config'  # default icon

XTRA = getattr(settings, 'ADMIN_SHORTCUTS_CLASS_MAPPINGS_EXTRA', [])

CLASS_MAPPINGS = XTRA + getattr(settings, 'ADMIN_SHORTCUTS_CLASS_MAPPINGS', [
    ['cms_page', 'file2'],
    ['product', 'basket'],
    ['order', 'cash'],
    ['category', 'archive'],
    ['user', 'user'],
    ['account', 'user'],
    ['address', 'letter'],
    ['folder', 'folder'],
    ['gallery', 'picture'],
    ['blog', 'blog'],
    ['event', 'date'],
    ['mail', 'openmail'],
    ['message', 'openmail'],
    ['contact', 'openmail'],
    ['location', 'pin'],
    ['store', 'pin'],
    ['delivery', 'delivery2'],
    ['shipping', 'delivery2'],
    ['add', 'plus'],
    ['change', 'pencil'],
    ['home', 'home'],

    # extra classes
    [None, 'archive'],
    [None, 'back'],
    [None, 'camera'],
    [None, 'card'],
    [None, 'cd'],
    [None, 'certificate'],
    [None, 'clock'],
    [None, 'cloud1'],
    [None, 'cloud2'],
    [None, 'cloud3'],
    [None, 'cloud4'],
    [None, 'config'],
    [None, 'config2'],
    [None, 'date'],
    [None, 'delivery1'],
    [None, 'diskette'],
    [None, 'file1'],
    [None, 'file3'],
    [None, 'file4'],
    [None, 'film'],
    [None, 'flag'],
    [None, 'gamepad'],
    [None, 'garbage'],
    [None, 'gift'],
    [None, 'help'],
    [None, 'key'],
    [None, 'less'],
    [None, 'letters'],
    [None, 'light'],
    [None, 'lock'],
    [None, 'love'],
    [None, 'mail'],
    [None, 'monitor'],
    [None, 'music'],
    [None, 'note'],
    [None, 'notepad'],
    [None, 'ok'],
    [None, 'package'],
    [None, 'phone'],
    [None, 'pin'],
    [None, 'print'],
    [None, 'sound'],
    [None, 'suitcase'],
    [None, 'tag'],
    [None, 'ticket'],
    [None, 'tool'],
    [None, 'unlock'],
    [None, 'wallet'],
    [None, 'warning'],
    [None, 'way'],
    [None, 'zoom'],
])
<commit_msg>Fix shortcuts child icons<commit_after>import copy
import inspect
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.importlib import import_module

register = template.Library()

def make_url(shortcut, request):
    if not shortcut.get('url'):
        try:
            url_name = shortcut['url_name']
        except KeyError:
            raise ImproperlyConfigured(
                _('settings.ADMIN_SHORTCUTS is improperly configured. '
                  'Please supply either a "url" or a "url_name" '
                  'for each shortcut.')
            )
        current_app = shortcut.get('app_name')
        if isinstance(url_name, list):
            shortcut['url'] = reverse(
                url_name[0], args=url_name[1:], current_app=current_app
            )
        else:
            shortcut['url'] = reverse(
                url_name, current_app=current_app)

        shortcut['url'] += shortcut.get('url_extra', '')

    if not shortcut.get('class'):
        shortcut['class'] = get_shortcut_class(
            shortcut.get('url_name', shortcut['url']))

    if shortcut.get('count'):
        shortcut['count'] = eval_func(shortcut['count'], request)

    if shortcut.get('count_new'):
        shortcut['count_new'] = eval_func(
            shortcut['count_new'], request)

@register.inclusion_tag('admin_shortcuts/base.html', takes_context=True)
def admin_shortcuts(context):
    if 'ADMIN_SHORTCUTS' in context:
        admin_shortcuts = copy.deepcopy(context['ADMIN_SHORTCUTS'])
    else:
        admin_shortcuts = copy.deepcopy(
            getattr(settings, 'ADMIN_SHORTCUTS', []))
    admin_shortcuts_settings = copy.deepcopy(
        getattr(settings, 'ADMIN_SHORTCUTS_SETTINGS', {}))
    request = context.get('request', None)
    if not admin_shortcuts:
        return ''

    for group in admin_shortcuts:
        if not group.get('shortcuts'):
            raise ImproperlyConfigured(
                'settings.ADMIN_SHORTCUTS is improperly configured.')

        for shortcut in group.get('shortcuts', []):
            make_url(shortcut, request)

            for child in shortcut.get('children', []):
                make_url(child, request)
                # TODO: make it recursive for submenus

    return {
        'admin_shortcuts': admin_shortcuts,
        'settings': admin_shortcuts_settings,
        'context': context
    }

@register.inclusion_tag('admin_shortcuts/style.css')
def admin_shortcuts_css():
    return {
        'classes': [value for key, value in CLASS_MAPPINGS],
    }

@register.inclusion_tag('admin_shortcuts/js.html')
def admin_shortcuts_js():
    admin_shortcuts_settings = getattr(
        settings, 'ADMIN_SHORTCUTS_SETTINGS', None)
    return {
        'settings': admin_shortcuts_settings,
    }

def eval_func(func_path, request):
    try:
        module_str = '.'.join(func_path.split('.')[:-1])
        func_str = func_path.split('.')[-1:][0]
        module = import_module(module_str)
        result = getattr(module, func_str)
        if callable(result):
            args, varargs, keywords, defaults = inspect.getargspec(result)
            if 'request' in args:
                result = result(request)
            else:
                result = result()
        return result
    except:
        return func_path

@register.simple_tag
def admin_static_url():
    """
    If set, returns the string contained in the setting
    ADMIN_MEDIA_PREFIX, otherwise returns STATIC_URL + 'admin/'.
    """
    return getattr(settings, 'ADMIN_MEDIA_PREFIX', None) \
        or ''.join([settings.STATIC_URL, 'admin/'])

def get_shortcut_class(url):
    if url == '/':
        return 'home'
    for key, value in CLASS_MAPPINGS:
        if key is not None and key in url:
            return value
    return 'config'  # default icon

XTRA = getattr(settings, 'ADMIN_SHORTCUTS_CLASS_MAPPINGS_EXTRA', [])

CLASS_MAPPINGS = XTRA + getattr(settings, 'ADMIN_SHORTCUTS_CLASS_MAPPINGS', [
    ['cms_page', 'file2'],
    ['product', 'basket'],
    ['order', 'cash'],
    ['category', 'archive'],
    ['user', 'user'],
    ['account', 'user'],
    ['address', 'letter'],
    ['folder', 'folder'],
    ['gallery', 'picture'],
    ['blog', 'blog'],
    ['event', 'date'],
    ['mail', 'openmail'],
    ['message', 'openmail'],
    ['contact', 'openmail'],
    ['location', 'pin'],
    ['store', 'pin'],
    ['delivery', 'delivery2'],
    ['shipping', 'delivery2'],
    ['add', 'plus'],
    ['change', 'pencil'],
    ['home', 'home'],

    # extra classes
    [None, 'archive'],
    [None, 'back'],
    [None, 'camera'],
    [None, 'card'],
    [None, 'cd'],
    [None, 'certificate'],
    [None, 'clock'],
    [None, 'cloud1'],
    [None, 'cloud2'],
    [None, 'cloud3'],
    [None, 'cloud4'],
    [None, 'config'],
    [None, 'config2'],
    [None, 'date'],
    [None, 'delivery1'],
    [None, 'diskette'],
    [None, 'file1'],
    [None, 'file3'],
    [None, 'file4'],
    [None, 'film'],
    [None, 'flag'],
    [None, 'gamepad'],
    [None, 'garbage'],
    [None, 'gift'],
    [None, 'help'],
    [None, 'key'],
    [None, 'less'],
    [None, 'letters'],
    [None, 'light'],
    [None, 'lock'],
    [None, 'love'],
    [None, 'mail'],
    [None, 'monitor'],
    [None, 'music'],
    [None, 'note'],
    [None, 'notepad'],
    [None, 'ok'],
    [None, 'package'],
    [None, 'phone'],
    [None, 'pin'],
    [None, 'print'],
    [None, 'sound'],
    [None, 'suitcase'],
    [None, 'tag'],
    [None, 'ticket'],
    [None, 'tool'],
    [None, 'unlock'],
    [None, 'wallet'],
    [None, 'warning'],
    [None, 'way'],
    [None, 'zoom'],
])
<|endoftext|>


<commit_before># coding=utf-8

import os
from python_sqlite_tools import SqliteTool

# test delete table
def drop_table_test(test_db):
    print('delete table testing...')
    test_db.drop_db_table('test_table')

# create table test
def create_table_test(test_db):
    print('create table testing...')

    create_table_sql = '''CREATE TABLE 'test_table' (
                          'id' int(11) NOT NULL,
                          'name' varchar(20) NOT NULL,
                          'gender' varchar(8) DEFAULT NULL,
                          'age' int(11) DEFAULT NULL,
                          'address' varchar(200) DEFAULT NULL,
                          'phone' varchar(20) DEFAULT NULL,
                           PRIMARY KEY ('id')
                        )'''
    test_db.create_db_table(create_table_sql)

# insert test
def insert_test(test_db):
    print('Insert testing...')

    sql = '''INSERT INTO test_table values (?, ?, ?, ?, ?, ?)'''
    data = [(1, 'Dave', 'man', 20, 'BeiJing', '183****62'),
            (2, 'test_table', 'man', 22, 'ShenZhen', '178****63'),
            (3, 'Oracle', 'women', 18, 'GuangDong', '177****87'),
            (4, 'Python', 'women', 21, 'ShangHai', '135****32')]
    test_db.insert(sql, data)

# search all test
def fetch_all_test(test_db):
    print('Search all testing...')

    fetchall_sql = '''SELECT * FROM test_table'''
    test_db.fetch_all(fetchall_sql)

# search one test
def fetch_one_test(test_db):
    print('Search one data testing...')

    fetchone_sql = 'SELECT * FROM test_table WHERE id = ? '
    data = 1
    # fetchone_sql = 'SELECT * FROM test_table WHERE address = ? '
    # data = '美国'
    test_db.fetch_one(fetchone_sql, data)

# update test
def update_test(test_db):
    print('Update tesing...')

    update_sql = 'UPDATE test_table SET name = ? WHERE id = ? '
    data = [('Oracle', 1),
            ('MySQL', 2)]
    test_db.update(update_sql, data)

# delete data test
def delete_test(test_db):
    print('Detele data testing...')

    delete_sql = 'DELETE FROM test_table WHERE NAME = ? AND id = ? '
    data = [('Oracle', 1),
            ('python', 2)]
    test_db.delete(delete_sql, data)

# print menu
def show_menu():
    print "******* Welcome Menu *******\n"
    print "  1 ---> Create table test."
    print "  2 ---> Drop table test."
    print "  3 ---> Insert data test."
    print "  4 ---> Search only test."
    print "  5 ---> Search all test."
    print "  6 ---> Update data test."
    print "  7 ---> Delete data test."
    print "  0 ---> Quit."
    print "\n****************************"
    print ">> Input your choice: "

# main test
def main_test():
    db_file_path = "test.db"
    test_db = SqliteTool(db_file_path)

    # clean screen and show menu
    os.system("cls")
    show_menu()
    input_value = input()

    while input_value:
        # run test case
        if (input_value >= 1) and (input_value <= 7):
            if input_value == 1:
                create_table_test(test_db)
            elif input_value == 2:
                drop_table_test(test_db)
            elif input_value == 3:
                insert_test(test_db)
            elif input_value == 4:
                fetch_one_test(test_db)
            elif input_value == 5:
                fetch_all_test(test_db)
            elif input_value == 6:
                update_test(test_db)
            elif input_value == 7:
                delete_test(test_db)
            elif input_value == 0:
                break
        else:
            print "Error: input value is wrong!"

        print ">> Input your choice: "
        input_value = input()

if __name__ == '__main__':
    main_test()
<commit_msg>Update python<commit_after># coding=utf-8

import os
from python_sqlite_tools import SqliteTool

# test delete table
def drop_table_test(test_db):
    print('delete table testing...')
    test_db.drop_db_table('test_table')

# create table test
def create_table_test(test_db):
    print('create table testing...')

    create_table_sql = '''CREATE TABLE 'test_table' (
                          'id' int(11) NOT NULL,
                          'name' varchar(20) NOT NULL,
                          'gender' varchar(8) DEFAULT NULL,
                          'age' int(11) DEFAULT NULL,
                          'address' varchar(200) DEFAULT NULL,
                          'phone' varchar(20) DEFAULT NULL,
                           PRIMARY KEY ('id')
                        )'''
    test_db.create_db_table(create_table_sql)

# insert test
def insert_test(test_db):
    print('Insert testing...')

    sql = '''INSERT INTO test_table values (?, ?, ?, ?, ?, ?)'''
    data = [(1, 'Dave', 'man', 20, 'BeiJing', '183****62'),
            (2, 'test_table', 'man', 22, 'ShenZhen', '178****63'),
            (3, 'Oracle', 'women', 18, 'GuangDong', '177****87'),
            (4, 'Python', 'women', 21, 'ShangHai', '135****32')]
    test_db.insert(sql, data)

# search all test
def fetch_all_test(test_db):
    print('Search all testing...')

    fetchall_sql = '''SELECT * FROM test_table'''
    test_db.fetch_all(fetchall_sql)

# search one test
def fetch_one_test(test_db):
    print('Search one data testing...')

    fetchone_sql = 'SELECT * FROM test_table WHERE id = ? '
    data = 1
    # fetchone_sql = 'SELECT * FROM test_table WHERE address = ? '
    # data = 'ShenZhen'
    test_db.fetch_one(fetchone_sql, data)

# update test
def update_test(test_db):
    print('Update tesing...')

    update_sql = 'UPDATE test_table SET name = ? WHERE id = ? '
    data = [('Oracle', 1),
            ('MySQL', 2)]
    test_db.update(update_sql, data)

# delete data test
def delete_test(test_db):
    print('Detele data testing...')

    delete_sql = 'DELETE FROM test_table WHERE NAME = ? AND id = ? '
    data = [('Oracle', 1),
            ('python', 2)]
    test_db.delete(delete_sql, data)

# print menu
def show_menu():
    print "******* Welcome Menu *******\n"
    print "  1 ---> Create table test."
    print "  2 ---> Drop table test."
    print "  3 ---> Insert data test."
    print "  4 ---> Search only test."
    print "  5 ---> Search all test."
    print "  6 ---> Update data test."
    print "  7 ---> Delete data test."
    print "  0 ---> Quit."
    print "\n****************************"
    print ">> Input your choice: "

# main test
def main_test():
    db_file_path = "test.db"
    test_db = SqliteTool(db_file_path)

    # clean screen and show menu
    os.system("cls")
    show_menu()
    input_value = input()

    while input_value:
        # run test case
        if (input_value >= 1) and (input_value <= 7):
            if input_value == 1:
                create_table_test(test_db)
            elif input_value == 2:
                drop_table_test(test_db)
            elif input_value == 3:
                insert_test(test_db)
            elif input_value == 4:
                fetch_one_test(test_db)
            elif input_value == 5:
                fetch_all_test(test_db)
            elif input_value == 6:
                update_test(test_db)
            elif input_value == 7:
                delete_test(test_db)
            elif input_value == 0:
                break
        else:
            print "Error: input value is wrong!"

        print ">> Input your choice: "
        input_value = input()

if __name__ == '__main__':
    main_test()
<|endoftext|>


<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 OpenStack LLC
#
# 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.

"""Main entry point into the EC2 Credentials service.

This service allows the creation of access/secret credentials used for
the ec2 interop layer of OpenStack.

A user can create as many access/secret pairs, each of which map to a
specific tenant.  This is required because OpenStack supports a user
belonging to multiple tenants, whereas the signatures created on ec2-style
requests don't allow specification of which tenant the user wishs to act
upon.

To complete the cycle, we provide a method that OpenStack services can
use to validate a signature and get a corresponding openstack token.  This
token allows method calls to other services within the context the
access/secret was created.  As an example, nova requests keystone to validate
the signature of a request, receives a token, and then makes a request to
glance to list images needed to perform the requested task.

"""

import uuid

from keystone import catalog
from keystone.common import manager
from keystone.common import utils
from keystone.common import wsgi
from keystone import config
from keystone import exception
from keystone import identity
from keystone import policy
from keystone import service
from keystone import token

CONF = config.CONF

class Manager(manager.Manager):
    """Default pivot point for the EC2 Credentials backend.

    See :mod:`keystone.common.manager.Manager` for more details on how this
    dynamically calls the backend.

    """

    def __init__(self):
        super(Manager, self).__init__(CONF.ec2.driver)

class Ec2Extension(wsgi.ExtensionRouter):
    def add_routes(self, mapper):
        ec2_controller = Ec2Controller()
        # validation
        mapper.connect(
            '/ec2tokens',
            controller=ec2_controller,
            action='authenticate',
            conditions=dict(method=['POST']))

        # crud
        mapper.connect(
            '/users/{user_id}/credentials/OS-EC2',
            controller=ec2_controller,
            action='create_credential',
            conditions=dict(method=['POST']))
        mapper.connect(
            '/users/{user_id}/credentials/OS-EC2',
            controller=ec2_controller,
            action='get_credentials',
            conditions=dict(method=['GET']))
        mapper.connect(
            '/users/{user_id}/credentials/OS-EC2/{credential_id}',
            controller=ec2_controller,
            action='get_credential',
            conditions=dict(method=['GET']))
        mapper.connect(
            '/users/{user_id}/credentials/OS-EC2/{credential_id}',
            controller=ec2_controller,
            action='delete_credential',
            conditions=dict(method=['DELETE']))

class Ec2Controller(wsgi.Application):
    def __init__(self):
        self.catalog_api = catalog.Manager()
        self.identity_api = identity.Manager()
        self.token_api = token.Manager()
        self.policy_api = policy.Manager()
        self.ec2_api = Manager()
        super(Ec2Controller, self).__init__()

    def check_signature(self, creds_ref, credentials):
        signer = utils.Ec2Signer(creds_ref['secret'])
        signature = signer.generate(credentials)
        if utils.auth_str_equal(credentials['signature'], signature):
            return
        # NOTE(vish): Some libraries don't use the port when signing
        #             requests, so try again without port.
        elif ':' in credentials['signature']:
            hostname, _port = credentials['host'].split(':')
            credentials['host'] = hostname
            signature = signer.generate(credentials)
            if not utils.auth_str_equal(credentials.signature, signature):
                raise exception.Unauthorized(message='Invalid EC2 signature.')
        else:
            raise exception.Unauthorized(message='EC2 signature not supplied.')

    def authenticate(self, context, credentials=None, ec2Credentials=None):
        """Validate a signed EC2 request and provide a token.

        Other services (such as Nova) use this **admin** call to determine
        if a request they signed received is from a valid user.

        If it is a valid signature, an openstack token that maps
        to the user/tenant is returned to the caller, along with
        all the other details returned from a normal token validation
        call.

        The returned token is useful for making calls to other
        OpenStack services within the context of the request.

        :param context: standard context
        :param credentials: dict of ec2 signature
        :param ec2Credentials: DEPRECATED dict of ec2 signature
        :returns: token: openstack token equivalent to access key along
                         with the corresponding service catalog and roles
        """

        # FIXME(ja): validate that a service token was used!

        # NOTE(termie): backwards compat hack
        if not credentials and ec2Credentials:
            credentials = ec2Credentials

        if not 'access' in credentials:
            raise exception.Unauthorized(message='EC2 signature not supplied.')

        creds_ref = self._get_credentials(context,
                                          credentials['access'])
        self.check_signature(creds_ref, credentials)

        # TODO(termie): don't create new tokens every time
        # TODO(termie): this is copied from TokenController.authenticate
        token_id = uuid.uuid4().hex
        tenant_ref = self.identity_api.get_tenant(
            context=context,
            tenant_id=creds_ref['tenant_id'])
        user_ref = self.identity_api.get_user(
            context=context,
            user_id=creds_ref['user_id'])
        metadata_ref = self.identity_api.get_metadata(
            context=context,
            user_id=user_ref['id'],
            tenant_id=tenant_ref['id'])
        catalog_ref = self.catalog_api.get_catalog(
            context=context,
            user_id=user_ref['id'],
            tenant_id=tenant_ref['id'],
            metadata=metadata_ref)

        token_ref = self.token_api.create_token(
            context, token_id, dict(id=token_id,
                                    user=user_ref,
                                    tenant=tenant_ref,
                                    metadata=metadata_ref))

        # TODO(termie): optimize this call at some point and put it into the
        #               the return for metadata
        # fill out the roles in the metadata
        roles_ref = []
        for role_id in metadata_ref.get('roles', []):
            roles_ref.append(self.identity_api.get_role(context, role_id))

        # TODO(termie): make this a util function or something
        # TODO(termie): i don't think the ec2 middleware currently expects a
        #               full return, but it contains a note saying that it
        #               would be better to expect a full return
        token_controller = service.TokenController()
        return token_controller._format_authenticate(
            token_ref, roles_ref, catalog_ref)

    def create_credential(self, context, user_id, tenant_id):
        """Create a secret/access pair for use with ec2 style auth.

        Generates a new set of credentials that map the the user/tenant
        pair.

        :param context: standard context
        :param user_id: id of user
        :param tenant_id: id of tenant
        :returns: credential: dict of ec2 credential
        """
        if not self._is_admin(context):
            self._assert_identity(context, user_id)

        self._assert_valid_user_id(context, user_id)
        self._assert_valid_tenant_id(context, tenant_id)

        cred_ref = {'user_id': user_id,
                    'tenant_id': tenant_id,
                    'access': uuid.uuid4().hex,
                    'secret': uuid.uuid4().hex}
        self.ec2_api.create_credential(context, cred_ref['access'], cred_ref)
        return {'credential': cred_ref}

    def get_credentials(self, context, user_id):
        """List all credentials for a user.

        :param context: standard context
        :param user_id: id of user
        :returns: credentials: list of ec2 credential dicts
        """
        if not self._is_admin(context):
            self._assert_identity(context, user_id)
        self._assert_valid_user_id(context, user_id)
        return {'credentials': self.ec2_api.list_credentials(context, user_id)}

    def get_credential(self, context, user_id, credential_id):
        """Retreive a user's access/secret pair by the access key.

        Grab the full access/secret pair for a given access key.

        :param context: standard context
        :param user_id: id of user
        :param credential_id: access key for credentials
        :returns: credential: dict of ec2 credential
        """
        if not self._is_admin(context):
            self._assert_identity(context, user_id)
        self._assert_valid_user_id(context, user_id)
        creds = self._get_credentials(context, credential_id)
        return {'credential': creds}

    def delete_credential(self, context, user_id, credential_id):
        """Delete a user's access/secret pair.

        Used to revoke a user's access/secret pair

        :param context: standard context
        :param user_id: id of user
        :param credential_id: access key for credentials
        :returns: bool: success
        """
        if not self._is_admin(context):
            self._assert_identity(context, user_id)
            self._assert_owner(context, user_id, credential_id)

        self._assert_valid_user_id(context, user_id)
        self._get_credentials(context, credential_id)
        return self.ec2_api.delete_credential(context, credential_id)

    def _get_credentials(self, context, credential_id):
        """Return credentials from an ID.

        :param context: standard context
        :param credential_id: id of credential
        :raises exception.Unauthorized: when credential id is invalid
        :returns: credential: dict of ec2 credential.
        """
        creds = self.ec2_api.get_credential(context,
                                            credential_id)
        if not creds:
            raise exception.Unauthorized(message='EC2 access key not found.')
        return creds

    def _assert_identity(self, context, user_id):
        """Check that the provided token belongs to the user.

        :param context: standard context
        :param user_id: id of user
        :raises exception.Forbidden: when token is invalid

        """
        try:
            token_ref = self.token_api.get_token(
                context=context,
                token_id=context['token_id'])
        except exception.TokenNotFound:
            raise exception.Unauthorized()
        token_user_id = token_ref['user'].get('id')
        if not token_user_id == user_id:
            raise exception.Forbidden()

    def _is_admin(self, context):
        """Wrap admin assertion error return statement.

        :param context: standard context
        :returns: bool: success

        """
        try:
            self.assert_admin(context)
            return True
        except exception.Forbidden:
            return False

    def _assert_owner(self, context, user_id, credential_id):
        """Ensure the provided user owns the credential.

        :param context: standard context
        :param user_id: expected credential owner
        :param credential_id: id of credential object
        :raises exception.Forbidden: on failure

        """
        cred_ref = self.ec2_api.get_credential(context, credential_id)
        if not user_id == cred_ref['user_id']:
            raise exception.Forbidden()

    def _assert_valid_user_id(self, context, user_id):
        """Ensure a valid user id.

        :param context: standard context
        :param user_id: expected credential owner
        :raises exception.UserNotFound: on failure

        """
        user_ref = self.identity_api.get_user(
            context=context,
            user_id=user_id)
        if not user_ref:
            raise exception.UserNotFound(user_id=user_id)

    def _assert_valid_tenant_id(self, context, tenant_id):
        """Ensure a valid tenant id.

        :param context: standard context
        :param user_id: expected credential owner
        :raises exception.UserNotFound: on failure

        """
        tenant_ref = self.identity_api.get_tenant(
            context=context,
            tenant_id=tenant_id)
        if not tenant_ref:
            raise exception.TenantNotFound(tenant_id=tenant_id)
<commit_msg>Ensures User is member of tenant in ec2 validation<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 OpenStack LLC
#
# 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.

"""Main entry point into the EC2 Credentials service.

This service allows the creation of access/secret credentials used for
the ec2 interop layer of OpenStack.

A user can create as many access/secret pairs, each of which map to a
specific tenant.  This is required because OpenStack supports a user
belonging to multiple tenants, whereas the signatures created on ec2-style
requests don't allow specification of which tenant the user wishs to act
upon.

To complete the cycle, we provide a method that OpenStack services can
use to validate a signature and get a corresponding openstack token.  This
token allows method calls to other services within the context the
access/secret was created.  As an example, nova requests keystone to validate
the signature of a request, receives a token, and then makes a request to
glance to list images needed to perform the requested task.

"""

import uuid

from keystone import catalog
from keystone.common import manager
from keystone.common import utils
from keystone.common import wsgi
from keystone import config
from keystone import exception
from keystone import identity
from keystone import policy
from keystone import service
from keystone import token

CONF = config.CONF

class Manager(manager.Manager):
    """Default pivot point for the EC2 Credentials backend.

    See :mod:`keystone.common.manager.Manager` for more details on how this
    dynamically calls the backend.

    """

    def __init__(self):
        super(Manager, self).__init__(CONF.ec2.driver)

class Ec2Extension(wsgi.ExtensionRouter):
    def add_routes(self, mapper):
        ec2_controller = Ec2Controller()
        # validation
        mapper.connect(
            '/ec2tokens',
            controller=ec2_controller,
            action='authenticate',
            conditions=dict(method=['POST']))

        # crud
        mapper.connect(
            '/users/{user_id}/credentials/OS-EC2',
            controller=ec2_controller,
            action='create_credential',
            conditions=dict(method=['POST']))
        mapper.connect(
            '/users/{user_id}/credentials/OS-EC2',
            controller=ec2_controller,
            action='get_credentials',
            conditions=dict(method=['GET']))
        mapper.connect(
            '/users/{user_id}/credentials/OS-EC2/{credential_id}',
            controller=ec2_controller,
            action='get_credential',
            conditions=dict(method=['GET']))
        mapper.connect(
            '/users/{user_id}/credentials/OS-EC2/{credential_id}',
            controller=ec2_controller,
            action='delete_credential',
            conditions=dict(method=['DELETE']))

class Ec2Controller(wsgi.Application):
    def __init__(self):
        self.catalog_api = catalog.Manager()
        self.identity_api = identity.Manager()
        self.token_api = token.Manager()
        self.policy_api = policy.Manager()
        self.ec2_api = Manager()
        super(Ec2Controller, self).__init__()

    def check_signature(self, creds_ref, credentials):
        signer = utils.Ec2Signer(creds_ref['secret'])
        signature = signer.generate(credentials)
        if utils.auth_str_equal(credentials['signature'], signature):
            return
        # NOTE(vish): Some libraries don't use the port when signing
        #             requests, so try again without port.
        elif ':' in credentials['signature']:
            hostname, _port = credentials['host'].split(':')
            credentials['host'] = hostname
            signature = signer.generate(credentials)
            if not utils.auth_str_equal(credentials.signature, signature):
                raise exception.Unauthorized(message='Invalid EC2 signature.')
        else:
            raise exception.Unauthorized(message='EC2 signature not supplied.')

    def authenticate(self, context, credentials=None, ec2Credentials=None):
        """Validate a signed EC2 request and provide a token.

        Other services (such as Nova) use this **admin** call to determine
        if a request they signed received is from a valid user.

        If it is a valid signature, an openstack token that maps
        to the user/tenant is returned to the caller, along with
        all the other details returned from a normal token validation
        call.

        The returned token is useful for making calls to other
        OpenStack services within the context of the request.

        :param context: standard context
        :param credentials: dict of ec2 signature
        :param ec2Credentials: DEPRECATED dict of ec2 signature
        :returns: token: openstack token equivalent to access key along
                         with the corresponding service catalog and roles
        """

        # FIXME(ja): validate that a service token was used!

        # NOTE(termie): backwards compat hack
        if not credentials and ec2Credentials:
            credentials = ec2Credentials

        if not 'access' in credentials:
            raise exception.Unauthorized(message='EC2 signature not supplied.')

        creds_ref = self._get_credentials(context,
                                          credentials['access'])
        self.check_signature(creds_ref, credentials)

        # TODO(termie): don't create new tokens every time
        # TODO(termie): this is copied from TokenController.authenticate
        token_id = uuid.uuid4().hex
        tenant_ref = self.identity_api.get_tenant(
            context=context,
            tenant_id=creds_ref['tenant_id'])
        user_ref = self.identity_api.get_user(
            context=context,
            user_id=creds_ref['user_id'])
        metadata_ref = self.identity_api.get_metadata(
            context=context,
            user_id=user_ref['id'],
            tenant_id=tenant_ref['id'])

        # TODO(termie): optimize this call at some point and put it into the
        #               the return for metadata
        # fill out the roles in the metadata
        roles = metadata_ref.get('roles', [])
        if not roles:
            raise exception.Unauthorized(message='User not valid for tenant.')
        roles_ref = [self.identity_api.get_role(context, role_id)
                     for role_id in roles]

        catalog_ref = self.catalog_api.get_catalog(
            context=context,
            user_id=user_ref['id'],
            tenant_id=tenant_ref['id'],
            metadata=metadata_ref)

        token_ref = self.token_api.create_token(
            context, token_id, dict(id=token_id,
                                    user=user_ref,
                                    tenant=tenant_ref,
                                    metadata=metadata_ref))

        # TODO(termie): make this a util function or something
        # TODO(termie): i don't think the ec2 middleware currently expects a
        #               full return, but it contains a note saying that it
        #               would be better to expect a full return
        token_controller = service.TokenController()
        return token_controller._format_authenticate(
            token_ref, roles_ref, catalog_ref)

    def create_credential(self, context, user_id, tenant_id):
        """Create a secret/access pair for use with ec2 style auth.

        Generates a new set of credentials that map the the user/tenant
        pair.

        :param context: standard context
        :param user_id: id of user
        :param tenant_id: id of tenant
        :returns: credential: dict of ec2 credential
        """
        if not self._is_admin(context):
            self._assert_identity(context, user_id)

        self._assert_valid_user_id(context, user_id)
        self._assert_valid_tenant_id(context, tenant_id)

        cred_ref = {'user_id': user_id,
                    'tenant_id': tenant_id,
                    'access': uuid.uuid4().hex,
                    'secret': uuid.uuid4().hex}
        self.ec2_api.create_credential(context, cred_ref['access'], cred_ref)
        return {'credential': cred_ref}

    def get_credentials(self, context, user_id):
        """List all credentials for a user.

        :param context: standard context
        :param user_id: id of user
        :returns: credentials: list of ec2 credential dicts
        """
        if not self._is_admin(context):
            self._assert_identity(context, user_id)
        self._assert_valid_user_id(context, user_id)
        return {'credentials': self.ec2_api.list_credentials(context, user_id)}

    def get_credential(self, context, user_id, credential_id):
        """Retreive a user's access/secret pair by the access key.

        Grab the full access/secret pair for a given access key.

        :param context: standard context
        :param user_id: id of user
        :param credential_id: access key for credentials
        :returns: credential: dict of ec2 credential
        """
        if not self._is_admin(context):
            self._assert_identity(context, user_id)
        self._assert_valid_user_id(context, user_id)
        creds = self._get_credentials(context, credential_id)
        return {'credential': creds}

    def delete_credential(self, context, user_id, credential_id):
        """Delete a user's access/secret pair.

        Used to revoke a user's access/secret pair

        :param context: standard context
        :param user_id: id of user
        :param credential_id: access key for credentials
        :returns: bool: success
        """
        if not self._is_admin(context):
            self._assert_identity(context, user_id)
            self._assert_owner(context, user_id, credential_id)

        self._assert_valid_user_id(context, user_id)
        self._get_credentials(context, credential_id)
        return self.ec2_api.delete_credential(context, credential_id)

    def _get_credentials(self, context, credential_id):
        """Return credentials from an ID.

        :param context: standard context
        :param credential_id: id of credential
        :raises exception.Unauthorized: when credential id is invalid
        :returns: credential: dict of ec2 credential.
        """
        creds = self.ec2_api.get_credential(context,
                                            credential_id)
        if not creds:
            raise exception.Unauthorized(message='EC2 access key not found.')
        return creds

    def _assert_identity(self, context, user_id):
        """Check that the provided token belongs to the user.

        :param context: standard context
        :param user_id: id of user
        :raises exception.Forbidden: when token is invalid

        """
        try:
            token_ref = self.token_api.get_token(
                context=context,
                token_id=context['token_id'])
        except exception.TokenNotFound:
            raise exception.Unauthorized()
        token_user_id = token_ref['user'].get('id')
        if not token_user_id == user_id:
            raise exception.Forbidden()

    def _is_admin(self, context):
        """Wrap admin assertion error return statement.

        :param context: standard context
        :returns: bool: success

        """
        try:
            self.assert_admin(context)
            return True
        except exception.Forbidden:
            return False

    def _assert_owner(self, context, user_id, credential_id):
        """Ensure the provided user owns the credential.

        :param context: standard context
        :param user_id: expected credential owner
        :param credential_id: id of credential object
        :raises exception.Forbidden: on failure

        """
        cred_ref = self.ec2_api.get_credential(context, credential_id)
        if not user_id == cred_ref['user_id']:
            raise exception.Forbidden()

    def _assert_valid_user_id(self, context, user_id):
        """Ensure a valid user id.

        :param context: standard context
        :param user_id: expected credential owner
        :raises exception.UserNotFound: on failure

        """
        user_ref = self.identity_api.get_user(
            context=context,
            user_id=user_id)
        if not user_ref:
            raise exception.UserNotFound(user_id=user_id)

    def _assert_valid_tenant_id(self, context, tenant_id):
        """Ensure a valid tenant id.

        :param context: standard context
        :param user_id: expected credential owner
        :raises exception.UserNotFound: on failure

        """
        tenant_ref = self.identity_api.get_tenant(
            context=context,
            tenant_id=tenant_id)
        if not tenant_ref:
            raise exception.TenantNotFound(tenant_id=tenant_id)
<|endoftext|>


<commit_before>__author__ = 'grokrz'
<commit_msg>add script<commit_after>from math import exp

__author__ = 'grokrz'

def log(x, i):
    if i == 1:
        return 1.0 / (1.0 + exp(-x))
    else:
        return 1.0 / (1.0 + exp(x))

def delta(x, i):
    if i == 1:
        return 1.0 - log(x, i)
    else:
        return -log(x, i)

data = [(2.5, 1), (0.3, -1), (2.8, 1), (0.5, 1)]
prob = 1.0
der = 0.0

for (x, y) in data:
    prob *= log(x, y)
    der += x * delta(x, y)

print prob, der
<|endoftext|>


<commit_before>#! /usr/bin/env python
#
#  Simple table plotter, e.g.
#         tabplot.py *.tab
#  would plot each table in a new color.
#
#  based on:
#  http://stackoverflow.com/questions/5260593/is-there-a-command-line-tool-for-data-visualization-and-analysis
#
#  http://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related
#
#  @todo    have a batch mode

import sys
import matplotlib.pyplot as plt

for filename in sys.argv[1:]:
   with open(filename,'rt') as sf:
      table = []
      for line in sf:
         if line[0] == '#': continue
         table.append( [float(val) for val in line.split()] )
      table = [ row for row in table if len(row) ] ## remove empty rows
      if len(table[0]) == 1 :
         plt.plot( [y[0] for y in table ] )
      for x in xrange(1,len(table[0])):
         plt.plot( [ y[0] for y in table ], [ y[x] for y in table ] )

plt.show()
<commit_msg>fix typo<commit_after>#! /usr/bin/env python
#
#  Simple table plotter, e.g.
#         tabplot.py *.tab
#  would plot each table in a new color.
#
#  based on:
#  http://stackoverflow.com/questions/5260593/is-there-a-command-line-tool-for-data-visualization-and-analysis
#
#  http://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related
#
#  @todo    have a batch mode

import sys
import matplotlib.pyplot as plt

for filename in sys.argv[1:]:
   with open(filename,'rt') as sf:
      table = []
      for line in sf:
         if line[0] == '#': continue
         table.append( [float(val) for val in line.split()] )
      table = [ row for row in table if len(row) ] ## remove empty rows
      if len(table[0]) == 1 :
         plt.plot( [y[0] for y in table ] )
      for x in range(1,len(table[0])):
         plt.plot( [ y[0] for y in table ], [ y[x] for y in table ] )

plt.show()
<|endoftext|>