<commit_before>from functools import partial
from itertools import chain
from nac.common import retrieve_hdf5_data
from nac.workflows.workflow_coupling import generate_pyxaid_hamiltonians
from nac.workflows.workflow_AbsortionSpectrum import workflow_oscillator_strength
from nac.workflows.initialization import initialize
from nose.plugins.attrib import attr
from os.path import join
from qmworks.utils import dict2Setting

import h5py
import numpy as np
import os
import shutil

cp2k_main = dict2Setting({
    'cell_parameters': 28.0, 'potential': 'GTH-PBE',
    'basis': 'DZVP-MOLOPT-SR-GTH', 'specific':
    {'cp2k': {'force_eval':
              {'subsys': {'cell': {'periodic': 'None'}}, 'dft':
               {'print': {'mo': {'mo_index_range': '248 327'}},
                'scf': {'eps_scf': 0.0005, 'max_scf': 200,
                        'added_mos': 30}}}}},
    'cell_angles': [90.0, 90.0, 90.0]})

cp2k_guess = dict2Setting({
    'cell_parameters': 28.0, 'potential': 'GTH-PBE',
    'basis': 'DZVP-MOLOPT-SR-GTH', 'specific':
    {'cp2k': {'force_eval':
              {'subsys': {'cell': {'periodic': 'None'}},
               'dft': {'scf': {'eps_scf': 1e-06, 'ot':
                               {'minimizer': 'DIIS',
                                'n_diis': 7, 'preconditioner':
                                'FULL_SINGLE_INVERSE'},
                               'scf_guess': 'restart',
                               'added_mos': 0}}}}},
    'cell_angles': [90.0, 90.0, 90.0]})

# Environment data
basisname = 'DZVP-MOLOPT-SR-GTH'
path_traj_xyz = 'test/test_files/Cd33Se33_fivePoints.xyz'
scratch_path = 'scratch'
path_original_hdf5 = 'test/test_files/Cd33Se33.hdf5'
path_test_hdf5 = join(scratch_path, 'test.hdf5')
project_name = 'Cd33Se33'

@attr('slow')
def test_couplings_and_oscillators():
    """
    Test couplings and oscillator strength for Cd33Se33
    """
    if not os.path.exists(scratch_path):
        os.makedirs(scratch_path)
    try:
        shutil.copy('test/test_files/BASIS_MOLOPT', scratch_path)
        shutil.copy('test/test_files/GTH_POTENTIALS', scratch_path)

        # Run the actual test
        copy_basis_and_orbitals(path_original_hdf5, path_test_hdf5,
                                project_name)
        data = calculate_couplings_and_oscillators()
        # Check couplings
        check_properties()
        # Check oscillator
        fij = list(*chain(*data[0]))[3]
        assert abs(fij - 0.13074799310750024) < 1e-8

    finally:
        # remove tmp data and clean global config
        shutil.rmtree(scratch_path)

def calculate_couplings_and_oscillators():
    """
    Compute a couple of couplings with the Levine algorithm
    using precalculated MOs.
    """
    initial_config = initialize(
        project_name, path_traj_xyz,
        basisname=basisname, path_basis=None,
        path_potential=None, enumerate_from=0,
        calculate_guesses='first', path_hdf5=path_test_hdf5,
        scratch_path=scratch_path)

    generate_pyxaid_hamiltonians(
        'cp2k', project_name, cp2k_main,
        guess_args=cp2k_guess, nHOMO=50,
        couplings_range=(50, 30), **initial_config)

    data = workflow_oscillator_strength(
        'cp2k', project_name, cp2k_main, guess_args=cp2k_guess,
        nHOMO=50, couplings_range=(50, 30), initial_states=[49],
        final_states=[[51]], **initial_config)

    return data

def check_properties():
    """
    Test if the coupling coupling by the Levine method is correct
    """
    # Paths to all the arrays to test
    path_swaps = join(project_name, 'swaps')
    name_Sji = 'overlaps_{}/mtx_sji_t0'
    name_Sji_fixed = 'overlaps_{}/mtx_sji_t0_corrected'
    path_overlaps = [join(project_name, name_Sji.format(i)) for i in range(4)]
    path_fixed_overlaps = [join(project_name, name_Sji_fixed.format(i))
                           for i in range(4)]
    path_couplings = [join(project_name, 'coupling_{}'.format(i))
                      for i in range(4)]

    # Define partial func
    fun_original = partial(stack_retrieve, path_original_hdf5)
    fun_test = partial(stack_retrieve, path_test_hdf5)

    # Read data from the HDF5
    swaps_original = retrieve_hdf5_data(path_original_hdf5, path_swaps)
    swaps_test = retrieve_hdf5_data(path_test_hdf5, path_swaps)

    overlaps_original = fun_original(path_overlaps)
    overlaps_test = fun_test(path_overlaps)

    fixed_overlaps_original = fun_original(path_fixed_overlaps)
    fixed_overlaps_test = fun_test(path_fixed_overlaps)

    css_original = fun_original(path_couplings)
    css_test = fun_test(path_couplings)

    # Test data
    b1 = np.allclose(swaps_original, swaps_test)
    b2 = np.allclose(overlaps_original, overlaps_test)
    b3 = np.allclose(fixed_overlaps_original, fixed_overlaps_test)
    b4 = np.allclose(css_original, css_test)

    assert all((b1, b2, b3, b4))

def stack_retrieve(path_hdf5, path_prop):
    """
    Retrieve a list of Numpy arrays and create a tensor out of it
    """
    return np.stack(retrieve_hdf5_data(path_hdf5, path_prop))

def copy_basis_and_orbitals(source, dest, project_name):
    """
    Copy the Orbitals and the basis set from one the HDF5 to another
    """
    keys = [project_name, 'cp2k']
    excluded = ['coupling', 'overlaps', 'swaps']
    with h5py.File(source, 'r') as f5, h5py.File(dest, 'w') as g5:
        for k in keys:
            if k not in g5:
                g5.create_group(k)
            for l in f5[k].keys():
                if not any(x in l for x in excluded):
                    path = join(k, l)
                    f5.copy(path, g5[k])
<commit_msg>modified test of the oscillator calculation<commit_after>from functools import partial
from itertools import chain
from nac.common import retrieve_hdf5_data
from nac.workflows.workflow_coupling import generate_pyxaid_hamiltonians
from nac.workflows.workflow_AbsortionSpectrum import workflow_oscillator_strength
from nac.workflows.initialization import initialize
from nose.plugins.attrib import attr
from os.path import join
from qmworks.utils import dict2Setting

import h5py
import numpy as np
import os
import shutil

cp2k_main = dict2Setting({
    'cell_parameters': 28.0, 'potential': 'GTH-PBE',
    'basis': 'DZVP-MOLOPT-SR-GTH', 'specific':
    {'cp2k': {'force_eval':
              {'subsys': {'cell': {'periodic': 'None'}}, 'dft':
               {'print': {'mo': {'mo_index_range': '248 327'}},
                'scf': {'eps_scf': 0.0005, 'max_scf': 200,
                        'added_mos': 30}}}}},
    'cell_angles': [90.0, 90.0, 90.0]})

cp2k_guess = dict2Setting({
    'cell_parameters': 28.0, 'potential': 'GTH-PBE',
    'basis': 'DZVP-MOLOPT-SR-GTH', 'specific':
    {'cp2k': {'force_eval':
              {'subsys': {'cell': {'periodic': 'None'}},
               'dft': {'scf': {'eps_scf': 1e-06, 'ot':
                               {'minimizer': 'DIIS',
                                'n_diis': 7, 'preconditioner':
                                'FULL_SINGLE_INVERSE'},
                               'scf_guess': 'restart',
                               'added_mos': 0}}}}},
    'cell_angles': [90.0, 90.0, 90.0]})

# Environment data
basisname = 'DZVP-MOLOPT-SR-GTH'
path_traj_xyz = 'test/test_files/Cd33Se33_fivePoints.xyz'
scratch_path = 'scratch'
path_original_hdf5 = 'test/test_files/Cd33Se33.hdf5'
path_test_hdf5 = join(scratch_path, 'test.hdf5')
project_name = 'Cd33Se33'

@attr('slow')
def test_couplings_and_oscillators():
    """
    Test couplings and oscillator strength for Cd33Se33
    """
    if not os.path.exists(scratch_path):
        os.makedirs(scratch_path)
    try:
        shutil.copy('test/test_files/BASIS_MOLOPT', scratch_path)
        shutil.copy('test/test_files/GTH_POTENTIALS', scratch_path)

        # Run the actual test
        copy_basis_and_orbitals(path_original_hdf5, path_test_hdf5,
                                project_name)
        data = calculate_couplings_and_oscillators()
        # Check couplings
        check_properties()
        # Check oscillator
        fij = list(*chain(*data[0]))[3]
        assert abs(fij - 0.13074799310750024) < 1e-8

    finally:
        # remove tmp data and clean global config
        shutil.rmtree(scratch_path)

def calculate_couplings_and_oscillators():
    """
    Compute a couple of couplings with the Levine algorithm
    using precalculated MOs.
    """
    initial_config = initialize(
        project_name, path_traj_xyz,
        basisname=basisname, path_basis=None,
        path_potential=None, enumerate_from=0,
        calculate_guesses='first', path_hdf5=path_test_hdf5,
        scratch_path=scratch_path)

    generate_pyxaid_hamiltonians(
        'cp2k', project_name, cp2k_main,
        guess_args=cp2k_guess, nHOMO=50,
        couplings_range=(50, 30), **initial_config)

    data = workflow_oscillator_strength(
        'cp2k', project_name, cp2k_main, guess_args=cp2k_guess,
        nHOMO=50, couplings_range=(50, 30), initial_states=[49],
        energy_range=(0, 5),  # eV
        final_states=[[51]], **initial_config)

    return data

def check_properties():
    """
    Test if the coupling coupling by the Levine method is correct
    """
    # Paths to all the arrays to test
    path_swaps = join(project_name, 'swaps')
    name_Sji = 'overlaps_{}/mtx_sji_t0'
    name_Sji_fixed = 'overlaps_{}/mtx_sji_t0_corrected'
    path_overlaps = [join(project_name, name_Sji.format(i)) for i in range(4)]
    path_fixed_overlaps = [join(project_name, name_Sji_fixed.format(i))
                           for i in range(4)]
    path_couplings = [join(project_name, 'coupling_{}'.format(i))
                      for i in range(4)]

    # Define partial func
    fun_original = partial(stack_retrieve, path_original_hdf5)
    fun_test = partial(stack_retrieve, path_test_hdf5)

    # Read data from the HDF5
    swaps_original = retrieve_hdf5_data(path_original_hdf5, path_swaps)
    swaps_test = retrieve_hdf5_data(path_test_hdf5, path_swaps)

    overlaps_original = fun_original(path_overlaps)
    overlaps_test = fun_test(path_overlaps)

    fixed_overlaps_original = fun_original(path_fixed_overlaps)
    fixed_overlaps_test = fun_test(path_fixed_overlaps)

    css_original = fun_original(path_couplings)
    css_test = fun_test(path_couplings)

    # Test data
    b1 = np.allclose(swaps_original, swaps_test)
    b2 = np.allclose(overlaps_original, overlaps_test)
    b3 = np.allclose(fixed_overlaps_original, fixed_overlaps_test)
    b4 = np.allclose(css_original, css_test)

    assert all((b1, b2, b3, b4))

def stack_retrieve(path_hdf5, path_prop):
    """
    Retrieve a list of Numpy arrays and create a tensor out of it
    """
    return np.stack(retrieve_hdf5_data(path_hdf5, path_prop))

def copy_basis_and_orbitals(source, dest, project_name):
    """
    Copy the Orbitals and the basis set from one the HDF5 to another
    """
    keys = [project_name, 'cp2k']
    excluded = ['coupling', 'overlaps', 'swaps']
    with h5py.File(source, 'r') as f5, h5py.File(dest, 'w') as g5:
        for k in keys:
            if k not in g5:
                g5.create_group(k)
            for l in f5[k].keys():
                if not any(x in l for x in excluded):
                    path = join(k, l)
                    f5.copy(path, g5[k])
<|endoftext|>


<commit_before>from __future__ import absolute_import

import logging

from sentry.utils.imports import import_string

logger = logging.getLogger('sentry.digests')

def load(options):
    return import_string(options['path'])(**options.get('options', {}))

DEFAULT_BACKOFF = {
    'path': 'sentry.digests.backoff.IntervalBackoffStrategy',
    'options': {
        'default': 60 * 15,
        'intervals': {
            0: 30,
            1: 60 * 5,
        },
    },
}

DEFAULT_CODEC = {
    'path': 'sentry.digests.codecs.CompressedPickleCodec',
}

class Backend(object):
    """
    A digest backend coordinates the addition of records to timelines, as well
    as scheduling their digestion (processing.) This allows for summarizations
    of activity that was recorded as having occurrred during a window of time.

    A timeline is the central abstraction for digests. A timeline is a
    reverse-chronological set of records. Timelines are identified by a unique
    key. Records within a timeline are also identified by a key that is unique
    with respect to the timeline they are a part of.

    A timeline can be in one of two states: "waiting" or "ready". When the
    first record is added to a timeline, the timeline transitions to the
    "waiting" state. This transition also causes the timeline to be scheduled
    for digestion after the waiting period. After the waiting period expires,
    the timeline transitions to the "ready" state, which causes the timeline to
    be digested. After the timeline is digested, it transitions back to the
    "waiting" state (causing it to be rescheduled) if it contained records. If
    the timeline did not contain any records when it was digested, it can be
    deleted (although deletion may be preempted by a new record being added to
    the timeline, requiring it to be transitioned to "waiting" instead.)
    """
    def __init__(self, **options):
        # The ``codec`` option provides the strategy for encoding and decoding
        # records in the timeline.
        self.codec = load(options.pop('codec', DEFAULT_CODEC))

        # The ``backoff`` option provides the strategy for calculating
        # scheduling intervals.
        self.backoff = load(options.pop('backoff', DEFAULT_BACKOFF))

        # The ``capacity`` option defines the maximum number of items that
        # should be contained within a timeline. (Whether this is a hard or
        # soft limit is backend dependent -- see the ``trim_chance`` option.)
        self.capacity = options.pop('capacity', None)
        if self.capacity is not None and self.capacity < 1:
            raise ValueError('Timeline capacity must be at least 1 if used.')

        # The ``truncation_chance`` option defines the probability that an
        # ``add`` operation will trigger a truncation of the timeline to keep
        # it's size close to the defined capacity. A value of 1 will cause the
        # timeline to be truncated on every ``add`` operation (effectively
        # making it a hard limit), while a lower probability will increase the
        # chance of the timeline growing past it's intended capacity, but
        # increases the performance of ``add`` operations (by avoiding
        # truncation, which is a potentially expensive operation, especially on
        # large data sets.)
        if self.capacity:
            self.truncation_chance = options.pop('truncation_chance', 1.0 / self.capacity)
        else:
            if options.get('truncation_chance') is not None:
                raise TypeError('No timeline capacity has been set, "truncation_chance" must be None.')
            else:
                self.truncation_chance = 0.0

    def add(self, key, record):
        """
        Add a record to a timeline.

        Adding a record to a timeline also causes it to be added to the
        schedule, if it is not already present.

        If another record exists in the timeline with the same record key, it
        will be overwritten.
        """
        raise NotImplementedError

    def digest(self, key):
        """
        Extract records from a timeline for processing.

        This method acts as a context manager. The target of the ``as`` clause
        is an iterator contains all of the records contained within the digest.

        If the context manager successfully exits, all records that were part
        of the digest are removed from the timeline and the timeline is placed
        back in the "waiting" state. If an exception is raised during the
        execution of the context manager, all records are preserved and no
        state change occurs so that the next invocation will contain all
        records that the were included previously, as well as any records that
        were added in between invocations. (This means that the caller must
        either retry the digest operation until it succeeds, or wait for the
        operation to be rescheduled as part of the maintenance process for the
        items to be processed.)

        Typically, the block that is surrounded by context manager includes all
        of the processing logic necessary to summarize the timeline contents
        (since this process is generally has no side effects), while any
        irrevocable action -- such as sending an email -- should occur after
        the context manager has exited, to ensure that action is performed at
        most once.

        For example::

            with timelines.digest('project:1') as records:
                message = build_digest_email(records)

            message.send()

        """
        raise NotImplementedError

    def schedule(self, deadline):
        """
        Identify timelines that are ready for processing.

        This method moves all timelines that are ready to be digested from the
        waiting state to the ready state if their schedule time is prior to the
        deadline. This method returns an iterator of schedule entries that were
        moved.
        """
        raise NotImplementedError

    def maintenance(self, deadline):
        """
        Identify timelines that appear to be stuck in the ready state.

        This method moves all timelines that are in the ready state back to the
        waiting state if their schedule time is prior to the deadline. (This
        does not reschdule any tasks directly, and should generally be
        performed as part of the scheduler task, before the ``schedule``
        call.)

        This is designed to handle the situation where task execution is
        managed by a separate system such as RabbitMQ & Celery from scheduling.
        A digest task may not be able to be succesfully retried after a failure
        (e.g. if the process executing the task can no longer communicate with
        the messaging broker) which can result in a task remaining in the ready
        state without an execution plan.

        This may cause issues when asynchronously processing timelines and
        there is a severe backlog of timelines to be digested. Timelines in the
        "ready" state that were scheduled for execution prior to the deadline
        may still have outstanding tasks associated with them -- remember that
        without the ability to interrogate the queue, we are unable to identify
        if these tasks have finished but were unable to bea removed from the
        schedule, failed outright, or are still pending. As part of
        maintenance, those timelines are moved back to the "waiting" state for
        rescheduling, and if a pending task for a timeline that was previously
        in the "ready" state but is now back in the "waiting" state actually
        executes, it will fail to be executed. The timeline will later be moved
        back to the "ready" state and rescheduled -- so it's contents will
        eventually be processed -- but this may be significantly delayed from
        the originally scheduled time. Both the queue backlog as well as the
        frequency of the digest task raising an exception when a timeline is in
        an invalid state should be monitored. If these exceptions happen
        frequently -- especially during periods of abnormal queue growth -- the
        frequency of maintenance tasks should be decreased, or the deadline
        should be pushed further towards the past (execution grace period
        increased) or both.
        """
        raise NotImplementedError
<commit_msg>Add catch-all validate to the base Digests backend.<commit_after>from __future__ import absolute_import

import logging

from sentry.utils.imports import import_string

logger = logging.getLogger('sentry.digests')

def load(options):
    return import_string(options['path'])(**options.get('options', {}))

DEFAULT_BACKOFF = {
    'path': 'sentry.digests.backoff.IntervalBackoffStrategy',
    'options': {
        'default': 60 * 15,
        'intervals': {
            0: 30,
            1: 60 * 5,
        },
    },
}

DEFAULT_CODEC = {
    'path': 'sentry.digests.codecs.CompressedPickleCodec',
}

class Backend(object):
    """
    A digest backend coordinates the addition of records to timelines, as well
    as scheduling their digestion (processing.) This allows for summarizations
    of activity that was recorded as having occurrred during a window of time.

    A timeline is the central abstraction for digests. A timeline is a
    reverse-chronological set of records. Timelines are identified by a unique
    key. Records within a timeline are also identified by a key that is unique
    with respect to the timeline they are a part of.

    A timeline can be in one of two states: "waiting" or "ready". When the
    first record is added to a timeline, the timeline transitions to the
    "waiting" state. This transition also causes the timeline to be scheduled
    for digestion after the waiting period. After the waiting period expires,
    the timeline transitions to the "ready" state, which causes the timeline to
    be digested. After the timeline is digested, it transitions back to the
    "waiting" state (causing it to be rescheduled) if it contained records. If
    the timeline did not contain any records when it was digested, it can be
    deleted (although deletion may be preempted by a new record being added to
    the timeline, requiring it to be transitioned to "waiting" instead.)
    """
    def __init__(self, **options):
        # The ``codec`` option provides the strategy for encoding and decoding
        # records in the timeline.
        self.codec = load(options.pop('codec', DEFAULT_CODEC))

        # The ``backoff`` option provides the strategy for calculating
        # scheduling intervals.
        self.backoff = load(options.pop('backoff', DEFAULT_BACKOFF))

        # The ``capacity`` option defines the maximum number of items that
        # should be contained within a timeline. (Whether this is a hard or
        # soft limit is backend dependent -- see the ``trim_chance`` option.)
        self.capacity = options.pop('capacity', None)
        if self.capacity is not None and self.capacity < 1:
            raise ValueError('Timeline capacity must be at least 1 if used.')

        # The ``truncation_chance`` option defines the probability that an
        # ``add`` operation will trigger a truncation of the timeline to keep
        # it's size close to the defined capacity. A value of 1 will cause the
        # timeline to be truncated on every ``add`` operation (effectively
        # making it a hard limit), while a lower probability will increase the
        # chance of the timeline growing past it's intended capacity, but
        # increases the performance of ``add`` operations (by avoiding
        # truncation, which is a potentially expensive operation, especially on
        # large data sets.)
        if self.capacity:
            self.truncation_chance = options.pop('truncation_chance', 1.0 / self.capacity)
        else:
            if options.get('truncation_chance') is not None:
                raise TypeError('No timeline capacity has been set, "truncation_chance" must be None.')
            else:
                self.truncation_chance = 0.0

    def validate(self):
        pass

    def add(self, key, record):
        """
        Add a record to a timeline.

        Adding a record to a timeline also causes it to be added to the
        schedule, if it is not already present.

        If another record exists in the timeline with the same record key, it
        will be overwritten.
        """
        raise NotImplementedError

    def digest(self, key):
        """
        Extract records from a timeline for processing.

        This method acts as a context manager. The target of the ``as`` clause
        is an iterator contains all of the records contained within the digest.

        If the context manager successfully exits, all records that were part
        of the digest are removed from the timeline and the timeline is placed
        back in the "waiting" state. If an exception is raised during the
        execution of the context manager, all records are preserved and no
        state change occurs so that the next invocation will contain all
        records that the were included previously, as well as any records that
        were added in between invocations. (This means that the caller must
        either retry the digest operation until it succeeds, or wait for the
        operation to be rescheduled as part of the maintenance process for the
        items to be processed.)

        Typically, the block that is surrounded by context manager includes all
        of the processing logic necessary to summarize the timeline contents
        (since this process is generally has no side effects), while any
        irrevocable action -- such as sending an email -- should occur after
        the context manager has exited, to ensure that action is performed at
        most once.

        For example::

            with timelines.digest('project:1') as records:
                message = build_digest_email(records)

            message.send()

        """
        raise NotImplementedError

    def schedule(self, deadline):
        """
        Identify timelines that are ready for processing.

        This method moves all timelines that are ready to be digested from the
        waiting state to the ready state if their schedule time is prior to the
        deadline. This method returns an iterator of schedule entries that were
        moved.
        """
        raise NotImplementedError

    def maintenance(self, deadline):
        """
        Identify timelines that appear to be stuck in the ready state.

        This method moves all timelines that are in the ready state back to the
        waiting state if their schedule time is prior to the deadline. (This
        does not reschdule any tasks directly, and should generally be
        performed as part of the scheduler task, before the ``schedule``
        call.)

        This is designed to handle the situation where task execution is
        managed by a separate system such as RabbitMQ & Celery from scheduling.
        A digest task may not be able to be succesfully retried after a failure
        (e.g. if the process executing the task can no longer communicate with
        the messaging broker) which can result in a task remaining in the ready
        state without an execution plan.

        This may cause issues when asynchronously processing timelines and
        there is a severe backlog of timelines to be digested. Timelines in the
        "ready" state that were scheduled for execution prior to the deadline
        may still have outstanding tasks associated with them -- remember that
        without the ability to interrogate the queue, we are unable to identify
        if these tasks have finished but were unable to bea removed from the
        schedule, failed outright, or are still pending. As part of
        maintenance, those timelines are moved back to the "waiting" state for
        rescheduling, and if a pending task for a timeline that was previously
        in the "ready" state but is now back in the "waiting" state actually
        executes, it will fail to be executed. The timeline will later be moved
        back to the "ready" state and rescheduled -- so it's contents will
        eventually be processed -- but this may be significantly delayed from
        the originally scheduled time. Both the queue backlog as well as the
        frequency of the digest task raising an exception when a timeline is in
        an invalid state should be monitored. If these exceptions happen
        frequently -- especially during periods of abnormal queue growth -- the
        frequency of maintenance tasks should be decreased, or the deadline
        should be pushed further towards the past (execution grace period
        increased) or both.
        """
        raise NotImplementedError
<|endoftext|>


<commit_before>import unittest
from cronquot.cronquot import has_directory

class CronquotTest(unittest.TestCase):

    def test_has_directory(self):
        self.assertTrue(has_directory('/tmp'))

if __name__ == '__main__':
    unittest.test()
<commit_msg>Fix to test crontab dir<commit_after>import unittest
import os
from cronquot.cronquot import has_directory

class CronquotTest(unittest.TestCase):

    def test_has_directory(self):
        sample_dir = os.path.join(
                os.path.dirname(__file__), 'crontab')
        self.assertTrue(has_directory(sample_dir))

if __name__ == '__main__':
    unittest.test()
<|endoftext|>


<commit_before># -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2013 SF Isle of Man Limited
#
# PyBossa 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.
#
# PyBossa 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 PyBossa.  If not, see <http://www.gnu.org/licenses/>.
from default import Test
from pybossa.view.facebook import manage_user, manage_user_login
from mock import patch
from factories import UserFactory
from pybossa.util import username_from_full_name

class TestFacebook(Test):

    def setUp(self):
        super(TestFacebook, self).setUp()
        self.user_data = {
            "id": 1234567890,
            "email": "me@facebook.com",
            "first_name": "Mauricio",
            "last_name": "Perez Sanchez",
            "name": "Mauricio Perez Sanchez"
        }
        self.name = username_from_full_name(self.user_data['name'])

    def test_manage_user_with_email(self):
        """Test FACEBOOK manage_user works."""
        # First with a new user
        token = 't'
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

        # Second with the same user
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

    def test_manage_user_without_email(self):
        """Test FACEBOOK manage_user without e-mail works."""
        # First with a new user
        del self.user_data['email']
        token = 't'
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

        # Second with the same user
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    def test_manage_user_with_email_newsletter(self, newsletter):
        """Test FACEBOOK manage_user newsletter works."""
        newsletter.app = True
        token = 't'
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

        newsletter.subscribe_user.assert_called_once_with(user)

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    def test_manage_user_without_email_newsletter(self, newsletter):
        """Test FACEBOOK manage_user without e-mail newsletter works."""
        newsletter.app = True
        del self.user_data['email']
        token = 't'
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user
        err_msg = "It should not be called."
        assert newsletter.subscribe_user.called is False, err_msg

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_local(self, redirect,
                                     url_for, flash,
                                     login_user,
                                     newsletter):
        """Test manage login user works."""
        newsletter.app = True
        user = UserFactory.create()
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(None, user_data, next_url)
        url_for.assert_called_once_with('account.forgot_password')

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_google_token(self, redirect,
                                            url_for, flash,
                                            login_user,
                                            newsletter):
        """Test manage login user works."""
        newsletter.app = True
        user = UserFactory.create(info={'google_token': 'k'})
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(None, user_data, next_url)
        url_for.assert_called_once_with('account.signin')

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_empty(self, redirect,
                                     url_for, flash,
                                     login_user,
                                     newsletter):
        """Test manage login user works."""
        newsletter.app = True
        user_data = dict(name='algo', email='email')
        next_url = '/'
        manage_user_login(None, user_data, next_url)
        url_for.assert_called_once_with('account.signin')

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_update_email(self, redirect,
                                            url_for, flash,
                                            login_user,
                                            newsletter):
        """Test manage login user works."""
        newsletter.app = True
        user = UserFactory.create(email_addr="None")
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(user, user_data, next_url)
        login_user.assert_called_once_with(user, remember=True)
        url_for.assert_called_once_with('account.update_profile',
                                        name=user.name)

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_good_email(self, redirect,
                                          url_for, flash,
                                          login_user,
                                          newsletter):
        """Test manage login user with good email works."""
        newsletter.app = True
        user = UserFactory.create()
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(user, user_data, next_url)
        login_user.assert_called_once_with(user, remember=True)
        url_for.assert_called_once_with('account.newsletter_subscribe',
                                        next=next_url)

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_already_asked(self, redirect,
                                             url_for, flash,
                                             login_user,
                                             newsletter):
        """Test manage login user already asked works."""
        newsletter.app = True
        user = UserFactory.create(newsletter_prompted=True)
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(user, user_data, next_url)
        login_user.assert_called_once_with(user, remember=True)
        redirect.assert_called_once_with(next_url)
<commit_msg>Update test<commit_after># -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2013 SF Isle of Man Limited
#
# PyBossa 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.
#
# PyBossa 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 PyBossa.  If not, see <http://www.gnu.org/licenses/>.
from default import Test
from pybossa.view.facebook import manage_user, manage_user_login
from mock import patch
from factories import UserFactory
from pybossa.util import username_from_full_name

class TestFacebook(Test):

    def setUp(self):
        super(TestFacebook, self).setUp()
        self.user_data = {
            "id": 1234567890,
            "email": "me@facebook.com",
            "first_name": "Mauricio",
            "last_name": "Perez Sanchez",
            "name": "Mauricio Perez Sanchez"
        }
        self.name = username_from_full_name(self.user_data['name'])

    def test_manage_user_with_email(self):
        """Test FACEBOOK manage_user works."""
        # First with a new user
        token = 't'
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

        # Second with the same user
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

    def test_manage_user_without_email(self):
        """Test FACEBOOK manage_user without e-mail works."""
        # First with a new user
        del self.user_data['email']
        token = 't'
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

        # Second with the same user
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    def test_manage_user_with_email_newsletter(self, newsletter):
        """Test FACEBOOK manage_user newsletter works."""
        newsletter.app = True
        token = 't'
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user

        newsletter.subscribe_user.assert_called_once_with(user)

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    def test_manage_user_without_email_newsletter(self, newsletter):
        """Test FACEBOOK manage_user without e-mail newsletter works."""
        newsletter.app = True
        del self.user_data['email']
        token = 't'
        user = manage_user(token, self.user_data)
        assert user.email_addr == self.user_data['email'], user
        assert user.name == self.name, user
        assert user.fullname == self.user_data['name'], user
        assert user.facebook_user_id == self.user_data['id'], user
        err_msg = "It should not be called."
        assert newsletter.subscribe_user.called is False, err_msg

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_local(self, redirect,
                                     url_for, flash,
                                     login_user,
                                     newsletter):
        """Test manage login user works."""
        newsletter.app = True
        user = UserFactory.create()
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(None, user_data, next_url)
        url_for.assert_called_once_with('account.forgot_password')

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_google_token(self, redirect,
                                            url_for, flash,
                                            login_user,
                                            newsletter):
        """Test manage login user works."""
        newsletter.app = True
        user = UserFactory.create(info={'google_token': 'k'})
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(None, user_data, next_url)
        url_for.assert_called_once_with('account.signin')

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_empty(self, redirect,
                                     url_for, flash,
                                     login_user,
                                     newsletter):
        """Test manage login user works."""
        newsletter.app = True
        user_data = dict(name='algo', email='email')
        next_url = '/'
        manage_user_login(None, user_data, next_url)
        url_for.assert_called_once_with('account.signin')

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_update_email(self, redirect,
                                            url_for, flash,
                                            login_user,
                                            newsletter):
        """Test manage login user works."""
        newsletter.app = True
        user = UserFactory.create(name='johndoe', email_addr='johndoe')
        user_data = dict(name=user.name)
        next_url = '/'
        manage_user_login(user, user_data, next_url)
        login_user.assert_called_once_with(user, remember=True)
        url_for.assert_called_once_with('account.update_profile',
                                        name=user.name)

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_good_email(self, redirect,
                                          url_for, flash,
                                          login_user,
                                          newsletter):
        """Test manage login user with good email works."""
        newsletter.app = True
        user = UserFactory.create()
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(user, user_data, next_url)
        login_user.assert_called_once_with(user, remember=True)
        url_for.assert_called_once_with('account.newsletter_subscribe',
                                        next=next_url)

    @patch('pybossa.view.facebook.newsletter', autospec=True)
    @patch('pybossa.view.facebook.login_user', return_value=True)
    @patch('pybossa.view.facebook.flash', return_value=True)
    @patch('pybossa.view.facebook.url_for', return_value=True)
    @patch('pybossa.view.facebook.redirect', return_value=True)
    def test_manage_login_user_already_asked(self, redirect,
                                             url_for, flash,
                                             login_user,
                                             newsletter):
        """Test manage login user already asked works."""
        newsletter.app = True
        user = UserFactory.create(newsletter_prompted=True)
        user_data = dict(name=user.name, email=user.email_addr)
        next_url = '/'
        manage_user_login(user, user_data, next_url)
        login_user.assert_called_once_with(user, remember=True)
        redirect.assert_called_once_with(next_url)
<|endoftext|>


<commit_before>#! /usr/bin/env python
# vi:ts=4:et

# $Id$

"""Setup script for the PycURL module distribution."""

PACKAGE = "pycurl"
VERSION = "7.10.3.2"

import glob, os, re, sys, string
import distutils
from distutils.core import setup
from distutils.extension import Extension
from distutils.util import split_quoted
from distutils.version import LooseVersion

include_dirs = []
define_macros = []
library_dirs = []
libraries = []
runtime_library_dirs = []
extra_objects = []
extra_compile_args = []
extra_link_args = []

def scan_argv(s, default):
    p = default
    i = 1
    while i < len(sys.argv):
        arg = sys.argv[i]
        if string.find(arg, s) == 0:
            p = arg[len(s):]
            assert p, arg
            del sys.argv[i]
        else:
            i = i + 1
    ##print sys.argv
    return p

# transform an environment variable (e.g. "LIB") to library_dirs[]
def add_libdirs(envvar, sep, delete=0, fatal=1):
    env = os.environ
    if not env.get(envvar):
        return 0
    r = 0
    for dir in string.split(env.get(envvar), sep):
        dir = string.strip(dir)
        if not dir:
            continue
        if os.path.isdir(dir):
            if dir not in library_dirs:
                library_dirs.append(dir)
        else:
            r = -1
            if fatal:
                print "FATAL: bad directory %s in environment variable %s" % (dir, envvar)
                raise SystemExit
    if delete:
        del env[envvar]
    return r

if sys.platform == "win32":
    # Windows users have to configure the CURL_DIR path parameter to match
    # their cURL source installation.  The path set here is just an example
    # and thus unlikely to match your installation.
    CURL_DIR = r"c:\src\build\curl-7.10.3"
    CURL_DIR = scan_argv("--curl-dir=", CURL_DIR)
    print "Using curl directory:", CURL_DIR
    assert os.path.isdir(CURL_DIR), "please check CURL_DIR in setup.py"
    include_dirs.append(os.path.join(CURL_DIR, "include"))
    extra_objects.append(os.path.join(CURL_DIR, "lib", "libcurl.lib"))
    extra_link_args.extend(["gdi32.lib", "winmm.lib", "ws2_32.lib",])
    add_libdirs("LIB", ";")
else:
    # Find out the rest the hard way
    CURL_CONFIG = "curl-config"
    CURL_CONFIG = scan_argv("--curl-config=", CURL_CONFIG)
    d = os.popen("%s --version" % CURL_CONFIG).read()
    if d:
        d = string.strip(d)
    if not d:
        raise Exception, ("`%s' not found -- please install the libcurl development files" % CURL_CONFIG)
    print "Using %s (%s)" % (CURL_CONFIG, d)
    for e in split_quoted(os.popen("%s --cflags" % CURL_CONFIG).read()):
        if e[:2] == "-I":
            # do not add /usr/include
            if not re.search(r"^\/+usr\/+include\/*$", e[2:]):
                include_dirs.append(e[2:])
        else:
            extra_compile_args.append(e)
    for e in split_quoted(os.popen("%s --libs" % CURL_CONFIG).read()):
        if e[:2] == "-l":
            libraries.append(e[2:])
        elif e[:2] == "-L":
            library_dirs.append(e[2:])
        else:
            extra_link_args.append(e)
    if not libraries:
        libraries.append("curl")
    # Add extra compile flag for MacOS X
    if sys.platform[:-1] == "darwin":
        extra_link_args.append("-flat_namespace")

###############################################################################

def get_kw(**kw): return kw

ext = Extension(
    name=PACKAGE,
    sources=[
        os.path.join("src", "curl.c"),
    ],
    include_dirs=include_dirs,
    define_macros=define_macros,
    library_dirs=library_dirs,
    libraries=libraries,
    runtime_library_dirs=runtime_library_dirs,
    extra_objects=extra_objects,
    extra_compile_args=extra_compile_args,
    extra_link_args=extra_link_args,
)
##print ext.__dict__; sys.exit(1)

###############################################################################

# prepare data_files

def get_data_files():
    # a list of tuples with (path to install to, a list of local files)
    data_files = []
    if sys.platform == "win32":
        datadir = os.path.join("doc", PACKAGE)
    else:
        datadir = os.path.join("share", "doc", PACKAGE)
    #
    files = ["ChangeLog", "COPYING", "INSTALL", "README", "TODO",]
    if files:
        data_files.append((os.path.join(datadir), files))
    files = glob.glob(os.path.join("doc", "*.html"))
    if files:
        data_files.append((os.path.join(datadir, "html"), files))
    files = glob.glob(os.path.join("examples", "*.py"))
    if files:
        data_files.append((os.path.join(datadir, "examples"), files))
    files = glob.glob(os.path.join("tests", "*.py"))
    if files:
        data_files.append((os.path.join(datadir, "tests"), files))
    #
    assert data_files
    for install_dir, files in data_files:
        assert files
        for f in files:
            assert os.path.isfile(f), (f, install_dir)
    return data_files

##print get_data_files(); sys.exit(1)

###############################################################################

setup_args = get_kw(
    name=PACKAGE,
    version=VERSION,
    description="PycURL -- cURL library module for Python",
    author="Kjetil Jacobsen, Markus F.X.J. Oberhumer",
    author_email="kjetilja@cs.uit.no, markus@oberhumer.com",
    maintainer="Kjetil Jacobsen, Markus F.X.J. Oberhumer",
    maintainer_email="kjetilja@cs.uit.no, markus@oberhumer.com",
    url="http://pycurl.sourceforge.net/",
    license="GNU Lesser General Public License (LGPL)",
    data_files=get_data_files(),
    ext_modules=[ext],
    long_description="""
This module provides Python bindings for the cURL library.""",
)

##print distutils.__version__
setup_args["licence"] = setup_args["license"]
if LooseVersion(distutils.__version__) > LooseVersion("1.0.1"):
    setup_args["platforms"] = "All"

if __name__ == "__main__":
    for o in ext.extra_objects:
        assert os.path.isfile(o), o
    apply(setup, (), setup_args)

<commit_msg>Simplified add_libdirs().<commit_after>#! /usr/bin/env python
# vi:ts=4:et

# $Id$

"""Setup script for the PycURL module distribution."""

PACKAGE = "pycurl"
VERSION = "7.10.3.2"

import glob, os, re, sys, string
import distutils
from distutils.core import setup
from distutils.extension import Extension
from distutils.util import split_quoted
from distutils.version import LooseVersion

include_dirs = []
define_macros = []
library_dirs = []
libraries = []
runtime_library_dirs = []
extra_objects = []
extra_compile_args = []
extra_link_args = []

def scan_argv(s, default):
    p = default
    i = 1
    while i < len(sys.argv):
        arg = sys.argv[i]
        if string.find(arg, s) == 0:
            p = arg[len(s):]
            assert p, arg
            del sys.argv[i]
        else:
            i = i + 1
    ##print sys.argv
    return p

# append contents of an environment variable to library_dirs[]
def add_libdirs(envvar, sep, fatal=1):
    v = os.environ.get(envvar)
    if not v:
        return
    for dir in string.split(v, sep):
        dir = string.strip(dir)
        if not dir:
            continue
        dir = os.path.normpath(dir)
        if os.path.isdir(dir):
            if not dir in library_dirs:
                library_dirs.append(dir)
        elif fatal:
            print "FATAL: bad directory %s in environment variable %s" % (dir, envvar)
            sys.exit(1)

if sys.platform == "win32":
    # Windows users have to configure the CURL_DIR path parameter to match
    # their cURL source installation.  The path set here is just an example
    # and thus unlikely to match your installation.
    CURL_DIR = r"c:\src\build\curl-7.10.3"
    CURL_DIR = scan_argv("--curl-dir=", CURL_DIR)
    print "Using curl directory:", CURL_DIR
    assert os.path.isdir(CURL_DIR), "please check CURL_DIR in setup.py"
    include_dirs.append(os.path.join(CURL_DIR, "include"))
    extra_objects.append(os.path.join(CURL_DIR, "lib", "libcurl.lib"))
    extra_link_args.extend(["gdi32.lib", "winmm.lib", "ws2_32.lib",])
    add_libdirs("LIB", ";")
else:
    # Find out the rest the hard way
    CURL_CONFIG = "curl-config"
    CURL_CONFIG = scan_argv("--curl-config=", CURL_CONFIG)
    d = os.popen("%s --version" % CURL_CONFIG).read()
    if d:
        d = string.strip(d)
    if not d:
        raise Exception, ("`%s' not found -- please install the libcurl development files" % CURL_CONFIG)
    print "Using %s (%s)" % (CURL_CONFIG, d)
    for e in split_quoted(os.popen("%s --cflags" % CURL_CONFIG).read()):
        if e[:2] == "-I":
            # do not add /usr/include
            if not re.search(r"^\/+usr\/+include\/*$", e[2:]):
                include_dirs.append(e[2:])
        else:
            extra_compile_args.append(e)
    for e in split_quoted(os.popen("%s --libs" % CURL_CONFIG).read()):
        if e[:2] == "-l":
            libraries.append(e[2:])
        elif e[:2] == "-L":
            library_dirs.append(e[2:])
        else:
            extra_link_args.append(e)
    if not libraries:
        libraries.append("curl")
    # Add extra compile flag for MacOS X
    if sys.platform[:-1] == "darwin":
        extra_link_args.append("-flat_namespace")

###############################################################################

def get_kw(**kw): return kw

ext = Extension(
    name=PACKAGE,
    sources=[
        os.path.join("src", "curl.c"),
    ],
    include_dirs=include_dirs,
    define_macros=define_macros,
    library_dirs=library_dirs,
    libraries=libraries,
    runtime_library_dirs=runtime_library_dirs,
    extra_objects=extra_objects,
    extra_compile_args=extra_compile_args,
    extra_link_args=extra_link_args,
)
##print ext.__dict__; sys.exit(1)

###############################################################################

# prepare data_files

def get_data_files():
    # a list of tuples with (path to install to, a list of local files)
    data_files = []
    if sys.platform == "win32":
        datadir = os.path.join("doc", PACKAGE)
    else:
        datadir = os.path.join("share", "doc", PACKAGE)
    #
    files = ["ChangeLog", "COPYING", "INSTALL", "README", "TODO",]
    if files:
        data_files.append((os.path.join(datadir), files))
    files = glob.glob(os.path.join("doc", "*.html"))
    if files:
        data_files.append((os.path.join(datadir, "html"), files))
    files = glob.glob(os.path.join("examples", "*.py"))
    if files:
        data_files.append((os.path.join(datadir, "examples"), files))
    files = glob.glob(os.path.join("tests", "*.py"))
    if files:
        data_files.append((os.path.join(datadir, "tests"), files))
    #
    assert data_files
    for install_dir, files in data_files:
        assert files
        for f in files:
            assert os.path.isfile(f), (f, install_dir)
    return data_files

##print get_data_files(); sys.exit(1)

###############################################################################

setup_args = get_kw(
    name=PACKAGE,
    version=VERSION,
    description="PycURL -- cURL library module for Python",
    author="Kjetil Jacobsen, Markus F.X.J. Oberhumer",
    author_email="kjetilja@cs.uit.no, markus@oberhumer.com",
    maintainer="Kjetil Jacobsen, Markus F.X.J. Oberhumer",
    maintainer_email="kjetilja@cs.uit.no, markus@oberhumer.com",
    url="http://pycurl.sourceforge.net/",
    license="GNU Lesser General Public License (LGPL)",
    data_files=get_data_files(),
    ext_modules=[ext],
    long_description="""
This module provides Python bindings for the cURL library.""",
)

##print distutils.__version__
setup_args["licence"] = setup_args["license"]
if LooseVersion(distutils.__version__) > LooseVersion("1.0.1"):
    setup_args["platforms"] = "All"

if __name__ == "__main__":
    for o in ext.extra_objects:
        assert os.path.isfile(o), o
    apply(setup, (), setup_args)

<|endoftext|>


<commit_before>#
# Copyright (c) 2014, Arista Networks, Inc.
# 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 Arista Networks 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 ARISTA NETWORKS
# 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.
#
import os
import sys
import importlib
import inspect
import logging
import logging.handlers
import collections

from itertools import tee

try:
    # Try Python 3.x import first
    from itertools import zip_longest
except ImportError:
    # Use Python 2.7 import as a fallback
    from itertools import izip_longest as zip_longest

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)

# Create a handler to log messages to syslog
if sys.platform == "darwin":
    _syslog_handler = logging.handlers.SysLogHandler(address='/var/run/syslog')
else:
    _syslog_handler = logging.handlers.SysLogHandler()
_LOGGER.addHandler(_syslog_handler)

# Create a handler to log messages to stderr
_stderr_formatter = logging.Formatter('\n\n**** LOG NOTE ****\n%(message)s\n')
_stderr_handler = logging.StreamHandler()
_stderr_handler.setFormatter(_stderr_formatter)
_LOGGER.addHandler(_stderr_handler)

def import_module(name):
    """ Imports a module into the current runtime environment

    This function emulates the Python import system that allows for
    importing full path modules.  It will break down the module and
    import each part (or skip if it is already loaded in cache).

    Args:
        name (str): The name of the module to import.  This should be
            the full path of the module

    Returns:
        The module that was imported

    """
    if name in sys.modules:
        # Be sure not to reload a previously loaded module
        mod = sys.modules[name]
    else:
        mod = importlib.import_module(name)
    return mod

def load_module(name):
    """ Attempts to load a module into the current environment

    This function will load a module specified by name.  The module
    name is first checked to see if it is already loaded and will return
    the module if it is.   If the module hasn't been previously loaded
    it will attempt to import it

    Args:
        name (str): Specifies the full name of the module.  For instance
            pyeapi.api.vlans

    Returns:
        The module that has been imported or retrieved from the sys modules

    """
    try:
        mod = None
        mod = sys.modules[name]
    except KeyError:
        mod = import_module(name)
    finally:
        if not mod:
            raise ImportError('unable to import module %s' % name)
        return mod

class ProxyCall(object):

    def __init__(self, proxy, method):
        self.proxy = proxy
        self.method = method

    def __call__(self, *args, **kwargs):
        return self.proxy(self.method, *args, **kwargs)

def islocalconnection():
    """ Checks if running locally on EOS device or remotely

    This function will return a boolean indicating if the current
    execution environment is running locally on an EOS device (True) or
    running remotely and communicating over HTTP/S (False)

    Returns:
        A boolean value that indicates whether or not the current
            thread is local or remote
    """
    return os.path.exists('/etc/Eos-release')

def debug(text):
    """Log a message to syslog and stderr

    Args:
        text (str): The string object to print

    """
    frame = inspect.currentframe().f_back
    module = frame.f_globals['__name__']
    func = frame.f_code.co_name
    msg = "%s.%s: %s" % (module, func, text)
    _LOGGER.debug(msg)

def make_iterable(value):
    """Converts the supplied value to a list object

    This function will inspect the supplied value and return an
    iterable in the form of a list.

    Args:
        value (object): An valid Python object

    Returns:
        An iterable object of type list
    """
    if sys.version_info <= (3, 0):
        # Convert unicode values to strings for Python 2
        if isinstance(value, unicode):
            value = str(value)
    if isinstance(value, str) or isinstance(value, dict):
        value = [value]

    if not isinstance(value, collections.abc.Iterable):
        raise TypeError('value must be an iterable object')

    return value

def lookahead(it):
    it1, it2 = tee(iter(it))
    next(it2)
    return zip_longest(it1, it2)

def expand_range(arg, value_delimiter=',', range_delimiter='-'):
    """
    Expands a delimited string of ranged integers into a list of strings

    :param arg: The string range to expand
    :param value_delimiter: The delimiter that separates values
    :param range_delimiter: The delimiter that signifies a range of values

    :return: An array of expanded string values
    :rtype: list
    """
    values = list()
    expanded = arg.split(value_delimiter)
    for item in expanded:
        if range_delimiter in item:
            start, end = item.split(range_delimiter)
            _expand = range(int(start), int(end) + 1)
            values.extend([str(x) for x in _expand])
        else:
            values.extend([item])
    return [str(x) for x in values]

def collapse_range(arg, value_delimiter=',', range_delimiter='-'):
    """
    Collapses a list of values into a range set

    :param arg: The list of values to collapse
    :param value_delimiter: The delimiter that separates values
    :param range_delimiter: The delimiter that separates a value range

    :return: An array of collapsed string values
    :rtype: list
    """
    values = list()
    expanded = arg.split(value_delimiter)
    range_start = None

    for v1, v2 in lookahead(expanded):
        if v2:
            v1 = int(v1)
            v2 = int(v2)
            if (v1 + 1) == v2:
                if not range_start:
                    range_start = v1
            elif range_start:
                item = '{}{}{}'.format(range_start, range_delimiter, v1)
                values.extend([item])
                range_start = None
            else:
                values.extend([v1])
        elif range_start:
            item = '{}{}{}'.format(range_start, range_delimiter, v1)
            values.extend([item])
            range_start = None
        else:
            values.extend([v1])
    return [str(x) for x in values]
<commit_msg>Fix python2 dependancy on collections<commit_after>#
# Copyright (c) 2014, Arista Networks, Inc.
# 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 Arista Networks 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 ARISTA NETWORKS
# 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.
#
import os
import sys
import importlib
import inspect
import logging
import logging.handlers
import collections

from itertools import tee

try:
    # Try Python 3.x import first
    from itertools import zip_longest
except ImportError:
    # Use Python 2.7 import as a fallback
    from itertools import izip_longest as zip_longest

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)

# Create a handler to log messages to syslog
if sys.platform == "darwin":
    _syslog_handler = logging.handlers.SysLogHandler(address='/var/run/syslog')
else:
    _syslog_handler = logging.handlers.SysLogHandler()
_LOGGER.addHandler(_syslog_handler)

# Create a handler to log messages to stderr
_stderr_formatter = logging.Formatter('\n\n**** LOG NOTE ****\n%(message)s\n')
_stderr_handler = logging.StreamHandler()
_stderr_handler.setFormatter(_stderr_formatter)
_LOGGER.addHandler(_stderr_handler)

def import_module(name):
    """ Imports a module into the current runtime environment

    This function emulates the Python import system that allows for
    importing full path modules.  It will break down the module and
    import each part (or skip if it is already loaded in cache).

    Args:
        name (str): The name of the module to import.  This should be
            the full path of the module

    Returns:
        The module that was imported

    """
    if name in sys.modules:
        # Be sure not to reload a previously loaded module
        mod = sys.modules[name]
    else:
        mod = importlib.import_module(name)
    return mod

def load_module(name):
    """ Attempts to load a module into the current environment

    This function will load a module specified by name.  The module
    name is first checked to see if it is already loaded and will return
    the module if it is.   If the module hasn't been previously loaded
    it will attempt to import it

    Args:
        name (str): Specifies the full name of the module.  For instance
            pyeapi.api.vlans

    Returns:
        The module that has been imported or retrieved from the sys modules

    """
    try:
        mod = None
        mod = sys.modules[name]
    except KeyError:
        mod = import_module(name)
    finally:
        if not mod:
            raise ImportError('unable to import module %s' % name)
        return mod

class ProxyCall(object):

    def __init__(self, proxy, method):
        self.proxy = proxy
        self.method = method

    def __call__(self, *args, **kwargs):
        return self.proxy(self.method, *args, **kwargs)

def islocalconnection():
    """ Checks if running locally on EOS device or remotely

    This function will return a boolean indicating if the current
    execution environment is running locally on an EOS device (True) or
    running remotely and communicating over HTTP/S (False)

    Returns:
        A boolean value that indicates whether or not the current
            thread is local or remote
    """
    return os.path.exists('/etc/Eos-release')

def debug(text):
    """Log a message to syslog and stderr

    Args:
        text (str): The string object to print

    """
    frame = inspect.currentframe().f_back
    module = frame.f_globals['__name__']
    func = frame.f_code.co_name
    msg = "%s.%s: %s" % (module, func, text)
    _LOGGER.debug(msg)

def make_iterable(value):
    """Converts the supplied value to a list object

    This function will inspect the supplied value and return an
    iterable in the form of a list.

    Args:
        value (object): An valid Python object

    Returns:
        An iterable object of type list
    """
    if sys.version_info <= (3, 0):
        # Convert unicode values to strings for Python 2
        if isinstance(value, unicode):
            value = str(value)
    if isinstance(value, str) or isinstance(value, dict):
        value = [value]

    if sys.version_info <= (3, 3):
        if not isinstance(value, collections.Iterable):
            raise TypeError('value must be an iterable object')
    else:
        if not isinstance(value, collections.abc.Iterable):
            raise TypeError('value must be an iterable object')

    return value

def lookahead(it):
    it1, it2 = tee(iter(it))
    next(it2)
    return zip_longest(it1, it2)

def expand_range(arg, value_delimiter=',', range_delimiter='-'):
    """
    Expands a delimited string of ranged integers into a list of strings

    :param arg: The string range to expand
    :param value_delimiter: The delimiter that separates values
    :param range_delimiter: The delimiter that signifies a range of values

    :return: An array of expanded string values
    :rtype: list
    """
    values = list()
    expanded = arg.split(value_delimiter)
    for item in expanded:
        if range_delimiter in item:
            start, end = item.split(range_delimiter)
            _expand = range(int(start), int(end) + 1)
            values.extend([str(x) for x in _expand])
        else:
            values.extend([item])
    return [str(x) for x in values]

def collapse_range(arg, value_delimiter=',', range_delimiter='-'):
    """
    Collapses a list of values into a range set

    :param arg: The list of values to collapse
    :param value_delimiter: The delimiter that separates values
    :param range_delimiter: The delimiter that separates a value range

    :return: An array of collapsed string values
    :rtype: list
    """
    values = list()
    expanded = arg.split(value_delimiter)
    range_start = None

    for v1, v2 in lookahead(expanded):
        if v2:
            v1 = int(v1)
            v2 = int(v2)
            if (v1 + 1) == v2:
                if not range_start:
                    range_start = v1
            elif range_start:
                item = '{}{}{}'.format(range_start, range_delimiter, v1)
                values.extend([item])
                range_start = None
            else:
                values.extend([v1])
        elif range_start:
            item = '{}{}{}'.format(range_start, range_delimiter, v1)
            values.extend([item])
            range_start = None
        else:
            values.extend([v1])
    return [str(x) for x in values]
<|endoftext|>


<commit_before>#!/usr/bin/env python
from datetime import date

import click
from reportlab.lib import colors

from pyearcal import YearCalendar, font_loader
from pyearcal.l10n import get_locale
from pyearcal.image_sources import UnsortedImageDirectory, SortedImageDirectory

def load_special_days(path, year):
    """Load special days from external file.

    The file is expected to have lines of MM, DD format
    """
    with open(path, "r") as f:
        days = []
        for line in f:
            month, day = [int(s.strip()) for s in line.split(",")]
            days.append(date(year, month, day))
        return days

@click.command()
@click.argument("output", default="calendar.pdf")
@click.option("-s", "--source", type=click.Path(), default=".")
@click.option("-l", "--locale", type=click.Choice(['en', 'cs', 'it', 'sk']), default="en")
@click.option("-y", "--year", default=date.today().year+1, type=int)
@click.option("-f", "--font", type=str)
@click.option("-d", "--special-days", type=str)
@click.option("--sorted/--unsorted", default=False)
def run(output, source, locale, year, special_days=None, font=None, sorted=False):
    if sorted:
        image_source = SortedImageDirectory(source)
    else:
        image_source = UnsortedImageDirectory(source)
    locales = get_locale(locale)
    kwargs = {}
    if font:
        kwargs["title_font_name"] = font
        kwargs["cell_font_name"] = font
    if special_days:
        kwargs["special_days"] = load_special_days(special_days, year)
    calendar = YearCalendar(year, image_source, locales, **kwargs)
    calendar.render(output)

if __name__ == "__main__":
    run()<commit_msg>CLI: verbosity option<commit_after>#!/usr/bin/env python
from datetime import date
import logging

import click
from reportlab.lib import colors

from pyearcal import YearCalendar, font_loader
from pyearcal.l10n import get_locale
from pyearcal.image_sources import UnsortedImageDirectory, SortedImageDirectory

def load_special_days(path, year):
    """Load special days from external file.

    The file is expected to have lines of MM, DD format
    """
    with open(path, "r") as f:
        days = []
        for line in f:
            month, day = [int(s.strip()) for s in line.split(",")]
            days.append(date(year, month, day))
        return days

@click.command()
@click.argument("output", default="calendar.pdf")
@click.option("-s", "--source", type=click.Path(), default=".")
@click.option("-l", "--locale", type=click.Choice(['en', 'cs', 'it', 'sk']), default="en")
@click.option("-y", "--year", default=date.today().year+1, type=int)
@click.option("-f", "--font", type=str)
@click.option("-d", "--special-days", type=str)
@click.option("--sorted/--unsorted", default=False)
@click.option("-v", "--verbose", count=True)
def run(output, source, locale, year, special_days=None, font=None, sorted=False, verbose=0):
    if verbose:
        if verbose == 1:
            logging.basicConfig(level=logging.INFO)
        if verbose == 2:
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.warn("Invalid verbosity level (available: 0..2)")

    if sorted:
        image_source = SortedImageDirectory(source)
    else:
        image_source = UnsortedImageDirectory(source)
    locales = get_locale(locale)
    kwargs = {}
    if font:
        kwargs["title_font_name"] = font
        kwargs["cell_font_name"] = font
    if special_days:
        kwargs["special_days"] = load_special_days(special_days, year)
    calendar = YearCalendar(year, image_source, locales, **kwargs)
    calendar.render(output)

if __name__ == "__main__":
    run()<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Ministerio de Fomento
#                    Instituto de Ciencias de la Construcción Eduardo Torroja (IETcc-CSIC)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import numpy as np

def verInd(EP, ver=True, texto=None):

    total = EP.EP['ren'] + EP.EP['nren']
    salida = []
    if texto:
        salida.append(texto + '\n')
    salida.append('EP           | EPpasoA\n')
    salida.append('ren ='+ str(round(EP.EP['ren'],1)).rjust(8)      +\
                '| ren ='+ str(round(EP.EPpasoA['ren'],1)).rjust(8) + '\n')
    salida.append('nren='+ str(round(EP.EP['nren'],1)).rjust(8)     +\
                '| nren='+ str(round(EP.EPpasoA['nren'],1)).rjust(8) +'\n')
    salida.append('tot ='+ str(round(total,1)).rjust(8)             +\
                '|\n')
    salida.append('RER ='+ str(round((EP.EP['ren']/total),2)).rjust(8) +\
                '|\n')
    #salida.append('----------------------------------------')
    if ver:
        print ''.join(salida)

    return salida

def ver_balance(balance):
    #import balance
    print '___balance___'
    for vector, datos_periodo in balance.items():
        print 'vector: ',vector
        for periodo, datos_fuentes in datos_periodo.items():
            print '  periodo:', periodo
            for fuente, usos in datos_fuentes.items():
                print '    fuente', fuente
                for uso, valor in usos.items():
                    print '      uso', uso, redondeo(valor)
    print '--------------'

def ver_datos_entrada(vector_data):
    print '___ vector data ____'
    for vector, balance_vector in vector_data.items():
        print 'vector: ',vector#, '\tclaves', balance_vector.keys()
        for fuente, usos in balance_vector['anual'].items():
            print '  fuente: ', fuente
            for uso, valor in usos.items():
                print '    uso', uso, redondeo(valor)
    print '--------------'

def redondeo(objeto, n=0):
    if (isinstance(objeto, list) or isinstance(objeto, np.ndarray)):
        if n == 0:
            return int(round(sum(objeto),n)),[int(round(e,n)) for e in objeto]
        else:
            return round(sum(objeto),n),[round(e,n) for e in objeto]
    elif isinstance(objeto, dict):
        salida = ['\n']
        for clave, valor in objeto.items():
            if isinstance(valor, float):
                salida.append('%s %.2f\n' % (clave, valor))
        return ''.join(salida)
            #print type(valor)
    elif isinstance(objeto, float):
        return round(objeto,n)
    else:
        print objeto
        return type(objeto),'no reconozco este tipo en la función redondeo'

<commit_msg>pyepbd: improve vista format to ease refactoring<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Ministerio de Fomento
#                    Instituto de Ciencias de la Construcción Eduardo Torroja (IETcc-CSIC)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import numpy as np

def verInd(EP, texto=None):

    total = EP.EP['ren'] + EP.EP['nren']
    salida = [texto + '\n'] if texto else []
    salida.append('EP           | EPpasoA\n')
    salida.append('ren =' + str(round(EP.EP['ren'], 1)).rjust(8) +
                  '| ren =' + str(round(EP.EPpasoA['ren'], 1)).rjust(8) + '\n')
    salida.append('nren=' + str(round(EP.EP['nren'], 1)).rjust(8) +
                  '| nren=' + str(round(EP.EPpasoA['nren'], 1)).rjust(8) +'\n')
    salida.append('tot =' + str(round(total, 1)).rjust(8) + '|\n')
    salida.append('RER =' + str(round((EP.EP['ren'] / total), 2)).rjust(8) + '|\n')
    print ''.join(salida)

    return salida

def ver_balance(balance):
    #import balance
    print '___balance___'
    for vector, datos_periodo in balance.items():
        print 'vector: ',vector
        for periodo, datos_fuentes in datos_periodo.items():
            print '  periodo:', periodo
            for fuente, usos in datos_fuentes.items():
                print '    fuente', fuente
                for uso, valor in usos.items():
                    print '      uso', uso, redondeo(valor)
    print '--------------'

def ver_datos_entrada(vector_data):
    print '___ vector data ____'
    for vector, balance_vector in vector_data.items():
        print 'vector: ',vector#, '\tclaves', balance_vector.keys()
        for fuente, usos in balance_vector['anual'].items():
            print '  fuente: ', fuente
            for uso, valor in usos.items():
                print '    uso', uso, redondeo(valor)
    print '--------------'

def redondeo(objeto, n=0):
    if (isinstance(objeto, list) or isinstance(objeto, np.ndarray)):
        if n == 0:
            return int(round(sum(objeto),n)),[int(round(e,n)) for e in objeto]
        else:
            return round(sum(objeto),n),[round(e,n) for e in objeto]
    elif isinstance(objeto, dict):
        salida = ['\n']
        for clave, valor in objeto.items():
            if isinstance(valor, float):
                salida.append('%s %.2f\n' % (clave, valor))
        return ''.join(salida)
            #print type(valor)
    elif isinstance(objeto, float):
        return round(objeto,n)
    else:
        print objeto
        return type(objeto),'no reconozco este tipo en la función redondeo'

<|endoftext|>


<commit_before>import random
import re
from datetime import datetime

from zope.interface import implements

from hamper.interfaces import Plugin

class Friendly(Plugin):
    """Be polite. When people say hello, response."""

    name = 'friendly'
    priority = 2

    def setup(self, factory):
        self.greetings = ['hi', 'hello', 'hey', 'sup', 'yo', 'hola']

    def process(self, bot, comm):
        if not comm['directed']:
            return

        if comm['message'].strip() in self.greetings:
            bot.reply(comm, '{0} {1[user]}'
                .format(random.choice(self.greetings), comm))
            return True

class OmgPonies(Plugin):
    """The classics never die."""

    name = 'ponies'
    priority = 3

    cooldown = 30 #seconds

    def setup(self, factory):
        self.last_pony_time = datetime.now()

    def process(self, bot, comm):
        if re.match(r'.*pon(y|ies).*', comm['message'], re.I):
            now = datetime.now()
            since_last = now - self.last_pony_time
            since_last = since_last.seconds + 24*3600*since_last.days

            if since_last >= self.cooldown:
                bot.reply(comm, 'OMG!!! PONIES!!!')
                self.last_pony_time = now
            else:
                print('too many ponies')

        # Always let the other plugins run
        return False

class BotSnack(Plugin):
    """Reward a good bot."""

    name = 'botsnack'
    priority = 0

    def setup(self, factory):
        self.rewards = {
            'botsnack': ['yummy', 'my favorite!'],
            'goodhamper': ['^_^', ':D'],
        }

    def process(self, bot, comm):
        slug = comm['message'].lower().replace(' ', '')
        for k, v in self.rewards.items():
            if k in slug:
                bot.reply(comm, random.choice(v))
                return True

        return False

friendly = Friendly()
omgponies = OmgPonies()
botsnack = BotSnack()
<commit_msg>Make botsnack directed only.<commit_after>import random
import re
from datetime import datetime

from zope.interface import implements

from hamper.interfaces import Plugin

class Friendly(Plugin):
    """Be polite. When people say hello, response."""

    name = 'friendly'
    priority = 2

    def setup(self, factory):
        self.greetings = ['hi', 'hello', 'hey', 'sup', 'yo', 'hola']

    def process(self, bot, comm):
        if not comm['directed']:
            return

        if comm['message'].strip() in self.greetings:
            bot.reply(comm, '{0} {1[user]}'
                .format(random.choice(self.greetings), comm))
            return True

class OmgPonies(Plugin):
    """The classics never die."""

    name = 'ponies'
    priority = 3

    cooldown = 30 #seconds

    def setup(self, factory):
        self.last_pony_time = datetime.now()

    def process(self, bot, comm):
        if re.match(r'.*pon(y|ies).*', comm['message'], re.I):
            now = datetime.now()
            since_last = now - self.last_pony_time
            since_last = since_last.seconds + 24*3600*since_last.days

            if since_last >= self.cooldown:
                bot.reply(comm, 'OMG!!! PONIES!!!')
                self.last_pony_time = now
            else:
                print('too many ponies')

        # Always let the other plugins run
        return False

class BotSnack(Plugin):
    """Reward a good bot."""

    name = 'botsnack'
    priority = 0

    def setup(self, factory):
        self.rewards = {
            'botsnack': ['yummy', 'my favorite!'],
            'goodhamper': ['^_^', ':D'],
        }

    def process(self, bot, comm):
        if not comm['directed']:
            return
        slug = comm['message'].lower().replace(' ', '')
        for k, v in self.rewards.items():
            if k in slug:
                bot.reply(comm, random.choice(v))
                return True

        return False

friendly = Friendly()
omgponies = OmgPonies()
botsnack = BotSnack()
<|endoftext|>


<commit_before>"""
Prints output with the main information.
"""
import numpy

# yapf: disable
def print_summary(surf_array, field_array, param, results_dict):
    """
    Prints a summary with the main information of the run.

    Arguments
    ----------
    surf_array : array, contains the surface classes of each region on the
                        surface.
    field_array: array, contains the Field classes of each region on the surface.
    param      : class, parameters related to the surface.
    """

    Nsurf = len(surf_array)
    print(28 * '-' + '\n')
    print('{} surfaces:\n'.format(Nsurf))
    for i in range(len(surf_array)):
        N_aux = len(surf_array[i].triangle)
        rr = numpy.zeros(len(surf_array[i].tree))
        for ii in range(len(surf_array[i].tree)):
            rr[ii] = surf_array[i].tree[ii].r
        Levels = int(numpy.log(surf_array[i].tree[0].r/numpy.min(rr))/numpy.log(2) + 1)

        print('Surface {}:'.format(i))
        print('\t{} elements'.format(N_aux))
        print('\tSurface type         : {}'.format(surf_array[i].surf_type))
        print('\tCells                : {}'.format(len(surf_array[i].tree)))
        print('\tTwigs                : {}'.format(len(surf_array[i].twig)))
        print('\tLevels               : {}'.format(Levels))
        print('\tC0 size              : {}'.format(surf_array[i].tree[0].r))
        print('\tC0 box center        : {}, {}, {}'.format(surf_array[i].tree[0].xc,
                                                         surf_array[i].tree[0].yc,
                                                         surf_array[i].tree[0].zc))
        print('\tTwig cell size       : {}'.format(numpy.min(rr)))
        print('\tRbox/theta           : {}'.format(numpy.min(rr)/param.theta))
        print('\tAnalytic distance    : {}'.format(numpy.average(numpy.sqrt(2*surf_array[i].area))/param.threshold))
        print('\tElem. per Ang^2\n\t(when lspr, per nm^2): {}'.format(1/numpy.average(surf_array[i].area)))
        print('\tMax, min, avg elem.  : {}, {}, {}'.format(numpy.max(surf_array[i].area),
                                                         numpy.min(surf_array[i].area),
                                                         numpy.average(surf_array[i].area)))
        print('\tTotal area           : {}'.format(numpy.sum(surf_array[i].area)))

    print(30*'-'+'\n')

    Nfield = len(field_array)
    print('{} regions:\n'.format(Nfield))
    for i in range(len(field_array)):
        print('Region {}:'.format(i))
        print('\tLaplace or Yukawa: {}'.format(field_array[i].LorY))
        print('\tkappa            : {}'.format(field_array[i].kappa))
        print('\tdielectric const : {}'.format(field_array[i].E))
        print('\tNumber of charges: {}'.format(len(field_array[i].q)))
        print('\tParent surface   : {}'.format(field_array[i].parent))
        print('\tChild surfaces   : {}'.format(field_array[i].child))

    print(30*'-'+'\n')

    print('Parameters:')
    print('\tData type               : {}'.format(param.REAL))
    print('\tUse GPU                 : {}'.format(param.GPU))
    print('\tP                       : {}'.format(param.P))
    print('\tthreshold               : {:.2f}'.format(param.threshold))
    print('\ttheta                   : {:.2f}'.format(param.theta))
    print('\tNCRIT                   : {}'.format(param.NCRIT))
    print('\tCUDA block size         : {}'.format(param.BSZ))
    print('\tGauss points per element: {}'.format(param.K))
    print('\tGauss points near singlr: {}'.format(param.K_fine))
    print('\t1D Gauss points per side: {}'.format(param.Nk))
    print('\tGMRES tolerance         : {}'.format(param.tol))
    print('\tGMRES max iterations    : {}'.format(param.max_iter))
    print('\tGMRES restart iteration : {}'.format(param.restart))

    print(28*'-'+'\n')
    try:
        key = 'elem_sq_ang/nm_surf{}'.format(i)
        results_dict[key] = [1/numpy.average(surf_array[i].area)]
    except IndexError:
        pass

    return results_dict
# yapf: enable
<commit_msg>Fix indentation<commit_after>"""
Prints output with the main information.
"""
import numpy

# yapf: disable
def print_summary(surf_array, field_array, param, results_dict):
    """
    Prints a summary with the main information of the run.

    Arguments
    ----------
    surf_array : array, contains the surface classes of each region on the
                        surface.
    field_array: array, contains the Field classes of each region on the surface.
    param      : class, parameters related to the surface.
    """

    Nsurf = len(surf_array)
    print(28 * '-' + '\n')
    print('{} surfaces:\n'.format(Nsurf))
    for i in range(len(surf_array)):
        N_aux = len(surf_array[i].triangle)
        rr = numpy.zeros(len(surf_array[i].tree))
        for ii in range(len(surf_array[i].tree)):
            rr[ii] = surf_array[i].tree[ii].r
        Levels = int(numpy.log(surf_array[i].tree[0].r/numpy.min(rr))/numpy.log(2) + 1)

        print('Surface {}:'.format(i))
        print('\t{} elements'.format(N_aux))
        print('\tSurface type         : {}'.format(surf_array[i].surf_type))
        print('\tCells                : {}'.format(len(surf_array[i].tree)))
        print('\tTwigs                : {}'.format(len(surf_array[i].twig)))
        print('\tLevels               : {}'.format(Levels))
        print('\tC0 size              : {}'.format(surf_array[i].tree[0].r))
        print('\tC0 box center        : {}, {}, {}'.format(surf_array[i].tree[0].xc,
                                                         surf_array[i].tree[0].yc,
                                                         surf_array[i].tree[0].zc))
        print('\tTwig cell size       : {}'.format(numpy.min(rr)))
        print('\tRbox/theta           : {}'.format(numpy.min(rr)/param.theta))
        print('\tAnalytic distance    : {}'.format(numpy.average(numpy.sqrt(2*surf_array[i].area))/param.threshold))
        print('\tElem. per Ang^2\n\t(when lspr, per nm^2): {}'.format(1/numpy.average(surf_array[i].area)))
        print('\tMax, min, avg elem.  : {}, {}, {}'.format(numpy.max(surf_array[i].area),
                                                         numpy.min(surf_array[i].area),
                                                         numpy.average(surf_array[i].area)))
        print('\tTotal area           : {}'.format(numpy.sum(surf_array[i].area)))

    print(30*'-'+'\n')

    Nfield = len(field_array)
    print('{} regions:\n'.format(Nfield))
    for i in range(len(field_array)):
        print('Region {}:'.format(i))
        print('\tLaplace or Yukawa: {}'.format(field_array[i].LorY))
        print('\tkappa            : {}'.format(field_array[i].kappa))
        print('\tdielectric const : {}'.format(field_array[i].E))
        print('\tNumber of charges: {}'.format(len(field_array[i].q)))
        print('\tParent surface   : {}'.format(field_array[i].parent))
        print('\tChild surfaces   : {}'.format(field_array[i].child))

    print(30*'-'+'\n')

    print('Parameters:')
    print('\tData type               : {}'.format(param.REAL))
    print('\tUse GPU                 : {}'.format(param.GPU))
    print('\tP                       : {}'.format(param.P))
    print('\tthreshold               : {:.2f}'.format(param.threshold))
    print('\ttheta                   : {:.2f}'.format(param.theta))
    print('\tNCRIT                   : {}'.format(param.NCRIT))
    print('\tCUDA block size         : {}'.format(param.BSZ))
    print('\tGauss points per element: {}'.format(param.K))
    print('\tGauss points near singlr: {}'.format(param.K_fine))
    print('\t1D Gauss points per side: {}'.format(param.Nk))
    print('\tGMRES tolerance         : {}'.format(param.tol))
    print('\tGMRES max iterations    : {}'.format(param.max_iter))
    print('\tGMRES restart iteration : {}'.format(param.restart))

    print(28*'-'+'\n')
        try:
            key = 'elem_sq_ang_surf{}'.format(i)
            results_dict[key] = [1/numpy.average(surf_array[i].area)]
        except IndexError:
            pass

    return results_dict
# yapf: enable
<|endoftext|>