<commit_before>import datetime
import numbers
from types import GeneratorType

import netCDF4
import numpy
import pytz
import six
from django.core.exceptions import ObjectDoesNotExist
from fiona.collection import Collection
from shapely.geometry.base import BaseGeometry

from ncdjango.geoprocessing.data import Raster
from ncdjango.models import Service
from ncdjango.utils import best_fit
from ncdjango.views import NetCdfDatasetMixin

class ParameterNotValidError(ValueError):
    """Indicates that a value is not valid for the parameter type"""

class ParameterBase(type):
    """Parameter metaclass, used to register parameter classes for lookup by name."""

    _parameters_by_id = {}

    def __new__(mcs, name, bases, attrs):
        new_class = super(ParameterBase, mcs).__new__(mcs, name, bases, attrs)

        name = getattr(new_class, 'id', None)
        if name:
            mcs._parameters_by_id[new_class.id] = new_class

        setattr(new_class, '_parameters_by_id', mcs._parameters_by_id)

        return new_class

class Parameter(six.with_metaclass(ParameterBase)):
    """
    Base parameter (input, output, or uniform) class for a task or workflow. Extended to implement specific parameter
    types.
    """

    id = None  # A unique name for this parameter, used for serialization

    def __init__(self, name, required=True):
        """
        :param name: The parameter name. This will be used as a keyword argument to the task's `execute()` method.
        :param required: When True, input validation will fail if no value is provided.
        """

        self.name = name
        self.required = required

    @classmethod
    def by_id(cls, param_id):
        return cls._parameters_by_id.get(param_id)

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        return value

    def serialize_args(self):
        """Returns (args, kwargs) to be used when deserializing this parameter."""

        return (self.name,), {'required': True}

    @staticmethod
    def deserialize_args(args, kwargs):
        """Returns deserialized forms of (args, kwargs) to be used when re-constructing this parameter."""

        return args, kwargs

class ParameterCollection(object):
    """Manages a collection of parameter types and values"""

    def __init__(self, parameters):
        """
        :param parameters: A list of `Parameter` objects
        """

        self.parameters = parameters
        self.by_name = {p.name: p for p in self.parameters}
        self.values = {}

    def __setitem__(self, key, value):
        self.values[key] = self.by_name[key].clean(value)

    def __getitem__(self, item):
        return self.values.get(item)

    @property
    def is_complete(self):
        """Do all required parameters have values?"""

        return all(p.name in self.values for p in self.parameters if p.required)

    def format_args(self):
        """Returns dictionary containing values for all named parameters."""

        return self.values

class AnyParameter(Parameter):
    """Accepts any value"""

    id = 'any'

class MultiParameter(Parameter):
    """
    Accepts a value matching one of several defined types.
    E.g., `MultiParameter([StringParameter(''), DictParameter('')])` will accept both strings and dictionaries.
    """

    id = 'multiple'

    def __init__(self, types, *args, **kwargs):
        """
        :param types: A list of `Parameter` instances to accept.
        """

        super(MultiParameter, self).__init__(*args, **kwargs)

        self.types = types

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        for parameter_obj in self.types:
            try:
                return parameter_obj.clean(value)
            except ParameterNotValidError:
                continue

        raise ParameterNotValidError

    def serialize_args(self):
        """Returns (args, kwargs) to be used when deserializing this parameter."""

        args, kwargs = super(MultiParameter, self).serialize_args()
        args.insert(0, [[t.id, t.serialize_args()] for t in self.types])

        return args, kwargs

    @staticmethod
    def deserialize_args(args, kwargs):
        args, kwargs = super(MultiParameter, MultiParameter).deserialize_args(args[1:], kwargs)

        types = []
        for type_id, type_args in args[0]:
            type_cls = Parameter.by_id(type_id)
            type_args, type_kwargs = type_cls.deserialize_args(*type_args)
            types.append(type_cls(*type_args, **type_kwargs))

        args.insert(0, types)

        return args, kwargs

class ListParameter(Parameter):
    """
    Accepts a list of a given parameter type. This parameter will accept either a list-type object (list, tuple, set)
    or a generator object. If it receives a generator object, `clean()` will also return a generator object. Otherwise,
    it will return a list.
    """

    id = 'list'

    def __init__(self, param_type, *args, **kwargs):
        """
        :param param_type: A `Parameter` instance.
        """

        super(ListParameter, self).__init__(*args, **kwargs)

        self.param_type = param_type

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if not isinstance(value, (list, tuple, set, GeneratorType)):
            value = [value]

        gen = (self.param_type.clean(x) for x in value)

        if isinstance(value, GeneratorType):
            return gen
        else:
            return list(gen)

    def serialize_args(self):
        """Returns (args, kwargs) to be used when deserializing this parameter."""

        args, kwargs = super(ListParameter, self).serialize_args()
        args.insert(0, [self.param_type.id, self.param_type.serialize_args()])

    @staticmethod
    def deserialize_args(args, kwargs):
        type_id, type_args = args[0]
        args, kwargs = super(ListParameter, ListParameter).deserialize_args(args[1:], kwargs)

        type_cls = Parameter.by_id(type_id)
        type_args, type_kwargs = type_cls.deserialize_args(*type_args)
        args.insert(0, type_cls(*type_args, **type_kwargs))

        return args, kwargs

class StringParameter(Parameter):
    """Accepts a string. Will convert numbers to string."""

    id = 'string'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, six.string_types):
            return value
        elif isinstance(value, numbers.Number):
            return str(value)

        raise ParameterNotValidError

class NumberParameter(Parameter):
    """Accepts a number. Will convert numeric strings."""

    id = 'number'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, numbers.Number):
            return value
        elif isinstance(value, six.string_types):
            try:
                value = float(value)
                return int(value) if value.is_integer() else value
            except ValueError:
                raise ParameterNotValidError

        raise ParameterNotValidError

class IntParameter(NumberParameter):
    """Accepts an integer value. Will convert strings, other numbers."""

    id = 'int'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        return int(super(IntParameter, self).clean(value))

class FloatParameter(NumberParameter):
    """Accepts a float value. Will convert strings, other numbers."""

    id = 'float'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        return float(super(FloatParameter, self).clean(value))

class BooleanParameter(Parameter):
    """Accepts a boolean value. Will cast other objects to bool. Will treat a string, "false" as False."""

    id = 'bool'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, six.string_types) and value.lower() == 'false':
            return False

        return bool(value)

class DictParameter(Parameter):
    """Accepts a dictionary"""

    id = 'dict'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, (dict, list)):
            return value

        raise ParameterNotValidError

class NdArrayParameter(Parameter):
    """Accepts a numpy array or Python list"""

    id = 'array'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, numpy.ndarray):
            return value
        elif isinstance(value, (list, tuple)):
            return numpy.array(value)

        raise ParameterNotValidError

class RasterParameter(Parameter):
    """Accepts a `Raster` instance"""

    id = 'raster'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, Raster):
            return value

        raise ParameterNotValidError

class FeatureParameter(Parameter):
    """Accepts a single feature (as a Shapely geometry object)"""

    id = 'feature'

    # Todo: geometry type checking

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, BaseGeometry):
            return value

        raise ParameterNotValidError

class FeatureCollectionParameter(FeatureParameter):
    """Accepts a list of features (as Shapely geometry objects)"""

    id = 'feature_collection'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, (list, tuple)):
            return [super(FeatureCollectionParameter, self).clean(x) for x in value]

        raise ParameterNotValidError

class RasterDatasetParameter(Parameter):
    """Accepts a NetCDF `Dataset` object"""

    id = 'raster_dataset'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, netCDF4.Dataset):
            return value

        raise ParameterNotValidError

class FeatureDatasetParameter(Parameter):
    """Accepts a Fiona `Collection` object"""

    id = 'feature_dataset'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, Collection):
            return value

        raise ParameterNotValidError

class RegisteredDatasetParameter(NetCdfDatasetMixin, Parameter):
    """Used by the web API to map registered datasets to inputs"""

    def __init__(self, *args, **kwargs):
        self.service = None

        super(RegisteredDatasetParameter, self).__init__(*args, **kwargs)

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if not isinstance(value, six.string_types):
            raise ParameterNotValidError

        try:
            source, value = value.split('://', 1)
        except ValueError:
            raise ParameterNotValidError

        if source == 'service':
            if '@' in value:
                value, timestamp = value.split('@', 1)

                try:
                    service_time = datetime.datetime.utcfromtimestamp(int(timestamp)).replace(tzinfo=pytz.utc)
                except ValueError:
                    raise ParameterNotValidError
            else:
                service_time = None

            if ':' in value:
                service_name, variable_name = value.split(':', 1)
            else:
                service_name = value
                variable_name = None

            try:
                self.service = Service.objects.get(name=service_name)
            except ObjectDoesNotExist:
                raise ParameterNotValidError("Service '{}' not found".format(service_name))

            if variable_name:
                try:
                    variable = self.service.variable_set.all().get(variable=variable_name)
                except ObjectDoesNotExist:
                    raise ParameterNotValidError("Variable '{}' not found".format(variable_name))

                if service_time is not None:
                    time_index = best_fit(variable.time_stops, service_time)
                else:
                    time_index = None
                print(time_index)
                data = self.get_grid_for_variable(variable, time_index=time_index)
                return Raster(data, variable.full_extent, 1, 0, self.is_y_increasing(variable))
            else:
                return self.dataset
        else:
            raise ParameterNotValidError('Invalid source: {}'.format(source))
<commit_msg>Remove print<commit_after>import datetime
import numbers
from types import GeneratorType

import netCDF4
import numpy
import pytz
import six
from django.core.exceptions import ObjectDoesNotExist
from fiona.collection import Collection
from shapely.geometry.base import BaseGeometry

from ncdjango.geoprocessing.data import Raster
from ncdjango.models import Service
from ncdjango.utils import best_fit
from ncdjango.views import NetCdfDatasetMixin

class ParameterNotValidError(ValueError):
    """Indicates that a value is not valid for the parameter type"""

class ParameterBase(type):
    """Parameter metaclass, used to register parameter classes for lookup by name."""

    _parameters_by_id = {}

    def __new__(mcs, name, bases, attrs):
        new_class = super(ParameterBase, mcs).__new__(mcs, name, bases, attrs)

        name = getattr(new_class, 'id', None)
        if name:
            mcs._parameters_by_id[new_class.id] = new_class

        setattr(new_class, '_parameters_by_id', mcs._parameters_by_id)

        return new_class

class Parameter(six.with_metaclass(ParameterBase)):
    """
    Base parameter (input, output, or uniform) class for a task or workflow. Extended to implement specific parameter
    types.
    """

    id = None  # A unique name for this parameter, used for serialization

    def __init__(self, name, required=True):
        """
        :param name: The parameter name. This will be used as a keyword argument to the task's `execute()` method.
        :param required: When True, input validation will fail if no value is provided.
        """

        self.name = name
        self.required = required

    @classmethod
    def by_id(cls, param_id):
        return cls._parameters_by_id.get(param_id)

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        return value

    def serialize_args(self):
        """Returns (args, kwargs) to be used when deserializing this parameter."""

        return (self.name,), {'required': True}

    @staticmethod
    def deserialize_args(args, kwargs):
        """Returns deserialized forms of (args, kwargs) to be used when re-constructing this parameter."""

        return args, kwargs

class ParameterCollection(object):
    """Manages a collection of parameter types and values"""

    def __init__(self, parameters):
        """
        :param parameters: A list of `Parameter` objects
        """

        self.parameters = parameters
        self.by_name = {p.name: p for p in self.parameters}
        self.values = {}

    def __setitem__(self, key, value):
        self.values[key] = self.by_name[key].clean(value)

    def __getitem__(self, item):
        return self.values.get(item)

    @property
    def is_complete(self):
        """Do all required parameters have values?"""

        return all(p.name in self.values for p in self.parameters if p.required)

    def format_args(self):
        """Returns dictionary containing values for all named parameters."""

        return self.values

class AnyParameter(Parameter):
    """Accepts any value"""

    id = 'any'

class MultiParameter(Parameter):
    """
    Accepts a value matching one of several defined types.
    E.g., `MultiParameter([StringParameter(''), DictParameter('')])` will accept both strings and dictionaries.
    """

    id = 'multiple'

    def __init__(self, types, *args, **kwargs):
        """
        :param types: A list of `Parameter` instances to accept.
        """

        super(MultiParameter, self).__init__(*args, **kwargs)

        self.types = types

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        for parameter_obj in self.types:
            try:
                return parameter_obj.clean(value)
            except ParameterNotValidError:
                continue

        raise ParameterNotValidError

    def serialize_args(self):
        """Returns (args, kwargs) to be used when deserializing this parameter."""

        args, kwargs = super(MultiParameter, self).serialize_args()
        args.insert(0, [[t.id, t.serialize_args()] for t in self.types])

        return args, kwargs

    @staticmethod
    def deserialize_args(args, kwargs):
        args, kwargs = super(MultiParameter, MultiParameter).deserialize_args(args[1:], kwargs)

        types = []
        for type_id, type_args in args[0]:
            type_cls = Parameter.by_id(type_id)
            type_args, type_kwargs = type_cls.deserialize_args(*type_args)
            types.append(type_cls(*type_args, **type_kwargs))

        args.insert(0, types)

        return args, kwargs

class ListParameter(Parameter):
    """
    Accepts a list of a given parameter type. This parameter will accept either a list-type object (list, tuple, set)
    or a generator object. If it receives a generator object, `clean()` will also return a generator object. Otherwise,
    it will return a list.
    """

    id = 'list'

    def __init__(self, param_type, *args, **kwargs):
        """
        :param param_type: A `Parameter` instance.
        """

        super(ListParameter, self).__init__(*args, **kwargs)

        self.param_type = param_type

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if not isinstance(value, (list, tuple, set, GeneratorType)):
            value = [value]

        gen = (self.param_type.clean(x) for x in value)

        if isinstance(value, GeneratorType):
            return gen
        else:
            return list(gen)

    def serialize_args(self):
        """Returns (args, kwargs) to be used when deserializing this parameter."""

        args, kwargs = super(ListParameter, self).serialize_args()
        args.insert(0, [self.param_type.id, self.param_type.serialize_args()])

    @staticmethod
    def deserialize_args(args, kwargs):
        type_id, type_args = args[0]
        args, kwargs = super(ListParameter, ListParameter).deserialize_args(args[1:], kwargs)

        type_cls = Parameter.by_id(type_id)
        type_args, type_kwargs = type_cls.deserialize_args(*type_args)
        args.insert(0, type_cls(*type_args, **type_kwargs))

        return args, kwargs

class StringParameter(Parameter):
    """Accepts a string. Will convert numbers to string."""

    id = 'string'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, six.string_types):
            return value
        elif isinstance(value, numbers.Number):
            return str(value)

        raise ParameterNotValidError

class NumberParameter(Parameter):
    """Accepts a number. Will convert numeric strings."""

    id = 'number'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, numbers.Number):
            return value
        elif isinstance(value, six.string_types):
            try:
                value = float(value)
                return int(value) if value.is_integer() else value
            except ValueError:
                raise ParameterNotValidError

        raise ParameterNotValidError

class IntParameter(NumberParameter):
    """Accepts an integer value. Will convert strings, other numbers."""

    id = 'int'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        return int(super(IntParameter, self).clean(value))

class FloatParameter(NumberParameter):
    """Accepts a float value. Will convert strings, other numbers."""

    id = 'float'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        return float(super(FloatParameter, self).clean(value))

class BooleanParameter(Parameter):
    """Accepts a boolean value. Will cast other objects to bool. Will treat a string, "false" as False."""

    id = 'bool'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, six.string_types) and value.lower() == 'false':
            return False

        return bool(value)

class DictParameter(Parameter):
    """Accepts a dictionary"""

    id = 'dict'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, (dict, list)):
            return value

        raise ParameterNotValidError

class NdArrayParameter(Parameter):
    """Accepts a numpy array or Python list"""

    id = 'array'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, numpy.ndarray):
            return value
        elif isinstance(value, (list, tuple)):
            return numpy.array(value)

        raise ParameterNotValidError

class RasterParameter(Parameter):
    """Accepts a `Raster` instance"""

    id = 'raster'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, Raster):
            return value

        raise ParameterNotValidError

class FeatureParameter(Parameter):
    """Accepts a single feature (as a Shapely geometry object)"""

    id = 'feature'

    # Todo: geometry type checking

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, BaseGeometry):
            return value

        raise ParameterNotValidError

class FeatureCollectionParameter(FeatureParameter):
    """Accepts a list of features (as Shapely geometry objects)"""

    id = 'feature_collection'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, (list, tuple)):
            return [super(FeatureCollectionParameter, self).clean(x) for x in value]

        raise ParameterNotValidError

class RasterDatasetParameter(Parameter):
    """Accepts a NetCDF `Dataset` object"""

    id = 'raster_dataset'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, netCDF4.Dataset):
            return value

        raise ParameterNotValidError

class FeatureDatasetParameter(Parameter):
    """Accepts a Fiona `Collection` object"""

    id = 'feature_dataset'

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if isinstance(value, Collection):
            return value

        raise ParameterNotValidError

class RegisteredDatasetParameter(NetCdfDatasetMixin, Parameter):
    """Used by the web API to map registered datasets to inputs"""

    def __init__(self, *args, **kwargs):
        self.service = None

        super(RegisteredDatasetParameter, self).__init__(*args, **kwargs)

    def clean(self, value):
        """Cleans and returns the given value, or raises a ParameterNotValidError exception"""

        if not isinstance(value, six.string_types):
            raise ParameterNotValidError

        try:
            source, value = value.split('://', 1)
        except ValueError:
            raise ParameterNotValidError

        if source == 'service':
            if '@' in value:
                value, timestamp = value.split('@', 1)

                try:
                    service_time = datetime.datetime.utcfromtimestamp(int(timestamp)).replace(tzinfo=pytz.utc)
                except ValueError:
                    raise ParameterNotValidError
            else:
                service_time = None

            if ':' in value:
                service_name, variable_name = value.split(':', 1)
            else:
                service_name = value
                variable_name = None

            try:
                self.service = Service.objects.get(name=service_name)
            except ObjectDoesNotExist:
                raise ParameterNotValidError("Service '{}' not found".format(service_name))

            if variable_name:
                try:
                    variable = self.service.variable_set.all().get(variable=variable_name)
                except ObjectDoesNotExist:
                    raise ParameterNotValidError("Variable '{}' not found".format(variable_name))

                if service_time is not None:
                    time_index = best_fit(variable.time_stops, service_time)
                else:
                    time_index = None

                data = self.get_grid_for_variable(variable, time_index=time_index)
                return Raster(data, variable.full_extent, 1, 0, self.is_y_increasing(variable))
            else:
                return self.dataset
        else:
            raise ParameterNotValidError('Invalid source: {}'.format(source))
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import base64
import cProfile
import os
import pprint
import re
import sys

from Crypto.Cipher import AES
from collections import Counter
from copy import copy
from itertools import cycle, zip_longest

printer = pprint.PrettyPrinter(width=120)
pp = printer.pprint

def hex_to_base64(hex_string):
    return base64.b64encode(bytes.fromhex(hex_string))

def bytes_to_string(b):
    return b.decode("utf-8", errors="replace")

def xor_bytes(bytes1, bytes2):
    if len(bytes1) != len(bytes2):
        raise ValueError("strings must be of equal length")
    return bytes(a ^ b for a, b in zip(bytes1, bytes2))

def xor_encrypt(input_bytes, key):
    return bytes(a ^ b for a, b in zip(input_bytes, cycle(key)))

def edit_distance(bytes1, bytes2):
    result = 0
    for byte1, byte2 in zip(bytes1, bytes2):
        result += sum(1 for i in range(8) if byte1 & (1 << i) != byte2 & (1 << i))
    return result

def byte_chunks(input_bytes, chunk_size):
    # TODO: return generator instead?
    return [input_bytes[i : i + chunk_size] for i in range(0, len(input_bytes), chunk_size)]

def english_like_score(text):
    # Character frequencies from http://www.data-compression.com/english.html
    frequencies = {
        "a": 0.0651738, "b": 0.0124248, "c": 0.0217339, "d": 0.0349835, "e": 0.1041442,
        "f": 0.0197881, "g": 0.0158610, "h": 0.0492888, "i": 0.0558094, "j": 0.0009033,
        "k": 0.0050529, "l": 0.0331490, "m": 0.0202124, "n": 0.0564513, "o": 0.0596302,
        "p": 0.0137645, "q": 0.0008606, "r": 0.0497563, "s": 0.0515760, "t": 0.0729357,
        "u": 0.0225134, "v": 0.0082903, "w": 0.0171272, "x": 0.0013692, "y": 0.0145984,
        "z": 0.0007836, " ": 0.1918182}
    text_length = len(text)
    chi2 = 0
    for letter, count in Counter(text.lower()).items():
        expected = text_length * frequencies.get(letter, 8e-4)
        difference = count - expected
        chi2 += difference**2 / expected
    total_letter_count = sum(1 for char in text.lower() if char in frequencies)
    return total_letter_count / chi2 / text_length

def all_english_like_scores_data(cipher_bytes):
    result = []
    for i in range(256):
        key = bytes([i])
        message = bytes_to_string(xor_encrypt(cipher_bytes, key))
        score = english_like_score(message)
        result.append({
            "key": list(key),
            "key_binary": ["{:08b}".format(b) for b in key],
            "message": message,
            "original": cipher_bytes,
            "score": score})
    return result

def best_english_like_score_data(text, num=1):
    return sorted(all_english_like_scores_data(text), key=lambda m: m["score"], reverse=True)[:num]

def challenge1():
    """Convert hex to base64"""
    cipher_hex = ("49276d206b696c6c696e6720796f757220627261696e206c" +
        "696b65206120706f69736f6e6f7573206d757368726f6f6d")
    result = hex_to_base64(cipher_hex)
    assert result == b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
    print(bytes_to_string(result))

def challenge2():
    """Fixed XOR"""
    output = xor_bytes(
        bytes.fromhex("1c0111001f010100061a024b53535009181c"),
        bytes.fromhex("686974207468652062756c6c277320657965"))
    assert output == b"the kid don't play"
    print(bytes_to_string(output))
    print(output.hex())

def challenge3():
    """Single-byte XOR cipher"""
    cipher_hex = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
    ciphertext = bytes.fromhex(cipher_hex)
    best_data = best_english_like_score_data(ciphertext, num=5)
    pp(best_data)
    print(best_data[0]["message"])
    assert best_data[0]["message"] == "Cooking MC's like a pound of bacon"

def challenge4():
    """Detect single-character XOR"""
    cipher_hexes = [line.rstrip() for line in open("4.txt").readlines()]
    decoded_string_data = []
    for i, string in enumerate(cipher_hexes):
        cipher_bytes = bytes.fromhex(string)
        decoded_string_data.append(best_english_like_score_data(cipher_bytes)[0])
        decoded_string_data[-1]["index"] = i
    best_decodings = sorted(decoded_string_data, key=lambda d: d["score"], reverse=True)
    result = best_decodings[:3]
    pp(result)
    assert best_decodings[0]["message"] == "Now that the party is jumping\n"

def challenge5():
    """Implement repeating-key XOR"""
    stanza = "Burning 'em, if you ain't quick and nimble\n" + "I go crazy when I hear a cymbal"
    result = xor_encrypt(stanza.encode("utf-8"), b"ICE").hex()
    assert result == ("0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343"
        "c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b"
        "20283165286326302e27282f")
    print(result)

def challenge6():
    """Break repeating-key XOR"""
    assert edit_distance(b"this is a test", b"wokka wokka!!!") == 37
    cipher_bytes = base64.b64decode(open("6.txt").read())
    edit_distances = {}
    test_chunk_count = 10
    for key_size in range(2, 41):
        chunks = byte_chunks(cipher_bytes, key_size)
        edit_distances[key_size] = 0
        for i in range(test_chunk_count):
            edit_distances[key_size] += edit_distance(chunks[i], chunks[i + 1])
        edit_distances[key_size] /= key_size
    key_sizes = sorted(edit_distances, key=lambda key_size: edit_distances[key_size])
    best_key_size = key_sizes[0]

    chunks = byte_chunks(cipher_bytes, best_key_size)
    transposed_messages = []
    key = bytearray()
    for i in range(best_key_size):
        transposed_block = bytes(chunk[i] for chunk in chunks if i < len(chunk))
        score_data = best_english_like_score_data(transposed_block)[0]
        transposed_messages.append(score_data["message"])
        key.extend(score_data["key"])
    print(key)
    print()
    plaintext = []
    for message in zip_longest(*transposed_messages):
        plaintext += "".join(char for char in message if char is not None)
    plaintext = "".join(plaintext)
    print(plaintext)
    assert "white boy" in plaintext

def challenge7():
    """AES in ECB mode"""
    cipher_bytes = base64.b64decode(open("7.txt").read())
    message = AES.new("YELLOW SUBMARINE", AES.MODE_ECB).decrypt(cipher_bytes)
    print(bytes_to_string(message))
    assert b"white boy" in message

def challenge8():
    """Detect AES in ECB mode"""
    ecb_texts = []
    for i, line in enumerate(open("8.txt").readlines()):
        cipher_bytes = bytes.fromhex(line.strip())
        chunk_counter = Counter(byte_chunks(cipher_bytes, 16))
        if chunk_counter.most_common(1)[0][1] > 1:
            ecb_texts.append({"index": i, "ciphertext": cipher_bytes})
    pp(ecb_texts)
    assert len(ecb_texts) == 1

def challenge9():
    """Implement PKCS#7 padding"""
    b"YELLOW SUBMARINE\x04\x04\x04\x04"

def challenge10():
    """Implement CBC mode"""
    cipher_bytes = base64.b64decode(open("10.txt").read())
    iv = bytes([0] * 16)

    plain_bytes = AES.new("YELLOW SUBMARINE", AES.MODE_CBC, iv).decrypt(cipher_bytes)
    # print(bytes_to_string(plain_bytes))
    # Create new cipher object because using cipher object messes up internal IV state
    cbc_result = AES.new("YELLOW SUBMARINE", AES.MODE_CBC, iv).encrypt(plain_bytes)
    assert cbc_result == cipher_bytes

    last_cipher_chunk = iv
    cipher = AES.new("YELLOW SUBMARINE", AES.MODE_ECB, iv)
    result = bytearray()
    for plain_chunk in byte_chunks(plain_bytes, 16):
        combined_chunk = xor_bytes(last_cipher_chunk, plain_chunk)
        cipher_chunk = cipher.encrypt(combined_chunk)
        result.extend(cipher_chunk)
        last_cipher_chunk = cipher_chunk
    assert result == cipher_bytes

def test_all_challenges():
    challenges = {}
    for name, var in globals().copy().items():
        try:
            num = int(re.findall("challenge(\d+)", name)[0])
        except (IndexError, ValueError):
            pass
        else:
            if callable(var):
                challenges[num] = var
    old_stdout = sys.stdout
    sys.stdout = open(os.devnull, "w")
    new_printer = copy(printer)
    printer._stream = sys.stdout
    for num in sorted(challenges):
        print("running challenge {}".format(num), file=old_stdout)
        challenges[num]()
    sys.stdout = old_stdout

if __name__ == "__main__":
    try:
        challenge = globals()["challenge" + sys.argv[1]]
    except IndexError:
        test_all_challenges()
        # cProfile.run("test_all_challenges()", sort="cumtime")
    else:
        challenge()
        # cProfile.run("challenge()", sort="cumtime")
<commit_msg>Add pkcs7_pad function and use it<commit_after>#!/usr/bin/env python3

import base64
import cProfile
import os
import pprint
import re
import sys

from Crypto.Cipher import AES
from collections import Counter
from copy import copy
from itertools import cycle, zip_longest

printer = pprint.PrettyPrinter(width=120)
pp = printer.pprint

def hex_to_base64(hex_string):
    return base64.b64encode(bytes.fromhex(hex_string))

def bytes_to_string(b):
    return b.decode("utf-8", errors="replace")

def xor_bytes(bytes1, bytes2):
    if len(bytes1) != len(bytes2):
        raise ValueError("strings must be of equal length")
    return bytes(a ^ b for a, b in zip(bytes1, bytes2))

def xor_encrypt(input_bytes, key):
    return bytes(a ^ b for a, b in zip(input_bytes, cycle(key)))

def edit_distance(bytes1, bytes2):
    result = 0
    for byte1, byte2 in zip(bytes1, bytes2):
        result += sum(1 for i in range(8) if byte1 & (1 << i) != byte2 & (1 << i))
    return result

def byte_chunks(input_bytes, chunk_size):
    # TODO: return generator instead?
    return [input_bytes[i : i + chunk_size] for i in range(0, len(input_bytes), chunk_size)]

def english_like_score(text):
    # Character frequencies from http://www.data-compression.com/english.html
    frequencies = {
        "a": 0.0651738, "b": 0.0124248, "c": 0.0217339, "d": 0.0349835, "e": 0.1041442,
        "f": 0.0197881, "g": 0.0158610, "h": 0.0492888, "i": 0.0558094, "j": 0.0009033,
        "k": 0.0050529, "l": 0.0331490, "m": 0.0202124, "n": 0.0564513, "o": 0.0596302,
        "p": 0.0137645, "q": 0.0008606, "r": 0.0497563, "s": 0.0515760, "t": 0.0729357,
        "u": 0.0225134, "v": 0.0082903, "w": 0.0171272, "x": 0.0013692, "y": 0.0145984,
        "z": 0.0007836, " ": 0.1918182}
    text_length = len(text)
    chi2 = 0
    for letter, count in Counter(text.lower()).items():
        expected = text_length * frequencies.get(letter, 8e-4)
        difference = count - expected
        chi2 += difference**2 / expected
    total_letter_count = sum(1 for char in text.lower() if char in frequencies)
    return total_letter_count / chi2 / text_length

def all_english_like_scores_data(cipher_bytes):
    result = []
    for i in range(256):
        key = bytes([i])
        message = bytes_to_string(xor_encrypt(cipher_bytes, key))
        score = english_like_score(message)
        result.append({
            "key": list(key),
            "key_binary": ["{:08b}".format(b) for b in key],
            "message": message,
            "original": cipher_bytes,
            "score": score})
    return result

def best_english_like_score_data(text, num=1):
    return sorted(all_english_like_scores_data(text), key=lambda m: m["score"], reverse=True)[:num]

def pkcs7_pad(input_bytes, block_size=16):
    padding_length = -len(input_bytes) % block_size
    if padding_length == 0:
        padding_length = block_size
    return input_bytes + bytes([padding_length] * padding_length)

def challenge1():
    """Convert hex to base64"""
    cipher_hex = ("49276d206b696c6c696e6720796f757220627261696e206c" +
        "696b65206120706f69736f6e6f7573206d757368726f6f6d")
    result = hex_to_base64(cipher_hex)
    assert result == b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
    print(bytes_to_string(result))

def challenge2():
    """Fixed XOR"""
    output = xor_bytes(
        bytes.fromhex("1c0111001f010100061a024b53535009181c"),
        bytes.fromhex("686974207468652062756c6c277320657965"))
    assert output == b"the kid don't play"
    print(bytes_to_string(output))
    print(output.hex())

def challenge3():
    """Single-byte XOR cipher"""
    cipher_hex = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
    ciphertext = bytes.fromhex(cipher_hex)
    best_data = best_english_like_score_data(ciphertext, num=5)
    pp(best_data)
    print(best_data[0]["message"])
    assert best_data[0]["message"] == "Cooking MC's like a pound of bacon"

def challenge4():
    """Detect single-character XOR"""
    cipher_hexes = [line.rstrip() for line in open("4.txt").readlines()]
    decoded_string_data = []
    for i, string in enumerate(cipher_hexes):
        cipher_bytes = bytes.fromhex(string)
        decoded_string_data.append(best_english_like_score_data(cipher_bytes)[0])
        decoded_string_data[-1]["index"] = i
    best_decodings = sorted(decoded_string_data, key=lambda d: d["score"], reverse=True)
    result = best_decodings[:3]
    pp(result)
    assert best_decodings[0]["message"] == "Now that the party is jumping\n"

def challenge5():
    """Implement repeating-key XOR"""
    stanza = "Burning 'em, if you ain't quick and nimble\n" + "I go crazy when I hear a cymbal"
    result = xor_encrypt(stanza.encode("utf-8"), b"ICE").hex()
    assert result == ("0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343"
        "c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b"
        "20283165286326302e27282f")
    print(result)

def challenge6():
    """Break repeating-key XOR"""
    assert edit_distance(b"this is a test", b"wokka wokka!!!") == 37
    cipher_bytes = base64.b64decode(open("6.txt").read())
    edit_distances = {}
    test_chunk_count = 10
    for key_size in range(2, 41):
        chunks = byte_chunks(cipher_bytes, key_size)
        edit_distances[key_size] = 0
        for i in range(test_chunk_count):
            edit_distances[key_size] += edit_distance(chunks[i], chunks[i + 1])
        edit_distances[key_size] /= key_size
    key_sizes = sorted(edit_distances, key=lambda key_size: edit_distances[key_size])
    best_key_size = key_sizes[0]

    chunks = byte_chunks(cipher_bytes, best_key_size)
    transposed_messages = []
    key = bytearray()
    for i in range(best_key_size):
        transposed_block = bytes(chunk[i] for chunk in chunks if i < len(chunk))
        score_data = best_english_like_score_data(transposed_block)[0]
        transposed_messages.append(score_data["message"])
        key.extend(score_data["key"])
    print(key)
    print()
    plaintext = []
    for message in zip_longest(*transposed_messages):
        plaintext += "".join(char for char in message if char is not None)
    plaintext = "".join(plaintext)
    print(plaintext)
    assert "white boy" in plaintext

def challenge7():
    """AES in ECB mode"""
    cipher_bytes = base64.b64decode(open("7.txt").read())
    message = AES.new("YELLOW SUBMARINE", AES.MODE_ECB).decrypt(cipher_bytes)
    print(bytes_to_string(message))
    assert b"white boy" in message

def challenge8():
    """Detect AES in ECB mode"""
    ecb_texts = []
    for i, line in enumerate(open("8.txt").readlines()):
        cipher_bytes = bytes.fromhex(line.strip())
        chunk_counter = Counter(byte_chunks(cipher_bytes, 16))
        if chunk_counter.most_common(1)[0][1] > 1:
            ecb_texts.append({"index": i, "ciphertext": cipher_bytes})
    pp(ecb_texts)
    assert len(ecb_texts) == 1

def challenge9():
    """Implement PKCS#7 padding"""
    assert pkcs7_pad(b"YELLOW SUBMARINE", 20) == b"YELLOW SUBMARINE\x04\x04\x04\x04"

def challenge10():
    """Implement CBC mode"""
    cipher_bytes = base64.b64decode(open("10.txt").read())
    iv = bytes([0] * 16)

    plain_bytes = AES.new("YELLOW SUBMARINE", AES.MODE_CBC, iv).decrypt(cipher_bytes)
    # print(bytes_to_string(plain_bytes))
    # Create new cipher object because using cipher object messes up internal IV state
    cbc_result = AES.new("YELLOW SUBMARINE", AES.MODE_CBC, iv).encrypt(plain_bytes)
    assert cbc_result == cipher_bytes

    last_cipher_chunk = iv
    cipher = AES.new("YELLOW SUBMARINE", AES.MODE_ECB, iv)
    result = bytearray()
    for plain_chunk in byte_chunks(plain_bytes, 16):
        combined_chunk = xor_bytes(last_cipher_chunk, plain_chunk)
        cipher_chunk = cipher.encrypt(combined_chunk)
        result.extend(cipher_chunk)
        last_cipher_chunk = cipher_chunk
    assert result == cipher_bytes

def test_all_challenges():
    challenges = {}
    for name, var in globals().copy().items():
        try:
            num = int(re.findall("challenge(\d+)", name)[0])
        except (IndexError, ValueError):
            pass
        else:
            if callable(var):
                challenges[num] = var
    old_stdout = sys.stdout
    sys.stdout = open(os.devnull, "w")
    new_printer = copy(printer)
    printer._stream = sys.stdout
    for num in sorted(challenges):
        print("running challenge {}".format(num), file=old_stdout)
        challenges[num]()
    sys.stdout = old_stdout

if __name__ == "__main__":
    try:
        challenge = globals()["challenge" + sys.argv[1]]
    except IndexError:
        test_all_challenges()
        # cProfile.run("test_all_challenges()", sort="cumtime")
    else:
        challenge()
        # cProfile.run("challenge()", sort="cumtime")
<|endoftext|>


<commit_before>REQUIRED_CONFIRMATIONS = 2
COIN = 10 ** 8
MIN_TIME = 60 * 6  # min 6 hours<commit_msg>increase MIN_TIME to 14 days<commit_after>REQUIRED_CONFIRMATIONS = 2
COIN = 10 ** 8
MIN_TIME = 60 * 24 * 14 - 1  # min 14 days<|endoftext|>


<commit_before>import os
import pytest

from hadoop_env_config import env_config

def get_abspath_test_path():

    return os.path.dirname(os.path.abspath(__file__))

def get_settings_path():

    return os.path.join(get_abspath_test_path(), 'resources/settings/')

@pytest.fixture(params=['hdp-with-server.settings', 'hdp-wo-server.settings'])
def hdp_settings_path(request):

    return os.path.join(get_settings_path(), request.param)

@pytest.fixture(params=['hdp-with-server.settings', 'hdp-wo-server.settings'])
def hdp_settings_path(request):

    return os.path.join(get_settings_path(), request.param)

def test_env_config_undefined_settings():

    with pytest.raises(env_config.UndefinedSettingsFile):
        conf = env_config.HadoopEnvConfig(None)

def test_env_config_empty_settings():

    with pytest.raises(env_config.IncorrectJsonFormat):
        conf = env_config.HadoopEnvConfig(os.path.join(get_settings_path(), 'empty.settings'))

def test_env_config_incorrect_path_to_settings():

    with pytest.raises(env_config.IncorrectJsonFormat):
        conf = env_config.HadoopEnvConfig(os.path.join(get_settings_path(), 'shadow.settings'))

def test_hadoop_env_config_init(hdp_settings_path):

    conf = env_config.HadoopEnvConfig(hdp_settings_path)
<commit_msg>[+] test_hadoop_env_config()<commit_after>import os
import pytest

from hadoop_env_config import env_config

def get_abspath_test_path():

    return os.path.dirname(os.path.abspath(__file__))

def get_settings_path():

    return os.path.join(get_abspath_test_path(), 'resources/settings/')

def get_configs_path():

    return os.path.join(get_abspath_test_path(), 'resources/conf-dirs/')

@pytest.fixture(params=['hdp-with-server.settings', 'hdp-wo-server.settings'])
def hdp_settings_path(request):

    return os.path.join(get_settings_path(), request.param)

@pytest.fixture(params=['hdp-with-server.settings', 'hdp-wo-server.settings'])
def hdp_settings_path(request):

    return os.path.join(get_settings_path(), request.param)

def test_env_config_undefined_settings():

    with pytest.raises(env_config.UndefinedSettingsFile):
        conf = env_config.HadoopEnvConfig(None)

def test_env_config_empty_settings():

    with pytest.raises(env_config.IncorrectJsonFormat):
        conf = env_config.HadoopEnvConfig(os.path.join(get_settings_path(), 'empty.settings'))

def test_env_config_incorrect_path_to_settings():

    with pytest.raises(env_config.IncorrectJsonFormat):
        conf = env_config.HadoopEnvConfig(os.path.join(get_settings_path(), 'shadow.settings'))

def test_hadoop_env_config(hdp_settings_path):

    conf = env_config.HadoopEnvConfig(hdp_settings_path)
    conf.settings['config-dir'] = os.path.join(get_configs_path(), 'hdp-2.2.8/')
    env_props = conf.parse()

    assert isinstance(env_props, dict) and len(env_props) != 0
    assert 'oozieServer' in env_props and env_props['oozieServer'] == 'http://oozie.bigdata.hosts:11000'
    assert 'jobTracker' in env_props and env_props['jobTracker'] == 'yarn.bigdata.hosts:8050'
    assert 'nameNode' in env_props and env_props['nameNode'] == 'hdfs://nn.bigdata.hosts:8020'

    assert 'sshUserName' in env_props and env_props['sshUserName'] == 'username'
    assert 'sshPassword' in env_props and env_props['sshPassword'] == 'password'

<|endoftext|>


<commit_before>"""
conway.py
Author: Morgan Meliment
Credit: http://brythonserver.github.io/ggame/#ggame.EllipseAsset, http://stackoverflow.com/questions/10665591/how-to-remove-list-elements-in-a-for-loop-in-python
Assignment:
Write and submit a program that plays Conway's Game of Life, per
https://github.com/HHS-IntroProgramming/Conway-Life

"""
from ggame import App, Color, Sprite, RectangleAsset, LineStyle, MouseEvent

purple = Color(0x512DA8, 1)
nostroke = LineStyle(0, grey)
livecells = {}
makecells = []
surcells = []
restingcells = {}
killcells = {}

def create():
    for celle in makecells[:]:
        if restingcells.get((celle[0], celle[1]), False) == False:
            Cell((celle[0], celle[1]))
        else:
            restingcells[(celle[0], celle[1])] = False
            livecells[(celle[0], celle[1])] = True
        makecells.remove(celle)

def getcoor(xx, yy):
    return([[xx - 10, yy - 10], [xx - 10, yy], [xx - 10, yy + 10], [xx, yy - 10], [xx, yy + 10], [xx + 10, yy - 10], [xx + 10, yy], [xx + 10, yy + 10]])

def check():
    for celle in surcells[:]:
        if find(celle[0], celle[1]) == 3:
            makecells.append(celle)
        surcells.remove(celle)

def checking(xx, yy):
    dei = getcoor(xx, yy)
    for deadcell in dei:
        if livecells.get((deadcell[0], deadcell[1]), False) == False:
            if deadcell in surcells:
                at = 1
            else:
                surcells.append(deadcell)

def find(xx, yy):
    neighbors = getcoor(xx, yy)
    neighborcount = 0
    for posi in neighbors:
        if livecells.get((posi[0], posi[1]), False) == True:
            neighborcount += 1
    return(neighborcount)

class Cell(Sprite):
    pix = RectangleAsset(10, 10, nostroke, purple)
    def __init__(self, position):
        super().__init__(Cell.pix, position)
        livecells[self.position] = True

    def step(self):
        n = find(self.x, self.y)
        if n < 2 or n > 3:
            killcells[self.position] = True
        checking(self.x, self.y)
        if livecells.get(self.position) == False:
            self.visible = False
            restingcells[self.position] = True
        if restingcells.get(self.position) == False:
            self.visible = True

    def kill(self):
        if killcells.get(self.position, False) == True:
            livecells[self.position] = False
            killcells[self.position] = False

class Conway(App):
    def __init__(self, width, height):
        super().__init__(width, height)
        Cell((100, 50))
        Cell((100, 60))
        Cell((100, 70))
        Cell((110, 60))

    def step(self):
        create()
        for cell in self.getSpritesbyClass(Cell):
            cell.kill()
        for cell in self.getSpritesbyClass(Cell):
            cell.step()
        check()

myapp = Conway(640, 480)
myapp.run()

<commit_msg>Updated from Brython Server: October 12, 2015 8:22:58 PM EDT<commit_after>"""
conway.py
Author: Morgan Meliment
Credit: http://brythonserver.github.io/ggame/#ggame.EllipseAsset, http://stackoverflow.com/questions/10665591/how-to-remove-list-elements-in-a-for-loop-in-python
Assignment:
Write and submit a program that plays Conway's Game of Life, per
https://github.com/HHS-IntroProgramming/Conway-Life

"""
from ggame import App, Color, Sprite, RectangleAsset, LineStyle, MouseEvent

purple = Color(0x512DA8, 1)
nostroke = LineStyle(0, grey)
livecells = {}
makecells = []
surcells = []
restingcells = {}
killcells = {}

def create():
    for celle in makecells[:]:
        if restingcells.get((celle[0], celle[1]), False) == False:
            Cell((celle[0], celle[1]))
        else:
            restingcells[(celle[0], celle[1])] = False
            livecells[(celle[0], celle[1])] = True
        makecells.remove(celle)

def getcoor(xx, yy):
    return([[xx - 10, yy - 10], [xx - 10, yy], [xx - 10, yy + 10], [xx, yy - 10], [xx, yy + 10], [xx + 10, yy - 10], [xx + 10, yy], [xx + 10, yy + 10]])

def check():
    for celle in surcells[:]:
        if find(celle[0], celle[1]) == 3:
            makecells.append(celle)
        surcells.remove(celle)

def checking(xx, yy):
    dei = getcoor(xx, yy)
    for deadcell in dei:
        if livecells.get((deadcell[0], deadcell[1]), False) == False:
            if deadcell in surcells:
                at = 1
            else:
                surcells.append(deadcell)

def find(xx, yy):
    neighbors = getcoor(xx, yy)
    neighborcount = 0
    for posi in neighbors:
        if livecells.get((posi[0], posi[1]), False) == True:
            neighborcount += 1
    return(neighborcount)

class Cell(Sprite):
    pix = RectangleAsset(10, 10, nostroke, purple)
    def __init__(self, position):
        super().__init__(Cell.pix, position)
        livecells[self.position] = True

    def step(self):
        n = find(self.x, self.y)
        if n < 2 or n > 3:
            killcells[self.position] = True
        checking(self.x, self.y)
        if livecells.get(self.position) == False:
            self.visible = False
            restingcells[self.position] = True
        if restingcells.get(self.position) == False:
            self.visible = True

    def kill(self):
        if killcells.get(self.position, False) == True:
            livecells[self.position] = False
            killcells[self.position] = False

class Conway(App):
    def __init__(self, width, height):
        super().__init__(width, height)
        Cell((100, 50))
        Cell((110, 60))
        Cell((110, 70))
        Cell((100, 70))
        Cell((90, 70))

    def step(self):
        create()
        for cell in self.getSpritesbyClass(Cell):
            cell.kill()
        for cell in self.getSpritesbyClass(Cell):
            cell.step()
        check()

myapp = Conway(640, 480)
myapp.run()

<|endoftext|>


<commit_before>from string import Template
import json
from PIL import Image
import argparse
import os
import sys

temp = ""
output_hex = True
template_path = "font.template"

parser = argparse.ArgumentParser(
    description='Create makise code from generated image and data.')
parser.add_argument('json', nargs="?", default="img.json",
                    help='Size of the font in pixels. Need only for TTF')
parser.add_argument('-b', '--binary', action='store_true',
                    help='Make output table binary. Default is hex')
parser.add_argument('-o', '--out', default='out.c',
                    help='Specifiy output file. Default is out.c')
parser.add_argument('-s', '--space', default=0,
                    help='Space between characters in pixels. Default is 0')

args = parser.parse_args()

output_hex = not args.binary

s_pth = os.path.dirname(sys.argv[0])
template_path = os.path.join(s_pth, template_path)
if not os.path.isfile(template_path):
    print("Font template not found. Check script directory.")
    parser.print_help()
    sys.exit()
with open(template_path, "r") as f:
    temp = Template(f.read())

data = {}
if not os.path.isfile(args.json):
    print("Data json file %s not found" % args.json)
    parser.print_help()
    sys.exit()
with open(args.json, "r") as f:
    data = json.load(f)

if not os.path.isfile(data['img']):
    print("Bitmap file %s not found" % data['img'])
    parser.print_help()
    sys.exit()
im = Image.open(data['img'])

table_i = data['table_i']

# first block without gasps
# (usually codes from 32 to 127). Optimization for perfomance.
first_offset = table_i[0]
first_count = 1
last = table_i[0]
for i in table_i[1:]:
    if i != last + 1:
        break
    last = i
    first_count += 1

table_i = table_i[first_count:]

# Read bitmap and convert it in the bytes
def print_char(x, y, w, h):
    global im, output_hex
    i = 0
    sg = ""
    bits = list()
    for i in range((h + 1) * w):
        if(i % 8 == 0 and i != 0):
            bits.append(sg)
            sg = ""
        r, g, b, a = im.getpixel((x + i % w, y + i/w))
        sg += '1' if (r == g == b == 255) else '0'
    # print(i)
    #if i % 8 != 0 and i % 8 != 7:
    while i % 8 != 0:
        i += 1
        sg += "0"
        bits.append(sg)
    out = ""
    bytesinchar = 0
    for z in bits:
        if(output_hex):
            out += ("0x%02x" % int(z[::-1], 2)) + ", "
        else:
            out += "0b" + z[::-1] + ", "
        bytesinchar += 1
    return out

table = ""
table_o = []
table_w = data['table_w']
x = 1
o = 0
i = 0
for w in data['table_w']:
    s = print_char(x, 1, w, data["height"])
    if not chr(data['table_i'][i]) == '\\':
        table += s + '//' + chr(data['table_i'][i])
    else:
        table += s
    table += '\n'
    table_o.append(o)
    x += w + 1
    o += s.count(',')
    i += 1

table_w = str(table_w)[1:-1]
table_o = str(table_o)[1:-1]
table_i = str(table_i)[1:-1]

r = temp.substitute(
    name=data['name'],
    size=data['size'],
    table=table,
    width=table_w,  # if data['const_w'] == -1 else 0,
    offset=table_o,
    unicode=table_i,
    const_width=data['const_w'] if data['const_w'] != -1 else 0,
    height=data['height'],
    first_offset=first_offset,
    first_count=first_count,
    total_count=len(data["table_i"]),
    space=args.space
)
with open(args.out, "w") as f:
    f.write(r)

print("Font file %s generated" % args.out)
<commit_msg>font creator fix<commit_after>from string import Template
import json
from PIL import Image
import argparse
import os
import sys

temp = ""
output_hex = True
template_path = "font.template"

parser = argparse.ArgumentParser(
    description='Create makise code from generated image and data.')
parser.add_argument('json', nargs="?", default="img.json",
                    help='Specifiy location of font data. Default: img.json')
parser.add_argument('-b', '--binary', action='store_true',
                    help='Make output table binary. Default is hex')
parser.add_argument('-o', '--out', default='out.c',
                    help='Specifiy output file. Default is out.c')
parser.add_argument('-s', '--space', default=0,
                    help='Space between characters in pixels. Default is 0')

args = parser.parse_args()

output_hex = not args.binary

s_pth = os.path.dirname(sys.argv[0])
template_path = os.path.join(s_pth, template_path)
if not os.path.isfile(template_path):
    print("Font template not found. Check script directory.")
    parser.print_help()
    sys.exit()
with open(template_path, "r") as f:
    temp = Template(f.read())

data = {}
if not os.path.isfile(args.json):
    print("Data json file %s not found" % args.json)
    parser.print_help()
    sys.exit()
with open(args.json, "r") as f:
    data = json.load(f)

if not os.path.isfile(data['img']):
    print("Bitmap file %s not found" % data['img'])
    parser.print_help()
    sys.exit()
im = Image.open(data['img'])

table_i = data['table_i']

# first block without gasps
# (usually codes from 32 to 127). Optimization for perfomance.
first_offset = table_i[0]
first_count = 1
last = table_i[0]
for i in table_i[1:]:
    if i != last + 1:
        break
    last = i
    first_count += 1

table_i = table_i[first_count:]

# Read bitmap and convert it in the bytes
def print_char(x, y, w, h):
    global im, output_hex
    i = 0
    sg = ""
    bits = list()
    for i in range((h + 1) * w):
        if(i % 8 == 0 and i != 0):
            bits.append(sg)
            sg = ""
        r, g, b, a = im.getpixel((x + i % w, y + i/w))
        sg += '1' if (r == g == b == 255) else '0'
    # print(i)
    #if i % 8 != 0 and i % 8 != 7:
    while i % 8 != 0:
        i += 1
        sg += "0"
        bits.append(sg)
    out = ""
    bytesinchar = 0
    for z in bits:
        if(output_hex):
            out += ("0x%02x" % int(z[::-1], 2)) + ", "
        else:
            out += "0b" + z[::-1] + ", "
        bytesinchar += 1
    return out

table = ""
table_o = []
table_w = data['table_w']
x = 1
o = 0
i = 0
for w in data['table_w']:
    s = print_char(x, 1, w, data["height"])
    if not chr(data['table_i'][i]) == '\\':
        table += s + '//' + chr(data['table_i'][i])
    else:
        table += s
    table += '\n'
    table_o.append(o)
    x += w + 1
    o += s.count(',')
    i += 1

table_w = str(table_w)[1:-1]
table_o = str(table_o)[1:-1]
table_i = str(table_i)[1:-1]

r = temp.substitute(
    name=data['name'],
    size=data['size'],
    table=table,
    width=table_w,  # if data['const_w'] == -1 else 0,
    offset=table_o,
    unicode=table_i,
    const_width=data['const_w'] if data['const_w'] != -1 else 0,
    height=data['height'],
    first_offset=first_offset,
    first_count=first_count,
    total_count=len(data["table_i"]),
    space=args.space
)
with open(args.out, "w") as f:
    f.write(r)

print("Font file %s generated" % args.out)
<|endoftext|>


<commit_before>import datetime
import gzip
import os
import platform
import signal
import sys
import tempfile
import time
from importlib import reload
from subprocess import Popen

import numpy as np
import pytest
from owslib.fes import PropertyIsEqualTo

import pydov
from pydov.search.boring import BoringSearch
from pydov.search.grondwaterfilter import GrondwaterFilterSearch
from pydov.util.caching import GzipTextFileCache
from pydov.util.errors import XmlFetchWarning, XmlStaleWarning, XsdFetchWarning
from tests.abstract import ServiceCheck

@pytest.fixture(scope="module", autouse=True)
def dov_proxy_no_xdov():
    process = Popen([sys.executable,
                     os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                  'stub', 'dov_proxy.py'),
                     '--no-xdov'])
    time.sleep(2)

    os.environ['PYDOV_BASE_URL'] = 'http://localhost:1337/'

    yield

    del os.environ['PYDOV_BASE_URL']

    if platform.system() == 'Windows':
        process.send_signal(signal.CTRL_C_EVENT)
    else:
        process.send_signal(signal.SIGINT)

    process.communicate()

@pytest.fixture(scope="module", autouse=True)
def reload_modules(dov_proxy_no_xdov):
    reload(pydov.types.boring)
    reload(pydov.types.grondwaterfilter)

    yield

    reload(pydov.types.boring)
    reload(pydov.types.grondwaterfilter)

@pytest.fixture(scope="function", autouse=True)
def reset_cache(dov_proxy_no_xdov):
    """Reset the cache to a temporary folder to remove influence from other
    tests.

    The cache needs to be reset after setting the PYDOV_BASE_URL variable
    because at initialisation this URL is used to construct a regex for
    determining the datatype of an XML request.

    Parameters
    ----------
    dov_proxy_no_xdov : pytest.fixture
        Fixture starting up the DOV proxy stub and setting PYDOV_BASE_URL
        accordingly.
    """
    gziptext_cache = GzipTextFileCache(
        cachedir=os.path.join(tempfile.gettempdir(), 'pydov_tests_error'),
        max_age=datetime.timedelta(seconds=0.1))
    gziptext_cache.remove()

    orig_cache = pydov.cache
    pydov.cache = gziptext_cache

    yield

    gziptext_cache.remove()
    pydov.cache = orig_cache

class TestErrorPaths(object):
    """Class grouping tests related failing DOV services."""

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_do_not_cache_error(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        bs.search(query=PropertyIsEqualTo(
            'pkey_boring',
            'https://www.dov.vlaanderen.be/data/boring/2004-103984'))

        assert not os.path.exists(os.path.join(
            pydov.cache.cachedir, 'boring', '2004-103984.xml.gz'
        ))

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_do_not_overwrite_stale_cache(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        testdata_path = os.path.join(
            'tests', 'data', 'types', 'boring', 'boring.xml')

        cache_path = os.path.join(
            pydov.cache.cachedir, 'boring', '2004-103984.xml.gz'
        )
        os.makedirs(os.path.dirname(cache_path))

        with open(testdata_path, 'r') as testdata:
            with gzip.open(cache_path, 'wb') as cached_data:
                cached_data.write(testdata.read().encode('utf8'))
        time.sleep(0.5)

        bs.search(query=PropertyIsEqualTo(
            'pkey_boring',
            'https://www.dov.vlaanderen.be/data/boring/2004-103984'))

        with gzip.open(cache_path, 'rb') as cached_data:
            assert 'GEO-04/169-BNo-B1' in cached_data.read().decode('utf8')

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_stale_warning(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        testdata_path = os.path.join(
            'tests', 'data', 'types', 'boring', 'boring.xml')

        cache_path = os.path.join(
            pydov.cache.cachedir, 'boring', '2004-103984.xml.gz'
        )
        os.makedirs(os.path.dirname(cache_path))

        with open(testdata_path, 'r') as testdata:
            with gzip.open(cache_path, 'wb') as cached_data:
                cached_data.write(testdata.read().encode('utf8'))
        time.sleep(0.5)

        with pytest.warns(XmlStaleWarning):
            df = bs.search(query=PropertyIsEqualTo(
                'pkey_boring',
                'https://www.dov.vlaanderen.be/data/boring/2004-103984'))

        assert not df.iloc[0].boorgatmeting
        assert df.iloc[0].boormethode == 'spade'

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_wfs_data_present(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        df = bs.search(query=PropertyIsEqualTo(
            'pkey_boring',
            'https://www.dov.vlaanderen.be/data/boring/2016-122561'))

        assert df.iloc[0].gemeente == 'Wortegem-Petegem'

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_nan_and_fetch_warning(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        with pytest.warns(XmlFetchWarning):
            df = bs.search(query=PropertyIsEqualTo(
                'pkey_boring',
                'https://www.dov.vlaanderen.be/data/boring/2016-122561'))

        assert np.isnan(df.iloc[0].boorgatmeting)

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_no_xsd_warning(self):
        with pytest.warns(XsdFetchWarning):
            gwf = GrondwaterFilterSearch(
                objecttype=pydov.types.grondwaterfilter.GrondwaterFilter)
            fields = gwf.get_fields()

        assert 'values' not in fields['aquifer_code']

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_no_xsd_wfs_only(self):
        gwf = GrondwaterFilterSearch(
            objecttype=pydov.types.grondwaterfilter.GrondwaterFilter)

        df = gwf.search(max_features=1)
        assert df.iloc[0].pkey_filter is not None
<commit_msg>Use the terminator.<commit_after>import datetime
import gzip
import os
import sys
import tempfile
import time
from importlib import reload
from subprocess import Popen

import numpy as np
import pytest
from owslib.fes import PropertyIsEqualTo

import pydov
from pydov.search.boring import BoringSearch
from pydov.search.grondwaterfilter import GrondwaterFilterSearch
from pydov.util.caching import GzipTextFileCache
from pydov.util.errors import XmlFetchWarning, XmlStaleWarning, XsdFetchWarning
from tests.abstract import ServiceCheck

@pytest.fixture(scope="module", autouse=True)
def dov_proxy_no_xdov():
    process = Popen([sys.executable,
                     os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                  'stub', 'dov_proxy.py'),
                     '--no-xdov'])
    time.sleep(2)

    os.environ['PYDOV_BASE_URL'] = 'http://localhost:1337/'

    yield

    del os.environ['PYDOV_BASE_URL']

    process.terminate()
    process.communicate()

@pytest.fixture(scope="module", autouse=True)
def reload_modules(dov_proxy_no_xdov):
    reload(pydov.types.boring)
    reload(pydov.types.grondwaterfilter)

    yield

    reload(pydov.types.boring)
    reload(pydov.types.grondwaterfilter)

@pytest.fixture(scope="function", autouse=True)
def reset_cache(dov_proxy_no_xdov):
    """Reset the cache to a temporary folder to remove influence from other
    tests.

    The cache needs to be reset after setting the PYDOV_BASE_URL variable
    because at initialisation this URL is used to construct a regex for
    determining the datatype of an XML request.

    Parameters
    ----------
    dov_proxy_no_xdov : pytest.fixture
        Fixture starting up the DOV proxy stub and setting PYDOV_BASE_URL
        accordingly.
    """
    gziptext_cache = GzipTextFileCache(
        cachedir=os.path.join(tempfile.gettempdir(), 'pydov_tests_error'),
        max_age=datetime.timedelta(seconds=0.1))
    gziptext_cache.remove()

    orig_cache = pydov.cache
    pydov.cache = gziptext_cache

    yield

    gziptext_cache.remove()
    pydov.cache = orig_cache

class TestErrorPaths(object):
    """Class grouping tests related failing DOV services."""

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_do_not_cache_error(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        bs.search(query=PropertyIsEqualTo(
            'pkey_boring',
            'https://www.dov.vlaanderen.be/data/boring/2004-103984'))

        assert not os.path.exists(os.path.join(
            pydov.cache.cachedir, 'boring', '2004-103984.xml.gz'
        ))

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_do_not_overwrite_stale_cache(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        testdata_path = os.path.join(
            'tests', 'data', 'types', 'boring', 'boring.xml')

        cache_path = os.path.join(
            pydov.cache.cachedir, 'boring', '2004-103984.xml.gz'
        )
        os.makedirs(os.path.dirname(cache_path))

        with open(testdata_path, 'r') as testdata:
            with gzip.open(cache_path, 'wb') as cached_data:
                cached_data.write(testdata.read().encode('utf8'))
        time.sleep(0.5)

        bs.search(query=PropertyIsEqualTo(
            'pkey_boring',
            'https://www.dov.vlaanderen.be/data/boring/2004-103984'))

        with gzip.open(cache_path, 'rb') as cached_data:
            assert 'GEO-04/169-BNo-B1' in cached_data.read().decode('utf8')

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_stale_warning(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        testdata_path = os.path.join(
            'tests', 'data', 'types', 'boring', 'boring.xml')

        cache_path = os.path.join(
            pydov.cache.cachedir, 'boring', '2004-103984.xml.gz'
        )
        os.makedirs(os.path.dirname(cache_path))

        with open(testdata_path, 'r') as testdata:
            with gzip.open(cache_path, 'wb') as cached_data:
                cached_data.write(testdata.read().encode('utf8'))
        time.sleep(0.5)

        with pytest.warns(XmlStaleWarning):
            df = bs.search(query=PropertyIsEqualTo(
                'pkey_boring',
                'https://www.dov.vlaanderen.be/data/boring/2004-103984'))

        assert not df.iloc[0].boorgatmeting
        assert df.iloc[0].boormethode == 'spade'

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_wfs_data_present(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        df = bs.search(query=PropertyIsEqualTo(
            'pkey_boring',
            'https://www.dov.vlaanderen.be/data/boring/2016-122561'))

        assert df.iloc[0].gemeente == 'Wortegem-Petegem'

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_nan_and_fetch_warning(self):
        bs = BoringSearch(objecttype=pydov.types.boring.Boring)

        with pytest.warns(XmlFetchWarning):
            df = bs.search(query=PropertyIsEqualTo(
                'pkey_boring',
                'https://www.dov.vlaanderen.be/data/boring/2016-122561'))

        assert np.isnan(df.iloc[0].boorgatmeting)

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_no_xsd_warning(self):
        with pytest.warns(XsdFetchWarning):
            gwf = GrondwaterFilterSearch(
                objecttype=pydov.types.grondwaterfilter.GrondwaterFilter)
            fields = gwf.get_fields()

        assert 'values' not in fields['aquifer_code']

    @pytest.mark.online
    @pytest.mark.skipif(not ServiceCheck.service_ok(),
                        reason="DOV service is unreachable")
    def test_no_xsd_wfs_only(self):
        gwf = GrondwaterFilterSearch(
            objecttype=pydov.types.grondwaterfilter.GrondwaterFilter)

        df = gwf.search(max_features=1)
        assert df.iloc[0].pkey_filter is not None
<|endoftext|>


<commit_before>"""
conway.py
Author: Mary Feyrer
Credit: Tess Snyder, Mr. Dennison
Assignment:
Write and submit a program that plays Conway's Game of Life, per
https://github.com/HHS-IntroProgramming/Conway-Life
"""
from ggame import App, RectangleAsset, ImageAsset
from ggame import LineStyle, Color, Sprite, Sound

white = Color(0xffffff, 1.0)
blue = Color(0x000000, 1.0)
black = Color(0x000000, 1.0)
red = Color(0xff0000, 1.0)
noline = LineStyle(0, black)
line = LineStyle(.2, black)
list1 = []

square = RectangleAsset(20, 20, noline, red)
screen = RectangleAsset(400,400, line, white)
Sprite(screen,(0,0))

for x in range(0,20):
    for y in range(0,20):
        s = Sprite(square, (20*x,20*y))
        s.visible = False
        list1.append(s)

class Conway(App):

    def __init__(self):
        super().__init__()
        self.listenMouseEvent( 'click', self.click)
        self.running = False
        self.listenKeyEvent("keydown", "space", self.spacebar)

    def click(self, event):
        x = event.x
        y = event.y
        for sprite in list1:
            if x<= sprite.x +10 and x>= sprite.x-10 and y<= sprite.y+10 and y>= sprite.y-10:
                sprite.visible = True

    def spacebar(self, event):
        self.running = True

    def step(self):
        if self.running == True:
            for sprite in list1:
                surroudlist = []
                redlist = []
                for spr in list1:
                    if spr.visible==True:
                        redlist.append(spr)
                for redsprite in redlist:
                    if abs(sprite.x - redsprite.x) == 20 and abs(sprite.y - redsprite.y) == 20:
                        surroundlist.append(redsprite)
                        n = len(surroundlist)
                        if n > 3:
                            sprite.visible = False
                        elif n == 3:
                            sprite.visible = True
                        elif n < 2:
                            sprite.visible = False

myapp = Conway()
myapp.run()
<commit_msg>Updated from Brython Server: 3/23/2016 1:40:48 PM<commit_after>"""
conway.py
Author: Mary Feyrer
Credit: Tess Snyder, Mr. Dennison
Assignment:
Write and submit a program that plays Conway's Game of Life, per
https://github.com/HHS-IntroProgramming/Conway-Life
"""
from ggame import App, RectangleAsset, ImageAsset
from ggame import LineStyle, Color, Sprite, Sound

white = Color(0xffffff, 1.0)
blue = Color(0x000000, 1.0)
black = Color(0x000000, 1.0)
red = Color(0xff0000, 1.0)
noline = LineStyle(0, black)
line = LineStyle(.2, black)
list1 = []

square = RectangleAsset(20, 20, noline, red)
screen = RectangleAsset(400,400, line, white)
Sprite(screen,(0,0))

for x in range(0,20):
    for y in range(0,20):
        s = Sprite(square, (20*x,20*y))
        s.visible = False
        list1.append(s)

class Conway(App):

    def __init__(self):
        super().__init__()
        self.listenMouseEvent( 'click', self.click)
        self.running = False
        self.listenKeyEvent("keydown", "space", self.spacebar)

    def click(self, event):
        x = event.x
        y = event.y
        for sprite in list1:
            if x<= sprite.x +10 and x>= sprite.x-10 and y<= sprite.y+10 and y>= sprite.y-10:
                sprite.visible = True

    def spacebar(self, event):
        self.running = True

    def step(self):
        if self.running == True:
            for sprite in list1:
                surroudlist=[]
                redlist=[]
                for spr in list1:
                    if spr.visible==True:
                        redlist.append(spr)
                for redsprite in redlist:
                    if abs(sprite.x-redsprite.x) <= 20 and abs(sprite.y-redsprite.y) <= 20:
                        surroundlist.append(redsprite)
                        num=len(surroundlist)
                        if sprite.visible==True:
                            num=num-1
                        if num>3:
                            sprite.visible = False
                        elif num==3:
                            sprite.visible = True
                        elif num<2:
                            sprite.visible = False

myapp = Conway()
myapp.run()
<|endoftext|>


<commit_before>#! /usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np
import tensorflow as tf

__all__ = [
    'Initializer', 'Zeros', 'Ones', 'Constant', 'RandomUniform', 'RandomNormal', 'TruncatedNormal',
    'deconv2d_bilinear_upsampling_initializer'
]

class Initializer(object):
    """Initializer base class: all initializers inherit from this class.
    """

    def __call__(self, shape, dtype=None):
        """Returns a tensor object initialized as specified by the initializer.

        Parameters
        ----------
        shape : tuple of int.
            The shape of the tensor.
        dtype : Optional dtype of the tensor.
            If not provided will return tensor of `tf.float32`.

        Returns
        -------

        """
        raise NotImplementedError

    def get_config(self):
        """Returns the configuration of the initializer as a JSON-serializable dict.

        Returns
        -------
            A JSON-serializable Python dict.
        """
        return {}

    @classmethod
    def from_config(cls, config):
        """Instantiates an initializer from a configuration dictionary.

        Parameters
        ----------
        config : A python dictionary.
            It will typically be the output of `get_config`.

        Returns
        -------
            An Initializer instance.
        """
        if 'dtype' in config:
            config.pop('dtype')
        return cls(**config)

class Zeros(Initializer):
    """Initializer that generates tensors initialized to 0.
    """

    def __call__(self, shape, dtype=tf.float32):
        return tf.zeros(shape, dtype=dtype)

class Ones(Initializer):
    """Initializer that generates tensors initialized to 1.
    """

    def __call__(self, shape, dtype=tf.float32):
        return tf.ones(shape, dtype=dtype)

class Constant(Initializer):
    """Initializer that generates tensors initialized to a constant value.

    Parameters
    ----------
    value : A python scalar or a numpy array.
        The assigned value.

    """

    def __init__(self, value=0):
        self.value = value

    def __call__(self, shape, dtype=None):
        return tf.constant(self.value, shape=shape, dtype=dtype)

    def get_config(self):
        return {"value": self.value}

class RandomUniform(Initializer):
    """Initializer that generates tensors with a uniform distribution.

    Parameters
    ----------
    minval : A python scalar or a scalar tensor.
        Lower bound of the range of random values to generate.
    maxval : A python scalar or a scalar tensor.
        Upper bound of the range of random values to generate.
    seed : A Python integer.
        Used to seed the random generator.

    """

    def __init__(self, minval=-0.05, maxval=0.05, seed=None):
        self.minval = minval
        self.maxval = maxval
        self.seed = seed

    def __call__(self, shape, dtype=tf.float32):
        return tf.random.uniform(shape, self.minval, self.maxval, dtype=dtype, seed=self.seed)

    def get_config(self):
        return {"minval": self.minval, "maxval": self.maxval, "seed": self.seed}

class RandomNormal(Initializer):
    """Initializer that generates tensors with a normal distribution.

    Parameters
    ----------
    mean : A python scalar or a scalar tensor.
        Mean of the random values to generate.
    stddev : A python scalar or a scalar tensor.
        Standard deviation of the random values to generate.
    seed : A Python integer.
        Used to seed the random generator.
    """

    def __init__(self, mean=0.0, stddev=0.05, seed=None):
        self.mean = mean
        self.stddev = stddev
        self.seed = seed

    def __call__(self, shape, dtype=tf.float32):
        return tf.random.normal(shape, self.mean, self.stddev, dtype=dtype, seed=self.seed)

    def get_config(self):
        return {"mean": self.mean, "stddev": self.stddev, "seed": self.seed}

class TruncatedNormal(Initializer):
    """Initializer that generates a truncated normal distribution.

    These values are similar to values from a `RandomNormal`
    except that values more than two standard deviations from the mean
    are discarded and re-drawn. This is the recommended initializer for
    neural network weights and filters.

    Parameters
    ----------
    mean : A python scalar or a scalar tensor.
        Mean of the random values to generate.
    stddev : A python scalar or a scalar tensor.
        Standard deviation of the andom values to generate.
    seed : A Python integer.
        Used to seed the random generator.
    """

    def __init__(self, mean=0.0, stddev=0.05, seed=None):
        self.mean = mean
        self.stddev = stddev
        self.seed = seed

    def __call__(self, shape, dtype=tf.float32):
        return tf.random.truncated_normal(shape, self.mean, self.stddev, dtype=dtype, seed=self.seed)

    def get_config(self):
        return {"mean": self.mean, "stddev": self.stddev, "seed": self.seed}

def deconv2d_bilinear_upsampling_initializer(shape):
    """Returns the initializer that can be passed to DeConv2dLayer for initializing the
    weights in correspondence to channel-wise bilinear up-sampling.
    Used in segmentation approaches such as [FCN](https://arxiv.org/abs/1605.06211)

    Parameters
    ----------
    shape : tuple of int
        The shape of the filters, [height, width, output_channels, in_channels].
        It must match the shape passed to DeConv2dLayer.

    Returns
    -------
    ``tf.constant_initializer``
        A constant initializer with weights set to correspond to per channel bilinear upsampling
        when passed as W_int in DeConv2dLayer

    Examples
    --------
    - Upsampling by a factor of 2, ie e.g 100->200
    >>> import tensorflow as tf
    >>> import tensorlayer as tl
    >>> rescale_factor = 2
    >>> imsize = 128
    >>> num_channels = 3
    >>> num_in_channels = 3
    >>> num_out_channels = 3
    >>> filter_shape = (5, 5, num_out_channels, num_in_channels)
    >>> ni = tl.layers.Input(shape=(1, imsize, imsize, num_channels))
    >>> bilinear_init = deconv2d_bilinear_upsampling_initializer(shape=filter_shape)
    >>> net = tl.layers.DeConv2dLayer(
    ...                    shape=filter_shape,
    ...                    outputs_shape=(1, imsize*rescale_factor, imsize*rescale_factor, num_out_channels),
    ...                    strides=(1, rescale_factor, rescale_factor, 1),
    ...                    W_init=bilinear_init,
    ...                    padding='SAME',
    ...                    act=None, name='g/h1/decon2d')(ni)

    """
    if shape[0] != shape[1]:
        raise Exception('deconv2d_bilinear_upsampling_initializer only supports symmetrical filter sizes')

    if shape[3] < shape[2]:
        raise Exception(
            'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels '
        )

    filter_size = shape[0]
    num_out_channels = shape[2]
    num_in_channels = shape[3]

    # Create bilinear filter kernel as numpy array
    bilinear_kernel = np.zeros([filter_size, filter_size], dtype=np.float32)
    scale_factor = (filter_size + 1) // 2
    if filter_size % 2 == 1:
        center = scale_factor - 1
    else:
        center = scale_factor - 0.5
    for x in range(filter_size):
        for y in range(filter_size):
            bilinear_kernel[x, y] = (1 - abs(x - center) / scale_factor) * (1 - abs(y - center) / scale_factor)
    weights = np.zeros((filter_size, filter_size, num_out_channels, num_in_channels), dtype=np.float32)
    for i in range(num_out_channels):
        weights[:, :, i, i] = bilinear_kernel

    # assign numpy array to constant_initalizer and pass to get_variable
    return tf.constant_initializer(value=weights)

# Alias
zeros = Zeros
ones = Ones
constant = Constant
random_uniform = RandomUniform
random_normal = RandomNormal
truncated_normal = TruncatedNormal
<commit_msg>Update initializers.py<commit_after>#! /usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np
import tensorflow as tf

__all__ = [
    'Initializer', 'Zeros', 'Ones', 'Constant', 'RandomUniform', 'RandomNormal', 'TruncatedNormal',
    'deconv2d_bilinear_upsampling_initializer'
]

class Initializer(object):
    """Initializer base class: all initializers inherit from this class.
    """

    def __call__(self, shape, dtype=None):
        """Returns a tensor object initialized as specified by the initializer.

        Parameters
        ----------
        shape : tuple of int.
            The shape of the tensor.
        dtype : Optional dtype of the tensor.
            If not provided will return tensor of `tf.float32`.

        Returns
        -------

        """
        raise NotImplementedError

    def get_config(self):
        """Returns the configuration of the initializer as a JSON-serializable dict.

        Returns
        -------
            A JSON-serializable Python dict.
        """
        return {}

    @classmethod
    def from_config(cls, config):
        """Instantiates an initializer from a configuration dictionary.

        Parameters
        ----------
        config : A python dictionary.
            It will typically be the output of `get_config`.

        Returns
        -------
            An Initializer instance.
        """
        if 'dtype' in config:
            config.pop('dtype')
        return cls(**config)

class Zeros(Initializer):
    """Initializer that generates tensors initialized to 0.
    """

    def __call__(self, shape, dtype=tf.float32):
        return tf.zeros(shape, dtype=dtype)

class Ones(Initializer):
    """Initializer that generates tensors initialized to 1.
    """

    def __call__(self, shape, dtype=tf.float32):
        return tf.ones(shape, dtype=dtype)

class Constant(Initializer):
    """Initializer that generates tensors initialized to a constant value.

    Parameters
    ----------
    value : A python scalar or a numpy array.
        The assigned value.

    """

    def __init__(self, value=0):
        self.value = value

    def __call__(self, shape, dtype=None):
        return tf.constant(self.value, shape=shape, dtype=dtype)

    def get_config(self):
        return {"value": self.value}

class RandomUniform(Initializer):
    """Initializer that generates tensors with a uniform distribution.

    Parameters
    ----------
    minval : A python scalar or a scalar tensor.
        Lower bound of the range of random values to generate.
    maxval : A python scalar or a scalar tensor.
        Upper bound of the range of random values to generate.
    seed : A Python integer.
        Used to seed the random generator.

    """

    def __init__(self, minval=-0.05, maxval=0.05, seed=None):
        self.minval = minval
        self.maxval = maxval
        self.seed = seed

    def __call__(self, shape, dtype=tf.float32):
        return tf.random.uniform(shape, self.minval, self.maxval, dtype=dtype, seed=self.seed)

    def get_config(self):
        return {"minval": self.minval, "maxval": self.maxval, "seed": self.seed}

class RandomNormal(Initializer):
    """Initializer that generates tensors with a normal distribution.

    Parameters
    ----------
    mean : A python scalar or a scalar tensor.
        Mean of the random values to generate.
    stddev : A python scalar or a scalar tensor.
        Standard deviation of the random values to generate.
    seed : A Python integer.
        Used to seed the random generator.
    """

    def __init__(self, mean=0.0, stddev=0.05, seed=None):
        self.mean = mean
        self.stddev = stddev
        self.seed = seed

    def __call__(self, shape, dtype=tf.float32):
        return tf.random.normal(shape, self.mean, self.stddev, dtype=dtype, seed=self.seed)

    def get_config(self):
        return {"mean": self.mean, "stddev": self.stddev, "seed": self.seed}

class TruncatedNormal(Initializer):
    """Initializer that generates a truncated normal distribution.

    These values are similar to values from a `RandomNormal`
    except that values more than two standard deviations from the mean
    are discarded and re-drawn. This is the recommended initializer for
    neural network weights and filters.

    Parameters
    ----------
    mean : A python scalar or a scalar tensor.
        Mean of the random values to generate.
    stddev : A python scalar or a scalar tensor.
        Standard deviation of the andom values to generate.
    seed : A Python integer.
        Used to seed the random generator.
    """

    def __init__(self, mean=0.0, stddev=0.05, seed=None):
        self.mean = mean
        self.stddev = stddev
        self.seed = seed

    def __call__(self, shape, dtype=tf.float32):
        return tf.random.truncated_normal(shape, self.mean, self.stddev, dtype=dtype, seed=self.seed)

    def get_config(self):
        return {"mean": self.mean, "stddev": self.stddev, "seed": self.seed}

def deconv2d_bilinear_upsampling_initializer(shape):
    """Returns the initializer that can be passed to DeConv2dLayer for initializing the
    weights in correspondence to channel-wise bilinear up-sampling.
    Used in segmentation approaches such as [FCN](https://arxiv.org/abs/1605.06211)

    Parameters
    ----------
    shape : tuple of int
        The shape of the filters, [height, width, output_channels, in_channels].
        It must match the shape passed to DeConv2dLayer.

    Returns
    -------
    ``tf.constant_initializer``
        A constant initializer with weights set to correspond to per channel bilinear upsampling
        when passed as W_int in DeConv2dLayer

    Examples
    --------
    Upsampling by a factor of 2, ie e.g 100->200
    >>> import tensorflow as tf
    >>> import tensorlayer as tl
    >>> rescale_factor = 2
    >>> imsize = 128
    >>> num_channels = 3
    >>> num_in_channels = 3
    >>> num_out_channels = 3
    >>> filter_shape = (5, 5, num_out_channels, num_in_channels)
    >>> ni = tl.layers.Input(shape=(1, imsize, imsize, num_channels))
    >>> bilinear_init = deconv2d_bilinear_upsampling_initializer(shape=filter_shape)
    >>> net = tl.layers.DeConv2dLayer(
    ...                    shape=filter_shape,
    ...                    outputs_shape=(1, imsize*rescale_factor, imsize*rescale_factor, num_out_channels),
    ...                    strides=(1, rescale_factor, rescale_factor, 1),
    ...                    W_init=bilinear_init,
    ...                    padding='SAME',
    ...                    act=None, name='g/h1/decon2d')(ni)

    """
    if shape[0] != shape[1]:
        raise Exception('deconv2d_bilinear_upsampling_initializer only supports symmetrical filter sizes')

    if shape[3] < shape[2]:
        raise Exception(
            'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels '
        )

    filter_size = shape[0]
    num_out_channels = shape[2]
    num_in_channels = shape[3]

    # Create bilinear filter kernel as numpy array
    bilinear_kernel = np.zeros([filter_size, filter_size], dtype=np.float32)
    scale_factor = (filter_size + 1) // 2
    if filter_size % 2 == 1:
        center = scale_factor - 1
    else:
        center = scale_factor - 0.5
    for x in range(filter_size):
        for y in range(filter_size):
            bilinear_kernel[x, y] = (1 - abs(x - center) / scale_factor) * (1 - abs(y - center) / scale_factor)
    weights = np.zeros((filter_size, filter_size, num_out_channels, num_in_channels), dtype=np.float32)
    for i in range(num_out_channels):
        weights[:, :, i, i] = bilinear_kernel

    # assign numpy array to constant_initalizer and pass to get_variable
    return tf.constant_initializer(value=weights)

# Alias
zeros = Zeros
ones = Ones
constant = Constant
random_uniform = RandomUniform
random_normal = RandomNormal
truncated_normal = TruncatedNormal
<|endoftext|>