<commit_before>from array import array

import constants
from pyipmi.utils import ByteBuffer
from pyipmi.errors import CompletionCodeError, EncodingError, DecodingError, \
        DescriptionError

class BaseField:
    def __init__(self, name, length, default=None):
        self.name = name
        self.length = length
        self.default = default

    def decode(self, obj, data):
        raise NotImplementedError()

    def encode(self, obj, data):
        raise NotImplementedError()

    def create(self):
        raise NotImplementedError()

class ByteArray(BaseField):
    def __init__(self, name, length, default=None):
        BaseField.__init__(self, name, length)
        if default is not None:
            self.default = array('B', default)
        else:
            self.default = None

    def encode(self, obj, data):
        a = getattr(obj, self.name)
        if len(a) != self.length:
            raise EncodingError('Array must be exaclty %d bytes long '
                    '(but is %d long)' % (self.length, len(a)))
        for i in xrange(self.length):
            data.push_unsigned_int(a[i], 1)

    def decode(self, obj, data):
        a = getattr(obj, self.name)
        bytes = []
        for i in xrange(self.length):
            bytes.append(data.pop_unsigned_int(1))
        setattr(obj, self.name, array('B', bytes))

    def create(self):
        if self.default is not None:
            return array('B', self.default)
        else:
            return array('B', self.length * '\x00')

class UnsignedInt(BaseField):
    def encode(self, obj, data):
        value = getattr(obj, self.name)
        data.push_unsigned_int(value, self.length)

    def decode(self, obj, data):
        value = data.pop_unsigned_int(self.length)
        setattr(obj, self.name, value)

    def create(self):
        if self.default is not None:
            return self.default
        else:
            return 0

class String(BaseField):
    def encode(self, obj, data):
        value = getattr(obj, self.name)
        data.push_string(value)
        data.from_string(value)

    def decode(self, obj, data):
        value = data.pop_string(self.length)
        setattr(obj, self.name, value)

    def create(self):
        if self.default is not None:
            return self.default
        else:
            return ''

class CompletionCode(UnsignedInt):
    def __init__(self, name='completion_code'):
        UnsignedInt.__init__(self, name, 1, None)

    def decode(self, obj, data):
        UnsignedInt.decode(self, obj, data)
        cc = getattr(obj, self.name)
        if cc != constants.CC_OK:
            raise CompletionCodeError(cc)

class UnsignedIntMask(UnsignedInt):
    def __init__(self, name, length, mask, default=None):
        UnsignedInt.__init__(self, name, length, default)

class Timestamp(UnsignedInt):
    def __init__(self, name):
        UnsignedInt.__init__(self, name, 4, None)

class Conditional:
    def __init__(self, cond_fn, field):
        self._condition_fn = cond_fn
        self._field = field

    def __getattr__(self, name):
        return getattr(self._field, name)

    def encode(self, obj, data):
        if self._condition_fn(obj):
            self._field.encode(obj, data)

    def decode(self, obj, data):
        if self._condition_fn(obj):
            self._field.decode(obj, data)

class Optional:
    def __init__(self, field):
        self._field = field

    def __getattr__(self, name):
        return getattr(self._field, name)

    def decode(self, obj, data):
        if len(data) > 0:
            self._field.decode(obj,data)

    def encode(self, obj, data):
        self._field.encode(obj, data)

class RemainingBytes(BaseField):
    def __init__(self, name):
        BaseField.__init__(self, name, None)

    def encode(self, obj, data):
        a = getattr(obj, self.name)
        data.push_string(a)

    def decode(self, obj, data):
        setattr(obj, self.name, array('B', data[:]))
        del data[:]

    def create(self):
        return array('B')

class Bitfield(BaseField):
    class Bit:
        def __init__(self, name, width=1, default=None):
            self.name = name
            self._width = width
            self.default = default

    class ReservedBit(Bit):
        counter = 0
        def __init__(self, width, default=0):
            Bitfield.Bit.__init__(self, 'reserved_bit_%d' %
                    Bitfield.reserved_bit_counter, width, default)
            Bitfield.reserved_bit_counter += 1

    class BitWrapper:
        def __init__(self, bits):
            self._bits = bits
            for bit in bits:
                if hasattr(self, bit.name):
                    raise DescriptionError('Bit with name "%s" already added' %
                            bit.name)
                if bit.default is not None:
                    setattr(self, bit.name, bit.default)
                else:
                    setattr(self, bit.name, 0)

        def __str__(self):
            s = '['
            for attr in dir(self):
                if attr.startswith('_'):
                    continue
                s += '%s=%s ' % (attr, getattr(self, attr))
            s += ']'
            return s

    reserved_bit_counter = 0

    def __init__(self, name, length, *bits):
        BaseField.__init__(self, name, length)
        self._bits = bits
        self._precalc_offsets()

    def _precalc_offsets(self):
        offset = 0
        for b in self._bits:
            b._offset = offset
            offset += b._width
        if offset != 8 * self.length:
            raise DescriptionError('Bit description does not match bitfield '
                    'length')

    def encode(self, obj, data):
        value = 0
        for bit in self._bits:
            wrapper = getattr(obj, self.name)
            bit_value = getattr(wrapper, bit.name)
            if bit_value is None:
                bit_value = bit.default
            if bit_value == None:
                raise EncodingError('Bitfield "%s" not set.' % bit.name)

            value |= (bit_value & (2**bit._width - 1)) << bit._offset
        for i in xrange(self.length):
            data.push_unsigned_int((value >> (8*i)) & 0xff, 1)

    def decode(self, obj, data):
        value = 0
        for i in xrange(self.length):
            try:
                value |= data.pop_unsigned_int(1) << (8*i)
            except IndexError:
                raise DecodingError('Data too short for message')
        for bit in self._bits:
            tmp = (value >> bit._offset) & (2**bit._width - 1)
            wrapper = getattr(obj, self.name)
            setattr(wrapper, bit.name, tmp)

    def create(self):
        return Bitfield.BitWrapper(self._bits)

class Message:
    RESERVED_FIELD_NAMES = ['cmdid', 'netfn', 'lun']

    def __init__(self, *args, **kwargs):
        """Message constructor with ([buf], [field=val,...]) prototype.

        Arguments:

        buf -- option message buffer to decode

        Optional keyword arguments corresponts to members to set (matching
        fields in self.__fields__, or 'data').
        """

        # create message fields
        if hasattr(self, '__fields__'):
            self._create_fields()

        # set default lun
        self.lun = self.__default_lun__

        self.data = ''
        if args:
            self._decode(args[0])
        else:
            for (name, value) in kwargs.iteritems():
                self._set_field(name, value)

    def _set_field(self, name, value):
        raise NotImplementedError()
        # TODO walk along the properties..
        setattr(self, name, value)

    def _create_fields(self):
        for field in self.__fields__:
            if field.name in self.RESERVED_FIELD_NAMES:
                raise DescriptionError('Field name "%s" is reserved' %
                        field.name)
            if hasattr(self, field.name):
                raise DescriptionError('Field "%s" already added',
                        field.name)
            setattr(self, field.name, field.create())

    def _encode(self):
        if not hasattr(self, '__fields__'):
            raise NotImplementedError('You have to overwrite this method')

        data = ByteBuffer()
        for field in self.__fields__:
            if getattr(self, field.name) == None:
                raise EncodingError('Field "%s" not set.' % field.name)
            field.encode(self, data)
        return data.to_string()

    def _decode(self, data):
        if not hasattr(self, '__fields__'):
            raise NotImplementedError('You have to overwrite this method')

        data = ByteBuffer(data)
        cc = None
        for field in self.__fields__:
            try:
                field.decode(self, data)
            except CompletionCodeError, e:
                # stop decoding on completion code != 0
                cc = e.cc
                break

        if (cc == None or cc == 0) and len(data) > 0:
            raise DecodingError('Data has extra bytes')

    def _is_request(self):
        return self.__netfn__ & 1 == 0

    def _is_response(self):
        return self.__netfn__ & 1 == 1

    netfn = property(lambda s: s.__netfn__)
    cmdid = property(lambda s: s.__cmdid__)

encode_message = lambda m: m._encode()
decode_message = lambda m,d: m._decode(d)

<commit_msg>msgs: various fixes<commit_after>from array import array

import constants
from pyipmi.utils import ByteBuffer
from pyipmi.errors import CompletionCodeError, EncodingError, DecodingError, \
        DescriptionError

class BaseField:
    def __init__(self, name, length, default=None):
        self.name = name
        self.length = length
        self.default = default

    def decode(self, obj, data):
        raise NotImplementedError()

    def encode(self, obj, data):
        if getattr(obj, self.name) == None:
            raise EncodingError('Field "%s" not set.' % self.name)
        raise NotImplementedError()

    def create(self):
        raise NotImplementedError()

class ByteArray(BaseField):
    def __init__(self, name, length, default=None):
        BaseField.__init__(self, name, length)
        if default is not None:
            self.default = array('B', default)
        else:
            self.default = None

    def encode(self, obj, data):
        a = getattr(obj, self.name)
        if len(a) != self.length:
            raise EncodingError('Array must be exaclty %d bytes long '
                    '(but is %d long)' % (self.length, len(a)))
        for i in xrange(self.length):
            data.push_unsigned_int(a[i], 1)

    def decode(self, obj, data):
        a = getattr(obj, self.name)
        bytes = []
        for i in xrange(self.length):
            bytes.append(data.pop_unsigned_int(1))
        setattr(obj, self.name, array('B', bytes))

    def create(self):
        if self.default is not None:
            return array('B', self.default)
        else:
            return array('B', self.length * '\x00')

class UnsignedInt(BaseField):
    def encode(self, obj, data):
        value = getattr(obj, self.name)
        data.push_unsigned_int(value, self.length)

    def decode(self, obj, data):
        value = data.pop_unsigned_int(self.length)
        setattr(obj, self.name, value)

    def create(self):
        if self.default is not None:
            return self.default
        else:
            return 0

class String(BaseField):
    def encode(self, obj, data):
        value = getattr(obj, self.name)
        data.push_string(value)
        data.from_string(value)

    def decode(self, obj, data):
        value = data.pop_string(self.length)
        setattr(obj, self.name, value)

    def create(self):
        if self.default is not None:
            return self.default
        else:
            return ''

class CompletionCode(UnsignedInt):
    def __init__(self, name='completion_code'):
        UnsignedInt.__init__(self, name, 1, None)

    def decode(self, obj, data):
        UnsignedInt.decode(self, obj, data)
        cc = getattr(obj, self.name)
        if cc != constants.CC_OK:
            raise CompletionCodeError(cc)

class UnsignedIntMask(UnsignedInt):
    def __init__(self, name, length, mask, default=None):
        UnsignedInt.__init__(self, name, length, default)

class Timestamp(UnsignedInt):
    def __init__(self, name):
        UnsignedInt.__init__(self, name, 4, None)

class Conditional:
    def __init__(self, cond_fn, field):
        self._condition_fn = cond_fn
        self._field = field

    def __getattr__(self, name):
        return getattr(self._field, name)

    def encode(self, obj, data):
        if self._condition_fn(obj):
            self._field.encode(obj, data)

    def decode(self, obj, data):
        if self._condition_fn(obj):
            self._field.decode(obj, data)

class Optional:
    def __init__(self, field):
        self._field = field

    def __getattr__(self, name):
        return getattr(self._field, name)

    def decode(self, obj, data):
        if len(data) > 0:
            self._field.decode(obj,data)
        else:
            setattr(obj, self._field.name, None)

    def encode(self, obj, data):
        if getattr(obj, self._field.name) is not None:
            self._field.encode(obj, data)

    def create(self):
        return None

class RemainingBytes(BaseField):
    def __init__(self, name):
        BaseField.__init__(self, name, None)

    def encode(self, obj, data):
        a = getattr(obj, self.name)
        data.push_string(a)

    def decode(self, obj, data):
        setattr(obj, self.name, array('B', data[:]))
        del data[:]

    def create(self):
        return array('B')

class Bitfield(BaseField):
    class Bit:
        def __init__(self, name, width=1, default=None):
            self.name = name
            self._width = width
            self.default = default

    class ReservedBit(Bit):
        counter = 0
        def __init__(self, width, default=0):
            Bitfield.Bit.__init__(self, 'reserved_bit_%d' %
                    Bitfield.reserved_bit_counter, width, default)
            Bitfield.reserved_bit_counter += 1

    class BitWrapper:
        def __init__(self, bits):
            self._bits = bits
            for bit in bits:
                if hasattr(self, bit.name):
                    raise DescriptionError('Bit with name "%s" already added' %
                            bit.name)
                if bit.default is not None:
                    setattr(self, bit.name, bit.default)
                else:
                    setattr(self, bit.name, 0)

        def __str__(self):
            s = '['
            for attr in dir(self):
                if attr.startswith('_'):
                    continue
                s += '%s=%s ' % (attr, getattr(self, attr))
            s += ']'
            return s

    reserved_bit_counter = 0

    def __init__(self, name, length, *bits):
        BaseField.__init__(self, name, length)
        self._bits = bits
        self._precalc_offsets()

    def _precalc_offsets(self):
        offset = 0
        for b in self._bits:
            b._offset = offset
            offset += b._width
        if offset != 8 * self.length:
            raise DescriptionError('Bit description does not match bitfield '
                    'length')

    def encode(self, obj, data):
        value = 0
        for bit in self._bits:
            wrapper = getattr(obj, self.name)
            bit_value = getattr(wrapper, bit.name)
            if bit_value is None:
                bit_value = bit.default
            if bit_value == None:
                raise EncodingError('Bitfield "%s" not set.' % bit.name)

            value |= (bit_value & (2**bit._width - 1)) << bit._offset
        for i in xrange(self.length):
            data.push_unsigned_int((value >> (8*i)) & 0xff, 1)

    def decode(self, obj, data):
        value = 0
        for i in xrange(self.length):
            try:
                value |= data.pop_unsigned_int(1) << (8*i)
            except IndexError:
                raise DecodingError('Data too short for message')
        for bit in self._bits:
            tmp = (value >> bit._offset) & (2**bit._width - 1)
            wrapper = getattr(obj, self.name)
            setattr(wrapper, bit.name, tmp)

    def create(self):
        return Bitfield.BitWrapper(self._bits)

class Message:
    RESERVED_FIELD_NAMES = ['cmdid', 'netfn', 'lun']

    def __init__(self, *args, **kwargs):
        """Message constructor with ([buf], [field=val,...]) prototype.

        Arguments:

        buf -- option message buffer to decode

        Optional keyword arguments corresponts to members to set (matching
        fields in self.__fields__, or 'data').
        """

        # create message fields
        if hasattr(self, '__fields__'):
            self._create_fields()

        # set default lun
        self.lun = self.__default_lun__

        self.data = ''
        if args:
            self._decode(args[0])
        else:
            for (name, value) in kwargs.iteritems():
                self._set_field(name, value)

    def _set_field(self, name, value):
        raise NotImplementedError()
        # TODO walk along the properties..
        setattr(self, name, value)

    def _create_fields(self):
        for field in self.__fields__:
            if field.name in self.RESERVED_FIELD_NAMES:
                raise DescriptionError('Field name "%s" is reserved' %
                        field.name)
            if hasattr(self, field.name):
                raise DescriptionError('Field "%s" already added',
                        field.name)
            setattr(self, field.name, field.create())

    def _encode(self):
        if not hasattr(self, '__fields__'):
            raise NotImplementedError('You have to overwrite this method')

        data = ByteBuffer()
        for field in self.__fields__:
            field.encode(self, data)
        return data.to_string()

    def _decode(self, data):
        if not hasattr(self, '__fields__'):
            raise NotImplementedError('You have to overwrite this method')

        data = ByteBuffer(data)
        cc = None
        for field in self.__fields__:
            try:
                field.decode(self, data)
            except CompletionCodeError, e:
                # stop decoding on completion code != 0
                cc = e.cc
                break

        if (cc == None or cc == 0) and len(data) > 0:
            raise DecodingError('Data has extra bytes')

    def _is_request(self):
        return self.__netfn__ & 1 == 0

    def _is_response(self):
        return self.__netfn__ & 1 == 1

    netfn = property(lambda s: s.__netfn__)
    cmdid = property(lambda s: s.__cmdid__)

encode_message = lambda m: m._encode()
decode_message = lambda m,d: m._decode(d)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2006 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <jonas@edgewall.com>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <jonas@edgewall.com>

import re
import time

from trac.config import IntOption
from trac.core import *
from trac.perm import IPermissionRequestor
from trac.util.datefmt import format_datetime
from trac.util.markup import escape, html, Element
from trac.web import IRequestHandler
from trac.web.chrome import add_link, add_stylesheet, INavigationContributor
from trac.wiki import IWikiSyntaxProvider, wiki_to_link

class ISearchSource(Interface):
    """
    Extension point interface for adding search sources to the Trac
    Search system.
    """

    def get_search_filters(self, req):
        """
        Return a list of filters that this search source supports. Each
        filter must be a (name, label[, default]) tuple, where `name` is the
        internal name, `label` is a human-readable name for display and
        `default` is an optional boolean for determining whether this filter
        is searchable by default.
        """

    def get_search_results(self, req, terms, filters):
        """
        Return a list of search results matching each search term in `terms`.
        The `filters` parameters is a list of the enabled
        filters, each item being the name of the tuples returned by
        `get_search_events`.

        The events returned by this function must be tuples of the form
        (href, title, date, author, excerpt).
        """

def search_terms(q):
    """
    Break apart a search query into its various search terms.  Terms are
    grouped implicitly by word boundary, or explicitly by (single or double)
    quotes.
    """
    results = []
    for term in re.split('(".*?")|(\'.*?\')|(\s+)', q):
        if term != None and term.strip() != '':
            if term[0] == term[-1] == "'" or term[0] == term[-1] == '"':
                term = term[1:-1]
            results.append(term)
    return results

def search_to_sql(db, columns, terms):
    """
    Convert a search query into a SQL condition string and corresponding
    parameters. The result is returned as a (string, params) tuple.
    """
    if len(columns) < 1 or len(terms) < 1:
        raise TracError('Empty search attempt, this should really not happen.')

    likes = ['%s %s' % (i, db.like()) for i in columns]
    c = ' OR '.join(likes)
    sql = '(' + ') AND ('.join([c] * len(terms)) + ')'
    args = []
    for t in terms:
        args.extend(['%'+db.like_escape(t)+'%'] * len(columns))
    return sql, tuple(args)

def shorten_result(text='', keywords=[], maxlen=240, fuzz=60):
    if not text: text = ''
    text_low = text.lower()
    beg = -1
    for k in keywords:
        i = text_low.find(k.lower())
        if (i > -1 and i < beg) or beg == -1:
            beg = i
    excerpt_beg = 0
    if beg > fuzz:
        for sep in ('.', ':', ';', '='):
            eb = text.find(sep, beg - fuzz, beg - 1)
            if eb > -1:
                eb += 1
                break
        else:
            eb = beg - fuzz
        excerpt_beg = eb
    if excerpt_beg < 0: excerpt_beg = 0
    msg = text[excerpt_beg:beg+maxlen]
    if beg > fuzz:
        msg = '... ' + msg
    if beg < len(text)-maxlen:
        msg = msg + ' ...'
    return msg

class SearchModule(Component):

    implements(INavigationContributor, IPermissionRequestor, IRequestHandler,
               IWikiSyntaxProvider)

    search_sources = ExtensionPoint(ISearchSource)

    RESULTS_PER_PAGE = 10

    min_query_length = IntOption('search', 'min_query_length', 3,
        """Minimum length of query string allowed when performing a search.""")

    # INavigationContributor methods

    def get_active_navigation_item(self, req):
        return 'search'

    def get_navigation_items(self, req):
        if not req.perm.has_permission('SEARCH_VIEW'):
            return
        yield ('mainnav', 'search',
               html.A('Search', href=req.href.search(), accesskey=4))

    # IPermissionRequestor methods

    def get_permission_actions(self):
        return ['SEARCH_VIEW']

    # IRequestHandler methods

    def match_request(self, req):
        return re.match(r'/search/?', req.path_info) is not None

    def process_request(self, req):
        req.perm.assert_permission('SEARCH_VIEW')

        available_filters = []
        for source in self.search_sources:
            available_filters += source.get_search_filters(req)

        filters = [f[0] for f in available_filters if req.args.has_key(f[0])]
        if not filters:
            filters = [f[0] for f in available_filters
                       if len(f) < 3 or len(f) > 2 and f[2]]

        req.hdf['search.filters'] = [
            { 'name': filter[0],
              'label': filter[1],
              'active': filter[0] in filters
            } for filter in available_filters]

        req.hdf['title'] = 'Search'

        query = req.args.get('q')
        if query:
            page = int(req.args.get('page', '1'))
            if query.startswith('!'):
                query = query[1:]
            self.check_quickjump(req, query)
            terms = search_terms(query)
            # Refuse queries that obviously would result in a huge result set
            if len(terms) == 1 and len(terms[0]) < self.min_query_length:
                raise TracError('Search query too short. '
                                'Query must be at least %d characters long.' % \
                                self.min_query_length, 'Search Error')
            results = []
            for source in self.search_sources:
                results += list(source.get_search_results(req, terms, filters))
            results.sort(lambda x,y: cmp(y[2], x[2]))
            page_size = self.RESULTS_PER_PAGE
            n = len(results)
            n_pages = (n-1) / page_size + 1
            results = results[(page-1) * page_size: page * page_size]

            req.hdf['title'] = 'Search Results'
            req.hdf['search.q'] = req.args.get('q')
            req.hdf['search.page'] = page
            req.hdf['search.n_hits'] = n
            req.hdf['search.n_pages'] = n_pages
            req.hdf['search.page_size'] = page_size
            if page < n_pages:
                next_href = req.href.search(zip(filters, ['on'] * len(filters)),
                                            q=req.args.get('q'), page=page + 1)
                add_link(req, 'next', next_href, 'Next Page')
            if page > 1:
                prev_href = req.href.search(zip(filters, ['on'] * len(filters)),
                                            q=req.args.get('q'), page=page - 1)
                add_link(req, 'prev', prev_href, 'Previous Page')
            req.hdf['search.page_href'] = req.href.search(zip(filters, ['on'] * len(filters)),
                                                          q=req.args.get('q'))
            req.hdf['search.result'] = [
                { 'href': result[0],
                  'title': result[1],
                  'date': format_datetime(result[2]),
                  'author': result[3],
                  'excerpt': result[4]
                } for result in results]

        add_stylesheet(req, 'common/css/search.css')
        return 'search.cs', None

    def check_quickjump(self, req, kwd):
        noquickjump = int(req.args.get('noquickjump', '0'))
        # Source quickjump
        quickjump_href = None
        if kwd[0] == '/':
            quickjump_href = req.href.browser(kwd)
            name = kwd
            description = 'Browse repository path ' + kwd
        else:
            link = wiki_to_link(kwd, self.env, req)
            if isinstance(link, Element):
                quickjump_href = link.attr['href']
                name = link.children
                description = link.attr.get('title', '')
        if quickjump_href:
            if noquickjump:
                req.hdf['search.quickjump'] = {
                    'href': quickjump_href,
                    'name': html.EM(name),
                    'description': description
                    }
            else:
                req.redirect(quickjump_href)

    # IWikiSyntaxProvider methods

    def get_wiki_syntax(self):
        return []

    def get_link_resolvers(self):
        yield ('search', self._format_link)

    def _format_link(self, formatter, ns, target, label):
        path, query, fragment = formatter.split_link(target)
        if query:
            href = formatter.href.search() + query.replace(' ', '+')
        else:
            href = formatter.href.search(q=path)
        return html.A(label, class_='search', href=href)
<commit_msg>Follow-up to r3576: the quickjump check should of course be done before the removal of the "!" escape, so by entering e.g. "!!WikiStart" in the quickjump box, you get the search results for WikiStart instead of jumping there.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2006 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <jonas@edgewall.com>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <jonas@edgewall.com>

import re
import time

from trac.config import IntOption
from trac.core import *
from trac.perm import IPermissionRequestor
from trac.util.datefmt import format_datetime
from trac.util.markup import escape, html, Element
from trac.web import IRequestHandler
from trac.web.chrome import add_link, add_stylesheet, INavigationContributor
from trac.wiki import IWikiSyntaxProvider, wiki_to_link

class ISearchSource(Interface):
    """
    Extension point interface for adding search sources to the Trac
    Search system.
    """

    def get_search_filters(self, req):
        """
        Return a list of filters that this search source supports. Each
        filter must be a (name, label[, default]) tuple, where `name` is the
        internal name, `label` is a human-readable name for display and
        `default` is an optional boolean for determining whether this filter
        is searchable by default.
        """

    def get_search_results(self, req, terms, filters):
        """
        Return a list of search results matching each search term in `terms`.
        The `filters` parameters is a list of the enabled
        filters, each item being the name of the tuples returned by
        `get_search_events`.

        The events returned by this function must be tuples of the form
        (href, title, date, author, excerpt).
        """

def search_terms(q):
    """
    Break apart a search query into its various search terms.  Terms are
    grouped implicitly by word boundary, or explicitly by (single or double)
    quotes.
    """
    results = []
    for term in re.split('(".*?")|(\'.*?\')|(\s+)', q):
        if term != None and term.strip() != '':
            if term[0] == term[-1] == "'" or term[0] == term[-1] == '"':
                term = term[1:-1]
            results.append(term)
    return results

def search_to_sql(db, columns, terms):
    """
    Convert a search query into a SQL condition string and corresponding
    parameters. The result is returned as a (string, params) tuple.
    """
    if len(columns) < 1 or len(terms) < 1:
        raise TracError('Empty search attempt, this should really not happen.')

    likes = ['%s %s' % (i, db.like()) for i in columns]
    c = ' OR '.join(likes)
    sql = '(' + ') AND ('.join([c] * len(terms)) + ')'
    args = []
    for t in terms:
        args.extend(['%'+db.like_escape(t)+'%'] * len(columns))
    return sql, tuple(args)

def shorten_result(text='', keywords=[], maxlen=240, fuzz=60):
    if not text: text = ''
    text_low = text.lower()
    beg = -1
    for k in keywords:
        i = text_low.find(k.lower())
        if (i > -1 and i < beg) or beg == -1:
            beg = i
    excerpt_beg = 0
    if beg > fuzz:
        for sep in ('.', ':', ';', '='):
            eb = text.find(sep, beg - fuzz, beg - 1)
            if eb > -1:
                eb += 1
                break
        else:
            eb = beg - fuzz
        excerpt_beg = eb
    if excerpt_beg < 0: excerpt_beg = 0
    msg = text[excerpt_beg:beg+maxlen]
    if beg > fuzz:
        msg = '... ' + msg
    if beg < len(text)-maxlen:
        msg = msg + ' ...'
    return msg

class SearchModule(Component):

    implements(INavigationContributor, IPermissionRequestor, IRequestHandler,
               IWikiSyntaxProvider)

    search_sources = ExtensionPoint(ISearchSource)

    RESULTS_PER_PAGE = 10

    min_query_length = IntOption('search', 'min_query_length', 3,
        """Minimum length of query string allowed when performing a search.""")

    # INavigationContributor methods

    def get_active_navigation_item(self, req):
        return 'search'

    def get_navigation_items(self, req):
        if not req.perm.has_permission('SEARCH_VIEW'):
            return
        yield ('mainnav', 'search',
               html.A('Search', href=req.href.search(), accesskey=4))

    # IPermissionRequestor methods

    def get_permission_actions(self):
        return ['SEARCH_VIEW']

    # IRequestHandler methods

    def match_request(self, req):
        return re.match(r'/search/?', req.path_info) is not None

    def process_request(self, req):
        req.perm.assert_permission('SEARCH_VIEW')

        available_filters = []
        for source in self.search_sources:
            available_filters += source.get_search_filters(req)

        filters = [f[0] for f in available_filters if req.args.has_key(f[0])]
        if not filters:
            filters = [f[0] for f in available_filters
                       if len(f) < 3 or len(f) > 2 and f[2]]

        req.hdf['search.filters'] = [
            { 'name': filter[0],
              'label': filter[1],
              'active': filter[0] in filters
            } for filter in available_filters]

        req.hdf['title'] = 'Search'

        query = req.args.get('q')
        if query:
            page = int(req.args.get('page', '1'))
            self.check_quickjump(req, query)
            if query.startswith('!'):
                query = query[1:]
            terms = search_terms(query)
            # Refuse queries that obviously would result in a huge result set
            if len(terms) == 1 and len(terms[0]) < self.min_query_length:
                raise TracError('Search query too short. '
                                'Query must be at least %d characters long.' % \
                                self.min_query_length, 'Search Error')
            results = []
            for source in self.search_sources:
                results += list(source.get_search_results(req, terms, filters))
            results.sort(lambda x,y: cmp(y[2], x[2]))
            page_size = self.RESULTS_PER_PAGE
            n = len(results)
            n_pages = (n-1) / page_size + 1
            results = results[(page-1) * page_size: page * page_size]

            req.hdf['title'] = 'Search Results'
            req.hdf['search.q'] = req.args.get('q')
            req.hdf['search.page'] = page
            req.hdf['search.n_hits'] = n
            req.hdf['search.n_pages'] = n_pages
            req.hdf['search.page_size'] = page_size
            if page < n_pages:
                next_href = req.href.search(zip(filters, ['on'] * len(filters)),
                                            q=req.args.get('q'), page=page + 1)
                add_link(req, 'next', next_href, 'Next Page')
            if page > 1:
                prev_href = req.href.search(zip(filters, ['on'] * len(filters)),
                                            q=req.args.get('q'), page=page - 1)
                add_link(req, 'prev', prev_href, 'Previous Page')
            req.hdf['search.page_href'] = req.href.search(zip(filters, ['on'] * len(filters)),
                                                          q=req.args.get('q'))
            req.hdf['search.result'] = [
                { 'href': result[0],
                  'title': result[1],
                  'date': format_datetime(result[2]),
                  'author': result[3],
                  'excerpt': result[4]
                } for result in results]

        add_stylesheet(req, 'common/css/search.css')
        return 'search.cs', None

    def check_quickjump(self, req, kwd):
        noquickjump = int(req.args.get('noquickjump', '0'))
        # Source quickjump
        quickjump_href = None
        if kwd[0] == '/':
            quickjump_href = req.href.browser(kwd)
            name = kwd
            description = 'Browse repository path ' + kwd
        else:
            link = wiki_to_link(kwd, self.env, req)
            if isinstance(link, Element):
                quickjump_href = link.attr['href']
                name = link.children
                description = link.attr.get('title', '')
        if quickjump_href:
            if noquickjump:
                req.hdf['search.quickjump'] = {
                    'href': quickjump_href,
                    'name': html.EM(name),
                    'description': description
                    }
            else:
                req.redirect(quickjump_href)

    # IWikiSyntaxProvider methods

    def get_wiki_syntax(self):
        return []

    def get_link_resolvers(self):
        yield ('search', self._format_link)

    def _format_link(self, formatter, ns, target, label):
        path, query, fragment = formatter.split_link(target)
        if query:
            href = formatter.href.search() + query.replace(' ', '+')
        else:
            href = formatter.href.search(q=path)
        return html.A(label, class_='search', href=href)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html

"""
USAGE: %(program)s -train CORPUS -output VECTORS -size SIZE -window WINDOW
-cbow CBOW -sample SAMPLE -hs HS -negative NEGATIVE -threads THREADS -iter ITER
-min_count MIN-COUNT -binary BINARY -accuracy FILE

Trains a neural embedding model on text file CORPUS.
Parameters essentially reproduce those used by the original C tool
(see https://code.google.com/archive/p/word2vec/).

Parameters for training:
        -train <file>
                Use text data from <file> to train the model
        -output <file>
                Use <file> to save the resulting word vectors / word clusters
        -size <int>
                Set size of word vectors; default is 100
        -window <int>
                Set max skip length between words; default is 5
        -sample <float>
                Set threshold for occurrence of words. Those that appear with higher frequency in the training data
                will be randomly down-sampled; default is 1e-3, useful range is (0, 1e-5)
        -hs <int>
                Use Hierarchical Softmax; default is 0 (not used)
        -negative <int>
                Number of negative examples; default is 5, common values are 3 - 10 (0 = not used)
        -threads <int>
                Use <int> threads (default 3)
        -iter <int>
                Run more training iterations (default 5)
        -min_count <int>
                This will discard words that appear less than <int> times; default is 5
        -binary <int>
                Save the resulting vectors in binary moded; default is 0 (off)
        -cbow <int>
                Use the continuous bag of words model; default is 1 (use 0 for skip-gram model)
        -accuracy <file>
                Compute accuracy of the resulting model analogical inference power on questions file <file>
                See an example of questions file at https://code.google.com/p/word2vec/source/browse/trunk/questions-words.txt

Example: python -m gensim.scripts.word2vec_standalone -train data.txt -output vec.txt -size 200 -sample 1e-4 -binary 0 -iter 3
"""

import logging
import os.path
import sys
import argparse

logger = logging.getLogger(__name__)

if __name__ == "__main__":
    from gensim.models.word2vec import Word2Vec  # avoid referencing __main__ in pickle
    from gensim.models.word2vec_inner import FAST_VERSION
    from gensim.models.word2vec import LineSentence
    from numpy import seterr

    logging.basicConfig(
        format='%(asctime)s : %(threadName)s : %(levelname)s : %(message)s',
        level=logging.INFO)
    logger.info("running %s", " ".join(sys.argv))
    logger.info("using optimization %s", FAST_VERSION)

    # check and process cmdline input
    program = os.path.basename(sys.argv[0])
    if len(sys.argv) < 2:
        print(globals()['__doc__'] % locals())
        sys.exit(1)

    seterr(all='raise')  # don't ignore numpy errors

    parser = argparse.ArgumentParser()
    parser.add_argument("-train", help="Use text data from file TRAIN to train the model", required=True)
    parser.add_argument("-output", help="Use file OUTPUT to save the resulting word vectors")
    parser.add_argument("-window", help="Set max skip length WINDOW between words; default is 5", type=int, default=5)
    parser.add_argument("-size", help="Set size of word vectors; default is 100", type=int, default=100)
    parser.add_argument("-sample", help="""Set threshold for occurrence of words. Those that appear with higher frequency in the training data will be randomly down-sampled;
    default is 1e-3, useful range is (0, 1e-5)""", type=float, default=1e-3)
    parser.add_argument("-hs", help="Use Hierarchical Softmax; default is 0 (not used)", type=int, default=0, choices=[0, 1])
    parser.add_argument("-negative", help="Number of negative examples; default is 5, common values are 3 - 10 (0 = not used)", type=int, default=5)
    parser.add_argument("-threads", help="Use THREADS threads (default 3)", type=int, default=3)
    parser.add_argument("-iter", help="Run more training iterations (default 5)", type=int, default=5)
    parser.add_argument("-min_count", help="This will discard words that appear less than MIN_COUNT times; default is 5", type=int, default=5)
    parser.add_argument("-cbow", help="Use the continuous bag of words model; default is 1 (use 0 for skip-gram model)", type=int, default=1, choices=[0, 1])
    parser.add_argument("-binary", help="Save the resulting vectors in binary mode; default is 0 (off)", type=int, default=0, choices=[0, 1])
    parser.add_argument("-accuracy", help="Use questions from file ACCURACY to evaluate the model")

    args = parser.parse_args()

    if args.cbow == 0:
        skipgram = 1
    else:
        skipgram = 0

    corpus = LineSentence(args.train)

    model = Word2Vec(corpus, size=args.size, min_count=args.min_count, workers=args.threads, window=args.window,
    sample=args.sample, sg=skipgram, hs=args.hs, negative=args.negative, cbow_mean=1, iter=args.iter)

    if args.output:
        outfile = args.output
        model.save_word2vec_format(outfile, binary=args.binary)
    else:
        outfile = args.train.split('.')[0]
        model.save(outfile + '.model')
        if args.binary == 1:
            model.save_word2vec_format(outfile + '.model.bin', binary=True)
        else:
            model.save_word2vec_format(outfile + '.model.txt', binary=False)

    if args.accuracy:
        questions_file = args.accuracy
        model.accuracy(questions_file)

    logger.info("finished running %s", program)
<commit_msg>Alpha argument. with different defaults for CBOW ans skipgram.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html

"""
USAGE: %(program)s -train CORPUS -output VECTORS -size SIZE -window WINDOW
-cbow CBOW -sample SAMPLE -hs HS -negative NEGATIVE -threads THREADS -iter ITER
-min_count MIN-COUNT -alpha ALPHA -binary BINARY -accuracy FILE

Trains a neural embedding model on text file CORPUS.
Parameters essentially reproduce those used by the original C tool
(see https://code.google.com/archive/p/word2vec/).

Parameters for training:
        -train <file>
                Use text data from <file> to train the model
        -output <file>
                Use <file> to save the resulting word vectors / word clusters
        -size <int>
                Set size of word vectors; default is 100
        -window <int>
                Set max skip length between words; default is 5
        -sample <float>
                Set threshold for occurrence of words. Those that appear with higher frequency in the training data
                will be randomly down-sampled; default is 1e-3, useful range is (0, 1e-5)
        -hs <int>
                Use Hierarchical Softmax; default is 0 (not used)
        -negative <int>
                Number of negative examples; default is 5, common values are 3 - 10 (0 = not used)
        -threads <int>
                Use <int> threads (default 3)
        -iter <int>
                Run more training iterations (default 5)
        -min_count <int>
                This will discard words that appear less than <int> times; default is 5
        -alpha <float>
                Set the starting learning rate; default is 0.025 for skip-gram and 0.05 for CBOW
        -binary <int>
                Save the resulting vectors in binary moded; default is 0 (off)
        -cbow <int>
                Use the continuous bag of words model; default is 1 (use 0 for skip-gram model)
        -accuracy <file>
                Compute accuracy of the resulting model analogical inference power on questions file <file>
                See an example of questions file at https://code.google.com/p/word2vec/source/browse/trunk/questions-words.txt

Example: python -m gensim.scripts.word2vec_standalone -train data.txt -output vec.txt -size 200 -sample 1e-4 -binary 0 -iter 3
"""

import logging
import os.path
import sys
import argparse

logger = logging.getLogger(__name__)

if __name__ == "__main__":
    from gensim.models.word2vec import Word2Vec  # avoid referencing __main__ in pickle
    from gensim.models.word2vec_inner import FAST_VERSION
    from gensim.models.word2vec import LineSentence
    from numpy import seterr

    logging.basicConfig(
        format='%(asctime)s : %(threadName)s : %(levelname)s : %(message)s',
        level=logging.INFO)
    logger.info("running %s", " ".join(sys.argv))
    logger.info("using optimization %s", FAST_VERSION)

    # check and process cmdline input
    program = os.path.basename(sys.argv[0])
    if len(sys.argv) < 2:
        print(globals()['__doc__'] % locals())
        sys.exit(1)

    seterr(all='raise')  # don't ignore numpy errors

    parser = argparse.ArgumentParser()
    parser.add_argument("-train", help="Use text data from file TRAIN to train the model", required=True)
    parser.add_argument("-output", help="Use file OUTPUT to save the resulting word vectors")
    parser.add_argument("-window", help="Set max skip length WINDOW between words; default is 5", type=int, default=5)
    parser.add_argument("-size", help="Set size of word vectors; default is 100", type=int, default=100)
    parser.add_argument("-sample", help="""Set threshold for occurrence of words. Those that appear with higher frequency in the training data will be randomly down-sampled;
    default is 1e-3, useful range is (0, 1e-5)""", type=float, default=1e-3)
    parser.add_argument("-hs", help="Use Hierarchical Softmax; default is 0 (not used)", type=int, default=0, choices=[0, 1])
    parser.add_argument("-negative", help="Number of negative examples; default is 5, common values are 3 - 10 (0 = not used)", type=int, default=5)
    parser.add_argument("-threads", help="Use THREADS threads (default 3)", type=int, default=3)
    parser.add_argument("-iter", help="Run more training iterations (default 5)", type=int, default=5)
    parser.add_argument("-min_count", help="This will discard words that appear less than MIN_COUNT times; default is 5", type=int, default=5)
    parser.add_argument("-alpha", help="Set the starting learning rate; default is 0.025 for skip-gram and 0.05 for CBOW", type=float)
    parser.add_argument("-cbow", help="Use the continuous bag of words model; default is 1 (use 0 for skip-gram model)", type=int, default=1, choices=[0, 1])
    parser.add_argument("-binary", help="Save the resulting vectors in binary mode; default is 0 (off)", type=int, default=0, choices=[0, 1])
    parser.add_argument("-accuracy", help="Use questions from file ACCURACY to evaluate the model")

    args = parser.parse_args()

    if args.cbow == 0:
        skipgram = 1
        if not args.alpha:
            args.alpha = 0.025
    else:
        skipgram = 0
        if not args.alpha:
            args.alpha = 0.05

    corpus = LineSentence(args.train)

    model = Word2Vec(corpus, size=args.size, min_count=args.min_count, workers=args.threads, window=args.window,
    sample=args.sample, alpha=args.alpha, sg=skipgram, hs=args.hs, negative=args.negative, cbow_mean=1, iter=args.iter)

    if args.output:
        outfile = args.output
        model.save_word2vec_format(outfile, binary=args.binary)
    else:
        outfile = args.train.split('.')[0]
        model.save(outfile + '.model')
        if args.binary == 1:
            model.save_word2vec_format(outfile + '.model.bin', binary=True)
        else:
            model.save_word2vec_format(outfile + '.model.txt', binary=False)

    if args.accuracy:
        questions_file = args.accuracy
        model.accuracy(questions_file)

    logger.info("finished running %s", program)
<|endoftext|>


<commit_before># This file is part of the pyMOR project (http://www.pymor.org).
# Copyright Holders: Rene Milk, Stephan Rave, Felix Schindler
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

"""This module contains algorithms for the empirical interpolation of operators.

The main work for generating the necessary interpolation data is handled by
the :func:`ei_greedy` method. The objects returned by this method can be used
to instantiate an |EmpiricalInterpolatedOperator|.

:func:`ei_greedy` expects an iterable of operator evaluations which are to be
interpolated. These evaluation can be provided by an instance of
:class:`EvaluationProvider` which, given a discretization, names of |Operators|
and a set of parameters, provides evaluations of the |Operators| on the solution
snapshots for the given parameters. Caching of the evaluations is also
handled by :class:`EvaluationProvider`.

As a convenience, the :func:`interpolate_operators` method allows to perform
the empirical interpolation of the |Operators| of a given discretization with
a single function call.
"""

from __future__ import absolute_import, division, print_function

import numpy as np
from scipy.linalg import solve_triangular, cho_factor, cho_solve

from pymor.core.logger import getLogger
from pymor.core.cache import CacheableInterface, cached
from pymor.la.interfaces import VectorArrayInterface
from pymor.la.pod import pod
from pymor.operators.ei import EmpiricalInterpolatedOperator

def ei_greedy(U, error_norm=None, target_error=None, max_interpolation_dofs=None,
              projection='orthogonal', product=None):
    """Generate data for empirical operator interpolation by a greedy search (EI-Greedy algorithm).

    Given a |VectorArray| `U`, this method generates a collateral_basis and
    interpolation DOFs for empirical interpolation of the vectors contained in `U`.
    The returned objects can also be used to instantiate an |EmpiricalInterpolatedOperator|.

    The interpolation data is generated by a greedy search algorithm, adding in each
    loop the worst approximated vector in `U` to the collateral basis.

    Parameters
    ----------
    U
        A |VectorArray| of vectors to interpolate.
    error_norm
        Norm w.r.t. which to calculate the interpolation error. If `None`, the Euclidean norm
        is used.
    target_error
        Stop the greedy search if the largest approximation error is below this threshold.
    max_interpolation_dofs
        Stop the greedy search if the number of interpolation DOF (= dimension of the collateral
        basis) reaches this value.
    projection
        If `ei`, compute the approximation error by comparing the given vector by its
        interpolant. If `orthogonal`, compute the error by comparing with the orthogonal projection
        onto the span of the collateral basis.
    product
        If `projection == 'orthogonal'`, the product which is used to perform the projection.
        If `None`, the Euclidean product is used.

    Returns
    -------
    interpolation_dofs
        |NumPy array| of the DOFs at which the vectors are interpolated.
    collateral_basis
        |VectorArray| containing the generated collateral basis.
    data
        Dict containing the following fields:

            :errors:                sequence of maximum approximation errors during
                                    greedy search.
            :triangularity_errors:  sequence of maximum absolute values of interoplation
                                    matrix coefficients in the upper triangle (should
                                    be near zero).
    """

    assert projection in ('orthogonal', 'ei')
    assert isinstance(U, VectorArrayInterface)

    logger = getLogger('pymor.algorithms.ei.ei_greedy')
    logger.info('Generating Interpolation Data ...')

    interpolation_dofs = np.zeros((0,), dtype=np.int32)
    interpolation_matrix = np.zeros((0, 0))
    collateral_basis = U.empty()
    max_errs = []
    triangularity_errs = []

    def interpolate(U, ind=None):
        coefficients = solve_triangular(interpolation_matrix, U.components(interpolation_dofs, ind=ind).T,
                                        lower=True, unit_diagonal=True).T
        # coefficients = np.linalg.solve(interpolation_matrix, U.components(interpolation_dofs, ind=ind).T).T
        return collateral_basis.lincomb(coefficients)

    # main loop
    while True:
        # precompute gramian_inverse if needed
        if projection == 'orthogonal' and len(interpolation_dofs) > 0:
            if product is None:
                gramian = collateral_basis.gramian()
            else:
                gramian = product.apply2(collateral_basis, collateral_basis, pairwise=False)
            gramian_cholesky = cho_factor(gramian, overwrite_a=True)

        # compute interpolation error
        if len(interpolation_dofs) > 0:
            if projection == 'ei':
                U_interpolated = interpolate(U)
                ERR = U - U_interpolated
            else:
                if product is None:
                    coefficients = cho_solve(gramian_cholesky,
                                             collateral_basis.dot(U, pairwise=False)).T
                else:
                    coefficients = cho_solve(gramian_cholesky,
                                             product.apply2(collateral_basis, U, pairwise=False)).T
                U_projected = collateral_basis.lincomb(coefficients)
                ERR = U - U_projected
        else:
            ERR = U
        errs = ERR.l2_norm() if error_norm is None else error_norm(ERR)
        max_err_ind = np.argmax(errs)
        max_err = errs[max_err_ind]

        if len(interpolation_dofs) >= max_interpolation_dofs:
            logger.info('Maximum number of interpolation DOFs reached. Stopping extension loop.')
            logger.info('Final maximum interpolation error with {} interpolation DOFs: {}'.format(
                len(interpolation_dofs), max_err))
            break

        if len(interpolation_dofs) == 0 or projection == 'ei':
            new_vec = ERR.copy(ind=max_err_ind)
        else:
            new_vec = U.copy(ind=max_err_ind)
            new_vec -= interpolate(U, ind=max_err_ind)

        logger.info('Maximum interpolation error with {} interpolation DOFs: {}'.format(len(interpolation_dofs),
                                                                                max_err))
        if target_error is not None and max_err <= target_error:
            logger.info('Target error reached! Stopping extension loop.')
            break

        # compute new interpolation dof and collateral basis vector
        new_dof = new_vec.amax()[0][0]
        if new_dof in interpolation_dofs:
            logger.info('DOF {} selected twice for interplation! Stopping extension loop.'.format(new_dof))
            break
        new_dof_value = new_vec.components([new_dof])[0, 0]
        if new_dof_value == 0.:
            logger.info('DOF {} selected for interpolation has zero maximum error! Stopping extension loop.'.format(new_dof))
            break
        new_vec *= 1 / new_dof_value
        interpolation_dofs = np.hstack((interpolation_dofs, new_dof))
        collateral_basis.append(new_vec, remove_from_other=True)
        interpolation_matrix = collateral_basis.components(interpolation_dofs).T
        max_errs.append(max_err)

        triangularity_error = np.max(np.abs(interpolation_matrix - np.tril(interpolation_matrix)))
        triangularity_errs.append(triangularity_error)
        logger.info('Interpolation matrix is not lower triangular with maximum error of {}'
                    .format(triangularity_error))

        logger.info('')

    data = {'errors': max_errs, 'triangularity_errors': triangularity_errs}

    return interpolation_dofs, collateral_basis, data

def deim(evaluations, modes=None, error_norm=None, product=None):
    """Generate data for empirical operator interpolation using DEIM algorithm.

    Given evaluations of |Operators|, this method generates a collateral_basis and
    interpolation DOFs for empirical operator interpolation. The returned objects
    can be used to instantiate an |EmpiricalInterpolatedOperator|.

    The collateral basis is determined by the first POD modes of the operator
    evaluations.

    Parameters
    ----------
    evaluations
        A |VectorArray| of operator evaluations.
    modes
        Dimension of the collateral basis i.e. number of POD modes of the operator evaluations.
    error_norm
        Norm w.r.t. which to calculate the interpolation error. If `None`, the Euclidean norm
        is used.
    product
        Product |Operator| used for POD.

    Returns
    -------
    interpolation_dofs
        |NumPy array| of the DOFs at which the operators have to be evaluated.
    collateral_basis
        |VectorArray| containing the generated collateral basis.
    data
        Dict containing the following fields:

            :errors: sequence of maximum approximation errors during greedy search.
    """

    assert isinstance(evaluations, VectorArrayInterface)

    logger = getLogger('pymor.algorithms.ei.deim')
    logger.info('Generating Interpolation Data ...')

    collateral_basis = pod(evaluations, modes, product=product)[0]

    interpolation_dofs = np.zeros((0,), dtype=np.int32)
    interpolation_matrix = np.zeros((0, 0))
    errs = []

    for i in xrange(len(collateral_basis)):

        if len(interpolation_dofs) > 0:
            coefficients = np.linalg.solve(interpolation_matrix,
                                           collateral_basis.components(interpolation_dofs, ind=i).T).T
            U_interpolated = collateral_basis.lincomb(coefficients, ind=range(len(interpolation_dofs)))
            ERR = collateral_basis.copy(ind=i)
            ERR -= U_interpolated
        else:
            ERR = collateral_basis.copy(ind=i)

        err = ERR.l2_norm() if error_norm is None else error_norm(ERR)

        logger.info('Interpolation error for basis vector {}: {}'.format(i, err))

        # compute new interpolation dof and collateral basis vector
        new_dof = ERR.amax()[0][0]

        if new_dof in interpolation_dofs:
            logger.info('DOF {} selected twice for interplation! Stopping extension loop.'.format(new_dof))
            break

        interpolation_dofs = np.hstack((interpolation_dofs, new_dof))
        interpolation_matrix = collateral_basis.components(interpolation_dofs, ind=range(len(interpolation_dofs))).T
        errs.append(err)

        logger.info('')

    if len(interpolation_dofs) < len(collateral_basis):
        collateral_basis.remove(ind=range(len(interpolation_dofs), len(collateral_basis)))

    logger.info('Finished.'.format(new_dof))

    data = {'errors': errs}

    return interpolation_dofs, collateral_basis, data

def interpolate_operators(discretization, operator_names, parameter_sample, error_norm=None,
                          target_error=None, max_interpolation_dofs=None,
                          projection='orthogonal', product=None, cache_region='memory'):
    """Empirical operator interpolation using the EI-Greedy algorithm.

    This is a convenience method for facilitating the use of :func:`ei_greedy`. Given
    a |Discretization|, names of |Operators|, and a sample of |Parameters|, first the operators
    are evaluated on the solution snapshots of the discretization for the provided parameters.
    These evaluations are then used as input for :func:`ei_greedy`. Finally the resulting
    interpolation data is used to create |EmpiricalInterpolatedOperators| and a new
    discretization with the interpolated operators is returned.

    Note that this implementation creates ONE common collateral basis for all operators
    which might not be what you want.

    Parameters
    ----------
    discretization
        The |Discretization| whose |Operators| will be interpolated.
    operator_names
        List of keys in the `operators` dict of the discretization. The corresponding
        |Operators| will be interpolated.
    sample
        A list of |Parameters| for which solution snapshots are calculated.
    error_norm
        See :func:`ei_greedy`.
    target_error
        See :func:`ei_greedy`.
    max_interpolation_dofs
        See :func:`ei_greedy`.
    projection
        See :func:`ei_greedy`.
    product
        See :func:`ei_greedy`.
    cache_region
        Name of the |CacheRegion| in which the operator evaluations will be stored.

    Returns
    -------
    ei_discretization
        |Discretization| with |Operators| given by `operator_names` replaced by
        |EmpiricalInterpolatedOperators|.
    data
        Dict containing the following fields:

            :dofs:   |NumPy array| of the DOFs at which the |Operators| have to be evaluated.
            :basis:  |VectorArray| containing the generated collateral basis.
            :errors: sequence of maximum approximation errors during greedy search.
    """

    sample = tuple(parameter_sample)
    operators = [discretization.operators[operator_name] for operator_name in operator_names]

    evaluations = operators[0].range.empty()
    for mu in sample:
        U = discretization.solve(mu)
        for op in operators:
            evaluations.append(op.apply(U, mu=mu))

    dofs, basis, data = ei_greedy(evaluations, error_norm, target_error, max_interpolation_dofs,
                                  projection=projection, product=product)

    ei_operators = {name: EmpiricalInterpolatedOperator(operator, dofs, basis, triangular=True)
                    for name, operator in zip(operator_names, operators)}
    operators_dict = discretization.operators.copy()
    operators_dict.update(ei_operators)
    ei_discretization = discretization.with_(operators=operators_dict, name='{}_ei'.format(discretization.name))

    data.update({'dofs': dofs, 'basis': basis})
    return ei_discretization, data
<commit_msg>[algorithms.ei] vastly improve performance of ei_greedy<commit_after># This file is part of the pyMOR project (http://www.pymor.org).
# Copyright Holders: Rene Milk, Stephan Rave, Felix Schindler
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

"""This module contains algorithms for the empirical interpolation of operators.

The main work for generating the necessary interpolation data is handled by
the :func:`ei_greedy` method. The objects returned by this method can be used
to instantiate an |EmpiricalInterpolatedOperator|.

:func:`ei_greedy` expects an iterable of operator evaluations which are to be
interpolated. These evaluation can be provided by an instance of
:class:`EvaluationProvider` which, given a discretization, names of |Operators|
and a set of parameters, provides evaluations of the |Operators| on the solution
snapshots for the given parameters. Caching of the evaluations is also
handled by :class:`EvaluationProvider`.

As a convenience, the :func:`interpolate_operators` method allows to perform
the empirical interpolation of the |Operators| of a given discretization with
a single function call.
"""

from __future__ import absolute_import, division, print_function

import numpy as np

from pymor.core.logger import getLogger
from pymor.la.gram_schmidt import gram_schmidt
from pymor.la.interfaces import VectorArrayInterface
from pymor.la.pod import pod
from pymor.operators.ei import EmpiricalInterpolatedOperator

def ei_greedy(U, error_norm=None, target_error=None, max_interpolation_dofs=None,
              projection='orthogonal', product=None):
    """Generate data for empirical operator interpolation by a greedy search (EI-Greedy algorithm).

    Given a |VectorArray| `U`, this method generates a collateral_basis and
    interpolation DOFs for empirical interpolation of the vectors contained in `U`.
    The returned objects can also be used to instantiate an |EmpiricalInterpolatedOperator|.

    The interpolation data is generated by a greedy search algorithm, adding in each
    loop the worst approximated vector in `U` to the collateral basis.

    Parameters
    ----------
    U
        A |VectorArray| of vectors to interpolate.
    error_norm
        Norm w.r.t. which to calculate the interpolation error. If `None`, the Euclidean norm
        is used.
    target_error
        Stop the greedy search if the largest approximation error is below this threshold.
    max_interpolation_dofs
        Stop the greedy search if the number of interpolation DOF (= dimension of the collateral
        basis) reaches this value.
    projection
        If `ei`, compute the approximation error by comparing the given vector by its
        interpolant. If `orthogonal`, compute the error by comparing with the orthogonal projection
        onto the span of the collateral basis.
    product
        If `projection == 'orthogonal'`, the product which is used to perform the projection.
        If `None`, the Euclidean product is used.

    Returns
    -------
    interpolation_dofs
        |NumPy array| of the DOFs at which the vectors are interpolated.
    collateral_basis
        |VectorArray| containing the generated collateral basis.
    data
        Dict containing the following fields:

            :errors:                sequence of maximum approximation errors during
                                    greedy search.
            :triangularity_errors:  sequence of maximum absolute values of interoplation
                                    matrix coefficients in the upper triangle (should
                                    be near zero).
    """

    assert projection in ('orthogonal', 'ei')
    assert isinstance(U, VectorArrayInterface)

    logger = getLogger('pymor.algorithms.ei.ei_greedy')
    logger.info('Generating Interpolation Data ...')

    interpolation_dofs = np.zeros((0,), dtype=np.int32)
    interpolation_matrix = np.zeros((0, 0))
    collateral_basis = U.empty()
    max_errs = []
    triangularity_errs = []

    if projection == 'orthogonal':
        ERR = U.copy()
        onb_collateral_basis = collateral_basis.empty()
    else:
        ERR = U

    # main loop
    while True:
        errs = ERR.l2_norm() if error_norm is None else error_norm(ERR)
        max_err_ind = np.argmax(errs)
        max_err = errs[max_err_ind]

        if len(interpolation_dofs) >= max_interpolation_dofs:
            logger.info('Maximum number of interpolation DOFs reached. Stopping extension loop.')
            logger.info('Final maximum {} error with {} interpolation DOFs: {}'.format(
                'projection' if projection else 'interpolation', len(interpolation_dofs), max_err))
            break

        logger.info('Maximum {} error with {} interpolation DOFs: {}'
                    .format('projection' if projection else 'interpolation',
                            len(interpolation_dofs), max_err))

        if target_error is not None and max_err <= target_error:
            logger.info('Target error reached! Stopping extension loop.')
            break

        # compute new interpolation dof and collateral basis vector
        new_vec = U.copy(ind=max_err_ind)
        new_dof = new_vec.amax()[0][0]
        if new_dof in interpolation_dofs:
            logger.info('DOF {} selected twice for interplation! Stopping extension loop.'.format(new_dof))
            break
        new_dof_value = new_vec.components([new_dof])[0, 0]
        if new_dof_value == 0.:
            logger.info('DOF {} selected for interpolation has zero maximum error! Stopping extension loop.'
                        .format(new_dof))
            break
        new_vec *= 1 / new_dof_value
        interpolation_dofs = np.hstack((interpolation_dofs, new_dof))
        collateral_basis.append(new_vec)
        interpolation_matrix = collateral_basis.components(interpolation_dofs).T
        max_errs.append(max_err)

        triangularity_error = np.max(np.abs(interpolation_matrix - np.tril(interpolation_matrix)))
        triangularity_errs.append(triangularity_error)
        logger.info('Interpolation matrix is not lower triangular with maximum error of {}'
                    .format(triangularity_error))

        logger.info('')

        # update U and ERR
        new_dof_values = U.components([new_dof])
        U -= new_vec.lincomb(new_dof_values)
        if projection == 'orthogonal':
            onb_collateral_basis.append(new_vec)
            gram_schmidt(onb_collateral_basis, offset=len(onb_collateral_basis) - 1, copy=False)
            coeffs = ERR.dot(onb_collateral_basis, o_ind=len(onb_collateral_basis) - 1, pairwise=False)
            ERR -= onb_collateral_basis.lincomb(coeffs, ind=len(onb_collateral_basis) - 1)

    data = {'errors': max_errs, 'triangularity_errors': triangularity_errs}

    return interpolation_dofs, collateral_basis, data

def deim(evaluations, modes=None, error_norm=None, product=None):
    """Generate data for empirical operator interpolation using DEIM algorithm.

    Given evaluations of |Operators|, this method generates a collateral_basis and
    interpolation DOFs for empirical operator interpolation. The returned objects
    can be used to instantiate an |EmpiricalInterpolatedOperator|.

    The collateral basis is determined by the first POD modes of the operator
    evaluations.

    Parameters
    ----------
    evaluations
        A |VectorArray| of operator evaluations.
    modes
        Dimension of the collateral basis i.e. number of POD modes of the operator evaluations.
    error_norm
        Norm w.r.t. which to calculate the interpolation error. If `None`, the Euclidean norm
        is used.
    product
        Product |Operator| used for POD.

    Returns
    -------
    interpolation_dofs
        |NumPy array| of the DOFs at which the operators have to be evaluated.
    collateral_basis
        |VectorArray| containing the generated collateral basis.
    data
        Dict containing the following fields:

            :errors: sequence of maximum approximation errors during greedy search.
    """

    assert isinstance(evaluations, VectorArrayInterface)

    logger = getLogger('pymor.algorithms.ei.deim')
    logger.info('Generating Interpolation Data ...')

    collateral_basis = pod(evaluations, modes, product=product)[0]

    interpolation_dofs = np.zeros((0,), dtype=np.int32)
    interpolation_matrix = np.zeros((0, 0))
    errs = []

    for i in xrange(len(collateral_basis)):

        if len(interpolation_dofs) > 0:
            coefficients = np.linalg.solve(interpolation_matrix,
                                           collateral_basis.components(interpolation_dofs, ind=i).T).T
            U_interpolated = collateral_basis.lincomb(coefficients, ind=range(len(interpolation_dofs)))
            ERR = collateral_basis.copy(ind=i)
            ERR -= U_interpolated
        else:
            ERR = collateral_basis.copy(ind=i)

        err = ERR.l2_norm() if error_norm is None else error_norm(ERR)

        logger.info('Interpolation error for basis vector {}: {}'.format(i, err))

        # compute new interpolation dof and collateral basis vector
        new_dof = ERR.amax()[0][0]

        if new_dof in interpolation_dofs:
            logger.info('DOF {} selected twice for interplation! Stopping extension loop.'.format(new_dof))
            break

        interpolation_dofs = np.hstack((interpolation_dofs, new_dof))
        interpolation_matrix = collateral_basis.components(interpolation_dofs, ind=range(len(interpolation_dofs))).T
        errs.append(err)

        logger.info('')

    if len(interpolation_dofs) < len(collateral_basis):
        collateral_basis.remove(ind=range(len(interpolation_dofs), len(collateral_basis)))

    logger.info('Finished.'.format(new_dof))

    data = {'errors': errs}

    return interpolation_dofs, collateral_basis, data

def interpolate_operators(discretization, operator_names, parameter_sample, error_norm=None,
                          target_error=None, max_interpolation_dofs=None,
                          projection='orthogonal', product=None, cache_region='memory'):
    """Empirical operator interpolation using the EI-Greedy algorithm.

    This is a convenience method for facilitating the use of :func:`ei_greedy`. Given
    a |Discretization|, names of |Operators|, and a sample of |Parameters|, first the operators
    are evaluated on the solution snapshots of the discretization for the provided parameters.
    These evaluations are then used as input for :func:`ei_greedy`. Finally the resulting
    interpolation data is used to create |EmpiricalInterpolatedOperators| and a new
    discretization with the interpolated operators is returned.

    Note that this implementation creates ONE common collateral basis for all operators
    which might not be what you want.

    Parameters
    ----------
    discretization
        The |Discretization| whose |Operators| will be interpolated.
    operator_names
        List of keys in the `operators` dict of the discretization. The corresponding
        |Operators| will be interpolated.
    sample
        A list of |Parameters| for which solution snapshots are calculated.
    error_norm
        See :func:`ei_greedy`.
    target_error
        See :func:`ei_greedy`.
    max_interpolation_dofs
        See :func:`ei_greedy`.
    projection
        See :func:`ei_greedy`.
    product
        See :func:`ei_greedy`.
    cache_region
        Name of the |CacheRegion| in which the operator evaluations will be stored.

    Returns
    -------
    ei_discretization
        |Discretization| with |Operators| given by `operator_names` replaced by
        |EmpiricalInterpolatedOperators|.
    data
        Dict containing the following fields:

            :dofs:   |NumPy array| of the DOFs at which the |Operators| have to be evaluated.
            :basis:  |VectorArray| containing the generated collateral basis.
            :errors: sequence of maximum approximation errors during greedy search.
    """

    sample = tuple(parameter_sample)
    operators = [discretization.operators[operator_name] for operator_name in operator_names]

    evaluations = operators[0].range.empty()
    for mu in sample:
        U = discretization.solve(mu)
        for op in operators:
            evaluations.append(op.apply(U, mu=mu))

    dofs, basis, data = ei_greedy(evaluations, error_norm, target_error, max_interpolation_dofs,
                                  projection=projection, product=product)

    ei_operators = {name: EmpiricalInterpolatedOperator(operator, dofs, basis, triangular=True)
                    for name, operator in zip(operator_names, operators)}
    operators_dict = discretization.operators.copy()
    operators_dict.update(ei_operators)
    ei_discretization = discretization.with_(operators=operators_dict, name='{}_ei'.format(discretization.name))

    data.update({'dofs': dofs, 'basis': basis})
    return ei_discretization, data
<|endoftext|>


<commit_before># -*- coding:utf-8 -*-

from ritsu_utils import *
from ritsu_api import *

import requests
import re
import logging as log

log.basicConfig(format="%(levelname)s: %(message)s", level=log.ERROR)

def get_search_page(query, engine):
    if not isinstance(query, str):
        raise TypeError('"query" must be "str", not "{}"'.format(
            query.__class__.__name__
        ))
    if len(query) is 0:
        raise ValueError('"query" length must be greater than 0"')

    if engine is 'google':
        url = requests.utils.requote_uri("https://www.google.com/search?q=" +
                                         query +
                                         "&ie=utf-8&oe=utf-8")
    elif engine is 'sputnik':
         url = requests.utils.requote_uri("http://www.sputnik.ru/search?q=" + query)
    else:
        raise ValueError('"engine" must be either "google" or "sputnik"')
    headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:46.0) Gecko/20100101 Firefox/46.0'
    }
    try:
        response = requests.get(url, headers=headers)
    except Exception as e:
        log.error('An error has occured while page loading: {}'.format(e))
        return None
    return response

def get_links(query, engine):
    try:
        page = get_search_page(query, engine)
    except Exception as e:
        log.error('Query error: {}'.format(e))
        page =  None
    if page is None: return None

    result = []
    if engine is 'google':
        link_regex_str = r'<a href="(.*?)"'
        links = [link.split(' ')[0] for link in re.findall(link_regex_str, page.content)
                 if link.startswith('http')]
        log.debug(links)
        for link in links:
            googlestuff = re.compile(r'.*[.]*google[.]+')
            if not re.match(googlestuff, link) and link not in result:
                result.append(link)
    elif engine is 'sputnik':
        link_regex_str = r'
.*<a href=\"(.*?)\".*<\/div>'
        result = re.findall(link_regex_str, page.content)
    else:
        raise ValueError('"engine" must be either "google" or "sputnik"')
    return result

def get_search_function(engine):
    def command_search(bot, room, nick, access_level, parameters, message):
        if nick != bot.gss['nick']:
            if not parameters:
                return 'Query expected.'
        else:
            if parameters == bot.gss['query'] or not parameters:
                if bot.gss['current'] < len(bot.gss['links']) - 1:
                    bot.gss['current'] = bot.gss['current'] + 1
                    return bot.gss['links'][bot.gss['current']]
                else:
                    return "End of search results. Please refine your search query."
        links = get_link(parameters, engine)
        if links is None:
            return 'Search error! Please contact your system administrator.'
        if links is []:
            return 'Nothing is found!'
        bot.gss['links'] = links
        bot.gss['query'] = parameters
        bot.gss['current'] = 0
        bot.gss['nick'] = nick

        bot.prevAction = 'search'

        return bot.gss['links'][bot.gss['current']]
    return command_search

def load(bot):
  bot.gss = {  # Google Search State
      'query': None,
      'links': None,
      'current': None,
      'nick': None
  }
  bot.add_command('google', get_search_function('google'), LEVEL_GUEST, 'google')
  bot.add_command('g', get_search_function('google'), LEVEL_GUEST, 'google')
  bot.add_command(u'п', get_search_function('sputnik'), LEVEL_GUEST, 'rkn')

def unload(bot):
  pass

def info(bot):
  return 'Search plugin v2.0.0-alpha'
<commit_msg>Fixed bot behavior when the usage of search engines are mixed.<commit_after># -*- coding:utf-8 -*-

from ritsu_utils import *
from ritsu_api import *

import requests
import re
import logging as log

log.basicConfig(format="%(levelname)s: %(message)s", level=log.ERROR)

def get_search_page(query, engine):
    if not isinstance(query, str):
        raise TypeError('"query" must be "str", not "{}"'.format(
            query.__class__.__name__
        ))
    if len(query) is 0:
        raise ValueError('"query" length must be greater than 0"')

    if engine is 'google':
        url = requests.utils.requote_uri("https://www.google.com/search?q=" +
                                         query +
                                         "&ie=utf-8&oe=utf-8")
    elif engine is 'sputnik':
         url = requests.utils.requote_uri("http://www.sputnik.ru/search?q=" + query)
    else:
        raise ValueError('"engine" must be either "google" or "sputnik"')
    headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:46.0) Gecko/20100101 Firefox/46.0'
    }
    try:
        response = requests.get(url, headers=headers)
    except Exception as e:
        log.error('An error has occured while page loading: {}'.format(e))
        return None
    return response

def get_links(query, engine):
    try:
        page = get_search_page(query, engine)
    except Exception as e:
        log.error('Query error: {}'.format(e))
        page =  None
    if page is None: return None

    result = []
    if engine is 'google':
        link_regex_str = r'<a href="(.*?)"'
        links = [link.split(' ')[0] for link in re.findall(link_regex_str, page.content)
                 if link.startswith('http')]
        log.debug(links)
        for link in links:
            googlestuff = re.compile(r'.*[.]*google[.]+')
            if not re.match(googlestuff, link) and link not in result:
                result.append(link)
    elif engine is 'sputnik':
        link_regex_str = r'
.*<a href=\"(.*?)\".*<\/div>'
        result = re.findall(link_regex_str, page.content)
    else:
        raise ValueError('"engine" must be either "google" or "sputnik"')
    return result

def get_search_function(engine):
    def command_search(bot, room, nick, access_level, parameters, message):
        if nick != bot.ss[engine]['nick']:
            if not parameters:
                return 'Query expected.'
        else:
            if parameters == bot.ss[engine]['query'] or not parameters:
                if bot.ss[engine]['current'] < len(bot.ss[engine]['links']) - 1:
                    bot.ss[engine]['current'] = bot.ss[engine]['current'] + 1
                    return bot.ss[engine]['links'][bot.ss[engine]['current']]
                else:
                    return "End of search results. Please refine your search query."
        links = get_link(parameters, engine)
        if links is None:
            return 'Search error! Please contact your system administrator.'
        if links is []:
            return 'Nothing is found!'
        bot.ss[engine]['links'] = links
        bot.ss[engine]['query'] = parameters
        bot.ss[engine]['current'] = 0
        bot.ss[engine]['nick'] = nick

        bot.prevAction = 'search'

        return bot.ss[engine]['links'][bot.ss[engine]['current']]
    return command_search

def load(bot):
    bot.ss = {  # Search State
        'google': {
            'query': None,
            'links': None,
            'current': None,
            'nick': None
        },
        'sputnik': {
            'query': None,
            'links': None,
            'current': None,
            'nick': None
        }
    }
    bot.add_command('google', get_search_function('google'), LEVEL_GUEST, 'google')
    bot.add_command('g', get_search_function('google'), LEVEL_GUEST, 'google')
    bot.add_command(u'п', get_search_function('sputnik'), LEVEL_GUEST, 'rkn')

def unload(bot):
    pass

def info(bot):
    return 'Search plugin v2.0.1'
<|endoftext|>


<commit_before># =============================================================================
# COPYRIGHT 2013 Brain Corporation.
# License under MIT license (see LICENSE file)
# =============================================================================

import subprocess
import tempfile
import shutil
import os

class GitAccessor(object):
    def access(self, repo_link, tag, path_to_file):
        '''
        Checksout a single file from git repo specified
        by 'repo_link' from branch 'tag' under path 'path_to_file'.
        @return: file content
        '''
        tmp_dir = tempfile.mkdtemp()
        if tag is not None:
            subprocess.call(['git', 'clone', '-b', tag, repo_link, tmp_dir])
        else:
            subprocess.call(['git', 'clone', repo_link, tmp_dir])
        with open(os.path.join(tmp_dir, path_to_file)) as file:
            lines = file.readlines()
        shutil.rmtree(tmp_dir)
        return lines
<commit_msg>shallow git clone<commit_after># =============================================================================
# COPYRIGHT 2013 Brain Corporation.
# License under MIT license (see LICENSE file)
# =============================================================================

import subprocess
import tempfile
import shutil
import os

class GitAccessor(object):
    def access(self, repo_link, tag, path_to_file):
        '''
        Checksout a single file from git repo specified
        by 'repo_link' from branch 'tag' under path 'path_to_file'.
        @return: file content
        '''
        tmp_dir = tempfile.mkdtemp()
        if tag is not None:
            subprocess.call(['git', 'clone', '--depth', '1', '-b', tag, repo_link, tmp_dir])
        else:
            subprocess.call(['git', 'clone', '--depth', '1', repo_link, tmp_dir])
        with open(os.path.join(tmp_dir, path_to_file)) as file:
            lines = file.readlines()
        shutil.rmtree(tmp_dir)
        return lines
<|endoftext|>


<commit_before>import numpy as np
import h5py
import scipy.linalg as lag
from matplotlib import pyplot as plt

def pot(x):
    return -1/np.sqrt(x**2 + 1)

file = h5py.File("../../build/copytest.h5")

x = np.array(file["/dset0real"])

file.close()

file = h5py.File("../../build/res.h5")
params = np.array(file["/params"])
file.close()

y = np.zeros(x.size)

z = x + 1j*y

print(params)

x,h = np.linspace( params[1], params[0], params[4], retstep=True)
t,tau = np.linspace( params[2], params[3], params[5], retstep=True)
k1 = 1j*-tau/(4*h**2)*np.ones(x.size-1)

H1 = np.diag(1 + 1j * tau/2*( z/h**2 + pot(x)))
H2 = np.diag(-k1,k=-1)
H3 = np.diag(-k1, k=1)
HL = H1 + H2 + H3

<commit_msg>CN Working in python<commit_after>import numpy as np
import h5py
import scipy.linalg as lag
from matplotlib import pyplot as plt

def pot(x):
    return x**2/2

# file = h5py.File("../../build/copytest.h5")

# x1 = np.array(file["/dset0real"])

# file.close()

# file = h5py.File("../../build/res.h5")
# params = np.array(file["/params"])
x, h = np.linspace(-6, 6, 1e3, retstep=True)

# file.close()

x1 = 2*x*1/np.pi**(0.25)*1/np.sqrt(2)*np.exp(-x**2/2)
y = np.zeros(x1.size)

z = x1 + 1j * y

# print(params)

t,tau = np.linspace( 0, 10, 1e4, retstep=True)
k1 = 1j*-tau/(4*h**2)*np.ones(x1.size - 1)

H1 = np.diag(1 + 1j * tau / 2 * (1 / h**2 + pot(x)))
H2 = np.diag(k1,k=-1)
H3 = np.diag(k1, k=1)
HL = H1 + H2 + H3

H1 = np.diag(1 - 1j * tau / 2 * (1 / h**2 + pot(x)))
H2 = np.diag(-k1,k=-1)
H3 = np.diag(-k1, k=1)
HR = H1 + H2 + H3

numt = 30

def U(En, nt, tau):

    return np.exp(-1j * En * numt * tau)

psi_t = np.dot(HR, z)
fig = plt.figure()
ax = fig.add_subplot(111)
p = ax.plot(x,psi_t.imag)
an = z
for i in range(0, numt):

    print(U(1.5, i+1, tau))
    psi_t = lag.solve(HL, psi_t)
    plt.setp(p[0],ydata=psi_t.imag)
    ax.relim()
    ax.autoscale_view(True, True, True)
    plt.pause(0.05)
    psi_t = np.dot(HR, psi_t)

an= U(1.5, numt, tau) * z
plt.plot(x,an.imag)
plt.draw()
plt.show()<|endoftext|>