<commit_before>from datetime import datetime

from jfr_playoff.template import PlayoffTemplate
from jfr_playoff.data import PlayoffData
from jfr_playoff.logger import PlayoffLogger

class PlayoffGenerator(object):
    def __init__(self, settings):
        self.data = PlayoffData(settings)
        self.page = settings.get('page')
        PlayoffLogger.get('generator').info(
            'page settings: %s', self.page)
        self.canvas = {}
        if settings.has_section('canvas'):
            self.canvas = settings.get('canvas')
        PlayoffLogger.get('generator').info(
            'canvas settings: %s', self.canvas)
        self.leaderboard_classes = {}
        if settings.has_section('position_styles'):
            self.leaderboard_classes = settings.get('position_styles')
        PlayoffLogger.get('generator').info(
            'leaderboard classes settings: %s', self.leaderboard_classes)
        self.p_temp = PlayoffTemplate(
            settings.get('i18n') if settings.has_section('i18n') else {})

    def generate_content(self):
        match_grid = self.get_match_grid(
            self.data.get_dimensions(),
            self.data.generate_phases(),
            self.data.fill_match_info())
        leaderboard_table = self.get_leaderboard_table()
        return self.p_temp.get(
            'PAGE',
            self.p_temp.get(
                'PAGE_HEAD',
                self.p_temp.get(
                    'PAGE_HEAD_REFRESH',
                    self.page['refresh']) \
                if self.page['refresh'] > 0 else '',
                self.page['title']),
            self.p_temp.get(
                'PAGE_BODY',
                self.page['logoh'],
                match_grid,
                self.get_swiss_links(),
                leaderboard_table,
                self.get_leaderboard_caption_table() if leaderboard_table else '',
                self.p_temp.get(
                    'PAGE_BODY_FOOTER',
                    datetime.now().strftime('%Y-%m-%d o %H:%M:%S'))))

    def get_match_table(self, match):
        rows = ''
        for team in match.teams:
            score_html = self.p_temp.get('MATCH_SCORE', team.score)
            team_label = ' / '.join([
                self.data.get_shortname(name) for name in
                team.name.split('
')])
            team_html = self.p_temp.get(
                'MATCH_TEAM_LINK',
                match.link, team.name, team_label) \
                if match.link is not None \
                   else self.p_temp.get(
                           'MATCH_TEAM_NON_LINK',
                           team.name, team_label)
            rows += self.p_temp.get(
                'MATCH_TEAM_ROW',
                ' '.join([
                    'winner' if team.name == match.winner else '',
                    'loser' if team.name == match.loser else ''
                ]).strip(),
                team_html,
                self.p_temp.get(
                    'MATCH_LINK',
                    match.link, score_html) \
                if match.link is not None else score_html)
        html = self.p_temp.get(
            'MATCH_TABLE',
            int(self.page['width'] * 0.7),
            int(self.page['width'] * 0.2),
            rows)
        if match.running > 0:
            running_html = self.p_temp.get('MATCH_RUNNING', match.running)
            html += self.p_temp.get('MATCH_LINK', match.link, running_html) if match.link is not None else running_html
        PlayoffLogger.get('generator').info(
            'match table for #%d generated: %d bytes', match.id, len(html))
        return html

    def get_phase_header(self, phase, position):
        grid_header = self.p_temp.get(
            'MATCH_GRID_PHASE_RUNNING' if phase.running \
            else 'MATCH_GRID_PHASE',
            phase.title)
        if phase.link is not None:
            return self.p_temp.get(
                'MATCH_GRID_PHASE_LINK',
                phase.link,
                self.page['width'], position,
                grid_header)
        else:
            return self.p_temp.get(
                'MATCH_GRID_PHASE_NON_LINK',
                self.page['width'], position,
                grid_header)

    def get_match_box(self, match, position):
        if match is not None:
            winner_link = [
                str(m) for m in match.winner_matches
            ] if match.winner_matches is not None else []
            loser_link = [
                str(m) for m in match.loser_matches
            ] if match.loser_matches is not None else []
            place_loser_link = []
            place_winner_link = []
            if 'starting_position_indicators' in self.page \
               and self.page['starting_position_indicators']:
                for team in match.teams:
                    if len(team.place) > 0:
                        place_link = ['place-' + str(pl) for pl in team.place]
                        if len(team.place) > 1:
                            place_loser_link += place_link
                        else:
                            place_winner_link += place_link
            return self.p_temp.get(
                'MATCH_BOX',
                position[0], position[1],
                match.id,
                ' '.join(winner_link),
                ' '.join(loser_link),
                ' '.join(place_winner_link),
                ' '.join(place_loser_link),
                self.get_match_table(match))
        return ''

    def get_starting_position_box(self, positions, dimensions):
        if 'starting_position_indicators' not in self.page \
           or not self.page['starting_position_indicators']:
            return ''
        boxes = ''
        order = 0
        for place in sorted(positions):
            boxes += self.p_temp.get(
                'STARTING_POSITION_BOX',
                0,
                self.page['margin'] / 2 + int(float(order) / float(len(positions)) * dimensions[1]),
                place,
                self.p_temp.get('POSITION_BOX', place))
            order += 1
        return boxes

    def get_match_grid(self, dimensions, grid, matches):
        canvas_size = [
            dimensions[0] * (
                self.page['width'] + self.page['margin']
            ),
            dimensions[1] * (
                self.page['height'] + self.page['margin']
            ) - self.page['margin']]
        if 'starting_position_indicators' not in self.page \
           or not self.page['starting_position_indicators']:
            canvas_size[0] -= self.page['margin']
        PlayoffLogger.get('generator').info(
            'canvas size: %s', canvas_size)
        grid_boxes = ''
        col_no = 0
        starting_positions = set()
        for phase in grid:
            grid_x = col_no * self.page['width'] + (col_no + 1) * self.page['margin'] \
                     if self.page['starting_position_indicators'] \
                        else col_no * (self.page['width'] + self.page['margin'])
            grid_boxes += self.get_phase_header(phase, grid_x)
            match_height = canvas_size[1] / len(phase.matches)
            row_no = 0
            for match in phase.matches:
                grid_y = self.page['margin'] / 2 if dimensions[1] == 1 else \
                         int(row_no * match_height +
                             0.5 * (match_height - self.page['height']))
                PlayoffLogger.get('generator').info(
                    'grid box (%d, %d) position: (%d, %d)',
                    col_no, row_no, grid_x, grid_y)
                grid_boxes += self.get_match_box(
                    matches[match] if match is not None else None,
                    (grid_x, grid_y))
                if match is not None:
                    for team in matches[match].teams:
                        starting_positions.update(team.place)
                row_no += 1
            col_no += 1
        starting_positions_boxes = self.get_starting_position_box(
            starting_positions, canvas_size)
        return self.p_temp.get(
            'MATCH_GRID',
            canvas_size[0], canvas_size[1],
            canvas_size[0], canvas_size[1],
            ' '.join(['data-%s="%s"' % (
                setting.replace('_', '-'), str(value)
            ) for setting, value in self.canvas.iteritems()]),
            starting_positions_boxes,
            grid_boxes
        )

    def get_leaderboard_row_class(self, position):
        classes = []
        for style in self.leaderboard_classes:
            if position in style['positions']:
                classes.append(style['class'])
        return ' '.join(classes)

    def get_leaderboard_caption_table(self):
        rows = ''
        for style in self.leaderboard_classes:
            if 'caption' in style:
                rows += self.p_temp.get(
                    'LEADERBOARD_CAPTION_TABLE_ROW',
                    style['class'], style['caption'])
        return self.p_temp.get('LEADERBOARD_CAPTION_TABLE', rows) if rows else ''

    def get_leaderboard_table(self):
        leaderboard = self.data.fill_leaderboard()
        if len([t for t in leaderboard if t is not None]) == 0:
            return ''
        position = 1
        rows = ''
        for team in leaderboard:
            rows += self.p_temp.get(
                'LEADERBOARD_ROW',
                self.get_leaderboard_row_class(position),
                position, self.get_flag(team), team or '')
            position += 1
        html = self.p_temp.get('LEADERBOARD', rows)
        PlayoffLogger.get('generator').info(
            'leaderboard HTML generated: %d bytes', len(html))
        return html

    def get_swiss_links(self):
        info = []
        for event in self.data.get_swiss_info():
            event_label = self.p_temp.get('SWISS_DEFAULT_LABEL', event['position'])
            if 'label' in event and event['label'] is not None:
                event_label = event['label']
            info.append((self.p_temp.get('SWISS_LINK') \
                         if event['finished'] \
                         else self.p_temp.get(
                                 'SWISS_RUNNING_LINK',
                                 event['link'], event_label)))
        html = '\n'.join(info)
        PlayoffLogger.get('generator').info(
            'swiss HTML generated: %d bytes', len(html))
        return html

    def get_flag(self, team):
        flag = self.data.get_team_image(team)
        return '' if flag is None else self.p_temp.get('LEADERBOARD_ROW_FLAG', flag)
<commit_msg>Expanding canvas if finishing positions are rendered<commit_after>from datetime import datetime

from jfr_playoff.template import PlayoffTemplate
from jfr_playoff.data import PlayoffData
from jfr_playoff.logger import PlayoffLogger

class PlayoffGenerator(object):
    def __init__(self, settings):
        self.data = PlayoffData(settings)
        self.page = settings.get('page')
        PlayoffLogger.get('generator').info(
            'page settings: %s', self.page)
        self.canvas = {}
        if settings.has_section('canvas'):
            self.canvas = settings.get('canvas')
        PlayoffLogger.get('generator').info(
            'canvas settings: %s', self.canvas)
        self.leaderboard_classes = {}
        if settings.has_section('position_styles'):
            self.leaderboard_classes = settings.get('position_styles')
        PlayoffLogger.get('generator').info(
            'leaderboard classes settings: %s', self.leaderboard_classes)
        self.p_temp = PlayoffTemplate(
            settings.get('i18n') if settings.has_section('i18n') else {})

    def generate_content(self):
        match_grid = self.get_match_grid(
            self.data.get_dimensions(),
            self.data.generate_phases(),
            self.data.fill_match_info())
        leaderboard_table = self.get_leaderboard_table()
        return self.p_temp.get(
            'PAGE',
            self.p_temp.get(
                'PAGE_HEAD',
                self.p_temp.get(
                    'PAGE_HEAD_REFRESH',
                    self.page['refresh']) \
                if self.page['refresh'] > 0 else '',
                self.page['title']),
            self.p_temp.get(
                'PAGE_BODY',
                self.page['logoh'],
                match_grid,
                self.get_swiss_links(),
                leaderboard_table,
                self.get_leaderboard_caption_table() if leaderboard_table else '',
                self.p_temp.get(
                    'PAGE_BODY_FOOTER',
                    datetime.now().strftime('%Y-%m-%d o %H:%M:%S'))))

    def get_match_table(self, match):
        rows = ''
        for team in match.teams:
            score_html = self.p_temp.get('MATCH_SCORE', team.score)
            team_label = ' / '.join([
                self.data.get_shortname(name) for name in
                team.name.split('
')])
            team_html = self.p_temp.get(
                'MATCH_TEAM_LINK',
                match.link, team.name, team_label) \
                if match.link is not None \
                   else self.p_temp.get(
                           'MATCH_TEAM_NON_LINK',
                           team.name, team_label)
            rows += self.p_temp.get(
                'MATCH_TEAM_ROW',
                ' '.join([
                    'winner' if team.name == match.winner else '',
                    'loser' if team.name == match.loser else ''
                ]).strip(),
                team_html,
                self.p_temp.get(
                    'MATCH_LINK',
                    match.link, score_html) \
                if match.link is not None else score_html)
        html = self.p_temp.get(
            'MATCH_TABLE',
            int(self.page['width'] * 0.7),
            int(self.page['width'] * 0.2),
            rows)
        if match.running > 0:
            running_html = self.p_temp.get('MATCH_RUNNING', match.running)
            html += self.p_temp.get('MATCH_LINK', match.link, running_html) if match.link is not None else running_html
        PlayoffLogger.get('generator').info(
            'match table for #%d generated: %d bytes', match.id, len(html))
        return html

    def get_phase_header(self, phase, position):
        grid_header = self.p_temp.get(
            'MATCH_GRID_PHASE_RUNNING' if phase.running \
            else 'MATCH_GRID_PHASE',
            phase.title)
        if phase.link is not None:
            return self.p_temp.get(
                'MATCH_GRID_PHASE_LINK',
                phase.link,
                self.page['width'], position,
                grid_header)
        else:
            return self.p_temp.get(
                'MATCH_GRID_PHASE_NON_LINK',
                self.page['width'], position,
                grid_header)

    def get_match_box(self, match, position):
        if match is not None:
            winner_link = [
                str(m) for m in match.winner_matches
            ] if match.winner_matches is not None else []
            loser_link = [
                str(m) for m in match.loser_matches
            ] if match.loser_matches is not None else []
            place_loser_link = []
            place_winner_link = []
            if 'starting_position_indicators' in self.page \
               and self.page['starting_position_indicators']:
                for team in match.teams:
                    if len(team.place) > 0:
                        place_link = ['place-' + str(pl) for pl in team.place]
                        if len(team.place) > 1:
                            place_loser_link += place_link
                        else:
                            place_winner_link += place_link
            return self.p_temp.get(
                'MATCH_BOX',
                position[0], position[1],
                match.id,
                ' '.join(winner_link),
                ' '.join(loser_link),
                ' '.join(place_winner_link),
                ' '.join(place_loser_link),
                self.get_match_table(match))
        return ''

    def get_starting_position_box(self, positions, dimensions):
        if 'starting_position_indicators' not in self.page \
           or not self.page['starting_position_indicators']:
            return ''
        boxes = ''
        order = 0
        for place in sorted(positions):
            boxes += self.p_temp.get(
                'STARTING_POSITION_BOX',
                0,
                self.page['margin'] / 2 + int(float(order) / float(len(positions)) * dimensions[1]),
                place,
                self.p_temp.get('POSITION_BOX', place))
            order += 1
        return boxes

    def get_match_grid(self, dimensions, grid, matches):
        canvas_size = [
            dimensions[0] * (
                self.page['width'] + self.page['margin']
            ) + self.page['margin'],
            dimensions[1] * (
                self.page['height'] + self.page['margin']
            ) - self.page['margin']]
        if 'starting_position_indicators' not in self.page \
           or not self.page['starting_position_indicators']:
            canvas_size[0] -= self.page['margin']
        if 'finishing_position_indicators' not in self.page \
           or not self.page['finishing_position_indicators']:
            canvas_size[0] -= self.page['margin']
        PlayoffLogger.get('generator').info(
            'canvas size: %s', canvas_size)
        grid_boxes = ''
        col_no = 0
        starting_positions = set()
        for phase in grid:
            grid_x = col_no * self.page['width'] + (col_no + 1) * self.page['margin'] \
                     if self.page['starting_position_indicators'] \
                        else col_no * (self.page['width'] + self.page['margin'])
            grid_boxes += self.get_phase_header(phase, grid_x)
            match_height = canvas_size[1] / len(phase.matches)
            row_no = 0
            for match in phase.matches:
                grid_y = self.page['margin'] / 2 if dimensions[1] == 1 else \
                         int(row_no * match_height +
                             0.5 * (match_height - self.page['height']))
                PlayoffLogger.get('generator').info(
                    'grid box (%d, %d) position: (%d, %d)',
                    col_no, row_no, grid_x, grid_y)
                grid_boxes += self.get_match_box(
                    matches[match] if match is not None else None,
                    (grid_x, grid_y))
                if match is not None:
                    for team in matches[match].teams:
                        starting_positions.update(team.place)
                row_no += 1
            col_no += 1
        starting_positions_boxes = self.get_starting_position_box(
            starting_positions, canvas_size)
        return self.p_temp.get(
            'MATCH_GRID',
            canvas_size[0], canvas_size[1],
            canvas_size[0], canvas_size[1],
            ' '.join(['data-%s="%s"' % (
                setting.replace('_', '-'), str(value)
            ) for setting, value in self.canvas.iteritems()]),
            starting_positions_boxes,
            grid_boxes
        )

    def get_leaderboard_row_class(self, position):
        classes = []
        for style in self.leaderboard_classes:
            if position in style['positions']:
                classes.append(style['class'])
        return ' '.join(classes)

    def get_leaderboard_caption_table(self):
        rows = ''
        for style in self.leaderboard_classes:
            if 'caption' in style:
                rows += self.p_temp.get(
                    'LEADERBOARD_CAPTION_TABLE_ROW',
                    style['class'], style['caption'])
        return self.p_temp.get('LEADERBOARD_CAPTION_TABLE', rows) if rows else ''

    def get_leaderboard_table(self):
        leaderboard = self.data.fill_leaderboard()
        if len([t for t in leaderboard if t is not None]) == 0:
            return ''
        position = 1
        rows = ''
        for team in leaderboard:
            rows += self.p_temp.get(
                'LEADERBOARD_ROW',
                self.get_leaderboard_row_class(position),
                position, self.get_flag(team), team or '')
            position += 1
        html = self.p_temp.get('LEADERBOARD', rows)
        PlayoffLogger.get('generator').info(
            'leaderboard HTML generated: %d bytes', len(html))
        return html

    def get_swiss_links(self):
        info = []
        for event in self.data.get_swiss_info():
            event_label = self.p_temp.get('SWISS_DEFAULT_LABEL', event['position'])
            if 'label' in event and event['label'] is not None:
                event_label = event['label']
            info.append((self.p_temp.get('SWISS_LINK') \
                         if event['finished'] \
                         else self.p_temp.get(
                                 'SWISS_RUNNING_LINK',
                                 event['link'], event_label)))
        html = '\n'.join(info)
        PlayoffLogger.get('generator').info(
            'swiss HTML generated: %d bytes', len(html))
        return html

    def get_flag(self, team):
        flag = self.data.get_team_image(team)
        return '' if flag is None else self.p_temp.get('LEADERBOARD_ROW_FLAG', flag)
<|endoftext|>


<commit_before>import email.header
import logging

import six

from django.conf import settings

logger = logging.getLogger(__name__)

DEFAULT_CHARSET = getattr(
    settings,
    'DJANGO_MAILBOX_DEFAULT_CHARSET',
    'iso8859-1',
)

def convert_header_to_unicode(header):
    def _decode(value, encoding):
        if isinstance(value, six.text_type):
            return value
        if not encoding or encoding == 'unknown-8bit':
            encoding = DEFAULT_CHARSET
        return value.decode(encoding, 'REPLACE')

    try:
        return ''.join(
            [
                (
                    _decode(bytestr, encoding)
                ) for bytestr, encoding in email.header.decode_header(header)
            ]
        )
    except UnicodeDecodeError:
        logger.exception(
            'Errors encountered decoding header %s into encoding %s.',
            header,
            DEFAULT_CHARSET,
        )
        return unicode(header, DEFAULT_CHARSET, 'replace')

def get_body_from_message(message, maintype, subtype):
    """
    Fetchs the body message matching main/sub content type.
    """
    body = six.text_type('')
    for part in message.walk():
        if part.get_content_maintype() == maintype and \
                part.get_content_subtype() == subtype:
            charset = part.get_content_charset()
            this_part = part.get_payload(decode=True)
            if charset:
                try:
                    this_part = this_part.decode(charset, 'replace')
                except LookupError:
                    this_part = this_part.decode('ascii', 'replace')
                    logger.warning(
                        'Unknown encoding %s encountered while decoding '
                        'text payload.  Interpreting as ASCII with '
                        'replacement, but some data may not be '
                        'represented as the sender intended.',
                        charset
                    )
                except ValueError:
                    this_part = this_part.decode('ascii', 'replace')
                    logger.warning(
                        'Error encountered while decoding text '
                        'payload from an incorrectly-constructed '
                        'e-mail; payload was converted to ASCII with '
                        'replacement, but some data may not be '
                        'represented as the sender intended.'
                    )
            else:
                this_part = this_part.decode('ascii', 'replace')

            body += this_part

    return body
<commit_msg>Correcting error function name in header decoding; fixes #55.<commit_after>import email.header
import logging

import six

from django.conf import settings

logger = logging.getLogger(__name__)

DEFAULT_CHARSET = getattr(
    settings,
    'DJANGO_MAILBOX_DEFAULT_CHARSET',
    'iso8859-1',
)

def convert_header_to_unicode(header):
    def _decode(value, encoding):
        if isinstance(value, six.text_type):
            return value
        if not encoding or encoding == 'unknown-8bit':
            encoding = DEFAULT_CHARSET
        return value.decode(encoding, 'replace')

    try:
        return ''.join(
            [
                (
                    _decode(bytestr, encoding)
                ) for bytestr, encoding in email.header.decode_header(header)
            ]
        )
    except UnicodeDecodeError:
        logger.exception(
            'Errors encountered decoding header %s into encoding %s.',
            header,
            DEFAULT_CHARSET,
        )
        return unicode(header, DEFAULT_CHARSET, 'replace')

def get_body_from_message(message, maintype, subtype):
    """
    Fetchs the body message matching main/sub content type.
    """
    body = six.text_type('')
    for part in message.walk():
        if part.get_content_maintype() == maintype and \
                part.get_content_subtype() == subtype:
            charset = part.get_content_charset()
            this_part = part.get_payload(decode=True)
            if charset:
                try:
                    this_part = this_part.decode(charset, 'replace')
                except LookupError:
                    this_part = this_part.decode('ascii', 'replace')
                    logger.warning(
                        'Unknown encoding %s encountered while decoding '
                        'text payload.  Interpreting as ASCII with '
                        'replacement, but some data may not be '
                        'represented as the sender intended.',
                        charset
                    )
                except ValueError:
                    this_part = this_part.decode('ascii', 'replace')
                    logger.warning(
                        'Error encountered while decoding text '
                        'payload from an incorrectly-constructed '
                        'e-mail; payload was converted to ASCII with '
                        'replacement, but some data may not be '
                        'represented as the sender intended.'
                    )
            else:
                this_part = this_part.decode('ascii', 'replace')

            body += this_part

    return body
<|endoftext|>


<commit_before>from sympy.core import Basic
from sympy.functions import ceiling, log

import random

class GrayCode(Basic):
    """
    A Gray code is essentially a Hamiltonian walk on
    a n-dimensional cube with edge length of one.
    The vertices of the cube are represented by vectors
    whose values are binary. The Hamilton walk visits
    each vertex exactly once. The Gray code for a 3d
    cube is [[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,1,1],
    [1,1,1],[1,0,1],[0,0,1]].

    A Gray code solves the problem of sequentially
    generating all possible subsets of n objects in such
    a way that each subset is obtained from the previous
    one by either deleting or adding a single object.
    In the above example, 1 indicates that the object is
    present, and 0 indicates that its absent.

    Gray codes have applications in statistics as well when
    we want to compute various statistics related to subsets
    in an efficient manner.

    References:
    [1] Nijenhuis,A. and Wilf,H.S.(1978).
    Combinatorial Algorithms. Academic Press.
    [2] Knuth, D. (2011). The Art of Computer Programming, Vol 4
    Addison Wesley

    Examples:
    >>> from sympy.combinatorics.graycode import GrayCode
    >>> a = GrayCode(3)
    >>> list(a.generate_bitlist())
    [['0', '0', '0'], ['0', '0', '1'], ['0', '1', '1'], \
    ['0', '1', '0'], ['1', '1', '0'], ['1', '1', '1'], \
    ['1', '0', '1'], ['1', '0', '0']]
    """

    _reset = False
    _current = 0

    def __new__(cls, *args, **kw_args):
        """
        Default constructor
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3)
        >>> a
        GrayCode(3)
        >>> a = GrayCode(3, start = ['1','0','0'])
        >>> a.selections
        8
        """
        obj = Basic.__new__(cls, *args, **kw_args)
        if kw_args.has_key("start"):
            obj._current = kw_args["start"]
        return obj

    @property
    def selections(self):
        """
        Returns the number of bit vectors in the
        Gray code.

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3)
        >>> a.selections
        8
        """
        return 2**self.n

    @property
    def n(self):
        """
        Returns the number of objects.

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(5)
        >>> a.n
        5
        """
        return self.args[0]

    def generate_bitlist(self, start = None):
        """
        Generates the sequence of bit vectors of a Gray Code.

        [1] Knuth, D. (2011). The Art of Computer Programming,
        Vol 4, Addison Wesley

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3)
        >>> list(a.generate_bitlist())
        [['0', '0', '0'], ['0', '0', '1'], ['0', '1', '1'], \
        ['0', '1', '0'], ['1', '1', '0'], ['1', '1', '1'], \
        ['1', '0', '1'], ['1', '0', '0']]
        >>> list(a.generate_bitlist(['0', '1', '1']))
        [['0', '1', '1'], ['0', '1', '0'], ['1', '1', '0'], \
        ['1', '1', '1'], ['1', '0', '1'], ['1', '0', '0']]
        """
        bits = self.args[0]
        if start != None:
            self._current = start
        if isinstance(self._current, list):
            graycode_bin = gray_to_bin(self._current)
            self._current = int(''.join(self._current), 2)
        else:
            graycode_bin = gray_to_bin(list(bin(self._current))[2:])
        graycode_bin = int(''.join(graycode_bin), 2)
        for i in xrange(graycode_bin, 1 << bits):
            if self._reset:
                self._current = 0
                self._reset = False
                break
            retlist = list(bin(self._current))[2:]
            yield ['0'] * (self.n - len(retlist)) + retlist
            bbtc = (i ^ (i + 1))
            gbtc = (bbtc ^ (bbtc >> 1))
            self._current = (self._current ^ gbtc)

    def reset(self):
        """
        Resets the bit generation.

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3)
        >>> for i in a.generate_bitlist():
        ...     if i == ['0', '1', '0']:
        ...         a.reset()
        ...     print i
        ...
        ['0', '0', '0']
        ['0', '0', '1']
        ['0', '1', '1']
        ['0', '1', '0']
        """
        self._reset = True

    @property
    def rank(self):
        """
        Ranks the gray code.

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3, start = ['1','0','0'])
        >>> a.rank
        1
        >>> a = GrayCode(5, start = ['1','0','1','0','0'])
        >>> a.rank
        6
        """
        if isinstance(self._current, int):
            self._current = list(bin(self._current), 2)[2:]
        if len(self._current)==0:
            return 0
        elif self._current[-1]=='0':
            return GrayCode(start = self._current[:-1]).rank
        else:
            return 2**len(self._current) - \
                   GrayCode(start = self._current[:-1]).rank - 1

    @property
    def current(self):
        """
        Returns the currently referenced gray code.
        """
        return self._current

def random_bitlist(n):
    """
    Generates a random bitlist of length n.
    """
    return [random.choice(['0', '1']) for i in xrange(n)]

def gray_to_bin(bin_list):
    """
    Convert from Gray coding to binary coding.

    We assume big endian encoding.

    Examples:
    >>> from sympy.combinatorics.graycode import gray_to_bin
    >>> gray_to_bin(['1','0','0'])
    ['1', '1', '1']
    """
    b = bin_list[0]
    for i in xrange(1, len(bin_list)):
        b += str(int(b[i-1] != bin_list[i]))

    return list(b)

def bin_to_gray(bin_list):
    """
    Convert from binary coding to gray coding.

    We assume big endian encoding.

    Examples:
    >>> from sympy.combinatorics.graycode import bin_to_gray
    >>> bin_to_gray(['1','1','1'])
    ['1', '0', '0']
    """
    b = bin_list[0]
    for i in xrange(0, len(bin_list) - 1):
        b += str(int(bin_list[i]) ^ int(b[i - 1]))

    return list(b)

def get_subset_from_bitlist(super_set, bitlist):
    """
    Gets the subset defined by the bitlist.

    Examples:
    >>> from sympy.combinatorics.graycode import get_subset_from_bitlist
    >>> get_subset_from_bitlist(['a','b','c','d'],['0','0','1','1'])
    ['c', 'd']
    """
    if len(super_set) != len(bitlist):
        raise ValueError("The sizes of the lists are not equal")
    ret_set = super_set[:]
    for i in xrange(len(bitlist)):
        if bitlist[i] == '0':
            ret_set.remove(super_set[i])
    return ret_set

def gray_code_subsets(gray_code_set):
    """
    Generates the subsets as enumerated by a Gray code.

    This does not work with multisets.

    Examples:
    >>> from sympy.combinatorics.graycode import gray_code_subsets
    >>> list(gray_code_subsets(['a','b','c']))
    [[], ['c'], ['b', 'c'], ['b'], ['a', 'b'], ['a', 'b', 'c'], \
    ['a', 'c'], ['a']]
    """
    return [get_subset_from_bitlist(gray_code_set, bitlist) for \
            bitlist in list(GrayCode(len(gray_code_set)).generate_bitlist())]

def unrank_gray_code(k, n):
    """
    Unranks an n-bit sized gray code of rank k.

    We generate in reverse order to allow for tail-call
    optimization.

    Examples:
    >>> from sympy.combinatorics.graycode import unrank_gray_code
    >>> unrank_gray_code(3, 5).current
    ['0', '1', '0', '0', '0']
    >>> unrank_gray_code(7, 10).rank
    7
    """
    def unrank(k, n):
        if n == 1:
            return [str(k % 2)]
        m = 2**(n - 1)
        if k < m:
            return ["0"] + unrank(k, n - 1)
        return ["1"] + unrank(m - (k % m) - 1, n - 1)
    ret_list = unrank(k, n)
    list.reverse(ret_list)
    return GrayCode(start = ret_list)
<commit_msg>combinatorics/graycode: Made rank a cached property.<commit_after>from sympy.core import Basic
from sympy.functions import ceiling, log

import random

class GrayCode(Basic):
    """
    A Gray code is essentially a Hamiltonian walk on
    a n-dimensional cube with edge length of one.
    The vertices of the cube are represented by vectors
    whose values are binary. The Hamilton walk visits
    each vertex exactly once. The Gray code for a 3d
    cube is [[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,1,1],
    [1,1,1],[1,0,1],[0,0,1]].

    A Gray code solves the problem of sequentially
    generating all possible subsets of n objects in such
    a way that each subset is obtained from the previous
    one by either deleting or adding a single object.
    In the above example, 1 indicates that the object is
    present, and 0 indicates that its absent.

    Gray codes have applications in statistics as well when
    we want to compute various statistics related to subsets
    in an efficient manner.

    References:
    [1] Nijenhuis,A. and Wilf,H.S.(1978).
    Combinatorial Algorithms. Academic Press.
    [2] Knuth, D. (2011). The Art of Computer Programming, Vol 4
    Addison Wesley

    Examples:
    >>> from sympy.combinatorics.graycode import GrayCode
    >>> a = GrayCode(3)
    >>> list(a.generate_bitlist())
    [['0', '0', '0'], ['0', '0', '1'], ['0', '1', '1'], \
    ['0', '1', '0'], ['1', '1', '0'], ['1', '1', '1'], \
    ['1', '0', '1'], ['1', '0', '0']]
    """

    _reset = False
    _current = 0
    _rank = None

    def __new__(cls, *args, **kw_args):
        """
        Default constructor
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3)
        >>> a
        GrayCode(3)
        >>> a = GrayCode(3, start = ['1','0','0'])
        >>> a.selections
        8
        """
        obj = Basic.__new__(cls, *args, **kw_args)
        if kw_args.has_key("start"):
            obj._current = kw_args["start"]
        return obj

    @property
    def selections(self):
        """
        Returns the number of bit vectors in the
        Gray code.

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3)
        >>> a.selections
        8
        """
        return 2**self.n

    @property
    def n(self):
        """
        Returns the number of objects.

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(5)
        >>> a.n
        5
        """
        return self.args[0]

    def generate_bitlist(self, start = None):
        """
        Generates the sequence of bit vectors of a Gray Code.

        [1] Knuth, D. (2011). The Art of Computer Programming,
        Vol 4, Addison Wesley

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3)
        >>> list(a.generate_bitlist())
        [['0', '0', '0'], ['0', '0', '1'], ['0', '1', '1'], \
        ['0', '1', '0'], ['1', '1', '0'], ['1', '1', '1'], \
        ['1', '0', '1'], ['1', '0', '0']]
        >>> list(a.generate_bitlist(['0', '1', '1']))
        [['0', '1', '1'], ['0', '1', '0'], ['1', '1', '0'], \
        ['1', '1', '1'], ['1', '0', '1'], ['1', '0', '0']]
        """
        bits = self.args[0]
        if start != None:
            self._current = start
        if isinstance(self._current, list):
            graycode_bin = gray_to_bin(self._current)
            self._current = int(''.join(self._current), 2)
        else:
            graycode_bin = gray_to_bin(list(bin(self._current))[2:])
        graycode_bin = int(''.join(graycode_bin), 2)
        for i in xrange(graycode_bin, 1 << bits):
            if self._reset:
                self._current = 0
                self._reset = False
                break
            retlist = list(bin(self._current))[2:]
            yield ['0'] * (self.n - len(retlist)) + retlist
            bbtc = (i ^ (i + 1))
            gbtc = (bbtc ^ (bbtc >> 1))
            self._current = (self._current ^ gbtc)

    def reset(self):
        """
        Resets the bit generation.

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3)
        >>> for i in a.generate_bitlist():
        ...     if i == ['0', '1', '0']:
        ...         a.reset()
        ...     print i
        ...
        ['0', '0', '0']
        ['0', '0', '1']
        ['0', '1', '1']
        ['0', '1', '0']
        """
        self._reset = True

    @property
    def rank(self):
        """
        Ranks the gray code.

        Examples:
        >>> from sympy.combinatorics.graycode import GrayCode
        >>> a = GrayCode(3, start = ['1','0','0'])
        >>> a.rank
        1
        >>> a = GrayCode(5, start = ['1','0','1','0','0'])
        >>> a.rank
        6
        """
        if self._rank is None:
            if isinstance(self._current, int):
                self._current = list(bin(self._current), 2)[2:]
            if len(self._current)==0:
                return 0
            elif self._current[-1]=='0':
                self._rank = GrayCode(start = self._current[:-1]).rank
            else:
                self._rank =  2**len(self._current) - \
                             GrayCode(start = self._current[:-1]).rank - 1
        return self._rank

    @property
    def current(self):
        """
        Returns the currently referenced gray code.
        """
        return self._current

def random_bitlist(n):
    """
    Generates a random bitlist of length n.
    """
    return [random.choice(['0', '1']) for i in xrange(n)]

def gray_to_bin(bin_list):
    """
    Convert from Gray coding to binary coding.

    We assume big endian encoding.

    Examples:
    >>> from sympy.combinatorics.graycode import gray_to_bin
    >>> gray_to_bin(['1','0','0'])
    ['1', '1', '1']
    """
    b = bin_list[0]
    for i in xrange(1, len(bin_list)):
        b += str(int(b[i-1] != bin_list[i]))

    return list(b)

def bin_to_gray(bin_list):
    """
    Convert from binary coding to gray coding.

    We assume big endian encoding.

    Examples:
    >>> from sympy.combinatorics.graycode import bin_to_gray
    >>> bin_to_gray(['1','1','1'])
    ['1', '0', '0']
    """
    b = bin_list[0]
    for i in xrange(0, len(bin_list) - 1):
        b += str(int(bin_list[i]) ^ int(b[i - 1]))

    return list(b)

def get_subset_from_bitlist(super_set, bitlist):
    """
    Gets the subset defined by the bitlist.

    Examples:
    >>> from sympy.combinatorics.graycode import get_subset_from_bitlist
    >>> get_subset_from_bitlist(['a','b','c','d'],['0','0','1','1'])
    ['c', 'd']
    """
    if len(super_set) != len(bitlist):
        raise ValueError("The sizes of the lists are not equal")
    ret_set = super_set[:]
    for i in xrange(len(bitlist)):
        if bitlist[i] == '0':
            ret_set.remove(super_set[i])
    return ret_set

def gray_code_subsets(gray_code_set):
    """
    Generates the subsets as enumerated by a Gray code.

    This does not work with multisets.

    Examples:
    >>> from sympy.combinatorics.graycode import gray_code_subsets
    >>> list(gray_code_subsets(['a','b','c']))
    [[], ['c'], ['b', 'c'], ['b'], ['a', 'b'], ['a', 'b', 'c'], \
    ['a', 'c'], ['a']]
    """
    return [get_subset_from_bitlist(gray_code_set, bitlist) for \
            bitlist in list(GrayCode(len(gray_code_set)).generate_bitlist())]

def unrank_gray_code(k, n):
    """
    Unranks an n-bit sized gray code of rank k.

    We generate in reverse order to allow for tail-call
    optimization.

    Examples:
    >>> from sympy.combinatorics.graycode import unrank_gray_code
    >>> unrank_gray_code(3, 5).current
    ['0', '1', '0', '0', '0']
    >>> unrank_gray_code(7, 10).rank
    7
    """
    def unrank(k, n):
        if n == 1:
            return [str(k % 2)]
        m = 2**(n - 1)
        if k < m:
            return ["0"] + unrank(k, n - 1)
        return ["1"] + unrank(m - (k % m) - 1, n - 1)
    ret_list = unrank(k, n)
    list.reverse(ret_list)
    return GrayCode(start = ret_list)
<|endoftext|>


<commit_before>import os
import pkg_resources

from mako.template import Template as MakoTemplate
from mako.lookup import TemplateLookup

from django.template import Context, loader
from django.http import HttpResponse

from django.templatetags.static import static

TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')

JS_URLS = [
    # VENDOR
    'discussion/js/vendor/split.js',
    'discussion/js/vendor/i18n.js',
    'discussion/js/vendor/URI.min.js',
    'discussion/js/vendor/jquery.leanModal.min.js',
    'discussion/js/vendor/jquery.timeago.js',
    'discussion/js/vendor/underscore-min.js',
    'discussion/js/vendor/backbone-min.js',
    'discussion/js/vendor/mustache.js',
    'discussion/js/vendor/mathjax-MathJax-c9db6ac/MathJax.js?config=default',
    'discussion/js/vendor/Markdown.Converter.js',
    'discussion/js/vendor/Markdown.Sanitizer.js',
    'discussion/js/vendor/Markdown.Editor.js',
    'discussion/js/vendor/mathjax_delay_renderer.js',
    'discussion/js/vendor/customwmd.js',

    # DISCUSSION
    'discussion/js/tooltip_manager.js',
    'discussion/js/models/discussion_user.js',
    'discussion/js/content.js',
    'discussion/js/discussion.js',
    'discussion/js/main.js',
    'discussion/js/discussion_filter.js',
    'discussion/js/views/discussion_content_view.js',
    'discussion/js/views/response_comment_view.js',
    'discussion/js/views/thread_response_show_view.js',
    'discussion/js/views/discussion_user_profile_view.js',
    'discussion/js/views/new_post_inline_vew.js',
    'discussion/js/views/thread_response_edit_view.js',
    'discussion/js/views/discussion_thread_view.js',
    'discussion/js/views/discussion_thread_view_inline.js',
    'discussion/js/views/thread_response_view.js',
    'discussion/js/views/discussion_thread_list_view.js',
    'discussion/js/views/discussion_thread_show_view.js',
    'discussion/js/views/discussion_thread_edit_view.js',
    'discussion/js/views/response_comment_show_view.js',
    'discussion/js/views/discussion_thread_profile_view.js',
    'discussion/js/views/new_post_view.js',
    'discussion/js/views/response_comment_edit_view.js',
    'discussion/js/discussion_router.js',
    'discussion/js/utils.js',
    'discussion/js/templates.js',
    'discussion/js/discussion_module_view.js'
]

CSS_URLS = [
    'discussion/css/discussion-app.css',
    'discussion/css/vendor/font-awesome.css'
]

def get_js_urls():
    return [static(path) for path in JS_URLS]

def get_css_urls():
    return [static(path) for path in CSS_URLS]

# TODO Remove the all following lines, was used for testing as a standalone app.
STATIC_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
_template_lookup = TemplateLookup(TEMPLATE_DIR)

def index(request):
    t = loader.get_template('index.html')
    c = Context({})
    return HttpResponse(t.render(c))

# dummy, for testing as a standalone app against a mock server API.
def inline_test(request):
    js_test_deps = '<script type="text/javascript" src="{}"></script>'.format(static('js/vendor/jquery.min.js'))

    return HttpResponse(u"""
    <html>
    <head>
        <title>Inline Test</title>
    {js_test_deps}
    {css_links}
    {js_links}
    </head>
    <body>
    {body}
    {js_body}
    </body>
    </html>
    """.format(
        js_test_deps=js_test_deps,
        css_links=get_css_links(),
        js_links=get_js_links(),
        body=get_inline_body(discussion_id="foo"),
        js_body=get_js_body()
        )
    )

def get_inline_body(discussion_id):
    t = _template_lookup.get_template('discussion/_discussion_module.html')
    return t.render_unicode(discussion_id=discussion_id)

def get_css_links():
    return os.linesep.join('<link rel="stylesheet" type="text/css" href="{}"/>'.format(url) for url in self.get_css_urls())

def get_js_filenames():
    return [pkg_resources.resource_filename('discussion', path) for path in VENDOR_JS_URLS]

def get_js_links():
    return os.linesep.join('<script type="text/javascript" src="{}"/>'.format(url) for url in self.get_js_urls())

def get_js_body():
    js_dir = os.path.join(STATIC_DIR, 'discussion/js')
    js_urls = []
    for root, dirs, filenames in os.walk(js_dir):
        prefix = root[len(STATIC_DIR+1):]
        for filename in filenames:
            if filename.endswith('.js'):
                path = os.path.join(prefix, filename)
                print [root, prefix, filename, path]
                js_urls.append(static(path))
    tags = os.linesep.join('<script type="text/javascript" src="{}"></script>'.format(url) for url in js_urls)
    # moved to discussion_forum... will be deleted anyway
    return tags + os.linesep + render_mustache_templates()
<commit_msg>Add get_template_dir to serve template in the LMS<commit_after>import os
import pkg_resources

from mako.template import Template as MakoTemplate
from mako.lookup import TemplateLookup

from django.template import Context, loader
from django.http import HttpResponse

from django.templatetags.static import static

TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')

JS_URLS = [
    # VENDOR
    'discussion/js/vendor/split.js',
    'discussion/js/vendor/i18n.js',
    'discussion/js/vendor/URI.min.js',
    'discussion/js/vendor/jquery.leanModal.min.js',
    'discussion/js/vendor/jquery.timeago.js',
    'discussion/js/vendor/underscore-min.js',
    'discussion/js/vendor/backbone-min.js',
    'discussion/js/vendor/mustache.js',
    'discussion/js/vendor/mathjax-MathJax-c9db6ac/MathJax.js?config=default',
    'discussion/js/vendor/Markdown.Converter.js',
    'discussion/js/vendor/Markdown.Sanitizer.js',
    'discussion/js/vendor/Markdown.Editor.js',
    'discussion/js/vendor/mathjax_delay_renderer.js',
    'discussion/js/vendor/customwmd.js',

    # DISCUSSION
    'discussion/js/tooltip_manager.js',
    'discussion/js/models/discussion_user.js',
    'discussion/js/content.js',
    'discussion/js/discussion.js',
    'discussion/js/main.js',
    'discussion/js/discussion_filter.js',
    'discussion/js/views/discussion_content_view.js',
    'discussion/js/views/response_comment_view.js',
    'discussion/js/views/thread_response_show_view.js',
    'discussion/js/views/discussion_user_profile_view.js',
    'discussion/js/views/new_post_inline_vew.js',
    'discussion/js/views/thread_response_edit_view.js',
    'discussion/js/views/discussion_thread_view.js',
    'discussion/js/views/discussion_thread_view_inline.js',
    'discussion/js/views/thread_response_view.js',
    'discussion/js/views/discussion_thread_list_view.js',
    'discussion/js/views/discussion_thread_show_view.js',
    'discussion/js/views/discussion_thread_edit_view.js',
    'discussion/js/views/response_comment_show_view.js',
    'discussion/js/views/discussion_thread_profile_view.js',
    'discussion/js/views/new_post_view.js',
    'discussion/js/views/response_comment_edit_view.js',
    'discussion/js/discussion_router.js',
    'discussion/js/utils.js',
    'discussion/js/templates.js',
    'discussion/js/discussion_module_view.js'
]

CSS_URLS = [
    'discussion/css/discussion-app.css',
    'discussion/css/vendor/font-awesome.css'
]

def get_template_dir():
    return TEMPLATE_DIR

def get_js_urls():
    return [static(path) for path in JS_URLS]

def get_css_urls():
    return [static(path) for path in CSS_URLS]

# TODO Remove the all following lines, was used for testing as a standalone app.
STATIC_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
_template_lookup = TemplateLookup(TEMPLATE_DIR)

def index(request):
    t = loader.get_template('index.html')
    c = Context({})
    return HttpResponse(t.render(c))

# dummy, for testing as a standalone app against a mock server API.
def inline_test(request):
    js_test_deps = '<script type="text/javascript" src="{}"></script>'.format(static('js/vendor/jquery.min.js'))

    return HttpResponse(u"""
    <html>
    <head>
        <title>Inline Test</title>
    {js_test_deps}
    {css_links}
    {js_links}
    </head>
    <body>
    {body}
    {js_body}
    </body>
    </html>
    """.format(
        js_test_deps=js_test_deps,
        css_links=get_css_links(),
        js_links=get_js_links(),
        body=get_inline_body(discussion_id="foo"),
        js_body=get_js_body()
        )
    )

def get_inline_body(discussion_id):
    t = _template_lookup.get_template('discussion/_discussion_module.html')
    return t.render_unicode(discussion_id=discussion_id)

def get_css_links():
    return os.linesep.join('<link rel="stylesheet" type="text/css" href="{}"/>'.format(url) for url in self.get_css_urls())

def get_js_filenames():
    return [pkg_resources.resource_filename('discussion', path) for path in VENDOR_JS_URLS]

def get_js_links():
    return os.linesep.join('<script type="text/javascript" src="{}"/>'.format(url) for url in self.get_js_urls())

def get_js_body():
    js_dir = os.path.join(STATIC_DIR, 'discussion/js')
    js_urls = []
    for root, dirs, filenames in os.walk(js_dir):
        prefix = root[len(STATIC_DIR+1):]
        for filename in filenames:
            if filename.endswith('.js'):
                path = os.path.join(prefix, filename)
                print [root, prefix, filename, path]
                js_urls.append(static(path))
    tags = os.linesep.join('<script type="text/javascript" src="{}"></script>'.format(url) for url in js_urls)
    # moved to discussion_forum... will be deleted anyway
    return tags + os.linesep + render_mustache_templates()
<|endoftext|>


<commit_before>import numpy as np
import time

from data_block import DataBlock, SweepData

default_segments_hz = [#np.arange(0,200e3,8e3)-490e3,
                       #np.arange(200e3,360e3,4e3)-490e3,
                       #np.arange(360e3,440e3,2e3)-490e3,
                       #           np.arange(440e3,480e3,1e3)-490e3,
                                  np.arange(480e3,500e3,0.5e3)-490e3]

default_segments_mhz = [x/1e6 for x in default_segments_hz]

default_offsets_hz = np.concatenate(default_segments_hz)

def segmented_fine_sweep(ri,center_freqs,segments=default_segments_mhz,nchan_per_step=4,reads_per_step=2,sweep_id=2):
    swp = SweepData(sweep_id)
    for k,offsets in enumerate(segments):
        mindelta = np.abs(np.diff(offsets)).min()
        nsamp = np.ceil(np.log2(ri.fs/mindelta))
        print "segment",k,"log2(nsamp)",nsamp
        for offs in offsets:
            coarse_sweep(ri,center_freqs+offs,nsamp=2**nsamp,sweep_data = swp, nchan_per_step = nchan_per_step, reads_per_step=reads_per_step)
    return swp

def fine_sweep(ri,center_freqs, sweep_width = 0.1,npoints =128,nsamp=2**20, sweep_data = None):
    offsets = np.linspace(-sweep_width/2.0, sweep_width/2.0, npoints)
    if sweep_data is not None:
        swp = sweep_data
    else:
        swp = SweepData()

    for k,offset in enumerate(offsets):
        print "subsweep",k,"of",npoints
        coarse_sweep(ri, center_freqs + offset, nsamp=nsamp, nchan_per_step=4, reads_per_step=2, callback=None,sweep_id=k,sweep_data=swp)
    return swp

def coarse_sweep(ri,freqs=np.linspace(10,200,384),nsamp=2**15,nchan_per_step=4,reads_per_step=2, callback = None, sweep_id = 1, sweep_data = None):
    actual_freqs = ri.set_tone_freqs(freqs,nsamp=nsamp)
    if sweep_data is not None:
        data = sweep_data
    else:
        data = SweepData(sweep_id)
    ri.r.write_int('sync',0)
    ri.r.write_int('sync',1)
    ri.r.write_int('sync',0)
    time.sleep(1)
    nchan = ri.fft_bins.shape[0]
    nstep = int(np.ceil(nchan/float(nchan_per_step)))
    toread = set(range(nchan))
    for k in range(nstep):
        if len(toread) == 0:
            break
        if len(toread) > nchan_per_step:
            start = k
            stop = k + (nchan//nstep)*nstep
            selection = range(start,stop,nchan/nchan_per_step)[:nchan_per_step]
            toread = toread.difference(set(selection))
        else:
            selection = list(toread)
            toread = set()
        ri.select_fft_bins(selection)
        ri.r.write_int('sync',0)
        ri.r.write_int('sync',1)
        ri.r.write_int('sync',0)

        time.sleep(0.2)
        try:
            dmod,addr = ri.get_data(reads_per_step)
        except:
            continue
#        dmod = dmod*ri.wavenorm
        chids = ri.fpga_fft_readout_indexes+1
        tones = ri.tone_bins[ri.readout_selection]
        abort = False
        for m in range(len(chids)):
            block = DataBlock(data = dmod[:,m], tone=tones[m], fftbin = chids[m],
                     nsamp = nsamp, nfft = ri.nfft, wavenorm = ri.wavenorm, t0 = time.time(), fs = ri.fs, sweep_index=selection[m])
            block.progress = (k+1)/float(nstep)
            data.add_block(block)
            if callback:
                abort = callback(block)

        if abort:
            break

    return data

def reduce_catcher(din):
    chandata = {}
    for frame in din:
        for pkt in frame:
            if chandata.has_key(pkt['channel_id']):
                chandata[pkt['channel_id']].append(pkt['data'])
            else:
                chandata[pkt['channel_id']] =[pkt['data']]
    chanids = chandata.keys()
    chanids.sort()
    chanids = np.array(chanids)
    dout = None
    for n,chid in enumerate(chanids):
        darr = np.concatenate(chandata[chid])
        print darr.shape
        if dout is None:
            dout = np.empty((len(darr),len(chanids)),dtype='complex64')
            print dout.shape
        dout[:,n] = darr
    return chanids,dout<commit_msg>add code for doing pre-prepared sweeps very quickly<commit_after>import numpy as np
import time

from data_block import DataBlock, SweepData

default_segments_hz = [#np.arange(0,200e3,8e3)-490e3,
                       #np.arange(200e3,360e3,4e3)-490e3,
                       #np.arange(360e3,440e3,2e3)-490e3,
                       #           np.arange(440e3,480e3,1e3)-490e3,
                                  np.arange(480e3,500e3,0.5e3)-490e3]

default_segments_mhz = [x/1e6 for x in default_segments_hz]

default_offsets_hz = np.concatenate(default_segments_hz)

def segmented_fine_sweep(ri,center_freqs,segments=default_segments_mhz,nchan_per_step=4,reads_per_step=2,sweep_id=2):
    swp = SweepData(sweep_id)
    for k,offsets in enumerate(segments):
        mindelta = np.abs(np.diff(offsets)).min()
        nsamp = np.ceil(np.log2(ri.fs/mindelta))
        print "segment",k,"log2(nsamp)",nsamp
        for offs in offsets:
            coarse_sweep(ri,center_freqs+offs,nsamp=2**nsamp,sweep_data = swp, nchan_per_step = nchan_per_step, reads_per_step=reads_per_step)
    return swp

def prepare_sweep(ri,center_freqs,offsets,nsamp=2**21):
    if nsamp*4*len(offsets) > 2**29:
        raise ValueError("total number of waveforms (%d) times number of samples (%d) exceeds DRAM capacity" % (len(offsets),nsamp))
    freqs = center_freqs[None,:] + offsets[:,None]
    return ri.set_tone_freqs(freqs,nsamp=nsamp)

def do_prepared_sweep(ri,nchan_per_step=8,reads_per_step=2,callback = None, sweep_data=None):
    if sweep_data is not None:
        swp = sweep_data
    else:
        swp = SweepData()
    nbanks = ri.tone_bins.shape[0]
    nsamp = ri.tone_nsamp
    for bank in range(nbanks):
        ri.select_bank(bank)
        nchan = ri.fft_bins.shape[1]
        nstep = int(np.ceil(nchan/float(nchan_per_step)))
        toread = set(range(nchan))
        for k in range(nstep):
            if len(toread) == 0:
                break
            if len(toread) > nchan_per_step:
                start = k
                stop = k + (nchan//nstep)*nstep
                selection = range(start,stop,nchan/nchan_per_step)[:nchan_per_step]
                toread = toread.difference(set(selection))
            else:
                selection = list(toread)
                toread = set()
            selection.sort()
            ri.select_fft_bins(selection)
            ri.r.write_int('sync',0)
            ri.r.write_int('sync',1)
            ri.r.write_int('sync',0)

            time.sleep(0.2)
            try:
                dmod,addr = ri.get_data(reads_per_step)
            except Exception,e:
                print e
                continue
    #        dmod = dmod*ri.wavenorm
            chids = ri.fpga_fft_readout_indexes+1
            tones = ri.tone_bins[bank,ri.readout_selection]
            abort = False
            for m in range(len(chids)):
                sweep_index = selection[m]# np.abs(actual_freqs - ri.fs*tones[m]/nsamp).argmin()
                block = DataBlock(data = dmod[:,m], tone=tones[m], fftbin = chids[m],
                         nsamp = nsamp, nfft = ri.nfft, wavenorm = ri.wavenorm, t0 = time.time(), fs = ri.fs, sweep_index=sweep_index)
                block.progress = (k+1)/float(nstep)
                swp.add_block(block)
                if callback:
                    abort = callback(block)

            if abort:
                break

    return swp

def fine_sweep(ri,center_freqs, sweep_width = 0.1,npoints =128,nsamp=2**20, sweep_data = None):
    offsets = np.linspace(-sweep_width/2.0, sweep_width/2.0, npoints)
    if sweep_data is not None:
        swp = sweep_data
    else:
        swp = SweepData()

    for k,offset in enumerate(offsets):
        print "subsweep",k,"of",npoints
        coarse_sweep(ri, center_freqs + offset, nsamp=nsamp, nchan_per_step=4, reads_per_step=2, callback=None,sweep_id=k,sweep_data=swp)
    return swp

def coarse_sweep(ri,freqs=np.linspace(10,200,384),nsamp=2**15,nchan_per_step=4,reads_per_step=2, callback = None, sweep_id = 1, sweep_data = None):
    actual_freqs = ri.set_tone_freqs(freqs,nsamp=nsamp)
    if sweep_data is not None:
        data = sweep_data
    else:
        data = SweepData(sweep_id)
    ri.r.write_int('sync',0)
    ri.r.write_int('sync',1)
    ri.r.write_int('sync',0)
    time.sleep(1)
    nchan = ri.fft_bins.shape[1]
    nstep = int(np.ceil(nchan/float(nchan_per_step)))
    toread = set(range(nchan))
    for k in range(nstep):
        if len(toread) == 0:
            break
        if len(toread) > nchan_per_step:
            start = k
            stop = k + (nchan//nstep)*nstep
            selection = range(start,stop,nchan/nchan_per_step)[:nchan_per_step]
            toread = toread.difference(set(selection))
        else:
            selection = list(toread)
            toread = set()
        ri.select_fft_bins(selection)
        ri.r.write_int('sync',0)
        ri.r.write_int('sync',1)
        ri.r.write_int('sync',0)

        time.sleep(0.2)
        try:
            dmod,addr = ri.get_data(reads_per_step)
        except Exception,e:
            print e
            continue
#        dmod = dmod*ri.wavenorm
        chids = ri.fpga_fft_readout_indexes+1
        tones = ri.tone_bins[0,ri.readout_selection]
        abort = False
        for m in range(len(chids)):
            sweep_index = selection[m] # np.abs(actual_freqs - ri.fs*tones[m]/nsamp).argmin()
            block = DataBlock(data = dmod[:,m], tone=tones[m], fftbin = chids[m],
                     nsamp = nsamp, nfft = ri.nfft, wavenorm = ri.wavenorm, t0 = time.time(), fs = ri.fs, sweep_index=sweep_index)
            block.progress = (k+1)/float(nstep)
            data.add_block(block)
            if callback:
                abort = callback(block)

        if abort:
            break

    return data

def reduce_catcher(din):
    chandata = {}
    for frame in din:
        for pkt in frame:
            if chandata.has_key(pkt['channel_id']):
                chandata[pkt['channel_id']].append(pkt['data'])
            else:
                chandata[pkt['channel_id']] =[pkt['data']]
    chanids = chandata.keys()
    chanids.sort()
    chanids = np.array(chanids)
    dout = None
    for n,chid in enumerate(chanids):
        darr = np.concatenate(chandata[chid])
        print darr.shape
        if dout is None:
            dout = np.empty((len(darr),len(chanids)),dtype='complex64')
            print dout.shape
        dout[:,n] = darr
    return chanids,dout<|endoftext|>


<commit_before>from setuptools import setup, find_packages

version = '0.2.0.dev1'

setup(
    name='briefkasten',
    version=version,
    description='a reasonably secure web application for submitting content anonymously',
    long_description="",
    classifiers=[
        "Programming Language :: Python",
        "Framework :: Pylons",
        "Topic :: Internet :: WWW/HTTP",
        "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
        'License :: OSI Approved :: BSD License',
    ],
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        'Pyramid<1.7',
        'pyramid_chameleon',
        'colander',
        'deform',
        'diazo',
        'itsdangerous',
        'python-gnupg',
        'repoze.xmliter',
        'Paste',
    ],
    extras_require={
        'development': [
            'webtest',
            'flake8',
            'mock',
            'pep8 < 1.6',
            'pyramid_debugtoolbar',
            'pytest <2.8',
            'py >= 1.4.17',
            'pyflakes < 0.9',
            'pytest-flakes',
            'pytest-pep8',
            'pytest-cov',
            'pytest-capturelog',
            'tox',
            'pyquery',
            'mr.hermes',
            'setuptools-git',
            'devpi-client',
            'click',
        ],
    },
    entry_points="""
        [paste.app_factory]
        main = briefkasten:main
        [pytest11]
        briefkasten = briefkasten.testing
    """,
    message_extractors={'briefkasten': [
        ('**.py', 'lingua_python', None),
        ('**.zcml', 'lingua_xml', None),
        ('**.pt', 'lingua_xml', None),
    ]},
)
<commit_msg>make sure that building of the package works everywhere<commit_after>from setuptools import setup, find_packages

version = '0.2.0.dev1'

setup(
    name='briefkasten',
    version=version,
    description='a reasonably secure web application for submitting content anonymously',
    long_description="",
    classifiers=[
        "Programming Language :: Python",
        "Framework :: Pylons",
        "Topic :: Internet :: WWW/HTTP",
        "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
        'License :: OSI Approved :: BSD License',
    ],
    packages=find_packages(),
    include_package_data=True,
    package_data={
        'briefkasten': [
            'templates/*.*',
            'tests/*.*',
            'tests/gpghome/*.*',
        ],
    },
    zip_safe=False,
    setup_requires=[
        'setuptools-git >= 0',
        'setuptools-git-version'
    ],
    install_requires=[
        'Pyramid<1.7',
        'pyramid_chameleon',
        'colander',
        'deform',
        'diazo',
        'itsdangerous',
        'python-gnupg',
        'repoze.xmliter',
        'Paste',
    ],
    extras_require={
        'development': [
            'webtest',
            'flake8',
            'mock',
            'pep8 < 1.6',
            'pyramid_debugtoolbar',
            'pytest <2.8',
            'py >= 1.4.17',
            'pyflakes < 0.9',
            'pytest-flakes',
            'pytest-pep8',
            'pytest-cov',
            'pytest-capturelog',
            'tox',
            'pyquery',
            'mr.hermes',
            'setuptools-git',
            'devpi-client',
            'click',
        ],
    },
    entry_points="""
        [paste.app_factory]
        main = briefkasten:main
        [pytest11]
        briefkasten = briefkasten.testing
    """,
    message_extractors={'briefkasten': [
        ('**.py', 'lingua_python', None),
        ('**.zcml', 'lingua_xml', None),
        ('**.pt', 'lingua_xml', None),
    ]},
)
<|endoftext|>


<commit_before># coding=UTF-8
from __future__ import absolute_import, division, print_function

import collections
import io
import six
import struct

from coolamqp.framing.frames import AMQPBodyFrame, AMQPHeaderFrame, AMQPHeartbeatFrame, AMQPMethodFrame
from coolamqp.framing.definitions import FRAME_HEADER, FRAME_HEARTBEAT, FRAME_END, FRAME_METHOD, FRAME_BODY

FRAME_TYPES = {
    FRAME_HEADER: AMQPHeaderFrame,
    FRAME_BODY: AMQPBodyFrame,
    FRAME_METHOD: AMQPMethodFrame
}

class ReceivingFramer(object):
    """
    Assembles AMQP framing from received data.

    Just call with .put(data) upon receiving,
    and on_frame will be called with fresh frames.

    Not thread safe.

    State machine
        (frame_type is None)  and has_bytes(1)        ->          (frame_type <- bytes(1))

        (frame_type is HEARTBEAT) and has_bytes(AMQPHeartbeatFrame.LENGTH-1)  ->          (output_frame, frame_type <- None)
        (frame_type is not HEARTBEAT and not None) and has_bytes(6)  ->      (frame_channel <- bytes(2),
                                                                 frame_size <- bytes(4))

        (frame_size is not None) and has_bytes(frame_size+1)    ->  (output_frame,
                                                                            frame_type <- None
                                                                            frame_size < None)
    """
    def __init__(self, on_frame=lambda frame: None):
        self.chunks = collections.deque()   # all received data
        self.total_data_len = 0

        self.frame_type = None
        self.frame_channel = None
        self.frame_size = None

        self.bytes_needed = None    # bytes needed for a new frame
        self.on_frame = on_frame

    def put(self, data):
        """
        Called upon receiving data.

        May result in any number of .on_frame() calls
        :param data: received data
        """
        self.total_data_len += len(data)
        self.chunks.append(memoryview(data))

        while self._statemachine():
            pass

    def _extract(self, up_to): # return up to up_to bytes from current chunk, switch if necessary
        assert self.total_data_len >= up_to, 'Tried to extract %s but %s remaining' % (up_to, self.total_data_len)
        if up_to >= len(self.chunks[0]):
            q =  self.chunks.popleft()
        else:
            q = self.chunks[0][:up_to]
            self.chunks[0] = self.chunks[0][up_to:]

        self.total_data_len -= len(q)
        print(repr(q))
        return q

    def _statemachine(self):
        # state rule 1
        if self.frame_type is None and self.total_data_len > 0:
            if six.PY3:
                self.frame_type = self._extract(1)[0]
            else:
                self.frame_type = ord(self._extract(1)[0])

            if self.frame_type not in (FRAME_HEARTBEAT, FRAME_HEADER, FRAME_METHOD, FRAME_BODY):
                raise ValueError('Invalid frame')

            return True

        # state rule 2
        elif (self.frame_type == FRAME_HEARTBEAT) and (self.total_data_len >= AMQPHeartbeatFrame.LENGTH-1):
            data = b''
            while len(data) < AMQPHeartbeatFrame.LENGTH-1:
                data = data + self._extract(AMQPHeartbeatFrame.LENGTH-1 - len(data)).tobytes()

            if data != AMQPHeartbeatFrame.DATA[1:]:
                # Invalid heartbeat frame!
                raise ValueError('Invalid AMQP heartbeat')

            self.on_frame(AMQPHeartbeatFrame())
            self.frame_type = None

            return True

        # state rule 3
        elif (self.frame_type != FRAME_HEARTBEAT) and (self.frame_type is not None) and (self.frame_size is None) and (self.total_data_len > 6):
            hdr = b''
            while len(hdr) < 6:
                hdr = hdr + self._extract(6 - len(hdr)).tobytes()

            self.frame_channel, self.frame_size = struct.unpack('!HI',hdr)

            return True

        # state rule 4
        elif (self.frame_size is not None) and (self.total_data_len >= (self.frame_size+1)):

            if len(self.chunks[0]) >= self.frame_size:
                # We can subslice it - it's very fast
                payload = self._extract(self.frame_size)
            else:
                # Construct a separate buffer :(
                payload = io.BytesIO()
                while payload.tell() < self.frame_size:
                    payload.write(self._extract(self.frame_size - payload.tell()))

                assert payload.tell() <= self.total_data_len

                payload = memoryview(payload.getvalue())

            z = self._extract(1)[0]
            if six.PY2:
                z = ord(z)

            if z != FRAME_END:
                raise ValueError('Invalid frame end')

            try:
                frame = FRAME_TYPES[self.frame_type].unserialize(self.frame_channel, payload)
            except ValueError:
                raise

            self.on_frame(frame)
            self.frame_type = None
            self.frame_size = None

            return True
        return False<commit_msg>print removed<commit_after># coding=UTF-8
from __future__ import absolute_import, division, print_function

import collections
import io
import six
import struct

from coolamqp.framing.frames import AMQPBodyFrame, AMQPHeaderFrame, AMQPHeartbeatFrame, AMQPMethodFrame
from coolamqp.framing.definitions import FRAME_HEADER, FRAME_HEARTBEAT, FRAME_END, FRAME_METHOD, FRAME_BODY

FRAME_TYPES = {
    FRAME_HEADER: AMQPHeaderFrame,
    FRAME_BODY: AMQPBodyFrame,
    FRAME_METHOD: AMQPMethodFrame
}

class ReceivingFramer(object):
    """
    Assembles AMQP framing from received data.

    Just call with .put(data) upon receiving,
    and on_frame will be called with fresh frames.

    Not thread safe.

    State machine
        (frame_type is None)  and has_bytes(1)        ->          (frame_type <- bytes(1))

        (frame_type is HEARTBEAT) and has_bytes(AMQPHeartbeatFrame.LENGTH-1)  ->          (output_frame, frame_type <- None)
        (frame_type is not HEARTBEAT and not None) and has_bytes(6)  ->      (frame_channel <- bytes(2),
                                                                 frame_size <- bytes(4))

        (frame_size is not None) and has_bytes(frame_size+1)    ->  (output_frame,
                                                                            frame_type <- None
                                                                            frame_size < None)
    """
    def __init__(self, on_frame=lambda frame: None):
        self.chunks = collections.deque()   # all received data
        self.total_data_len = 0

        self.frame_type = None
        self.frame_channel = None
        self.frame_size = None

        self.bytes_needed = None    # bytes needed for a new frame
        self.on_frame = on_frame

    def put(self, data):
        """
        Called upon receiving data.

        May result in any number of .on_frame() calls
        :param data: received data
        """
        self.total_data_len += len(data)
        self.chunks.append(memoryview(data))

        while self._statemachine():
            pass

    def _extract(self, up_to): # return up to up_to bytes from current chunk, switch if necessary
        assert self.total_data_len >= up_to, 'Tried to extract %s but %s remaining' % (up_to, self.total_data_len)
        if up_to >= len(self.chunks[0]):
            q =  self.chunks.popleft()
        else:
            q = self.chunks[0][:up_to]
            self.chunks[0] = self.chunks[0][up_to:]

        self.total_data_len -= len(q)
        return q

    def _statemachine(self):
        # state rule 1
        if self.frame_type is None and self.total_data_len > 0:
            if six.PY3:
                self.frame_type = self._extract(1)[0]
            else:
                self.frame_type = ord(self._extract(1)[0])

            if self.frame_type not in (FRAME_HEARTBEAT, FRAME_HEADER, FRAME_METHOD, FRAME_BODY):
                raise ValueError('Invalid frame')

            return True

        # state rule 2
        elif (self.frame_type == FRAME_HEARTBEAT) and (self.total_data_len >= AMQPHeartbeatFrame.LENGTH-1):
            data = b''
            while len(data) < AMQPHeartbeatFrame.LENGTH-1:
                data = data + self._extract(AMQPHeartbeatFrame.LENGTH-1 - len(data)).tobytes()

            if data != AMQPHeartbeatFrame.DATA[1:]:
                # Invalid heartbeat frame!
                raise ValueError('Invalid AMQP heartbeat')

            self.on_frame(AMQPHeartbeatFrame())
            self.frame_type = None

            return True

        # state rule 3
        elif (self.frame_type != FRAME_HEARTBEAT) and (self.frame_type is not None) and (self.frame_size is None) and (self.total_data_len > 6):
            hdr = b''
            while len(hdr) < 6:
                hdr = hdr + self._extract(6 - len(hdr)).tobytes()

            self.frame_channel, self.frame_size = struct.unpack('!HI',hdr)

            return True

        # state rule 4
        elif (self.frame_size is not None) and (self.total_data_len >= (self.frame_size+1)):

            if len(self.chunks[0]) >= self.frame_size:
                # We can subslice it - it's very fast
                payload = self._extract(self.frame_size)
            else:
                # Construct a separate buffer :(
                payload = io.BytesIO()
                while payload.tell() < self.frame_size:
                    payload.write(self._extract(self.frame_size - payload.tell()))

                assert payload.tell() <= self.total_data_len

                payload = memoryview(payload.getvalue())

            z = self._extract(1)[0]
            if six.PY2:
                z = ord(z)

            if z != FRAME_END:
                raise ValueError('Invalid frame end')

            try:
                frame = FRAME_TYPES[self.frame_type].unserialize(self.frame_channel, payload)
            except ValueError:
                raise

            self.on_frame(frame)
            self.frame_type = None
            self.frame_size = None

            return True
        return False<|endoftext|>