<commit_before>"""Holds various Version strings of MPF.

This modules holds the MPF version strings, including the version of BCP it
needs and the config file version it needs.

It's used internally for all sorts of things, from printing the output of the
`mpf --version` command, to making sure any processes connected via BCP are
the proper versions, to automatically triggering new builds and deployments to
PyPI.

"""

__version__ = '0.50.0-dev.89'
'''The full version of MPF.'''

__short_version__ = '0.50'
'''The major.minor version of MPF.'''

__bcp_version__ = '1.1'
'''The version of BCP this build of MPF uses.'''

__config_version__ = '5'
'''The config file version this build of MPF uses.'''

__show_version__ = '5'
'''The show format version this build of MPF uses.'''

version = "MPF v{}".format(__version__)
'''A friendly version string for this build of MPF.'''

extended_version = "MPF v{}, Config version:{}, Show version: {}, " \
                   "BCP version:{}".format(__version__, __config_version__,
                                           __show_version__, __bcp_version__)
'''An extended version string that includes the MPF version, show version,
and BCP versions used in this build of MPF.'''

__api__ = ['version',
           '__short_version__',
           '__bcp_version__',
           '__config_version__',
           '__show_version__',
           'version',
           'extended_version']
<commit_msg>dev-90<commit_after>"""Holds various Version strings of MPF.

This modules holds the MPF version strings, including the version of BCP it
needs and the config file version it needs.

It's used internally for all sorts of things, from printing the output of the
`mpf --version` command, to making sure any processes connected via BCP are
the proper versions, to automatically triggering new builds and deployments to
PyPI.

"""

__version__ = '0.50.0-dev.90'
'''The full version of MPF.'''

__short_version__ = '0.50'
'''The major.minor version of MPF.'''

__bcp_version__ = '1.1'
'''The version of BCP this build of MPF uses.'''

__config_version__ = '5'
'''The config file version this build of MPF uses.'''

__show_version__ = '5'
'''The show format version this build of MPF uses.'''

version = "MPF v{}".format(__version__)
'''A friendly version string for this build of MPF.'''

extended_version = "MPF v{}, Config version:{}, Show version: {}, " \
                   "BCP version:{}".format(__version__, __config_version__,
                                           __show_version__, __bcp_version__)
'''An extended version string that includes the MPF version, show version,
and BCP versions used in this build of MPF.'''

__api__ = ['version',
           '__short_version__',
           '__bcp_version__',
           '__config_version__',
           '__show_version__',
           'version',
           'extended_version']
<|endoftext|>


<commit_before># Crop position mask

import cv2
import numpy as np
import math
from . import print_image
from . import plot_image
from . import fatal_error

def crop_position_mask(img, mask, device, x, y, v_pos="top", h_pos="right", debug=None):
    """Crop position mask

    Inputs:
    img     = image to mask
    mask    = mask to use (must be correct size, if, not use make_resize_mask function)
    x       = x position
    y       = y position
    v_pos   = push from "top" or "bottom"
    h_pos   = push to "right" or "left"
    device  = device counter
    debug   = None, print, or plot. Print = save to file, Plot = print to screen.

    Returns:
    device  = device number
    newmask = image mask

    :param img: numpy array
    :param mask: numpy array
    :param device: int
    :param x: int
    :param y: int
    :param v_pos: str
    :param h_pos: str
    :param debug: str
    :return device: int
    :return newmask: numpy array
    """

    ori_mask = np.copy(mask)

    device += 1

    if x < 0 or y < 0:
        fatal_error("x and y cannot be negative numbers or non-integers")

    # get the sizes of the images
    # subtract 1 from x and y since python counts start from 0
    if y != 0:
        y = y - 1
    if x != 0:
        x = x - 1

    if len(np.shape(img)) == 3:
        ix, iy, iz = np.shape(img)
        ori_img = np.copy(img)
    else:
        ix, iy = np.shape(img)
        ori_img = np.dstack((img, img, img))

    if len(np.shape(mask)) == 3:
        mx, my, mz = np.shape(mask)
        mask = mask[0]
    else:
        mx, my = np.shape(mask)

    npimg = np.zeros((ix, iy), dtype=np.uint8)

    # resize the images so they are equal in size and centered
    if mx >= ix:
        r = mx - ix
        if r % 2 == 0:
            r1 = int(np.rint(r / 2.0))
            r2 = r1
        else:
            r1 = int(np.rint(r / 2.0))
            r2 = r1 - 1
        mask = mask[r1:mx - r2, 0:my]
    if my >= iy:
        r = my - iy
        if r % 2 == 0:
            r1 = int(np.rint(r / 2.0))
            r2 = r1
        else:
            r1 = int(np.rint(r / 2.0))
            r2 = r1 - 1
        mask = mask[0:mx, r1:my - r2]

    # get he sizes of the images again since you might have changed them.
    if len(np.shape(img)) == 3:
        ix, iy, iz = np.shape(img)
        ori_img = np.copy(img)
    else:
        ix, iy = np.shape(img)
        ori_img = np.dstack((img, img, img))

    if len(np.shape(mask)) == 3:
        mx, my, mz = np.shape(mask)
    else:
        mx, my = np.shape(mask)

    if v_pos == "top":
        # Add rows to the top
        top = np.zeros((x, my), dtype=np.uint8)

        maskv = np.vstack((top, mask))

        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        if mx >= ix:
            maskv = maskv[0:ix, 0:my]

        if mx < ix:
            r = ix - mx
            if r % 2 == 0:
                r1 = int(r / 2.0)
                rows1 = np.zeros((r1, my), dtype=np.uint8)
                maskv = np.vstack((rows1, maskv, rows1))
            else:
                r1 = int(math.ceil(r / 2.0))
                r2 = r1 - 1
                rows1 = np.zeros((r1, my), dtype=np.uint8)
                rows2 = np.zeros((r2, my), dtype=np.uint8)
                maskv = np.vstack((rows1, maskv, rows2))
        if debug == 'print':
            print_image(maskv, (str(device) + "_push-top_.png"))
        elif debug == 'plot':
            plot_image(maskv, cmap='gray')

    if v_pos == "bottom":
        # Add rows to the bottom
        bottom = np.zeros((x, my), dtype=np.uint8)

        maskv = np.vstack((mask, bottom))
        # print_image(maskv,(str(device)+"_push-bottom-test.png"))

        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        if mx >= ix:
            maskdiff = mx - ix
            maskv = maskv[maskdiff:mx, 0:my]
            # print_image(maskv,(str(device)+"_push-bottom-test.png"))

        if mx < ix:
            r = ix - mx
            if r % 2 == 0:
                r1 = int(r / 2.0)
                rows1 = np.zeros((r1, my), dtype=np.uint8)
                maskv = np.vstack((rows1, maskv, rows1))
            else:
                r1 = int(math.ceil(r / 2.0))
                r2 = r1 - 1
                rows1 = np.zeros((r1, my), dtype=np.uint8)
                rows2 = np.zeros((r2, my), dtype=np.uint8)
                maskv = np.vstack((rows1, maskv, rows2))
        if debug == 'print':
            print_image(maskv, (str(device) + "_push-bottom.png"))
        elif debug == 'plot':
            plot_image(maskv, cmap='gray')

    if h_pos == "left":
        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        # Add rows to the left
        left = np.zeros((mx, y), dtype=np.uint8)
        maskv = np.hstack((left, maskv))

        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        if my >= iy:
            maskv = maskv[0:mx, 0:iy]

        if my < iy:
            c = iy - my
            if c % 2 == 0:
                c1 = int(c / 2.0)
                col = np.zeros((mx, c1), dtype=np.uint8)
                maskv = np.hstack((col, maskv, col))
            else:
                c1 = int(math.ceil(c / 2.0))
                c2 = c1 - 1
                col1 = np.zeros((mx, c1), dtype=np.uint8)
                col2 = np.zeros((mx, c2), dtype=np.uint8)
                maskv = np.hstack((col1, maskv, col2))
        if debug == 'print':
            print_image(maskv, (str(device) + "_push-left.png"))
        elif debug == 'plot':
            plot_image(maskv, cmap='gray')

    if h_pos == "right":
        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        # Add rows to the left
        right = np.zeros((mx, y), dtype=np.uint8)
        maskv = np.hstack((maskv, right))

        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        if my >= iy:
            ex = my - iy
            maskv = maskv[0:mx, ex:my]

        if my < iy:
            c = iy - my
            if c % 2 == 0:
                c1 = int(c / 2.0)
                col = np.zeros((mx, c1), dtype=np.uint8)
                maskv = np.hstack((col, maskv, col))
            else:
                c1 = int(math.ceil(c / 2.0))
                c2 = c1 - 1
                col1 = np.zeros((mx, c1), dtype=np.uint8)
                col2 = np.zeros((mx, c2), dtype=np.uint8)
                maskv = np.hstack((col1, maskv, col2))
        if debug == 'print':
            print_image(maskv, (str(device) + "_push-right.png"))
        elif debug == 'plot':
            plot_image(maskv, cmap='gray')

    newmask = np.array(maskv)
    if debug is not None:
        if debug == 'print':
            print_image(newmask, (str(device) + "_newmask.png"))
        elif debug == 'plot':
            plot_image(newmask, cmap='gray')
        objects, hierarchy = cv2.findContours(np.copy(newmask), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        for i, cnt in enumerate(objects):
            cv2.drawContours(ori_img, objects, i, (255, 102, 255), -1, lineType=8, hierarchy=hierarchy)
        if debug == 'print':
            print_image(ori_img, (str(device) + '_mask_overlay.png'))
        elif debug == 'plot':
            plot_image(ori_img)

    return device, newmask
<commit_msg>Removed unused variables<commit_after># Crop position mask

import cv2
import numpy as np
import math
from . import print_image
from . import plot_image
from . import fatal_error

def crop_position_mask(img, mask, device, x, y, v_pos="top", h_pos="right", debug=None):
    """Crop position mask

    Inputs:
    img     = image to mask
    mask    = mask to use (must be correct size, if, not use make_resize_mask function)
    x       = x position
    y       = y position
    v_pos   = push from "top" or "bottom"
    h_pos   = push to "right" or "left"
    device  = device counter
    debug   = None, print, or plot. Print = save to file, Plot = print to screen.

    Returns:
    device  = device number
    newmask = image mask

    :param img: numpy array
    :param mask: numpy array
    :param device: int
    :param x: int
    :param y: int
    :param v_pos: str
    :param h_pos: str
    :param debug: str
    :return device: int
    :return newmask: numpy array
    """

    device += 1

    if x < 0 or y < 0:
        fatal_error("x and y cannot be negative numbers or non-integers")

    # get the sizes of the images
    # subtract 1 from x and y since python counts start from 0
    if y != 0:
        y = y - 1
    if x != 0:
        x = x - 1

    if len(np.shape(img)) == 3:
        ix, iy, iz = np.shape(img)
    else:
        ix, iy = np.shape(img)

    if len(np.shape(mask)) == 3:
        mx, my, mz = np.shape(mask)
        mask = mask[0]
    else:
        mx, my = np.shape(mask)

    # resize the images so they are equal in size and centered
    if mx >= ix:
        r = mx - ix
        if r % 2 == 0:
            r1 = int(np.rint(r / 2.0))
            r2 = r1
        else:
            r1 = int(np.rint(r / 2.0))
            r2 = r1 - 1
        mask = mask[r1:mx - r2, 0:my]
    if my >= iy:
        r = my - iy
        if r % 2 == 0:
            r1 = int(np.rint(r / 2.0))
            r2 = r1
        else:
            r1 = int(np.rint(r / 2.0))
            r2 = r1 - 1
        mask = mask[0:mx, r1:my - r2]

    # get he sizes of the images again since you might have changed them.
    if len(np.shape(img)) == 3:
        ix, iy, iz = np.shape(img)
        ori_img = np.copy(img)
    else:
        ix, iy = np.shape(img)
        ori_img = np.dstack((img, img, img))

    if len(np.shape(mask)) == 3:
        mx, my, mz = np.shape(mask)
    else:
        mx, my = np.shape(mask)

    if v_pos == "top":
        # Add rows to the top
        top = np.zeros((x, my), dtype=np.uint8)

        maskv = np.vstack((top, mask))

        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        if mx >= ix:
            maskv = maskv[0:ix, 0:my]

        if mx < ix:
            r = ix - mx
            if r % 2 == 0:
                r1 = int(r / 2.0)
                rows1 = np.zeros((r1, my), dtype=np.uint8)
                maskv = np.vstack((rows1, maskv, rows1))
            else:
                r1 = int(math.ceil(r / 2.0))
                r2 = r1 - 1
                rows1 = np.zeros((r1, my), dtype=np.uint8)
                rows2 = np.zeros((r2, my), dtype=np.uint8)
                maskv = np.vstack((rows1, maskv, rows2))
        if debug == 'print':
            print_image(maskv, (str(device) + "_push-top_.png"))
        elif debug == 'plot':
            plot_image(maskv, cmap='gray')

    if v_pos == "bottom":
        # Add rows to the bottom
        bottom = np.zeros((x, my), dtype=np.uint8)

        maskv = np.vstack((mask, bottom))
        # print_image(maskv,(str(device)+"_push-bottom-test.png"))

        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        if mx >= ix:
            maskdiff = mx - ix
            maskv = maskv[maskdiff:mx, 0:my]
            # print_image(maskv,(str(device)+"_push-bottom-test.png"))

        if mx < ix:
            r = ix - mx
            if r % 2 == 0:
                r1 = int(r / 2.0)
                rows1 = np.zeros((r1, my), dtype=np.uint8)
                maskv = np.vstack((rows1, maskv, rows1))
            else:
                r1 = int(math.ceil(r / 2.0))
                r2 = r1 - 1
                rows1 = np.zeros((r1, my), dtype=np.uint8)
                rows2 = np.zeros((r2, my), dtype=np.uint8)
                maskv = np.vstack((rows1, maskv, rows2))
        if debug == 'print':
            print_image(maskv, (str(device) + "_push-bottom.png"))
        elif debug == 'plot':
            plot_image(maskv, cmap='gray')

    if h_pos == "left":
        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        # Add rows to the left
        left = np.zeros((mx, y), dtype=np.uint8)
        maskv = np.hstack((left, maskv))

        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        if my >= iy:
            maskv = maskv[0:mx, 0:iy]

        if my < iy:
            c = iy - my
            if c % 2 == 0:
                c1 = int(c / 2.0)
                col = np.zeros((mx, c1), dtype=np.uint8)
                maskv = np.hstack((col, maskv, col))
            else:
                c1 = int(math.ceil(c / 2.0))
                c2 = c1 - 1
                col1 = np.zeros((mx, c1), dtype=np.uint8)
                col2 = np.zeros((mx, c2), dtype=np.uint8)
                maskv = np.hstack((col1, maskv, col2))
        if debug == 'print':
            print_image(maskv, (str(device) + "_push-left.png"))
        elif debug == 'plot':
            plot_image(maskv, cmap='gray')

    if h_pos == "right":
        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        # Add rows to the left
        right = np.zeros((mx, y), dtype=np.uint8)
        maskv = np.hstack((maskv, right))

        if len(np.shape(maskv)) == 3:
            mx, my, mz = np.shape(maskv)
        else:
            mx, my = np.shape(maskv)

        if my >= iy:
            ex = my - iy
            maskv = maskv[0:mx, ex:my]

        if my < iy:
            c = iy - my
            if c % 2 == 0:
                c1 = int(c / 2.0)
                col = np.zeros((mx, c1), dtype=np.uint8)
                maskv = np.hstack((col, maskv, col))
            else:
                c1 = int(math.ceil(c / 2.0))
                c2 = c1 - 1
                col1 = np.zeros((mx, c1), dtype=np.uint8)
                col2 = np.zeros((mx, c2), dtype=np.uint8)
                maskv = np.hstack((col1, maskv, col2))
        if debug == 'print':
            print_image(maskv, (str(device) + "_push-right.png"))
        elif debug == 'plot':
            plot_image(maskv, cmap='gray')

    newmask = np.array(maskv)
    if debug is not None:
        if debug == 'print':
            print_image(newmask, (str(device) + "_newmask.png"))
        elif debug == 'plot':
            plot_image(newmask, cmap='gray')
        objects, hierarchy = cv2.findContours(np.copy(newmask), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        for i, cnt in enumerate(objects):
            cv2.drawContours(ori_img, objects, i, (255, 102, 255), -1, lineType=8, hierarchy=hierarchy)
        if debug == 'print':
            print_image(ori_img, (str(device) + '_mask_overlay.png'))
        elif debug == 'plot':
            plot_image(ori_img)

    return device, newmask
<|endoftext|>


<commit_before><commit_msg>add imports in __init__.py<commit_after>from transmute_core import *
from .swagger import add_swagger_api_route, create_swagger_json_handler, add_swagger
from .route import add_route

route = add_route
<|endoftext|>


<commit_before>"""
MIT License
Copyright (c) 2016 Santi Dsp
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.
"""

from sys import version_info as py_version

import numpy as np
from sklearn.metrics import f1_score, precision_score, recall_score

def RMSE(groundtruth, prediction, mask=None):
    """
    groundtruth: matrix containing the real samples to be predicted (N samples,
                 sample dim)
    prediction: matrix containing the prediction (N samples,
                sample dim)
    mask: optional binary mask to not consider certain samples
          (0 in mask)
    """
    groundtruth = np.array(groundtruth, dtype=np.float32)
    prediction = np.array(prediction, dtype=np.float32)
    assert groundtruth.shape == prediction.shape
    if mask is not None:
        mask = np.array(mask)
        groundtruth = groundtruth[mask == 1]
        prediction = prediction[mask == 1]
    D = (groundtruth - prediction) ** 2
    D = np.mean(D, axis=0)
    return np.sqrt(D)

def AFPR(groundtruth, prediction):
    """
    Evaluate Accuracy, F-measure, Precision and Recall for binary inputs
    """
    groundtruth = np.array(groundtruth)
    prediction = np.array(prediction)
    assert groundtruth.shape == prediction.shape
    # A: accuracy
    I = np.mean(groundtruth == prediction)
    F = f1_score(groundtruth, prediction)
    P = precision_score(groundtruth, prediction)
    R = recall_score(groundtruth, prediction)
    return I, F, P, R

def MCD(gt_cep, pr_cep):
    """
    Mel Cepstral Distortion
    Input are matrices with shape (time, cc_dim)
    """
    MCD = 0
    if py_version.major >= 3:
        for t in range(gt_cep.shape[0]):
            acum = 0
            for n in range(gt_cep.shape[1]):
                acum += (gt_cep[t, n] - pr_cep[t, n]) ** 2
            MCD += np.sqrt(acum)
    else:
        for t in xrange(gt_cep.shape[0]):
            acum = 0
            for n in xrange(gt_cep.shape[1]):
                acum += (gt_cep[t, n] - pr_cep[t, n]) ** 2
            MCD += np.sqrt(acum)

    # scale factor
    alpha = ((10. * np.sqrt(2)) / (gt_cep.shape[0] * np.log(10)))
    MCD += alpha
    return MCD
<commit_msg>Fix in MCD computation<commit_after>"""
MIT License
Copyright (c) 2016 Santi Dsp
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.
"""

from sys import version_info as py_version

import numpy as np
from sklearn.metrics import f1_score, precision_score, recall_score

def RMSE(groundtruth, prediction, mask=None):
    """
    groundtruth: matrix containing the real samples to be predicted (N samples,
                 sample dim)
    prediction: matrix containing the prediction (N samples,
                sample dim)
    mask: optional binary mask to not consider certain samples
          (0 in mask)
    """
    groundtruth = np.array(groundtruth, dtype=np.float32)
    prediction = np.array(prediction, dtype=np.float32)
    assert groundtruth.shape == prediction.shape
    if mask is not None:
        mask = np.array(mask)
        groundtruth = groundtruth[mask == 1]
        prediction = prediction[mask == 1]
    D = (groundtruth - prediction) ** 2
    D = np.mean(D, axis=0)
    return np.sqrt(D)

def AFPR(groundtruth, prediction):
    """
    Evaluate Accuracy, F-measure, Precision and Recall for binary inputs
    """
    groundtruth = np.array(groundtruth)
    prediction = np.array(prediction)
    assert groundtruth.shape == prediction.shape
    # A: accuracy
    I = np.mean(groundtruth == prediction)
    F = f1_score(groundtruth, prediction)
    P = precision_score(groundtruth, prediction)
    R = recall_score(groundtruth, prediction)
    return I, F, P, R

def MCD(gt_cep, pr_cep):
    from six.moves import xrange
    """
    Mel Cepstral Distortion
    Input are matrices with shape (time, cc_dim)
    """
    MCD = 0
    for t in xrange(gt_cep.shape[0]):
        acum = 0
        # TODO Vectoritzar segon bucle
        for n in xrange(gt_cep.shape[1]):
            acum += (gt_cep[t, n] - pr_cep[t, n]) ** 2
        MCD += np.sqrt(acum)

    # scale factor
    alpha = ((10. * np.sqrt(2)) / (gt_cep.shape[0] * np.log(10)))
    MCD *= alpha
    return MCD
<|endoftext|>


<commit_before>#
# This file is part of the PyMeasure package.
#
# Copyright (c) 2013-2015 Colin Jermain, Graham Rowlands
#
# 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.
#

from .parameters import Parameter

class Procedure(object):
    """Provides the base class of a procedure to organize the experiment
    execution. Procedures should be run in ProcedureThreads to ensure that
    concurrent control is properly managed.

    .. code-block:: python

        procedure = Procedure()
        thread = ProcedureThread() # or QProcedureThread()
        thread.load(procedure)
        thread.start()

    Inhereting classes are required to define the enter, execute,
    and exit methods. The exit method is called on independent of the
    sucessful completion, software error, or abort of code run in the
    execute method.
    """

    DATA_COLUMNS = []
    FINISHED, FAILED, ABORTED, QUEUED, RUNNING = 0, 1, 2, 3, 4

    _parameters = {}

    def __init__(self):
        self.status = Procedure.QUEUED
        self._update_parameters()

    def _update_parameters(self):
        """ Collects all the Parameter objects for this procedure and stores
        them in a meta dictionary so that the actual values can be set in their
        stead
        """
        if not self._parameters:
            self._parameters = {}
        for item in dir(self):
            parameter = getattr(self, item)
            if isinstance(parameter, Parameter):
                self._parameters[item] = parameter
                if parameter.is_set():
                    setattr(self, item, parameter.value)
                else:
                    setattr(self, item, None)

    def parameters_are_set(self):
        """ Returns True if all parameters are set """
        for name, parameter in self._parameters.items():
            if getattr(self, name) is None:
                return False
        return True

    def check_parameters(self):
        """ Raises an exception if any parameter is missing before calling
        the associated function. Ensures that each value can be set and
        got, which should cast it into the right format. Used as a decorator
        @checkParameters on the enter method
        """
        for name, parameter in self._parameters.items():
            value = getattr(self, name)
            if value is None:
                raise NameError("Missing %s '%s' in %s" % (
                    parameter.__class__, name, self.__class__))

    def parameter_values(self):
        """ Returns a dictionary of all the Parameter values and grabs any
        current values that are not in the default definitions
        """
        result = {}
        for name, parameter in self._parameters.items():
            value = getattr(self, name)
            if value is not None:
                parameter.value = value
                setattr(self, name, parameter.value)
                result[name] = parameter.value
            else:
                result[name] = None
        return result

    def parameter_objects(self):
        """ Returns a dictionary of all the Parameter objects and grabs any
        current values that are not in the default definitions
        """
        result = {}
        for name, parameter in self._parameters.items():
            value = getattr(self, name)
            if value is not None:
                parameter.value = value
                setattr(self, name, parameter.value)
            result[name] = parameter
        return result

    def refresh_parameters(self):
        """ Enforces that all the parameters are re-cast and updated in the meta
        dictionary
        """
        for name, parameter in self._parameters.items():
            value = getattr(self, name)
            parameter.value = value
            setattr(self, name, parameter.value)

    def set_parameters(self, parameters, except_missing=True):
        """ Sets a dictionary of parameters and raises an exception if additional
        parameters are present if except_missing is True
        """
        for name, value in parameters.items():
            if name in self._parameters:
                self._parameters[name].value = value
                setattr(self, name, self._parameters[name].value)
            else:
                if except_missing:
                    raise NameError("Parameter '%s' does not belong to '%s'" % (
                            name, repr(self)))

    def enter(self):
        """ Executes the commands needed at the start-up of the measurement
        """
        pass

    def execute(self):
        """ Preforms the commands needed for the measurement itself. During
        execution the exit method will always be run following this method.
        This includes when Exceptions are raised.
        """
        pass

    def exit(self):
        """ Executes the commands necessary to shut down the instruments
        and leave them in a safe state.
        """
        pass

    def __str__(self):
        result = repr(self) + "\n"
        for name, obj in self.parameterObjects().items():
            if obj.unit:
                result += "%s: %s %s\n" % (obj.name, obj.value, obj.unit)
            else:
                result += "%s: %s\n" % (obj.name, obj.value)
        return result

class UnknownProcedure(Procedure):
    """ Handles the case when a :class:`.Procedure` object can not be imported
    during loading in the :class:`.Results` class
    """

    def __init__(self, parameters):
        self._parameters = parameters

    def enter(self):
        raise Exception("UnknownProcedure can not be run")
<commit_msg>Updated string method for Procedure<commit_after>#
# This file is part of the PyMeasure package.
#
# Copyright (c) 2013-2015 Colin Jermain, Graham Rowlands
#
# 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.
#

from .parameters import Parameter

class Procedure(object):
    """Provides the base class of a procedure to organize the experiment
    execution. Procedures should be run in ProcedureThreads to ensure that
    concurrent control is properly managed.

    .. code-block:: python

        procedure = Procedure()
        thread = ProcedureThread() # or QProcedureThread()
        thread.load(procedure)
        thread.start()

    Inhereting classes are required to define the enter, execute,
    and exit methods. The exit method is called on independent of the
    sucessful completion, software error, or abort of code run in the
    execute method.
    """

    DATA_COLUMNS = []
    FINISHED, FAILED, ABORTED, QUEUED, RUNNING = 0, 1, 2, 3, 4

    _parameters = {}

    def __init__(self):
        self.status = Procedure.QUEUED
        self._update_parameters()

    def _update_parameters(self):
        """ Collects all the Parameter objects for this procedure and stores
        them in a meta dictionary so that the actual values can be set in their
        stead
        """
        if not self._parameters:
            self._parameters = {}
        for item in dir(self):
            parameter = getattr(self, item)
            if isinstance(parameter, Parameter):
                self._parameters[item] = parameter
                if parameter.is_set():
                    setattr(self, item, parameter.value)
                else:
                    setattr(self, item, None)

    def parameters_are_set(self):
        """ Returns True if all parameters are set """
        for name, parameter in self._parameters.items():
            if getattr(self, name) is None:
                return False
        return True

    def check_parameters(self):
        """ Raises an exception if any parameter is missing before calling
        the associated function. Ensures that each value can be set and
        got, which should cast it into the right format. Used as a decorator
        @checkParameters on the enter method
        """
        for name, parameter in self._parameters.items():
            value = getattr(self, name)
            if value is None:
                raise NameError("Missing %s '%s' in %s" % (
                    parameter.__class__, name, self.__class__))

    def parameter_values(self):
        """ Returns a dictionary of all the Parameter values and grabs any
        current values that are not in the default definitions
        """
        result = {}
        for name, parameter in self._parameters.items():
            value = getattr(self, name)
            if value is not None:
                parameter.value = value
                setattr(self, name, parameter.value)
                result[name] = parameter.value
            else:
                result[name] = None
        return result

    def parameter_objects(self):
        """ Returns a dictionary of all the Parameter objects and grabs any
        current values that are not in the default definitions
        """
        result = {}
        for name, parameter in self._parameters.items():
            value = getattr(self, name)
            if value is not None:
                parameter.value = value
                setattr(self, name, parameter.value)
            result[name] = parameter
        return result

    def refresh_parameters(self):
        """ Enforces that all the parameters are re-cast and updated in the meta
        dictionary
        """
        for name, parameter in self._parameters.items():
            value = getattr(self, name)
            parameter.value = value
            setattr(self, name, parameter.value)

    def set_parameters(self, parameters, except_missing=True):
        """ Sets a dictionary of parameters and raises an exception if additional
        parameters are present if except_missing is True
        """
        for name, value in parameters.items():
            if name in self._parameters:
                self._parameters[name].value = value
                setattr(self, name, self._parameters[name].value)
            else:
                if except_missing:
                    raise NameError("Parameter '%s' does not belong to '%s'" % (
                            name, repr(self)))

    def enter(self):
        """ Executes the commands needed at the start-up of the measurement
        """
        pass

    def execute(self):
        """ Preforms the commands needed for the measurement itself. During
        execution the exit method will always be run following this method.
        This includes when Exceptions are raised.
        """
        pass

    def exit(self):
        """ Executes the commands necessary to shut down the instruments
        and leave them in a safe state.
        """
        pass

    def __str__(self):
        result = repr(self) + "\n"
        for parameter self._parameters.items():
            result += str(parameter)
        return result

class UnknownProcedure(Procedure):
    """ Handles the case when a :class:`.Procedure` object can not be imported
    during loading in the :class:`.Results` class
    """

    def __init__(self, parameters):
        self._parameters = parameters

    def enter(self):
        raise Exception("UnknownProcedure can not be run")
<|endoftext|>


<commit_before>import socket
import sys

def server(log_buffer=sys.stderr):
    address = ('127.0.0.1', 10000)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # print >>log_buffer, "making a server on {0}:{1}".format(*address)
    sock.bind(address)
    sock.listen(1)

    try:
        while True:
            # print >>log_buffer, 'waiting for a connection'
            conn, addr = sock.accept()  # blocking
            try:
                # print >>log_buffer, 'connection - {0}:{1}'.format(*addr)
                while True:
                    data = conn.recv(16)
                    # print >>log_buffer, 'received "{0}"'.format(data)
                    if data:
                        # msg = 'sending data back to client'
                        # print >>log_buffer, msg
                        conn.sendall(data)
                    else:
                        # msg = 'no more data from {0}:{1}'.format(*addr)
                        # print >>log_buffer, msg
                        break
            finally:
                conn.close()

    except KeyboardInterrupt:
        sock.close()
        return

if __name__ == '__main__':
    server()
    sys.exit(0)
<commit_msg>clean commented log strings out of the way<commit_after>import socket
import sys

def server(log_buffer=sys.stderr):
    address = ('127.0.0.1', 10000)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # allow rapid re-use of the socket
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(1)

    try:
        while True:
            conn, addr = sock.accept()  # blocking
            try:
                while True:
                    data = conn.recv(16)
                    if data:
                        conn.sendall(data)
                    else:
                        break
            finally:
                conn.close()

    except KeyboardInterrupt:
        sock.close()
        return

if __name__ == '__main__':
    server()
    sys.exit(0)
<|endoftext|>


<commit_before>"""Holds various Version strings of MPF.

This modules holds the MPF version strings, including the version of BCP it
needs and the config file version it needs.

It's used internally for all sorts of things, from printing the output of the
`mpf --version` command, to making sure any processes connected via BCP are
the proper versions, to automatically triggering new builds and deployments to
PyPI.

"""

__version__ = '0.50.0-dev.71'
'''The full version of MPF.'''

__short_version__ = '0.50'
'''The major.minor version of MPF.'''

__bcp_version__ = '1.1'
'''The version of BCP this build of MPF uses.'''

__config_version__ = '5'
'''The config file version this build of MPF uses.'''

__show_version__ = '5'
'''The show format version this build of MPF uses.'''

version = "MPF v{}".format(__version__)
'''A friendly version string for this build of MPF.'''

extended_version = "MPF v{}, Config version:{}, Show version: {}, " \
                   "BCP version:{}".format(__version__, __config_version__,
                                           __show_version__, __bcp_version__)
'''An extended version string that includes the MPF version, show version,
and BCP versions used in this build of MPF.'''

__api__ = ['version',
           '__short_version__',
           '__bcp_version__',
           '__config_version__',
           '__show_version__',
           'version',
           'extended_version']
<commit_msg>dev-72<commit_after>"""Holds various Version strings of MPF.

This modules holds the MPF version strings, including the version of BCP it
needs and the config file version it needs.

It's used internally for all sorts of things, from printing the output of the
`mpf --version` command, to making sure any processes connected via BCP are
the proper versions, to automatically triggering new builds and deployments to
PyPI.

"""

__version__ = '0.50.0-dev.72'
'''The full version of MPF.'''

__short_version__ = '0.50'
'''The major.minor version of MPF.'''

__bcp_version__ = '1.1'
'''The version of BCP this build of MPF uses.'''

__config_version__ = '5'
'''The config file version this build of MPF uses.'''

__show_version__ = '5'
'''The show format version this build of MPF uses.'''

version = "MPF v{}".format(__version__)
'''A friendly version string for this build of MPF.'''

extended_version = "MPF v{}, Config version:{}, Show version: {}, " \
                   "BCP version:{}".format(__version__, __config_version__,
                                           __show_version__, __bcp_version__)
'''An extended version string that includes the MPF version, show version,
and BCP versions used in this build of MPF.'''

__api__ = ['version',
           '__short_version__',
           '__bcp_version__',
           '__config_version__',
           '__show_version__',
           'version',
           'extended_version']
<|endoftext|>


<commit_before>import wia
from wia.rest_client import post, get, put, delete
from wia.util import logger
from wia.stream_client import Stream
import time
import logging

class Device(object):
    def __init__(self, **kwargs):
        self.id = (kwargs['id'] if 'id' in kwargs else None)
        self.name = (kwargs['name'] if 'name' in kwargs else None)
        self.sensors = (kwargs['sensors'] if 'sensors' in kwargs else None)
        self.events = (kwargs['events'] if 'events' in kwargs else None)
        self.location = (kwargs['location'] if 'location' in kwargs else None)
        self.public = (kwargs['public'] if 'public' in kwargs else None)
        self.isOnline = (kwargs['isOnline'] if 'isOnline' in kwargs else None)
        self.createdAt = (kwargs['createdAt'] if 'createdAt' in kwargs else None)
        self.updatedAt = (kwargs['updatedAt'] if 'updatedAt' in kwargs else None)

    @classmethod
    def create(self, **kwargs):
        path = 'devices'
        created_device = post(path, kwargs)
        return created_device

    @classmethod
    def retrieve(self, id):
        path = 'devices/' + id
        return Device(**retrieved_device)

    def save(self):
        path = 'devices/' + self.id
        return put(path, name=self.name)

    def delete(self):
        path = 'devices/' + self.id
        if delete(path).status_code == 200:
            return True
        else:
            return False

    @classmethod
    def list(self, **kwargs):
        list_devices = get('devices', **kwargs)
        for device in list_devices['devices']:
            data = device
            logger.info("device: %s", data)
        logger.info("count: %s", list_devices['count'])
        return list_devices

class Event(object):
    def __init__(self, **kwargs):
        self.id = (kwargs['id'] if 'id' in kwargs else None)
        self.name = (kwargs['name'] if 'name' in kwargs else None)
        self.data = (kwargs['data'] if 'data' in kwargs else None)
        self.file = (kwargs['file'] if 'file' in kwargs else None)
        self.timestamp = (kwargs['timestamp'] if 'timestamp' in kwargs else None)

    @classmethod
    def publish(self, **kwargs):
        path = 'events'
        if kwargs['file'] is None and wia.Stream.connected and Wia().client_id is not None:
            topic = 'devices/' + Wia().client_id + '/' + path + '/' + kwargs['name']
            Stream.publish(topic=topic, **kwargs)
            return Event()
        else:
            response = post(path, **kwargs)
            return Event(response)

    @classmethod
    def subscribe(self, **kwargs):
        device=kwargs['device']
        topic='devices/' + device + '/events/'
        if 'name' in kwargs:
            topic += kwargs['name']
        else:
            topic += '+'
        Stream.subscribe(topic=topic, func=kwargs['func'])

    @classmethod
    def unsubscribe(self, **kwargs):
        device=kwargs['device']
        topic='devices/' + device + '/events/'
        if 'name' in kwargs:
            topic += kwargs['name']
        else:
            topic += '+'
        Stream.unsubscribe(topic=topic)

    @classmethod
    def list(self, **kwargs):
        response = get('events', **kwargs)
        events = []
        for event in response['events']:
            events = Event(event)
        return {'event':events,'count':response['count']}

class Sensor(object):
    def __init__(self, **kwargs):
        self.name = (kwargs['name'] if 'name' in kwargs else None)
        self.data = (kwargs['data'] if 'data' in kwargs else None)
        self.timestamp = (kwargs['timestamp'] if 'timestamp' in kwargs else None)

    @classmethod
    def publish(self, **kwargs):
        path = 'sensors'
        if wia.Stream.connected and Wia().client_id is not None:
            topic = 'devices/' + Wia().client_id + '/' + path + '/' + kwargs['name']
            Stream.publish(topic=topic, **kwargs)
            return {}
        else:
            return post(path, kwargs)

    @classmethod
    def subscribe(self, **kwargs):
        device=kwargs['device']
        topic='devices/' + device + '/sensors/'
        if 'name' in kwargs:
            topic += kwargs['name']
        else:
            topic += '+'
        Stream.subscribe(topic=topic, func=kwargs['func'])

    @classmethod
    def unsubscribe(self, **kwargs):
        device=kwargs['device']
        topic='devices/' + device + '/sensors/'
        if 'name' in kwargs:
            topic += kwargs['name']
        else:
            topic += '+'
        Stream.unsubscribe(topic=topic)

    @classmethod
    def list(self, **kwargs):
        return get('sensors', **kwargs)

class Location(object):
    def __init__(self, **kwargs):
        self.id = (kwargs['id'] if 'id' in kwargs else None)
        self.latitude = (kwargs['latitude'] if 'latitude' in kwargs else None)
        self.longitude = (kwargs['longitude'] if 'longitude' in kwargs else None)
        self.altitude = (kwargs['altitude'] if 'altitude' in kwargs else None)
        self.timestamp = (kwargs['timestamp'] if 'timestamp' in kwargs else None)
        self.recievedTimestamp = (kwargs['recievedTimestamp'] if 'recievedTimestamp' in kwargs else None)

    @classmethod
    def publish(self, **kwargs):
        path = 'locations'
        if wia.Stream.connected and Wia().client_id is not None:
            topic = 'devices/' + Wia().client_id + '/' + path
            Stream.publish(topic=topic, **kwargs)
            return {}
        else:
            return post(path, kwargs)

    @classmethod
    def subscribe(self, **kwargs):
        device = kwargs['device']
        topic = 'devices/' + device + '/locations'
        Stream.subscribe(topic=topic, func=kwargs['func'])

    @classmethod
    def unsubscribe(self, **kwargs):
        device = kwargs['device']
        topic = 'devices/' + device + '/locations'
        Stream.unsubscribe(topic=topic)

    @classmethod
    def list(self, **kwargs):
        list_locations = get('locations', **kwargs)
        for location in list_locations['locations']:
            data = location
            logger.info("location: %s", data)
        logger.info("count: %s", list_locations['count'])
        return list_locations

class Log(object):
    def __init__(self, **kwargs):
        self.level = (kwargs['level'] if 'level' in kwargs else None)
        self.message = (kwargs['message'] if 'message' in kwargs else None)
        self.data = (kwargs['data'] if 'data' in kwargs else None)
        self.timestamp = (kwargs['timestamp'] if 'timestamp' in kwargs else None)

    @classmethod
    def publish(self, **kwargs):
        path = 'logs'
        if wia.Stream.connected and Wia().client_id is not None:
            topic = 'devices/' + Wia().client_id + '/' + path + '/' + kwargs['level']
            Stream.publish(topic=topic, **kwargs)
            return {}
        else:
            return post(path, kwargs)

    @classmethod
    def subscribe(self, **kwargs):
        device = kwargs['device']
        topic = 'devices/' + device + '/logs'
        Stream.subscribe(topic=topic, func=kwargs['func'])

    @classmethod
    def unsubscribe(self, **kwargs):
        device = kwargs['device']
        topic = 'devices/' + device + '/logs'
        Stream.unsubscribe(topic=topic)

    @classmethod
    def list(self, **kwargs):
        list_logs = get('logs', **kwargs)
        for log in list_logs['logs']:
            data = log
            logger.info("log: %s", data)
        logger.info("count: %s", list_logs['count'])
        return list_logs

class Function(object):
    def __init__(self, **kwargs):
        self.id = (kwargs['id'] if 'id' in kwargs else None)
        self.name = (kwargs['name'] if 'name' in kwargs else None)
        self.isEnabled = (kwargs['isEnabled'] if 'isEnabled' in kwargs else None)
        self.device = (kwargs['device'] if 'device' in kwargs else None)
        self.enabledAt = (kwargs['enabledAt'] if 'enabledAt' in kwargs else None)
        self.createdAt = (kwargs['createdAt'] if 'createdAt' in kwargs else None)
        self.updatedAt = (kwargs['updatedAt'] if 'updatedAt' in kwargs else None)

    @classmethod
    def create(self, **kwargs):
        path = 'functions'
        data = {'name': kwargs['name']}
        new_function = post(path, data)
        device = wia.device_id
        topic = 'devices/' + device + '/functions/' + new_function['id'] + '/call'
        attempts = 0
        while attempts < 6:
            Stream.subscribe(topic=topic, func=kwargs['function'])
            time.sleep(0.5)
            attempts += 1
            if Stream.subscribed == True:
                break
        if not Stream.subscribed:
            raise Exception("SUBSCRIPTION UNSUCCESSFUL")
        return new_function

    @classmethod
    def delete(self, func_id):
        path = 'functions/' + func_id
        if delete(path).status_code == 200:
            return True
        else:
            return False

    @classmethod
    def call(self, **kwargs):
        if wia.Stream.connected:
            topic = 'devices/' + kwargs['device'] + '/functions/' + kwargs['func'] + '/call'
            if 'data' not in kwargs:
                Stream.publish(topic=topic)
            elif type(kwargs['data']) is dict:
                Stream.publish(topic=topic, **kwargs['data'])
            else:
                data = kwargs['data']
                kwargs.pop('data')
                kwargs['data'] = {'arg': data}
                Stream.publish(topic=topic, **kwargs['data'])
        else:
            raise Exception("Unable to call function, not connected to stream")

    @classmethod
    def list(self, **kwargs):
        list_functions = get('functions', **kwargs)
        for function in list_functions['functions']:
            data = function
            logger.info("function: %s", data)
        logger.info("count: %s", list_functions['count'])
        return list_functions

class Customer(object):
    @classmethod
    def signup(self, **kwargs):
        return post('customers/signup', kwargs)

    @classmethod
    def login(self, **kwargs):
        kwargs['scope'] = 'customer'
        kwargs['grantType'] = 'password'
        return post('auth/token', kwargs)

class WhoAmI(object):
    def __init__(self, **kwargs):
        self.contextData = (kwargs['contextData'] if 'contextData' in kwargs else None)
        self.scope = (kwargs['scope'] if 'scope' in kwargs else None)

    @classmethod
    def retrieve(self):
        response = get('whoami')
        return WhoAmI(**response)
<commit_msg>kwargs validation fix<commit_after>import wia
from wia.rest_client import post, get, put, delete
from wia.util import logger
from wia.stream_client import Stream
import time
import logging

class Device(object):
    def __init__(self, **kwargs):
        self.id = (kwargs['id'] if 'id' in kwargs else None)
        self.name = (kwargs['name'] if 'name' in kwargs else None)
        self.sensors = (kwargs['sensors'] if 'sensors' in kwargs else None)
        self.events = (kwargs['events'] if 'events' in kwargs else None)
        self.location = (kwargs['location'] if 'location' in kwargs else None)
        self.public = (kwargs['public'] if 'public' in kwargs else None)
        self.isOnline = (kwargs['isOnline'] if 'isOnline' in kwargs else None)
        self.createdAt = (kwargs['createdAt'] if 'createdAt' in kwargs else None)
        self.updatedAt = (kwargs['updatedAt'] if 'updatedAt' in kwargs else None)

    @classmethod
    def create(self, **kwargs):
        path = 'devices'
        created_device = post(path, kwargs)
        return created_device

    @classmethod
    def retrieve(self, id):
        path = 'devices/' + id
        return Device(**retrieved_device)

    def save(self):
        path = 'devices/' + self.id
        return put(path, name=self.name)

    def delete(self):
        path = 'devices/' + self.id
        if delete(path).status_code == 200:
            return True
        else:
            return False

    @classmethod
    def list(self, **kwargs):
        list_devices = get('devices', **kwargs)
        for device in list_devices['devices']:
            data = device
            logger.info("device: %s", data)
        logger.info("count: %s", list_devices['count'])
        return list_devices

class Event(object):
    def __init__(self, **kwargs):
        self.id = (kwargs['id'] if 'id' in kwargs else None)
        self.name = (kwargs['name'] if 'name' in kwargs else None)
        self.data = (kwargs['data'] if 'data' in kwargs else None)
        self.file = (kwargs['file'] if 'file' in kwargs else None)
        self.timestamp = (kwargs['timestamp'] if 'timestamp' in kwargs else None)

    @classmethod
    def publish(self, **kwargs):
        path = 'events'
        if 'file' in kwargs and wia.Stream.connected and Wia().client_id is not None:
            topic = 'devices/' + Wia().client_id + '/' + path + '/' + kwargs['name']
            Stream.publish(topic=topic, **kwargs)
            return Event()
        else:
            response = post(path, **kwargs)
            return Event(response)

    @classmethod
    def subscribe(self, **kwargs):
        device=kwargs['device']
        topic='devices/' + device + '/events/'
        if 'name' in kwargs:
            topic += kwargs['name']
        else:
            topic += '+'
        Stream.subscribe(topic=topic, func=kwargs['func'])

    @classmethod
    def unsubscribe(self, **kwargs):
        device=kwargs['device']
        topic='devices/' + device + '/events/'
        if 'name' in kwargs:
            topic += kwargs['name']
        else:
            topic += '+'
        Stream.unsubscribe(topic=topic)

    @classmethod
    def list(self, **kwargs):
        response = get('events', **kwargs)
        events = []
        for event in response['events']:
            events = Event(event)
        return {'event':events,'count':response['count']}

class Sensor(object):
    def __init__(self, **kwargs):
        self.name = (kwargs['name'] if 'name' in kwargs else None)
        self.data = (kwargs['data'] if 'data' in kwargs else None)
        self.timestamp = (kwargs['timestamp'] if 'timestamp' in kwargs else None)

    @classmethod
    def publish(self, **kwargs):
        path = 'sensors'
        if wia.Stream.connected and Wia().client_id is not None:
            topic = 'devices/' + Wia().client_id + '/' + path + '/' + kwargs['name']
            Stream.publish(topic=topic, **kwargs)
            return {}
        else:
            return post(path, kwargs)

    @classmethod
    def subscribe(self, **kwargs):
        device=kwargs['device']
        topic='devices/' + device + '/sensors/'
        if 'name' in kwargs:
            topic += kwargs['name']
        else:
            topic += '+'
        Stream.subscribe(topic=topic, func=kwargs['func'])

    @classmethod
    def unsubscribe(self, **kwargs):
        device=kwargs['device']
        topic='devices/' + device + '/sensors/'
        if 'name' in kwargs:
            topic += kwargs['name']
        else:
            topic += '+'
        Stream.unsubscribe(topic=topic)

    @classmethod
    def list(self, **kwargs):
        return get('sensors', **kwargs)

class Location(object):
    def __init__(self, **kwargs):
        self.id = (kwargs['id'] if 'id' in kwargs else None)
        self.latitude = (kwargs['latitude'] if 'latitude' in kwargs else None)
        self.longitude = (kwargs['longitude'] if 'longitude' in kwargs else None)
        self.altitude = (kwargs['altitude'] if 'altitude' in kwargs else None)
        self.timestamp = (kwargs['timestamp'] if 'timestamp' in kwargs else None)
        self.recievedTimestamp = (kwargs['recievedTimestamp'] if 'recievedTimestamp' in kwargs else None)

    @classmethod
    def publish(self, **kwargs):
        path = 'locations'
        if wia.Stream.connected and Wia().client_id is not None:
            topic = 'devices/' + Wia().client_id + '/' + path
            Stream.publish(topic=topic, **kwargs)
            return {}
        else:
            return post(path, kwargs)

    @classmethod
    def subscribe(self, **kwargs):
        device = kwargs['device']
        topic = 'devices/' + device + '/locations'
        Stream.subscribe(topic=topic, func=kwargs['func'])

    @classmethod
    def unsubscribe(self, **kwargs):
        device = kwargs['device']
        topic = 'devices/' + device + '/locations'
        Stream.unsubscribe(topic=topic)

    @classmethod
    def list(self, **kwargs):
        list_locations = get('locations', **kwargs)
        for location in list_locations['locations']:
            data = location
            logger.info("location: %s", data)
        logger.info("count: %s", list_locations['count'])
        return list_locations

class Log(object):
    def __init__(self, **kwargs):
        self.level = (kwargs['level'] if 'level' in kwargs else None)
        self.message = (kwargs['message'] if 'message' in kwargs else None)
        self.data = (kwargs['data'] if 'data' in kwargs else None)
        self.timestamp = (kwargs['timestamp'] if 'timestamp' in kwargs else None)

    @classmethod
    def publish(self, **kwargs):
        path = 'logs'
        if wia.Stream.connected and Wia().client_id is not None:
            topic = 'devices/' + Wia().client_id + '/' + path + '/' + kwargs['level']
            Stream.publish(topic=topic, **kwargs)
            return {}
        else:
            return post(path, kwargs)

    @classmethod
    def subscribe(self, **kwargs):
        device = kwargs['device']
        topic = 'devices/' + device + '/logs'
        Stream.subscribe(topic=topic, func=kwargs['func'])

    @classmethod
    def unsubscribe(self, **kwargs):
        device = kwargs['device']
        topic = 'devices/' + device + '/logs'
        Stream.unsubscribe(topic=topic)

    @classmethod
    def list(self, **kwargs):
        list_logs = get('logs', **kwargs)
        for log in list_logs['logs']:
            data = log
            logger.info("log: %s", data)
        logger.info("count: %s", list_logs['count'])
        return list_logs

class Function(object):
    def __init__(self, **kwargs):
        self.id = (kwargs['id'] if 'id' in kwargs else None)
        self.name = (kwargs['name'] if 'name' in kwargs else None)
        self.isEnabled = (kwargs['isEnabled'] if 'isEnabled' in kwargs else None)
        self.device = (kwargs['device'] if 'device' in kwargs else None)
        self.enabledAt = (kwargs['enabledAt'] if 'enabledAt' in kwargs else None)
        self.createdAt = (kwargs['createdAt'] if 'createdAt' in kwargs else None)
        self.updatedAt = (kwargs['updatedAt'] if 'updatedAt' in kwargs else None)

    @classmethod
    def create(self, **kwargs):
        path = 'functions'
        data = {'name': kwargs['name']}
        new_function = post(path, data)
        device = wia.device_id
        topic = 'devices/' + device + '/functions/' + new_function['id'] + '/call'
        attempts = 0
        while attempts < 6:
            Stream.subscribe(topic=topic, func=kwargs['function'])
            time.sleep(0.5)
            attempts += 1
            if Stream.subscribed == True:
                break
        if not Stream.subscribed:
            raise Exception("SUBSCRIPTION UNSUCCESSFUL")
        return new_function

    @classmethod
    def delete(self, func_id):
        path = 'functions/' + func_id
        if delete(path).status_code == 200:
            return True
        else:
            return False

    @classmethod
    def call(self, **kwargs):
        if wia.Stream.connected:
            topic = 'devices/' + kwargs['device'] + '/functions/' + kwargs['func'] + '/call'
            if 'data' not in kwargs:
                Stream.publish(topic=topic)
            elif type(kwargs['data']) is dict:
                Stream.publish(topic=topic, **kwargs['data'])
            else:
                data = kwargs['data']
                kwargs.pop('data')
                kwargs['data'] = {'arg': data}
                Stream.publish(topic=topic, **kwargs['data'])
        else:
            raise Exception("Unable to call function, not connected to stream")

    @classmethod
    def list(self, **kwargs):
        list_functions = get('functions', **kwargs)
        for function in list_functions['functions']:
            data = function
            logger.info("function: %s", data)
        logger.info("count: %s", list_functions['count'])
        return list_functions

class Customer(object):
    @classmethod
    def signup(self, **kwargs):
        return post('customers/signup', kwargs)

    @classmethod
    def login(self, **kwargs):
        kwargs['scope'] = 'customer'
        kwargs['grantType'] = 'password'
        return post('auth/token', kwargs)

class WhoAmI(object):
    def __init__(self, **kwargs):
        self.contextData = (kwargs['contextData'] if 'contextData' in kwargs else None)
        self.scope = (kwargs['scope'] if 'scope' in kwargs else None)

    @classmethod
    def retrieve(self):
        response = get('whoami')
        return WhoAmI(**response)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Thomas Fillon <thomas@parisson.com>

# This file is part of TimeSide.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Author: Thomas Fillon <thomas@parisson.com>

from timeside.core import implements, interfacedoc
from timeside.core.api import IAnalyzer
from timeside.core.tools.parameters import HasTraits
from timeside.core.tools.parameters import store_parameters

import vamp
import vampyhost
from .vampyhost_wrapper import VampAnalyzer

import numpy as np

class VampSpectralSlope(VampAnalyzer):
    """Extract the spectral slope of an audio spectrum"""

    implements(IAnalyzer)

    class _Param(HasTraits):
        pass

    _schema = {'$schema': 'http://json-schema.org/schema#',
               'properties': {},
               'type': 'object'}

    @store_parameters
    def __init__(self):
        super(VampSpectralSlope, self).__init__()
        self.input_blocksize = 1024
        self.input_stepsize = self.input_blocksize
        # Define Vamp plugin key and output
        self.plugin_key = 'vamp-libxtract:spectral_slope'
        self.plugin_output = 'spectral_slope'

    @interfacedoc
    def setup(self, channels=None, samplerate=None,
              blocksize=None, totalframes=None):
        super(VampSpectralSlope, self).setup(
            channels, samplerate, blocksize, totalframes)

    @staticmethod
    @interfacedoc
    def id():
        return "vamp_spectral_slope"

    @staticmethod
    @interfacedoc
    def name():
        return "Spectral slope (vamp)"

    @staticmethod
    @interfacedoc
    def version():
        return "1.0.0"

    @staticmethod
    @interfacedoc
    def unit():
        return ""

    def post_process(self):
        super(VampSpectralSlope, self).post_process()  # get remaining results

        result = self.new_result(data_mode='value', time_mode='framewise')
        result.data_object.value = self.vamp_results['vector'][1]
        result.data_object.y_value = self.vamp_results['vector'][0]
        self.add_result(result)
<commit_msg>[analyzers] remove scalar from VampSpectralSlope output (see #279)<commit_after># -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Thomas Fillon <thomas@parisson.com>

# This file is part of TimeSide.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Author: Thomas Fillon <thomas@parisson.com>

from timeside.core import implements, interfacedoc
from timeside.core.api import IAnalyzer
from timeside.core.tools.parameters import HasTraits
from timeside.core.tools.parameters import store_parameters

import vamp
import vampyhost
from .vampyhost_wrapper import VampAnalyzer

import numpy as np

class VampSpectralSlope(VampAnalyzer):
    """Extract the spectral slope of an audio spectrum"""

    implements(IAnalyzer)

    class _Param(HasTraits):
        pass

    _schema = {'$schema': 'http://json-schema.org/schema#',
               'properties': {},
               'type': 'object'}

    @store_parameters
    def __init__(self):
        super(VampSpectralSlope, self).__init__()
        self.input_blocksize = 1024
        self.input_stepsize = self.input_blocksize
        # Define Vamp plugin key and output
        self.plugin_key = 'vamp-libxtract:spectral_slope'
        self.plugin_output = 'spectral_slope'

    @interfacedoc
    def setup(self, channels=None, samplerate=None,
              blocksize=None, totalframes=None):
        super(VampSpectralSlope, self).setup(
            channels, samplerate, blocksize, totalframes)

    @staticmethod
    @interfacedoc
    def id():
        return "vamp_spectral_slope"

    @staticmethod
    @interfacedoc
    def name():
        return "Spectral slope (vamp)"

    @staticmethod
    @interfacedoc
    def version():
        return "1.0.0"

    @staticmethod
    @interfacedoc
    def unit():
        return ""

    def post_process(self):
        super(VampSpectralSlope, self).post_process()  # get remaining results

        result = self.new_result(data_mode='value', time_mode='framewise')
        result.data_object.value = self.vamp_results['vector'][1]
        self.add_result(result)
<|endoftext|>


<commit_before>from datetime import datetime

from django.http import Http404
from rest_framework.decorators import detail_route, api_view
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.exceptions import APIException

from bondapi.models import Bond
from bondapi.serializers import BondSerializer

class BondViewSet(ModelViewSet):
    serializer_class = BondSerializer
    queryset = Bond.objects.all()

    @detail_route(methods=['GET'])
    def valuation_by_date(self, request, pk=None):
        bond = self.get_object()
        string_date = request.query_params.get('valuation_date', None)

        if string_date is not None:
            valuation_date = datetime.strptime(string_date, "%Y-%m-%d").date()
            maturity_period_elapsed = self._get_elapsed_fraction_of_period(bond, valuation_date)

            dirty_price = float(bond.bond_valuation) * float((1 + bond.annual_required_return / bond.annual_payment_frequency)) ** maturity_period_elapsed
            accrued_interest = maturity_period_elapsed * float(bond.annual_coupon_rate / bond.annual_payment_frequency * bond.face_value)
            clean_price = dirty_price - accrued_interest

            return Response({
                'bond_name': bond.name,
                'bond_valuation_date': valuation_date,
                'bond_settlement_date': bond.settlement_date,
                'bond_maturity_date': bond.maturity_date,
                'elapsed_period': maturity_period_elapsed,
                'bond_term_to_maturity': bond.term_to_maturity,
                'dirty_price': dirty_price,
                'accrued_interest': accrued_interest,
                'clean_price': clean_price,
            })
        else:
            raise Http404

    def _get_elapsed_fraction_of_period(self, bond, valuation_date):
        seconds_elapsed = valuation_date - bond.settlement_date
        years_elapsed = seconds_elapsed.days / 365
        fraction_of_years_elapsed = years_elapsed * bond.annual_payment_frequency

        original_periods_to_maturity = bond.term_to_maturity * bond.annual_payment_frequency
        elapsed_periods = fraction_of_years_elapsed * bond.annual_payment_frequency

        return elapsed_periods

# class BondValuationOnDateDetail(APIView):
#
#     def get_object(self, pk):
#         try:
#             return Bond.objects.get(pk=pk, available=True)
#
#         except Bond.DoesNotExist:
#             raise Http404
#
#     def get(self, request, pk, format=None):
#         bond = self.get_object(pk)
#
#         serializer = BondSerializer(bond, context={'request': request})
#
#         return Response(serializer.data)
#
#     def put(self, request, pk, format=None):
#         raise ForbiddenAccess
#
#     def delete(self, request, pk, format=None):
#         raise ForbiddenAccess
#
#
# class ForbiddenAccess(APIException):
#     status_code = 403
#     default_detail = 'Action Forbidden'
<commit_msg>Refactor private_methods.<commit_after>from datetime import datetime

from django.http import Http404
from rest_framework.decorators import detail_route, api_view
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.exceptions import APIException

from bondapi.models import Bond
from bondapi.serializers import BondSerializer

class BondViewSet(ModelViewSet):
    serializer_class = BondSerializer
    queryset = Bond.objects.all()

    @detail_route(methods=['GET'])
    def valuation_by_date(self, request, pk=None):
        bond = self.get_object()
        string_date = request.query_params.get('valuation_date', None)

        if string_date is not None:
            valuation_date = datetime.strptime(string_date, "%Y-%m-%d").date()
            maturity_period_elapsed = self._get_elapsed_fraction_of_period(bond, valuation_date)

            dirty_price = self._get_dirty_price(bond, maturity_period_elapsed)
            accrued_interest = self._get_accrued_interest(bond, maturity_period_elapsed)
            clean_price = self._get_clean_price(accrued_interest, dirty_price)

            return Response({
                'bond_name': bond.name,
                'bond_valuation_date': valuation_date,
                'bond_settlement_date': bond.settlement_date,
                'bond_maturity_date': bond.maturity_date,
                'elapsed_period': maturity_period_elapsed,
                'bond_term_to_maturity': bond.term_to_maturity,
                'dirty_price': dirty_price,
                'accrued_interest': accrued_interest,
                'clean_price': clean_price,
            })
        else:
            raise Http404

    def _get_clean_price(self, accrued_interest, dirty_price):
        return dirty_price - accrued_interest

    def _get_accrued_interest(self, bond, maturity_period_elapsed):
        return maturity_period_elapsed * float(
            bond.annual_coupon_rate / bond.annual_payment_frequency * bond.face_value)

    def _get_dirty_price(self, bond, maturity_period_elapsed):
        return float(bond.bond_valuation) * float(
            (1 + bond.annual_required_return / bond.annual_payment_frequency)) ** maturity_period_elapsed

    def _get_elapsed_fraction_of_period(self, bond, valuation_date):
        seconds_elapsed = valuation_date - bond.settlement_date
        years_elapsed = seconds_elapsed.days / 365
        fraction_of_years_elapsed = years_elapsed * bond.annual_payment_frequency

        original_periods_to_maturity = bond.term_to_maturity * bond.annual_payment_frequency
        elapsed_periods = fraction_of_years_elapsed * bond.annual_payment_frequency

        return elapsed_periods

# class BondValuationOnDateDetail(APIView):
#
#     def get_object(self, pk):
#         try:
#             return Bond.objects.get(pk=pk, available=True)
#
#         except Bond.DoesNotExist:
#             raise Http404
#
#     def get(self, request, pk, format=None):
#         bond = self.get_object(pk)
#
#         serializer = BondSerializer(bond, context={'request': request})
#
#         return Response(serializer.data)
#
#     def put(self, request, pk, format=None):
#         raise ForbiddenAccess
#
#     def delete(self, request, pk, format=None):
#         raise ForbiddenAccess
#
#
# class ForbiddenAccess(APIException):
#     status_code = 403
#     default_detail = 'Action Forbidden'
<|endoftext|>


<commit_before>#!/usr/bin/python

# -*- coding: utf-8 -*-

# Copyright (C) 2009-2012:
#    Gabes Jean, naparuba@gmail.com
#    Gerhard Lausser, Gerhard.Lausser@consol.de
#    Gregory Starck, g.starck@gmail.com
#    Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken 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.
#
# Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.

"""This Class is a plugin for the Shinken Broker. It is in charge
to brok information of the service/host perfdatas into the Graphite
backend. http://graphite.wikidot.com/start
"""

import re
from socket import socket

from shinken.basemodule import BaseModule

properties = {
    'daemons' : ['broker'],
    'type' : 'graphite_perfdata',
    'external' : False,
    }

# Called by the plugin manager to get a broker
def get_instance(mod_conf):
    print "Get a graphite data module for plugin %s" % mod_conf.get_name()
    instance = Graphite_broker(mod_conf)
    return instance

# Class for the Graphite Broker
# Get broks and send them to a Carbon instance of Graphite
class Graphite_broker(BaseModule):
    def __init__(self, modconf):
        BaseModule.__init__(self, modconf)
        self.host = getattr(modconf, 'host', 'localhost')
        self.port = int(getattr(modconf, 'port', '2003'))

    # Called by Broker so we can do init stuff
    # TODO : add conf param to get pass with init
    # Conf from arbiter!
    def init(self):
        print "[%s] I init the graphite server connection to %s:%s" % (self.get_name(), self.host, self.port)
        self.con = socket()
        self.con.connect( (self.host, self.port))

    # For a perf_data like /=30MB;4899;4568;1234;0  /var=50MB;4899;4568;1234;0 /toto=
    # return ('/', '30'), ('/var', '50')
    def get_metric_and_value(self, perf_data):
        res = []
        s = perf_data.strip()
        # Get all metrics non void
        elts = s.split(' ')
        metrics = [e for e in elts if e != '']

        for e in metrics:
 #           print "Graphite : groking : ", e
            elts = e.split('=', 1)
            if len(elts) != 2:
                continue
            name = self.illegal_char.sub('_', elts[0])

            raw = elts[1]
            # get the first value of ;
            if ';' in raw:
                elts = raw.split(';')
                name_value = { name : elts[0], name+'_warn' : elts[1], name+'_crit' : elts[2] }
            else:
                value = raw
                name_value = { name : raw }
            # bailout if need
            if name_value[name] == '':
                continue

            # Try to get the int/float in it :)
            for key,value in name_value.items():
                m = re.search("(\d*\.?\d*)(.*)", value)
                if m:
                    name_value[key] = m.groups(0)[0]
                else:
                    continue
#           print "graphite : end of grok :", name, value
            for key,value in name_value.items():
                res.append((key, value))
        return res

    # A service check result brok has just arrived, we UPDATE data info with this
    def manage_service_check_result_brok(self, b):
        data = b.data

        perf_data = data['perf_data']
        couples = self.get_metric_and_value(perf_data)

        # If no values, we can exit now
        if len(couples) == 0:
            return

        hname = self.illegal_char.sub('_', data['host_name'])
        desc = self.illegal_char.sub('_', data['service_description'])
        check_time = int(data['last_chk'])

#        print "Graphite:", hname, desc, check_time, perf_data

        lines = []
        # Send a bulk of all metrics at once
        for (metric, value) in couples:
            lines.append("%s.%s.%s %s %d" % (hname, desc, metric, value, check_time))
        packet = '\n'.join(lines) + '\n' # Be sure we put \n every where
#        print "Graphite launching :", packet
        self.con.sendall(packet)

    # A host check result brok has just arrived, we UPDATE data info with this
    def manage_host_check_result_brok(self, b):
        data = b.data

        perf_data = data['perf_data']
        couples = self.get_metric_and_value(perf_data)

        # If no values, we can exit now
        if len(couples) == 0:
            return

        hname = self.illegal_char.sub('_', data['host_name'])
        check_time = int(data['last_chk'])

 #       print "Graphite:", hname, check_time, perf_data

        lines = []
        # Send a bulk of all metrics at once
        for (metric, value) in couples:
            lines.append("%s.__HOST__.%s %s %d" % (hname, metric, value, check_time))
        packet = '\n'.join(lines) + '\n' # Be sure we put \n every where
  #      print "Graphite launching :", packet
        self.con.sendall(packet)
<commit_msg>* Graphite module can send negative datas and doesn't send empty value<commit_after>#!/usr/bin/python

# -*- coding: utf-8 -*-

# Copyright (C) 2009-2012:
#    Gabes Jean, naparuba@gmail.com
#    Gerhard Lausser, Gerhard.Lausser@consol.de
#    Gregory Starck, g.starck@gmail.com
#    Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken 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.
#
# Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.

"""This Class is a plugin for the Shinken Broker. It is in charge
to brok information of the service/host perfdatas into the Graphite
backend. http://graphite.wikidot.com/start
"""

import re
from socket import socket

from shinken.basemodule import BaseModule

properties = {
    'daemons' : ['broker'],
    'type' : 'graphite_perfdata',
    'external' : False,
    }

# Called by the plugin manager to get a broker
def get_instance(mod_conf):
    print "Get a graphite data module for plugin %s" % mod_conf.get_name()
    instance = Graphite_broker(mod_conf)
    return instance

# Class for the Graphite Broker
# Get broks and send them to a Carbon instance of Graphite
class Graphite_broker(BaseModule):
    def __init__(self, modconf):
        BaseModule.__init__(self, modconf)
        self.host = getattr(modconf, 'host', 'localhost')
        self.port = int(getattr(modconf, 'port', '2003'))

    # Called by Broker so we can do init stuff
    # TODO : add conf param to get pass with init
    # Conf from arbiter!
    def init(self):
        print "[%s] I init the graphite server connection to %s:%s" % (self.get_name(), self.host, self.port)
        self.con = socket()
        self.con.connect( (self.host, self.port))

    # For a perf_data like /=30MB;4899;4568;1234;0  /var=50MB;4899;4568;1234;0 /toto=
    # return ('/', '30'), ('/var', '50')
    def get_metric_and_value(self, perf_data):
        res = []
        s = perf_data.strip()
        # Get all metrics non void
        elts = s.split(' ')
        metrics = [e for e in elts if e != '']

        for e in metrics:
 #           print "Graphite : groking : ", e
            elts = e.split('=', 1)
            if len(elts) != 2:
                continue
            name = self.illegal_char.sub('_', elts[0])

            raw = elts[1]
            # get the first value of ;
            if ';' in raw:
                elts = raw.split(';')
                name_value = { name : elts[0], name+'_warn' : elts[1], name+'_crit' : elts[2] }
            else:
                value = raw
                name_value = { name : raw }
            # bailout if need
            if name_value[name] == '':
                continue

            # Try to get the int/float in it :)
            for key,value in name_value.items():
                m = re.search("(-?\d*\.?\d*)(.*)", value)
                if m:
                    name_value[key] = m.groups(0)[0]
                else:
                    continue
#           print "graphite : end of grok :", name, value
            for key,value in name_value.items():
                res.append((key, value))
        return res

    # A service check result brok has just arrived, we UPDATE data info with this
    def manage_service_check_result_brok(self, b):
        data = b.data

        perf_data = data['perf_data']
        couples = self.get_metric_and_value(perf_data)

        # If no values, we can exit now
        if len(couples) == 0:
            return

        hname = self.illegal_char.sub('_', data['host_name'])
        desc = self.illegal_char.sub('_', data['service_description'])
        check_time = int(data['last_chk'])

#        print "Graphite:", hname, desc, check_time, perf_data

        lines = []
        # Send a bulk of all metrics at once
        for (metric, value) in couples:
            if value:
                lines.append("%s.%s.%s %s %d" % (hname, desc, metric, value, check_time))
        packet = '\n'.join(lines) + '\n' # Be sure we put \n every where
#        print "Graphite launching :", packet
        self.con.sendall(packet)

    # A host check result brok has just arrived, we UPDATE data info with this
    def manage_host_check_result_brok(self, b):
        data = b.data

        perf_data = data['perf_data']
        couples = self.get_metric_and_value(perf_data)

        # If no values, we can exit now
        if len(couples) == 0:
            return

        hname = self.illegal_char.sub('_', data['host_name'])
        check_time = int(data['last_chk'])

 #       print "Graphite:", hname, check_time, perf_data

        lines = []
        # Send a bulk of all metrics at once
        for (metric, value) in couples:
            lines.append("%s.__HOST__.%s %s %d" % (hname, metric, value, check_time))
        packet = '\n'.join(lines) + '\n' # Be sure we put \n every where
  #      print "Graphite launching :", packet
        self.con.sendall(packet)
<|endoftext|>


<commit_before>"""Holds various Version strings of MPF.

This modules holds the MPF version strings, including the version of BCP it
needs and the config file version it needs.

It's used internally for all sorts of things, from printing the output of the
`mpf --version` command, to making sure any processes connected via BCP are
the proper versions, to automatically triggering new builds and deployments to
PyPI.

"""

__version__ = '0.56.0-dev.11'
'''The full version of MPF.'''

__short_version__ = '0.56'
'''The major.minor version of MPF.'''

__bcp_version__ = '1.1'
'''The version of BCP this build of MPF uses.'''

__config_version__ = '5'
'''The config file version this build of MPF uses.'''

__show_version__ = '5'
'''The show format version this build of MPF uses.'''

# pylint: disable-msg=invalid-name
version = "MPF v{}".format(__version__)
'''A friendly version string for this build of MPF.'''

# pylint: disable-msg=invalid-name
extended_version = "MPF v{}, Config version:{}, Show version: {}, " \
                   "BCP version:{}".format(__version__, __config_version__,
                                           __show_version__, __bcp_version__)
'''An extended version string that includes the MPF version, show version,
and BCP versions used in this build of MPF.'''

if "dev" in __version__:
    # pylint: disable-msg=invalid-name
    log_url = "https://docs.missionpinball.org/en/dev/logs/{}.html"
else:
    # pylint: disable-msg=invalid-name
    log_url = "https://docs.missionpinball.org/en/{}/logs/{{}}.html".format(__short_version__)

__api__ = ['version',
           '__short_version__',
           '__bcp_version__',
           '__config_version__',
           '__show_version__',
           'version',
           'extended_version',
           'log_url']
<commit_msg>0.56.0-dev.12<commit_after>"""Holds various Version strings of MPF.

This modules holds the MPF version strings, including the version of BCP it
needs and the config file version it needs.

It's used internally for all sorts of things, from printing the output of the
`mpf --version` command, to making sure any processes connected via BCP are
the proper versions, to automatically triggering new builds and deployments to
PyPI.

"""

__version__ = '0.56.0-dev.12'
'''The full version of MPF.'''

__short_version__ = '0.56'
'''The major.minor version of MPF.'''

__bcp_version__ = '1.1'
'''The version of BCP this build of MPF uses.'''

__config_version__ = '5'
'''The config file version this build of MPF uses.'''

__show_version__ = '5'
'''The show format version this build of MPF uses.'''

# pylint: disable-msg=invalid-name
version = "MPF v{}".format(__version__)
'''A friendly version string for this build of MPF.'''

# pylint: disable-msg=invalid-name
extended_version = "MPF v{}, Config version:{}, Show version: {}, " \
                   "BCP version:{}".format(__version__, __config_version__,
                                           __show_version__, __bcp_version__)
'''An extended version string that includes the MPF version, show version,
and BCP versions used in this build of MPF.'''

if "dev" in __version__:
    # pylint: disable-msg=invalid-name
    log_url = "https://docs.missionpinball.org/en/dev/logs/{}.html"
else:
    # pylint: disable-msg=invalid-name
    log_url = "https://docs.missionpinball.org/en/{}/logs/{{}}.html".format(__short_version__)

__api__ = ['version',
           '__short_version__',
           '__bcp_version__',
           '__config_version__',
           '__show_version__',
           'version',
           'extended_version',
           'log_url']
<|endoftext|>


<commit_before>from io import FileIO, BufferedWriter
import os
from StringIO import StringIO

from django.conf import settings

from ajaxuploader.backends.base import AbstractUploadBackend

class LocalUploadBackend(AbstractUploadBackend):
    UPLOAD_DIR = "uploads"

    def setup(self, filename):
        self._path = os.path.join(
            settings.MEDIA_ROOT, self.UPLOAD_DIR, filename)
        try:
            os.makedirs(os.path.realpath(os.path.dirname(self._path)))
        except:
            pass
        self._dest = BufferedWriter(FileIO(self._path, "wb"))

    def upload_chunk(self, chunk):
        self._dest.write(chunk)

    def upload_complete(self, request, filename):
        path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + filename
        return {"path": path}
<commit_msg>Removed StringIO import which wasn't being used. Changed file io mode.<commit_after>from io import FileIO, BufferedWriter
import os

from django.conf import settings

from ajaxuploader.backends.base import AbstractUploadBackend

class LocalUploadBackend(AbstractUploadBackend):
    UPLOAD_DIR = "uploads"

    def setup(self, filename):
        self._path = os.path.join(
            settings.MEDIA_ROOT, self.UPLOAD_DIR, filename)
        try:
            os.makedirs(os.path.realpath(os.path.dirname(self._path)))
        except:
            pass
        self._dest = BufferedWriter(FileIO(self._path, "w"))

    def upload_chunk(self, chunk):
        self._dest.write(chunk)

    def upload_complete(self, request, filename):
        path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + filename
        return {"path": path}
<|endoftext|>


<commit_before>class appdirs(PythonWig):
	git_uri = 'http://github.com/ActiveState/appdirs'
	tarball_uri = 'https://github.com/ActiveState/appdirs/archive/$RELEASE_VERSION$.tar.gz'
	last_release_version = 'v1.4.3'
<commit_msg>Delete appdirs.py<commit_after><|endoftext|>