<commit_before>import urllib2
from urllib import urlencode
import simplejson as json
import sys, re

_API_URL = "https://www.comicvine.com/api/"

_cached_resources = {}

api_key = ""

class InvalidResourceError(Exception):
    pass

class InvalidAPIKeyError(Exception):
    pass

class ObjectNotFoundError(Exception):
    pass

class ErrorInURLFormatError(Exception):
    pass

class JSONError(Exception):
    pass

class FilterError(Exception):
    pass

class SubscriberOnlyError(Exception):
    pass

class UnknownStatusError(Exception):
    pass

class IllegalArquementException(Exception):
    pass

_EXCEPTIONS = {
        100: InvalidAPIKeyError,
        101: ObjectNotFoundError,
        102: ErrorInURLFormatError,
        103: JSONError,
        104: FilterError,
        105: SubscriberOnlyError,
    }

class _Resource(object):
    class _Response:
        def __init__(
                self,
                error,
                limit,
                offset,
                number_of_page_results,
                number_of_total_results,
                status_code,
                version,
                results
            ):
            self.error = error
            self.limit = limit
            self.offset = offset
            self.number_of_page_results = number_of_page_results
            self.number_of_total_results = number_of_total_results
            self.status_code = status_code
            self.results = results

    def __init__(self, *args, **kwargs):
        raise NotImplemented()

    def _request_object(self, baseurl, **params):
        raise NotImplemented()

    @classmethod
    def _ensure_resource_url(type):
        if not '_resource_url' in type.__dict__:
            resource_type = type.__name__.lower()
            type._resource_url = _API_URL + resource_type + "/"

    @classmethod
    def _request(type, baseurl, **params):
        if 'api_key' not in params:
            if len(api_key) == 0:
                raise InvalidAPIKeyError(
                        "Invalid API Key"
                    )
            params['api_key'] = api_key
        if 'field_list' in params and params['field_list'] != None:
            if not isinstance(params['field_list'], (str, unicode)):
                field_list = ""
                try:
                    for field_name in params['field_list']:
                        field_list += str(field_name)+","
                except TypeError, e:
                    raise IllegalArquementException(
                            "'field_list' must be iterable"
                        )
                params['field_list'] = field_list
            if re.search(
                    "([a-z_]*,)*id(,[a-z_]*)*", params['field_list']
                ) == None:
                params['field_list'] = "id," + params['field_list']
        params['format'] = 'json'
        params = urlencode(params)
        response = type._Response(**json.loads(urllib2.urlopen(
                baseurl+"?"+params
            ).read()))
        if response.status_code != 1:
            raise _EXCEPTIONS.get(response.status_code,UnknownStatusError)(
                    response.error
                )
        if 'aliases' in response.results:
            response.results['aliases'] = response.results[
                    'aliases'
                ].split('\n')
        return response

class _SingularResource(_Resource):
    def __new__(type, id, **kwargs):
        resource_type = kwargs.get(
                'resource_type',
                type.__name__.lower()
            )
        try:
            type_id = Types()[resource_type]['id']
        except KeyError:
            return InvalidResourceError(resource_type)
        type._ensure_resource_url()
        key = "{0:d}-{1:d}".format(type_id, id)
        obj = _cached_resources.get(key)
        if obj == None:
            obj = object.__new__(type, id, **kwargs)
            _cached_resources[key] = obj
        return obj

    def __init__(self, id, **kwargs):
        if '_ready' not in self.__dict__:
            self._ready = True
            resource_type = type(self).__name__.lower()
            try:
                type_id = Types()[resource_type]['id']
            except KeyError:
                raise InvalidResourceError(
                        "Resource type '{0!s}' does not exist.".format(
                                resource_type
                            )
                    )
            if 'api_detail_url' in kwargs:
                self._detail_url = kwargs['api_detail_url']
            else:
                self._detail_url = type(self)._resource_url + \
                        "{0:d}-{1:d}/".format(type_id, id)
            self._fields = {'id': id}
            if 'field_list' in kwargs:
                self._fields.update(self._request_object(
                        kwargs['field_list']
                    ).results)
                del kwargs['field_list']
            self._fields.update(kwargs)
        elif 'field_list' in kwargs:
            self._fields.update(self._request_object(
                    kwargs['field_list']
                ).results)

    def _request_object(self, field_list = None):
        return type(self)._request(
                self._detail_url,
                field_list=field_list
            )

    def __getattribute__(self, name):
        def _object_attribute(name):
            return object.__getattribute__(self, name)
        try:
            if name not in ['__dict__', '_request_object'] and \
                    name not in self.__dict__:
                if name in _object_attribute('_fields'):
                    return _object_attribute('_fields')[name]
                else:
                    self._fields.update(
                            _object_attribute('_request_object')(
                                    [name]
                                ).results
                        )
                    return _object_attribute('_fields')[name]
        except KeyError:
            pass
        return _object_attribute(name)

class _ListResource(_Resource):
    def _request_object(self, **params):
        type(self)._ensure_resource_url()
        return type(self)._request(type(self)._resource_url, **params)

    def __init__(self, **kwargs):
        response = self._request_object(**kwargs)
        self._results = response.offset*[None] + response.results
        self._total = response.number_of_total_results
        self._limit = response.limit
        if 'limit' in kwargs:
            del kwargs['limit']
        if 'offset' in kwargs:
            del kwargs['offset']
        self._args = kwargs

    def __len__(self):
        return self._total

    def __getitem__(self, index):
        if type(index) == slice:
            start = index.start or 0
            stop = index.stop or self._total
            step = index.step or 1
        else:
            start = index
            stop = index+1
            step = 1
        if start < 0 or start >= self._total or stop < 0 or \
                stop > self._total:
            raise IndexError['Index out of range']
        if len(self._results) < stop or \
                None in self._results[start:stop:step]:
            for i in range(start, stop, 100):
                response = self._request_object(
                        limit=self._limit,
                        offset=i,
                        **self._args
                    )
                self._results[i:self._limit+i] = response.results
        if type(self._results[index]) == list:
            for i in range(start, stop, step):
                if type(self._results[i]) == dict:
                    self._parse_result(i)
        elif type(self._results[index]) == dict:
            self._parse_result(index)
        return self._results[index]

    def __iter__(self):
        for index in xrange(self._total):
            yield self[index]

    def __str__(self):
        string = "["
        for element in self[:100]:
            string += str(element)+","
        string = string[:-1]+"]"
        return string

    def _parse_result(self, index):
        if type(self) != Types:
            type_dict = Types()
            if type(self) == Search:
                self._results[index] = type_dict.singular_resource_class(
                        self._results[index]['resource_type']
                    )(**self._results[index])
            else:
                resource_type = type(self).__name__.lower()
                self._results[index] = type_dict.singular_resource_class(
                        resource_type
                    )(**self._results[index])

class _SortableListResource(_ListResource):
    def __init__(self, sort = None, **kwargs):
        if sort != None:
            if isinstance(sort, (str, unicode)):
                if ':' not in sort:
                    sort += ":asc"
            else:
                try:
                    sort = str(sort[0])+":"+str(sort[1])
                except KeyError:
                    if 'field' not in sort:
                        raise IllegalArquementException(
                                "Argument 'sort' must contain item 'field'"
                            )
                    if 'direction' in sort:
                        sort = sort['field']+":"+str(sort['direction'])
                    else:
                        sort = sort['field']+":asc"
            kwargs['sort'] = sort
        super(_SortableListResource, self).__init__(**kwargs)

class Search(_ListResource):
    def __init__(self, resources, query, **kwargs):
        super(Search, self).__init__(
                resources=resources,
                query=query,
                **kwargs
            )

class Types(_ListResource):
    def __new__(type):
        if not '_instance' in type.__dict__:
            type._instance = object.__new__(type)
        return type._instance

    def __init__(self):
        if not '_ready' in dir(self):
            super(Types, self).__init__()
            self._mapping = {}
            for type in self:
                self._mapping[type['detail_resource_name']] = type
                self._mapping[type['list_resource_name']] = type
            self._ready = True

    def __getitem__(self, key):
        if type(key) in [int, long, slice]:
            return super(Types, self).__getitem__(key)
        return self._mapping[key]

    def singular_resource_class(self, key):
        key = key.lower()
        if key not in self._mapping:
            raise InvalidResourceError(key)
        classname = self._mapping[key]['detail_resource_name']
        classname = classname[0].upper() + classname[1:].lower()
        return getattr(sys.modules[__name__], classname)
<commit_msg>Add capability to initialise list resource without API<commit_after>import urllib2
from urllib import urlencode
import simplejson as json
import sys, re

_API_URL = "https://www.comicvine.com/api/"

_cached_resources = {}

api_key = ""

class InvalidResourceError(Exception):
    pass

class InvalidAPIKeyError(Exception):
    pass

class ObjectNotFoundError(Exception):
    pass

class ErrorInURLFormatError(Exception):
    pass

class JSONError(Exception):
    pass

class FilterError(Exception):
    pass

class SubscriberOnlyError(Exception):
    pass

class UnknownStatusError(Exception):
    pass

class IllegalArquementException(Exception):
    pass

_EXCEPTIONS = {
        100: InvalidAPIKeyError,
        101: ObjectNotFoundError,
        102: ErrorInURLFormatError,
        103: JSONError,
        104: FilterError,
        105: SubscriberOnlyError,
    }

class _Resource(object):
    class _Response:
        def __init__(
                self,
                error,
                limit,
                offset,
                number_of_page_results,
                number_of_total_results,
                status_code,
                version,
                results
            ):
            self.error = error
            self.limit = limit
            self.offset = offset
            self.number_of_page_results = number_of_page_results
            self.number_of_total_results = number_of_total_results
            self.status_code = status_code
            self.results = results

    def __init__(self, *args, **kwargs):
        raise NotImplemented()

    def _request_object(self, baseurl, **params):
        raise NotImplemented()

    @classmethod
    def _ensure_resource_url(type):
        if not '_resource_url' in type.__dict__:
            resource_type = type.__name__.lower()
            type._resource_url = _API_URL + resource_type + "/"

    @classmethod
    def _request(type, baseurl, **params):
        if 'api_key' not in params:
            if len(api_key) == 0:
                raise InvalidAPIKeyError(
                        "Invalid API Key"
                    )
            params['api_key'] = api_key
        if 'field_list' in params and params['field_list'] != None:
            if not isinstance(params['field_list'], (str, unicode)):
                field_list = ""
                try:
                    for field_name in params['field_list']:
                        field_list += str(field_name)+","
                except TypeError, e:
                    raise IllegalArquementException(
                            "'field_list' must be iterable"
                        )
                params['field_list'] = field_list
            if re.search(
                    "([a-z_]*,)*id(,[a-z_]*)*", params['field_list']
                ) == None:
                params['field_list'] = "id," + params['field_list']
        params['format'] = 'json'
        params = urlencode(params)
        response = type._Response(**json.loads(urllib2.urlopen(
                baseurl+"?"+params
            ).read()))
        if response.status_code != 1:
            raise _EXCEPTIONS.get(response.status_code,UnknownStatusError)(
                    response.error
                )
        if 'aliases' in response.results:
            response.results['aliases'] = response.results[
                    'aliases'
                ].split('\n')
        return response

class _SingularResource(_Resource):
    def __new__(type, id, **kwargs):
        resource_type = kwargs.get(
                'resource_type',
                type.__name__.lower()
            )
        try:
            type_id = Types()[resource_type]['id']
        except KeyError:
            return InvalidResourceError(resource_type)
        type._ensure_resource_url()
        key = "{0:d}-{1:d}".format(type_id, id)
        obj = _cached_resources.get(key)
        if obj == None:
            obj = object.__new__(type, id, **kwargs)
            _cached_resources[key] = obj
        return obj

    def __init__(self, id, **kwargs):
        if '_ready' not in self.__dict__:
            self._ready = True
            resource_type = type(self).__name__.lower()
            try:
                type_id = Types()[resource_type]['id']
            except KeyError:
                raise InvalidResourceError(
                        "Resource type '{0!s}' does not exist.".format(
                                resource_type
                            )
                    )
            if 'api_detail_url' in kwargs:
                self._detail_url = kwargs['api_detail_url']
            else:
                self._detail_url = type(self)._resource_url + \
                        "{0:d}-{1:d}/".format(type_id, id)
            self._fields = {'id': id}
            if 'field_list' in kwargs:
                self._fields.update(self._request_object(
                        kwargs['field_list']
                    ).results)
                del kwargs['field_list']
            self._fields.update(kwargs)
        elif 'field_list' in kwargs:
            self._fields.update(self._request_object(
                    kwargs['field_list']
                ).results)

    def _request_object(self, field_list = None):
        return type(self)._request(
                self._detail_url,
                field_list=field_list
            )

    def __getattribute__(self, name):
        def _object_attribute(name):
            return object.__getattribute__(self, name)
        try:
            if name not in ['__dict__', '_request_object'] and \
                    name not in self.__dict__:
                if name in _object_attribute('_fields'):
                    return _object_attribute('_fields')[name]
                else:
                    self._fields.update(
                            _object_attribute('_request_object')(
                                    [name]
                                ).results
                        )
                    return _object_attribute('_fields')[name]
        except KeyError:
            pass
        return _object_attribute(name)

class _ListResource(_Resource):
    def _request_object(self, **params):
        type(self)._ensure_resource_url()
        return type(self)._request(type(self)._resource_url, **params)

    def __init__(self, init_list = None, **kwargs):
        if init_list != None:
            if len(kwargs) > 0:
                raise TypeError(
                        "If 'init_list' is given it is the only "+
                        "allowed argument"
                    )
            self._results = init_list
            self._total = len(init_list)
            self._limit = len(init_list)
        else:
            response = self._request_object(**kwargs)
            self._results = response.offset*[None] + response.results
            self._total = response.number_of_total_results
            self._limit = response.limit
            if 'limit' in kwargs:
                del kwargs['limit']
            if 'offset' in kwargs:
                del kwargs['offset']
            self._args = kwargs

    def __len__(self):
        return self._total

    def __getitem__(self, index):
        if type(index) == slice:
            start = index.start or 0
            stop = index.stop or self._total
            step = index.step or 1
        else:
            start = index
            stop = index+1
            step = 1
        if start < 0 or start >= self._total or stop < 0 or \
                stop > self._total:
            raise IndexError['Index out of range']
        if len(self._results) < stop or \
                None in self._results[start:stop:step]:
            for i in range(start, stop, 100):
                response = self._request_object(
                        limit=self._limit,
                        offset=i,
                        **self._args
                    )
                self._results[i:self._limit+i] = response.results
        if type(self._results[index]) == list:
            for i in range(start, stop, step):
                if type(self._results[i]) == dict:
                    self._parse_result(i)
        elif type(self._results[index]) == dict:
            self._parse_result(index)
        return self._results[index]

    def __iter__(self):
        for index in xrange(self._total):
            yield self[index]

    def __str__(self):
        string = "["
        for element in self[:100]:
            string += str(element)+","
        string = string[:-1]+"]"
        return string

    def _parse_result(self, index):
        if type(self) != Types:
            type_dict = Types()
            if type(self) == Search:
                self._results[index] = type_dict.singular_resource_class(
                        self._results[index]['resource_type']
                    )(**self._results[index])
            else:
                resource_type = type(self).__name__.lower()
                self._results[index] = type_dict.singular_resource_class(
                        resource_type
                    )(**self._results[index])

class _SortableListResource(_ListResource):
    def __init__(self, init_list = [], sort = None, **kwargs):
        if sort != None:
            if isinstance(sort, (str, unicode)):
                if ':' not in sort:
                    sort += ":asc"
            else:
                try:
                    sort = str(sort[0])+":"+str(sort[1])
                except KeyError:
                    if 'field' not in sort:
                        raise IllegalArquementException(
                                "Argument 'sort' must contain item 'field'"
                            )
                    if 'direction' in sort:
                        sort = sort['field']+":"+str(sort['direction'])
                    else:
                        sort = sort['field']+":asc"
            kwargs['sort'] = sort
        super(_SortableListResource, self).__init__(init_list, **kwargs)

class Search(_ListResource):
    def __init__(self, resources, query, **kwargs):
        super(Search, self).__init__(
                resources=resources,
                query=query,
                **kwargs
            )

class Types(_ListResource):
    def __new__(type):
        if not '_instance' in type.__dict__:
            type._instance = object.__new__(type)
        return type._instance

    def __init__(self):
        if not '_ready' in dir(self):
            super(Types, self).__init__()
            self._mapping = {}
            for type in self:
                self._mapping[type['detail_resource_name']] = type
                self._mapping[type['list_resource_name']] = type
            self._ready = True

    def __getitem__(self, key):
        if type(key) in [int, long, slice]:
            return super(Types, self).__getitem__(key)
        return self._mapping[key]

    def singular_resource_class(self, key):
        key = key.lower()
        if key not in self._mapping:
            raise InvalidResourceError(key)
        classname = self._mapping[key]['detail_resource_name']
        classname = classname[0].upper() + classname[1:].lower()
        return getattr(sys.modules[__name__], classname)
<|endoftext|>


<commit_before>from django.http import HttpResponse,HttpResponseRedirect
from django.template import RequestContext, loader
from django.shortcuts import render
from django.conf import settings
from django.db import models
from django import forms
from .forms import SearchForm
from importlib import import_module
import pykemon,simplejson
from pykemon import api,request,models

from .models import Pokemon, Moves, Abilities

def valid_address(request,form):
    poke = request.session.get('pokename')
    if(form.is_valid()):
        r = form.cleaned_data['search_query']
        print("search_query: " + r)
        try:
            poke = pykemon.get(pokemon=r)
            print("poke.name" + poke.name)

#             try:

            p = Pokemon(name=poke.name, id=poke.id,
                        description=poke.descriptions, egg_group=poke.egg_groups,)

            i = 0
            kv=""
            p.save()
            m = poke.moves
            a = poke.abilities
            for f in m:
                butt = Moves.objects.create(move=f)
                butt.save()

            for f in a:
                ding = Abilities.objects.create(ability=f)
                ding.save()

            print("Here")
            print(p.name + " a " + p.type1 + " Pokemon!")

            request.session['pokename'] = poke.name
            print("poke.name: " + poke.name)
            print("session pokename: " + request.session.get('pokename') )

        except(pykemon.ResourceNotFoundError):
            print("Why does this keep happening?") # INADEQUATE VALIDATION YOU FUCKER

        print("True")
        return True

    print("False outer")
    return False

def meth_is_POST(request,form):
    if request.method == 'POST':
        print("form")
        foo = valid_address(request,form)
        if  foo == False:
#             move = ""
#             egg_group = ""
#             form = SearchForm()
#             context = RequestContext(request, { 'move': move,
#                                            'egg_group': egg_group,'form': form,
#                                            })
#
#             return context
            return True
        return True
    else:
        return False

def index(request):
    if request.method == 'POST':
        form = SearchForm(request.POST)

        if(form.is_valid()):
            return HttpResponseRedirect('/')

    else:
        form = SearchForm()

    return render(request, 'pokepy/index.html', {'form': form})

def search(request):
    form = SearchForm(request.POST)
    template = loader.get_template('pokepy/search.html')

    t = meth_is_POST(request, form)

    if t == True:
        print("True blue")
    elif t == False:
        return HttpResponseRedirect('/')
    else:
        context = t
        return HttpResponse(template.render(context))
    n = request.session.get('pokename')
    p = Pokemon.objects.get(name__contains=n)
    print("p: " + p.name )
    try:
        form = SearchForm()

        print(p.name)

        o = (p.name + "_gen_6")
        des_key = p.description.get(o)
        print(o)
        i=5
        while(i>0):
            try:
                o = (p.name + "_gen_" + i)
                des_key = p.description.get(o)
                i-=5
            except AttributeError:
                i-=1

        print(des_key)

        description = pykemon.get(description = des_key)
        print(description)
        print("am i even")
        context = RequestContext(request, {'name':p.name, 'description':description,
            'form': form,
        })

        print(context)
        return HttpResponse(template.render(context))

    except AttributeError:
        return HttpResponseRedirect('/')

    <commit_msg>sweet god almighty i've unearthed a bug mountain<commit_after>from django.http import HttpResponse,HttpResponseRedirect
from django.template import RequestContext, loader
from django.shortcuts import render
from django.conf import settings
from django.db import models
from django import forms
from .forms import SearchForm
from importlib import import_module
import pykemon,simplejson
from pykemon import api,request,models

from .models import Pokemon, Moves, Abilities
from pykemon.exceptions import ResourceNotFoundError

def valid_address(request,form):
    poke = request.session.get('pokename')
    if(form.is_valid()):
        r = form.cleaned_data['search_query']
        print("search_query: " + r)
        try:
            poke = pykemon.get(pokemon=r)
            print("poke.name" + poke.name)

#             try:

            p = Pokemon(name=poke.name, id=poke.id,
                        description=poke.descriptions, egg_group=poke.egg_groups,)

            i = 0
            kv=""
            p.save()
            m = poke.moves
            a = poke.abilities
            for f in m:
                butt = Moves.objects.create(move=f)
                butt.save()

            for f in a:
                ding = Abilities.objects.create(ability=f)
                ding.save()

            print("Here")
            print(p.name + " a " + p.type1 + " Pokemon!")

            request.session['pokename'] = poke.name
            print("poke.name: " + poke.name)
            print("session pokename: " + request.session.get('pokename') )

        except(pykemon.ResourceNotFoundError):
            print("Why does this keep happening?") # INADEQUATE VALIDATION YOU FUCKER

        print("True")
        return True

    print("False outer")
    return False

def meth_is_POST(request,form):
    if request.method == 'POST':
        print("form")
        foo = valid_address(request,form)
        if  foo == False:
#             move = ""
#             egg_group = ""
#             form = SearchForm()
#             context = RequestContext(request, { 'move': move,
#                                            'egg_group': egg_group,'form': form,
#                                            })
#
#             return context
            return True
        return True
    else:
        return False

def index(request):
    if request.method == 'POST':
        form = SearchForm(request.POST)

        if(form.is_valid()):
            return HttpResponseRedirect('/')

    else:
        form = SearchForm()

    return render(request, 'pokepy/index.html', {'form': form})

def des_key_validate(des_key,x):
    print("des_key: " + des_key)
    print("x: " + x)
    try:
        d = pykemon.get(description_id=des_key)
    except ResourceNotFoundError:
        return False
    return d

def search(request):
    form = SearchForm(request.POST)
    template = loader.get_template('pokepy/search.html')

    t = meth_is_POST(request, form)

    if t == True:
        print("True blue")
    elif t == False:
        return HttpResponseRedirect('/')
    else:
        context = t
        return HttpResponse(template.render(context))
    n = request.session.get('pokename')
    print('n: ' + n)
    p = Pokemon.objects.get(name__contains=n)
    print("p: " + p.name )
    try:

        print("p.name: " + p.name)
        x = p.name.lower()
        o = (x + "_gen_6")
        print("o: " + o)
        i=5
        des_key=""
        description=""
        gen = "_gen_"
        while(i>0):
            h = str(i)

            try:
                des_key = p.description.get( o )
                description = des_key_validate( des_key, x )
                if(description != False):
                    break
            except ResourceNotFoundError:
                i-=1
                print(i)
            o = (x + gen + h)

        print(des_key)

#         description = pykemon.get(description = des_key)
        print(description)
        print("am i even")
        context = RequestContext(request, {'name':p.name, 'description':description,
            'form': form,
        })

        print(context)
        return HttpResponse(template.render(context))

    except AttributeError:
        return HttpResponseRedirect('/')

    <|endoftext|>


<commit_before>'''
Implement continuous wavelet transform for python
'''

import scipy.signal
import numpy as np
import pywt
import matplotlib.pyplot as plt

class CWT:
	def __init__(self , waveletType):
		self.wavelet = pywt.Wavelet(waveletType)
    	[self.phi, self.psi, self.x] = wavelet.wavefun(4)
    	# Compute the central frequency
        fft = np.fft.fft(psi)
    	freqs = np.fft.fftfreq(len(fft) , x[1]-x[0])
        idx = np.nonzero(freqs >= 0)[0] # Only consider positive frequencies
        freqs = freqs[idx]
        fft = fft[idx]
        idx = np.nonzero(fft == np.max(fft))[0]
        self.centralFreq = freqs[idx][0]

    def centralFrequency():
        return self.centralFreq

    def getWavelet(self, scale, dt, time, data):
        basis = x * self.scale
        basis = basis * dt
        yVals = self.psi/np.sqrt(scale)
        delta = basis[1] - basis[0]
        if max(basis) < max(time):
            basis = np.arange(0,len(yVals))*delta
            signal = data
            modTime = time
            idx = np.nonzero(modTime <= np.max(basis))[0]
            return np.interp(modTime[idx] , basis, yVals), signal, modTime
        else:
            num_pads = np.ceil((max(basis) - max(time))/dt)
            num_pads = max(0,num_pads)
            postPads = np.zeros(num_pads)
            signal = np.concatenate((data,postPads))
            modTime = np.arange(0,len(signal))*dt
            return np.interp(modTime , basis, yVals), signal

    def transform(self, data, dt, scales, scaleFactor):
        time = np.array(range(len(data)))*dt
        output = np.zeros([len(scales), len(data)])
        for idx, scale in enumerate(scales):
            wavelet, signal = getWavelet(scale , dt, time, data)
            output[idx, :] = scipy.signal.convolve(signal, wavelet, mode='same')[:len(data)]
        return output

    def scaleToFreq(self , scale, dt = 1):
        return self.centralFreq/(scale * dt)

    def freqToScale(self, freq, dt = 1):
        return self.centralFreq/(dt * freq)

    def plot(self, data, dt, scales, coefs):
        time = np.arange(0,len(data))*dt
        X, Y = np.meshgrid(time, scales)
        plt.subplot(2,1,1)
        plt.plot(time , data)
        plt.subplot(2,1,2)
        plt.pcolor(X, Y, coefs**2)
        plt.show()

    def savePlot(self, data, dt, scales, coefs, fname):
        time = np.arange(0,len(data))*dt
        X, Y = np.meshgrid(time, scales)
        plt.subplot(2,1,1)
        plt.plot(time , data)
        plt.subplot(2,1,2)
        plt.pcolor(X, Y, coefs**2)
        plt.savefig(fname)
        plt.close()
<commit_msg>fixed some bugs<commit_after>'''
Implement continuous wavelet transform for python
'''

import scipy.signal
import numpy as np
import pywt
import matplotlib.pyplot as plt

class CWT:
    def __init__(self , waveletType):
        self.wavelet = pywt.Wavelet(waveletType)
        [self.phi, self.psi, self.x] = self.wavelet.wavefun(4)
        # Compute the central frequency
        fft = np.fft.fft(self.psi)
        freqs = np.fft.fftfreq(len(fft) , self.x[1] - self.x[0])
        idx = np.nonzero(freqs >= 0)[0] # Only consider positive frequencies
        freqs = freqs[idx]
        fft = fft[idx]
        idx = np.nonzero(fft == np.max(fft))[0]
        self.centralFreq = freqs[idx][0]

    def centralFrequency():
        return self.centralFreq

    def getWavelet(self, scale, dt, time, data):
        basis = self.x * scale
        basis = basis * dt
        yVals = self.psi/np.sqrt(scale)
        delta = basis[1] - basis[0]
        if max(basis) < max(time):
            basis = np.arange(0,len(yVals))*delta
            signal = data
            modTime = time
            idx = np.nonzero(modTime <= np.max(basis))[0]
            return np.interp(modTime[idx] , basis, yVals), signal
        else:
            num_pads = np.ceil((max(basis) - max(time))/dt)
            num_pads = max(0,num_pads)
            postPads = np.zeros(num_pads)
            signal = np.concatenate((data,postPads))
            modTime = np.arange(0,len(signal))*dt
            return np.interp(modTime , basis, yVals), signal

    def transform(self, data, dt, scales):
        time = np.array(range(len(data)))*dt
        output = np.zeros([len(scales), len(data)])
        print 'Working on',
        for idx, scale in enumerate(scales):
            print idx,
            wavelet, signal = self.getWavelet(scale , dt, time, data)
            output[idx, :] = scipy.signal.convolve(signal, wavelet, mode='same')[:len(data)]
        print 'Done'
        return output

    def scaleToFreq(self , scale, dt = 1):
        return self.centralFreq/(scale * dt)

    def freqToScale(self, freq, dt = 1):
        return self.centralFreq/(dt * freq)

    def plot(self, data, dt, periods, coefs):
        time = np.arange(0,len(data))*dt
        X, Y = np.meshgrid(time, periods)
        plt.subplot(2,1,1)
        plt.plot(time , data)
        plt.subplot(2,1,2)
        plt.pcolor(X, Y, coefs**2)
        plt.yscale('log')
        plt.show()

    def savePlot(self, data, dt, scales, coefs, fname):
        time = np.arange(0,len(data))*dt
        X, Y = np.meshgrid(time, scales)
        plt.subplot(2,1,1)
        plt.plot(time , data)
        plt.subplot(2,1,2)
        plt.pcolor(X, Y, coefs**2)
        plt.yscale('log')
        plt.savefig(fname)
        plt.close()
<|endoftext|>


<commit_before>"""
    Example usage of Memcache class:

    >>> from pony import caching
    >>> cache = caching.Memcache()
    >>> cache.set('k1', 'v1')
    True
    >>> cache.set('k2', 'v2', 10)
    True
    >>> cache.set('k3', 'v3')
    True
    >>> cache.set('k4', 'v4')
    True
    >>> cache.items()
    [('k4', 'v4'), ('k3', 'v3'), ('k2', 'v2'), ('k1', 'v1')]
    >>> cache.get('k2')
    'v2'
    >>> from time import sleep; sleep(10)
    >>> print cache.get('k2')
    None
    >>> cache.items()
    [('k4', 'v4'), ('k3', 'v3'), ('k1', 'v1')]
    >>> cache.add('k5', 'v5')
    True
    >>> cache.add('k5', 'v6')
    False
    >>> cache.items()
    [('k5', 'v5'), ('k4', 'v4'), ('k3', 'v3'), ('k1', 'v1')]
    >>> print cache.incr('x')
    None
    >>> cache.set('x', '100')
    True
    >>> cache.incr('x')
    101
    >>> cache.decr('x', 10)
    91
    >>> cache.get('k3')
    'v3'
    >>> cache.items()
    [('k3', 'v3'), ('x', '91'), ('k5', 'v5'), ('k4', 'v4'), ('k1', 'v1')]
    >>> len(cache)
    5
    >>> 'x' in cache
    True
    >>> cache.replace('k4', 'v4_new')
    True
    >>> cache.replace('notexists', 'value')
    False
    >>> cache.items()
    [('k4', 'v4_new'), ('k3', 'v3'), ('x', '91'), ('k5', 'v5'), ('k1', 'v1')]
"""

from heapq import heappush, heappop, heapify
from threading import Lock
from time import time
from weakref import ref

class Node(object):
    __slots__ = 'prev', 'next', 'expire', 'key', 'value', '__weakref__'

def normalize(key, value="", expire=None):
    if isinstance(key, tuple): hash_value, key = key
    elif not isinstance(key, str): raise ValueError('Key must be tuple or string. Got: %s' % key.__class__.__name__)
    if not isinstance(value, str): raise ValueError('Value must be string. Got: %s' % value.__class__.__name__)
    if len(value) > 1024 * 1024: raise ValueError('Value size too big: %d' % len(value))
    if expire is not None: expire = time() + expire
    return key, value, expire

class Memcache(object):
    def __init__(self, max_data_size=64*1024*1024):
        self.dict = {}
        list = self.list = Node()
        list.prev = list.next = list.expire = list.key = list.value = None
        list.prev = list.next = list
        self.lock = Lock()
        self.heap = []
        self.data_size = 0
        self.max_data_size = max_data_size
    def __len__(self):
        return len(self.dict)
    def __contains__(self, key):
        return key in self.dict
    def __iter__(self):
        return iter(self.items())
    def items(self):
        now = time()
        result = []
        append = result.append
        list = self.list
        self.lock.acquire()
        try:
            node = list.prev
            while node is not list:
                expire = node.expire
                if expire is not None and expire <= now: self._delete_node(node)
                else: append((node.key, node.value))
                node = node.prev
        finally: self.lock.release()
        return result
    def _delete_node(self, node):
        prev, next = node.prev, node.next
        prev.next = next
        next.prev = prev
        del self.dict[node.key]
        self.data_size -= (len(node.key) + len(node.value))
    def _find_node(self, key):
        node = self.dict.get(key)
        if node is None: return None
        prev, next = node.prev, node.next
        prev.next = next
        next.prev = prev
        expire = node.expire
        if expire is None or expire > time(): return node
        del self.dict[node.key]
        self.data_size -= (len(node.key) + len(node.value))
        return None
    def _create_node(self, key):
        self.dict[key] = node = Node()
        node.key = key
        node.value = None
        return node
    def _place_on_top(self, node):
        list = self.list
        node.next = list
        node.prev = prev_top = list.prev
        list.prev = prev_top.next = node
    def _set_node_value(self, node, value, expire):
        prev_value = node.value
        node.value, node.expire = value, expire
        if prev_value is None:
            self.data_size += len(node.key)
            self.data_size += len(value)
        else:
            self.data_size -= len(prev_value)
            self.data_size += len(value)
        if expire is not None: heappush(self.heap, (expire, ref(node)))
        self._delete_expired_nodes()
        self._conform_to_limits()
        if len(self.heap) > len(self.dict) * 2: self._pack_heap()
    def _delete_expired_nodes(self):
        now = time()
        heap = self.heap
        while heap:
            expire, node_ref = heap[0]
            if expire > now: break
            heappop(heap)
            node = node_ref()
            if node is not None and expire == node.expire: self._delete_node(node)
    def _conform_to_limits(self):
        list = self.list
        while self.data_size > self.max_data_size:
            bottom = list.next
            self._delete_node(bottom)
    def _pack_heap(self):
        new_heap = []
        for item in self.heap:
            expire, node_ref = item
            node = node_ref()
            if node is not None and expire == node.expire: new_heap.append(item)
        heapify(new_heap)
        self.heap = new_heap
    def get(self, key):
        key, _, _ = normalize(key)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: return None
            self._place_on_top(node)
            return node.value
        finally: self.lock.release()
    def set(self, key, value, expire=None):
        key, value, expire = normalize(key, value, expire)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: node = self._create_node(key)
            self._place_on_top(node)
            self._set_node_value(node, value, expire)
        finally: self.lock.release()
        return True
    def add(self, key, value, expire=None):
        key, value, expire = normalize(key, value, expire)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node:
                self._place_on_top(node)
                return False
            node = self._create_node(key)
            self._place_on_top(node)
            self._set_node_value(node, value, expire)
        finally: self.lock.release()
        return True
    def replace(self, key, value, expire=None):
        key, value, expire = normalize(key, value, expire)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: return False
            self._place_on_top(node)
            self._set_node_value(node, value, expire)
        finally: self.lock.release()
        return True
    def delete(self, key):
        key, _, _ = normalize(key)
        node = self._find_node(key)
        if node is None: return 1
        self._delete_node(node)
        return 2
    def incr(self, key, delta=1):
        key, _, _ = normalize(key)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: return None
            self._place_on_top(node)
            try: value = int(node.value) + delta
            except ValueError: return None
            node.value = str(value)
        finally: self.lock.release()
        return value
    def decr(self, key, delta=1):
        key, _, _ = normalize(key)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: return None
            self._place_on_top(node)
            try: value = int(node.value) - delta
            except ValueError: return None
            node.value = str(value)
        finally: self.lock.release()
        return value
    def flush_all(self):
        self.lock.acquire()
        try:
            self.dict.clear()
            self.heap = []
            self.list.prev = self.list.next = self.list
            for node in self.dict.itervalues():
                node.prev = node.next = None
        finally: self.lock.release()
<commit_msg>Swap meaning of node.prev and node.next<commit_after>"""
    Example usage of Memcache class:

    >>> from pony import caching
    >>> cache = caching.Memcache()
    >>> cache.set('k1', 'v1')
    True
    >>> cache.set('k2', 'v2', 10)
    True
    >>> cache.set('k3', 'v3')
    True
    >>> cache.set('k4', 'v4')
    True
    >>> cache.items()
    [('k4', 'v4'), ('k3', 'v3'), ('k2', 'v2'), ('k1', 'v1')]
    >>> cache.get('k2')
    'v2'
    >>> from time import sleep; sleep(10)
    >>> print cache.get('k2')
    None
    >>> cache.items()
    [('k4', 'v4'), ('k3', 'v3'), ('k1', 'v1')]
    >>> cache.add('k5', 'v5')
    True
    >>> cache.add('k5', 'v6')
    False
    >>> cache.items()
    [('k5', 'v5'), ('k4', 'v4'), ('k3', 'v3'), ('k1', 'v1')]
    >>> print cache.incr('x')
    None
    >>> cache.set('x', '100')
    True
    >>> cache.incr('x')
    101
    >>> cache.decr('x', 10)
    91
    >>> cache.get('k3')
    'v3'
    >>> cache.items()
    [('k3', 'v3'), ('x', '91'), ('k5', 'v5'), ('k4', 'v4'), ('k1', 'v1')]
    >>> len(cache)
    5
    >>> 'x' in cache
    True
    >>> cache.replace('k4', 'v4_new')
    True
    >>> cache.replace('notexists', 'value')
    False
    >>> cache.items()
    [('k4', 'v4_new'), ('k3', 'v3'), ('x', '91'), ('k5', 'v5'), ('k1', 'v1')]
"""

from heapq import heappush, heappop, heapify
from threading import Lock
from time import time
from weakref import ref

class Node(object):
    __slots__ = 'prev', 'next', 'expire', 'key', 'value', '__weakref__'

def normalize(key, value="", expire=None):
    if isinstance(key, tuple): hash_value, key = key
    elif not isinstance(key, str): raise ValueError('Key must be tuple or string. Got: %s' % key.__class__.__name__)
    if not isinstance(value, str): raise ValueError('Value must be string. Got: %s' % value.__class__.__name__)
    if len(value) > 1024 * 1024: raise ValueError('Value size too big: %d' % len(value))
    if expire is not None: expire = time() + expire
    return key, value, expire

class Memcache(object):
    def __init__(self, max_data_size=64*1024*1024):
        self.dict = {}
        list = self.list = Node()
        list.prev = list.next = list.expire = list.key = list.value = None
        list.prev = list.next = list
        self.lock = Lock()
        self.heap = []
        self.data_size = 0
        self.max_data_size = max_data_size
    def __len__(self):
        return len(self.dict)
    def __contains__(self, key):
        return key in self.dict
    def __iter__(self):
        return iter(self.items())
    def items(self):
        now = time()
        result = []
        append = result.append
        list = self.list
        self.lock.acquire()
        try:
            node = list.next
            while node is not list:
                expire = node.expire
                if expire is not None and expire <= now: self._delete_node(node)
                else: append((node.key, node.value))
                node = node.next
        finally: self.lock.release()
        return result
    def _delete_node(self, node):
        prev, next = node.prev, node.next
        prev.next = next
        next.prev = prev
        del self.dict[node.key]
        self.data_size -= (len(node.key) + len(node.value))
    def _find_node(self, key):
        node = self.dict.get(key)
        if node is None: return None
        prev, next = node.prev, node.next
        prev.next = next
        next.prev = prev
        expire = node.expire
        if expire is None or expire > time(): return node
        del self.dict[node.key]
        self.data_size -= (len(node.key) + len(node.value))
        return None
    def _create_node(self, key):
        self.dict[key] = node = Node()
        node.key = key
        node.value = None
        return node
    def _place_on_top(self, node):
        list = self.list
        old_top = list.next
        node.prev, node.next = list, old_top
        list.next = old_top.prev = node
    def _set_node_value(self, node, value, expire):
        prev_value = node.value
        node.value, node.expire = value, expire
        if prev_value is None:
            self.data_size += len(node.key)
            self.data_size += len(value)
        else:
            self.data_size -= len(prev_value)
            self.data_size += len(value)
        if expire is not None: heappush(self.heap, (expire, ref(node)))
        self._delete_expired_nodes()
        self._conform_to_limits()
        if len(self.heap) > len(self.dict) * 2: self._pack_heap()
    def _delete_expired_nodes(self):
        now = time()
        heap = self.heap
        while heap:
            expire, node_ref = heap[0]
            if expire > now: break
            heappop(heap)
            node = node_ref()
            if node is not None and expire == node.expire: self._delete_node(node)
    def _conform_to_limits(self):
        list = self.list
        while self.data_size > self.max_data_size:
            bottom = list.prev
            self._delete_node(bottom)
    def _pack_heap(self):
        new_heap = []
        for item in self.heap:
            expire, node_ref = item
            node = node_ref()
            if node is not None and expire == node.expire: new_heap.append(item)
        heapify(new_heap)
        self.heap = new_heap
    def get(self, key):
        key, _, _ = normalize(key)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: return None
            self._place_on_top(node)
            return node.value
        finally: self.lock.release()
    def set(self, key, value, expire=None):
        key, value, expire = normalize(key, value, expire)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: node = self._create_node(key)
            self._place_on_top(node)
            self._set_node_value(node, value, expire)
        finally: self.lock.release()
        return True
    def add(self, key, value, expire=None):
        key, value, expire = normalize(key, value, expire)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node:
                self._place_on_top(node)
                return False
            node = self._create_node(key)
            self._place_on_top(node)
            self._set_node_value(node, value, expire)
        finally: self.lock.release()
        return True
    def replace(self, key, value, expire=None):
        key, value, expire = normalize(key, value, expire)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: return False
            self._place_on_top(node)
            self._set_node_value(node, value, expire)
        finally: self.lock.release()
        return True
    def delete(self, key):
        key, _, _ = normalize(key)
        node = self._find_node(key)
        if node is None: return 1
        self._delete_node(node)
        return 2
    def incr(self, key, delta=1):
        key, _, _ = normalize(key)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: return None
            self._place_on_top(node)
            try: value = int(node.value) + delta
            except ValueError: return None
            node.value = str(value)
        finally: self.lock.release()
        return value
    def decr(self, key, delta=1):
        key, _, _ = normalize(key)
        self.lock.acquire()
        try:
            node = self._find_node(key)
            if node is None: return None
            self._place_on_top(node)
            try: value = int(node.value) - delta
            except ValueError: return None
            node.value = str(value)
        finally: self.lock.release()
        return value
    def flush_all(self):
        self.lock.acquire()
        try:
            self.dict.clear()
            self.heap = []
            self.list.prev = self.list.next = self.list
            for node in self.dict.itervalues():
                node.prev = node.next = None
        finally: self.lock.release()
<|endoftext|>


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

import os
import subprocess
import textwrap

import click

DEFAULT_BASE_IMAGES = ['python:2.7']

@click.group(chain=True)
@click.version_option()
@click.option('-b', '--base-images',
              default=None,
              help='Base docker images (comma-separated list) - e.g.: '
                   '"python:2.7-onbuild,python:3.4-onbuild". Conflicts with '
                   '--python-versions.')
@click.option('-c', '--cmd',
              help='Command (CMD) to set in image. Conflicts with --procfile')
@click.option('-e', '--entrypoint',
              help='Entry point (ENTRYPOINT) to set in image')
@click.option('-p', '--python-versions',
              default=None,
              help='Python versions (comma-separated list) - e.g.: '
                   '"2.7,3.4". Conflicts with --base-images.')
@click.option('--procfile', type=click.File(),
              help='Procfile to get command from. Conflicts with --cmd.')
@click.option('-t', '--tag',
              help='Repository name (and optionally a tag) to be applied to '
                   'the resulting image in case of success')
@click.option('-r', '--requirement',
              'requirements_file', type=click.Path(exists=True),
              default='requirements.txt',
              help='pip requirements file with packages to install')
@click.pass_context
def pydockerize(ctx, requirements_file, tag, cmd, entrypoint, procfile,
                base_images=None, python_versions=None):
    """Create Docker images for Python apps"""

    if base_images is not None and python_versions is not None:
        raise Exception(
            'Cannot specify both --base-images and --python_versions')

    if base_images is not None:
        base_images = base_images.split(',')
    else:
        base_images = DEFAULT_BASE_IMAGES

    if python_versions is not None:
        python_versions = python_versions.split(',')
        base_images = get_base_images_from_python_versions(python_versions)

    if tag is None:
        tag = os.path.basename(os.getcwd()).lower()

    ctx.obj = {
        'base_images': base_images,
        'requirements_file': requirements_file,
        'cmd': cmd,
        'procfile': procfile,
        'entrypoint': entrypoint,
        'tag': tag,
    }

@pydockerize.command()
@click.pass_context
def generate(ctx):
    """Write Dockerfile(s)"""

    base_images = ctx.obj['base_images']
    requirements_file = ctx.obj['requirements_file']
    cmd = ctx.obj['cmd']
    procfile = ctx.obj['procfile']
    entrypoint = ctx.obj['entrypoint']

    if cmd is not None and procfile is not None:
        raise Exception('Cannot specify both --cmd and --procfile')

    if cmd is None and procfile is None and os.path.exists('Procfile'):
        procfile = open('Procfile')

    if procfile:
        cmd = get_cmd_from_procfile(procfile)
        click.echo('generate: Got cmd from %s => %r' % (procfile.name, cmd))
    else:
        click.echo('generate: cmd = %r' % cmd)

    click.echo('generate: entrypoint = %r' % entrypoint)
    click.echo('generate: base_images = %r' % base_images)
    click.echo('generate: requirements_file = %r' % requirements_file)

    base_images_and_filenames = []

    for base_image in base_images:
        filename = get_filename_from_base_image(base_image, base_images)
        generate_one(base_image, requirements_file, filename, cmd, entrypoint)
        base_images_and_filenames.append((base_image, filename))

    ctx.obj['base_images_and_filenames'] = base_images_and_filenames

def get_base_images_from_python_versions(python_versions):
    return ['python:%s-onbuild' % python_version
            for python_version in python_versions]

def get_cmd_from_procfile(procfile):
    lines = procfile.readlines()
    if len(lines) > 1:
        raise Exception(
            'Procfile with multiple lines not supported')
    return lines[0].split(':')[1].strip()

def generate_one(base_image, requirements_file, filename, cmd, entrypoint):
    click.echo('generate_one: base_image = %r' % base_image)
    click.echo('generate_one: Writing %s' % filename)

    with open(filename, 'w+') as f:
        f.write(textwrap.dedent("""\
            # This is a Dockerfile
            # Dockerfile reference: https://docs.docker.com/reference/builder/

            FROM {base_image}

            RUN mkdir -p /usr/src/app
            WORKDIR /usr/src/app

            # Install necessary Python packages from pip requirements file
            # requirements files: http://bit.ly/pip-requirements-files
            COPY {requirements_file} /usr/src/app/
            RUN pip install -r {requirements_file}

            # This is so one can mount a volume from the host to give the
            # container access to the host's current working directory. E.g.:
            #
            #   - `docker run -v $(pwd):/host` from command-line or ...
            #   - `volumes: [".:/host"]` in fig.yml
            WORKDIR /host
        """.format(base_image=base_image,
                   requirements_file=requirements_file)))
        if entrypoint:
            f.write("\nENTRYPOINT %s\n" % entrypoint)
        if cmd:
            f.write("\nCMD %s\n" % cmd)

    return filename

@pydockerize.command(
    short_help="Run `docker build` with Dockerfile(s) from `generate`")
@click.pass_context
def build(ctx):
    """Run `docker build` with Dockerfile(s) from `generate`"""

    tags_built = []
    tag = ctx.obj['tag']
    base_images = ctx.obj['base_images']

    click.echo("build: tag = '%s'" % tag)

    if no_dockerfiles_already_exist(base_images):
        ctx.invoke(generate)

    for base_image in base_images:
        filename = get_filename_from_base_image(base_image, base_images)
        tag_built = build_one(tag, base_image, base_images, filename)
        tags_built.append(tag_built or '<No tag>')

    click.secho('build: %d Docker build(s) succeeded: %s'
                % (len(base_images), ', '.join(tags_built)),
                fg='green')

    ctx.invoke(images)

def no_dockerfiles_already_exist(base_images):
    for base_image in base_images:
        filename = get_filename_from_base_image(base_image, base_images)
        if os.path.exists(filename):
            return False

    return True

@pydockerize.command()
@click.pass_context
def images(ctx):
    """Show images for repo from --tag"""

    tag = ctx.obj['tag']

    if tag:
        click.echo('\nShowing Docker images for %s:\n' % tag)
        show_docker_images(tag)

# Note that `ignore_unknown_options` requires Click >= 4.0
# and Click 4.0 is not released as of this writing
# See http://pocoo-click.readthedocs.org/en/latest/advanced/
# #forwarding-unknown-options
# (@msabramo 2015-02-13)
@pydockerize.command(context_settings=dict(
    ignore_unknown_options=True,
))
@click.pass_context
@click.argument('docker_run_args', nargs=-1, type=click.UNPROCESSED)
def run(ctx, docker_run_args):
    """Run a Docker container"""

    tag = ctx.obj['tag']
    mount_volume_from_host = True

    cmd = get_run_cmd(tag, mount_volume_from_host, docker_run_args)

    click.secho('Invoking: %s' % ' '.join(cmd), fg='yellow')
    status = subprocess.call(cmd)

def get_run_cmd(tag, mount_volume_from_host=True, docker_run_args=None):
    cmd = ['docker', 'run']
    cmd.append('-it')
    if mount_volume_from_host:
        cmd.append('-v')
        cmd.append('%s:/host' % os.getcwd())
    if docker_run_args:
        cmd.extend(docker_run_args)
    cmd.append(tag)
    return cmd

def build_one(tag, base_image, base_images, filename):
    cmd = ['docker', 'build']
    if tag:
        if ':' in tag:
            raise Exception("':' in tag not supported yet")
        tag = tag + ':' + get_tag_from_base_image(base_image, base_images)
        cmd.append('--tag')
        cmd.append(tag)
    if filename != 'Dockerfile':
        cmd.append('--file')
        cmd.append(filename)
    cmd.append('.')
    click.echo("build_one: Calling subprocess with cmd = '%s'\n"
               % ' '.join(cmd))
    status = subprocess.call(cmd)
    if status == 0:
        click.secho('build_one: Docker build for tag "%s" succeeded.' % tag,
                    fg='green')
        return tag
    else:
        click.secho('build_one: Docker build for tag "%s" failed with %d'
                    % (tag, status),
                    fg='red')
        raise click.Abort()

def show_docker_images(repo):
    cmd = ['docker', 'images', repo]
    return subprocess.call(cmd)

def get_filename_from_base_image(base_image, base_images):
    if len(base_images) == 1:
        return 'Dockerfile'
    else:
        return 'Dockerfile-' + base_image

def get_tag_from_base_image(base_image, base_images):
    if len(base_images) == 1:
        return 'latest'

    tag = base_image
    replacements = {'python:': 'py', '-onbuild': ''}

    for old, new in replacements.items():
        tag = tag.replace(old, new)

    return tag

if __name__ == '__main__':
    pydockerize()
<commit_msg>Split only on first colon when reading Procfile<commit_after>#!/usr/bin/env python

import os
import subprocess
import textwrap

import click

DEFAULT_BASE_IMAGES = ['python:2.7']

@click.group(chain=True)
@click.version_option()
@click.option('-b', '--base-images',
              default=None,
              help='Base docker images (comma-separated list) - e.g.: '
                   '"python:2.7-onbuild,python:3.4-onbuild". Conflicts with '
                   '--python-versions.')
@click.option('-c', '--cmd',
              help='Command (CMD) to set in image. Conflicts with --procfile')
@click.option('-e', '--entrypoint',
              help='Entry point (ENTRYPOINT) to set in image')
@click.option('-p', '--python-versions',
              default=None,
              help='Python versions (comma-separated list) - e.g.: '
                   '"2.7,3.4". Conflicts with --base-images.')
@click.option('--procfile', type=click.File(),
              help='Procfile to get command from. Conflicts with --cmd.')
@click.option('-t', '--tag',
              help='Repository name (and optionally a tag) to be applied to '
                   'the resulting image in case of success')
@click.option('-r', '--requirement',
              'requirements_file', type=click.Path(exists=True),
              default='requirements.txt',
              help='pip requirements file with packages to install')
@click.pass_context
def pydockerize(ctx, requirements_file, tag, cmd, entrypoint, procfile,
                base_images=None, python_versions=None):
    """Create Docker images for Python apps"""

    if base_images is not None and python_versions is not None:
        raise Exception(
            'Cannot specify both --base-images and --python_versions')

    if base_images is not None:
        base_images = base_images.split(',')
    else:
        base_images = DEFAULT_BASE_IMAGES

    if python_versions is not None:
        python_versions = python_versions.split(',')
        base_images = get_base_images_from_python_versions(python_versions)

    if tag is None:
        tag = os.path.basename(os.getcwd()).lower()

    ctx.obj = {
        'base_images': base_images,
        'requirements_file': requirements_file,
        'cmd': cmd,
        'procfile': procfile,
        'entrypoint': entrypoint,
        'tag': tag,
    }

@pydockerize.command()
@click.pass_context
def generate(ctx):
    """Write Dockerfile(s)"""

    base_images = ctx.obj['base_images']
    requirements_file = ctx.obj['requirements_file']
    cmd = ctx.obj['cmd']
    procfile = ctx.obj['procfile']
    entrypoint = ctx.obj['entrypoint']

    if cmd is not None and procfile is not None:
        raise Exception('Cannot specify both --cmd and --procfile')

    if cmd is None and procfile is None and os.path.exists('Procfile'):
        procfile = open('Procfile')

    if procfile:
        cmd = get_cmd_from_procfile(procfile)
        click.echo('generate: Got cmd from %s => %r' % (procfile.name, cmd))
    else:
        click.echo('generate: cmd = %r' % cmd)

    click.echo('generate: entrypoint = %r' % entrypoint)
    click.echo('generate: base_images = %r' % base_images)
    click.echo('generate: requirements_file = %r' % requirements_file)

    base_images_and_filenames = []

    for base_image in base_images:
        filename = get_filename_from_base_image(base_image, base_images)
        generate_one(base_image, requirements_file, filename, cmd, entrypoint)
        base_images_and_filenames.append((base_image, filename))

    ctx.obj['base_images_and_filenames'] = base_images_and_filenames

def get_base_images_from_python_versions(python_versions):
    return ['python:%s-onbuild' % python_version
            for python_version in python_versions]

def get_cmd_from_procfile(procfile):
    lines = procfile.readlines()
    if len(lines) > 1:
        raise Exception(
            'Procfile with multiple lines not supported')
    return lines[0].split(':', 1)[1].strip()

def generate_one(base_image, requirements_file, filename, cmd, entrypoint):
    click.echo('generate_one: base_image = %r' % base_image)
    click.echo('generate_one: Writing %s' % filename)

    with open(filename, 'w+') as f:
        f.write(textwrap.dedent("""\
            # This is a Dockerfile
            # Dockerfile reference: https://docs.docker.com/reference/builder/

            FROM {base_image}

            RUN mkdir -p /usr/src/app
            WORKDIR /usr/src/app

            # Install necessary Python packages from pip requirements file
            # requirements files: http://bit.ly/pip-requirements-files
            COPY {requirements_file} /usr/src/app/
            RUN pip install -r {requirements_file}

            # This is so one can mount a volume from the host to give the
            # container access to the host's current working directory. E.g.:
            #
            #   - `docker run -v $(pwd):/host` from command-line or ...
            #   - `volumes: [".:/host"]` in fig.yml
            WORKDIR /host
        """.format(base_image=base_image,
                   requirements_file=requirements_file)))
        if entrypoint:
            f.write("\nENTRYPOINT %s\n" % entrypoint)
        if cmd:
            f.write("\nCMD %s\n" % cmd)

    return filename

@pydockerize.command(
    short_help="Run `docker build` with Dockerfile(s) from `generate`")
@click.pass_context
def build(ctx):
    """Run `docker build` with Dockerfile(s) from `generate`"""

    tags_built = []
    tag = ctx.obj['tag']
    base_images = ctx.obj['base_images']

    click.echo("build: tag = '%s'" % tag)

    if no_dockerfiles_already_exist(base_images):
        ctx.invoke(generate)

    for base_image in base_images:
        filename = get_filename_from_base_image(base_image, base_images)
        tag_built = build_one(tag, base_image, base_images, filename)
        tags_built.append(tag_built or '<No tag>')

    click.secho('build: %d Docker build(s) succeeded: %s'
                % (len(base_images), ', '.join(tags_built)),
                fg='green')

    ctx.invoke(images)

def no_dockerfiles_already_exist(base_images):
    for base_image in base_images:
        filename = get_filename_from_base_image(base_image, base_images)
        if os.path.exists(filename):
            return False

    return True

@pydockerize.command()
@click.pass_context
def images(ctx):
    """Show images for repo from --tag"""

    tag = ctx.obj['tag']

    if tag:
        click.echo('\nShowing Docker images for %s:\n' % tag)
        show_docker_images(tag)

# Note that `ignore_unknown_options` requires Click >= 4.0
# and Click 4.0 is not released as of this writing
# See http://pocoo-click.readthedocs.org/en/latest/advanced/
# #forwarding-unknown-options
# (@msabramo 2015-02-13)
@pydockerize.command(context_settings=dict(
    ignore_unknown_options=True,
))
@click.pass_context
@click.argument('docker_run_args', nargs=-1, type=click.UNPROCESSED)
def run(ctx, docker_run_args):
    """Run a Docker container"""

    tag = ctx.obj['tag']
    mount_volume_from_host = True

    cmd = get_run_cmd(tag, mount_volume_from_host, docker_run_args)

    click.secho('Invoking: %s' % ' '.join(cmd), fg='yellow')
    status = subprocess.call(cmd)

def get_run_cmd(tag, mount_volume_from_host=True, docker_run_args=None):
    cmd = ['docker', 'run']
    cmd.append('-it')
    if mount_volume_from_host:
        cmd.append('-v')
        cmd.append('%s:/host' % os.getcwd())
    if docker_run_args:
        cmd.extend(docker_run_args)
    cmd.append(tag)
    return cmd

def build_one(tag, base_image, base_images, filename):
    cmd = ['docker', 'build']
    if tag:
        if ':' in tag:
            raise Exception("':' in tag not supported yet")
        tag = tag + ':' + get_tag_from_base_image(base_image, base_images)
        cmd.append('--tag')
        cmd.append(tag)
    if filename != 'Dockerfile':
        cmd.append('--file')
        cmd.append(filename)
    cmd.append('.')
    click.echo("build_one: Calling subprocess with cmd = '%s'\n"
               % ' '.join(cmd))
    status = subprocess.call(cmd)
    if status == 0:
        click.secho('build_one: Docker build for tag "%s" succeeded.' % tag,
                    fg='green')
        return tag
    else:
        click.secho('build_one: Docker build for tag "%s" failed with %d'
                    % (tag, status),
                    fg='red')
        raise click.Abort()

def show_docker_images(repo):
    cmd = ['docker', 'images', repo]
    return subprocess.call(cmd)

def get_filename_from_base_image(base_image, base_images):
    if len(base_images) == 1:
        return 'Dockerfile'
    else:
        return 'Dockerfile-' + base_image

def get_tag_from_base_image(base_image, base_images):
    if len(base_images) == 1:
        return 'latest'

    tag = base_image
    replacements = {'python:': 'py', '-onbuild': ''}

    for old, new in replacements.items():
        tag = tag.replace(old, new)

    return tag

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


<commit_before># pyfiap
# https://github.com/miettal/pyfiap
#
# Copyright 2016, Hiromasa Ihara
# Licensed under the MIT license.

# note:suds-jurko
from suds.client import Client
import uuid
from datetime import *

# for gzip transport
from suds.transport.http import HttpTransport
import gzip
from io import BytesIO

class GzipTransport(HttpTransport) :
  def send(self, request):
    request.headers['Accept-encoding'] = 'gzip'
    result = HttpTransport.send(self, request)
    if ('content-encoding' in result.headers and
        result.headers['content-encoding'] == 'gzip') :
      buf = BytesIO(result.message)
      f = gzip.GzipFile(fileobj=buf)
      result.message = f.read()
    return result

class APP() :
  def __init__(self, wsdl_url) :
    self.soap_client = Client(wsdl_url, transport=GzipTransport())

  def fetch_latest(self, point_ids) :
    if type(point_ids) != list :
      point_ids = [point_ids]

    keys = []
    for point_id in point_ids :
      keys.append({
        'point_id': point_id,
        'attrName': "time",
        'select': "maximum",
      })

    return self.fetch(keys)

  def fetch_by_time(self, point_ids, from_, to) :
    if type(point_ids) != list :
      point_ids = [point_ids]

    keys = []
    for point_id in point_ids :
      keys.append({
        'point_id': point_id,
        'attrName': "time",
        'gteq': from_,
        'lteq': to,
      })

    return self.fetch(keys)

  def fetch(self, keys) :
    if type(keys) != list :
      keys = [keys]

    data = []

    cursor = ""
    while True :
      # build request
      transport_rq = self.soap_client.factory.create('ns0:transport')

      transport_rq.header.query._id = str(uuid.uuid4())
      transport_rq.header.query._type = "storage"
      transport_rq.header.query._cursor = cursor
      transport_rq.header.query._acceptableSize = 1000

      for key_ in keys :
        key = self.soap_client.factory.create('ns0:key')
        key._id = key_["point_id"]
        key._attrName = key_["attrName"]
        if "eq" in key_ : key._eq = key_["eq"]
        if "neq" in key_ : key._neq = key_["neq"]
        if "lt" in key_ : key._lt = key_["lt"]
        if "gt" in key_ : key._gt =  key_["gt"]
        if "lteq" in key_ : key._lteq = key_["lteq"]
        if "gteq" in key_ : key._gteq = key_["gteq"]
        if "select" in key_ : key._select = key_["select"]
        transport_rq.header.query.key.append(key)

      # dummy
      transport_rq.header.error._type = "DUMMY"

      # fetch
      transport_rs = self.soap_client.service.query(transport_rq)
      if 'OK' in dir(transport_rs.header) :
        pass
      else :
        raise Exception(transport_rs.header.error.value)

      # add point_data
      if 'point_set' in dir(transport_rs.body) :
        for point_set_child in transport_rs.body.point_set:
          data += concat_point_set(point_set_child)

      for point in transport_rs.body.point:
        point_id = point._id
        if 'value' in dir(point) :
          for value in point.value :
            data.append({
              "point_id":point_id,
              "value":value.value,
              "time":value._time})

      # set next cursor
      if '_cursor' in dir(transport_rs.header.query) :
        cursor = transport_rs.header.query._cursor
      else :
        break

    return data

  def write(self, data) :
    if type(data) != list :
      data = [data]
    # build request
    transport_rq = self.soap_client.factory.create('ns0:transport')
    for (point_id, value, time) in data :
      point = self.soap_client.factory.create('ns0:point')
      value_ = self.soap_client.factory.create('ns0:value')
      value_.value = value
      value_._time = time
      point._id = point_id
      point.value = value_
      transport_rq.body.point.append(point)

    # fetch
    transport_rs = self.soap_client.service.data(transport_rq)

    if 'OK' in dir(transport_rs.header) :
      pass
    else :
      raise Exception(transport_rs.header.error.value)

def concat_point_set(point_set) :
  values = []
  if 'point_set' in point_set :
    for point_set_child in point_set.point_set:
      values += concat_point_set(point_set_child)

  for point_child in point_set.point :
    point_id = point_child._id
    for value in point.value :
      values.append({"point_id":point_id,
        "value":value.value,
        "time":value._time})

  return values

if __name__ == "__main__" :
  import pprint
  wsdl_url = "http://fiap-dev.gutp.ic.i.u-tokyo.ac.jp/axis2/services/FIAPStorage?wsdl"
  point_id = "http://..."
  app = APP(wsdl_url)

  from_ = (datetime.now()-timedelta(days=3))
  to = datetime.now()
  pprint.pprint(app.fetch_by_time(point_id, from_=from_, to=to))
<commit_msg>２重リスト判定に変更<commit_after># pyfiap
# https://github.com/miettal/pyfiap
#
# Copyright 2016, Hiromasa Ihara
# Licensed under the MIT license.

# note:suds-jurko
from suds.client import Client
import uuid
from datetime import *

# for gzip transport
from suds.transport.http import HttpTransport
import gzip
from io import BytesIO

class GzipTransport(HttpTransport) :
  def send(self, request):
    request.headers['Accept-encoding'] = 'gzip'
    result = HttpTransport.send(self, request)
    if ('content-encoding' in result.headers and
        result.headers['content-encoding'] == 'gzip') :
      buf = BytesIO(result.message)
      f = gzip.GzipFile(fileobj=buf)
      result.message = f.read()
    return result

class APP() :
  def __init__(self, wsdl_url) :
    self.soap_client = Client(wsdl_url, transport=GzipTransport())

  def fetch_latest(self, point_ids) :
    if type(point_ids) != list :
      point_ids = [point_ids]

    keys = []
    for point_id in point_ids :
      keys.append({
        'point_id': point_id,
        'attrName': "time",
        'select': "maximum",
      })

    return self.fetch(keys)

  def fetch_by_time(self, point_ids, from_, to) :
    if type(point_ids) != list :
      point_ids = [point_ids]

    keys = []
    for point_id in point_ids :
      keys.append({
        'point_id': point_id,
        'attrName': "time",
        'gteq': from_,
        'lteq': to,
      })

    return self.fetch(keys)

  def fetch(self, keys) :
    if type(keys) != list :
      keys = [keys]

    data = []

    cursor = ""
    while True :
      # build request
      transport_rq = self.soap_client.factory.create('ns0:transport')

      transport_rq.header.query._id = str(uuid.uuid4())
      transport_rq.header.query._type = "storage"
      transport_rq.header.query._cursor = cursor
      transport_rq.header.query._acceptableSize = 1000

      for key_ in keys :
        key = self.soap_client.factory.create('ns0:key')
        key._id = key_["point_id"]
        key._attrName = key_["attrName"]
        if "eq" in key_ : key._eq = key_["eq"]
        if "neq" in key_ : key._neq = key_["neq"]
        if "lt" in key_ : key._lt = key_["lt"]
        if "gt" in key_ : key._gt =  key_["gt"]
        if "lteq" in key_ : key._lteq = key_["lteq"]
        if "gteq" in key_ : key._gteq = key_["gteq"]
        if "select" in key_ : key._select = key_["select"]
        transport_rq.header.query.key.append(key)

      # dummy
      transport_rq.header.error._type = "DUMMY"

      # fetch
      transport_rs = self.soap_client.service.query(transport_rq)
      if 'OK' in dir(transport_rs.header) :
        pass
      else :
        raise Exception(transport_rs.header.error.value)

      # add point_data
      if 'point_set' in dir(transport_rs.body) :
        for point_set_child in transport_rs.body.point_set:
          data += concat_point_set(point_set_child)

      for point in transport_rs.body.point:
        point_id = point._id
        if 'value' in dir(point) :
          for value in point.value :
            data.append({
              "point_id":point_id,
              "value":value.value,
              "time":value._time})

      # set next cursor
      if '_cursor' in dir(transport_rs.header.query) :
        cursor = transport_rs.header.query._cursor
      else :
        break

    return data

  def write(self, data) :
    if type(data[0]) != list :
      data = [data]
    # build request
    transport_rq = self.soap_client.factory.create('ns0:transport')
    for (point_id, value, time) in data :
      point = self.soap_client.factory.create('ns0:point')
      value_ = self.soap_client.factory.create('ns0:value')
      value_.value = value
      value_._time = time
      point._id = point_id
      point.value = value_
      transport_rq.body.point.append(point)

    # fetch
    transport_rs = self.soap_client.service.data(transport_rq)

    if 'OK' in dir(transport_rs.header) :
      pass
    else :
      raise Exception(transport_rs.header.error.value)

def concat_point_set(point_set) :
  values = []
  if 'point_set' in point_set :
    for point_set_child in point_set.point_set:
      values += concat_point_set(point_set_child)

  for point_child in point_set.point :
    point_id = point_child._id
    for value in point.value :
      values.append({"point_id":point_id,
        "value":value.value,
        "time":value._time})

  return values

if __name__ == "__main__" :
  import pprint
  wsdl_url = "http://fiap-dev.gutp.ic.i.u-tokyo.ac.jp/axis2/services/FIAPStorage?wsdl"
  point_id = "http://..."
  app = APP(wsdl_url)

  from_ = (datetime.now()-timedelta(days=3))
  to = datetime.now()
  pprint.pprint(app.fetch_by_time(point_id, from_=from_, to=to))
<|endoftext|>