text:
# -*- coding: utf-8 -*-
#
#  ColorHandPose3DNetwork - Network for estimating 3D Hand Pose from a single RGB Image
#  Copyright (C) 2017  Christian Zimmermann
#
#  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 2 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/>.
#
from __future__ import print_function, unicode_literals

import tensorflow as tf
from tensorflow.python import pywrap_tensorflow
import numpy as np
import math
import cv2

class NetworkOps(object):
    """ Operations that are frequently used within networks. """
    neg_slope_of_relu = 0.01

    @classmethod
    def leaky_relu(cls, tensor, name='relu'):
        out_tensor = tf.maximum(tensor, cls.neg_slope_of_relu*tensor, name=name)
        return out_tensor

    @classmethod
    def conv(cls, in_tensor, layer_name, kernel_size, stride, out_chan, trainable=True):
        with tf.variable_scope(layer_name):
            in_size = in_tensor.get_shape().as_list()

            strides = [1, stride, stride, 1]
            kernel_shape = [kernel_size, kernel_size, in_size[3], out_chan]

            # conv
            kernel = tf.get_variable('weights', kernel_shape, tf.float32,
                                     tf.contrib.layers.xavier_initializer_conv2d(), trainable=trainable, collections=['wd', 'variables', 'filters'])
            tmp_result = tf.nn.conv2d(in_tensor, kernel, strides, padding='SAME')

            # bias
            biases = tf.get_variable('biases', [kernel_shape[3]], tf.float32,
                                     tf.constant_initializer(0.0001), trainable=trainable, collections=['wd', 'variables', 'biases'])
            out_tensor = tf.nn.bias_add(tmp_result, biases, name='out')

            return out_tensor

    @classmethod
    def conv_relu(cls, in_tensor, layer_name, kernel_size, stride, out_chan, trainable=True):
        tensor = cls.conv(in_tensor, layer_name, kernel_size, stride, out_chan, trainable)
        out_tensor = cls.leaky_relu(tensor, name='out')
        return out_tensor

    @classmethod
    def max_pool(cls, bottom, name='pool'):
        pooled = tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                padding='VALID', name=name)
        return pooled

    @classmethod
    def upconv(cls, in_tensor, layer_name, output_shape, kernel_size, stride, trainable=True):
        with tf.variable_scope(layer_name):
            in_size = in_tensor.get_shape().as_list()

            kernel_shape = [kernel_size, kernel_size, in_size[3], in_size[3]]
            strides = [1, stride, stride, 1]

            # conv
            kernel = cls.get_deconv_filter(kernel_shape, trainable)
            tmp_result = tf.nn.conv2d_transpose(value=in_tensor, filter=kernel, output_shape=output_shape,
                                                strides=strides, padding='SAME')

            # bias
            biases = tf.get_variable('biases', [kernel_shape[2]], tf.float32,
                                     tf.constant_initializer(0.0), trainable=trainable, collections=['wd', 'variables', 'biases'])
            out_tensor = tf.nn.bias_add(tmp_result, biases)
            return out_tensor

    @classmethod
    def upconv_relu(cls, in_tensor, layer_name, output_shape, kernel_size, stride, trainable=True):
        tensor = cls.upconv(in_tensor, layer_name, output_shape, kernel_size, stride, trainable)
        out_tensor = cls.leaky_relu(tensor, name='out')
        return out_tensor

    @staticmethod
    def get_deconv_filter(f_shape, trainable):
        width = f_shape[0]
        height = f_shape[1]
        f = math.ceil(width/2.0)
        c = (2 * f - 1 - f % 2) / (2.0 * f)
        bilinear = np.zeros([f_shape[0], f_shape[1]])
        for x in range(width):
            for y in range(height):
                value = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
                bilinear[x, y] = value
        weights = np.zeros(f_shape)
        for i in range(f_shape[2]):
            weights[:, :, i, i] = bilinear

        init = tf.constant_initializer(value=weights,
                                       dtype=tf.float32)
        return tf.get_variable(name="weights", initializer=init,
                               shape=weights.shape, trainable=trainable, collections=['wd', 'variables', 'filters'])

    @staticmethod
    def fully_connected(in_tensor, layer_name, out_chan, trainable=True):
        with tf.variable_scope(layer_name):
            in_size = in_tensor.get_shape().as_list()
            assert len(in_size) == 2, 'Input to a fully connected layer must be a vector.'
            weights_shape = [in_size[1], out_chan]

            # weight matrix
            weights = tf.get_variable('weights', weights_shape, tf.float32,
                                     tf.contrib.layers.xavier_initializer(), trainable=trainable)
            weights = tf.check_numerics(weights, 'weights: %s' % layer_name)

            # bias
            biases = tf.get_variable('biases', [out_chan], tf.float32,
                                     tf.constant_initializer(0.0001), trainable=trainable)
            biases = tf.check_numerics(biases, 'biases: %s' % layer_name)

            out_tensor = tf.matmul(in_tensor, weights) + biases
            return out_tensor

    @classmethod
    def fully_connected_relu(cls, in_tensor, layer_name, out_chan, trainable=True):
        tensor = cls.fully_connected(in_tensor, layer_name, out_chan, trainable)
        out_tensor = tf.maximum(tensor, cls.neg_slope_of_relu*tensor, name='out')
        return out_tensor

    @staticmethod
    def dropout(in_tensor, keep_prob, evaluation):
        """ Dropout: Each neuron is dropped independently. """
        with tf.variable_scope('dropout'):
            tensor_shape = in_tensor.get_shape().as_list()
            out_tensor = tf.cond(evaluation,
                                 lambda: tf.nn.dropout(in_tensor, 1.0,
                                                       noise_shape=tensor_shape),
                                 lambda: tf.nn.dropout(in_tensor, keep_prob,
                                                       noise_shape=tensor_shape))
            return out_tensor

    @staticmethod
    def spatial_dropout(in_tensor, keep_prob, evaluation):
        """ Spatial dropout: Not each neuron is dropped independently, but feature map wise. """
        with tf.variable_scope('spatial_dropout'):
            tensor_shape = in_tensor.get_shape().as_list()
            out_tensor = tf.cond(evaluation,
                                 lambda: tf.nn.dropout(in_tensor, 1.0,
                                                       noise_shape=tensor_shape),
                                 lambda: tf.nn.dropout(in_tensor, keep_prob,
                                                       noise_shape=[tensor_shape[0], 1, 1, tensor_shape[3]]))
            return out_tensor

def crop_image_from_xy(image, crop_location, crop_size, scale=1.0):
    """
    Crops an image. When factor is not given does an central crop.

    Inputs:
        image: 4D tensor, [batch, height, width, channels] which will be cropped in height and width dimension
        crop_location: tensor, [batch, 2] which represent the height and width location of the crop
        crop_size: int, describes the extension of the crop
    Outputs:
        image_crop: 4D tensor, [batch, crop_size, crop_size, channels]
    """
    with tf.name_scope('crop_image_from_xy'):
        s = image.get_shape().as_list()
        assert len(s) == 4, "Image needs to be of shape [batch, width, height, channel]"
        scale = tf.reshape(scale, [-1])
        crop_location = tf.cast(crop_location, tf.float32)
        crop_location = tf.reshape(crop_location, [s[0], 2])
        crop_size = tf.cast(crop_size, tf.float32)

        crop_size_scaled = crop_size / scale
        y1 = crop_location[:, 0] - crop_size_scaled//2
        y2 = y1 + crop_size_scaled
        x1 = crop_location[:, 1] - crop_size_scaled//2
        x2 = x1 + crop_size_scaled
        y1 /= s[1]
        y2 /= s[1]
        x1 /= s[2]
        x2 /= s[2]
        boxes = tf.stack([y1, x1, y2, x2], -1)

        crop_size = tf.cast(tf.stack([crop_size, crop_size]), tf.int32)
        box_ind = tf.range(s[0])
        image_c = tf.image.crop_and_resize(tf.cast(image, tf.float32), boxes, box_ind, crop_size, name='crop')
        return image_c

def find_max_location(scoremap):
    """ Returns the coordinates of the given scoremap with maximum value. """
    with tf.variable_scope('find_max_location'):
        s = scoremap.get_shape().as_list()
        if len(s) == 4:
            scoremap = tf.squeeze(scoremap, [3])
        if len(s) == 2:
            scoremap = tf.expand_dims(scoremap, 0)

        s = scoremap.get_shape().as_list()
        assert len(s) == 3, "Scoremap must be 3D."
        assert (s[0] < s[1]) and (s[0] < s[2]), "Scoremap must be [Batch, Width, Height]"

        # my meshgrid
        x_range = tf.expand_dims(tf.range(s[1]), 1)
        y_range = tf.expand_dims(tf.range(s[2]), 0)
        X = tf.tile(x_range, [1, s[2]])
        Y = tf.tile(y_range, [s[1], 1])

        x_vec = tf.reshape(X, [-1])
        y_vec = tf.reshape(Y, [-1])
        scoremap_vec = tf.reshape(scoremap, [s[0], -1])
        max_ind_vec = tf.cast(tf.argmax(scoremap_vec, dimension=1), tf.int32)

        xy_loc = list()
        for i in range(s[0]):
            x_loc = tf.reshape(x_vec[max_ind_vec[i]], [1])
            y_loc = tf.reshape(y_vec[max_ind_vec[i]], [1])
            xy_loc.append(tf.concat([x_loc, y_loc], 0))

        xy_loc = tf.stack(xy_loc, 0)
        return xy_loc

def single_obj_scoremap(scoremap):
    """ Applies my algorithm to figure out the most likely object from a given segmentation scoremap. """
    with tf.variable_scope('single_obj_scoremap'):
        filter_size = 21
        s = scoremap.get_shape().as_list()
        assert len(s) == 4, "Scoremap must be 4D."

        scoremap_softmax = tf.nn.softmax(scoremap)  #B, H, W, C --> normalizes across last dimension
        scoremap_fg = tf.reduce_max(scoremap_softmax[:, :, :, 1:], 3) # B, H, W
        detmap_fg = tf.round(scoremap_fg) # B, H, W

        # find maximum in the fg scoremap
        max_loc = find_max_location(scoremap_fg)

        # use maximum to start "growing" our objectmap
        objectmap_list = list()
        kernel_dil = tf.ones((filter_size, filter_size, 1)) / float(filter_size*filter_size)
        for i in range(s[0]):
            # create initial objectmap (put a one at the maximum)
            sparse_ind = tf.reshape(max_loc[i, :], [1, 2])  # reshape that its one point with 2dim)
            objectmap = tf.sparse_to_dense(sparse_ind, [s[1], s[2]], 1.0)

            # grow the map by dilation and pixelwise and
            num_passes = max(s[1], s[2]) // (filter_size//2) # number of passes needes to make sure the map can spread over the whole image
            for j in range(num_passes):
                objectmap = tf.reshape(objectmap, [1, s[1], s[2], 1])
                objectmap_dil = tf.nn.dilation2d(objectmap, kernel_dil, [1, 1, 1, 1], [1, 1, 1, 1], 'SAME')
                objectmap_dil = tf.reshape(objectmap_dil, [s[1], s[2]])
                objectmap = tf.round(tf.multiply(detmap_fg[i, :, :], objectmap_dil))

            objectmap = tf.reshape(objectmap, [s[1], s[2], 1])
            objectmap_list.append(objectmap)

        objectmap = tf.stack(objectmap_list)

        return objectmap

def calc_center_bb(binary_class_mask):
    """ Returns the center of mass coordinates for the given binary_class_mask. """
    with tf.variable_scope('calc_center_bb'):
        binary_class_mask = tf.cast(binary_class_mask, tf.int32)
        binary_class_mask = tf.equal(binary_class_mask, 1)
        s = binary_class_mask.get_shape().as_list()
        if len(s) == 4:
            binary_class_mask = tf.squeeze(binary_class_mask, [3])

        s = binary_class_mask.get_shape().as_list()
        assert len(s) == 3, "binary_class_mask must be 3D."
        assert (s[0] < s[1]) and (s[0] < s[2]), "binary_class_mask must be [Batch, Width, Height]"

        # my meshgrid
        x_range = tf.expand_dims(tf.range(s[1]), 1)
        y_range = tf.expand_dims(tf.range(s[2]), 0)
        X = tf.tile(x_range, [1, s[2]])
        Y = tf.tile(y_range, [s[1], 1])

        bb_list = list()
        center_list = list()
        crop_size_list = list()
        for i in range(s[0]):
            X_masked = tf.cast(tf.boolean_mask(X, binary_class_mask[i, :, :]), tf.float32)
            Y_masked = tf.cast(tf.boolean_mask(Y, binary_class_mask[i, :, :]), tf.float32)

            x_min = tf.reduce_min(X_masked)
            x_max = tf.reduce_max(X_masked)
            y_min = tf.reduce_min(Y_masked)
            y_max = tf.reduce_max(Y_masked)

            start = tf.stack([x_min, y_min])
            end = tf.stack([x_max, y_max])
            bb = tf.stack([start, end], 1)
            bb_list.append(bb)

            center_x = 0.5*(x_max + x_min)
            center_y = 0.5*(y_max + y_min)
            center = tf.stack([center_x, center_y], 0)

            center = tf.cond(tf.reduce_all(tf.is_finite(center)), lambda: center,
                                  lambda: tf.constant([160.0, 160.0]))
            center.set_shape([2])
            center_list.append(center)

            crop_size_x = x_max - x_min
            crop_size_y = y_max - y_min
            crop_size = tf.expand_dims(tf.maximum(crop_size_x, crop_size_y), 0)
            crop_size = tf.cond(tf.reduce_all(tf.is_finite(crop_size)), lambda: crop_size,
                                  lambda: tf.constant([100.0]))
            crop_size.set_shape([1])
            crop_size_list.append(crop_size)

        bb = tf.stack(bb_list)
        center = tf.stack(center_list)
        crop_size = tf.stack(crop_size_list)

        return center, bb, crop_size

def detect_keypoints(scoremaps):
    """ Performs detection per scoremap for the hands keypoints. """
    if len(scoremaps.shape) == 4:
        scoremaps = np.squeeze(scoremaps)
    s = scoremaps.shape
    assert len(s) == 3, "This function was only designed for 3D Scoremaps."
    assert (s[2] < s[1]) and (s[2] < s[0]), "Probably the input is not correct, because [H, W, C] is expected."

    keypoint_coords = np.zeros((s[2], 2))
    for i in range(s[2]):
        v, u = np.unravel_index(np.argmax(scoremaps[:, :, i]), (s[0], s[1]))
        keypoint_coords[i, 0] = v
        keypoint_coords[i, 1] = u
    return keypoint_coords

def trafo_coords(keypoints_crop_coords, centers, scale, crop_size):
    """ Transforms coords into global image coordinates. """
    keypoints_coords = np.copy(keypoints_crop_coords)

    keypoints_coords -= crop_size // 2

    keypoints_coords /= scale

    keypoints_coords += centers

    return keypoints_coords

def plot_hand(coords_hw, axis, color_fixed=None, linewidth='1'):
    """ Plots a hand stick figure into a matplotlib figure. """
    colors = np.array([[0., 0., 0.5],
                       [0., 0., 0.73172906],
                       [0., 0., 0.96345811],
                       [0., 0.12745098, 1.],
                       [0., 0.33137255, 1.],
                       [0., 0.55098039, 1.],
                       [0., 0.75490196, 1.],
                       [0.06008855, 0.9745098, 0.90765338],
                       [0.22454143, 1., 0.74320051],
                       [0.40164453, 1., 0.56609741],
                       [0.56609741, 1., 0.40164453],
                       [0.74320051, 1., 0.22454143],
                       [0.90765338, 1., 0.06008855],
                       [1., 0.82861293, 0.],
                       [1., 0.63979666, 0.],
                       [1., 0.43645606, 0.],
                       [1., 0.2476398, 0.],
                       [0.96345811, 0.0442992, 0.],
                       [0.73172906, 0., 0.],
                       [0.5, 0., 0.]])

    # define connections and colors of the bones
    bones = [((0, 4), colors[0, :]),
             ((4, 3), colors[1, :]),
             ((3, 2), colors[2, :]),
             ((2, 1), colors[3, :]),

             ((0, 8), colors[4, :]),
             ((8, 7), colors[5, :]),
             ((7, 6), colors[6, :]),
             ((6, 5), colors[7, :]),

             ((0, 12), colors[8, :]),
             ((12, 11), colors[9, :]),
             ((11, 10), colors[10, :]),
             ((10, 9), colors[11, :]),

             ((0, 16), colors[12, :]),
             ((16, 15), colors[13, :]),
             ((15, 14), colors[14, :]),
             ((14, 13), colors[15, :]),

             ((0, 20), colors[16, :]),
             ((20, 19), colors[17, :]),
             ((19, 18), colors[18, :]),
             ((18, 17), colors[19, :])]

    for connection, color in bones:
        coord1 = coords_hw[connection[0], :]
        coord2 = coords_hw[connection[1], :]
        coords = np.stack([coord1, coord2])
        if color_fixed is None:
            axis.plot(coords[:, 1], coords[:, 0], color=color, linewidth=linewidth)
        else:
            axis.plot(coords[:, 1], coords[:, 0], color_fixed, linewidth=linewidth)

def plot_hand_3d(coords_xyz, axis, color_fixed=None, linewidth='1'):
    """ Plots a hand stick figure into a matplotlib figure. """
    colors = np.array([[0., 0., 0.5],
                       [0., 0., 0.73172906],
                       [0., 0., 0.96345811],
                       [0., 0.12745098, 1.],
                       [0., 0.33137255, 1.],
                       [0., 0.55098039, 1.],
                       [0., 0.75490196, 1.],
                       [0.06008855, 0.9745098, 0.90765338],
                       [0.22454143, 1., 0.74320051],
                       [0.40164453, 1., 0.56609741],
                       [0.56609741, 1., 0.40164453],
                       [0.74320051, 1., 0.22454143],
                       [0.90765338, 1., 0.06008855],
                       [1., 0.82861293, 0.],
                       [1., 0.63979666, 0.],
                       [1., 0.43645606, 0.],
                       [1., 0.2476398, 0.],
                       [0.96345811, 0.0442992, 0.],
                       [0.73172906, 0., 0.],
                       [0.5, 0., 0.]])

    # define connections and colors of the bones
    bones = [((0, 4), colors[0, :]),
             ((4, 3), colors[1, :]),
             ((3, 2), colors[2, :]),
             ((2, 1), colors[3, :]),

             ((0, 8), colors[4, :]),
             ((8, 7), colors[5, :]),
             ((7, 6), colors[6, :]),
             ((6, 5), colors[7, :]),

             ((0, 12), colors[8, :]),
             ((12, 11), colors[9, :]),
             ((11, 10), colors[10, :]),
             ((10, 9), colors[11, :]),

             ((0, 16), colors[12, :]),
             ((16, 15), colors[13, :]),
             ((15, 14), colors[14, :]),
             ((14, 13), colors[15, :]),

             ((0, 20), colors[16, :]),
             ((20, 19), colors[17, :]),
             ((19, 18), colors[18, :]),
             ((18, 17), colors[19, :])]

    for connection, color in bones:
        coord1 = coords_xyz[connection[0], :]
        coord2 = coords_xyz[connection[1], :]
        coords = np.stack([coord1, coord2])
        if color_fixed is None:
            axis.plot(coords[:, 0], coords[:, 1], coords[:, 2], color=color, linewidth=linewidth)
        else:
            axis.plot(coords[:, 0], coords[:, 1], coords[:, 2], color_fixed, linewidth=linewidth)

    axis.view_init(azim=-90., elev=90.)


text:
from django.db import models

class alumno(models.Model):

    alum_id = models.AutoField(primary_key=True)
    alum_nom = models.CharField(max_length=100, help_text="Nombre del alumno", unique=True)
    alum_ape = models.CharField(max_length=100, help_text="Apellido del alumno", unique=True)

    def __str__(self):
        return '{}'.format(self.alum_nom, self.alum_ape)

    class Meta:
        db_table = "alumno"
        verbose_name_plural="alumnos"

class curso(models.Model):

    cur_id = models.AutoField(primary_key=True)
    cur_nom = models.CharField(max_length=100, help_text="Nombre del alumno", unique=True)

    def __str__(self):
        return '{}'.format(self.cur_nom)

    class Meta:
        db_table = "curso"
        verbose_name_plural="cursos"

class alm_cur(models.Model):

    almcur_id = models.AutoField(primary_key=True)
    alum_id = models.ForeignKey(alumno, on_delete=models.CASCADE)
    cur_id = models.ForeignKey(curso, on_delete=models.CASCADE)

    def __str__(self):
        return '{}'.format(self.almcur_id)

    class Meta:
        db_table = "alm_cur"
        verbose_name_plural="alm_cursos"

class asistencia(models.Model):

    asis_id = models.AutoField(primary_key=True)
    asis_fecha = models.Date()
    asis_est = models.BooleanField(default=False)
    almcur_id = models.ForeignKey(alm_cur, on_delete=models.CASCADE)

    def __str__(self):
        return '{}'.format(self.asis_fecha, self.asis_est)

    class Meta:
        db_table = "asistencia"
        verbose_name_plural="asistencias"

domain: python


text:
"""general utility functions for HTML Map templates"""

def safe_quotes(text, escape_single_quotes=False):
    """htmlify string"""
    if isinstance(text, str):
        safe_text = text.replace('"', """)
        if escape_single_quotes:
            safe_text = safe_text.replace("'", "\'")
        return safe_text.replace('True', 'true')
    return text

def quote_filter(value):
    return safe_quotes(value.unescape())

def iframe_size_filter(value):
    if isinstance(value, str):
        return value

    return '%spx;' % value

def clear_none_filter(value):
    return dict(filter(lambda item: item[1] is not None, value.items()))


text:
"""
This module contains the lambda function code for put-storage-tags API.

This file uses environment variables in place of config; thus
sddcapi_boot_dir is not required.
"""

# pylint: disable=import-error,logging-format-interpolation,broad-except,too-many-statements,C0413,W1203,R1703,R0914
import boto3
import botocore.exceptions
import os
import sys
import json
import traceback
from cloudx_sls_authorization import lambda_auth

THISDIR = os.path.dirname(__file__)  # boto3-proxy
APPDIR = os.path.dirname(THISDIR)  # boto3-proxy

if APPDIR not in sys.path:
    sys.path.append(APPDIR)
if THISDIR not in sys.path:
    sys.path.append(THISDIR)

from utils import api_request
from utils import helpers, secrets
from utils.exceptions import InvalidRegionException, InvalidInputException
from utils.log_helper import Logger

logger = Logger()

# Define LDAP lookup configs
LDAP_SERVER = os.environ['LDAP_SERVER']
LDAP_USERNAME = os.environ['LDAP_USERNAME']
LDAP_PASSWORD_SECRET_NAME = os.environ['LDAP_PASSWORD_SECRET_NAME']
LDAP_SEARCH_BASE = os.environ['LDAP_SEARCH_BASE']
LDAP_OBJECT_CLASS = os.environ['LDAP_OBJECT_CLASS']
LDAP_GROUP_NAME = os.environ['LDAP_GROUP_NAME'].split(',')
LDAP_LOOKUP_ATTRIBUTE = os.environ['LDAP_LOOKUP_ATTRIBUTE']
MSFT_IDP_TENANT_ID = os.environ['MSFT_IDP_TENANT_ID']
MSFT_IDP_APP_ID = os.environ['MSFT_IDP_APP_ID'].split(',')
MSFT_IDP_CLIENT_ROLES = os.environ['MSFT_IDP_CLIENT_ROLES'].split(',')

# Status codes
SUCCESS_STATUS = 200  # success
BAD_REQUEST_STATUS = 400  # service not supported, action not supported
NOT_FOUND_STATUS = 404  # invalid account, invalid region
UNAUTHORIZED_STATUS = 401  # invalid auth token
INTERNAL_SERVICE_ERROR_STATUS = 500  # internal service error

def handler(event, context):
    """
    Boto3 Proxy API Handler
    """
    headers = event.get('Headers', event.get('headers'))
    if 'request-context-id' in headers:
        logger.set_uuid(headers['request-context-id'])
    logger.info({"Incoming event": event})
    logger.info('Incoming context: %s', context)

    request_body = json.loads(event.get('body', {}))

    try:
        # Define service client
        secrets_client = boto3.client('secretsmanager')
        lambda_auth.authorize_lambda_request(event, MSFT_IDP_TENANT_ID, MSFT_IDP_APP_ID,
                                             MSFT_IDP_CLIENT_ROLES, LDAP_SERVER, LDAP_USERNAME,
                                             secrets.retrieve_ldap_password(secrets_client,
                                                                            logger,
                                                                            LDAP_PASSWORD_SECRET_NAME
                                                                            ),
                                             LDAP_SEARCH_BASE,
                                             LDAP_OBJECT_CLASS, LDAP_GROUP_NAME, LDAP_LOOKUP_ATTRIBUTE)

        # Get the SSM client
        ssm_client = boto3.client('ssm')

    except Exception as e:
        traceback.print_exc()
        return {
            'statusCode': UNAUTHORIZED_STATUS,
            'body': json.dumps({'error': f"Unauthorized. {str(e)}"})
        }

    # Get environment variables
    resp_headers = {
        'Content-Type': 'application/json',
        "request-context-id": logger.get_uuid()
    }
    if hasattr(context, 'local_test'):
        logger.info('Running at local')
    path_params = event.get('pathParameters', {})
    request_headers = event.get('headers', {})
    vpcxiam_endpoint = os.environ.get('vpcxiam_endpoint')
    vpcxiam_scope = os.environ.get('vpcxiam_scope')
    vpcxiam_host = os.environ.get('vpcxiam_host')

    # Set the default success response and status code
    resp = {
        'message': 'API action has been successfully completed'
    }
    status_code = SUCCESS_STATUS

    try:
        account = path_params.get('account-id')
        region = path_params.get('region-name')
        service = path_params.get('boto3-service')
        action = path_params.get('boto3-action')
        logger.info(f"Account: {account}")
        logger.info(f"Region: {region}")
        logger.info(f"Boto3 Service: {service}")
        logger.info(f"Boto3 Action: {action}")

        # is authorized?
        logger.info(f'is_authorized({request_headers}, {MSFT_IDP_APP_ID}, '
                    f'{MSFT_IDP_TENANT_ID}, {MSFT_IDP_CLIENT_ROLES}')

        # Get the credentials for the account resources will be created in.
        url = (vpcxiam_endpoint +
               f"/v1/accounts/{account}/roles/admin/credentials")
        scope = vpcxiam_scope
        additional_headers = {
            'Host': vpcxiam_host
        }
        api_requests = api_request.ApiRequests()
        credentials = json.loads(
            (api_requests.request(url=url, method='get', scope=scope, additional_headers=additional_headers)).text
        )
        error = credentials.get('error', {})
        if error:
            logger.error(error)
            raise ValueError(error)
        credentials = credentials.get('credentials', {})
        try:
            # Validate service and if valid, get the allowed actions
            ssm_parameter_name = '/vpcx/aws/boto3-proxy/allowed-actions/'+service
            logger.info("Looking up parameter "+ssm_parameter_name)
            allowed_actions = ssm_client.get_parameter(Name=ssm_parameter_name)
        except botocore.exceptions.ClientError as err:
            logger.error(err)
            if err.response['Error']['Code'] == 'ParameterNotFound':
                raise InvalidInputException("Service " + service + " is not an allowed service for the API")
            else:
                raise error

        # Validate action
        if action not in allowed_actions['Parameter']['Value']:
            raise InvalidInputException("Action "+action+" is not an allowed action for the API")

        # Validate region
        ec2_client = boto3.client(
            service_name='ec2',
            aws_access_key_id=credentials.get('AccessKeyId', ''),
            aws_secret_access_key=credentials.get('SecretAccessKey', ''),
            aws_session_token=credentials.get('SessionToken', ''))
        helpers.is_region_valid(ec2_client, region)
        logger.info(f"{region} is a valid region")

        # Create clients for the given region and given account
        boto3_client = boto3.client(
            service_name=service,
            region_name=region,
            aws_access_key_id=credentials.get('AccessKeyId', ''),
            aws_secret_access_key=credentials.get('SecretAccessKey', ''),
            aws_session_token=credentials.get('SessionToken', ''))

        # Call the action (function) for the boto3 client's service with the request params
        kwargs = request_body
        getattr(boto3_client, action)(**kwargs)

    # boto3 error
    except botocore.exceptions.ClientError as err:
        status_code = INTERNAL_SERVICE_ERROR_STATUS
        resp = {
            'error': f'{type(err).__name__}: {err}'
        }
    except InvalidRegionException:
        status_code = NOT_FOUND_STATUS
        resp = {
            'error': 'Please enter a valid region in the url path'
        }
    except InvalidInputException as err:
        status_code = BAD_REQUEST_STATUS
        resp = {
            'error': str(err)
        }
    except ValueError as err:
        status_code = NOT_FOUND_STATUS
        resp = {
            'error': str(err)
        }
    except Exception as err:
        status_code = INTERNAL_SERVICE_ERROR_STATUS
        resp = {
            'error': f'{type(err).__name__}: {err}'
        }
    resp = helpers.lambda_returns(status_code, resp_headers, json.dumps(resp))
    logger.info(f'response: {resp}')
    return resp


text:
from setuptools import setup

setup(name='mws',
      version='0.2',
      description='Multi window sender',
      url='https://github.com/TheWorldOfCode/MWS',
      author='TheWorldOfCode',
      author_email='dannj75@gmail.com',
      install_requires=[
          "python-daemon>=2.2.4",
          "python-xlib>=0.27"
                       ],
      license='BSD',
      packages=['mws'],
      zip_safe=False)


text:
#!/usr/bin/env python
# encoding: utf-8

class MeanVariance(object):
    def __init__(self):
        self.n = 0
        self.K = 0.0
        self.ex0 = 0.0
        self.ex2 = 0.0

    def add_variable(self, x):
        if self.n == 0:
            self.K = x
        self.n += 1
        delta = x - self.K
        self.ex0 += delta
        self.ex2 += delta * delta

    def get_mean_value(self, ):
        return self.K + self.ex0 / self.n

    def get_variance(self, ):
        return (self.ex2 - (self.ex0 * self.ex0) / self.n) / (self.n - 1)

if __name__ == "__main__":
    mv = MeanVariance()
    mv.add_variable(1.0)
    mv.add_variable(2.0)
    mv.add_variable(3.0)
    mv.add_variable(4.0)
    print mv.get_mean_value()


text:
# small demo for listmode TOF MLEM without subsets

import os
import matplotlib.pyplot as py
import pyparallelproj as ppp
from pyparallelproj.wrapper import joseph3d_fwd, joseph3d_fwd_tof, joseph3d_back, joseph3d_back_tof
import numpy as np
import argparse
import ctypes

from time import time

#---------------------------------------------------------------------------------
# parse the command line

parser = argparse.ArgumentParser()
parser.add_argument('--ngpus',    help = 'number of GPUs to use', default = 0,   type = int)
parser.add_argument('--nsubsets', help = 'number of subsets',     default = 28,  type = int)
parser.add_argument('--tpb',      help = 'threads per block',     default = 64,  type = int)
parser.add_argument('--nontof',   help = 'non-TOF instead of TOF', action = 'store_true')
parser.add_argument('--img_mem_order', help = 'memory layout for image', default = 'C',
                                       choices = ['C','F'])
args = parser.parse_args()

#---------------------------------------------------------------------------------

ngpus     = args.ngpus
nsubsets  = args.nsubsets
tpb       = args.tpb
tof       = not args.nontof

img_mem_order = args.img_mem_order
subset        = 0

if tof:
  ntofbins = 27
else:
  ntofbins = 1

np.random.seed(1)

#---------------------------------------------------------------------------------

# setup a scanner with one ring
scanner = ppp.RegularPolygonPETScanner(ncrystals_per_module = np.array([16,9]),
                                       nmodules             = np.array([28,5]))

# setup a test image
voxsize = np.array([2.,2.,2.])
n0      = 250
n1      = 250
n2      = max(1,int((scanner.xc2.max() - scanner.xc2.min()) / voxsize[2]))

# setup a random image
img = np.zeros((n0,n1,n2), dtype = np.float32, order = img_mem_order)
img[(n0//6):(5*n0//6),(n1//6):(5*n1//6),:] = 1
img_origin = (-(np.array(img.shape) / 2) +  0.5) * voxsize

# generate sinogram parameters and the projector
sd = np.array([[0,1,2],
               [0,2,1],
               [1,2,0],
               [1,0,2],
               [2,0,1],
               [2,1,0]])

for sdo in sd:
  sino_params = ppp.PETSinogramParameters(scanner, ntofbins = ntofbins, tofbin_width = 23.,
                                          spatial_dim_order = sdo)
  proj        = ppp.SinogramProjector(scanner, sino_params, img.shape, nsubsets = nsubsets,
                                      voxsize = voxsize, img_origin = img_origin, ngpus = ngpus,
                                      tof = tof, sigma_tof = 60./2.35, n_sigmas = 3.,
                                      threadsperblock = tpb)

  # do a forward / back projection of subset 0 - same as img_fwd = proj.fwd_project(img, 0)
  # we just write out the single steps to time the python overhead separately

  #img_fwd = proj.fwd_project(img, 0)
  #ones_sino = np.ones(img_fwd.shape, dtype = np.float32)
  #back = proj.back_project(ones_sino, 0)

  subset_slice = proj.subset_slices[subset]

  sigma_tof = np.full(proj.nLORs[subset], proj.sigma_tof, dtype = ctypes.c_float).ravel()
  tofcenter_offset = np.zeros(proj.nLORs[subset], dtype = ctypes.c_float).ravel()

  xstart = proj.xstart[subset_slice].ravel()
  xend   = proj.xend[subset_slice].ravel()
  img_ravel = img.ravel(order = img_mem_order)
  subset_nLORs     = proj.nLORs[subset]

  img_fwd = np.zeros(subset_nLORs*proj.ntofbins, dtype = ctypes.c_float)

  back_img = np.zeros(proj.nvox, dtype = ctypes.c_float)
  sino     = np.ones(subset_nLORs*proj.ntofbins, dtype = ctypes.c_float)

  #--- time fwd projection
  t0 = time()
  if tof:
    ok = joseph3d_fwd_tof(xstart, xend, img_ravel, proj.img_origin, proj.voxsize,
                          img_fwd, subset_nLORs, proj.img_dim,
                          proj.tofbin_width, sigma_tof, tofcenter_offset,
                          proj.nsigmas, proj.ntofbins,
                          threadsperblock = proj.threadsperblock, ngpus = proj.ngpus, lm = False)
  else:
    ok = joseph3d_fwd(xstart, xend, img_ravel, proj.img_origin, proj.voxsize,
                      img_fwd, subset_nLORs, proj.img_dim,
                      threadsperblock = proj.threadsperblock, ngpus = proj.ngpus, lm = False)

  t1 = time()

  #--- time back projection
  t2 = time()
  if tof:
    ok = joseph3d_back_tof(xstart, xend, back_img, proj.img_origin, proj.voxsize,
                           sino, subset_nLORs, proj.img_dim,
                           proj.tofbin_width, sigma_tof, tofcenter_offset,
                           proj.nsigmas, proj.ntofbins,
                           threadsperblock = proj.threadsperblock, ngpus = proj.ngpus, lm = False)
  else:
    ok = joseph3d_back(xstart, xend, back_img, proj.img_origin, proj.voxsize,
                       sino, subset_nLORs, proj.img_dim,
                       threadsperblock = proj.threadsperblock, ngpus = proj.ngpus, lm = False)
  t3 = time()

  print(f'{sdo} {t1-t0} {t3-t2}')


text:
#   -*- coding: utf-8 -*-
#
#   This file is part of PyBuilder
#
#   Copyright 2011-2015 PyBuilder Team
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import unittest

from pybuilder.errors import BuildFailedException
from pybuilder.plugins.python.test_plugin_helper import ReportsProcessor
from test_utils import Mock, patch

class ReportsProcessorTests(unittest.TestCase):
    def setUp(self):
        self.reports_processor = ReportsProcessor(Mock(), Mock())
        total_time = Mock()
        total_time.get_millis.return_value = 42
        self.reports_processor.process_reports([], total_time)

    def test_should_raise_exception_when_not_all_tests_pass(self):
        self.reports_processor.tests_failed = 1

        self.assertRaises(BuildFailedException, self.reports_processor.write_report_and_ensure_all_tests_passed)

    def test_should_not_raise_exception_when_all_tests_pass(self):
        self.reports_processor.tests_failed = 0

        self.reports_processor.write_report_and_ensure_all_tests_passed()

    @patch("pybuilder.plugins.python.test_plugin_helper.render_report", return_value='rendered-report')
    def test_should_write_report(self, render_report):
        self.reports_processor.write_report_and_ensure_all_tests_passed()

        self.reports_processor.project.write_report.assert_called_with("integrationtest.json", 'rendered-report')

    def test_should_parse_reports(self):
        reports = [
            {'test': 'name1', 'test_file':
                'file1', 'success': False, 'time': 1},
            {'test': 'name2', 'test_file':
                'file2', 'success': False, 'time': 2},
            {'test': 'name3', 'test_file':
                'file3', 'success': True, 'time': 3},
            {'test': 'name4', 'test_file': 'file4', 'success': True, 'time': 4}
        ]
        self.reports_processor.process_reports(reports, Mock())

        self.assertEqual(self.reports_processor.tests_failed, 2)
        self.assertEqual(self.reports_processor.tests_executed, 4)

    def test_should_create_test_report_with_attributes(self):
        mock_time = Mock()
        mock_time.get_millis.return_value = 42

        self.reports_processor.process_reports([], mock_time)
        self.reports_processor.tests_failed = 4
        self.reports_processor.tests_executed = 42
        self.reports_processor.reports = ['a', 'b', 'c']

        self.assertEqual(self.reports_processor.test_report,
                         {
                             'num_of_tests': 42,
                             'success': False,
                             'tests': ['a', 'b', 'c'],
                             'tests_failed': 4,
                             'time': 42
                         }
                         )


text:
from kafka import KafkaProducer
from json import dumps as json_dumps, load as json_load
import time

class ProducerServer(KafkaProducer):

    def __init__(self, input_file, topic, **kwargs):
        super().__init__(**kwargs)
        self.input_file = input_file
        self.topic = topic

    def generate_data(self):
        with open(self.input_file) as f:
            data = json_load(f)
            for line in data:
                message = self.dict_to_binary(line)
                self.send(self.topic, message)

    def dict_to_binary(self, json_dict):
        return json_dumps(json_dict).encode('utf-8')


text:
# stdlib
import json
from typing import List
from typing import NoReturn
from typing import Optional

# third party
from fastapi import APIRouter
from fastapi import Depends
from fastapi import File
from fastapi import Form
from fastapi import UploadFile
from loguru import logger
from starlette import status
from starlette.exceptions import HTTPException

# grid absolute
from grid.api.dependencies.current_user import get_current_user
from grid.api.users.models import ApplicantStatus
from grid.api.users.models import User
from grid.api.users.models import UserCandidate
from grid.api.users.models import UserCreate
from grid.api.users.models import UserPrivate
from grid.api.users.models import UserUpdate

# relative
from . import syft as syft_user_messages

def raise_generic_private_error() -> NoReturn:
    raise HTTPException(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        detail="There was an error processing your request.",
    )

router = APIRouter()

@router.get("/me", response_model=User, name="users:me", status_code=status.HTTP_200_OK)
def get_self(current_user: UserPrivate = Depends(get_current_user)) -> User:
    return current_user

# TODO: Syft should return the newly created user and the response model should be User.
@router.post("", name="users:create", status_code=status.HTTP_201_CREATED)
async def create_user_grid(
    current_user: UserPrivate = Depends(get_current_user),
    new_user: str = Form(...),
    file: Optional[UploadFile] = File(None),
) -> str:
    if file:
        pdf_file = file.file.read()  # type: ignore
    else:
        pdf_file = b""

    dict_user = json.loads(new_user)
    dict_user["daa_pdf"] = pdf_file
    user_schema = UserCreate(**dict_user)
    try:
        return syft_user_messages.create_user(user_schema, current_user)
    except Exception as err:
        logger.error(err)
        raise_generic_private_error()

@router.get("/applicants", name="users:applicants", status_code=status.HTTP_201_CREATED)
async def get_all_candidates(
    current_user: UserPrivate = Depends(get_current_user),
) -> List[UserCandidate]:
    try:
        return syft_user_messages.get_user_requests(current_user)
    except Exception as err:
        logger.error(err)
        raise_generic_private_error()

@router.patch(
    "/applicants/{candidate_id}",
    name="users:applicants:process",
    status_code=status.HTTP_201_CREATED,
)
async def process_applicant_request(
    candidate_id: int,
    request_status: ApplicantStatus,
    current_user: UserPrivate = Depends(get_current_user),
) -> str:
    try:
        return syft_user_messages.process_applicant_request(
            current_user=current_user,
            candidate_id=candidate_id,
            status=request_status.status,
        )
    except Exception as err:
        logger.error(err)
        raise_generic_private_error()

@router.get(
    "",
    response_model=List[User],
    name="users:read_all",
    status_code=status.HTTP_200_OK,
)
async def get_all_users_grid(
    current_user: UserPrivate = Depends(get_current_user),
) -> List[User]:
    try:
        return syft_user_messages.get_all_users(current_user)
    except Exception as err:
        logger.error(err)
        raise_generic_private_error()

@router.get(
    "/{user_id}",
    response_model=User,
    name="users:read_one",
    status_code=status.HTTP_200_OK,
)
async def get_user_grid(
    user_id: int, current_user: UserPrivate = Depends(get_current_user)
) -> User:
    try:
        return syft_user_messages.get_user(user_id, current_user)
    except Exception as err:
        logger.error(err)
        raise_generic_private_error()

@router.patch(
    "/{user_id}",
    name="users:update",
    status_code=status.HTTP_204_NO_CONTENT,
)
async def update_user_grid(
    user_id: int,
    updated_user: UserUpdate,
    current_user: UserPrivate = Depends(get_current_user),
) -> None:
    try:
        syft_user_messages.update_user(user_id, current_user, updated_user)
    except Exception as err:
        logger.error(err)
        raise_generic_private_error()

@router.delete(
    "/{user_id}", name="users:delete", status_code=status.HTTP_204_NO_CONTENT
)
async def delete_user_grid(
    user_id: int, current_user: UserPrivate = Depends(get_current_user)
) -> None:
    try:
        syft_user_messages.delete_user(user_id, current_user)
    except Exception as err:
        logger.error(err)
        raise_generic_private_error()


text:
import tweepy

from .config import Config

def update_twitter_banner(api: tweepy.API) -> None:
    """Update the twitter banner of the current profile using the image specified in config."""
    api.update_profile_banner(Config.IMAGE_PATH)


text:
#!/usr/bin/env python

# $Id: update_pot.py 40713 2011-09-30 09:25:53Z nazgul $
# ***** BEGIN GPL LICENSE BLOCK *****
#
# 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 2
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENSE BLOCK *****

# <pep8 compliant>

# update the pot file according the POTFILES.in

import subprocess
import os
from codecs import open

GETTEXT_XGETTEXT_EXECUTABLE = "xgettext"
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.join(CURRENT_DIR, "..")))
DOMAIN = "blender"
COMMENT_PREFIX = "#~ "  # from update_msg.py

FILE_NAME_POT = os.path.join(CURRENT_DIR, "blender.pot")
FILE_NAME_MESSAGES = os.path.join(CURRENT_DIR, "messages.txt")

def main():
    cmd = (GETTEXT_XGETTEXT_EXECUTABLE,
           "--files-from=%s" % os.path.join(SOURCE_DIR, "po", "POTFILES.in"),
           "--keyword=_",
           "--keyword=N_",
           "--directory=%s" % SOURCE_DIR,
           "--output=%s" % os.path.join(SOURCE_DIR, "po", "%s.pot" % DOMAIN),
           "--from-code=utf-8",
           )

    print(" ".join(cmd))
    process = subprocess.Popen(cmd)
    process.wait()

    def stripeol(s):
        return s.rstrip("\n\r")

    pot_messages = {}
    reading_message = False
    message = ""
    with open(FILE_NAME_POT, 'r', "utf-8") as handle:
        while True:
            line = handle.readline()

            if not line:
                break

            line = stripeol(line)
            if line.startswith("msgid"):
                reading_message = True
                message = line[7:-1]
            elif line.startswith("msgstr"):
                reading_message = False
                pot_messages[message] = True
            elif reading_message:
                message += line[1:-1]

    # add messages collected automatically from RNA
    with open(FILE_NAME_POT, "a", "utf-8") as pot_handle:
        with open(FILE_NAME_MESSAGES, 'r', "utf-8") as handle:
            msgsrc_ls = []
            while True:
                line = handle.readline()

                if not line:
                    break

                line = stripeol(line)

                # COMMENT_PREFIX
                if line.startswith(COMMENT_PREFIX):
                    msgsrc_ls.append(line[len(COMMENT_PREFIX):].strip())
                else:
                    line = line.replace("\\", "\\\\")
                    line = line.replace("\"", "\\\"")
                    line = line.replace("\t", "\\t")

                    if not pot_messages.get(line):
                        for msgsrc in msgsrc_ls:
                            pot_handle.write("#: %s\n" % msgsrc)
                        pot_handle.write("msgid \"%s\"\n" % line)
                        pot_handle.write("msgstr \"\"\n\n")
                    msgsrc_ls[:] = []

if __name__ == "__main__":
    print("\n\n *** Running %r *** \n" % __file__)
    main()


text:
from setuptools import setup

setup(
    name='expressvpn-python',
    version='1.1',
    packages=['expressvpn'],
    install_requires=['Flask','flask_restful'],
    url='https://github.com/philipperemy/expressvpn-python',
    license='MIT',
    author='Philippe Remy',
    author_email='premy.enseirb@gmail.com',
    description='ExpressVPN - Python Wrapper'
)


text:
from typing import Literal

from bolinette import data, types
from bolinette.data import mapping
from bolinette.exceptions import InternalError
from bolinette.utils.functions import getattr_, hasattr_, invoke

class Mapper:
    def __init__(self):
        self._payloads: dict[str, dict[str, mapping.Definition]] = {}
        self._responses: dict[str, dict[str, mapping.Definition]] = {}

    @staticmethod
    def _get_def(
        collection: dict[str, dict[str, mapping.Definition]], model_name: str, key: str
    ) -> "mapping.Definition":
        m = collection.get(model_name)
        if m is None:
            raise InternalError(f"mapping.unknown_model:{model_name}")
        d = m.get(key)
        if d is None:
            raise InternalError(f"mapping.unknown_definition:{model_name}.{key}")
        return d

    def payload(self, model_name: str, key: str):
        return self._get_def(self._payloads, model_name, key)

    @property
    def payloads(self):
        for model_name in self._payloads:
            for key in self._payloads[model_name]:
                yield model_name, key, self._payloads[model_name][key]

    def response(self, model_name: str, key: str):
        return self._get_def(self._responses, model_name, key)

    @property
    def responses(self):
        for model_name in self._responses:
            for key in self._responses[model_name]:
                yield model_name, key, self._responses[model_name][key]

    def _extract_defs(
        self,
        model: "data.Model",
        model_cls: type["data.Model"],
        collection: Literal["payloads", "responses"],
        merge_defs: Literal["ignore", "append", "overwrite"],
    ):
        defs = {}
        for parent in model_cls.__bases__:
            if issubclass(parent, data.Model) and parent != data.Model:
                for _key, _def in self._extract_defs(
                    model, parent, collection, merge_defs
                ).items():
                    defs[_key] = _def
        def_func = getattr(model_cls, collection)
        if hasattr_(def_func, "__func__"):
            def_func = def_func.__func__
        def_gen = def_func(model)
        if def_gen is None:
            return defs
        new_defs = list(def_gen)
        for _def in new_defs:
            if isinstance(_def, list):
                model_key = "default"
                payload = _def
            else:
                model_key, payload = _def
            if model_key in defs:
                if merge_defs == "append":
                    for _param in payload:
                        defs[model_key].append(_param)
                elif merge_defs == "overwrite":
                    defs[model_key] = payload
            else:
                defs[model_key] = payload
        return defs

    def register(self, model: "data.Model"):
        def create_defs(collection, attr_name: Literal["payloads", "responses"]):
            defs = self._extract_defs(
                model, type(model), attr_name, model.__blnt__.merge_defs
            )
            for model_key, payload in defs.items():
                definition = mapping.Definition(model.__blnt__.name, model_key)
                for field in payload:
                    definition.fields.append(field)
                if definition.model_name not in collection:
                    collection[definition.model_name] = {}
                collection[definition.model_name][definition.model_key] = definition

        create_defs(self._payloads, "payloads")
        create_defs(self._responses, "responses")

    def marshall(
        self,
        definition,
        entity,
        *,
        skip_none=False,
        as_list=False,
        use_foreign_key=False,
    ):
        if entity is None:
            return None
        if as_list:
            return [
                self.marshall(
                    definition,
                    e,
                    skip_none=skip_none,
                    as_list=False,
                    use_foreign_key=use_foreign_key,
                )
                for e in entity
            ]
        values = {}
        for field in definition.fields:
            self._marshall_object(values, field, entity, skip_none, use_foreign_key)
        return values

    def _marshall_object(
        self, values, field, entity, skip_none: bool, use_foreign_key: bool
    ):
        if isinstance(field, mapping.Field):
            self._marshall_field(values, field, entity, skip_none)
        elif isinstance(field, mapping.Reference) and use_foreign_key:
            values[field.foreign_key] = getattr_(entity, field.foreign_key, None)
        elif isinstance(field, mapping.Definition):
            self._marshall_definition(values, field, entity, skip_none, use_foreign_key)
        elif isinstance(field, mapping.List):
            self._marshall_list(values, field, entity, skip_none, use_foreign_key)

    @staticmethod
    def _marshall_field(values, field: "mapping.Field", entity, skip_none: bool):
        if field.function is not None:
            value = field.function(entity)
        else:
            value = getattr_(entity, field.key, None)
        if field.formatting is not None:
            value = field.formatting(value)
        if not skip_none or value is not None:
            values[field.name] = value

    def _marshall_definition(
        self,
        values,
        definition: "mapping.Definition",
        entity,
        skip_none: bool,
        use_foreign_key: bool,
    ):
        d = self.response(definition.model_name, definition.model_key)
        attr = None
        if definition.function and callable(definition.function):
            attr = definition.function(entity)
        elif hasattr_(entity, definition.name):
            attr = getattr_(entity, definition.name, None)
        values[definition.name] = self.marshall(
            d, attr, skip_none=skip_none, as_list=False, use_foreign_key=use_foreign_key
        )

    def _marshall_list(
        self,
        values,
        field: "mapping.List",
        entity,
        skip_none: bool,
        use_foreign_key: bool,
    ):
        if field.function and callable(field.function):
            e_list = invoke(field.function, entity)
        else:
            e_list = getattr_(entity, field.name, None)
        elem = field.element
        if isinstance(elem, types.db.DataType):
            values[field.name] = [e for e in e_list]
        elif isinstance(elem, mapping.Definition):
            d = self.response(elem.model_name, elem.model_key)
            values[field.name] = self.marshall(
                d,
                e_list,
                skip_none=skip_none,
                as_list=True,
                use_foreign_key=use_foreign_key,
            )


text:
from selenium import webdriver
from fixture.session import SessionHelper
from fixture.group import GroupHelper
from fixture.contact import ContactHelper

class Application:

    def __init__(self, browser, base_url):
        if browser == "firefox":
            self.wd = webdriver.Firefox(capabilities={"marionette": False}, firefox_binary="C:/Program Files/Mozilla Firefox/firefox.exe")
        elif browser == "chrome":
            self.wd = webdriver.Chrome()
        elif browser == "ie":
            self.wd = webdriver.Ie()
        else:
            raise ValueError("Unrecognized browser %s" % browser)
        self.session = SessionHelper (self)
        self.group = GroupHelper (self)
        self.contact = ContactHelper(self)
        self.base_url = base_url

    def is_valid (self):
        try:
            self.wd.current_url
            return True
        except:
            return False

    def open_home_page(self):
        wd = self.wd
        wd.get(self.base_url)

    def destroy (self):
        self.wd.quit()


text:
"""
Tests for pagination template tags and filters.

"""
from mock import Mock

from django import template

from tests import case

class PaginateTest(case.DBTestCase):
    """Tests for paginate template tag."""
    def test_paginate(self):
        """Places Pager object in context with size/num from request."""
        from moztrap.model.tags.models import Tag

        tpl = template.Template(
            "{% load pagination %}{% paginate queryset as pager %}"
            "{% for obj in pager.objects %}{{ obj }} {% endfor %}")

        request = Mock()
        request.GET = {"pagesize": 3, "pagenumber": 2}

        for i in range(1, 7):
            self.F.TagFactory.create(name=str(i))
        qs = Tag.objects.all()

        output = tpl.render(
            template.Context({"request": request, "queryset": qs}))

        self.assertEqual(output, "4 5 6 ")

class FilterTest(case.TestCase):
    """Tests for template filters."""
    def test_pagenumber_url(self):
        """``pagenumber_url`` filter updates pagenumber in URL."""
        from moztrap.view.lists.templatetags.pagination import pagenumber_url
        request = Mock()
        request.get_full_path.return_value = (
            "http://localhost/?pagenumber=2&pagesize=10")
        self.assertEqual(
            pagenumber_url(request, 1),
            "http://localhost/?pagenumber=1&pagesize=10")

    def test_pagesize_url(self):
        """``pagesize_url`` updates pagesize in URL (and jumps to page 1)."""
        from moztrap.view.lists.templatetags.pagination import pagesize_url
        request = Mock()
        request.get_full_path.return_value = (
            "http://localhost/?pagenumber=2&pagesize=10")
        self.assertEqual(
            pagesize_url(request, 20),
            "http://localhost/?pagenumber=1&pagesize=20")

    def test_pagenumber(self):
        """``pagenumber`` gets the pagenumber from the request."""
        from moztrap.view.lists.templatetags.pagination import pagenumber
        request = Mock()
        request.GET = {"pagenumber": 2, "pagesize": 10}
        self.assertEqual(pagenumber(request), 2)

    def test_pagesize(self):
        """``pagenumber`` gets the pagenumber from the request."""
        from moztrap.view.lists.templatetags.pagination import pagesize
        request = Mock()
        request.GET = {"pagenumber": 2, "pagesize": 10}
        self.assertEqual(pagesize(request), 10)


text:
import torch
from torchvision.transforms import functional as TFF
import matplotlib.pyplot as plt
from theseus.base.trainer.supervised_trainer import SupervisedTrainer
from theseus.utilities.loading import load_state_dict
from theseus.classification.utilities.gradcam import CAMWrapper, show_cam_on_image
from theseus.utilities.visualization.visualizer import Visualizer
from theseus.utilities.analysis.analyzer import ClassificationAnalyzer

from theseus.utilities.loggers.observer import LoggerObserver
LOGGER = LoggerObserver.getLogger("main")

class ClassificationTrainer(SupervisedTrainer):
    """Trainer for classification tasks

    """
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def check_best(self, metric_dict):
        """
        Hook function, called after metrics are calculated
        """
        if metric_dict['bl_acc'] > self.best_value:
            if self.iters > 0: # Have been training, else in evaluation-only mode or just sanity check
                LOGGER.text(
                    f"Evaluation improved from {self.best_value} to {metric_dict['bl_acc']}",
                    level=LoggerObserver.INFO)
                self.best_value = metric_dict['bl_acc']
                self.save_checkpoint('best')

            else:
                if self.visualize_when_val:
                    self.visualize_pred()

    def save_checkpoint(self, outname='last'):
        """
        Save all information of the current iteration
        """
        weights = {
            'model': self.model.model.state_dict(),
            'optimizer': self.optimizer.state_dict(),
            'iters': self.iters,
            'best_value': self.best_value,
        }

        if self.scaler is not None:
            weights[self.scaler.state_dict_key] = self.scaler.state_dict()

        self.checkpoint.save(weights, outname)

    def load_checkpoint(self, path:str):
        """
        Load all information the current iteration from checkpoint
        """
        LOGGER.text("Loading checkpoints...", level=LoggerObserver.INFO)
        state_dict = torch.load(path, map_location='cpu')
        self.iters = load_state_dict(self.iters, state_dict, 'iters')
        self.best_value = load_state_dict(self.best_value, state_dict, 'best_value')
        self.scaler = load_state_dict(self.scaler, state_dict, self.scaler.state_dict_key)

    def visualize_gt(self):
        """
        Visualize dataloader for sanity check
        """

        LOGGER.text("Visualizing dataset...", level=LoggerObserver.DEBUG)
        visualizer = Visualizer()

        # Train batch
        batch = next(iter(self.trainloader))
        images = batch["inputs"]

        batch = []
        for idx, inputs in enumerate(images):
            img_show = visualizer.denormalize(inputs)
            img_cam = TFF.to_tensor(img_show)
            batch.append(img_cam)
        grid_img = visualizer.make_grid(batch)

        fig = plt.figure(figsize=(8,8))
        plt.axis('off')
        plt.imshow(grid_img)
        plt.tight_layout(pad=0)
        LOGGER.log([{
            'tag': "Sanitycheck/batch/train",
            'value': fig,
            'type': LoggerObserver.FIGURE,
            'kwargs': {
                'step': self.iters
            }
        }])

        # Validation batch
        batch = next(iter(self.valloader))
        images = batch["inputs"]

        batch = []
        for idx, inputs in enumerate(images):
            img_show = visualizer.denormalize(inputs)
            img_cam = TFF.to_tensor(img_show)
            batch.append(img_cam)
        grid_img = visualizer.make_grid(batch)

        fig = plt.figure(figsize=(8,8))
        plt.axis('off')
        plt.imshow(grid_img)
        plt.tight_layout(pad=0)

        LOGGER.log([{
            'tag': "Sanitycheck/batch/val",
            'value': fig,
            'type': LoggerObserver.FIGURE,
            'kwargs': {
                'step': self.iters
            }
        }])

    @torch.enable_grad() #enable grad for CAM
    def visualize_pred(self):
        r"""Visualize model prediction and CAM

        """
        # Vizualize Grad Class Activation Mapping and model predictions
        LOGGER.text("Visualizing model predictions...", level=LoggerObserver.DEBUG)

        visualizer = Visualizer()

        batch = next(iter(self.valloader))
        images = batch["inputs"]
        targets = batch["targets"]

        self.model.eval()

        model_name = self.model.model.name
        grad_cam = CAMWrapper.get_method(
            name='gradcam',
            model=self.model.model.get_model(),
            model_name=model_name, use_cuda=next(self.model.parameters()).is_cuda)

        grayscale_cams, label_indices, scores = grad_cam(images, return_probs=True)

        gradcam_batch = []
        pred_batch = []
        for idx in range(len(grayscale_cams)):
            image = images[idx]
            target = targets[idx].item()
            label = label_indices[idx]
            grayscale_cam = grayscale_cams[idx, :]
            score = scores[idx]

            img_show = visualizer.denormalize(image)
            visualizer.set_image(img_show)
            if self.valloader.dataset.classnames is not None:
                label = self.valloader.dataset.classnames[label]
                target = self.valloader.dataset.classnames[target]

            if label == target:
                color = [0,1,0]
            else:
                color = [1,0,0]

            visualizer.draw_label(
                f"GT: {target}\nP: {label}\nC: {score:.4f}",
                fontColor=color,
                fontScale=0.8,
                thickness=2,
                outline=None,
                offset=100
            )

            img_cam =show_cam_on_image(img_show, grayscale_cam, use_rgb=True)

            img_cam = TFF.to_tensor(img_cam)
            gradcam_batch.append(img_cam)

            pred_img = visualizer.get_image()
            pred_img = TFF.to_tensor(pred_img)
            pred_batch.append(pred_img)

            if idx == 63: # limit number of images
                break

        # GradCAM images
        gradcam_grid_img = visualizer.make_grid(gradcam_batch)
        fig = plt.figure(figsize=(8,8))
        plt.imshow(gradcam_grid_img)
        plt.axis("off")
        plt.tight_layout(pad=0)
        LOGGER.log([{
            'tag': "Validation/gradcam",
            'value': fig,
            'type': LoggerObserver.FIGURE,
            'kwargs': {
                'step': self.iters
            }
        }])

        # Prediction images
        pred_grid_img = visualizer.make_grid(pred_batch)
        fig = plt.figure(figsize=(10,10))
        plt.imshow(pred_grid_img)
        plt.axis("off")
        plt.tight_layout(pad=0)
        LOGGER.log([{
            'tag': "Validation/prediction",
            'value': fig,
            'type': LoggerObserver.FIGURE,
            'kwargs': {
                'step': self.iters
            }
        }])

        # Zeroing gradients in optimizer for safety
        self.optimizer.zero_grad()

    @torch.no_grad()
    def visualize_model(self):
        # Vizualize Model Graph
        LOGGER.text("Visualizing architecture...", level=LoggerObserver.DEBUG)

        batch = next(iter(self.valloader))
        images = batch["inputs"].to(self.model.device)
        LOGGER.log([{
            'tag': "Sanitycheck/analysis/architecture",
            'value': self.model.model.get_model(),
            'type': LoggerObserver.TORCH_MODULE,
            'kwargs': {
                'inputs': images
            }
        }])

    def analyze_gt(self):
        """
        Perform simple data analysis
        """
        LOGGER.text("Analyzing datasets...", level=LoggerObserver.DEBUG)
        analyzer = ClassificationAnalyzer()
        analyzer.add_dataset(self.trainloader.dataset)
        fig = analyzer.analyze(figsize=(10,5))
        LOGGER.log([{
            'tag': "Sanitycheck/analysis/train",
            'value': fig,
            'type': LoggerObserver.FIGURE,
            'kwargs': {
                'step': self.iters
            }
        }])

        analyzer = ClassificationAnalyzer()
        analyzer.add_dataset(self.valloader.dataset)
        fig = analyzer.analyze(figsize=(10,5))
        LOGGER.log([{
            'tag': "Sanitycheck/analysis/val",
            'value': fig,
            'type': LoggerObserver.FIGURE,
            'kwargs': {
                'step': self.iters
            }
        }])

    def on_evaluate_end(self):
        if self.visualize_when_val:
            self.visualize_pred()
        self.save_checkpoint()

    def on_start(self):
        if self.resume is not None:
            self.load_checkpoint(self.resume)

    def sanitycheck(self):
        """Sanity check before training
        """
        self.visualize_gt()
        self.analyze_gt()
        self.visualize_model()
        self.evaluate_epoch()


text:
from typing import NamedTuple

from inari._internal import _format
from ward import each, test, using

class Fixtures(NamedTuple):
    raw_description: str
    attributes: str
    result: str

simple = Fixtures(
    raw_description="- var (`object`): Target object.",
    attributes="",
    result="- **var** (`object`): Target object.",
)
attr_and_emphasize = Fixtures(
    raw_description="* variables (`list[VariableCollector]`):"
    + " Class properties. **IMPORTANT!**",
    attributes="{: #ClassItself }",
    result="- **variables**{: #ClassItself } (`list[VariableCollector]`): Class "
    + "properties. **IMPORTANT!**",
)
multiline = Fixtures(
    raw_description="* var (`object`): Target object.\n"
    + "+ var (`object`): `backtick`\n\n"
    + "**Returns**\n"
    + "* `str`: Return type.\n",
    attributes="",
    result="- **var** (`object`): Target object.\n"
    + "- **var** (`object`): `backtick`\n\n"
    + "**Returns**\n"
    + "- `str`: Return type.\n",
)
no_description = Fixtures(
    raw_description="* no_description(`str`)",
    attributes="",
    result="- **no_description** (`str`)",
)

@test(
    "Description `` {raw_description} `` with `` {attributes} `` should be modified "
    + "into `` {result} `` ."
)
@using(
    raw_description=each(
        simple.raw_description,
        attr_and_emphasize.raw_description,
        multiline.raw_description,
        no_description.raw_description,
    ),
    attributes=each(
        simple.attributes,
        attr_and_emphasize.attributes,
        multiline.attributes,
        no_description.attributes,
    ),
    result=each(
        simple.result,
        attr_and_emphasize.result,
        multiline.result,
        no_description.result,
    ),
)
def _(raw_description: str, attributes: str, result: str) -> None:
    assert _format.modify_attrs(raw_description, attributes) == result


text:
import tensorflow as tf
if tf.__version__ > '2':
    import tensorflow.compat.v1 as tf

import model

def top_k_logits(logits, k):
    if k == 0:
        # no truncation
        return logits

    def _top_k():
        values, _ = tf.nn.top_k(logits, k=k)
        min_values = values[:, -1, tf.newaxis]
        return tf.where(
            logits < min_values,
            tf.ones_like(logits, dtype=logits.dtype) * -1e10,
            logits,
        )
    return tf.cond(
       tf.equal(k, 0),
       lambda: logits,
       lambda: _top_k(),
    )

def top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
    indices = tf.stack([
        tf.range(0, batch),
        # number of indices to include
        tf.maximum(tf.reduce_sum(tf.cast(cumulative_probs <= p, tf.int32), axis=-1) - 1, 0),
    ], axis=-1)
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(
        logits < min_values,
        tf.ones_like(logits) * -1e10,
        logits,
    )

def sample_sequence(*, hparams, length, start_token=None, batch_size=None, context=None, temperature=1, top_k=0, top_p=1):
    if start_token is None:
        assert context is not None, 'Specify exactly one of start_token and context!'
    else:
        assert context is None, 'Specify exactly one of start_token and context!'
        context = tf.fill([batch_size, 1], start_token)

    def step(hparams, tokens, past=None):
        lm_output = model.model(hparams=hparams, X=tokens, past=past, reuse=tf.AUTO_REUSE)

        logits = lm_output['logits'][:, :, :hparams.n_vocab]
        presents = lm_output['present']
        presents.set_shape(model.past_shape(hparams=hparams, batch_size=batch_size))
        return {
            'logits': logits,
            'presents': presents,
        }

    with tf.name_scope('sample_sequence'):
        def body(past, prev, output):
            next_outputs = step(hparams, prev, past=past)
            logits = next_outputs['logits'][:, -1, :]  / tf.to_float(temperature)
            logits = top_k_logits(logits, k=top_k)
            logits = top_p_logits(logits, p=top_p)
            samples = tf.multinomial(logits, num_samples=1, output_dtype=tf.int32)
            return [
                next_outputs['presents'] if past is None else tf.concat([past, next_outputs['presents']], axis=-2),
                samples,
                tf.concat([output, samples], axis=1)
            ]

        past, prev, output = body(None, context, context)

        def cond(*args):
            return True

        _, _, tokens = tf.while_loop(
            cond=cond, body=body,
            maximum_iterations=length - 1,
            loop_vars=[
                past,
                prev,
                output
            ],
            shape_invariants=[
                tf.TensorShape(model.past_shape(hparams=hparams, batch_size=batch_size)),
                tf.TensorShape([batch_size, None]),
                tf.TensorShape([batch_size, None]),
            ],
            back_prop=False,
        )

        return tokens


text:
from kfp.components import create_component_from_func, InputPath, OutputPath

def keras_convert_hdf5_model_to_tf_saved_model(
    model_path: InputPath('KerasModelHdf5'),
    converted_model_path: OutputPath('TensorflowSavedModel'),
):
    '''Converts Keras HDF5 model to Tensorflow SavedModel format.

    Args:
        model_path: Keras model in HDF5 format.
        converted_model_path: Keras model in Tensorflow SavedModel format.

    Annotations:
        author: Alexey Volkov <alexey.volkov@ark-kun.com>
    '''
    from pathlib import Path
    from tensorflow import keras

    model = keras.models.load_model(filepath=model_path)
    keras.models.save_model(model=model, filepath=converted_model_path, save_format='tf')

if __name__ == '__main__':
    keras_convert_hdf5_model_to_tf_saved_model_op = create_component_from_func(
        keras_convert_hdf5_model_to_tf_saved_model,
        base_image='tensorflow/tensorflow:2.3.0',
        packages_to_install=['h5py==2.10.0'],
        output_component_file='component.yaml',
        annotations={
            "author": "Alexey Volkov <alexey.volkov@ark-kun.com>",
            "canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/_converters/KerasModelHdf5/to_TensorflowSavedModel/component.yaml",
        },
    )


text:
import click

from . import __version__
from .cli_commands import create_cluster, upload, upload_and_update
from .configure import configure

def print_version(ctx, param, value):
    if not value or ctx.resilient_parsing:
        return
    click.echo('Version {}'.format(__version__))
    ctx.exit()

@click.group()
@click.option('--version', '-v', is_flag=True, callback=print_version,
              help=__version__)
def cli(version):
    pass

cli.add_command(configure)
cli.add_command(create_cluster)
cli.add_command(upload)
cli.add_command(upload_and_update)


text:
from collections import defaultdict
import pandas as pd
import pickle
from sqlalchemy import create_engine, inspect, Table, Column
from sqlalchemy.engine.url import make_url
from sys import exit

class DatabaseClient:

    """ Takes care of the database pass opening to find the url and can query
        the respected database.

        Input:
            dbpass_path     path to the text file with the list of database urls
            dbname          database name so we know which database to query from the list
    """

    def __init__(self, dbpass_path, dbname):
        self.dbpass_path = dbpass_path
        self.dbname = dbname
        self.db_url = self.get_db_url()
        self.engine = create_engine(self.db_url)

    def get_db_url(self):
        with open(self.dbpass_path, 'r') as infile:
            db_names = []
            for raw_url in infile.read().splitlines():
                url_obj = make_url(raw_url)
                if url_obj.database == self.dbname:
                    infile.close()
                    return raw_url
                db_names.append(url_obj.database)
        infile.close()
        exit('database name does not exist in dbpass given:' + ', '.join(db_names))

    def get_df_with_query(self, query):
        """ WARNING :: Will crash if too large. If so, you should just create the df file
            first via create_df_file(query=).

        load example:
            with open(input, 'rb') as infile:
                objs = []
                while True:
                    try:
                        obj = pickle.load(infile)
                    except EOFError:
                        break
                    ...
        """
        return pd.read_sql(query, self.engine)

    def create_df_file_with_query(self, query, output):
        """ Dumps in df in chunks to avoid crashes.
        """
        chunk_size = 100000
        offset = 0
        data = defaultdict(lambda : defaultdict(list))

        with open(output, 'wb') as outfile:
            query = query.replace(';', '')
            query += """ LIMIT {chunk_size} OFFSET {offset};"""
            while True:
                print(offset)
                query = query.format(
                    chunk_size=chunk_size,
                    offset=offset
                )
                df = pd.read_sql(query, self.engine)
                pickle.dump(df, outfile)
                offset += chunk_size
                if len(df) < chunk_size:
                    break
            outfile.close()


text:
from pomodoro import main

if __name__ == "__main__":
    main()


text:
import ipdb
import medis.speckle_nulling.sn_hardware as hardware
import medis.speckle_nulling.sn_preprocessing as pre
import numpy as np
import os
import astropy.io.fits as pf
import medis.speckle_nulling.sn_filehandling as flh
from configobj import ConfigObj

def build_median(imagelist, outputfile = None):
    """Takes a list of image paths and builds a median image"""
    first = True
    for image in imagelist:
        hdulist= pf.open(image)
        data = pre.combine_quadrants(hdulist)
        #data= hdulist[0].data
        #data = pre.combine_quadrants(data)
        #filesused.append(image+';   ')
        if first:
            imcube = data[:,:,np.newaxis]
            first = False
        else:
            np.concatenate((imcube, data[:,:,np.newaxis]), axis=2)
        hdulist.close()
    medimage = np.median(imcube, axis=2)
    if outputfile is not None:
        print "Writing median image to "+outputfile
        strfiles = [x+'; ' for x in imagelist]
        strfilesused = ("Files used to create master image:   "+
                        ''.join(strfiles))
        flh.writeout(medimage, outputfile,
                     comment = strfilesused)

    return medimage

def build_master_flat(mfminusmd, badpix=None,
                      kernelsize = 9,
                      outputfile = 'masterflat.fits',
                      removezeros = True):
    """removes bad pixels from a background subtracted master flat"""
    im1 = pre.removebadpix(mfminusmd, badpix, kernelsize=kernelsize)
    ans = im1/np.mean(im1)
    if removezeros:
        ans=pre.removebadpix(ans, ans==0, kernelsize = kernelsize)

    flh.writeout(ans, outputfile)
    return ans

def build_master_dark(rawdark, badpix = None, outputfile='masterdark.fits'):
    ans=pre.removebadpix(rawdark, badpix)
    flh.writeout(ans, outputfile)
    return ans

def build_badpixmask(image,
            method='gaussfit',outputfile = 'badpix.fits'):

    if method == 'gaussfit':
        masterbadpixelmask = pre.locate_badpix(image, sigmaclip = 2.5)
        print "Writing badpix image to "+outputfile
        flh.writeout(masterbadpixelmask, outputfile)
    return masterbadpixelmask

if __name__ == "__main__":
    hardwareconfigfile = 'speckle_instruments.ini'
    configfilename = 'speckle_null_config.ini'
    config = ConfigObj(configfilename)
    bgdconfig= config['BACKGROUNDS_CAL']
    outputdir = config['BACKGROUNDS_CAL']['dir']
    pharo = hardware.PHARO_COM('PHARO',
                configfile = hardwareconfigfile)

    print ("\n\n\n\nThis script is meant to tell PHARO to take a bunch of backgrounds,\n darks, and flats, then assemble them into the correctly formatted 1024x1024 region \nthat we care about, and place them in the following directory:")
    print config['BACKGROUNDS_CAL']['dir']

    print ('\n\n\n\nIf this script does not work, my advice would be to bypass it completely and do it manually take some flats, backgrounds and darks.  \nAssemble them yourselves (see sn_preprocessing.py), \nparticularly combine_quadrants, locate_badpix, and save them as masterflat.fits, masterdark.fits, badpix.fits in the same directory mentioned above')

    filetypes = ['backgrounds',
                 'flats', 'flatdarks']

    for ftype in filetypes:
        imnames = []
        commandstring = ("\n\n\n\nSet up Pharo to the configurations to take "+ftype.upper()+" then hit any key. ")
        s= raw_input(commandstring)

        if ftype == 'backgrounds':
            for i in range(int(config['BACKGROUNDS_CAL']['N'])):
                fname = pharo.take_src_return_imagename(
                            exptime = bgdconfig['bgdtime'])
                imnames.append(fname)
                print ftype.upper()+" taken so far: "
                print imnames
            background = build_median(imnames,
                           outputfile = os.path.join(outputdir, 'medbackground.fits'))
            ipdb.set_trace()
        if ftype == 'flats':

            for i in range(int(config['BACKGROUNDS_CAL']['N'])):
                fname = pharo.take_src_return_imagename(
                            exptime = bgdconfig['flattime'])
                imnames.append(fname)
                print ftype.upper()+" taken so far: "
                print imnames
            med_flat = build_median(imnames, outputfile=os.path.join(outputdir, 'medflat.fits'))
                #XXXX fix flat fielding
        if ftype == 'flatdarks':
            for i in range(int(config['BACKGROUNDS_CAL']['N'])):
                fname = pharo.take_src_return_imagename(
                            exptime = bgdconfig['flattime'])
                imnames.append(fname)
                print ftype.upper()+" taken so far: "
                print imnames
            med_flatdark = build_median(imnames, outputfile=os.path.join(outputdir, 'medflatdark.fits'))

    bp = build_badpixmask(med_flatdark,
        outputfile = os.path.join(outputdir,'badpix.fits'))
#bp  = build_badpixmask(targ_bkgd-cal_bkgd,
#    outputfile = os.path.join(outputdir,'badpix.fits'))
    mf = build_master_flat(med_flat-med_flatdark, badpix=bp,
        outputfile = os.path.join(outputdir,'masterflat.fits'))


text:
from typing import Text, Any, Dict, List, Union

from blinker import NamedSignal, signal
import rx
from rx import operators as ops
from dataclasses import dataclass

from sagas.nlu.pipes import pred_cond, filter_path, to_token
from sagas.util.collection_util import wrap, to_obj
import logging
logger = logging.getLogger(__name__)

cat_sig=signal('cat')
@cat_sig.connect
def cat_proc(sender, **kwargs):
    from sagas.nlu.utils import predicate
    from sagas.nlu.translator import trans_axis

    results=[]

    source = rx.of(*kwargs['rs'])
    lang = kwargs['lang']
    cond:pred_cond=kwargs['data']
    logger.debug(f"pred pos: {cond}")

    kind=cond.cond
    logger.debug(f"lang: {lang}, cond: {cond}")
    source.pipe(
        filter_path(cond.part),
        ops.map(lambda t: to_obj({'word': t.text if t.upos.lower() in ['adj'] else t.lemma, **t})),
        ops.map(lambda t: to_obj({'trans': trans_axis(t.word, lang, t.upos), **t})),
        ops.filter(lambda t: predicate(kind, t.trans, 'en', '*')),

        ops.map(lambda t: {'path':t.path,
                           'word': t.word,
                           'trans': t.trans,
                           'cat': kind,
                           'value': kind,
                           'pos': t.upos.lower()}),
    ).subscribe(
        on_next=lambda value: results.append({**value}),
        on_error=lambda e: logger.error(e),
    )
    logger.debug(f"result: {results}")
    return results


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

# Define here the models for your spider middleware
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals

class TutorialSpiderMiddleware(object):
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the spider middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_spider_input(self, response, spider):
        # Called for each response that goes through the spider
        # middleware and into the spider.

        # Should return None or raise an exception.
        return None

    def process_spider_output(self, response, result, spider):
        # Called with the results returned from the Spider, after
        # it has processed the response.

        # Must return an iterable of Request, dict or Item objects.
        for i in result:
            yield i

    def process_spider_exception(self, response, exception, spider):
        # Called when a spider or process_spider_input() method
        # (from other spider middleware) raises an exception.

        # Should return either None or an iterable of Response, dict
        # or Item objects.
        pass

    def process_start_requests(self, start_requests, spider):
        # Called with the start requests of the spider, and works
        # similarly to the process_spider_output() method, except
        # that it doesn’t have a response associated.

        # Must return only requests (not items).
        for r in start_requests:
            yield r

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)

class TutorialDownloaderMiddleware(object):
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the downloader middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.

        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        return None

    def process_response(self, request, response, spider):
        # Called with the response returned from the downloader.

        # Must either;
        # - return a Response object
        # - return a Request object
        # - or raise IgnoreRequest
        return response

    def process_exception(self, request, exception, spider):
        # Called when a download handler or a process_request()
        # (from other downloader middleware) raises an exception.

        # Must either:
        # - return None: continue processing this exception
        # - return a Response object: stops process_exception() chain
        # - return a Request object: stops process_exception() chain
        pass

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)


text:
# -*- coding: utf-8 -*-
import scrapy
from city_scrapers.spider import Spider
from datetime import datetime, timedelta
from dateutil.parser import parse as dateparse
import re

class Chi_school_community_action_councilSpider(Spider):
    name = 'chi_school_community_action_council'
    long_name = 'Chicago Public Schools Community Action Council'
    allowed_domains = ['cps.edu']
    start_urls = ['http://cps.edu/FACE/Pages/CAC.aspx']

    def parse(self, response):
        """
        `parse` should always `yield` a dict that follows the Event Schema
        <https://city-bureau.github.io/city-scrapers/06_event_schema.html>.

        Change the `_parse_id`, `_parse_name`, etc methods to fit your scraping
        needs.
        """
        month_counter = datetime.today().month # Sets month counter to the current month, which is passed to parse_start
        for x in range(12): # iterates through every month in the year after the current month
            if month_counter > 12:
                break
            else:
                for item in response.css("ul").css('li')[17:]:
                    try:
                        if item.css("strong").css("a::attr(href)").extract()[0] == 'http://www.humboldtparkportal.org/':
                            continue
                    except:
                        pass

                    data = {
                        '_type': 'event',
                        'name': self._parse_name(item),
                        'description': self._parse_description(item),
                        'classification': self._parse_classification(item),
                        'start_time': self._parse_start(item, month_counter),
                        'end_time': self._parse_end(item),
                        'timezone': self._parse_timezone(item),
                        'status': self._parse_status(item),
                        'all_day': self._parse_all_day(item),
                        'location': self._parse_location(item),
                        'sources': self._parse_sources(response),
                        'community_area' : self._parse_community_area(item)
                    }

                    data['id'] = self._generate_id(data)
                    data['end_time'] = data['start_time'] + timedelta(hours=3) #adds 3 hours to start time
                    yield data
            month_counter += 1  # month counter is increased by 1 month with each iteration of the for loop

    # self._parse_next(response) yields more responses to parse if necessary.
    # uncomment to find a "next" url
    # yield self._parse_next(response)

    def _parse_community_area(self, item):
        """
        Parse or generate community area.
        """
        if len(item.css('li').css('strong::text').extract()) == 1:
            community_name = item.css('li').css('strong::text').extract()
        else:
            community_name = item.css('li').css('strong').css('a::text').extract()
        return community_name[0]

    def _parse_name(self, item):
        """
        Parse or generate event name.
        """
        if len(item.css('li').css('strong::text').extract()) == 1:
            community_name = item.css('li').css('strong::text').extract()
        else:
            community_name = item.css('li').css('strong').css('a::text').extract()
        return community_name[0] + ' Community Action Council'

    def _parse_description(self, item):
        """
        Parse or generate event description.
        """
        return "Community Action Councils, or CACs, consist of 25-30 voting members who are " \
                                  "directly involved in developing a strategic plan for educational success within " \
                                  "their communities. CAC members include parents; elected officials; faith-based " \
                                  "institutions, health care and community-based organizations; Local School" \
                                  " Council (LSC) members; business leaders; educators and school administrators; " \
                                  "staff members from Chicago's Sister Agencies; community residents; " \
                                  "and students. There are nine CACs across Chicago. Each works to empower the " \
                                  "community they serve to lead the improvement of local quality education."

    def _parse_classification(self, item):
        """
        Parse or generate classification (e.g. public health, education, etc).
        """
        return 'Education'

    def _parse_start(self, item, month_counter):
        """
        Parse start date and time.

        Accepts month_counter as an argument from top level parse function to iterate through all months in the year.
        """

        def parse_day(source):
            '''Parses the source material and retrieves the day of the week that the meeting occurs.
            '''
            day_source = source[0]
            day_regex = re.compile(r'[a-zA-Z]+day')
            mo = day_regex.search(day_source)
            return mo.group().lower()

        def parse_time(source):
            '''Parses the source material and retrieves the time that the meeting occurs.
            '''
            time_source = source[1]
            time_regex = re.compile(r'(1[012]|[1-9]):[0-5][0-9](am|pm)')
            mo = time_regex.search(time_source)
            return mo.group()

        def count_days(day, week_count):
            '''Because the source material provides meeting dates on a reoccuring schedule, we must use the parsed day
            from the parse_day function and the '''
            today = datetime.today()
            week_day = {'monday': 0, 'tuesday': 1, 'wednesday': 2, 'thursday': 3, 'friday': 4, 'saturday': 5,
                        'sunday': 6}
            week_counter = 0
            for x in range(1, 31):
                try:
                    current_date = datetime(today.year, month_counter, x) #uses month counter from top level parse func.
                    if current_date.weekday() == week_day[day]:
                        week_counter += 1
                        if week_counter == int(week_count):
                            return current_date
                except ValueError as e:  # will break loop if range exceeds the number of days in the month
                    break

        def concat_date(meeting_date, time):
            '''Combines the meeting date with the time the meeting occurs. Function return a datetime
            object.
            '''
            return dateparse(
                str(meeting_date.year) + '-' + str(meeting_date.month) + '-' + str(meeting_date.day) + ' ' + time)

        def get_start(source):
            '''Combines above defined parse_day, parse_time, count_days, and concat_date functions to get the start
             date from the source. If a start time cannot be found the UNIX epoch date is returned.
             '''
            day = parse_day(source)
            week_count = source[0].strip()[
                0]  # selects first character in the source, which is usually the week count
            if week_count.isdigit():
                time = parse_time(source)
                meeting_date = count_days(day, week_count)
                start = concat_date(meeting_date, time)
            else:
                pass
            return start

        source = item.css('li::text').extract()

        return get_start(source)

    def _parse_end(self, item):
        """
        Parse end date and time.
        """
        return 'Estimated 3 hours'

    def _parse_timezone(self, item):
        """
        Parse or generate timzone in tzinfo format.
        """
        return 'America/Chicago'

    def _parse_all_day(self, item):
        """
        Parse or generate all-day status. Defaults to False.
        """
        return False

    def _parse_location(self, item):
        """
        Parse or generate location. Latitude and longitude can be
        left blank and will be geocoded later.
        """
        source = item.css('li::text').extract()[1]
        return {
            'url': None,
            'name': source[source.find("at")+2:source.find("(")].replace('the', ''),
            'address': source[source.find("(")+1:source.find(")")],
            'coordinates': {
                'latitude': None,
                'longitude': None,
            },
        }

    def _parse_status(self, item):
        """
        Parse or generate status of meeting. Can be one of:
        * cancelled
        * tentative
        * confirmed
        * passed
        By default, return "tentative"
        """
        return 'Tentative'

    def _parse_sources(self, response):
        """
        Parse or generate sources.
        """
        return [{
            'url': response.url,
            'note': '',
        }]


text:
# This file is part of Indico.
# Copyright (C) 2002 - 2021 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.

from __future__ import unicode_literals

from sqlalchemy.ext.declarative import declared_attr

from indico.core.db.sqlalchemy import db
from indico.core.db.sqlalchemy.util.models import auto_table_args
from indico.core.settings.models.base import JSONSettingsBase, PrincipalSettingsBase
from indico.util.decorators import strict_classproperty
from indico.util.string import return_ascii

class CoreSettingsMixin(object):
    @strict_classproperty
    @staticmethod
    def __auto_table_args():
        return (db.Index(None, 'module', 'name'),
                {'schema': 'indico'})

class Setting(JSONSettingsBase, CoreSettingsMixin, db.Model):
    @strict_classproperty
    @staticmethod
    def __auto_table_args():
        return db.UniqueConstraint('module', 'name'),

    @declared_attr
    def __table_args__(cls):
        return auto_table_args(cls)

    @return_ascii
    def __repr__(self):
        return '<Setting({}, {}, {!r})>'.format(self.module, self.name, self.value)

class SettingPrincipal(PrincipalSettingsBase, CoreSettingsMixin, db.Model):
    principal_backref_name = 'in_settings_acls'

    @declared_attr
    def __table_args__(cls):
        return auto_table_args(cls)

    @return_ascii
    def __repr__(self):
        return '<SettingPrincipal({}, {}, {!r})>'.format(self.module, self.name, self.principal)


text:
import bpy
track = bpy.context.edit_movieclip.tracking.tracks.active

track.color = (0.0, 1.0, 0.0)
track.use_custom_color = True


text:
from setuptools import find_packages, setup

setup(
    name='src',
    packages=find_packages(),
    version='0.1.0',
    description='wafer project using mlops',
    author='monika',
    license='MIT',
)