<commit_before>import copy
import json
import time

from lazy_object_proxy import Proxy
import six
from six import StringIO

import analyzere
from analyzere import utils
from analyzere.errors import MissingIdError
from analyzere.requestor import request, request_raw
from analyzere.utils import vectorize

# Helpers

class PaginatedCollection(list):
    def __init__(self, items, meta):
        super(PaginatedCollection, self).__init__(items)
        self.meta = meta

class Reference(Proxy):
    _id = None
    _href = None
    _resolved = False

    def __init__(self, href):
        collection_name, self._id = utils.parse_href(href)
        self._href = href

        def resolve():
            r = load_reference(collection_name, self._id)
            self._resolved = True
            return r

        super(Reference, self).__init__(resolve)

    def __copy__(self):
        if self._resolved:
            return copy.copy(self.__wrapped__)
        return Reference(self._href)

    def __deepcopy__(self, memo):
        if self._resolved:
            return copy.deepcopy(self.__wrapped__, memo)
        return Reference(self._href)

def load_reference(collection_name, id_):
    class_name = utils.to_camel_case(collection_name[:-1])
    try:
        cls = getattr(analyzere, class_name)
    except AttributeError:
        # For references to resources we don't know about, create a Resource
        # subclass with the correct collection name so retrieve() works.
        class UnknownResource(Resource):
            _collection_name = collection_name
        cls = UnknownResource
    return cls.retrieve(id_)

def convert_to_analyzere_object(value, cls=None):
    if isinstance(value, list):
        return [convert_to_analyzere_object(v, cls) for v in value]
    elif isinstance(value, dict):
        if 'href' in value:
            return Reference(value['href'])

        if 'items' in value and 'meta' in value:
            items = convert_to_analyzere_object(value['items'], cls)
            meta = convert_to_analyzere_object(value['meta'])
            return PaginatedCollection(items, meta)

        obj = cls() if (cls and 'id' in value) else EmbeddedResource()
        for k, v in six.iteritems(value):
            # Rename "_type" attribute to "type" so it's not considered private
            if k == '_type':
                k = 'type'
            setattr(obj, k, convert_to_analyzere_object(v))
        return obj
    else:
        return value

def to_dict(value):
    if isinstance(value, Reference):
        # Should appear first since other isinstance() checks may evaluate
        # the reference.
        return {'ref_id': value._id}
    elif isinstance(value, Resource):
        # Resources with IDs should be referenced, otherwise they're inlined
        if hasattr(value, 'id'):
            return {'ref_id': value.id}
        else:
            return value.to_dict()
    elif hasattr(value, 'to_dict'):
        return value.to_dict()
    elif isinstance(value, list):
        return [to_dict(i) for i in value]
    else:
        return value

# Base classes

class AnalyzeReObject(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __str__(self):
        return json.dumps(self.to_dict(), sort_keys=True, indent=2,
                          separators=(',', ': '), cls=utils.DateTimeEncoder)

    def __repr__(self):
        ident_parts = [type(self).__name__]

        id_ = getattr(self, 'id', None)
        if id_:
            ident_parts.append('id=%s' % id_)

        repr_str = '<%s at %s> JSON: %s' % (
            ' '.join(ident_parts), hex(id(self)), str(self))

        return repr_str

    def __eq__(self, other):
        return (isinstance(other, self.__class__)
                and self.__dict__ == other.__dict__)

    def __ne__(self, other):
        return not self.__eq__(other)

    def update(self, other):
        return self.__dict__.update(other.__dict__)

    def clear(self):
        return self.__dict__.clear()

    def to_dict(self):
        d = {}
        for k, v in six.iteritems(self.__dict__):
            if isinstance(k, str) and k.startswith('_'):
                continue
            elif k == 'type':
                k = '_type'
            d[k] = to_dict(v)
        return d

class Resource(AnalyzeReObject):
    """
    Base class for all API resources. Note that when a resource is saved,
    all values set on the object will be erased and replaced with the values
    returned from the server. This may result in attributes you have set that
    are ignored by the server being cleared.
    """
    _collection_name = None

    @classmethod
    def _get_collection_name(cls):
        if cls._collection_name:
            return cls._collection_name
        else:
            return utils.to_snake_case(cls.__name__) + 's'

    @classmethod
    def _get_path(cls, id_=None):
        path = cls._get_collection_name() + '/'
        return path + id_ if id_ else path

    @classmethod
    def retrieve(cls, id_):
        resp = request('get', cls._get_path(id_))
        return convert_to_analyzere_object(resp, cls)

    @classmethod
    def list(cls, **params):
        resp = request('get', cls._get_path(), params=params)
        return convert_to_analyzere_object(resp, cls)

    def save(self):
        id_ = getattr(self, 'id', None)
        method = 'put' if id_ else 'post'
        resp = request(method, self._get_path(id_), data=self.to_dict())
        self.clear()
        self.update(convert_to_analyzere_object(resp))
        return self

    def reload(self):
        id_ = getattr(self, 'id', None)
        if not id_:
            raise MissingIdError()
        self.clear()
        self.update(self.retrieve(id_))
        return self

class EmbeddedResource(AnalyzeReObject):
    pass

class DataResource(Resource):
    @property
    def _data_path(self):
        return self._get_path(self.id) + '/data'

    @property
    def _status_path(self):
        return self._data_path + '/status'

    @property
    def _commit_path(self):
        return self._data_path + '/commit'

    @property
    def status(self):
        resp = request('get', self._status_path)
        return convert_to_analyzere_object(resp)

    def upload_data(self, file_or_str):
        """
        Accepts a file-like object or string and uploads it. Files are
        automatically uploaded in 4mb chunks. Implements the tus protocol.
        """
        file_obj = StringIO(file_or_str) if isinstance(
            file_or_str, six.string_types) else file_or_str
        chunk_size = 4 * 1024 * 1024
        length = utils.file_length(file_obj)

        # Initiate upload session
        request_raw('post', self._data_path, headers={'Entity-Length': length})

        # Upload chunks
        for chunk, offset in utils.read_in_chunks(file_obj, chunk_size):
            headers = {'Offset': offset,
                       'Content-Type': 'application/offset+octet-stream'}
            request_raw('patch', self._data_path, headers=headers, body=chunk)

        # Commit the session
        request_raw('post', self._commit_path)

        # Block until data has finished processing
        while True:
            resp = self.status
            if (resp.status == 'Processing Successful' or
                    resp.status == 'Processing Failed'):
                return resp
            else:
                time.sleep(analyzere.upload_poll_interval)

    def download_data(self):
        return request_raw('get', self._data_path).content

    def delete_data(self):
        request_raw('delete', self._data_path)

class MetricsResource(Resource):
    """
    TODO: should the names of these functions be improved?
    """
    def _get_metrics(self, path, params=None):
        resp = request('get', path, params=params)
        return convert_to_analyzere_object(resp)

    def tail_metrics(self, probabilities, **params):
        probabilities = vectorize(probabilities)
        path = '%s/tail_metrics/%s' % (self._get_path(self.id), probabilities)
        return self._get_metrics(path, params)

    def co_metrics(self, probabilities, **params):
        probabilities = vectorize(probabilities)
        path = '%s/co_metrics/%s' % (self._get_path(self.id), probabilities)
        return self._get_metrics(path, params)

    def el(self, **params):
        path = '%s/el' % self._get_path(self.id)
        return float(request('get', path, params=params))

    def ep(self, thresholds, **params):
        thresholds = vectorize(thresholds)
        path = '%s/exceedance_probabilities/%s' % (self._get_path(self.id),
                                                   thresholds)
        return self._get_metrics(path, params)

    def tvar(self, probabilities, **params):
        probabilities = vectorize(probabilities)
        path = '%s/tvar/%s' % (self._get_path(self.id), probabilities)
        resp = self._get_metrics(path, params)
        return resp if isinstance(resp, list) else float(resp)

    def download_ylt(self, **params):
        path = '%s/ylt' % self._get_path(self.id)
        return request_raw('get', path, params=params).content

class OptimizationResource(Resource):
    def result(self):
        path = '%s/result' % self._get_path(self.id)
        resp = request('get', path)
        return convert_to_analyzere_object(resp)
<commit_msg>status -> upload_status<commit_after>import copy
import json
import time

from lazy_object_proxy import Proxy
import six
from six import StringIO

import analyzere
from analyzere import utils
from analyzere.errors import MissingIdError
from analyzere.requestor import request, request_raw
from analyzere.utils import vectorize

# Helpers

class PaginatedCollection(list):
    def __init__(self, items, meta):
        super(PaginatedCollection, self).__init__(items)
        self.meta = meta

class Reference(Proxy):
    _id = None
    _href = None
    _resolved = False

    def __init__(self, href):
        collection_name, self._id = utils.parse_href(href)
        self._href = href

        def resolve():
            r = load_reference(collection_name, self._id)
            self._resolved = True
            return r

        super(Reference, self).__init__(resolve)

    def __copy__(self):
        if self._resolved:
            return copy.copy(self.__wrapped__)
        return Reference(self._href)

    def __deepcopy__(self, memo):
        if self._resolved:
            return copy.deepcopy(self.__wrapped__, memo)
        return Reference(self._href)

def load_reference(collection_name, id_):
    class_name = utils.to_camel_case(collection_name[:-1])
    try:
        cls = getattr(analyzere, class_name)
    except AttributeError:
        # For references to resources we don't know about, create a Resource
        # subclass with the correct collection name so retrieve() works.
        class UnknownResource(Resource):
            _collection_name = collection_name
        cls = UnknownResource
    return cls.retrieve(id_)

def convert_to_analyzere_object(value, cls=None):
    if isinstance(value, list):
        return [convert_to_analyzere_object(v, cls) for v in value]
    elif isinstance(value, dict):
        if 'href' in value:
            return Reference(value['href'])

        if 'items' in value and 'meta' in value:
            items = convert_to_analyzere_object(value['items'], cls)
            meta = convert_to_analyzere_object(value['meta'])
            return PaginatedCollection(items, meta)

        obj = cls() if (cls and 'id' in value) else EmbeddedResource()
        for k, v in six.iteritems(value):
            # Rename "_type" attribute to "type" so it's not considered private
            if k == '_type':
                k = 'type'
            setattr(obj, k, convert_to_analyzere_object(v))
        return obj
    else:
        return value

def to_dict(value):
    if isinstance(value, Reference):
        # Should appear first since other isinstance() checks may evaluate
        # the reference.
        return {'ref_id': value._id}
    elif isinstance(value, Resource):
        # Resources with IDs should be referenced, otherwise they're inlined
        if hasattr(value, 'id'):
            return {'ref_id': value.id}
        else:
            return value.to_dict()
    elif hasattr(value, 'to_dict'):
        return value.to_dict()
    elif isinstance(value, list):
        return [to_dict(i) for i in value]
    else:
        return value

# Base classes

class AnalyzeReObject(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __str__(self):
        return json.dumps(self.to_dict(), sort_keys=True, indent=2,
                          separators=(',', ': '), cls=utils.DateTimeEncoder)

    def __repr__(self):
        ident_parts = [type(self).__name__]

        id_ = getattr(self, 'id', None)
        if id_:
            ident_parts.append('id=%s' % id_)

        repr_str = '<%s at %s> JSON: %s' % (
            ' '.join(ident_parts), hex(id(self)), str(self))

        return repr_str

    def __eq__(self, other):
        return (isinstance(other, self.__class__)
                and self.__dict__ == other.__dict__)

    def __ne__(self, other):
        return not self.__eq__(other)

    def update(self, other):
        return self.__dict__.update(other.__dict__)

    def clear(self):
        return self.__dict__.clear()

    def to_dict(self):
        d = {}
        for k, v in six.iteritems(self.__dict__):
            if isinstance(k, str) and k.startswith('_'):
                continue
            elif k == 'type':
                k = '_type'
            d[k] = to_dict(v)
        return d

class Resource(AnalyzeReObject):
    """
    Base class for all API resources. Note that when a resource is saved,
    all values set on the object will be erased and replaced with the values
    returned from the server. This may result in attributes you have set that
    are ignored by the server being cleared.
    """
    _collection_name = None

    @classmethod
    def _get_collection_name(cls):
        if cls._collection_name:
            return cls._collection_name
        else:
            return utils.to_snake_case(cls.__name__) + 's'

    @classmethod
    def _get_path(cls, id_=None):
        path = cls._get_collection_name() + '/'
        return path + id_ if id_ else path

    @classmethod
    def retrieve(cls, id_):
        resp = request('get', cls._get_path(id_))
        return convert_to_analyzere_object(resp, cls)

    @classmethod
    def list(cls, **params):
        resp = request('get', cls._get_path(), params=params)
        return convert_to_analyzere_object(resp, cls)

    def save(self):
        id_ = getattr(self, 'id', None)
        method = 'put' if id_ else 'post'
        resp = request(method, self._get_path(id_), data=self.to_dict())
        self.clear()
        self.update(convert_to_analyzere_object(resp))
        return self

    def reload(self):
        id_ = getattr(self, 'id', None)
        if not id_:
            raise MissingIdError()
        self.clear()
        self.update(self.retrieve(id_))
        return self

class EmbeddedResource(AnalyzeReObject):
    pass

class DataResource(Resource):
    @property
    def _data_path(self):
        return self._get_path(self.id) + '/data'

    @property
    def _status_path(self):
        return self._data_path + '/status'

    @property
    def _commit_path(self):
        return self._data_path + '/commit'

    @property
    def upload_status(self):
        resp = request('get', self._status_path)
        return convert_to_analyzere_object(resp)

    def upload_data(self, file_or_str):
        """
        Accepts a file-like object or string and uploads it. Files are
        automatically uploaded in 4mb chunks. Implements the tus protocol.
        """
        file_obj = StringIO(file_or_str) if isinstance(
            file_or_str, six.string_types) else file_or_str
        chunk_size = 4 * 1024 * 1024
        length = utils.file_length(file_obj)

        # Initiate upload session
        request_raw('post', self._data_path, headers={'Entity-Length': length})

        # Upload chunks
        for chunk, offset in utils.read_in_chunks(file_obj, chunk_size):
            headers = {'Offset': offset,
                       'Content-Type': 'application/offset+octet-stream'}
            request_raw('patch', self._data_path, headers=headers, body=chunk)

        # Commit the session
        request_raw('post', self._commit_path)

        # Block until data has finished processing
        while True:
            resp = self.upload_status
            if (resp.status == 'Processing Successful' or
                    resp.status == 'Processing Failed'):
                return resp
            else:
                time.sleep(analyzere.upload_poll_interval)

    def download_data(self):
        return request_raw('get', self._data_path).content

    def delete_data(self):
        request_raw('delete', self._data_path)

class MetricsResource(Resource):
    """
    TODO: should the names of these functions be improved?
    """
    def _get_metrics(self, path, params=None):
        resp = request('get', path, params=params)
        return convert_to_analyzere_object(resp)

    def tail_metrics(self, probabilities, **params):
        probabilities = vectorize(probabilities)
        path = '%s/tail_metrics/%s' % (self._get_path(self.id), probabilities)
        return self._get_metrics(path, params)

    def co_metrics(self, probabilities, **params):
        probabilities = vectorize(probabilities)
        path = '%s/co_metrics/%s' % (self._get_path(self.id), probabilities)
        return self._get_metrics(path, params)

    def el(self, **params):
        path = '%s/el' % self._get_path(self.id)
        return float(request('get', path, params=params))

    def ep(self, thresholds, **params):
        thresholds = vectorize(thresholds)
        path = '%s/exceedance_probabilities/%s' % (self._get_path(self.id),
                                                   thresholds)
        return self._get_metrics(path, params)

    def tvar(self, probabilities, **params):
        probabilities = vectorize(probabilities)
        path = '%s/tvar/%s' % (self._get_path(self.id), probabilities)
        resp = self._get_metrics(path, params)
        return resp if isinstance(resp, list) else float(resp)

    def download_ylt(self, **params):
        path = '%s/ylt' % self._get_path(self.id)
        return request_raw('get', path, params=params).content

class OptimizationResource(Resource):
    def result(self):
        path = '%s/result' % self._get_path(self.id)
        resp = request('get', path)
        return convert_to_analyzere_object(resp)
<|endoftext|>


<commit_before>#Section 1: Terminology
# 1) What is a recursive function?
#A recursive function is a defined function that calls itself and only is completed when the requirements of the base case is met. Usually a parameter is used
#
#
# 2) What happens if there is no base case defined in a recursive function?
#The recursive function will run for an infinite amount of times and will not be stopped.
#
#
# 3) What is the first thing to consider when designing a recursive function?
#Make sure to have a base case for the recursive function.
#
#
# 4) How do we put data into a function call?
#We can use parameters in your function call.
#
# 5) How do we get data out of a function call?
#The return function allows the program to spit out data back out by using return or parameters.
#
#

#Section 2: Reading
# Read the following function definitions and function calls.
# Then determine the values of the variables q1-q20.

#a1 = 8
#a2 = 8
#a3 = -1

#b1 = 2
#b2 = 2
#b3 = 4

#c1 = -2
#c2 = 4
#c3 = 5

#d1 = 6
#d2 = 8
#d3 = 4

#Section 3: Programming
#Write a script that asks the user to enter a series of numbers.
#When the user types in nothing, it should return the average of all the odd numbers
#that were typed in.
#In your code for the script, add a comment labeling the base case  on the line BEFORE the base case.
#Also add a comment label BEFORE the recursive case.
#It is NOT NECESSARY to print out a running total with each user input.

def adder(runningtotal=0, n=0):
    num = raw_input("Insert a number: ")

    if num == "":
        average = runningtotal / n
        print "The total average of the odd numbers is {}.".format(average)

    if float(num) % 2==0:
        adder()

    else:
        num = float(num)
        n = n+1
        runningtotal += num
        adder(runningtotal, n)

adder()
<commit_msg>Marked our computer quizes<commit_after>#Section 1: Terminology
# 1) What is a recursive function?
#A recursive function is a defined function that calls itself and only is completed when the requirements of the base case is met. Usually a parameter is used
#
#
# 2) What happens if there is no base case defined in a recursive function?
#The recursive function will run for an infinite amount of times and will not be stopped.
#
#
# 3) What is the first thing to consider when designing a recursive function?
#Make sure to have a base case for the recursive function.
#
#
# 4) How do we put data into a function call?
#We can use parameters in your function call.
#
# 5) How do we get data out of a function call?
#The return function allows the program to spit out data back out by using return or parameters.
#
#
#
#

#Section 2: Reading
# Read the following function definitions and function calls.
# Then determine the values of the variables q1-q20.

#a1 = 8
#a2 = 8
#a3 = -1

#b1 = 2
#b2 = 4         [x]
#b3 = 4

#c1 = -2
#c2 = 4
#c3 = 45

#d1 = 8         [x]
#d2 = 6         [x]
#d3 = 2         [x]

#Section 3: Programming
#Write a script that asks the user to enter a series of numbers.
#When the user types in nothing, it should return the average of all the odd numbers
#that were typed in.
#In your code for the script, add a comment labeling the base case  on the line BEFORE the base case.
#Also add a comment label BEFORE the recursive case.
#It is NOT NECESSARY to print out a running total with each user input.

def adder(runningtotal=0, n=0):
    num = raw_input("Insert a number: ")

    if num == "":
        average = runningtotal / n
        print "The total average of the odd numbers is {}.".format(average)

    if float(num) % 2==0:
        adder()

    else:
        num = float(num)
        n = n+1
        runningtotal += num
        adder(runningtotal, n)

adder()
<|endoftext|>


<commit_before># From http://code.activestate.com/recipes/498245/
import collections
import functools
from itertools import ifilterfalse
from heapq import nsmallest
from operator import itemgetter

class Counter(dict):
    'Mapping where default values are zero'

    def __missing__(self, key):
        return 0

def lru_cache(maxsize=100):
    def decorator(user_function):
        return lru_cache_object(user_function, maxsize)
    return decorator

class lru_cache_object(object):
    """
    Least-recently-used cache.

    Arguments to the cached function must be hashable.
    Cache performance statistics stored in f.hits and f.misses.
    Clear the cache with f.clear().
    http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

    Amended to accept two callbacks: should_decache and will_decache.

    should_decache is called with the result that is about to decache and should return True or False.
    will_decache is called with the result that is about to decache.

    Also provides an explicit decache function. Call it to decache a result with the given key.
    Calling decache will not call either callback.

    Also acts as an iterator over cached results, and can be checked if a key is in the cache with `key in cache`

    """

    def __init__(self, user_function, maxsize=100):

        self.maxqueue = maxsize * 10
        self.maxsize = maxsize
        self.cache = {}                   # mapping of args to results
        self.queue = collections.deque()  # order that keys have been used
        self.refcount = Counter()         # times each key is in the queue
        self.sentinel = object()          # marker for looping around the queue
        self.kwd_mark = object()          # separate positional and keyword args
        self.user_function = user_function

        self.hits = self.misses = 0

    def setCacheLimit(self, size):
        self.maxsize = size
        self.maxqueue = size * 10

    def __call__(self, *args, **kwds):
        # cache key records both positional and keyword args
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))

        # record recent use of this key
        self.queue.append(key)
        self.refcount[key] += 1

        # get cache entry or compute if not found
        try:
            result = self.cache[key]
            self.hits += 1
        except KeyError:
            result = self.user_function(*args, **kwds)
            self.cache[key] = result
            self.misses += 1

            # purge least recently used cache entry
            if len(self.cache) > self.maxsize:
                stale_key = self.queue.popleft()
                self.refcount[stale_key] -= 1
                cannot_decache = []
                while len(self.queue):
                    while self.refcount[stale_key]:
                        stale_key = self.queue.popleft()
                        self.refcount[stale_key] -= 1

                    if self.should_decache is None or self.should_decache(self.cache[stale_key]) is True:
                        if self.will_decache is not None:
                            self.will_decache(self.cache[stale_key])

                        del self.cache[stale_key], self.refcount[stale_key]
                        break
                    else:
                        self.refcount[stale_key] += 1
                        cannot_decache.append(stale_key)

                self.queue.extendleft(cannot_decache)

        # periodically compact the queue by eliminating duplicate keys
        # while preserving order of most recent access
        if len(self.queue) > self.maxqueue:
            self.refcount.clear()
            self.queue.appendleft(self.sentinel)
            for key in ifilterfalse(self.refcount.__contains__,
                                    iter(self.queue.pop, self.sentinel)):
                self.queue.appendleft(key)
                self.refcount[key] = 1

        return result

    def clear(self):
        self.cache.clear()
        self.queue.clear()
        self.refcount.clear()
        self.hits = self.misses = 0

    def decache(self, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        del self.cache[key], self.refcount[key]

    def store(self, result, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        self.cache[key] = result
        self.refcount[key] += 1
        self.queue.append(key)

    def __contains__(self, key, **kwds):
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        return key in self.cache

    def __iter__(self):
        return self.cache.itervalues()

    should_decache = None
    will_decache = None

def lfu_cache(maxsize=100):
    def decorator(user_function):
        return lfu_cache_object(user_function, maxsize)
    return decorator

class lfu_cache_object(object):
    """Least-frequenty-used cache.

    Arguments to the cached function must be hashable.
    Cache performance statistics stored in f.hits and f.misses.
    Clear the cache with f.clear().
    http://en.wikipedia.org/wiki/Least_Frequently_Used

    Accepts two callbacks: should_decache and will_decache

    should_decache is called with the result that is about to decache and should return True or False
    will_decache is called with the result that is about to decache.

    Also provides an explicit decache function. Call it to decache a result with the given key.
    Calling decache will not call either callback.

    Also acts as an iterator over cached results, and can be checked if a key is in the cache with `key in cache`

    """

    def __init__(self, user_function, maxsize):
        self.cache = {}                      # mapping of args to results
        self.use_count = Counter()           # times each key has been accessed
        self.kwd_mark = object()             # separate positional and keyword args
        self.hits = self.misses = 0
        self.user_function = user_function
        self.maxsize = maxsize

    def __call__(self, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        self.use_count[key] += 1

        # get cache entry or compute if not found
        try:
            result = self.cache[key]
            self.hits += 1
        except KeyError:
            result = self.user_function(*args, **kwds)
            self.cache[key] = result
            self.misses += 1

            # purge bottom 10% of least frequently used cache entries
            if len(self.cache) > self.maxsize:
                for key, _ in nsmallest(self.maxsize // 10,
                                        self.use_count.iteritems(),
                                        key=itemgetter(1)):
                    if self.should_decache is None or self.should_decache(self.cache[key]) is True:
                        if self.will_decache is not None:
                            self.will_decache(self.cache[key])
                        del self.cache[key], self.use_count[key]

        return result

    def clear(self):
        self.cache.clear()
        self.use_count.clear()
        self.hits = self.misses = 0

    def decache(self, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        del self.cache[key], self.use_count[key]

    def __contains__(self, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        return key in self.cache

    def __iter__(self):
        return self.cache.itervalues()

    should_decache = None
    will_decache = None

if __name__ == '__main__':

    @lru_cache(maxsize=20)
    def f_lru(x, y):
        return 3 * x + y

    domain = range(5)
    from random import choice
    for i in range(1000):
        r = f_lru(choice(domain), choice(domain))

    print(f_lru.hits, f_lru.misses)

    @lfu_cache(maxsize=20)
    def f_lfu(x, y):
        return 3 * x + y

    domain = range(5)
    from random import choice
    for i in range(1000):
        r = f_lfu(choice(domain), choice(domain))

    print(f_lfu.hits, f_lfu.misses)
<commit_msg>Add default maxsize for lfu_cache_object<commit_after># From http://code.activestate.com/recipes/498245/
import collections
import functools
from itertools import ifilterfalse
from heapq import nsmallest
from operator import itemgetter

class Counter(dict):
    'Mapping where default values are zero'

    def __missing__(self, key):
        return 0

def lru_cache(maxsize=100):
    def decorator(user_function):
        return lru_cache_object(user_function, maxsize)
    return decorator

class lru_cache_object(object):
    """
    Least-recently-used cache.

    Arguments to the cached function must be hashable.
    Cache performance statistics stored in f.hits and f.misses.
    Clear the cache with f.clear().
    http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

    Amended to accept two callbacks: should_decache and will_decache.

    should_decache is called with the result that is about to decache and should return True or False.
    will_decache is called with the result that is about to decache.

    Also provides an explicit decache function. Call it to decache a result with the given key.
    Calling decache will not call either callback.

    Also acts as an iterator over cached results, and can be checked if a key is in the cache with `key in cache`

    """

    def __init__(self, user_function, maxsize=100):

        self.maxqueue = maxsize * 10
        self.maxsize = maxsize
        self.cache = {}                   # mapping of args to results
        self.queue = collections.deque()  # order that keys have been used
        self.refcount = Counter()         # times each key is in the queue
        self.sentinel = object()          # marker for looping around the queue
        self.kwd_mark = object()          # separate positional and keyword args
        self.user_function = user_function

        self.hits = self.misses = 0

    def setCacheLimit(self, size):
        self.maxsize = size
        self.maxqueue = size * 10

    def __call__(self, *args, **kwds):
        # cache key records both positional and keyword args
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))

        # record recent use of this key
        self.queue.append(key)
        self.refcount[key] += 1

        # get cache entry or compute if not found
        try:
            result = self.cache[key]
            self.hits += 1
        except KeyError:
            result = self.user_function(*args, **kwds)
            self.cache[key] = result
            self.misses += 1

            # purge least recently used cache entry
            if len(self.cache) > self.maxsize:
                stale_key = self.queue.popleft()
                self.refcount[stale_key] -= 1
                cannot_decache = []
                while len(self.queue):
                    while self.refcount[stale_key]:
                        stale_key = self.queue.popleft()
                        self.refcount[stale_key] -= 1

                    if self.should_decache is None or self.should_decache(self.cache[stale_key]) is True:
                        if self.will_decache is not None:
                            self.will_decache(self.cache[stale_key])

                        del self.cache[stale_key], self.refcount[stale_key]
                        break
                    else:
                        self.refcount[stale_key] += 1
                        cannot_decache.append(stale_key)

                self.queue.extendleft(cannot_decache)

        # periodically compact the queue by eliminating duplicate keys
        # while preserving order of most recent access
        if len(self.queue) > self.maxqueue:
            self.refcount.clear()
            self.queue.appendleft(self.sentinel)
            for key in ifilterfalse(self.refcount.__contains__,
                                    iter(self.queue.pop, self.sentinel)):
                self.queue.appendleft(key)
                self.refcount[key] = 1

        return result

    def clear(self):
        self.cache.clear()
        self.queue.clear()
        self.refcount.clear()
        self.hits = self.misses = 0

    def decache(self, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        del self.cache[key], self.refcount[key]

    def store(self, result, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        self.cache[key] = result
        self.refcount[key] += 1
        self.queue.append(key)

    def __contains__(self, key, **kwds):
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        return key in self.cache

    def __iter__(self):
        return self.cache.itervalues()

    should_decache = None
    will_decache = None

def lfu_cache(maxsize=100):
    def decorator(user_function):
        return lfu_cache_object(user_function, maxsize)
    return decorator

class lfu_cache_object(object):
    """Least-frequenty-used cache.

    Arguments to the cached function must be hashable.
    Cache performance statistics stored in f.hits and f.misses.
    Clear the cache with f.clear().
    http://en.wikipedia.org/wiki/Least_Frequently_Used

    Accepts two callbacks: should_decache and will_decache

    should_decache is called with the result that is about to decache and should return True or False
    will_decache is called with the result that is about to decache.

    Also provides an explicit decache function. Call it to decache a result with the given key.
    Calling decache will not call either callback.

    Also acts as an iterator over cached results, and can be checked if a key is in the cache with `key in cache`

    """

    def __init__(self, user_function, maxsize=100):
        self.cache = {}                      # mapping of args to results
        self.use_count = Counter()           # times each key has been accessed
        self.kwd_mark = object()             # separate positional and keyword args
        self.hits = self.misses = 0
        self.user_function = user_function
        self.maxsize = maxsize

    def __call__(self, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        self.use_count[key] += 1

        # get cache entry or compute if not found
        try:
            result = self.cache[key]
            self.hits += 1
        except KeyError:
            result = self.user_function(*args, **kwds)
            self.cache[key] = result
            self.misses += 1

            # purge bottom 10% of least frequently used cache entries
            if len(self.cache) > self.maxsize:
                for key, _ in nsmallest(self.maxsize // 10,
                                        self.use_count.iteritems(),
                                        key=itemgetter(1)):
                    if self.should_decache is None or self.should_decache(self.cache[key]) is True:
                        if self.will_decache is not None:
                            self.will_decache(self.cache[key])
                        del self.cache[key], self.use_count[key]

        return result

    def clear(self):
        self.cache.clear()
        self.use_count.clear()
        self.hits = self.misses = 0

    def decache(self, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        del self.cache[key], self.use_count[key]

    def __contains__(self, *args, **kwds):
        key = args
        if kwds:
            key += (self.kwd_mark,) + tuple(sorted(kwds.items()))
        return key in self.cache

    def __iter__(self):
        return self.cache.itervalues()

    should_decache = None
    will_decache = None

if __name__ == '__main__':

    @lru_cache(maxsize=20)
    def f_lru(x, y):
        return 3 * x + y

    domain = range(5)
    from random import choice
    for i in range(1000):
        r = f_lru(choice(domain), choice(domain))

    print(f_lru.hits, f_lru.misses)

    @lfu_cache(maxsize=20)
    def f_lfu(x, y):
        return 3 * x + y

    domain = range(5)
    from random import choice
    for i in range(1000):
        r = f_lfu(choice(domain), choice(domain))

    print(f_lfu.hits, f_lfu.misses)
<|endoftext|>


<commit_before>from github import Github
import requests
import subprocess
import os
import zipfile
from log import logname
import time
import shutil
logger = logname("Updater")

class Updater():
    def __init__(self):
        self.latest_version = (0, 0, 0)
        self.installed_version = (0, 0, 0)

        self.organization_name = "TurtleRover"
        self.repository_name = "Turtle-Rover-Mission-Control"

        self.root_directory = "/home/pi/" + self.repository_name
        self.updates_directory = "/home/pi/updates/"
        self.backups_directory = "/home/pi/backups/"

        self.github = Github()

    def check(self):
        logger.info('Checking for a new release...')
        latest_release = self.get_latest_release(self.repository_name)
        installed_version = self.get_installed_version()
        # is_new_release = self.compare(latest_release.tag_name, installed_version)
        is_new_release = True
        if is_new_release == True:
            logger.info('Found a new release: %s', latest_release.tag_name)
            self.update(latest_release)
        elif is_new_release == False:
            logger.info('Installed version is up-to-date: %s',
                        installed_version[:-1])
        else:
            logger.error("Can't get latest release")

    def get_installed_version(self):
        return subprocess.run(['turtle', '-v'], stdout=subprocess.PIPE).stdout.decode('utf-8')

    def get_latest_release(self, repository_name):
        try:
            organization = self.github.get_organization(self.organization_name)
            repository = organization.get_repo(self.repository_name)
            latest_release = repository.get_latest_release()
            return latest_release
        except BaseException as e:
            logger.error(e)
            return None

    def convert_to_tuple(self, version):
        return (int(version[0]), int(version[2]), int(version[4]))

    def compare(self, latest_version, installed_version):
        latest_version_tuple = self.convert_to_tuple(latest_version)
        installed_version_tuple = self.convert_to_tuple(installed_version)
        if latest_version_tuple > installed_version_tuple:
            return True
        elif latest_version_tuple == installed_version_tuple:
            return False
        else:
            logger.warn("Installed version is ahead of released one.")
            return False

    def download(self, latest_release, path):
        self.create_updates_directory()
        logger.info('Starting download...')
        r = requests.get(latest_release.zipball_url, stream=True, allow_redirects=True)
        with open(path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)
        filesize = os.path.getsize(path)
        logger.info('Downloaded: %s', self.get_human_readable_size(filesize))
        return True

    def create_updates_directory(self):
        try:
            logger.debug('Creating "updates" directory')
            os.makedirs(self.updates_directory)
        except OSError as e:
            logger.debug(e)

    # https://stackoverflow.com/a/32009595/1589989
    def get_human_readable_size(self, size, precision=2):
        suffixes = ['B', 'KB', 'MB', 'GB', 'TB']
        suffixIndex = 0
        while size > 1024 and suffixIndex < 4:
            suffixIndex += 1  # increment the index of the suffix
            size = size / 1024.0  # apply the division
        return "%.*f%s" % (precision, size, suffixes[suffixIndex])

    def build_filename(self, tag_name):
        return self.repository_name +'-' + tag_name + ".zip"

    def unpack(self, path):
        extracted = None
        logger.info("Unzipping & installing update...")
        with zipfile.ZipFile(path,"r") as zip_ref:
            zip_ref.extractall("/tmp/")
            extracted = zip_ref.namelist()[0]

        subprocess.run(['cp', '-R', '/tmp/'+extracted + '.', '/home/pi/test/'])

    def pack(self):
        logger.info("Starting backup...")
        os.chdir(os.path.dirname(self.root_directory))
        self.clean_directory(self.backups_directory)
        backup_path = self.backups_directory + str(int(time.time())) + '.zip'
        # Check if it is ok to use here try/catch
        try:
            with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED, allowZip64=True) as zf:
                for root, _, filenames in os.walk(os.path.basename(self.root_directory)):
                    for name in filenames:
                        name = os.path.join(root, name)
                        name = os.path.normpath(name)
                        zf.write(name, name)
            logger.info("Backup saved: %s", backup_path)
            return True
        except BaseException as e:
            logger.error(e)
            return False

    def update(self, latest_release):
        path = self.updates_directory + self.build_filename(latest_release.tag_name)
        if not os.path.isdir(self.root_directory+ '/.git'):
            if not os.path.isfile(path):
                self.download(latest_release, path)
            else:
                logger.info('Already downloaded to: ' + path)

            self.unpack(path)
            self.run_postinstall()

        else:
            logger.warn('This is a development environment. Please use git instead')

    # https://stackoverflow.com/a/185941/1589989
    def clean_directory(self, directory):
        for file in os.listdir(directory):
            path = os.path.join(directory, file)
            try:
                if os.path.isfile(path):
                    os.unlink(path)
                elif os.path.isdir(path): shutil.rmtree(path)
                return True
            except Exception as e:
                logger.error(e)
                return False

    def run_postinstall(self):
        logger.info("Spawning pip3...")
        pip = subprocess.run(['sudo', 'pip3', 'install', '-r', '/home/pi/Turtle-Rover-Mission-Control/requirements.txt'])

    def yolo(self):
        print(os.path.isdir(self.root_directory+ '/.git'))

updater = Updater()
updater.check()
<commit_msg>cleaning<commit_after>from github import Github
import requests
import subprocess
import os
import zipfile
from log import logname
import time
import shutil
logger = logname("Updater")

class Updater():
    def __init__(self):
        self.latest_version = (0, 0, 0)
        self.installed_version = (0, 0, 0)

        self.organization_name = "TurtleRover"
        self.repository_name = "Turtle-Rover-Mission-Control"

        self.root_directory = "/home/pi/" + self.repository_name
        self.updates_directory = "/home/pi/updates/"
        self.backups_directory = "/home/pi/backups/"

        self.github = Github()

    def check(self):
        logger.info('Checking for new releases...')
        latest_release = self.get_latest_release(self.repository_name)
        installed_version = self.get_installed_version()
        # is_new_release = self.compare(latest_release.tag_name, installed_version)
        is_new_release = True
        if is_new_release == True:
            logger.info('Found a new release: %s', latest_release.tag_name)
            self.update(latest_release)
        elif is_new_release == False:
            logger.info('Installed version is up-to-date: %s',
                        installed_version[:-1])
        else:
            logger.error("Can't get latest release")

    def get_installed_version(self):
        return subprocess.run(['turtle', '-v'], stdout=subprocess.PIPE).stdout.decode('utf-8')

    def get_latest_release(self, repository_name):
        try:
            organization = self.github.get_organization(self.organization_name)
            repository = organization.get_repo(self.repository_name)
            latest_release = repository.get_latest_release()
            return latest_release
        except BaseException as e:
            logger.error(e)
            return None

    def convert_to_tuple(self, version):
        return (int(version[0]), int(version[2]), int(version[4]))

    def compare(self, latest_version, installed_version):
        latest_version_tuple = self.convert_to_tuple(latest_version)
        installed_version_tuple = self.convert_to_tuple(installed_version)
        if latest_version_tuple > installed_version_tuple:
            return True
        elif latest_version_tuple == installed_version_tuple:
            return False
        else:
            logger.warn("Installed version is ahead of released one.")
            return False

    def download(self, latest_release, path):
        self.create_updates_directory()
        logger.info('Starting download...')
        r = requests.get(latest_release.zipball_url, stream=True, allow_redirects=True)
        with open(path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)
        filesize = os.path.getsize(path)
        logger.info('Downloaded: %s', self.get_human_readable_size(filesize))
        return True

    def create_updates_directory(self):
        try:
            logger.debug('Creating "updates" directory')
            os.makedirs(self.updates_directory)
        except OSError as e:
            logger.debug(e)

    # https://stackoverflow.com/a/32009595/1589989
    def get_human_readable_size(self, size, precision=2):
        suffixes = ['B', 'KB', 'MB', 'GB', 'TB']
        suffixIndex = 0
        while size > 1024 and suffixIndex < 4:
            suffixIndex += 1  # increment the index of the suffix
            size = size / 1024.0  # apply the division
        return "%.*f%s" % (precision, size, suffixes[suffixIndex])

    def build_filename(self, tag_name):
        return self.repository_name +'-' + tag_name + ".zip"

    def unpack(self, path):
        extracted = None
        logger.info("Unzipping & installing update...")
        with zipfile.ZipFile(path,"r") as zip_ref:
            zip_ref.extractall("/tmp/")
            extracted = zip_ref.namelist()[0]

        subprocess.run(['cp', '-R', '/tmp/'+extracted + '.', '/home/pi/test/'])

    def pack(self):
        logger.info("Starting backup...")
        os.chdir(os.path.dirname(self.root_directory))
        self.clean_directory(self.backups_directory)
        backup_path = self.backups_directory + str(int(time.time())) + '.zip'
        # Check if it is ok to use here try/catch
        try:
            with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED, allowZip64=True) as zf:
                for root, _, filenames in os.walk(os.path.basename(self.root_directory)):
                    for name in filenames:
                        name = os.path.join(root, name)
                        name = os.path.normpath(name)
                        zf.write(name, name)
            logger.info("Backup saved: %s", backup_path)
            return True
        except BaseException as e:
            logger.error(e)
            return False

    def update(self, latest_release):
        path = self.updates_directory + self.build_filename(latest_release.tag_name)
        if not os.path.isdir(self.root_directory+ '/.git'):
            if not os.path.isfile(path):
                self.download(latest_release, path)
            else:
                logger.info('Already downloaded to: ' + path)

            self.unpack(path)
            self.run_postinstall()

        else:
            logger.warn('This is a development environment. Please use git instead')

    # https://stackoverflow.com/a/185941/1589989
    def clean_directory(self, directory):
        for file in os.listdir(directory):
            path = os.path.join(directory, file)
            try:
                if os.path.isfile(path):
                    os.unlink(path)
                elif os.path.isdir(path): shutil.rmtree(path)
                return True
            except Exception as e:
                logger.error(e)
                return False

    def run_postinstall(self):
        logger.info("Spawning pip3...")
        pip = subprocess.run(['sudo', 'pip3', 'install', '-r', '/home/pi/Turtle-Rover-Mission-Control/requirements.txt'])

updater = Updater()
updater.check()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Sphinx extension for parsing XML files generated by Doxygen.
#
# Written by Arto Bendiken <http://ar.to/>.
#
# This is free and unencumbered software released into the public domain.

#from lxml import etree as ElementTree
from xml.etree import cElementTree as ElementTree

__index__ = None

def index():
  global __index__
  if __index__ is None:
    __index__ = DoxygenIndex()
  return __index__

class DoxygenIndex(object):
  def __init__(self):
    tree = ElementTree.parse('../doxygen/xml/index.xml')
    self.symbols = {}
    for compound in tree.findall('compound'):
      compound_refid = compound.get('refid')
      for member in compound.findall('member'):
        member_refid = member.get('refid')
        member_name = member.find('name').text
        self.symbols[member_name] = (compound_refid, member_refid)
    # TODO: handle duplicate names?

  def lookup(self, symbol):
    compound_refid, member_refid = self.symbols[symbol]
    tree = ElementTree.parse('../doxygen/xml/%s.xml' % (compound_refid))
    for memberdef in tree.findall(".//sectiondef/memberdef[@id='%s']" % (member_refid)):
      return DoxygenMemberDef(memberdef)
    return None

class DoxygenMemberDef(object):
  BASIC_TAGS = ['definition', 'argsstring', 'name']
  OTHER_TAGS = ['type', 'briefdescription']
  PARAM_TAGS = ['type', 'declname']

  def __init__(self, tree):
    ElementTree.dump(tree)

    # Extract the attributes:
    for (attr_name, attr_value) in tree.items():
      self.__dict__[attr_name] = attr_value

    # Parse the basic tags:
    for node_name in self.BASIC_TAGS:
      self.__dict__[node_name] = tree.findtext(node_name)

    # Parse the parameter list:
    self.params = []
    if self.argsstring != '(void)':
      for param_node in tree.findall('param'):
        param = {}
        for node_name in self.PARAM_TAGS:
          param[node_name] = param_node.findtext(node_name)
        if 'type' in param:
          self.params.append(param)

    # Parse description tags:
    for node_name in self.OTHER_TAGS:
      self.__dict__[node_name] = self.extract_text(tree.find(node_name))

    # Parse the <location> tag:
    # TODO

  def extract_text(self, root):
    buffer = []
    for text in self.collect_text(root):
      buffer.append(text)
    return ''.join(buffer).strip()

  def collect_text(self, root):
    if root is not None:
      if root.text is not None:
        yield root.text
      for node in root:
        for text in self.collect_text(node):
          yield text
      if root.tail is not None:
        yield root.tail

  def __str__(self):
    return "<class %s %r>" % (type(self).__name__, self.__dict__)

def setup(app):
  app.require_sphinx('1.0')

if __name__ == '__main__':
  #node = DoxygenIndex().lookup('cpr_feature_exists')
  node = DoxygenIndex().lookup('cpr_vector_alloc')
  print node
<commit_msg>Refactored the Doxygen XML parsing for better code quality.<commit_after># -*- coding: utf-8 -*-
#
# Sphinx extension for parsing XML files generated by Doxygen.
#
# Written by Arto Bendiken <http://ar.to/>.
#
# This is free and unencumbered software released into the public domain.

#from lxml import etree as ElementTree
from xml.etree import cElementTree as ElementTree

ATTR_TYPES  = {'const': bool, 'explicit': bool, 'id': str, 'inline': bool,
               'kind': str, 'mutable': bool, 'prot': str, 'static': bool,
               'virt': str, 'volatile': bool}
MEMBER_TAGS = ['type', 'definition', 'argsstring', 'name', 'initializer', 'briefdescription'] # TODO
PARAM_TAGS  = ['type', 'declname', 'defname', 'array', 'defval', 'briefdescription']

__index__ = None

def index():
  global __index__
  if __index__ is None:
    __index__ = DoxygenIndex('../doxygen/xml/')
  return __index__

class DoxygenIndex(object):
  def __init__(self, path):
    self.path = path
    self.symbols = parse_index(ElementTree.parse(path + '/index.xml'))

  def get(self, symbol, default=None):
    if symbol in self.symbols:
      compound_refid, member_refid = self.symbols[symbol]
      tree = ElementTree.parse((self.path + '/%s.xml') % (compound_refid))
      for memberdef in tree.findall(".//sectiondef/memberdef[@id='%s']" % (member_refid)):
        return DoxygenMemberDef(memberdef)
    return default

  def __getitem__(self, symbol):
    result = self.get(symbol)
    if result is None:
      raise KeyError, symbol
    return result

  def __str__(self):
    return "<class %s %r>" % (type(self).__name__, self.__dict__)

class DoxygenMemberDef(object):
  def __init__(self, node):
    ElementTree.dump(node)

    # Extract the <memberdef> attributes:
    self.__dict__ = extract_attrs(node)

    # Parse the basic tags:
    for node_name in MEMBER_TAGS:
      self.__dict__[node_name] = extract_text(node.find(node_name))

    # Parse the <param> tags:
    if self.argsstring == '(void)':
      self.params = [] # special case for C support
    else:
      self.params = parse_param_tags(node.findall('param'))

    # Parse the <detaileddescription> tag:
    parse_detaileddescription_tag(node.find('detaileddescription'))

    # Parse the <inbodydescription> tag:
    parse_inbodydescription_tag(node.find('inbodydescription'))

    # Parse the <location> tag:
    location = parse_location_tag(node.find('location'))

  def __str__(self):
    return "<class %s %r>" % (type(self).__name__, self.__dict__)

def parse_index(tree):
  result = {}
  for compound in tree.findall('compound'):
    compound_refid = compound.get('refid')
    for member in compound.findall('member'):
      member_refid = member.get('refid')
      member_name = member.findtext('name')
      result[member_name] = (compound_refid, member_refid)
  # TODO: handle duplicate names?
  return result

def extract_attrs(node):
  result = {}
  for (attr_name, attr_value) in node.items():
    attr_type = ATTR_TYPES.get(attr_name)
    if attr_type == bool:
      attr_value = (attr_value == 'yes')
    result[attr_name] = attr_value
  return result

def parse_param_tags(nodes):
  result = []
  for node in nodes:
    param = {}
    for node_name in PARAM_TAGS:
      tag = node.find(node_name)
      if tag is not None:
        param[node_name] = extract_text(tag)
    result.append(param)
  return result

def parse_detaileddescription_tag(node):
  pass # TODO

def parse_inbodydescription_tag(node):
  pass # TODO

def parse_location_tag(node):
  if node is not None:
    return (node.get('file'), node.get('line'))
  return None

def extract_text(node):
  buffer = []
  for text in collect_text(node):
    buffer.append(text)
  return ''.join(buffer).strip()

def collect_text(node):
  if node is not None:
    if node.text is not None:
      yield node.text
    for subnode in node:
      for text in collect_text(subnode):
        yield text
    if node.tail is not None:
      yield node.tail

def setup(app):
  # Sphinx integration:
  if type(app).__name__ == 'sphinx.application.Sphinx':
    app.require_sphinx('1.0')

if __name__ == '__main__':
  #print index()
  #node = index().get('cpr_feature_exists')
  node = index().get('cpr_vector_alloc')
  #node = index().get('CPR_VERSION_STRING')
  #node = index().get('cpr_version_string')
  #node = index().get('cpr_vector_t')
  print node
<|endoftext|>


<commit_before>"""
Statistical tools for time series analysis
"""

import numpy as np
from scipy import stats
import scikits.statsmodels as sm
from scikits.statsmodels.sandbox.tsa.varma_tools import lagmat
#from scikits.statsmodels.sandbox.rls import RLS

def dfuller(X, nlags=1, noconstant=False, trend=False):
    """
    Augmented Dickey-Fuller test for a time series X.

    Parameters
    ----------
    X - array-like
        The time series that might contain a unit root.
    nlags - int
        nlags should be >= 0.
        TODO: Allow string 'AIC' and 'BIC'
    noconstant - bool
        Include a constant or not.  Default is false, so that a constant
        term is estimated.
    trend : bool
        Include a linear time trend or not.  Default is false for no
        time trend.

    Notes
    -----
    The ADF test statistic is not returned.  Refer to a ADF table for
    inference.  Is nlags = 0, dfuller returns the classic Dickey-Fuller
    statistic.

    """
    if nlags < 0:
        raise ValueError, "nlags should be >= 0"
    X = np.asarray(X)
    nobs = float(len(X))
    xdiff = np.diff(X)
    xlag1 = X[:-1]
    xdiffp = lagmat(xdiff,nlags,trim='both')[:,1:]
    t = np.arange(1,nobs-nlags)
    RHS = np.column_stack((xlag1[nlags:],xdiffp))
    if trend:
        t = np.arange(1,nobs-nlags)
        RHS = np.column_stack((t,RHS))
    if not noconstant:
        RHS = sm.add_constant(RHS,prepend=True)
    results = sm.OLS(xdiff[nlags:],RHS).fit()
    return results.t()[-(nlags+1)]

def acorr(X,nlags=40, level=95):
    """
    Autocorrelation function

    Parameters
    ----------
    X : array-like
        The time series
    nlags : int
        The number of lags to estimate the autocorrelation function for.
        Default is 40.
    level : int
        The level of the confidence intervals.  Defaults is 95 % confidence
        intervals.

    Returns
    -------
    acf : array
        The values of the autocorrelation function
    conf_int(acf) : array
        The confidence interval of acf at level
    """
    X = np.asarray(X).squeeze()
    nobs = float(len(X))
    if nlags > nobs:
        raise ValueError, "X does not have %s observations" % nlags
    Xbar = np.mean(X)
    acf = np.zeros(nlags)
    acov0 = np.var(X)
    for i in range(1,nlags+1):
        acf[i-1] = np.dot(X[i:] - Xbar,X[:-i] - Xbar)
    acf = 1/nobs*acf/acov0
    varacf = np.ones(nlags)/nobs
    varacf[1:] *= 1 + 2*np.cumsum(acf[1:]**2)
    confint = np.array(zip(acf - stats.norm.ppf(1-(100 - level)/\
            200.)*np.sqrt(varacf), acf+stats.norm.ppf(1-(100-level)/200.)\
            *np.sqrt(varacf)))
    return acf,confint

def pacorr(X,nlags=40, method="ols"):
    """
    Partial autocorrelation function
    """
    X = np.asarray(X).squeeze()
    nobs = float(len(X))
    if nlags > nobs:
        raise ValueError, "X does not have %s observations" % nlags
    pacf = np.zeros(nlags)
    for i in range(1,nlags+1):
        pacf[i-1] = sm.OLS(X[i:],sm.add_constant(lagmat(X, i,
            trim="both")[:,1:], prepend=True)).fit().params[-1]
    return pacf

if __name__=="__main__":
    data = sm.datasets.macrodata.Load().data
    adf = dfuller(data['realgdp'],4)
    acf,ci = acorr(data['realgdp'])
    pacf = pacorr(data['realgdp'])

<commit_msg>Added squeeze in PACF.  Started to work on a periodogram function.<commit_after>"""
Statistical tools for time series analysis
"""

import numpy as np
from scipy import stats, signal
import scikits.statsmodels as sm
from scikits.statsmodels.sandbox.tsa.varma_tools import lagmat
#from scikits.statsmodels.sandbox.rls import RLS

def dfuller(X, nlags=1, noconstant=False, trend=False):
    """
    Augmented Dickey-Fuller test for a time series X.

    Parameters
    ----------
    X - array-like
        The time series that might contain a unit root.
    nlags - int
        nlags should be >= 0.
        TODO: Allow string 'AIC' and 'BIC'
    noconstant - bool
        Include a constant or not.  Default is false, so that a constant
        term is estimated.
    trend : bool
        Include a linear time trend or not.  Default is false for no
        time trend.

    Notes
    -----
    The ADF test statistic is not returned.  Refer to a ADF table for
    inference.  Is nlags = 0, dfuller returns the classic Dickey-Fuller
    statistic.

    """
    if nlags < 0:
        raise ValueError, "nlags should be >= 0"
    X = np.asarray(X).squeeze()
    nobs = float(len(X))
    xdiff = np.diff(X)
    xlag1 = X[:-1]
    xdiffp = lagmat(xdiff,nlags,trim='both')[:,1:]
    t = np.arange(1,nobs-nlags)
    RHS = np.column_stack((xlag1[nlags:],xdiffp))
    if trend:
        t = np.arange(1,nobs-nlags)
        RHS = np.column_stack((t,RHS))
    if not noconstant:
        RHS = sm.add_constant(RHS,prepend=True)
    results = sm.OLS(xdiff[nlags:],RHS).fit()
    return results.t()[-(nlags+1)]

def acorr(X,nlags=40, level=95):
    """
    Autocorrelation function

    Parameters
    ----------
    X : array-like
        The time series
    nlags : int
        The number of lags to estimate the autocorrelation function for.
        Default is 40.
    level : int
        The level of the confidence intervals.  Defaults is 95 % confidence
        intervals.

    Returns
    -------
    acf : array
        The values of the autocorrelation function
    conf_int(acf) : array
        The confidence interval of acf at level
    """
    X = np.asarray(X).squeeze()
    nobs = float(len(X))
    if nlags > nobs:
        raise ValueError, "X does not have %s observations" % nlags
    Xbar = np.mean(X)
    acf = np.zeros(nlags)
    acov0 = np.var(X)
    for i in range(1,nlags+1):
        acf[i-1] = np.dot(X[i:] - Xbar,X[:-i] - Xbar)
    acf = 1/nobs*acf/acov0
    varacf = np.ones(nlags)/nobs
    varacf[1:] *= 1 + 2*np.cumsum(acf[1:]**2)
    confint = np.array(zip(acf - stats.norm.ppf(1-(100 - level)/\
            200.)*np.sqrt(varacf), acf+stats.norm.ppf(1-(100-level)/200.)\
            *np.sqrt(varacf)))
    return acf,confint

def pacorr(X,nlags=40, method="ols"):
    """
    Partial autocorrelation function
    """
    X = np.asarray(X).squeeze()
    nobs = float(len(X))
    if nlags > nobs:
        raise ValueError, "X does not have %s observations" % nlags
    pacf = np.zeros(nlags)
    for i in range(1,nlags+1):
        pacf[i-1] = sm.OLS(X[i:],sm.add_constant(lagmat(X, i,
            trim="both")[:,1:], prepend=True)).fit().params[-1]
    return pacf

def pergram(X, kernel='bartlett', log=True):
    """
    Returns the (log) periodogram for the natural frequency of X

    Parameters
    ----------
    X
    M : int
        Should this be hardcoded?
    kernel : str, optional
    Notes
    -----
    The autocovariances are normalized by len(X).
    The frequencies are calculated as
    If len(X) is odd M = (len(X) - 1)/2 else M = len(X)/2. Either way
        freq[i] = 2*[i+1]/T and len(freq) == M

    Reference
    ----------
    Based on Lutkepohl; Hamilton.

    Notes
    -----
    Doesn't look right yet.
    """
    X = np.asarray(X).squeeze()
    nobs = len(X)
    M = np.floor(nobs/2.)
    acov = np.zeros(M+1)
    acov[0] = np.var(X)
    Xbar = X.mean()
    for i in range(1,int(M+1)):
        acov[i] = np.dot(X[i:] - Xbar,X[:-i] - Xbar)
    acov /= nobs
    #    #TODO: make a list to check window
#    ell = np.r_[1,np.arange(1,M+1)*np.pi/nobs]
    if kernel == "bartlett":
        w = 1 - np.arange(M+1)/M

#    weights = exec('signal.'+window+'(M='str(M)')')
    j = np.arange(1,M+1)
    ell = np.linspace(0,np.pi,M)
    pergr = np.zeros_like(ell)
    for i,L in enumerate(ell):
        pergr[i] = 1/(2*np.pi)*acov[0] + 2 * np.sum(w[1:]*acov[1:]*np.cos(L*j))
    return pergr

if __name__=="__main__":
    data = sm.datasets.macrodata.Load().data
    adf = dfuller(data['realgdp'],4)
    acf,ci = acorr(data['realgdp'])
    pacf = pacorr(data['realgdp'])

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import json
from flask import Blueprint, Response, jsonify, request, current_app

from . import db, __version__
from .models import Customer, Account, Transaction
from .api_utils import *

mod = Blueprint('api', __name__)

@mod.route('/')
def root():
    result = db.session.query(Customer).first()
    current_app.logger.debug(result.name)
    response = {__name__: "v{}".format(__version__)}
    return jsonify(response)

@mod.route('/accounts',
           methods=['POST', 'GET'],
           defaults={'account_number': None})
@mod.route('/accounts/<account_number>', methods=['GET'])
def accounts(account_number):
    """Routes requests to `create_account()` or `show_balance()`"""
    if request.method == 'POST':
        return create_account(get_data(request))
    elif request.method == 'GET' and account_number:
        return show_balance(account_number)
    else:
        return error("Error processing {} request for account number {}".format(request.method, account_number))

def create_account(data):
    """Creates a new account for a customer identified by customer_id"""
    mandatory_params = ['customer_id', 'initial_deposit']
    result = check_required_params(mandatory_params, data)
    if result:
        return result

    customer = Customer.query.filter_by(id=data['customer_id']).first()
    if not customer:
        return error("No customer with id {} found".format(data['customer_id']), 404)

    new_account = Account(customer.id)
    db.session.add(new_account)
    db.session.commit()

    ## NOTE: It seems odd to have the new_account.id as both the originator and the beneficiary here
    new_transaction = Transaction(new_account.id, new_account.id, 'Initial Deposit', data['initial_deposit'])
    db.session.add(new_transaction)
    db.session.commit()

    return jsonify({"account_number": new_account.number}), 201

def show_balance(account_number):
    return jsonify({"balance": "Balance for {}".format(account_number)})

@mod.route('/transactions', methods=['POST'])
def create_transaction():
    data = get_data(request)
    mandatory_params = ['amount', 'reference', 'originator', 'beneficiary']
    result = check_required_params(mandatory_params, data)
    if result:
        return result

    (originator, error) = get_account('originator', data)
    if error:
        return error

    (beneficiary, error) = get_account('beneficiary', data)
    if error:
        return error

    new_transaction = Transaction(originator.id, beneficiary.id, data['reference'], data['amount'])
    db.session.add(new_transaction)
    db.session.commit()

    return jsonify({"transaction": new_transaction.id}), 201

@mod.route('/accounts/<account_number>/transactions', methods=['GET'])
def account_transactions(account_number):
    account = Account.query.filter_by(number=account_number).first()
    if not account:
        return error("No account with number {} found".format(account_number), 404)
    transactions = Transaction.query.filter_by(originator=account.id).all()
    return jsonify({"account_number": account.number, "transactions": transactions})
<commit_msg>Order list of transactions by date<commit_after># -*- coding: utf-8 -*-
import json
from flask import Blueprint, Response, jsonify, request, current_app

from . import db, __version__
from .models import Customer, Account, Transaction
from .api_utils import *

mod = Blueprint('api', __name__)

@mod.route('/')
def root():
    result = db.session.query(Customer).first()
    current_app.logger.debug(result.name)
    response = {__name__: "v{}".format(__version__)}
    return jsonify(response)

@mod.route('/accounts',
           methods=['POST', 'GET'],
           defaults={'account_number': None})
@mod.route('/accounts/<account_number>', methods=['GET'])
def accounts(account_number):
    """Routes requests to `create_account()` or `show_balance()`"""
    if request.method == 'POST':
        return create_account(get_data(request))
    elif request.method == 'GET' and account_number:
        return show_balance(account_number)
    else:
        return error("Error processing {} request for account number {}".format(request.method, account_number))

def create_account(data):
    """Creates a new account for a customer identified by customer_id"""
    mandatory_params = ['customer_id', 'initial_deposit']
    result = check_required_params(mandatory_params, data)
    if result:
        return result

    customer = Customer.query.filter_by(id=data['customer_id']).first()
    if not customer:
        return error("No customer with id {} found".format(data['customer_id']), 404)

    new_account = Account(customer.id)
    db.session.add(new_account)
    db.session.commit()

    ## NOTE: It seems odd to have the new_account.id as both the originator and the beneficiary here
    new_transaction = Transaction(new_account.id, new_account.id, 'Initial Deposit', data['initial_deposit'])
    db.session.add(new_transaction)
    db.session.commit()

    return jsonify({"account_number": new_account.number}), 201

def show_balance(account_number):
    return jsonify({"balance": "Balance for {}".format(account_number)})

@mod.route('/transactions', methods=['POST'])
def create_transaction():
    data = get_data(request)
    mandatory_params = ['amount', 'reference', 'originator', 'beneficiary']
    result = check_required_params(mandatory_params, data)
    if result:
        return result

    (originator, error) = get_account('originator', data)
    if error:
        return error

    (beneficiary, error) = get_account('beneficiary', data)
    if error:
        return error

    new_transaction = Transaction(originator.id, beneficiary.id, data['reference'], data['amount'])
    db.session.add(new_transaction)
    db.session.commit()

    return jsonify({"transaction": new_transaction.id}), 201

@mod.route('/accounts/<account_number>/transactions', methods=['GET'])
def account_transactions(account_number):
    account = Account.query.filter_by(number=account_number).first()
    if not account:
        return error("No account with number {} found".format(account_number), 404)
    transactions = Transaction.query.filter_by(originator=account.id).order_by('datetime').all()
    return jsonify({"account_number": account.number, "transactions": transactions})
<|endoftext|>


<commit_before>"""
Wrap the internal caffe C++ module (_caffe.so) with a clean, Pythonic
interface.
"""

from collections import OrderedDict
import numpy as np

from ._caffe import Net, SGDSolver

# we directly update methods from Net here (rather than using composition or
# inheritance) so that nets created by caffe (e.g., by SGDSolver) will
# automatically have the improved interface

# Input preprocessing
Net.mean = {}   # image mean (ndarray, input dimensional or broadcastable)
Net.input_scale = {}  # for a model that expects data = input * input_scale
Net.channel_swap = {}  # for RGB -> BGR and the like

@property
def _Net_blobs(self):
  """
  An OrderedDict (bottom to top, i.e., input to output) of network
  blobs indexed by name
  """
  return OrderedDict([(bl.name, bl) for bl in self._blobs])

Net.blobs = _Net_blobs

@property
def _Net_params(self):
  """
  An OrderedDict (bottom to top, i.e., input to output) of network
  parameters indexed by name; each is a list of multiple blobs (e.g.,
  weights and biases)
  """
  return OrderedDict([(lr.name, lr.blobs) for lr in self.layers
                                          if len(lr.blobs) > 0])

Net.params = _Net_params

def _Net_set_mean(self, input_, mean_f, mode='image'):
  """
  Set the mean to subtract for data centering.

  Take
    input_: which input to assign this mean.
    mean_f: path to mean .npy
    mode: image = use the whole-image mean (and check dimensions)
          channel = channel constant (i.e. mean pixel instead of mean image)
  """
  if input_ not in self.inputs:
    raise Exception('Input not in {}'.format(self.inputs))
  mean = np.load(mean_f)
  if mode == 'image':
    if mean.shape != self.input.data.shape[1:]:
      raise Exception('The mean shape does not match the input shape.')
    self.mean[input_] = mean
  elif mode == 'channel':
    self.mean[input_] = mean.mean(1).mean(1)
  else:
    raise Exception('Mode not in {}'.format(['image', 'channel']))

Net.set_mean = _Net_set_mean

def _Net_set_input_scale(self, input_, scale):
  """
  Set the input feature scaling factor s.t. input blob = input * scale.

  Take
    input_: which input to assign this scale factor
    scale: scale coefficient
  """
  if input_ not in self.inputs:
    raise Exception('Input not in {}'.format(self.inputs))
  self.input_scale[input_] = scale

Net.set_input_scale = _Net_set_input_scale

def _Net_set_channel_swap(self, input_, order):
  """
  Set the input channel order for e.g. RGB to BGR conversion
  as needed for the reference ImageNet model.

  Take
    input_: which input to assign this channel order
    order: the order to take the channels. (2,1,0) maps RGB to BGR for example.
  """
  if input_ not in self.inputs:
    raise Exception('Input not in {}'.format(self.inputs))
  self.channel_swap[input_] = order

Net.set_channel_swap = _Net_set_channel_swap

def _Net_format_image(self, input_, image):
  """
  Format image for input to Caffe:
  - convert to single
  - scale feature
  - reorder channels (for instance color to BGR)
  - subtract mean
  - reshape to 1 x K x H x W

  Take
    image: (H x W x K) ndarray

  Give
    image: (K x H x W) ndarray
  """
  caf_image = image.astype(np.float32)
  input_scale = self.input_scale.get(input_)
  channel_order = self.channel_swap.get(input_)
  mean = self.mean.get(input_)
  if input_scale:
    caf_image *= input_scale
  if channel_order:
    caf_image = caf_image[:, :, channel_order]
  if mean:
    caf_image -= mean
  caf_image = caf_image.transpose((2, 0, 1))
  caf_image = caf_image[np.newaxis, :, :, :]
  return caf_image

Net.format_image = _Net_format_image

def _Net_decaffeinate_image(self, input_, image):
  """
  Invert Caffe formatting; see _Net_format_image().
  """
  decaf_image = image.squeeze()
  decaf_image = decaf_image.transpose((1,2,0))
  input_scale = self.input_scale.get(input_)
  channel_order = self.channel_swap.get(input_)
  mean = self.mean.get(input_)
  if mean:
    decaf_image += mean
  if channel_order:
    decaf_image = decaf_image[:, :, channel_order[::-1]]
  if input_scale:
    decaf_image /= input_scale
  return decaf_image

Net.decaffeinate_image = _Net_decaffeinate_image

def _Net_set_input_arrays(self, data, labels):
  """
  Set input arrays of the in-memory MemoryDataLayer.
  (Note: this is only for networks declared with the memory data layer.)
  """
  if labels.ndim == 1:
    labels = np.ascontiguousarray(labels[:, np.newaxis, np.newaxis,
                                         np.newaxis])
  return self._set_input_arrays(data, labels)

Net.set_input_arrays = _Net_set_input_arrays
<commit_msg>pycaffe Net.forward() helper<commit_after>"""
Wrap the internal caffe C++ module (_caffe.so) with a clean, Pythonic
interface.
"""

from collections import OrderedDict
import numpy as np

from ._caffe import Net, SGDSolver

# we directly update methods from Net here (rather than using composition or
# inheritance) so that nets created by caffe (e.g., by SGDSolver) will
# automatically have the improved interface

# Input preprocessing
Net.mean = {}   # image mean (ndarray, input dimensional or broadcastable)
Net.input_scale = {}  # for a model that expects data = input * input_scale
Net.channel_swap = {}  # for RGB -> BGR and the like

@property
def _Net_blobs(self):
  """
  An OrderedDict (bottom to top, i.e., input to output) of network
  blobs indexed by name
  """
  return OrderedDict([(bl.name, bl) for bl in self._blobs])

Net.blobs = _Net_blobs

@property
def _Net_params(self):
  """
  An OrderedDict (bottom to top, i.e., input to output) of network
  parameters indexed by name; each is a list of multiple blobs (e.g.,
  weights and biases)
  """
  return OrderedDict([(lr.name, lr.blobs) for lr in self.layers
                                          if len(lr.blobs) > 0])

Net.params = _Net_params

def _Net_forward(self, **kwargs):
  """
  Forward pass: prepare inputs and run the net forward.

  Take
    kwargs: Keys are input blob names and values are lists of inputs.
            Images must be (H x W x K) ndarrays.
            If None, input is taken from data layers by ForwardPrefilled().

  Give
    out: {output blob name: list of output blobs} dict.
  """
  outs = {}
  if not kwargs:
    # Carry out prefilled forward pass and unpack output.
    self.ForwardPrefilled()
    out_blobs = [self.blobs[out].data for out in self.outputs]
  else:
    # Create input and output blobs according to net defined shapes
    # and make arrays single and C-contiguous as Caffe expects.
    in_blobs = [np.ascontiguousarray(np.concatenate(kwargs[in_]),
                                     dtype=np.float32) for in_ in self.inputs]
    out_blobs = [np.empty(self.blobs[out].data.shape, dtype=np.float32)
                 for out in self.outputs]

    self.Forward(in_blobs, out_blobs)

  # Unpack output blobs
  for out, out_blob in zip(self.outputs, out_blobs):
    outs[out] = [out_blob[ix, :, :, :].squeeze()
                  for ix in range(out_blob.shape[0])]
  return outs

Net.forward = _Net_forward

def _Net_set_mean(self, input_, mean_f, mode='image'):
  """
  Set the mean to subtract for data centering.

  Take
    input_: which input to assign this mean.
    mean_f: path to mean .npy
    mode: image = use the whole-image mean (and check dimensions)
          channel = channel constant (i.e. mean pixel instead of mean image)
  """
  if input_ not in self.inputs:
    raise Exception('Input not in {}'.format(self.inputs))
  mean = np.load(mean_f)
  if mode == 'image':
    if mean.shape != self.input.data.shape[1:]:
      raise Exception('The mean shape does not match the input shape.')
    self.mean[input_] = mean
  elif mode == 'channel':
    self.mean[input_] = mean.mean(1).mean(1)
  else:
    raise Exception('Mode not in {}'.format(['image', 'channel']))

Net.set_mean = _Net_set_mean

def _Net_set_input_scale(self, input_, scale):
  """
  Set the input feature scaling factor s.t. input blob = input * scale.

  Take
    input_: which input to assign this scale factor
    scale: scale coefficient
  """
  if input_ not in self.inputs:
    raise Exception('Input not in {}'.format(self.inputs))
  self.input_scale[input_] = scale

Net.set_input_scale = _Net_set_input_scale

def _Net_set_channel_swap(self, input_, order):
  """
  Set the input channel order for e.g. RGB to BGR conversion
  as needed for the reference ImageNet model.

  Take
    input_: which input to assign this channel order
    order: the order to take the channels. (2,1,0) maps RGB to BGR for example.
  """
  if input_ not in self.inputs:
    raise Exception('Input not in {}'.format(self.inputs))
  self.channel_swap[input_] = order

Net.set_channel_swap = _Net_set_channel_swap

def _Net_format_image(self, input_, image):
  """
  Format image for input to Caffe:
  - convert to single
  - scale feature
  - reorder channels (for instance color to BGR)
  - subtract mean
  - reshape to 1 x K x H x W

  Take
    image: (H x W x K) ndarray

  Give
    image: (K x H x W) ndarray
  """
  caf_image = image.astype(np.float32)
  input_scale = self.input_scale.get(input_)
  channel_order = self.channel_swap.get(input_)
  mean = self.mean.get(input_)
  if input_scale:
    caf_image *= input_scale
  if channel_order:
    caf_image = caf_image[:, :, channel_order]
  if mean:
    caf_image -= mean
  caf_image = caf_image.transpose((2, 0, 1))
  caf_image = caf_image[np.newaxis, :, :, :]
  return caf_image

Net.format_image = _Net_format_image

def _Net_decaffeinate_image(self, input_, image):
  """
  Invert Caffe formatting; see _Net_format_image().
  """
  decaf_image = image.squeeze()
  decaf_image = decaf_image.transpose((1,2,0))
  input_scale = self.input_scale.get(input_)
  channel_order = self.channel_swap.get(input_)
  mean = self.mean.get(input_)
  if mean:
    decaf_image += mean
  if channel_order:
    decaf_image = decaf_image[:, :, channel_order[::-1]]
  if input_scale:
    decaf_image /= input_scale
  return decaf_image

Net.decaffeinate_image = _Net_decaffeinate_image

def _Net_set_input_arrays(self, data, labels):
  """
  Set input arrays of the in-memory MemoryDataLayer.
  (Note: this is only for networks declared with the memory data layer.)
  """
  if labels.ndim == 1:
    labels = np.ascontiguousarray(labels[:, np.newaxis, np.newaxis,
                                         np.newaxis])
  return self._set_input_arrays(data, labels)

Net.set_input_arrays = _Net_set_input_arrays
<|endoftext|>


<commit_before># coding: utf-8
from __future__ import print_function
from __future__ import unicode_literals

import requests
import bs4
import re

from cvbot import DEBUG

def response_for_image(image_url, client_name):
    base_url = 'http://deeplearning.cs.toronto.edu/api/url.php'
    files = {
        'urllink': ('', image_url),
        'url-2txt': ('', '')
    }
    headers = {
        'connection': 'keep-alive',
        'X-Requested-With': 'XMLHttpRequest',
        'User-agent': "@interesting_jpg %s v. 1.0" % client_name
    }
    try:
        r = requests.post(base_url, files=files, headers=headers, timeout=5*60)
    except requests.exceptions.ReadTimeout as err:
        print("read time out")
        return
    text = r.text.strip()
    if DEBUG:
        print(r)
    if not len(text):
        print('no text in response. status: %d %s' % (r.status_code, r.reason))
        return None
    return text

def nearest_neighbour(raw_text):
    if raw_text:
        soup = bs4.BeautifulSoup(raw_text, 'html.parser')
        try:
            return soup.li.get_text()
        except AttributeError as err:
            print(err)
            print(soup.prettify())
            return None

def captions(raw_text):
    soup = bs4.BeautifulSoup(raw_text)
    header = soup.find('h4', text=re.compile(r'Top'))
    if not header:
        print('error parsing text')
        print(soup.prettify())
        return
    if DEBUG:
        print(header.find_next_sibling().prettify())
    next_sib = header.find_next_sibling()
    if next_sib:
        captions = next_sib.find_all('li')
        if captions:
            return [c.text for c in captions]
    print("no captions found?")
    print(soup.prettify())

def top_caption(raw_text):
    all_captions = captions(raw_text)
    if DEBUG:
        print(all_captions)
    if all_captions:
        return all_captions[0]

def main():
    sample_response = """
TAGS:

  cycler  peddler  salesman  rucksack  pedicab

Nearest Neighbor Sentence:

a woman outside with an umbrella riding a motor cart .

Top-5 Generated:

two men are wearing a hat , riding on a bicycle with a backpack .

a man in a cart filled with bikes .

a man wearing a hat while trying to ride a bicycle on a bike .

a man riding a bicycle with a cart attached .

a man wearing a hat on a bicycle and carrying a cart .

"""

    print("\n".join(captions(sample_response)))
    print(top_caption(sample_response))

if __name__ == "__main__":
    main()
<commit_msg>added new caption-grabbing code<commit_after># coding: utf-8
from __future__ import print_function
from __future__ import unicode_literals

import requests
import bs4
import re

DEBUG = False

def response_for_image(image_url, client_name):
    base_url = 'http://deeplearning.cs.toronto.edu/api/url.php'
    files = {
        'urllink': ('', image_url),
        'url-2txt': ('', '')
    }
    headers = {
        'connection': 'keep-alive',
        'X-Requested-With': 'XMLHttpRequest',
        'User-agent': "@interesting_jpg %s v. 1.0" % client_name
    }
    try:
        r = requests.post(base_url, files=files, headers=headers, timeout=5*60)
    except requests.exceptions.ReadTimeout as err:
        print("read time out")
        return
    text = r.text.strip()
    if DEBUG:
        print(r)
    if not len(text):
        print('no text in response. status: %d %s' % (r.status_code, r.reason))
        return None
    return text

def nearest_neighbour(raw_text):
    if raw_text:
        soup = bs4.BeautifulSoup(raw_text, 'html.parser')
        try:
            return soup.li.get_text()
        except AttributeError as err:
            print(err)
            print(soup.prettify())
            return None

def captions(raw_text):
    soup = bs4.BeautifulSoup(raw_text)
    header = soup.find('h4', text=re.compile(r'Top'))
    if not header:
        print('error parsing text')
        print(soup.prettify())
        return
    if DEBUG:
        print(header.find_next_sibling().prettify())
    next_sib = header.find_next_sibling()
    if next_sib:
        captions = next_sib.find_all('li')
        if captions:
            return [c.text for c in captions]
    print("no captions found?")
    print(soup.prettify())

def top_caption(raw_text):
    return old_caption(raw_text) or new_caption(raw_text)

def old_caption(raw_text):
    all_captions = captions(raw_text)
    if DEBUG:
        print(all_captions)
    if all_captions:
        return all_captions[0]

def new_caption(raw_text):
    """
    Tue Jun 30 17:14:11 2015
    a single caption is being returned, in the tags field. This extracts that caption.
    """
    soup = bs4.BeautifulSoup(raw_text)
    tag = soup.find('h4', text=re.compile(r'TAGS'))
    if not tag:
        return

    return tag.next_sibling.text.strip()

def main():
    sample_response = """
TAGS:

  cycler  peddler  salesman  rucksack  pedicab

Nearest Neighbor Sentence:

a woman outside with an umbrella riding a motor cart .

Top-5 Generated:

two men are wearing a hat , riding on a bicycle with a backpack .

a man in a cart filled with bikes .

a man wearing a hat while trying to ride a bicycle on a bike .

a man riding a bicycle with a cart attached .

a man wearing a hat on a bicycle and carrying a cart .

"""

    new_response = '''
TAGS:

  A group of people sitting in front of a television .\n

Nearest Neighbor Sentence:

Top-5 Generated:

'''
    return print(top_caption(new_response))

    print("\n".join(captions(sample_response)))
    print(top_caption(sample_response))

if __name__ == "__main__":
    main()
<|endoftext|>


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

from openerp.osv import orm, fields
from lxml import etree

OPERATION_TYPE = {
    'out_invoice': 'output',
    'in_invoice': 'input',
    'out_refund': 'input',
    'in_refund': 'output'
}

JOURNAL_TYPE = {
    'out_invoice': 'sale',
    'in_invoice': 'purchase',
    'out_refund': 'sale_refund',
    'in_refund': 'purchase_refund'
}

class account_invoice_refund(orm.TransientModel):

    _inherit = "account.invoice.refund"
    _columns = {
        'fiscal_category_id': fields.many2one(
            'l10n_br_account.fiscal.category', 'Categoria Fiscal',
            domain="[('journal_type', '=', 'sale_refund'), ('fiscal_type', '=', 'product')]", required=True),
    }

    def compute_refund(self, cr, uid, ids, mode='refund', context=None):
        inv_obj = self.pool.get('account.invoice')
        if not context:
            context = {}
        res = super(account_invoice_refund, self).compute_refund(cr, uid, ids, mode, context)
        domain = res['domain']
        ids_domain = [x for x in domain if x[0] == 'id'][0]
        invoice_ids = ids_domain[2]
        for wizard in self.browse(cr, uid, ids):
            fiscal_category_id = wizard.fiscal_category_id.id
        for invoice in inv_obj.browse(cr, uid, invoice_ids, context=context):
            if invoice.payment_term:
                payment_term = invoice.payment_term.id
            else:
                payment_term = False
            if invoice.partner_bank_id:
                bank = invoice.partner_bank_id.id
            else:
                bank = False
            onchange = inv_obj.onchange_partner_id(cr, uid, [invoice.id], 'out_refund', invoice.partner_id.id, invoice.date_invoice, payment_term, bank, invoice.company_id.id, fiscal_category_id)
            onchange['value']['fiscal_category_id'] = fiscal_category_id
            inv_obj.write(cr, uid, [invoice.id], onchange['value'], context=context)
        return res

    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        res = super(account_invoice_refund, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
        if not context:
            context = {}
        type = context.get('type', 'out_invoice')
        journal_type = JOURNAL_TYPE[type]
        type = OPERATION_TYPE[type]
        eview = etree.fromstring(res['arch'])
        fiscal_categ = eview.xpath("//field[@name='fiscal_category_id']")
        for field in fiscal_categ:
            field.set('domain', "[('journal_type', '=', '%s'),('fiscal_type', '=', 'product'), ('type', '=', '%s')]" % (journal_type, type,))
        res['arch'] = etree.tostring(eview)
        print res['arch']
        return res

<commit_msg>[NEW] Posições fiscais e categorias de devolução nas faturas de reembolso.<commit_after># -*- encoding: utf-8 -*-
###############################################################################
#                                                                             #
# Copyright (C) 2013  Florian da Costa - Akretion                             #
#                                                                             #
#This program is free software: you can redistribute it and/or modify         #
#it under the terms of the GNU Affero General Public License as published by  #
#the Free Software Foundation, either version 3 of the License, or            #
#(at your option) any later version.                                          #
#                                                                             #
#This program is distributed in the hope that it will be useful,              #
#but WITHOUT ANY WARRANTY; without even the implied warranty of               #
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
#GNU Affero General Public License for more details.                          #
#                                                                             #
#You should have received a copy of the GNU Affero General Public License     #
#along with this program.  If not, see <http://www.gnu.org/licenses/>.        #
###############################################################################

from openerp.osv import orm, fields
from lxml import etree

OPERATION_TYPE = {
    'out_invoice': 'output',
    'in_invoice': 'input',
    'out_refund': 'input',
    'in_refund': 'output'
}

JOURNAL_TYPE = {
    'out_invoice': 'sale',
    'in_invoice': 'purchase',
    'out_refund': 'sale_refund',
    'in_refund': 'purchase_refund'
}

class account_invoice_refund(orm.TransientModel):

    _inherit = "account.invoice.refund"
    _columns = {
        'fiscal_category_id': fields.many2one(
            'l10n_br_account.fiscal.category', 'Categoria Fiscal',
            domain="[('journal_type', '=', 'sale_refund'), ('fiscal_type', '=', 'product')]", required=True),
    }

    def compute_refund(self, cr, uid, ids, mode='refund', context=None):
        print context.get('active_ids')

        inv_obj = self.pool.get('account.invoice')
        line_obj = self.pool.get('account.invoice.line')
        if not context:
            context = {}

        for send_invoice in inv_obj.browse(cr, uid, context.get('active_ids'), context):

            fiscal_category_id = send_invoice.fiscal_category_id.refund_fiscal_category_id.id

            res = super(account_invoice_refund, self).compute_refund(cr, uid, ids, mode, context)
            domain = res['domain']

            ids_domain = [x for x in domain if x[0] == 'id'][0]
            invoice_ids = ids_domain[2]

            for invoice in inv_obj.browse(cr, uid, invoice_ids, context=context):

                line_ids = line_obj.search(cr, uid, [('invoice_id', '=', invoice.id)])

                if invoice.payment_term:
                    payment_term = invoice.payment_term.id
                else:
                    payment_term = False
                if invoice.partner_bank_id:
                    bank = invoice.partner_bank_id.id
                else:
                    bank = False
                onchange = inv_obj.onchange_partner_id(cr, uid, [invoice.id], 'out_refund', invoice.partner_id.id, invoice.date_invoice, payment_term, bank, invoice.company_id.id, fiscal_category_id)
                onchange['value']['fiscal_category_id'] = fiscal_category_id

                for idx, send_line in enumerate(send_invoice.invoice_line):

                    line_fiscal_category_id = send_line.fiscal_category_id.refund_fiscal_category_id.id
                    line_onchange = line_obj.onchange_fiscal_category_id(cr, uid, ids, invoice.partner_id.id,
                                     invoice.company_id.id, send_line.product_id.id, line_fiscal_category_id,
                                     send_line.account_id.id, context)
                    line_obj.write(cr, uid, line_ids[idx],line_onchange['value'],context)
                inv_obj.write(cr, uid, [invoice.id], onchange['value'], context=context)
            return res

    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        res = super(account_invoice_refund, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
        if not context:
            context = {}
        type = context.get('type', 'out_invoice')
        journal_type = JOURNAL_TYPE[type]
        type = OPERATION_TYPE[type]
        eview = etree.fromstring(res['arch'])
        fiscal_categ = eview.xpath("//field[@name='fiscal_category_id']")
        for field in fiscal_categ:
            field.set('domain', "[('journal_type', '=', '%s'),('fiscal_type', '=', 'product'), ('type', '=', '%s')]" % (journal_type, type,))
        res['arch'] = etree.tostring(eview)
        print res['arch']
        return res

<|endoftext|>