<commit_before>"""
:author: Damian Eads, 2009
:license: modified BSD
"""

import numpy as np
from scipy import ndimage
from skimage import draw

def square(width, dtype=np.uint8):
    """Generates a flat, square-shaped structuring element.

    Every pixel along the perimeter has a chessboard distance
    no greater than radius (radius=floor(width/2)) pixels.

    Parameters
    ----------
    width : int
        The width and height of the square.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        A structuring element consisting only of ones, i.e. every
        pixel belongs to the neighborhood.

    """
    return np.ones((width, width), dtype=dtype)

def rectangle(width, height, dtype=np.uint8):
    """Generates a flat, rectangular-shaped structuring element.

    Every pixel in the rectangle generated for a given width and given height
    belongs to the neighboorhood.

    Parameters
    ----------
    width : int
        The width of the rectangle.
    height : int
        The height of the rectangle.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        A structuring element consisting only of ones, i.e. every
        pixel belongs to the neighborhood.

    """
    return np.ones((width, height), dtype=dtype)

def diamond(radius, dtype=np.uint8):
    """Generates a flat, diamond-shaped structuring element.

    A pixel is part of the neighborhood (i.e. labeled 1) if
    the city block/manhattan distance between it and the center of
    the neighborhood is no greater than radius.

    Parameters
    ----------
    radius : int
        The radius of the diamond-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------

    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.
    """
    L = np.arange(0, radius * 2 + 1)
    I, J = np.meshgrid(L, L)
    return np.array(np.abs(I - radius) + np.abs(J - radius) <= radius,
                    dtype=dtype)

def disk(radius, dtype=np.uint8):
    """Generates a flat, disk-shaped structuring element.

    A pixel is within the neighborhood if the euclidean distance between
    it and the origin is no greater than radius.

    Parameters
    ----------
    radius : int
        The radius of the disk-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.
    """
    L = np.arange(-radius, radius + 1)
    X, Y = np.meshgrid(L, L)
    return np.array((X ** 2 + Y ** 2) <= radius ** 2, dtype=dtype)

def ellipse(radius, height, dtype=np.uint8):
    """Generates a flat, ellipse-shaped structuring element.

    Every pixel along the perimeter of ellipse satisfies
    the equation ``(x/radius+1)**2 + (y/height+1)**2 = 1``.

    Parameters
    ----------
    radius : int
        The radius of the ellipse-shaped structuring element.
    height : int
        The height of the ellipse-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.

    Examples
    --------
    >>> from skimage.morphology import selem
    >>> selem.ellipse(5, 3)
    array([[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0]], dtype=uint8)

    """

    selem = np.zeros((2 * height + 1, 2 * radius + 1), dtype=dtype)
    rows, cols = draw.ellipse(height, radius, height + 1, radius + 1)
    selem[rows, cols] = 1
    return selem

def cube(width, dtype=np.uint8):
    """ Generates a cube-shaped structuring element.

    This is the 3D equivalent of a square.
    Every pixel along the perimeter has a chessboard distance
    no greater than radius (radius=floor(width/2)) pixels.

    Parameters
    ----------
    width : int
        The width, height and depth of the cube.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        A structuring element consisting only of ones, i.e. every
        pixel belongs to the neighborhood.

    """
    return np.ones((width, width, width), dtype=dtype)

def octahedron(radius, dtype=np.uint8):
    """Generates a octahedron-shaped structuring element.

    This is the 3D equivalent of a diamond.
    A pixel is part of the neighborhood (i.e. labeled 1) if
    the city block/manhattan distance between it and the center of
    the neighborhood is no greater than radius.

    Parameters
    ----------
    radius : int
        The radius of the octahedron-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------

    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.
    """
    # note that in contrast to diamond(), this method allows non-integer radii
    n = 2 * radius + 1
    Z, Y, X = np.mgrid[-radius:radius:n * 1j,
                       -radius:radius:n * 1j,
                       -radius:radius:n * 1j]
    s = np.abs(X) + np.abs(Y) + np.abs(Z)
    return np.array(s <= radius, dtype=dtype)

def ball(radius, dtype=np.uint8):
    """Generates a ball-shaped structuring element.

    This is the 3D equivalent of a disk.
    A pixel is within the neighborhood if the euclidean distance between
    it and the origin is no greater than radius.

    Parameters
    ----------
    radius : int
        The radius of the ball-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.
    """
    n = 2 * radius + 1
    Z, Y, X = np.mgrid[-radius:radius:n * 1j,
                       -radius:radius:n * 1j,
                       -radius:radius:n * 1j]
    s = X ** 2 + Y ** 2 + Z ** 2
    return np.array(s <= radius * radius, dtype=dtype)

def octagon(m, n, dtype=np.uint8):
    """Generates an octagon shaped structuring element.

    For a given size of (m) horizontal and vertical sides
    and a given (n) height or width of slanted sides octagon is generated.
    The slanted sides are 45 or 135 degrees to the horizontal axis
    and hence the widths and heights are equal.

    Parameters
    ----------
    m : int
        The size of the horizontal and vertical sides.
    n : int
        The height or width of the slanted sides.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.

    """
    from . import convex_hull_image
    selem = np.zeros((m + 2 * n, m + 2 * n))
    selem[0, n] = 1
    selem[n, 0] = 1
    selem[0, m + n - 1] = 1
    selem[m + n - 1, 0] = 1
    selem[-1, n] = 1
    selem[n, -1] = 1
    selem[-1, m + n - 1] = 1
    selem[m + n - 1, -1] = 1
    selem = convex_hull_image(selem).astype(dtype)
    return selem

def star(a, dtype=np.uint8):
    """Generates a star shaped structuring element.

    Start has 8 vertices and is an overlap of square of size `2*a + 1`
    with its 45 degree rotated version.
    The slanted sides are 45 or 135 degrees to the horizontal axis.

    Parameters
    ----------
    a : int
        Parameter deciding the size of the star structural element. The side
        of the square array returned is `2*a + 1 + 2*floor(a / 2)`.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.

    """
    from . import convex_hull_image

    if a == 1:
        bfilter = np.zeros((3, 3), dtype)
        bfilter[:] = 1
        return bfilter

    m = 2 * a + 1
    n = a // 2
    selem_square = np.zeros((m + 2 * n, m + 2 * n))
    selem_square[n: m + n, n: m + n] = 1

    c = (m + 2 * n - 1) // 2
    selem_rotated = np.zeros((m + 2 * n, m + 2 * n))
    selem_rotated[0, c] = selem_rotated[-1, c] = 1
    selem_rotated[c, 0] = selem_rotated[c, -1] = 1
    selem_rotated = convex_hull_image(selem_rotated).astype(int)

    selem = selem_square + selem_rotated
    selem[selem > 0] = 1

    return selem.astype(dtype)

def _default_selem(ndim):
    """Generates a cross-shaped structuring element (connectivity=1).

    This is the default structuring element (selem) if no selem was specified.

    Parameters
    ----------
    ndim : int
        Number of dimensions of the image.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.

    """
    return ndimage.morphology.generate_binary_structure(ndim, 1)
<commit_msg>DOC: Change selem.ellipse params<commit_after>"""
:author: Damian Eads, 2009
:license: modified BSD
"""

import numpy as np
from scipy import ndimage
from skimage import draw

def square(width, dtype=np.uint8):
    """Generates a flat, square-shaped structuring element.

    Every pixel along the perimeter has a chessboard distance
    no greater than radius (radius=floor(width/2)) pixels.

    Parameters
    ----------
    width : int
        The width and height of the square.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        A structuring element consisting only of ones, i.e. every
        pixel belongs to the neighborhood.

    """
    return np.ones((width, width), dtype=dtype)

def rectangle(width, height, dtype=np.uint8):
    """Generates a flat, rectangular-shaped structuring element.

    Every pixel in the rectangle generated for a given width and given height
    belongs to the neighboorhood.

    Parameters
    ----------
    width : int
        The width of the rectangle.
    height : int
        The height of the rectangle.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        A structuring element consisting only of ones, i.e. every
        pixel belongs to the neighborhood.

    """
    return np.ones((width, height), dtype=dtype)

def diamond(radius, dtype=np.uint8):
    """Generates a flat, diamond-shaped structuring element.

    A pixel is part of the neighborhood (i.e. labeled 1) if
    the city block/manhattan distance between it and the center of
    the neighborhood is no greater than radius.

    Parameters
    ----------
    radius : int
        The radius of the diamond-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------

    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.
    """
    L = np.arange(0, radius * 2 + 1)
    I, J = np.meshgrid(L, L)
    return np.array(np.abs(I - radius) + np.abs(J - radius) <= radius,
                    dtype=dtype)

def disk(radius, dtype=np.uint8):
    """Generates a flat, disk-shaped structuring element.

    A pixel is within the neighborhood if the euclidean distance between
    it and the origin is no greater than radius.

    Parameters
    ----------
    radius : int
        The radius of the disk-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.
    """
    L = np.arange(-radius, radius + 1)
    X, Y = np.meshgrid(L, L)
    return np.array((X ** 2 + Y ** 2) <= radius ** 2, dtype=dtype)

def ellipse(width, height, dtype=np.uint8):
    """Generates a flat, ellipse-shaped structuring element.

    Every pixel along the perimeter of ellipse satisfies
    the equation ``(x/width+1)**2 + (y/height+1)**2 = 1``.

    Parameters
    ----------
    width : int
        The width of the ellipse-shaped structuring element.
    height : int
        The height of the ellipse-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.

    Examples
    --------
    >>> from skimage.morphology import selem
    >>> selem.ellipse(5, 3)
    array([[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0]], dtype=uint8)

    """
    selem = np.zeros((2 * height + 1, 2 * width + 1), dtype=dtype)
    rows, cols = draw.ellipse(height, width, height + 1, width + 1)
    selem[rows, cols] = 1
    return selem

def cube(width, dtype=np.uint8):
    """ Generates a cube-shaped structuring element.

    This is the 3D equivalent of a square.
    Every pixel along the perimeter has a chessboard distance
    no greater than radius (radius=floor(width/2)) pixels.

    Parameters
    ----------
    width : int
        The width, height and depth of the cube.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        A structuring element consisting only of ones, i.e. every
        pixel belongs to the neighborhood.

    """
    return np.ones((width, width, width), dtype=dtype)

def octahedron(radius, dtype=np.uint8):
    """Generates a octahedron-shaped structuring element.

    This is the 3D equivalent of a diamond.
    A pixel is part of the neighborhood (i.e. labeled 1) if
    the city block/manhattan distance between it and the center of
    the neighborhood is no greater than radius.

    Parameters
    ----------
    radius : int
        The radius of the octahedron-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------

    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.
    """
    # note that in contrast to diamond(), this method allows non-integer radii
    n = 2 * radius + 1
    Z, Y, X = np.mgrid[-radius:radius:n * 1j,
                       -radius:radius:n * 1j,
                       -radius:radius:n * 1j]
    s = np.abs(X) + np.abs(Y) + np.abs(Z)
    return np.array(s <= radius, dtype=dtype)

def ball(radius, dtype=np.uint8):
    """Generates a ball-shaped structuring element.

    This is the 3D equivalent of a disk.
    A pixel is within the neighborhood if the euclidean distance between
    it and the origin is no greater than radius.

    Parameters
    ----------
    radius : int
        The radius of the ball-shaped structuring element.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.
    """
    n = 2 * radius + 1
    Z, Y, X = np.mgrid[-radius:radius:n * 1j,
                       -radius:radius:n * 1j,
                       -radius:radius:n * 1j]
    s = X ** 2 + Y ** 2 + Z ** 2
    return np.array(s <= radius * radius, dtype=dtype)

def octagon(m, n, dtype=np.uint8):
    """Generates an octagon shaped structuring element.

    For a given size of (m) horizontal and vertical sides
    and a given (n) height or width of slanted sides octagon is generated.
    The slanted sides are 45 or 135 degrees to the horizontal axis
    and hence the widths and heights are equal.

    Parameters
    ----------
    m : int
        The size of the horizontal and vertical sides.
    n : int
        The height or width of the slanted sides.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.

    """
    from . import convex_hull_image
    selem = np.zeros((m + 2 * n, m + 2 * n))
    selem[0, n] = 1
    selem[n, 0] = 1
    selem[0, m + n - 1] = 1
    selem[m + n - 1, 0] = 1
    selem[-1, n] = 1
    selem[n, -1] = 1
    selem[-1, m + n - 1] = 1
    selem[m + n - 1, -1] = 1
    selem = convex_hull_image(selem).astype(dtype)
    return selem

def star(a, dtype=np.uint8):
    """Generates a star shaped structuring element.

    Start has 8 vertices and is an overlap of square of size `2*a + 1`
    with its 45 degree rotated version.
    The slanted sides are 45 or 135 degrees to the horizontal axis.

    Parameters
    ----------
    a : int
        Parameter deciding the size of the star structural element. The side
        of the square array returned is `2*a + 1 + 2*floor(a / 2)`.

    Other Parameters
    ----------------
    dtype : data-type
        The data type of the structuring element.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.

    """
    from . import convex_hull_image

    if a == 1:
        bfilter = np.zeros((3, 3), dtype)
        bfilter[:] = 1
        return bfilter

    m = 2 * a + 1
    n = a // 2
    selem_square = np.zeros((m + 2 * n, m + 2 * n))
    selem_square[n: m + n, n: m + n] = 1

    c = (m + 2 * n - 1) // 2
    selem_rotated = np.zeros((m + 2 * n, m + 2 * n))
    selem_rotated[0, c] = selem_rotated[-1, c] = 1
    selem_rotated[c, 0] = selem_rotated[c, -1] = 1
    selem_rotated = convex_hull_image(selem_rotated).astype(int)

    selem = selem_square + selem_rotated
    selem[selem > 0] = 1

    return selem.astype(dtype)

def _default_selem(ndim):
    """Generates a cross-shaped structuring element (connectivity=1).

    This is the default structuring element (selem) if no selem was specified.

    Parameters
    ----------
    ndim : int
        Number of dimensions of the image.

    Returns
    -------
    selem : ndarray
        The structuring element where elements of the neighborhood
        are 1 and 0 otherwise.

    """
    return ndimage.morphology.generate_binary_structure(ndim, 1)
<|endoftext|>


<commit_before>"""Utilities for input validation"""
# Authors: Olivier Grisel and Gael Varoquaux and others (please update me)
# License: BSD 3 clause

import warnings
import numbers

import numpy as np
from scipy import sparse

from ..externals import six
from .fixes import safe_copy

class DataConversionWarning(UserWarning):
    "A warning on implicit data conversions happening in the code"
    pass

warnings.simplefilter("always", DataConversionWarning)

class NonBLASDotWarning(UserWarning):
    "A warning on implicit dispatch to numpy.dot"
    pass

# Silenced by default to reduce verbosity. Turn on at runtime for
# performance profiling.
warnings.simplefilter('ignore', NonBLASDotWarning)

def _assert_all_finite(X):
    """Like assert_all_finite, but only for ndarray."""
    if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum())
            and not np.isfinite(X).all()):
        raise ValueError("Array contains NaN or infinity.")

def assert_all_finite(X):
    """Throw a ValueError if X contains NaN or infinity.

    Input MUST be an np.ndarray instance or a scipy.sparse matrix."""

    # First try an O(n) time, O(1) space solution for the common case that
    # there everything is finite; fall back to O(n) space np.isfinite to
    # prevent false positives from overflow in sum method.
    _assert_all_finite(X.data if sparse.issparse(X) else X)

def safe_asarray(X, dtype=None, order=None, copy=False):
    """Convert X to an array or sparse matrix.

    Prevents copying X when possible; sparse matrices are passed through."""
    if sparse.issparse(X):
        if copy:
            X = X.copy()
        assert_all_finite(X.data)
        # enforces dtype on data array (order should be kept the same).
        X.data = np.asarray(X.data, dtype=dtype)
    else:
        X = np.array(X, dtype=dtype, order=order, copy=copy)
        assert_all_finite(X)
    return X

def as_float_array(X, copy=True):
    """Converts an array-like to an array of floats

    The new dtype will be np.float32 or np.float64, depending on the original
    type. The function can create a copy or modify the argument depending
    on the argument copy.

    Parameters
    ----------
    X : {array-like, sparse matrix}

    copy : bool, optional
        If True, a copy of X will be created. If False, a copy may still be
        returned if X's dtype is not a floating point type.

    Returns
    -------
    XT : {array, sparse matrix}
        An array of type np.float
    """
    if isinstance(X, np.matrix) or (not isinstance(X, np.ndarray)
                                    and not sparse.issparse(X)):
        return safe_asarray(X, dtype=np.float64, copy=copy)
    elif sparse.issparse(X) and X.dtype in [np.float32, np.float64]:
        return X.copy() if copy else X
    elif X.dtype in [np.float32, np.float64]:  # is numpy array
        return X.copy('F' if X.flags['F_CONTIGUOUS'] else 'C') if copy else X
    else:
        return X.astype(np.float32 if X.dtype == np.int32 else np.float64)

def array2d(X, dtype=None, order=None, copy=False, force_all_finite=True):
    """Returns at least 2-d array with data from X"""
    if sparse.issparse(X):
        raise TypeError('A sparse matrix was passed, but dense data '
                        'is required. Use X.toarray() to convert to dense.')
    X_2d = np.asarray(np.atleast_2d(X), dtype=dtype, order=order)
    if force_all_finite:
        _assert_all_finite(X_2d)
    if X is X_2d and copy:
        X_2d = safe_copy(X_2d)
    return X_2d

def _atleast2d_or_sparse(X, dtype, order, copy, sparse_class, convmethod,
                         force_all_finite):
    if sparse.issparse(X):
        if dtype is None or X.dtype == dtype:
            X = getattr(X, convmethod)()
        else:
            X = sparse_class(X, dtype=dtype)
        if force_all_finite:
            _assert_all_finite(X.data)
        X.data = np.array(X.data, copy=False, order=order)
    else:
        X = array2d(X, dtype=dtype, order=order, copy=copy,
                    force_all_finite=force_all_finite)
        if force_all_finite:
            _assert_all_finite(X)
    return X

def atleast2d_or_csc(X, dtype=None, order=None, copy=False,
                     force_all_finite=True):
    """Like numpy.atleast_2d, but converts sparse matrices to CSC format.

    Also, converts np.matrix to np.ndarray.
    """
    return _atleast2d_or_sparse(X, dtype, order, copy, sparse.csc_matrix,
                                "tocsc", force_all_finite)

def atleast2d_or_csr(X, dtype=None, order=None, copy=False,
                     force_all_finite=True):
    """Like numpy.atleast_2d, but converts sparse matrices to CSR format

    Also, converts np.matrix to np.ndarray.
    """
    return _atleast2d_or_sparse(X, dtype, order, copy, sparse.csr_matrix,
                                "tocsr", force_all_finite)

def _num_samples(x):
    """Return number of samples in array-like x."""
    if not hasattr(x, '__len__') and not hasattr(x, 'shape'):
        raise TypeError("Expected sequence or array-like, got %r" % x)
    return x.shape[0] if hasattr(x, 'shape') else len(x)

def check_arrays(*arrays, **options):
    """Check that all arrays have consistent first dimensions.

    Checks whether all objects in arrays have the same shape or length.
    By default lists and tuples are converted to numpy arrays.

    It is possible to enforce certain properties, such as dtype, continguity
    and sparse matrix format (if a sparse matrix is passed).

    Converting lists to arrays can be disabled by setting ``allow_lists=True``.
    Lists can then contain arbitrary objects and are not checked for dtype,
    finiteness or anything else but length. Arrays are still checked
    and possibly converted.

    Parameters
    ----------
    *arrays : sequence of arrays or scipy.sparse matrices with same shape[0]
        Python lists or tuples occurring in arrays are converted to 1D numpy
        arrays, unless allow_lists is specified.

    sparse_format : 'csr', 'csc' or 'dense', None by default
        If not None, any scipy.sparse matrix is converted to
        Compressed Sparse Rows or Compressed Sparse Columns representations.
        If 'dense', an error is raised when a sparse array is
        passed.

    copy : boolean, False by default
        If copy is True, ensure that returned arrays are copies of the original
        (if not already converted to another format earlier in the process).

    check_ccontiguous : boolean, False by default
        Check that the arrays are C contiguous

    dtype : a numpy dtype instance, None by default
        Enforce a specific dtype.

    allow_lists : bool
        Allow lists of arbitrary objects as input, just check their length.
        Disables
    """
    sparse_format = options.pop('sparse_format', None)
    if sparse_format not in (None, 'csr', 'csc', 'dense'):
        raise ValueError('Unexpected sparse format: %r' % sparse_format)
    copy = options.pop('copy', False)
    check_ccontiguous = options.pop('check_ccontiguous', False)
    dtype = options.pop('dtype', None)
    allow_lists = options.pop('allow_lists', False)
    if options:
        raise TypeError("Unexpected keyword arguments: %r" % options.keys())

    if len(arrays) == 0:
        return None

    n_samples = _num_samples(arrays[0])

    checked_arrays = []
    for array in arrays:
        array_orig = array
        if array is None:
            # special case: ignore optional y=None kwarg pattern
            checked_arrays.append(array)
            continue
        size = _num_samples(array)

        if size != n_samples:
            raise ValueError("Found array with dim %d. Expected %d"
                             % (size, n_samples))

        if not allow_lists or hasattr(array, "shape"):
            if sparse.issparse(array):
                if sparse_format == 'csr':
                    array = array.tocsr()
                elif sparse_format == 'csc':
                    array = array.tocsc()
                elif sparse_format == 'dense':
                    raise TypeError('A sparse matrix was passed, but dense '
                                    'data is required. Use X.toarray() to '
                                    'convert to a dense numpy array.')
                if check_ccontiguous:
                    array.data = np.ascontiguousarray(array.data, dtype=dtype)
                else:
                    array.data = np.asarray(array.data, dtype=dtype)
                _assert_all_finite(array.data)
            else:
                if check_ccontiguous:
                    array = np.ascontiguousarray(array, dtype=dtype)
                else:
                    array = np.asarray(array, dtype=dtype)
                _assert_all_finite(array)

        if copy and array is array_orig:
            array = array.copy()
        checked_arrays.append(array)

    return checked_arrays

def column_or_1d(y, warn=False):
    """ Ravel column or 1d numpy array, else raises an error

    Parameters
    ----------
    y : array-like

    Returns
    -------
    y : array

    """
    shape = np.shape(y)
    if len(shape) == 1:
        return np.ravel(y)
    if len(shape) == 2 and shape[1] == 1:
        if warn:
            warnings.warn("A column-vector y was passed when a 1d array was"
                          " expected. Please change the shape of y to "
                          "(n_samples, ), for example using ravel().",
                          DataConversionWarning, stacklevel=2)
        return np.ravel(y)

    raise ValueError("bad input shape {0}".format(shape))

def warn_if_not_float(X, estimator='This algorithm'):
    """Warning utility function to check that data type is floating point.

    Returns True if a warning was raised (i.e. the input is not float) and
    False otherwise, for easier input validation.
    """
    if not isinstance(estimator, six.string_types):
        estimator = estimator.__class__.__name__
    if X.dtype.kind != 'f':
        warnings.warn("%s assumes floating point values as input, "
                      "got %s" % (estimator, X.dtype))
        return True
    return False

def check_random_state(seed):
    """Turn seed into a np.random.RandomState instance

    If seed is None, return the RandomState singleton used by np.random.
    If seed is an int, return a new RandomState instance seeded with seed.
    If seed is already a RandomState instance, return it.
    Otherwise raise ValueError.
    """
    if seed is None or seed is np.random:
        return np.random.mtrand._rand
    if isinstance(seed, (numbers.Integral, np.integer)):
        return np.random.RandomState(seed)
    if isinstance(seed, np.random.RandomState):
        return seed
    raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
                     ' instance' % seed)
<commit_msg>BUG duplicate finity check in input validation<commit_after>"""Utilities for input validation"""
# Authors: Olivier Grisel and Gael Varoquaux and others (please update me)
# License: BSD 3 clause

import warnings
import numbers

import numpy as np
from scipy import sparse

from ..externals import six
from .fixes import safe_copy

class DataConversionWarning(UserWarning):
    "A warning on implicit data conversions happening in the code"
    pass

warnings.simplefilter("always", DataConversionWarning)

class NonBLASDotWarning(UserWarning):
    "A warning on implicit dispatch to numpy.dot"
    pass

# Silenced by default to reduce verbosity. Turn on at runtime for
# performance profiling.
warnings.simplefilter('ignore', NonBLASDotWarning)

def _assert_all_finite(X):
    """Like assert_all_finite, but only for ndarray."""
    if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum())
            and not np.isfinite(X).all()):
        raise ValueError("Array contains NaN or infinity.")

def assert_all_finite(X):
    """Throw a ValueError if X contains NaN or infinity.

    Input MUST be an np.ndarray instance or a scipy.sparse matrix."""

    # First try an O(n) time, O(1) space solution for the common case that
    # there everything is finite; fall back to O(n) space np.isfinite to
    # prevent false positives from overflow in sum method.
    _assert_all_finite(X.data if sparse.issparse(X) else X)

def safe_asarray(X, dtype=None, order=None, copy=False):
    """Convert X to an array or sparse matrix.

    Prevents copying X when possible; sparse matrices are passed through."""
    if sparse.issparse(X):
        if copy:
            X = X.copy()
        assert_all_finite(X.data)
        # enforces dtype on data array (order should be kept the same).
        X.data = np.asarray(X.data, dtype=dtype)
    else:
        X = np.array(X, dtype=dtype, order=order, copy=copy)
        assert_all_finite(X)
    return X

def as_float_array(X, copy=True):
    """Converts an array-like to an array of floats

    The new dtype will be np.float32 or np.float64, depending on the original
    type. The function can create a copy or modify the argument depending
    on the argument copy.

    Parameters
    ----------
    X : {array-like, sparse matrix}

    copy : bool, optional
        If True, a copy of X will be created. If False, a copy may still be
        returned if X's dtype is not a floating point type.

    Returns
    -------
    XT : {array, sparse matrix}
        An array of type np.float
    """
    if isinstance(X, np.matrix) or (not isinstance(X, np.ndarray)
                                    and not sparse.issparse(X)):
        return safe_asarray(X, dtype=np.float64, copy=copy)
    elif sparse.issparse(X) and X.dtype in [np.float32, np.float64]:
        return X.copy() if copy else X
    elif X.dtype in [np.float32, np.float64]:  # is numpy array
        return X.copy('F' if X.flags['F_CONTIGUOUS'] else 'C') if copy else X
    else:
        return X.astype(np.float32 if X.dtype == np.int32 else np.float64)

def array2d(X, dtype=None, order=None, copy=False, force_all_finite=True):
    """Returns at least 2-d array with data from X"""
    if sparse.issparse(X):
        raise TypeError('A sparse matrix was passed, but dense data '
                        'is required. Use X.toarray() to convert to dense.')
    X_2d = np.asarray(np.atleast_2d(X), dtype=dtype, order=order)
    if force_all_finite:
        _assert_all_finite(X_2d)
    if X is X_2d and copy:
        X_2d = safe_copy(X_2d)
    return X_2d

def _atleast2d_or_sparse(X, dtype, order, copy, sparse_class, convmethod,
                         force_all_finite):
    if sparse.issparse(X):
        if dtype is None or X.dtype == dtype:
            X = getattr(X, convmethod)()
        else:
            X = sparse_class(X, dtype=dtype)
        if force_all_finite:
            _assert_all_finite(X.data)
        X.data = np.array(X.data, copy=False, order=order)
    else:
        X = array2d(X, dtype=dtype, order=order, copy=copy,
                    force_all_finite=force_all_finite)
    return X

def atleast2d_or_csc(X, dtype=None, order=None, copy=False,
                     force_all_finite=True):
    """Like numpy.atleast_2d, but converts sparse matrices to CSC format.

    Also, converts np.matrix to np.ndarray.
    """
    return _atleast2d_or_sparse(X, dtype, order, copy, sparse.csc_matrix,
                                "tocsc", force_all_finite)

def atleast2d_or_csr(X, dtype=None, order=None, copy=False,
                     force_all_finite=True):
    """Like numpy.atleast_2d, but converts sparse matrices to CSR format

    Also, converts np.matrix to np.ndarray.
    """
    return _atleast2d_or_sparse(X, dtype, order, copy, sparse.csr_matrix,
                                "tocsr", force_all_finite)

def _num_samples(x):
    """Return number of samples in array-like x."""
    if not hasattr(x, '__len__') and not hasattr(x, 'shape'):
        raise TypeError("Expected sequence or array-like, got %r" % x)
    return x.shape[0] if hasattr(x, 'shape') else len(x)

def check_arrays(*arrays, **options):
    """Check that all arrays have consistent first dimensions.

    Checks whether all objects in arrays have the same shape or length.
    By default lists and tuples are converted to numpy arrays.

    It is possible to enforce certain properties, such as dtype, continguity
    and sparse matrix format (if a sparse matrix is passed).

    Converting lists to arrays can be disabled by setting ``allow_lists=True``.
    Lists can then contain arbitrary objects and are not checked for dtype,
    finiteness or anything else but length. Arrays are still checked
    and possibly converted.

    Parameters
    ----------
    *arrays : sequence of arrays or scipy.sparse matrices with same shape[0]
        Python lists or tuples occurring in arrays are converted to 1D numpy
        arrays, unless allow_lists is specified.

    sparse_format : 'csr', 'csc' or 'dense', None by default
        If not None, any scipy.sparse matrix is converted to
        Compressed Sparse Rows or Compressed Sparse Columns representations.
        If 'dense', an error is raised when a sparse array is
        passed.

    copy : boolean, False by default
        If copy is True, ensure that returned arrays are copies of the original
        (if not already converted to another format earlier in the process).

    check_ccontiguous : boolean, False by default
        Check that the arrays are C contiguous

    dtype : a numpy dtype instance, None by default
        Enforce a specific dtype.

    allow_lists : bool
        Allow lists of arbitrary objects as input, just check their length.
        Disables
    """
    sparse_format = options.pop('sparse_format', None)
    if sparse_format not in (None, 'csr', 'csc', 'dense'):
        raise ValueError('Unexpected sparse format: %r' % sparse_format)
    copy = options.pop('copy', False)
    check_ccontiguous = options.pop('check_ccontiguous', False)
    dtype = options.pop('dtype', None)
    allow_lists = options.pop('allow_lists', False)
    if options:
        raise TypeError("Unexpected keyword arguments: %r" % options.keys())

    if len(arrays) == 0:
        return None

    n_samples = _num_samples(arrays[0])

    checked_arrays = []
    for array in arrays:
        array_orig = array
        if array is None:
            # special case: ignore optional y=None kwarg pattern
            checked_arrays.append(array)
            continue
        size = _num_samples(array)

        if size != n_samples:
            raise ValueError("Found array with dim %d. Expected %d"
                             % (size, n_samples))

        if not allow_lists or hasattr(array, "shape"):
            if sparse.issparse(array):
                if sparse_format == 'csr':
                    array = array.tocsr()
                elif sparse_format == 'csc':
                    array = array.tocsc()
                elif sparse_format == 'dense':
                    raise TypeError('A sparse matrix was passed, but dense '
                                    'data is required. Use X.toarray() to '
                                    'convert to a dense numpy array.')
                if check_ccontiguous:
                    array.data = np.ascontiguousarray(array.data, dtype=dtype)
                else:
                    array.data = np.asarray(array.data, dtype=dtype)
                _assert_all_finite(array.data)
            else:
                if check_ccontiguous:
                    array = np.ascontiguousarray(array, dtype=dtype)
                else:
                    array = np.asarray(array, dtype=dtype)
                _assert_all_finite(array)

        if copy and array is array_orig:
            array = array.copy()
        checked_arrays.append(array)

    return checked_arrays

def column_or_1d(y, warn=False):
    """ Ravel column or 1d numpy array, else raises an error

    Parameters
    ----------
    y : array-like

    Returns
    -------
    y : array

    """
    shape = np.shape(y)
    if len(shape) == 1:
        return np.ravel(y)
    if len(shape) == 2 and shape[1] == 1:
        if warn:
            warnings.warn("A column-vector y was passed when a 1d array was"
                          " expected. Please change the shape of y to "
                          "(n_samples, ), for example using ravel().",
                          DataConversionWarning, stacklevel=2)
        return np.ravel(y)

    raise ValueError("bad input shape {0}".format(shape))

def warn_if_not_float(X, estimator='This algorithm'):
    """Warning utility function to check that data type is floating point.

    Returns True if a warning was raised (i.e. the input is not float) and
    False otherwise, for easier input validation.
    """
    if not isinstance(estimator, six.string_types):
        estimator = estimator.__class__.__name__
    if X.dtype.kind != 'f':
        warnings.warn("%s assumes floating point values as input, "
                      "got %s" % (estimator, X.dtype))
        return True
    return False

def check_random_state(seed):
    """Turn seed into a np.random.RandomState instance

    If seed is None, return the RandomState singleton used by np.random.
    If seed is an int, return a new RandomState instance seeded with seed.
    If seed is already a RandomState instance, return it.
    Otherwise raise ValueError.
    """
    if seed is None or seed is np.random:
        return np.random.mtrand._rand
    if isinstance(seed, (numbers.Integral, np.integer)):
        return np.random.RandomState(seed)
    if isinstance(seed, np.random.RandomState):
        return seed
    raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
                     ' instance' % seed)
<|endoftext|>


<commit_before>"""Function to keep a maxmind database file up to date"""

from shutil import move
import os
import hashlib
import sys
import tarfile

import requests

__version__ = '0.1.0'
__url__ = 'https://github.com/yola/maxmind-updater'

UPDATE_URL = 'https://download.maxmind.com/app/geoip_download'

def hash_file(filename):
    if not os.path.exists(filename):
        return ''

    block_size = 65536
    hasher = hashlib.md5()

    with open(filename, 'rb') as f:
        buf = f.read(block_size)
        while len(buf) > 0:
            hasher.update(buf)
            buf = f.read(block_size)
    return hasher.hexdigest()

def update_db(curr_db, license_key):
    curr_db_path = os.path.dirname(curr_db)

    r = requests.get(UPDATE_URL,
                     params={'license_key': license_key,
                             'edition_id': 'GeoIP2-Country',
                             'suffix': 'tar.gz.md5',
                             })

    curr_md5 = hash_file('%s.tar.gz' % curr_db)
    if r.content == curr_md5 and os.path.exists(curr_db):
        return

    r = requests.get(UPDATE_URL, stream=True,
                     params={'license_key': license_key,
                             'edition_id': 'GeoIP2-Country',
                             'suffix': 'tar.gz',
                             })

    with open('%s.tar.gz' % curr_db, 'wb') as local_zip:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:  # filter out keep-alive new chunks
                local_zip.write(chunk)

    with tarfile.open('%s.tar.gz' % curr_db) as tar_file:
        # We only want the mmdb file. Maxmind kindly includes things
        # we don't want.
        extract_members = [member for member in tar_file.getmembers()
                           if member.name.endswith('.mmdb')]
        tar_file.extractall(path=curr_db_path, members=extract_members)
        # extractall keeps the subfolder structure. Account for this by
        # appending the path to the curr_db_path where it was extracted.
        new_db = os.path.join(curr_db_path, extract_members[0].path)
    try:
        pass
        # TODO
        # test_ip('8.8.8.8', new_db)
        # test_ip('2001:420::', new_db)
    except Exception, e:
        # pyGeoIP could break in a variety of ways - we don't
        # particularly care which ones.
        sys.stderr.write('Retrieved invalid GeoIP database - '
                         'check MaxMind account details: %s' % e)
    else:
        if not os.path.exists(os.path.dirname(curr_db)):
            os.makedirs(os.path.dirname(curr_db))
        move(new_db, curr_db)
        os.rmdir(os.path.dirname(new_db))
<commit_msg>Parametrise edition_id<commit_after>"""Function to keep a maxmind database file up to date"""

from shutil import move
import os
import hashlib
import sys
import tarfile

import requests

__version__ = '0.1.0'
__url__ = 'https://github.com/yola/maxmind-updater'

UPDATE_URL = 'https://download.maxmind.com/app/geoip_download'

def hash_file(filename):
    if not os.path.exists(filename):
        return ''

    block_size = 65536
    hasher = hashlib.md5()

    with open(filename, 'rb') as f:
        buf = f.read(block_size)
        while len(buf) > 0:
            hasher.update(buf)
            buf = f.read(block_size)
    return hasher.hexdigest()

def update_db(curr_db, license_key, edition_id):
    curr_db_path = os.path.dirname(curr_db)

    r = requests.get(UPDATE_URL,
                     params={'license_key': license_key,
                             'edition_id': edition_id,
                             'suffix': 'tar.gz.md5',
                             })

    curr_md5 = hash_file('%s.tar.gz' % curr_db)
    if r.content == curr_md5 and os.path.exists(curr_db):
        return

    r = requests.get(UPDATE_URL, stream=True,
                     params={'license_key': license_key,
                             'edition_id': edition_id,
                             'suffix': 'tar.gz',
                             })

    with open('%s.tar.gz' % curr_db, 'wb') as local_zip:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:  # filter out keep-alive new chunks
                local_zip.write(chunk)

    with tarfile.open('%s.tar.gz' % curr_db) as tar_file:
        # We only want the mmdb file. Maxmind kindly includes things
        # we don't want.
        extract_members = [member for member in tar_file.getmembers()
                           if member.name.endswith('.mmdb')]
        tar_file.extractall(path=curr_db_path, members=extract_members)
        # extractall keeps the subfolder structure. Account for this by
        # appending the path to the curr_db_path where it was extracted.
        new_db = os.path.join(curr_db_path, extract_members[0].path)
    try:
        pass
        # TODO
        # test_ip('8.8.8.8', new_db)
        # test_ip('2001:420::', new_db)
    except Exception, e:
        # pyGeoIP could break in a variety of ways - we don't
        # particularly care which ones.
        sys.stderr.write('Retrieved invalid GeoIP database - '
                         'check MaxMind account details: %s' % e)
    else:
        if not os.path.exists(os.path.dirname(curr_db)):
            os.makedirs(os.path.dirname(curr_db))
        move(new_db, curr_db)
        os.rmdir(os.path.dirname(new_db))
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    Sample Server

    This module starts the main TCP listener for XMPP clients

    :copyright: 2011 by the pyfire Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""

import sys
import os.path
# Add pyfire to namespace
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(path)

import errno
import functools
import contextlib
import socket
import thread

from zmq.eventloop import ioloop
from tornado import iostream
from tornado.stack_context import StackContext

from pyfire import configuration as config
from pyfire import zmq_forwarder, stanza_processor
from pyfire.auth.backends import DummyTrueValidator
from pyfire.server import XMPPServer, XMPPConnection
from pyfire.singletons import get_validation_registry

def start_client_listener():
    validation_registry = get_validation_registry()
    validator = DummyTrueValidator()
    validation_registry.register('dummy', validator)

    io_loop = ioloop.IOLoop.instance()
    server = XMPPServer(io_loop)
    server.bind(config.get('listeners', 'clientport'),
                config.get('listeners', 'ip'))
    server.start()
    try:
        io_loop.start()
    except (KeyboardInterrupt, SystemExit):
        io_loop.stop()
        print "exited cleanly"

def fire_up():
    # create a forwader/router for internal communication
    fwd = zmq_forwarder.ZMQForwarder()
    thread.start_new_thread(fwd.start, ())

    # create a stamza processor for local domains
    stanza_proc = stanza_processor.StanzaProcessor(config.get('listeners', 'domains'))
    thread.start_new_thread(stanza_proc.start, ())

    # start listener for incomming Connections
    start_client_listener()

if __name__ == '__main__':
    fire_up()
<commit_msg>Init Stanza publisher on startup because of zmq needing some time to start sockets<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    Sample Server

    This module starts the main TCP listener for XMPP clients

    :copyright: 2011 by the pyfire Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""

import sys
import os.path
# Add pyfire to namespace
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(path)

import errno
import functools
import contextlib
import socket
import thread

from zmq.eventloop import ioloop
from tornado import iostream
from tornado.stack_context import StackContext

from pyfire import configuration as config
from pyfire import zmq_forwarder, stanza_processor
from pyfire.auth.backends import DummyTrueValidator
from pyfire.server import XMPPServer, XMPPConnection
from pyfire.singletons import get_validation_registry, get_publisher

def start_client_listener():
    publisher = get_publisher()
    validation_registry = get_validation_registry()
    validator = DummyTrueValidator()
    validation_registry.register('dummy', validator)

    io_loop = ioloop.IOLoop.instance()
    server = XMPPServer(io_loop)
    server.bind(config.get('listeners', 'clientport'),
                config.get('listeners', 'ip'))
    server.start()
    try:
        io_loop.start()
    except (KeyboardInterrupt, SystemExit):
        io_loop.stop()
        print "exited cleanly"

def fire_up():
    # create a forwader/router for internal communication
    fwd = zmq_forwarder.ZMQForwarder()
    thread.start_new_thread(fwd.start, ())

    # create a stamza processor for local domains
    stanza_proc = stanza_processor.StanzaProcessor(config.get('listeners', 'domains'))
    thread.start_new_thread(stanza_proc.start, ())

    # start listener for incomming Connections
    start_client_listener()

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


<commit_before># -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import netsvc
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_SERIALIZABLE
from psycopg2.pool import ThreadedConnectionPool
from psycopg2.psycopg1 import cursor as psycopg1cursor

import psycopg2.extensions

psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)

types_mapping = {
    'date': (1082,),
    'time': (1083,),
    'datetime': (1114,),
}

def unbuffer(symb, cr):
    if symb is None: return None
    return str(symb)

def undecimalize(symb, cr):
    if symb is None: return None
    return float(symb)

for name, typeoid in types_mapping.items():
    psycopg2.extensions.register_type(psycopg2.extensions.new_type(typeoid, name, lambda x, cr: x))
psycopg2.extensions.register_type(psycopg2.extensions.new_type((700, 701, 1700,), 'float', undecimalize))

import tools
import re

from mx import DateTime as mdt
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$');

def log(msg, lvl=netsvc.LOG_DEBUG):
    logger = netsvc.Logger()
    logger.notifyChannel('sql', lvl, msg)

class Cursor(object):
    IN_MAX = 1000
    sql_from_log = {}
    sql_into_log = {}
    sql_log = False
    count = 0

    def check(f):
        from tools.func import wraps

        @wraps(f)
        def wrapper(self, *args, **kwargs):
            if not hasattr(self, '_obj'):
                raise psycopg2.ProgrammingError('Unable to use the cursor after having closing it')
            return f(self, *args, **kwargs)
        return wrapper

    def __init__(self, pool):
        self._pool = pool
        self._cnx = pool.getconn()
        self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
        self.autocommit(False)
        self.dbname = pool.dbname

    def __del__(self):
        if hasattr(self, '_obj'):
            # Oops. 'self' has not been closed explicitly.
            # The cursor will be deleted by the garbage collector,
            # but the database connection is not put back into the connection
            # pool, preventing some operation on the database like dropping it.
            # This can also lead to a server overload.
            log('Cursor not closed explicitly', netsvc.LOG_WARNING)
            self.close()

    @check
    def execute(self, query, params=None):
        if params is None:
            params=()
        if not isinstance(params, (tuple, list)):
            params = (params,)

        def base_string(s):
            if isinstance(s, unicode):
                return s.encode('utf-8')
            return s
        p=map(base_string, params)
        query = base_string(query)

        if '%d' in query or '%f' in query:
            #import traceback
            #traceback.print_stack()
            log(query, netsvc.LOG_WARNING)
            log("SQL queries mustn't containt %d or %f anymore. Use only %s", netsvc.LOG_WARNING)
            if p:
                query = query.replace('%d', '%s').replace('%f', '%s')

        if self.sql_log:
            now = mdt.now()

        res = self._obj.execute(query, p or None)

        if self.sql_log:
            log("query: %s" % self._obj.query)
            self.count+=1
            res_from = re_from.match(query.lower())
            if res_from:
                self.sql_from_log.setdefault(res_from.group(1), [0, 0])
                self.sql_from_log[res_from.group(1)][0] += 1
                self.sql_from_log[res_from.group(1)][1] += mdt.now() - now
            res_into = re_into.match(query.lower())
            if res_into:
                self.sql_into_log.setdefault(res_into.group(1), [0, 0])
                self.sql_into_log[res_into.group(1)][0] += 1
                self.sql_into_log[res_into.group(1)][1] += mdt.now() - now
        return res

    def print_log(self):
        def process(type):
            sqllogs = {'from':self.sql_from_log, 'into':self.sql_into_log}
            if not sqllogs[type]:
                return
            sqllogitems = sqllogs[type].items()
            sqllogitems.sort(key=lambda k: k[1][1])
            sum = 0
            log("SQL LOG %s:" % (type,))
            for r in sqllogitems:
                log("table: %s: %s/%s" %(r[0], str(r[1][1]), r[1][0]))
                sum+= r[1][1]
            log("SUM:%s/%d" % (sum, self.count))
            sqllogs[type].clear()
        process('from')
        process('into')
        self.count = 0

    @check
    def close(self):
        self.print_log()
        self._obj.close()

        # This force the cursor to be freed, and thus, available again. It is
        # important because otherwise we can overload the server very easily
        # because of a cursor shortage (because cursors are not garbage
        # collected as fast as they should). The problem is probably due in
        # part because browse records keep a reference to the cursor.
        del self._obj
        self._pool.putconn(self._cnx)

    @check
    def autocommit(self, on):
        self._cnx.set_isolation_level([ISOLATION_LEVEL_SERIALIZABLE, ISOLATION_LEVEL_AUTOCOMMIT][bool(on)])

    @check
    def commit(self):
        return self._cnx.commit()

    @check
    def rollback(self):
        return self._cnx.rollback()

    @check
    def __getattr__(self, name):
        return getattr(self._obj, name)

class ConnectionPool(object):
    def __init__(self, pool, dbname):
        self.dbname = dbname
        self._pool = pool

    def cursor(self):
        return Cursor(self)

    def __getattr__(self, name):
        return getattr(self._pool, name)

class PoolManager(object):
    _pools = {}
    _dsn = None
    maxconn =  int(tools.config['db_maxconn']) or 64

    def dsn(db_name):
        if PoolManager._dsn is None:
            PoolManager._dsn = ''
            for p in ('host', 'port', 'user', 'password'):
                cfg = tools.config['db_' + p]
                if cfg:
                    PoolManager._dsn += '%s=%s ' % (p, cfg)
        return '%s dbname=%s' % (PoolManager._dsn, db_name)
    dsn = staticmethod(dsn)

    def get(db_name):
        if db_name not in PoolManager._pools:
            logger = netsvc.Logger()
            try:
                logger.notifyChannel('dbpool', netsvc.LOG_INFO, 'Connecting to %s' % (db_name,))
                PoolManager._pools[db_name] = ConnectionPool(ThreadedConnectionPool(1, PoolManager.maxconn, PoolManager.dsn(db_name)), db_name)
            except Exception, e:
                logger.notifyChannel('dbpool', netsvc.LOG_CRITICAL, 'Unable to connect to %s: %s' % (db_name, e.message))
                raise
        return PoolManager._pools[db_name]
    get = staticmethod(get)

    def close(db_name):
        if db_name in PoolManager._pools:
            logger.notifyChannel('dbpool', netsvc.LOG_INFO, 'Closing all connections to %s' % (db_name,))
            PoolManager._pools[db_name].closeall()
            del PoolManager._pools[db_name]
    close = staticmethod(close)

def db_connect(db_name, serialize=0):
    return PoolManager.get(db_name)

def close_db(db_name):
    return PoolManager.close(db_name)

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

<commit_msg>[IMP] downgrade the logging level to ERROR when the server cannot connect to the database<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import netsvc
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_SERIALIZABLE
from psycopg2.pool import ThreadedConnectionPool
from psycopg2.psycopg1 import cursor as psycopg1cursor

import psycopg2.extensions

psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)

types_mapping = {
    'date': (1082,),
    'time': (1083,),
    'datetime': (1114,),
}

def unbuffer(symb, cr):
    if symb is None: return None
    return str(symb)

def undecimalize(symb, cr):
    if symb is None: return None
    return float(symb)

for name, typeoid in types_mapping.items():
    psycopg2.extensions.register_type(psycopg2.extensions.new_type(typeoid, name, lambda x, cr: x))
psycopg2.extensions.register_type(psycopg2.extensions.new_type((700, 701, 1700,), 'float', undecimalize))

import tools
import re

from mx import DateTime as mdt
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$');

def log(msg, lvl=netsvc.LOG_DEBUG):
    logger = netsvc.Logger()
    logger.notifyChannel('sql', lvl, msg)

class Cursor(object):
    IN_MAX = 1000
    sql_from_log = {}
    sql_into_log = {}
    sql_log = False
    count = 0

    def check(f):
        from tools.func import wraps

        @wraps(f)
        def wrapper(self, *args, **kwargs):
            if not hasattr(self, '_obj'):
                raise psycopg2.ProgrammingError('Unable to use the cursor after having closing it')
            return f(self, *args, **kwargs)
        return wrapper

    def __init__(self, pool):
        self._pool = pool
        self._cnx = pool.getconn()
        self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
        self.autocommit(False)
        self.dbname = pool.dbname

    def __del__(self):
        if hasattr(self, '_obj'):
            # Oops. 'self' has not been closed explicitly.
            # The cursor will be deleted by the garbage collector,
            # but the database connection is not put back into the connection
            # pool, preventing some operation on the database like dropping it.
            # This can also lead to a server overload.
            log('Cursor not closed explicitly', netsvc.LOG_WARNING)
            self.close()

    @check
    def execute(self, query, params=None):
        if params is None:
            params=()
        if not isinstance(params, (tuple, list)):
            params = (params,)

        def base_string(s):
            if isinstance(s, unicode):
                return s.encode('utf-8')
            return s
        p=map(base_string, params)
        query = base_string(query)

        if '%d' in query or '%f' in query:
            #import traceback
            #traceback.print_stack()
            log(query, netsvc.LOG_WARNING)
            log("SQL queries mustn't containt %d or %f anymore. Use only %s", netsvc.LOG_WARNING)
            if p:
                query = query.replace('%d', '%s').replace('%f', '%s')

        if self.sql_log:
            now = mdt.now()

        res = self._obj.execute(query, p or None)

        if self.sql_log:
            log("query: %s" % self._obj.query)
            self.count+=1
            res_from = re_from.match(query.lower())
            if res_from:
                self.sql_from_log.setdefault(res_from.group(1), [0, 0])
                self.sql_from_log[res_from.group(1)][0] += 1
                self.sql_from_log[res_from.group(1)][1] += mdt.now() - now
            res_into = re_into.match(query.lower())
            if res_into:
                self.sql_into_log.setdefault(res_into.group(1), [0, 0])
                self.sql_into_log[res_into.group(1)][0] += 1
                self.sql_into_log[res_into.group(1)][1] += mdt.now() - now
        return res

    def print_log(self):
        def process(type):
            sqllogs = {'from':self.sql_from_log, 'into':self.sql_into_log}
            if not sqllogs[type]:
                return
            sqllogitems = sqllogs[type].items()
            sqllogitems.sort(key=lambda k: k[1][1])
            sum = 0
            log("SQL LOG %s:" % (type,))
            for r in sqllogitems:
                log("table: %s: %s/%s" %(r[0], str(r[1][1]), r[1][0]))
                sum+= r[1][1]
            log("SUM:%s/%d" % (sum, self.count))
            sqllogs[type].clear()
        process('from')
        process('into')
        self.count = 0

    @check
    def close(self):
        self.print_log()
        self._obj.close()

        # This force the cursor to be freed, and thus, available again. It is
        # important because otherwise we can overload the server very easily
        # because of a cursor shortage (because cursors are not garbage
        # collected as fast as they should). The problem is probably due in
        # part because browse records keep a reference to the cursor.
        del self._obj
        self._pool.putconn(self._cnx)

    @check
    def autocommit(self, on):
        self._cnx.set_isolation_level([ISOLATION_LEVEL_SERIALIZABLE, ISOLATION_LEVEL_AUTOCOMMIT][bool(on)])

    @check
    def commit(self):
        return self._cnx.commit()

    @check
    def rollback(self):
        return self._cnx.rollback()

    @check
    def __getattr__(self, name):
        return getattr(self._obj, name)

class ConnectionPool(object):
    def __init__(self, pool, dbname):
        self.dbname = dbname
        self._pool = pool

    def cursor(self):
        return Cursor(self)

    def __getattr__(self, name):
        return getattr(self._pool, name)

class PoolManager(object):
    _pools = {}
    _dsn = None
    maxconn =  int(tools.config['db_maxconn']) or 64

    def dsn(db_name):
        if PoolManager._dsn is None:
            PoolManager._dsn = ''
            for p in ('host', 'port', 'user', 'password'):
                cfg = tools.config['db_' + p]
                if cfg:
                    PoolManager._dsn += '%s=%s ' % (p, cfg)
        return '%s dbname=%s' % (PoolManager._dsn, db_name)
    dsn = staticmethod(dsn)

    def get(db_name):
        if db_name not in PoolManager._pools:
            logger = netsvc.Logger()
            try:
                logger.notifyChannel('dbpool', netsvc.LOG_INFO, 'Connecting to %s' % (db_name,))
                PoolManager._pools[db_name] = ConnectionPool(ThreadedConnectionPool(1, PoolManager.maxconn, PoolManager.dsn(db_name)), db_name)
            except Exception, e:
                logger.notifyChannel('dbpool', netsvc.LOG_ERROR, 'Unable to connect to %s: %s' % (db_name, e.message))
                raise
        return PoolManager._pools[db_name]
    get = staticmethod(get)

    def close(db_name):
        if db_name in PoolManager._pools:
            logger.notifyChannel('dbpool', netsvc.LOG_INFO, 'Closing all connections to %s' % (db_name,))
            PoolManager._pools[db_name].closeall()
            del PoolManager._pools[db_name]
    close = staticmethod(close)

def db_connect(db_name, serialize=0):
    return PoolManager.get(db_name)

def close_db(db_name):
    return PoolManager.close(db_name)

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

<|endoftext|>


<commit_before>#ipc_lista1.11
#Professor: Jucimar Junior
#Any Mendes Carvalho
#
#
#
#
#Faça um programa que peça 2 numeros inteiros e um numero real. Calcule e mostre:
#a- o produto do dobro do primeiro com metade do segundo.
#b- a soma do triplo do primeiro com o terceiro.
#c- o terceiro elevado ao cubo.

import math

num1 = input("Digite o primeiro numero inteiro: ")
num2 = input("Digite o segundo numero inteiro: ")
numReal = input("Dgite um numero real: )
resultado1 = num1 * 2 * (num2
<commit_msg>Update ipc_lista1.11.py<commit_after>#ipc_lista1.11
#Professor: Jucimar Junior
#Any Mendes Carvalho
#
#
#
#
#Faça um programa que peça 2 numeros inteiros e um numero real. Calcule e mostre:
#a- o produto do dobro do primeiro com metade do segundo.
#b- a soma do triplo do primeiro com o terceiro.
#c- o terceiro elevado ao cubo.

import math

num1 = input("Digite o primeiro numero inteiro: ")
num2 = input("Digite o segundo numero inteiro: ")
numReal = input("Dgite um numero real: )
resultado1 = num1 * 2 * (num2
<|endoftext|>


<commit_before>from flask import Blueprint, request
from sqlalchemy import func

from skylines.api.json import jsonify
from skylines.database import db
from skylines.api.oauth import oauth
from skylines.lib.dbutil import get_requested_record
from skylines.model import Club, User
from skylines.model.event import (
    create_club_join_event
)

from skylines.schemas import ClubSchema, ValidationError

clubs_blueprint = Blueprint('clubs', 'skylines')

@clubs_blueprint.route('/clubs', strict_slashes=False)
def list():
    clubs = Club.query().order_by(func.lower(Club.name))

    name_filter = request.args.get('name')
    if name_filter:
        clubs = clubs.filter_by(name=name_filter)

    return jsonify(clubs=ClubSchema(only=('id', 'name')).dump(clubs, many=True).data)

@clubs_blueprint.route('/clubs/<club_id>', strict_slashes=False)
@oauth.optional()
def read(club_id):
    current_user = User.get(request.user_id) if request.user_id else None

    club = get_requested_record(Club, club_id)

    json = ClubSchema().dump(club).data
    json['isWritable'] = club.is_writable(current_user) or False

    return jsonify(json)

@clubs_blueprint.route('/clubs', methods=['PUT'])
@oauth.required()
def create_club():
    current_user = User.get(request.user_id)
    if not current_user:
        return jsonify(error='invalid-token'), 401

    json = request.get_json()
    if json is None:
        return jsonify(error='invalid-request'), 400

    try:
        data = ClubSchema(only=('name',)).load(json).data
    except ValidationError, e:
        return jsonify(error='validation-failed', fields=e.messages), 422

    if Club.exists(name=data.get('name')):
        return jsonify(error='duplicate-club-name'), 422

    # create the new club
    club = Club(**data)
    club.owner_id = current_user.id
    db.session.add(club)
    db.session.flush()

    # assign the user to the new club
    current_user.club = club

    # create the "user joined club" event
    create_club_join_event(club.id, current_user)

    db.session.commit()

    return jsonify(id=club.id)

@clubs_blueprint.route('/clubs/<club_id>', methods=['POST'], strict_slashes=False)
def update(club_id):
    club = get_requested_record(Club, club_id)

    json = request.get_json()
    if json is None:
        return jsonify(error='invalid-request'), 400

    try:
        data = ClubSchema(partial=True).load(json).data
    except ValidationError, e:
        return jsonify(error='validation-failed', fields=e.messages), 422

    if 'name' in data:
        name = data.get('name')

        if name != club.name and Club.exists(name=name):
            return jsonify(error='duplicate-club-name'), 422

        club.name = name

    if 'website' in data:
        club.website = data.get('website')

    db.session.commit()

    return jsonify()
<commit_msg>api/clubs: Fix authentication check<commit_after>from flask import Blueprint, request
from sqlalchemy import func

from skylines.api.json import jsonify
from skylines.database import db
from skylines.api.oauth import oauth
from skylines.lib.dbutil import get_requested_record
from skylines.model import Club, User
from skylines.model.event import (
    create_club_join_event
)

from skylines.schemas import ClubSchema, ValidationError

clubs_blueprint = Blueprint('clubs', 'skylines')

@clubs_blueprint.route('/clubs', strict_slashes=False)
def list():
    clubs = Club.query().order_by(func.lower(Club.name))

    name_filter = request.args.get('name')
    if name_filter:
        clubs = clubs.filter_by(name=name_filter)

    return jsonify(clubs=ClubSchema(only=('id', 'name')).dump(clubs, many=True).data)

@clubs_blueprint.route('/clubs/<club_id>', strict_slashes=False)
@oauth.optional()
def read(club_id):
    current_user = User.get(request.user_id) if request.user_id else None

    club = get_requested_record(Club, club_id)

    json = ClubSchema().dump(club).data
    json['isWritable'] = club.is_writable(current_user) or False

    return jsonify(json)

@clubs_blueprint.route('/clubs', methods=['PUT'])
@oauth.required()
def create_club():
    current_user = User.get(request.user_id)
    if not current_user:
        return jsonify(error='invalid-token'), 401

    json = request.get_json()
    if json is None:
        return jsonify(error='invalid-request'), 400

    try:
        data = ClubSchema(only=('name',)).load(json).data
    except ValidationError, e:
        return jsonify(error='validation-failed', fields=e.messages), 422

    if Club.exists(name=data.get('name')):
        return jsonify(error='duplicate-club-name'), 422

    # create the new club
    club = Club(**data)
    club.owner_id = current_user.id
    db.session.add(club)
    db.session.flush()

    # assign the user to the new club
    current_user.club = club

    # create the "user joined club" event
    create_club_join_event(club.id, current_user)

    db.session.commit()

    return jsonify(id=club.id)

@clubs_blueprint.route('/clubs/<club_id>', methods=['POST'], strict_slashes=False)
@oauth.required()
def update(club_id):
    current_user = User.get(request.user_id)
    if not current_user:
        return jsonify(error='invalid-token'), 401

    club = get_requested_record(Club, club_id)
    if not club.is_writable(current_user):
        return jsonify(error='forbidden'), 403

    json = request.get_json()
    if json is None:
        return jsonify(error='invalid-request'), 400

    try:
        data = ClubSchema(partial=True).load(json).data
    except ValidationError, e:
        return jsonify(error='validation-failed', fields=e.messages), 422

    if 'name' in data:
        name = data.get('name')

        if name != club.name and Club.exists(name=name):
            return jsonify(error='duplicate-club-name'), 422

        club.name = name

    if 'website' in data:
        club.website = data.get('website')

    db.session.commit()

    return jsonify()
<|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""Creates a homework configuration file, uploads files to repository
and sends the homework for evaluation"""

from __future__ import with_statement

__author__ = """Ana Savu <ana.savu86@gmail.com>
                Alexandru Moșoi <brtzsnr@gmail.com>"""

import ConfigParser
import os
import shutil
import sys
import time
from subprocess import check_call
from os.path import join, split, abspath, isfile, isdir, dirname
from tempfile import mkstemp
from zipfile import ZipFile

import misc
import vmcheckerpaths

def _call_git(repository, *args):
    return check_call(['git',
        '--git-dir=' + join(repository, '.git'),
        '--work-tree=' + repository] + list(args))

def build_config(user, assignment, archive):
    """Builds a coonfiguration file for user's assignment.

    Returns absolute path to assignment config.

    XXX Exit code of child processes is not checked.
    XXX Locks should protect the directory access"""
    assert assignment in misc.config().sections(), (
        'No such assigment `%s\'.' % assignment)
    repository = misc.repository(assignment)

    # the upload time is the system's current time
    upload_time = time.strftime(misc.DATE_FORMAT)

    location = join(repository, assignment, user)
    print >>sys.stderr, 'Storing files at `%s\'' % location

    # removes the old homework and adds the new one
    # NOTE: git is clever enough to store only diffs
    if os.path.isdir(location):
        shutil.rmtree(location)
    assert not os.path.exists(location)
    os.makedirs(location)

    check_call(['unzip', archive, '-d', os.path.join(location, 'archive')])

    # writes assignment configuration file
    assignment_config = join(location, 'config')

    with open(assignment_config, 'w') as handle:
        handle.write('[Assignment]\n')
        handle.write('User=%s\n' % user)
        handle.write('Assignment=%s\n' % assignment)
        handle.write('UploadTime=%s\n' % upload_time)

    # commits the changes
    _call_git(repository, 'add', location)
    _call_git(repository, 'clean', location, '-f', '-d')
    _call_git(repository, 'commit', '--allow-empty', location, '-m',
            'Updated assignment `%s\' from `%s\'' % (assignment, user))

    # XXX should create a clean zip from repository
    shutil.copy(archive, join(location, 'archive.zip'))

    return assignment_config

def submit_assignment(assignment_config):
    """Submits config file for evaluation.

    This function creates a zip archive, stores it in
    $VMCHECKER_ROOT/unchecked/ directory and calls submit
    script.

    The archive contains:
        config - assignment config (eg. name, time of submission etc)
        global - global assignments config (eg. deadlines)
        archive.zip - a zip containing the homework

    """
    assignment_config = abspath(assignment_config)

    # reads user, assignment and course
    config = ConfigParser.RawConfigParser()
    with open(assignment_config) as handler:
        config.readfp(handler)

    user = config.get('Assignment', 'User')
    assignment = config.get('Assignment', 'Assignment')
    course = misc.config().get(assignment, 'Course')

    archive = join(dirname(assignment_config), './archive.zip')
    tests = misc.relative_path('tests', assignment + '.zip')

    # finds location of penalty script
    penalty = misc.relative_path(misc.config().get(assignment, 'Penalty'))
    assert isfile(penalty)

    # builds archive with configuration

    # creates the zip archive with a unique name
    fd = mkstemp(
            suffix='.zip',
            prefix='%s_%s_%s_' % (course, assignment, user),
            dir=vmcheckerpaths.dir_unchecked())
    print >>sys.stderr, 'Creating zip package at `%s\'' % fd[1]

    # adds the files to
    with os.fdopen(fd[0], 'w+b') as handler:
        zip = ZipFile(handler, 'w')
        zip.write(assignment_config, 'config')             # assignment config
        zip.write(vmcheckerpaths.config_file(), 'storer')  # storer config
        zip.write(archive, 'archive.zip')                  # assignment archive
        zip.write(tests, 'tests.zip')                      # the archive containing tests
        zip.write(penalty, 'penalty')                      # penalty script
        zip.close()

    # sends homework to tester
    submit = vmcheckerpaths.abspath(misc.config().get(assignment, 'Submit'))
    logging.info('Calling submission script %s', submit)
    try:
        check_call((submit, fd[1]))
    except:
        logging.fatal('Cannot submit homework')
        raise

def print_help():
    """Prints help and exits"""
    print >>sys.stderr, 'Usage:'
    print >>sys.stderr, '\t%s user assignment archive' % sys.argv[0]
    print >>sys.stderr, '\t\tbuilds config and submits assignment for evaluation'
    print >>sys.stderr, '\t%s config' % sys.argv[0]
    print >>sys.stderr, '\t\tresubmits assignment for reevaluation'
    sys.exit(1)

def main():
    if len(sys.argv) == 1:
        print_help()

    if len(sys.argv) == 2:
        if not isfile(sys.argv[1]):
            print >>sys.stderr, '`%s\' must be an existing file.' % sys.argv[1]
            print_help()

        assignment_config = sys.argv[1]
    elif len(sys.argv) == 4:
        if not isfile(sys.argv[3]):
            print >>sys.stderr, '`%s\' must be an existing file.' % sys.argv[3]
            print_help()

        user = sys.argv[1]           # student's name
        assignment = sys.argv[2]     # assignment
        archive = sys.argv[3]        # archive
        assignment_config = build_config(user, assignment, archive)
    else:
        print_help()

    print >>sys.stderr, 'Assignment config located at `%s\'' % assignment_config
    submit_assignment(assignment_config)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main()

<commit_msg>fix remaining problems from merge<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""Creates a homework configuration file, uploads files to repository
and sends the homework for evaluation"""

from __future__ import with_statement

__author__ = """Ana Savu <ana.savu86@gmail.com>
                Alexandru Moșoi <brtzsnr@gmail.com>"""

import ConfigParser
import os
import shutil
import sys
import time
import logging
from subprocess import check_call
from os.path import join, split, abspath, isfile, isdir, dirname
from tempfile import mkstemp
from zipfile import ZipFile

import misc
import vmcheckerpaths

def _call_git(repository, *args):
    return check_call(['git',
        '--git-dir=' + join(repository, '.git'),
        '--work-tree=' + repository] + list(args))

def build_config(user, assignment, archive):
    """Builds a coonfiguration file for user's assignment.

    Returns absolute path to assignment config.

    XXX Exit code of child processes is not checked.
    XXX Locks should protect the directory access"""
    assert assignment in misc.config().sections(), (
        'No such assigment `%s\'.' % assignment)
    repository = misc.repository(assignment)

    # the upload time is the system's current time
    upload_time = time.strftime(misc.DATE_FORMAT)

    location = join(repository, assignment, user)
    print >>sys.stderr, 'Storing files at `%s\'' % location

    # removes the old homework and adds the new one
    # NOTE: git is clever enough to store only diffs
    if os.path.isdir(location):
        shutil.rmtree(location)
    assert not os.path.exists(location)
    os.makedirs(location)

    check_call(['unzip', archive, '-d', os.path.join(location, 'archive')])

    # writes assignment configuration file
    assignment_config = join(location, 'config')

    with open(assignment_config, 'w') as handle:
        handle.write('[Assignment]\n')
        handle.write('User=%s\n' % user)
        handle.write('Assignment=%s\n' % assignment)
        handle.write('UploadTime=%s\n' % upload_time)

    # commits the changes
    _call_git(repository, 'add', location)
    _call_git(repository, 'clean', location, '-f', '-d')
    _call_git(repository, 'commit', '--allow-empty', location, '-m',
            'Updated assignment `%s\' from `%s\'' % (assignment, user))

    # XXX should create a clean zip from repository
    shutil.copy(archive, join(location, 'archive.zip'))

    return assignment_config

def submit_assignment(assignment_config):
    """Submits config file for evaluation.

    This function creates a zip archive, stores it in
    $VMCHECKER_ROOT/unchecked/ directory and calls submit
    script.

    The archive contains:
        config - assignment config (eg. name, time of submission etc)
        global - global assignments config (eg. deadlines)
        archive.zip - a zip containing the homework

    """
    assignment_config = abspath(assignment_config)

    # reads user, assignment and course
    config = ConfigParser.RawConfigParser()
    with open(assignment_config) as handler:
        config.readfp(handler)

    user = config.get('Assignment', 'User')
    assignment = config.get('Assignment', 'Assignment')
    course = misc.config().get(assignment, 'Course')

    archive = join(dirname(assignment_config), './archive.zip')
    tests = misc.relative_path('tests', assignment + '.zip')

    # finds location of penalty script
    penalty = misc.relative_path(misc.config().get(assignment, 'Penalty'))
    assert isfile(penalty)

    # builds archive with configuration

    # creates the zip archive with a unique name
    fd = mkstemp(
            suffix='.zip',
            prefix='%s_%s_%s_' % (course, assignment, user),
            dir=vmcheckerpaths.dir_unchecked())
    print >>sys.stderr, 'Creating zip package at `%s\'' % fd[1]

    # adds the files to
    with os.fdopen(fd[0], 'w+b') as handler:
        zip = ZipFile(handler, 'w')
        zip.write(assignment_config, 'config')             # assignment config
        zip.write(vmcheckerpaths.config_file(), 'storer')  # storer config
        zip.write(archive, 'archive.zip')                  # assignment archive
        zip.write(tests, 'tests.zip')                      # the archive containing tests
        zip.write(penalty, 'penalty')                      # penalty script
        zip.close()

    # sends homework to tester
    submit = vmcheckerpaths.abspath(misc.config().get(assignment, 'Submit'))
    logging.info('Calling submission script %s', submit)
    try:
        check_call((submit, fd[1]))
    except:
        logging.fatal('Cannot submit homework')
        raise

def print_help():
    """Prints help and exits"""
    print >>sys.stderr, 'Usage:'
    print >>sys.stderr, '\t%s user assignment archive' % sys.argv[0]
    print >>sys.stderr, '\t\tbuilds config and submits assignment for evaluation'
    print >>sys.stderr, '\t%s config' % sys.argv[0]
    print >>sys.stderr, '\t\tresubmits assignment for reevaluation'
    sys.exit(1)

def main():
    if len(sys.argv) == 1:
        print_help()

    if len(sys.argv) == 2:
        if not isfile(sys.argv[1]):
            print >>sys.stderr, '`%s\' must be an existing file.' % sys.argv[1]
            print_help()

        assignment_config = sys.argv[1]
    elif len(sys.argv) == 4:
        if not isfile(sys.argv[3]):
            print >>sys.stderr, '`%s\' must be an existing file.' % sys.argv[3]
            print_help()

        user = sys.argv[1]           # student's name
        assignment = sys.argv[2]     # assignment
        archive = sys.argv[3]        # archive
        assignment_config = build_config(user, assignment, archive)
    else:
        print_help()

    print >>sys.stderr, 'Assignment config located at `%s\'' % assignment_config
    submit_assignment(assignment_config)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main()

<|endoftext|>


<commit_before>#ipc_lista2.05
#Professor: Jucimar Junior
#Any Mendes Carvalho - 1615310044
#
#
#
#
#Faça um programa para  a leitura de duas notas parciais de um aluno. O programa deve calcular a média alcançada por aluno e apresentar:
#--A mensagem "Aprovado", se a média alcançada for maior ou igual a sete;
#--A mensagem "Reprovado", se a média for menor que sete;
#--A mensagem "Aprovado com Distincao", se a média for
<commit_msg>Update ipc_lista2.05.py<commit_after>#ipc_lista2.05
#Professor: Jucimar Junior
#Any Mendes Carvalho - 1615310044
#
#
#
#
#Faça um programa para  a leitura de duas notas parciais de um aluno. O programa deve calcular a média alcançada por aluno e apresentar:
#--A mensagem "Aprovado", se a média alcançada for maior ou igual a sete;
#--A mensagem "Reprovado", se a média for menor que sete;
#--A mensagem "Aprovado com Distincao", se a média for igual a dez.
<|endoftext|>


<commit_before>from unittest.case import TestCase

class Nó:
    def __init__(self):
        self.valor = None
        self.proximo_noh = None

class ListaLigadaSimples:
    def __init__(self):
        self.inicio = None  # aponta para o Nó inicial

def adicionar(lista, valor):
    nó = Nó()
    nó.valor = valor
    lista.inicio = nó

class TestesDeAdicaoEmListaSimples(TestCase):
    def testar_inicializar(self):
        lista = ListaLigadaSimples()
        self.assertIsNone(lista.inicio)

    def testar_adiciao_de_um_elemento(self):
        lista = ListaLigadaSimples()
        adicionar(lista, 'A')
        noh_inicial = lista.inicio
        self.assertIsNotNone(noh_inicial)
        self.assertEqual('A', noh_inicial.valor)
<commit_msg>Teste para adição de segundo nó e remoção de acentos<commit_after>from unittest.case import TestCase

class Noh:
    def __init__(self):
        self.valor = None
        self.proximo_noh = None

class ListaLigadaSimples:
    def __init__(self):
        self.inicio = None  # aponta para o Noh inicial

def adicionar(lista, valor):
    noh = Noh()
    noh.valor = valor
    lista.inicio = noh

class TestesDeAdicaoEmListaSimples(TestCase):
    def testar_inicializar(self):
        lista = ListaLigadaSimples()
        self.assertIsNone(lista.inicio)

    def testar_adição_de_um_elemento(self):
        lista = ListaLigadaSimples()
        adicionar(lista, 'A')
        noh_inicial = lista.inicio
        self.assertIsNotNone(noh_inicial)
        self.assertEqual('A', noh_inicial.valor)

    def testar_adição_de_dois_elementos(self):
        lista = ListaLigadaSimples()
        adicionar(lista, 'A')
        noh_inicial = lista.inicio
        adicionar(lista, 'B')
        noh_final = noh_inicial.proximo_noh
        self.assertIsNotNone(noh_final)
        self.assertEqual('B', noh_final.valor)
        self.assertIsNone(noh_final.proximo_noh)
<|endoftext|>