<commit_before>from __future__ import division
import pyglet
from pyglet.gl import *
import math

import core
import shapes
from euclid import Vector3

class GameRenderer(object):
    """Top level renderer.

    Renders the score and calls all other game object renderers.
    """
    BACKGROUND_COLOR = (0.8862, 0.3215, 0.2784, 1)

    def __init__(self, game):
        self.game = game
        self.batch = pyglet.graphics.Batch()

        # TODO: render the score and other labels

        # Init the renderers for all game objects
        self.grid_renderer = GridRenderer(game.grid, self.batch)
        self.ball_renderer = None
        self.lock_renderer = LockRenderer()
        self.mouse = Mouse(self.batch)

    def update_ball(self):
        if self.game.ball and not self.ball_renderer:
            self.ball_renderer = BallRenderer(self.game.ball, self.batch)
        elif not self.game.ball and self.ball_renderer:
            self.ball_renderer.delete()
            self.ball_renderer = None

    def draw(self, show_locks):
        # We can draw the batch only after all renderers updated it
        if self.ball_renderer:
            self.ball_renderer.update()
        self.grid_renderer.update()
        self.update_ball()
        self.batch.draw()
        if show_locks:
            self.lock_renderer.draw(self.game.grid)

    def reset(self, game):
        self.game = game
        self.grid_renderer.delete()
        self.grid_renderer = GridRenderer(game.grid, self.batch)

class BlockRenderer (object):
    rotate = None
    shape_class = shapes.Box

    def __init__(self, batch, group, x, y, block):
        self.x = x
        self.y = y
        self.block = block
        self.shape = self.make_shape(batch, group)

    def make_shape(self, batch, group):
        return self.shape_class(batch, group, (self.x, self.y, 0), self.size, tuple(self.color), self.rotate)

    def delete(self):
        self.shape.delete()

    def update(self):
        pass

class Wall (BlockRenderer):
    size = (.9, .9, .9)
    color = (.3, .3, .3)

class Mirror (BlockRenderer):
    size = (.8, .1, .9)
    color = (.1, .9, .9)

    @property
    def rotate(self):
        return (0, math.pi * self.block.slope / 4, 0)

class FlipFlop (BlockRenderer):
    size = (.3, .3, .3)
    shape_class = shapes.Disc

    colors = {
        False: Vector3(.5, .5, .5),
        True: Vector3(.8, .8, .2)
    }

    THRESHOLD = 0.1
    SPEED = 0.1

    def __init__(self, batch, group, x, y, block):
        self.old_is_on = block.is_on
        self.new_color = self.color = self.colors[block.is_on]
        super(FlipFlop, self).__init__(batch, group, x, y, block)

    def update(self):
        if self.old_is_on != self.block.is_on:
            self.new_color = self.colors[self.block.is_on]
            self.old_is_on = self.block.is_on

        delta = self.new_color - self.color
        if abs(delta) < self.THRESHOLD:
            self.color = self.new_color.copy()
        else:
            self.color += delta * self.SPEED

        self.shape.set_color(tuple(self.color))

class ScalerGroup (pyglet.graphics.Group):
    def __init__(self, x, y, parent=None):
        super(ScalerGroup, self).__init__(parent)
        self.offset = x, y, 0

    def set_state(self):
        glPushMatrix()
        glTranslatef(*self.offset)
        glScalef(1/20, 1/20, 1/20)

    def unset_state(self):
        glPopMatrix()

class GenericRenderer (BlockRenderer):
    def __init__(self, batch, group, x, y, block):
        self.block = block
        self.shape = pyglet.text.Label(block.__class__.__name__[0],
                batch=batch, anchor_x='center', anchor_y='center',
                group=ScalerGroup(x, y, parent=group))

class GridRenderer(dict):
    def __init__(self, grid, batch):
        self.grid = grid
        self.batch = batch
        self.update(True)

    def update(self, force=False):
        items = self.grid if force else self.grid.get_dirty()

        for pos in items:
            old = self.get(pos)
            if old:
                old.delete()

            if self.grid[pos]:
                self[pos] = create_block_renderer(self.grid[pos], self.batch, None, *pos)
            else:
                del self[pos]

        [r.update() for r in self.itervalues()]

    def delete(self):
        for renderer in self.values():
            renderer.delete()

class LockRenderer (object):
    def __init__(self):
        self.sprite = pyglet.graphics.vertex_list(4,
            ('v3f', (-.4, -.4, 0, .4, -.4, 0, .4, .4, 0, -.4, .4, 0)),
            ('c4f', (.8, 0, 0, .5) * 4)
        )

    def draw(self, grid):
        for (x, y), block in grid.iteritems():
            if not block.locked:
                continue
            glPushMatrix()
            glTranslatef(x, y, 1.1)
            self.sprite.draw(GL_QUADS)
            glPopMatrix()

class BallGroup (pyglet.graphics.Group):
    def __init__(self, renderer, parent=None):
        super(BallGroup, self).__init__(parent)
        self.renderer = renderer

    def set_state(self):
        glPushMatrix()
        glTranslatef(*self.renderer.pos)

        s = max(0, self.renderer.size)

        glScalef(s, s, s)

    def unset_state(self):
        glPopMatrix()

class BallRenderer(object):
    SPEED = 0.02

    def __init__(self, ball, batch):
        self.ball = ball

        self.group = BallGroup(self)
        self.shape = shapes.Ico(batch, self.group, (0, 0, 0), (.2, .2, .2), (1, 1, 0))

        self.old_pos = None
        self.update()

    def update(self):
        if self.old_pos != self.ball.pos:
            self.old_pos = self.ball.pos

            self.frame = 0
            x, y = self.ball.pos
            self.pos = Vector3(x, y, 0)

            dx, dy = self.ball.direction
            self.vel = Vector3(dx, dy, 0) * self.SPEED
        else:
            self.frame += 1
            self.pos += self.vel

        self.size = 1 - self.frame / 20 + (self.frame % 5) / 50

    def delete(self):
        self.shape.delete()

class FollowGroup (pyglet.graphics.Group):
    def __init__(self, target, parent=None):
        super(FollowGroup, self).__init__(parent)
        self.target = target

    def set_state(self):
        glPushAttrib(GL_POLYGON_BIT)
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

        x, y, z = self.target.pos
        glPushMatrix()
        glTranslatef(x, y, z)

    def unset_state(self):
        glPopMatrix()
        glPopAttrib()

class Mouse (object):
    def __init__(self, batch):
        self.group = FollowGroup(self)
        # We need to cache the batch in order to change shapes on the fly
        self.batch = batch
        self.cursor = None
        self.block_class = None
        self.pos = (0, 0, 0)

    def set_cursor(self, blockClass):
        if self.block_class == blockClass:
            return
        self.block_class = blockClass

        if self.cursor:
            self.cursor.delete()
            self.cursor = None

        if self.block_class:
            block = self.block_class()
            self.cursor = create_block_renderer(block, self.batch, self.group, 0, 0)

CORE_MAPPING = {
    core.Wall: Wall,
    core.Mirror: Mirror,
    core.FlipFlop: FlipFlop,
}

# TODO: move to a better place
def create_block_renderer(block, batch, group, x, y):
    renderer_class = CORE_MAPPING.get(block.__class__, GenericRenderer)
    return renderer_class(batch, group, x, y, block)
<commit_msg>Recreate ball renderer on reset.<commit_after>from __future__ import division
import pyglet
from pyglet.gl import *
import math

import core
import shapes
from euclid import Vector3

class GameRenderer(object):
    """Top level renderer.

    Renders the score and calls all other game object renderers.
    """
    BACKGROUND_COLOR = (0.8862, 0.3215, 0.2784, 1)

    def __init__(self, game):
        self.game = game
        self.batch = pyglet.graphics.Batch()

        # TODO: render the score and other labels

        # Init the renderers for all game objects
        self.grid_renderer = GridRenderer(game.grid, self.batch)
        self.ball_renderer = None
        self.lock_renderer = LockRenderer()
        self.mouse = Mouse(self.batch)

    def update_ball(self):
        if self.game.ball and not self.ball_renderer:
            self.ball_renderer = BallRenderer(self.game.ball, self.batch)
        elif not self.game.ball and self.ball_renderer:
            self.ball_renderer.delete()
            self.ball_renderer = None

    def draw(self, show_locks):
        # We can draw the batch only after all renderers updated it
        if self.ball_renderer:
            self.ball_renderer.update()
        self.grid_renderer.update()
        self.update_ball()
        self.batch.draw()
        if show_locks:
            self.lock_renderer.draw(self.game.grid)

    def reset(self, game):
        self.game = game
        self.ball_renderer.delete()
        self.ball_renderer = None

        self.grid_renderer.delete()
        self.grid_renderer = GridRenderer(game.grid, self.batch)

class BlockRenderer (object):
    rotate = None
    shape_class = shapes.Box

    def __init__(self, batch, group, x, y, block):
        self.x = x
        self.y = y
        self.block = block
        self.shape = self.make_shape(batch, group)

    def make_shape(self, batch, group):
        return self.shape_class(batch, group, (self.x, self.y, 0), self.size, tuple(self.color), self.rotate)

    def delete(self):
        self.shape.delete()

    def update(self):
        pass

class Wall (BlockRenderer):
    size = (.9, .9, .9)
    color = (.3, .3, .3)

class Mirror (BlockRenderer):
    size = (.8, .1, .9)
    color = (.1, .9, .9)

    @property
    def rotate(self):
        return (0, math.pi * self.block.slope / 4, 0)

class FlipFlop (BlockRenderer):
    size = (.3, .3, .3)
    shape_class = shapes.Disc

    colors = {
        False: Vector3(.5, .5, .5),
        True: Vector3(.8, .8, .2)
    }

    THRESHOLD = 0.1
    SPEED = 0.1

    def __init__(self, batch, group, x, y, block):
        self.old_is_on = block.is_on
        self.new_color = self.color = self.colors[block.is_on]
        super(FlipFlop, self).__init__(batch, group, x, y, block)

    def update(self):
        if self.old_is_on != self.block.is_on:
            self.new_color = self.colors[self.block.is_on]
            self.old_is_on = self.block.is_on

        delta = self.new_color - self.color
        if abs(delta) < self.THRESHOLD:
            self.color = self.new_color.copy()
        else:
            self.color += delta * self.SPEED

        self.shape.set_color(tuple(self.color))

class ScalerGroup (pyglet.graphics.Group):
    def __init__(self, x, y, parent=None):
        super(ScalerGroup, self).__init__(parent)
        self.offset = x, y, 0

    def set_state(self):
        glPushMatrix()
        glTranslatef(*self.offset)
        glScalef(1/20, 1/20, 1/20)

    def unset_state(self):
        glPopMatrix()

class GenericRenderer (BlockRenderer):
    def __init__(self, batch, group, x, y, block):
        self.block = block
        self.shape = pyglet.text.Label(block.__class__.__name__[0],
                batch=batch, anchor_x='center', anchor_y='center',
                group=ScalerGroup(x, y, parent=group))

class GridRenderer(dict):
    def __init__(self, grid, batch):
        self.grid = grid
        self.batch = batch
        self.update(True)

    def update(self, force=False):
        items = self.grid if force else self.grid.get_dirty()

        for pos in items:
            old = self.get(pos)
            if old:
                old.delete()

            if self.grid[pos]:
                self[pos] = create_block_renderer(self.grid[pos], self.batch, None, *pos)
            else:
                del self[pos]

        [r.update() for r in self.itervalues()]

    def delete(self):
        for renderer in self.values():
            renderer.delete()

class LockRenderer (object):
    def __init__(self):
        self.sprite = pyglet.graphics.vertex_list(4,
            ('v3f', (-.4, -.4, 0, .4, -.4, 0, .4, .4, 0, -.4, .4, 0)),
            ('c4f', (.8, 0, 0, .5) * 4)
        )

    def draw(self, grid):
        for (x, y), block in grid.iteritems():
            if not block.locked:
                continue
            glPushMatrix()
            glTranslatef(x, y, 1.1)
            self.sprite.draw(GL_QUADS)
            glPopMatrix()

class BallGroup (pyglet.graphics.Group):
    def __init__(self, renderer, parent=None):
        super(BallGroup, self).__init__(parent)
        self.renderer = renderer

    def set_state(self):
        glPushMatrix()
        glTranslatef(*self.renderer.pos)

        s = max(0, self.renderer.size)

        glScalef(s, s, s)

    def unset_state(self):
        glPopMatrix()

class BallRenderer(object):
    SPEED = 0.02

    def __init__(self, ball, batch):
        self.ball = ball

        self.group = BallGroup(self)
        self.shape = shapes.Ico(batch, self.group, (0, 0, 0), (.2, .2, .2), (1, 1, 0))

        self.old_pos = None
        self.update()

    def update(self):
        if self.old_pos != self.ball.pos:
            self.old_pos = self.ball.pos

            self.frame = 0
            x, y = self.ball.pos
            self.pos = Vector3(x, y, 0)

            dx, dy = self.ball.direction
            self.vel = Vector3(dx, dy, 0) * self.SPEED
        else:
            self.frame += 1
            self.pos += self.vel

        self.size = 1 - self.frame / 20 + (self.frame % 5) / 50

    def delete(self):
        self.shape.delete()

class FollowGroup (pyglet.graphics.Group):
    def __init__(self, target, parent=None):
        super(FollowGroup, self).__init__(parent)
        self.target = target

    def set_state(self):
        glPushAttrib(GL_POLYGON_BIT)
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

        x, y, z = self.target.pos
        glPushMatrix()
        glTranslatef(x, y, z)

    def unset_state(self):
        glPopMatrix()
        glPopAttrib()

class Mouse (object):
    def __init__(self, batch):
        self.group = FollowGroup(self)
        # We need to cache the batch in order to change shapes on the fly
        self.batch = batch
        self.cursor = None
        self.block_class = None
        self.pos = (0, 0, 0)

    def set_cursor(self, blockClass):
        if self.block_class == blockClass:
            return
        self.block_class = blockClass

        if self.cursor:
            self.cursor.delete()
            self.cursor = None

        if self.block_class:
            block = self.block_class()
            self.cursor = create_block_renderer(block, self.batch, self.group, 0, 0)

CORE_MAPPING = {
    core.Wall: Wall,
    core.Mirror: Mirror,
    core.FlipFlop: FlipFlop,
}

# TODO: move to a better place
def create_block_renderer(block, batch, group, x, y):
    renderer_class = CORE_MAPPING.get(block.__class__, GenericRenderer)
    return renderer_class(batch, group, x, y, block)
<|endoftext|>


<commit_before>"""
Core views, including the main homepage,

documentation and header rendering, and server errors.
"""

import os
import logging

from django.conf import settings
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView

from readthedocs.builds.models import Build
from readthedocs.builds.models import Version
from readthedocs.core.utils import broadcast
from readthedocs.projects import constants
from readthedocs.projects.models import Project, ImportedFile
from readthedocs.projects.tasks import remove_dir
from readthedocs.redirects.utils import get_redirect_response

log = logging.getLogger(__name__)

class NoProjectException(Exception):
    pass

class HomepageView(TemplateView):

    template_name = 'homepage.html'

    def get_context_data(self, **kwargs):
        """Add latest builds and featured projects"""
        context = super(HomepageView, self).get_context_data(**kwargs)
        latest = []
        latest_builds = (
            Build.objects
            .filter(
                project__privacy_level=constants.PUBLIC,
                success=True,
            )
            .order_by('-date')
        )[:100]
        for build in latest_builds:
            if (build.project not in latest and len(latest) < 10):
                latest.append(build.project)
        context['project_list'] = latest
        context['featured_list'] = Project.objects.filter(featured=True)
        return context

class SupportView(TemplateView):
    template_name = 'support.html'

    def get_context_data(self, **kwargs):
        context = super(SupportView, self).get_context_data(**kwargs)
        support_email = getattr(settings, 'SUPPORT_EMAIL', None)
        if not support_email:
            support_email = 'support@{domain}'.format(
                domain=getattr(settings, 'PRODUCTION_DOMAIN', 'readthedocs.org'))

        context['support_email'] = support_email
        return context

def random_page(request, project_slug=None):
    imported_file = ImportedFile.objects.order_by('?')
    if project_slug:
        imported_file = imported_file.filter(project__slug=project_slug)
    imported_file = imported_file.first()
    if imported_file is None:
        raise Http404
    url = imported_file.get_absolute_url()
    return HttpResponseRedirect(url)

@csrf_exempt
def wipe_version(request, project_slug, version_slug):
    version = get_object_or_404(Version, project__slug=project_slug,
                                slug=version_slug)
    if request.user not in version.project.users.all():
        raise Http404("You must own this project to wipe it.")

    if request.method == 'POST':
        del_dirs = [
            os.path.join(version.project.doc_path, 'checkouts', version.slug),
            os.path.join(version.project.doc_path, 'envs', version.slug),
            os.path.join(version.project.doc_path, 'conda', version.slug),
        ]
        for del_dir in del_dirs:
            broadcast(type='build', task=remove_dir, args=[del_dir])
        return redirect('project_version_list', project_slug)
    else:
        return render_to_response('wipe_version.html',
                                  context_instance=RequestContext(request))

def divide_by_zero(request):
    return 1 / 0

def server_error(request, template_name='500.html', **kwargs):
    """A simple 500 handler so we get media"""
    r = render_to_response(template_name,
                           context_instance=RequestContext(request))
    r.status_code = 500
    return r

def server_error_404(request, template_name='404.html', **kwargs):
    """A simple 404 handler so we get media"""
    response = get_redirect_response(request, path=request.get_full_path())
    if response:
        return response
    r = render_to_response(template_name,
                           context_instance=RequestContext(request))
    r.status_code = 404
    return r
<commit_msg>Use the proper arguments<commit_after>"""
Core views, including the main homepage,

documentation and header rendering, and server errors.
"""

import os
import logging

from django.conf import settings
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView

from readthedocs.builds.models import Build
from readthedocs.builds.models import Version
from readthedocs.core.utils import broadcast
from readthedocs.projects import constants
from readthedocs.projects.models import Project, ImportedFile
from readthedocs.projects.tasks import remove_dir
from readthedocs.redirects.utils import get_redirect_response

log = logging.getLogger(__name__)

class NoProjectException(Exception):
    pass

class HomepageView(TemplateView):

    template_name = 'homepage.html'

    def get_context_data(self, **kwargs):
        """Add latest builds and featured projects"""
        context = super(HomepageView, self).get_context_data(**kwargs)
        latest = []
        latest_builds = (
            Build.objects
            .filter(
                project__privacy_level=constants.PUBLIC,
                success=True,
            )
            .order_by('-date')
        )[:100]
        for build in latest_builds:
            if (build.project not in latest and len(latest) < 10):
                latest.append(build.project)
        context['project_list'] = latest
        context['featured_list'] = Project.objects.filter(featured=True)
        return context

class SupportView(TemplateView):
    template_name = 'support.html'

    def get_context_data(self, **kwargs):
        context = super(SupportView, self).get_context_data(**kwargs)
        support_email = getattr(settings, 'SUPPORT_EMAIL', None)
        if not support_email:
            support_email = 'support@{domain}'.format(
                domain=getattr(settings, 'PRODUCTION_DOMAIN', 'readthedocs.org'))

        context['support_email'] = support_email
        return context

def random_page(request, project_slug=None):
    imported_file = ImportedFile.objects.order_by('?')
    if project_slug:
        imported_file = imported_file.filter(project__slug=project_slug)
    imported_file = imported_file.first()
    if imported_file is None:
        raise Http404
    url = imported_file.get_absolute_url()
    return HttpResponseRedirect(url)

@csrf_exempt
def wipe_version(request, project_slug, version_slug):
    version = get_object_or_404(Version, project__slug=project_slug,
                                slug=version_slug)
    if request.user not in version.project.users.all():
        raise Http404("You must own this project to wipe it.")

    if request.method == 'POST':
        del_dirs = [
            os.path.join(version.project.doc_path, 'checkouts', version.slug),
            os.path.join(version.project.doc_path, 'envs', version.slug),
            os.path.join(version.project.doc_path, 'conda', version.slug),
        ]
        for del_dir in del_dirs:
            broadcast(type='build', task=remove_dir, args=[del_dir])
        return redirect('project_version_list', project_slug)
    else:
        return render_to_response('wipe_version.html',
                                  context_instance=RequestContext(request))

def divide_by_zero(request):
    return 1 / 0

def server_error(request, exception, template_name='500.html'):
    """A simple 500 handler so we get media"""
    r = render_to_response(template_name,
                           context_instance=RequestContext(request))
    r.status_code = 500
    return r

def server_error_404(request, exception, template_name='404.html'):
    """A simple 404 handler so we get media"""
    response = get_redirect_response(request, path=request.get_full_path())
    if response:
        return response
    r = render_to_response(template_name,
                           context_instance=RequestContext(request))
    r.status_code = 404
    return r
<|endoftext|>


<commit_before>import re
from django.contrib import admin, messages
from django.utils.translation import ugettext_lazy as _
from django.utils.text import mark_safe

from adminsortable2.admin import SortableInlineAdminMixin

from real_estate.admin import AdminInlineImages

from .models import ResaleApartment, ResaleApartmentImage, ResaleCharacteristic
from .forms import ResaleApartmentForm, ResaleApartmentImageForm

class ResaleImageInline(
        SortableInlineAdminMixin, admin.TabularInline, AdminInlineImages):
    model = ResaleApartmentImage
    form = ResaleApartmentImageForm
    classes = ['collapse', ]
    extra = 0
    min_num = 0
    fields = ('thumbnail', 'image')
    readonly_fields = ('thumbnail', )

@admin.register(ResaleApartment)
class ResaleApartmentAdmin(admin.ModelAdmin):
    inlines = [ResaleImageInline, ]
    form = ResaleApartmentForm

    list_display_initial = (
        'id', 'status', 'is_active', 'rooms', 'total_area',
        'full_price', 'agency_price', 'fee', 'price',
        'residental_complex',
    )

    list_filter_initial = ('rooms', 'residental_complex', )

    list_display = list_display_initial
    list_filter = list_filter_initial
    list_display_links = (
        'id', 'status', 'rooms', 'total_area'
    )
    list_editable = ('is_active', )
    readonly_fields = ['id', 'date_added', 'modified_by']
    filter_horizontal = ['characteristics']

    def fee(self, obj):
        return obj.fee
    fee.short_description = _('комиссия')

    def full_price(self, obj):
        return obj.full_price
    full_price.short_description = _("текущая стоимость")

    image_inline_pattern = re.compile('^photos-\d+-(file|image)$')
    fieldsets = None

    def get_fieldsets(self, request, obj=None):
        self.fieldsets = self.dynamic_fieldset(request)
        return super().get_fieldsets(request, obj)

    def dynamic_fieldset(self, request):
        """
        get the dynamic field sets
        """
        deal_status_part_fields = (
            'date_added',
            ('sold_date', 'previous_buy_date',),
            'status',
            'amount_of_owners',
            'comment',
            'related_mortgage',
            'price',
            'agency_price',
            'agency_price_with_sales',
        )
        if request.user.has_perm('resale.can_add_change_delete_all_resale'):
            deal_status_part_fields += ('created_by', 'modified_by',)
        deal_status_part = (_('Информация по сделке'), {
            'classes': ('collapse',),
            'fields': deal_status_part_fields,
        })

        owner_part = (_('Информация по продавцу'), {
            'classes': ('collapse',),
            'fields': (('owner_name', 'owner_phone_number'),),
        })

        apartment_part_fields = (
            'neighbourhood', 'residental_complex', 'street',
            ('building', 'building_block',), 'coordinates',
            'rooms', ('floor', 'number_of_storeys'),
            'section', 'apartment_number', 'building_type',
            'home_series', 'date_of_construction',
            ('total_area', 'kitchen_area', 'balcony_area',),
            ('celling_height', 'interior_decoration',),
            'layout', 'description', 'characteristics'
        )
        apartment_part = (_('Информация по квартире'), {
            'classes': ('collapse',),
            'fields': apartment_part_fields,
        })

        fieldsets = (
            (None, {
                'fields': ('id', 'is_active',),
            }),
            deal_status_part,
            owner_part,
            apartment_part,
        )

        return fieldsets

    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        # Auto set user
        if not change:
            obj.created_by = request.user
        if 'modified_by' not in form.changed_data:
            obj.modified_by = request.user
        obj.save()

        for i in request.FILES:
            # only for appropriate formsets:
            m = self.image_inline_pattern.search(i)
            if m:
                for afile in request.FILES.getlist(i)[:-1]:
                    img_form = ResaleApartmentImageForm(
                        {'apartment': obj.id}, {'image': afile}
                    )
                    if img_form.is_valid():
                        img_form.save()
                    else:
                        message_text = _(
                            '
Файл "%(file_name)s" не загружен
') \
                            % {'file_name': afile.name}
                        for field, errors in img_form.errors.items():
                            for error in errors:
                                message_text += '
{field}:\
                                 {error}
'.format(
                                    field=field,
                                    error=error,
                                    message_class=messages.DEFAULT_TAGS.get(
                                        messages.WARNING),
                                )
                        message_text += "

"
                        messages.add_message(
                            request, messages.WARNING, mark_safe(message_text))

    def get_queryset(self, request):
        qs = super(ResaleApartmentAdmin, self).get_queryset(request)
        if request.user.has_perm('resale.can_add_change_delete_all_resale'):
            return qs
        return qs.filter(created_by=request.user)

    def changelist_view(self, request, extra_context=None):
        self.list_display = self.list_display_initial
        self.list_filter = self.list_filter_initial
        if request.user.has_perm('resale.can_add_change_delete_all_resale'):
            self.list_display += ('created_by',)
            self.list_filter = ('created_by',) + self.list_filter
        return super(ResaleApartmentAdmin, self).changelist_view(
            request, extra_context)

admin.site.register(ResaleCharacteristic)
<commit_msg>Use reusable MultiuploadInlinesContainerMixin in resale.<commit_after>import re
from django.contrib import admin, messages
from django.utils.translation import ugettext_lazy as _
from django.utils.text import mark_safe

from adminsortable2.admin import SortableInlineAdminMixin

from real_estate.admin import AdminInlineImages, MultiuploadInlinesContainerMixin

from .models import ResaleApartment, ResaleApartmentImage, ResaleCharacteristic
from .forms import ResaleApartmentForm, ResaleApartmentImageForm

class ResaleImageInline(
        SortableInlineAdminMixin, admin.TabularInline, AdminInlineImages):
    model = ResaleApartmentImage
    form = ResaleApartmentImageForm
    classes = ['collapse', ]
    extra = 0
    min_num = 0
    fields = ('thumbnail', 'image')
    readonly_fields = ('thumbnail', )

@admin.register(ResaleApartment)
class ResaleApartmentAdmin(MultiuploadInlinesContainerMixin, admin.ModelAdmin):
    inlines = [ResaleImageInline, ]
    form = ResaleApartmentForm

    list_display_initial = (
        'id', 'status', 'is_active', 'rooms', 'total_area',
        'full_price', 'agency_price', 'fee', 'price',
        'residental_complex',
    )

    list_filter_initial = ('rooms', 'residental_complex', )

    list_display = list_display_initial
    list_filter = list_filter_initial
    list_display_links = (
        'id', 'status', 'rooms', 'total_area'
    )
    list_editable = ('is_active', )
    readonly_fields = ['id', 'date_added', 'modified_by']
    filter_horizontal = ['characteristics']

    def fee(self, obj):
        return obj.fee
    fee.short_description = _('комиссия')

    def full_price(self, obj):
        return obj.full_price
    full_price.short_description = _("текущая стоимость")

    fieldsets = None

    # For multiupload by MultiuploadInlinesContainerMixin
    related_inline_form = ResaleImageInline
    related_inline_fk = 'apartment'

    def get_fieldsets(self, request, obj=None):
        self.fieldsets = self.dynamic_fieldset(request)
        return super().get_fieldsets(request, obj)

    def dynamic_fieldset(self, request):
        """
        get the dynamic field sets
        """
        deal_status_part_fields = (
            'date_added',
            ('sold_date', 'previous_buy_date',),
            'status',
            'amount_of_owners',
            'comment',
            'related_mortgage',
            'price',
            'agency_price',
            'agency_price_with_sales',
        )
        if request.user.has_perm('resale.can_add_change_delete_all_resale'):
            deal_status_part_fields += ('created_by', 'modified_by',)
        deal_status_part = (_('Информация по сделке'), {
            'classes': ('collapse',),
            'fields': deal_status_part_fields,
        })

        owner_part = (_('Информация по продавцу'), {
            'classes': ('collapse',),
            'fields': (('owner_name', 'owner_phone_number'),),
        })

        apartment_part_fields = (
            'neighbourhood', 'residental_complex', 'street',
            ('building', 'building_block',), 'coordinates',
            'rooms', ('floor', 'number_of_storeys'),
            'section', 'apartment_number', 'building_type',
            'home_series', 'date_of_construction',
            ('total_area', 'kitchen_area', 'balcony_area',),
            ('celling_height', 'interior_decoration',),
            'layout', 'description', 'characteristics'
        )
        apartment_part = (_('Информация по квартире'), {
            'classes': ('collapse',),
            'fields': apartment_part_fields,
        })

        fieldsets = (
            (None, {
                'fields': ('id', 'is_active',),
            }),
            deal_status_part,
            owner_part,
            apartment_part,
        )

        return fieldsets

    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        # Auto set user
        if not change:
            obj.created_by = request.user
        if 'modified_by' not in form.changed_data:
            obj.modified_by = request.user
        obj.save()

    def get_queryset(self, request):
        qs = super(ResaleApartmentAdmin, self).get_queryset(request)
        if request.user.has_perm('resale.can_add_change_delete_all_resale'):
            return qs
        return qs.filter(created_by=request.user)

    def changelist_view(self, request, extra_context=None):
        self.list_display = self.list_display_initial
        self.list_filter = self.list_filter_initial
        if request.user.has_perm('resale.can_add_change_delete_all_resale'):
            self.list_display += ('created_by',)
            self.list_filter = ('created_by',) + self.list_filter
        return super(ResaleApartmentAdmin, self).changelist_view(
            request, extra_context)

admin.site.register(ResaleCharacteristic)
<|endoftext|>


<commit_before>"""
Re-usable view components
"""
import functools
import json
import datetime

from django.utils.dateformat import format
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.core.serializers.json import DjangoJSONEncoder
from rest_framework import mixins, viewsets
from django.conf import settings

class LoginRequiredMixin(object):
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

class OpalSerializer(DjangoJSONEncoder):
    def default(self, o):
        if isinstance(o, datetime.datetime):
            return format(o, settings.DATETIME_FORMAT)
        elif isinstance(o, datetime.date):
            return format(
                datetime.datetime.combine(
                    o, datetime.datetime.min.time()
                ), settings.DATE_FORMAT
            )
        super(OpalSerializer, self).default(o)

def _get_request_data(request):
    data = request.read()
    return json.loads(data)

def _build_json_response(data, status_code=200):
    response = HttpResponse()
    response['Content-Type'] = 'application/json'
    response.content = json.dumps(data, cls=OpalSerializer)
    # response.content = ''+json.dumps(data, cls=DjangoJSONEncoder)+''
    response.status_code = status_code
    return response

def with_no_caching(view):
    @functools.wraps(view)
    def no_cache(*args, **kw):
        response = view(*args, **kw)
        response['Cache-Control'] = 'no-cache'
        response['Pragma'] = 'no-cache'
        response['Expires'] = '-1'
        return response

    return no_cache

class ModelViewSet(
        mixins.CreateModelMixin,
        mixins.RetrieveModelMixin,
        mixins.ListModelMixin,
        viewsets.GenericViewSet):
    pass
<commit_msg>decode() data coming from request.read() in utils._get_request_data<commit_after>"""
Re-usable view components
"""
import functools
import json
import datetime

from django.utils.dateformat import format
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.core.serializers.json import DjangoJSONEncoder
from rest_framework import mixins, viewsets
from django.conf import settings

class LoginRequiredMixin(object):
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

class OpalSerializer(DjangoJSONEncoder):
    def default(self, o):
        if isinstance(o, datetime.datetime):
            return format(o, settings.DATETIME_FORMAT)
        elif isinstance(o, datetime.date):
            return format(
                datetime.datetime.combine(
                    o, datetime.datetime.min.time()
                ), settings.DATE_FORMAT
            )
        super(OpalSerializer, self).default(o)

def _get_request_data(request):
    data = request.read().decode('UTF-8')
    return json.loads(data)

def _build_json_response(data, status_code=200):
    response = HttpResponse()
    response['Content-Type'] = 'application/json'
    response.content = json.dumps(data, cls=OpalSerializer)
    # response.content = ''+json.dumps(data, cls=DjangoJSONEncoder)+''
    response.status_code = status_code
    return response

def with_no_caching(view):
    @functools.wraps(view)
    def no_cache(*args, **kw):
        response = view(*args, **kw)
        response['Cache-Control'] = 'no-cache'
        response['Pragma'] = 'no-cache'
        response['Expires'] = '-1'
        return response

    return no_cache

class ModelViewSet(
        mixins.CreateModelMixin,
        mixins.RetrieveModelMixin,
        mixins.ListModelMixin,
        viewsets.GenericViewSet):
    pass
<|endoftext|>


<commit_before>from ..utils import *

##
# Minions

# Water Elemental
class CS2_033:
	def DAMAGE(self, source, target, amount):
		if source is self:
			return [Freeze(target)]

# Ethereal Arcanist
class EX1_274:
	def OWN_TURN_END(self):
		if self.controller.secrets:
			return [Buff(SELF, "EX1_274e")]

# Archmage Antonidas
class EX1_559:
	def OWN_CARD_PLAYED(self, card):
		if card.type == CardType.SPELL:
			return [Give(CONTROLLER, "CS2_029")]

# Kirin Tor Mage
class EX1_612:
	action = [Buff(FRIENDLY_HERO, "EX1_612o")]

class EX1_612oa:
	cost = lambda self, i: 0

# Mana Wyrm
class NEW1_012:
	def OWN_CARD_PLAYED(self, card):
		if card.type == CardType.SPELL:
			return [Buff(SELF, "NEW1_012o")]

##
# Spells

# Polymorph
class CS2_022:
	action = [Morph(TARGET, "CS2_tk1")]

# Arcane Intellect
class CS2_023:
	action = [Draw(CONTROLLER, 2)]

# Frostbolt
class CS2_024:
	action = [Hit(TARGET, 3), Freeze(TARGET)]

# Arcane Explosion
class CS2_025:
	action = [Hit(ENEMY_MINIONS, 1)]

# Frost Nova
class CS2_026:
	action = [Freeze(ENEMY_MINIONS)]

# Mirror Image
class CS2_027:
	action = [Summon(CONTROLLER, "CS2_mirror"), Summon(CONTROLLER, "CS2_mirror")]

# Blizzard
class CS2_028:
	action = [Hit(ENEMY_MINIONS, 2), Freeze(ENEMY_MINIONS)]

# Fireball
class CS2_029:
	action = [Hit(TARGET, 6)]

# Ice Lance
class CS2_031:
	def action(self, target):
		if target.frozen:
			return [Hit(TARGET, 4)]
		return [Freeze(TARGET)]

# Flamestrike
class CS2_032:
	action = [Hit(ENEMY_MINIONS, 4)]

# Cone of Cold
class EX1_275:
	action = [Hit(TARGET | TARGET_ADJACENT, 1), Freeze(TARGET | TARGET_ADJACENT)]

# Arcane Missiles
class EX1_277:
	def action(self):
		count = 3 + self.controller.spellpower
		return [Hit(RANDOM_ENEMY_CHARACTER, 1) * count]

# Pyroblast
class EX1_279:
	action = [Hit(TARGET, 10)]

##
# Secrets

# Ice Barrier
class EX1_289:
	def BEFORE_ATTACK(self, source, target):
		if target == self.controller.hero:
			return [GainArmor(FRIENDLY_HERO, 8), Reveal(SELF)]

# Vaporize
class EX1_594:
	def ATTACK(self, source, target):
		if target == self.controller.hero and source.type == CardType.MINION:
			return [Destroy(source), Reveal(SELF)]
<commit_msg>Move Ice Barrier to the ATTACK event<commit_after>from ..utils import *

##
# Minions

# Water Elemental
class CS2_033:
	def DAMAGE(self, source, target, amount):
		if source is self:
			return [Freeze(target)]

# Ethereal Arcanist
class EX1_274:
	def OWN_TURN_END(self):
		if self.controller.secrets:
			return [Buff(SELF, "EX1_274e")]

# Archmage Antonidas
class EX1_559:
	def OWN_CARD_PLAYED(self, card):
		if card.type == CardType.SPELL:
			return [Give(CONTROLLER, "CS2_029")]

# Kirin Tor Mage
class EX1_612:
	action = [Buff(FRIENDLY_HERO, "EX1_612o")]

class EX1_612oa:
	cost = lambda self, i: 0

# Mana Wyrm
class NEW1_012:
	def OWN_CARD_PLAYED(self, card):
		if card.type == CardType.SPELL:
			return [Buff(SELF, "NEW1_012o")]

##
# Spells

# Polymorph
class CS2_022:
	action = [Morph(TARGET, "CS2_tk1")]

# Arcane Intellect
class CS2_023:
	action = [Draw(CONTROLLER, 2)]

# Frostbolt
class CS2_024:
	action = [Hit(TARGET, 3), Freeze(TARGET)]

# Arcane Explosion
class CS2_025:
	action = [Hit(ENEMY_MINIONS, 1)]

# Frost Nova
class CS2_026:
	action = [Freeze(ENEMY_MINIONS)]

# Mirror Image
class CS2_027:
	action = [Summon(CONTROLLER, "CS2_mirror"), Summon(CONTROLLER, "CS2_mirror")]

# Blizzard
class CS2_028:
	action = [Hit(ENEMY_MINIONS, 2), Freeze(ENEMY_MINIONS)]

# Fireball
class CS2_029:
	action = [Hit(TARGET, 6)]

# Ice Lance
class CS2_031:
	def action(self, target):
		if target.frozen:
			return [Hit(TARGET, 4)]
		return [Freeze(TARGET)]

# Flamestrike
class CS2_032:
	action = [Hit(ENEMY_MINIONS, 4)]

# Cone of Cold
class EX1_275:
	action = [Hit(TARGET | TARGET_ADJACENT, 1), Freeze(TARGET | TARGET_ADJACENT)]

# Arcane Missiles
class EX1_277:
	def action(self):
		count = 3 + self.controller.spellpower
		return [Hit(RANDOM_ENEMY_CHARACTER, 1) * count]

# Pyroblast
class EX1_279:
	action = [Hit(TARGET, 10)]

##
# Secrets

# Ice Barrier
class EX1_289:
	def ATTACK(self, source, target):
		if target == self.controller.hero:
			return [GainArmor(FRIENDLY_HERO, 8), Reveal(SELF)]

# Vaporize
class EX1_594:
	def ATTACK(self, source, target):
		if target == self.controller.hero and source.type == CardType.MINION:
			return [Destroy(source), Reveal(SELF)]
<|endoftext|>


<commit_before>"""
A simple binary heap implementation (using a list).

Operations:
- __len__
- insert
- pop
- peek

TODO:
- Add operations: heapify (maybe in __init__)

Author:
  Christos Nitsas
  (nitsas)
  (chrisnitsas)

Language:
  Python 3(.4)

Date:
  November, 2014
"""

import operator

__all__ = ['BinaryHeap', 'heapify']

def heapify(list_, max_=False):
    """
    Turn a list into a binary heap in place, in linear time.

    list_ -- a list of items
    max_ -- if True, make a max-heap; min-heap otherwise (default)

    With the default `max_` parameter the lowest valued items are placed
    "higher" in the heap (the lowest valued item is the one returned by
    `sorted(list(items))[0]`), i.e. the list is turned into a min-heap. Users
    must set the named parameter `max_=True` if they want a max-heap.

    A typical pattern for items is a tuple in the form:
    (priority_number, data)
    """
    n = len(list_)
    if max_:
        less = operator.gt
    else:
        less = operator.lt
    for i in reversed(range(n//2)):
        _shift_down(list_, i, less)

def _swap(list_, a, b):
    """
    Swap items in positions a and b of list_.

    list_ -- a list
    a -- an index in list_
    b -- an index in list_
    """
    list_[a], list_[b] = list_[b], list_[a]

def _shift_up(list_, index, less):
    """
    Move a heap node up in the heap, as long as needed.

    list_ -- the heap as a list
    index -- the index of the node in list_
    less -- the callable we'll use to compare items
    """
    parent = (index - 1) // 2
    while index > 0 and not less(list_[parent], list_[index]):
        # swap item with its parent
        _swap(list_, index, parent)
        # update position pointers
        index = parent
        parent = (index - 1) // 2

def _shift_down(list_, index, less):
    """
    Move a heap node down in the heap, as long as needed.

    list_ -- the heap as a list
    index -- the index of the node in list_
    less -- the callable we'll use to compare items
    """
    # initialize the positions of the node's children
    # left child
    left = 2 * index + 1
    # right child
    right = left + 1
    # does the node actually have (both) children?
    if left >= len(list_):
        # no children
        return
    elif right >= len(list_):
        # only left child
        if less(list_[left], list_[index]):
            _swap(list_, index, left)
        return
    # node has two children
    while less(list_[left], list_[index]) or less(list_[right], list_[index]):
        # the item is *less* than at least one of its children
        # swap it with the *smallest* of its children
        if less(list_[left], list_[right]):
            _swap(list_, index, left)
            index = left
        else:
            _swap(list_, index, right)
            index = right
        # update child position pointers
        left = 2 * index + 1
        right = left + 1
        # does the node actually have (both) children?
        if left >= len(list_):
            # no children
            return
        elif right >= len(list_):
            # only left child
            if less(list_[left], list_[index]):
                _swap(list_, index, left)
            return

class BinaryHeap:
    """
    A simple binary heap implementation (using a list).

    A typical pattern for items is a tuple in the form:
    (priority_number, data).
    """

    def __init__(self, list_=None, max_=False):
        """
        Initialize an empty heap.

        list_ -- a list of initial items; this won't be copied, just wrapped
                 and heapified; careful: mutating the list outside the heap's
                 interface will probably break the heap property
        max_ -- if True, make a max-heap; min-heap otherwise (default)

        By default the lowest valued items are retrieved first (the lowest
        valued item is the one returned by `sorted(list(items))[0]`). Users
        must set the named parameter `max_=True` if they want a max-heap.

        A typical pattern for items is a tuple in the form:
        (priority_number, data)
        """
        if max_:
            self._less = operator.gt
        else:
            self._less = operator.lt
        if list_ is not None:
            self._items = list_
            heapify(self._items, max_)
        else:
            self._items = []

    def __len__(self):
        """Return the number of items in the heap as an int."""
        return len(self._items)

    def insert(self, item):
        """
        Insert a new item.

        item -- the item to be inserted

        A typical pattern for items is a tuple in the form:
        (priority_number, data)

        This operation's time complexity is `O(log(n))`, where `n` is the
        number of items in the heap.
        """
        # insert item at the end of the list of items
        self._items.append(item)
        # shift the item up as needed to restore the heap property
        _shift_up(self._items, len(self._items) - 1, self._less)

    def peek(self):
        """
        Return the item on top of the heap without removing the item.

        Return the item with the *lowest* value according to the heap's
        (partial) ordering (e.g. the min item if we have a min heap).

        Raises a `LookupError('peek into empty heap')` if the heap is empty.
        """
        if len(self._items) == 0:
            raise LookupError('peek into empty heap')
        return self._items[0]

    def pop(self):
        """
        Remove and return the item that's currently on top of the heap.

        Remove and return the item with the *lowest* value according to the
        heap's (partial) ordering (e.g. the min item if we have a min heap).

        Raises a `LookupError('pop from empty heap')` if the heap is empty.
        """
        if len(self._items) == 0:
            raise LookupError('pop from empty heap')
        # else:
        # swap top item with the last item of self._items, and remove it
        _swap(self._items, 0, -1)
        min_item = self._items.pop()
        # now repair the heap property
        _shift_down(self._items, 0, self._less)
        # return
        return min_item
<commit_msg>[binary_heap] Make _shift_down a bit faster<commit_after>"""
A simple binary heap implementation (using a list).

Operations:
- __len__
- insert
- pop
- peek

TODO:
- Add operations: heapify (maybe in __init__)

Author:
  Christos Nitsas
  (nitsas)
  (chrisnitsas)

Language:
  Python 3(.4)

Date:
  November, 2014
"""

import operator

__all__ = ['BinaryHeap', 'heapify']

def heapify(list_, max_=False):
    """
    Turn a list into a binary heap in place, in linear time.

    list_ -- a list of items
    max_ -- if True, make a max-heap; min-heap otherwise (default)

    With the default `max_` parameter the lowest valued items are placed
    "higher" in the heap (the lowest valued item is the one returned by
    `sorted(list(items))[0]`), i.e. the list is turned into a min-heap. Users
    must set the named parameter `max_=True` if they want a max-heap.

    A typical pattern for items is a tuple in the form:
    (priority_number, data)
    """
    n = len(list_)
    if max_:
        less = operator.gt
    else:
        less = operator.lt
    for i in reversed(range(n//2)):
        _shift_down(list_, i, less)

def _swap(list_, a, b):
    """
    Swap items in positions a and b of list_.

    list_ -- a list
    a -- an index in list_
    b -- an index in list_
    """
    list_[a], list_[b] = list_[b], list_[a]

def _shift_up(list_, index, less):
    """
    Move a heap node up in the heap, as long as needed.

    list_ -- the heap as a list
    index -- the index of the node in list_
    less -- the callable we'll use to compare items
    """
    parent = (index - 1) // 2
    while index > 0 and not less(list_[parent], list_[index]):
        # swap item with its parent
        _swap(list_, index, parent)
        # update position pointers
        index = parent
        parent = (index - 1) // 2

def _shift_down(list_, index, less):
    """
    Move a heap node down in the heap, as long as needed.

    list_ -- the heap as a list
    index -- the index of the node in list_
    less -- the callable we'll use to compare items
    """
    # initialize the positions of the node's children
    # left child
    left = 2 * index + 1
    # right child
    right = left + 1
    # get the min child (ignore right if it does not exist)
    try:
        if less(list_[right], list_[left]):
            min_child = right
        else:
            min_child = left
    except IndexError:
        if left < len(list_):
            min_child = left
        else:
            return
    while less(list_[min_child], list_[index]):
        # the item is *less* than at least one of its children
        # swap it with the *smallest* of its children
        _swap(list_, index, min_child)
        index = min_child
        # update child position pointers
        left = 2 * index + 1
        right = left + 1
        # get the min child (ignore right if it does not exist)
        try:
            if less(list_[right], list_[left]):
                min_child = right
            else:
                min_child = left
        except IndexError:
            if left < len(list_):
                min_child = left
            else:
                return

class BinaryHeap:
    """
    A simple binary heap implementation (using a list).

    A typical pattern for items is a tuple in the form:
    (priority_number, data).
    """

    def __init__(self, list_=None, max_=False):
        """
        Initialize an empty heap.

        list_ -- a list of initial items; this won't be copied, just wrapped
                 and heapified; careful: mutating the list outside the heap's
                 interface will probably break the heap property
        max_ -- if True, make a max-heap; min-heap otherwise (default)

        By default the lowest valued items are retrieved first (the lowest
        valued item is the one returned by `sorted(list(items))[0]`). Users
        must set the named parameter `max_=True` if they want a max-heap.

        A typical pattern for items is a tuple in the form:
        (priority_number, data)
        """
        if max_:
            self._less = operator.gt
        else:
            self._less = operator.lt
        if list_ is not None:
            self._items = list_
            heapify(self._items, max_)
        else:
            self._items = []

    def __len__(self):
        """Return the number of items in the heap as an int."""
        return len(self._items)

    def insert(self, item):
        """
        Insert a new item.

        item -- the item to be inserted

        A typical pattern for items is a tuple in the form:
        (priority_number, data)

        This operation's time complexity is `O(log(n))`, where `n` is the
        number of items in the heap.
        """
        # insert item at the end of the list of items
        self._items.append(item)
        # shift the item up as needed to restore the heap property
        _shift_up(self._items, len(self._items) - 1, self._less)

    def peek(self):
        """
        Return the item on top of the heap without removing the item.

        Return the item with the *lowest* value according to the heap's
        (partial) ordering (e.g. the min item if we have a min heap).

        Raises a `LookupError('peek into empty heap')` if the heap is empty.
        """
        if len(self._items) == 0:
            raise LookupError('peek into empty heap')
        return self._items[0]

    def pop(self):
        """
        Remove and return the item that's currently on top of the heap.

        Remove and return the item with the *lowest* value according to the
        heap's (partial) ordering (e.g. the min item if we have a min heap).

        Raises a `LookupError('pop from empty heap')` if the heap is empty.
        """
        if len(self._items) == 0:
            raise LookupError('pop from empty heap')
        # else:
        # swap top item with the last item of self._items, and remove it
        _swap(self._items, 0, -1)
        min_item = self._items.pop()
        # now repair the heap property
        _shift_down(self._items, 0, self._less)
        # return
        return min_item
<|endoftext|>


<commit_before>from mongoengine import EmbeddedDocument, DynamicDocument
from mongoengine.fields import (
    BooleanField,
    DateTimeField,
    DictField,
    EmbeddedDocumentField,
    IntField,
    ListField,
    StringField,
    ReferenceField,
)
from mongoengine.queryset import CASCADE

from openelex.us import STATE_POSTALS

class Office(EmbeddedDocument):
    state = StringField(choices=STATE_POSTALS)
    name = StringField()
    district = StringField()

class Contest(DynamicDocument):
    created = DateTimeField()
    updated = DateTimeField()
    source = StringField(required=True, help_text="Name of data source (preferably from datasource.py). NOTE: this could be a single file among many for a given state, if results are split into different files by reporting level")
    election_id = StringField(required=True, help_text="election id, e.g. md-2012-11-06-general")
    slug = StringField(required=True, help_text="Slugified office name, plus district and party if relevant")
    state = StringField(required=True, choices=STATE_POSTALS)
    start_date = DateTimeField(required=True)
    end_date = DateTimeField(required=True)
    election_type = StringField(help_text="general, primary, etc. from OpenElex metadata")
    result_type = StringField(required=True)
    special = BooleanField(default=False)
    raw_office = StringField(required=True)
    raw_district = StringField()
    raw_party = StringField(help_text="This should only be assigned for closed primaries, where voters must be registered in party to vote in the contest")

    # FIELDS FOR TRANSFORMED/LINKED DATA
    office = EmbeddedDocumentField(Office)
    district = StringField()
    party = StringField(help_text="This should only be assigned for closed primaries, where voters must be registered in party to vote in the contest")

    def __unicode__(self):
        return u'%s-%s' % self.key

    @property
    def key(self):
        return (self.election_id, self.slug)

class Candidate(DynamicDocument):
    """
    State is included because in nearly all cases, a candidate is unique to a state (presidential candidates run in multiple states,
    but hail from a single state). This would help with lookups and prevent duplicates. Identifiers is a DictField because a candidate
    may have 0 or more identifiers, including state-level IDs.

    parties = ['Democratic', 'Republican']

    identifiers = {
        'bioguide' : <bioguide_id>,
        'fec' : [<fecid_1>, <fecid_2>, ...],
        'votesmart' : <votesmart_id>,
        ...
    }

    """
    source = StringField(required=True, help_text="Name of data source (preferably from datasource.py). NOTE: this could be a single file among many for a given state, if results are split into different files by reporting level")
    election_id = StringField(required=True, help_text="election id, e.g. md-2012-11-06-general")
    contest = ReferenceField(Contest, reverse_delete_rule=CASCADE, required=True)
    contest_slug = StringField(required=True, help_text="Denormalized contest slug for easier querying and obj repr")
    state = StringField(required=True, choices=STATE_POSTALS)
    #TODO: Add validation to require raw_full_name or raw_family_name
    raw_full_name = StringField(max_length=300)
    slug = StringField(max_length=300, required=True, help_text="Slugified name for easier querying and obj repr")
    raw_given_name = StringField(max_length=200)
    raw_family_name = StringField(max_length=200)
    raw_suffix = StringField(max_length=200)
    raw_additional_name = StringField(max_length=200, help_text="For middle names, nicknames, etc")
    raw_parties = ListField(StringField(), default=list)

    # FIELDS FOR TRANSFORMED/CLEANED DATA or LINKS TO OTHER DATA SETS
    #name = StringField(max_length=300, required=True)
    given_name = StringField(max_length=200)
    family_name = StringField(max_length=200)
    suffix = StringField(max_length=200)
    additional_name = StringField(max_length=200, help_text="For middle names, nicknames, etc")
    other_names = ListField(StringField(), default=list)
    parties = ListField(StringField(), default=list) # normalized? abbreviations?
    identifiers = DictField()

    def __unicode__(self):
        name =  u'%s - %s' % (self.contest_slug, self.name)
        parties = ""
        if self.raw_parties:
            parties = ", ".join([party for party in self.raw_parties])
            if parties:
                name += " (%s)" % parties
        return name

    @property
    def name(self):
        if self.raw_full_name:
            name =  self.raw_full_name
        else:
            name = "%s, %s" % (self.raw_family_name, self.raw_given_name)
            if self.raw_suffix:
                name += " %s" % self.raw_suff
        return name

    @property
    def key(self):
        return (self.election_id, self.contest_slug, self.slug)

class Result(DynamicDocument):

    REPORTING_LEVEL_CHOICES = (
        'state',
        'congressional_district',
        'state_legislative',
        'county',
        'precinct',
        'parish',
    )
    source = StringField(required=True, help_text="Name of data source for this file, preferably standardized filename from datasource.py")
    election_id = StringField(required=True, help_text="election id, e.g. md-2012-11-06-general")
    state = StringField(required=True, choices=STATE_POSTALS)
    reporting_level = StringField(required=True, choices=REPORTING_LEVEL_CHOICES)
    contest = ReferenceField(Contest, reverse_delete_rule=CASCADE, required=True)
    contest_slug = StringField(required=True, help_text="Denormalized contest slug for easier querying and obj repr")
    candidate = ReferenceField(Candidate, reverse_delete_rule=CASCADE, required=True)
    candidate_slug = StringField(required=True, help_text="Denormalized candidate slug for easier querying and obj repr")
    ocd_id = StringField()
    #TODO: Add validation to require raw_jurisdiction or jurisdiction
    raw_jurisdiction = StringField(help_text="Political geography from raw results, if present. E.g. county name, congressional district, precinct number.")
    raw_total_votes = IntField(required=True)
    raw_winner = StringField()
    raw_write_in = StringField()
    raw_vote_breakdowns = DictField(help_text="If provided, store vote totals for election day, absentee, provisional, etc.")

    jurisdiction = StringField(help_text="Derived/standardized political geography, typically when not found in raw results.")
    total_votes = IntField()
    winner = BooleanField(help_text="Winner as determined by OpenElex, if not provided natively in data")
    write_in = BooleanField()
    #vote_breakdowns = DictField(help_text="If provided, store vote totals for election day, absentee, provisional, etc.")

    def __unicode__(self):
        bits = (
            self.election_id,
            self.contest_slug,
            self.candidate_slug,
            self.reporting_level,
            self.raw_jurisdiction,
            self.raw_total_votes,
        )
        return u'%s-%s-%s-%s-%s (%s)' % bits
<commit_msg>Add indexes on election_id<commit_after>from mongoengine import EmbeddedDocument, DynamicDocument
from mongoengine.fields import (
    BooleanField,
    DateTimeField,
    DictField,
    EmbeddedDocumentField,
    IntField,
    ListField,
    StringField,
    ReferenceField,
)
from mongoengine.queryset import CASCADE

from openelex.us import STATE_POSTALS

class Office(EmbeddedDocument):
    state = StringField(choices=STATE_POSTALS)
    name = StringField()
    district = StringField()

class Contest(DynamicDocument):
    created = DateTimeField()
    updated = DateTimeField()
    source = StringField(required=True, help_text="Name of data source (preferably from datasource.py). NOTE: this could be a single file among many for a given state, if results are split into different files by reporting level")
    election_id = StringField(required=True, help_text="election id, e.g. md-2012-11-06-general")
    slug = StringField(required=True, help_text="Slugified office name, plus district and party if relevant")
    state = StringField(required=True, choices=STATE_POSTALS)
    start_date = DateTimeField(required=True)
    end_date = DateTimeField(required=True)
    election_type = StringField(help_text="general, primary, etc. from OpenElex metadata")
    result_type = StringField(required=True)
    special = BooleanField(default=False)
    raw_office = StringField(required=True)
    raw_district = StringField()
    raw_party = StringField(help_text="This should only be assigned for closed primaries, where voters must be registered in party to vote in the contest")

    # FIELDS FOR TRANSFORMED/LINKED DATA
    office = EmbeddedDocumentField(Office)
    district = StringField()
    party = StringField(help_text="This should only be assigned for closed primaries, where voters must be registered in party to vote in the contest")

    meta = {
        'indexes': ['election_id',],
    }

    def __unicode__(self):
        return u'%s-%s' % self.key

    @property
    def key(self):
        return (self.election_id, self.slug)

class Candidate(DynamicDocument):
    """
    State is included because in nearly all cases, a candidate is unique to a state (presidential candidates run in multiple states,
    but hail from a single state). This would help with lookups and prevent duplicates. Identifiers is a DictField because a candidate
    may have 0 or more identifiers, including state-level IDs.

    parties = ['Democratic', 'Republican']

    identifiers = {
        'bioguide' : <bioguide_id>,
        'fec' : [<fecid_1>, <fecid_2>, ...],
        'votesmart' : <votesmart_id>,
        ...
    }

    """
    source = StringField(required=True, help_text="Name of data source (preferably from datasource.py). NOTE: this could be a single file among many for a given state, if results are split into different files by reporting level")
    election_id = StringField(required=True, help_text="election id, e.g. md-2012-11-06-general")
    contest = ReferenceField(Contest, reverse_delete_rule=CASCADE, required=True)
    contest_slug = StringField(required=True, help_text="Denormalized contest slug for easier querying and obj repr")
    state = StringField(required=True, choices=STATE_POSTALS)
    #TODO: Add validation to require raw_full_name or raw_family_name
    raw_full_name = StringField(max_length=300)
    slug = StringField(max_length=300, required=True, help_text="Slugified name for easier querying and obj repr")
    raw_given_name = StringField(max_length=200)
    raw_family_name = StringField(max_length=200)
    raw_suffix = StringField(max_length=200)
    raw_additional_name = StringField(max_length=200, help_text="For middle names, nicknames, etc")
    raw_parties = ListField(StringField(), default=list)

    # FIELDS FOR TRANSFORMED/CLEANED DATA or LINKS TO OTHER DATA SETS
    #name = StringField(max_length=300, required=True)
    given_name = StringField(max_length=200)
    family_name = StringField(max_length=200)
    suffix = StringField(max_length=200)
    additional_name = StringField(max_length=200, help_text="For middle names, nicknames, etc")
    other_names = ListField(StringField(), default=list)
    parties = ListField(StringField(), default=list) # normalized? abbreviations?
    identifiers = DictField()

    meta = {
        'indexes': ['election_id',],
    }

    def __unicode__(self):
        name =  u'%s - %s' % (self.contest_slug, self.name)
        parties = ""
        if self.raw_parties:
            parties = ", ".join([party for party in self.raw_parties])
            if parties:
                name += " (%s)" % parties
        return name

    @property
    def name(self):
        if self.raw_full_name:
            name =  self.raw_full_name
        else:
            name = "%s, %s" % (self.raw_family_name, self.raw_given_name)
            if self.raw_suffix:
                name += " %s" % self.raw_suff
        return name

    @property
    def key(self):
        return (self.election_id, self.contest_slug, self.slug)

class Result(DynamicDocument):

    REPORTING_LEVEL_CHOICES = (
        'state',
        'congressional_district',
        'state_legislative',
        'county',
        'precinct',
        'parish',
    )
    source = StringField(required=True, help_text="Name of data source for this file, preferably standardized filename from datasource.py")
    election_id = StringField(required=True, help_text="election id, e.g. md-2012-11-06-general")
    state = StringField(required=True, choices=STATE_POSTALS)
    reporting_level = StringField(required=True, choices=REPORTING_LEVEL_CHOICES)
    contest = ReferenceField(Contest, reverse_delete_rule=CASCADE, required=True)
    contest_slug = StringField(required=True, help_text="Denormalized contest slug for easier querying and obj repr")
    candidate = ReferenceField(Candidate, reverse_delete_rule=CASCADE, required=True)
    candidate_slug = StringField(required=True, help_text="Denormalized candidate slug for easier querying and obj repr")
    ocd_id = StringField()
    #TODO: Add validation to require raw_jurisdiction or jurisdiction
    raw_jurisdiction = StringField(help_text="Political geography from raw results, if present. E.g. county name, congressional district, precinct number.")
    raw_total_votes = IntField(required=True)
    raw_winner = StringField()
    raw_write_in = StringField()
    raw_vote_breakdowns = DictField(help_text="If provided, store vote totals for election day, absentee, provisional, etc.")

    jurisdiction = StringField(help_text="Derived/standardized political geography, typically when not found in raw results.")
    total_votes = IntField()
    winner = BooleanField(help_text="Winner as determined by OpenElex, if not provided natively in data")
    write_in = BooleanField()
    #vote_breakdowns = DictField(help_text="If provided, store vote totals for election day, absentee, provisional, etc.")

    meta = {
        'indexes': ['election_id',],
    }

    def __unicode__(self):
        bits = (
            self.election_id,
            self.contest_slug,
            self.candidate_slug,
            self.reporting_level,
            self.raw_jurisdiction,
            self.raw_total_votes,
        )
        return u'%s-%s-%s-%s-%s (%s)' % bits
<|endoftext|>


<commit_before>from flask import Flask
from flask import request, jsonify
import pickle
import base64
import png
import io
import requests
import numpy as np
from adv_cnn.model import is_admin, do_adver

app = Flask(__name__)

def get_adv(face, mod_face):
    red1 = face[:,:,0]
    green1 = face[:,:,1]
    blue1 = face[:,:,2]
    red2 = mod_face[:,:,0]
    green2 = mod_face[:,:,1]
    blue2 = mod_face[:,:,2]
    diff_red = red1-red2
    diff_green = green1-green2
    diff_blue =blue1-blue2
    min_red = diff_red.min()
    max_red = diff_red.max()
    min_green = diff_green.min()
    max_green = diff_green.max()
    min_blue = diff_blue.min()
    max_blue = diff_blue.max()
    diff_red = (diff_red-min_red)*(255/max_red)
    diff_green = (diff_green-min_green)*(255/max_green)
    diff_blue = (diff_blue-min_blue)*(255/max_blue)
    diff = np.zeros_like(face)
    diff[:,:,0] = diff_red
    diff[:,:,1] = diff_green
    diff[:,:,2] = diff_blue
    return diff

def sanitise_image(mat):
    int_mat = np.rint(mat).astype(int)
    return np.clip(int_mat, 0, 255).astype(np.uint8)

def publish_image(face_im, adv_im, combined_im, confidence=0.0, hack=False):
    """convert png; base64 encode that and post to stat server"""
    # Do face
    text_buf = io.BytesIO()
    png.from_array(face_im, 'RGB').save(text_buf)
    encoded_face = b"data:image/png;base64,"+base64.b64encode(text_buf.getvalue(),b'#/')

    # Do adv
    text_buf = io.BytesIO()
    png.from_array(adv_im, 'RGB').save(text_buf)
    encoded_adv = b"data:image/png;base64," + base64.b64encode(text_buf.getvalue(),b'#/')

    # Do combined
    text_buf = io.BytesIO()
    png.from_array(combined_im, 'RGB').save(text_buf)
    encoded_combined = b"data:image/png;base64," + base64.b64encode(text_buf.getvalue(),b'#/')

    url = "http://facejack.westeurope.cloudapp.azure.com:5000/push_stats"
    if hack:
        payload = b"adversarial=yes&original_img=" + encoded_face + \
                  b"&adv_mod_img=" + encoded_adv + \
                  b"&modified_img=" + encoded_combined + \
                  b"&confidence=" + str(confidence * 100).encode()
    else:
        payload = b"adversarial=no&original_img="+encoded_face+\
                  b"&adv_mod_img="+encoded_adv+\
                  b"&modified_img="+encoded_combined+\
                  b"&confidence="+str(confidence*100).encode()
    headers = {
        'content-type': "application/x-www-form-urlencoded",
        'cache-control': "no-cache"
    }

    response = requests.request("POST", url, data=payload, headers=headers, )

def proc_face(face):
    """
    :param face: np.array with face in it
    :return: bool: true for admin and false otherwise
    """
    print("PROC_FACE")
    # time.sleep(3)
    # publish_image(face, face, face)
    res, conf = is_admin(face)
    publish_image(face, np.ones_like(face)*255, face, conf)
    return res

def proc_face_with_hack(face):
    print("MAJOR HACK IN PROGRESS")
    for face1, confidence in do_adver(face):
        face1 = sanitise_image(face1)
        publish_image(face, get_adv(face, face1), face1, confidence, hack=True)
    return proc_face(face1)

@app.route('/')
def hello_world():
    return 'Image authentication server, post the image to /imsend'

@app.route('/imsend', methods=['GET', 'POST'])
def imreceive():
    data = None
    hack = False
    if request.method == "POST":
        data = request.get_data()
        if 'hack' in request.args:
            hack = request.args['hack']=="True"
        face = pickle.loads(data)
        print("Image Received")
        if face.shape==(224,224, 3) and face.dtype=="uint8":
            if hack and proc_face_with_hack(face):
                return jsonify(dict(authentication="ALLOWED"))
            elif not hack and proc_face(face):
                return jsonify(dict(authentication="ALLOWED"))
    return jsonify(dict(authentication="DENIED"))
<commit_msg>change<commit_after>from flask import Flask
from flask import request, jsonify
import pickle
import base64
import png
import io
import requests
import numpy as np
from adv_cnn.model import is_admin, do_adver

app = Flask(__name__)

def get_adv(face, mod_face):
    red1 = face[:,:,0]
    green1 = face[:,:,1]
    blue1 = face[:,:,2]
    red2 = mod_face[:,:,0]
    green2 = mod_face[:,:,1]
    blue2 = mod_face[:,:,2]
    diff_red = red1-red2
    diff_green = green1-green2
    diff_blue =blue1-blue2
    min_red = diff_red.min()
    max_red = diff_red.max()
    min_green = diff_green.min()
    max_green = diff_green.max()
    min_blue = diff_blue.min()
    max_blue = diff_blue.max()
    diff_red = (diff_red-min_red)*(255/max_red)
    diff_green = (diff_green-min_green)*(255/max_green)
    diff_blue = (diff_blue-min_blue)*(255/max_blue)
    diff = np.zeros_like(face)
    diff[:,:,0] = diff_red
    diff[:,:,1] = diff_green
    diff[:,:,2] = diff_blue
    return diff

def sanitise_image(mat):
    int_mat = np.rint(mat).astype(int)
    return np.clip(int_mat, 0, 255).astype(np.uint8)

def publish_image(face_im, adv_im, combined_im, confidence=0.0, hack=False):
    """convert png; base64 encode that and post to stat server"""
    # Do face
    text_buf = io.BytesIO()
    png.from_array(face_im, 'RGB').save(text_buf)
    encoded_face = b"data:image/png;base64,"+base64.b64encode(text_buf.getvalue(),b'#/')

    # Do adv
    text_buf = io.BytesIO()
    png.from_array(adv_im, 'RGB').save(text_buf)
    encoded_adv = b"data:image/png;base64," + base64.b64encode(text_buf.getvalue(),b'#/')

    # Do combined
    text_buf = io.BytesIO()
    png.from_array(combined_im, 'RGB').save(text_buf)
    encoded_combined = b"data:image/png;base64," + base64.b64encode(text_buf.getvalue(),b'#/')

    url = "http://facejack.westeurope.cloudapp.azure.com:5000/push_stats"
    if hack:
        payload = b"adversarial=yes&original_img=" + encoded_face + \
                  b"&adv_mod_img=" + encoded_adv + \
                  b"&modified_img=" + encoded_combined + \
                  b"&confidence=" + str(confidence * 100).encode()
    else:
        payload = b"adversarial=no&original_img="+encoded_face+\
                  b"&adv_mod_img="+encoded_adv+\
                  b"&modified_img="+encoded_combined+\
                  b"&confidence="+str(confidence*100).encode()
    headers = {
        'content-type': "application/x-www-form-urlencoded",
        'cache-control': "no-cache"
    }

    response = requests.request("POST", url, data=payload, headers=headers, )

def proc_face(face):
    """
    :param face: np.array with face in it
    :return: bool: true for admin and false otherwise
    """
    print("PROC_FACE")
    # time.sleep(3)
    # publish_image(face, face, face)
    res, conf = is_admin(face)
    publish_image(face, np.ones_like(face)*255, face, conf)
    return res

def proc_face_with_hack(face):
    print("MAJOR HACK IN PROGRESS")
    for face1, confidence in do_adver(face):
        face1 = sanitise_image(face1)
        publish_image(face, get_adv(face, face1), face1, confidence, hack=True)
    res, conf = is_admin(face1)
    return res

@app.route('/')
def hello_world():
    return 'Image authentication server, post the image to /imsend'

@app.route('/imsend', methods=['GET', 'POST'])
def imreceive():
    data = None
    hack = False
    if request.method == "POST":
        data = request.get_data()
        if 'hack' in request.args:
            hack = request.args['hack']=="True"
        face = pickle.loads(data)
        print("Image Received")
        if face.shape==(224,224, 3) and face.dtype=="uint8":
            if hack and proc_face_with_hack(face):
                return jsonify(dict(authentication="ALLOWED"))
            elif not hack and proc_face(face):
                return jsonify(dict(authentication="ALLOWED"))
    return jsonify(dict(authentication="DENIED"))
<|endoftext|>


<commit_before>import platform
from distutils.version import StrictVersion

def _boost_python_module_action(target, source, env):
    # Code to build "target" from "source"
    headers = [header for header in source if header.suffix == '.h']
    sources = [source for source in source if source.suffix in ['.cpp', '.cxx', '.c++']]
    targets = list(itertools.chain(*[env.SharedObject(None, source) for source in sources]))

    if system is 'linux' and len(headers) == 1:
        if len(header) == 1:
            cmd = env.Command(header[0].target_from_source('', '.h.gch'), header, '$CXX -o $TARGET -x c++-header -c -fPIC $SHCXXFLAGS $_CCCOMCOM $SOURCE')
            env.Depends(targets, cmd)

    source = env.File('response_file.rsp')
    with open(source.abspath, 'w') as filehandler:
        filehandler.write(' '.join(target.abspath.replace('\\','/') + ' ' for target in targets))

    env.Append(LINKFLAGS = '@' + source.abspath)

    kwargs = dict(SHLIBSUFFIX = '.so',
                  SHLIBPREFIX = '')

    if system is 'darwin':
        bpm = env.LoadableModule(target, [], LDMODULESUFFIX='.so',
            FRAMEWORKSFLAGS = '-flat_namespace -undefined suppress', **kwargs)
    else:
        bpm = env.LoadableModule(target, [], **kwargs)

    return bpm

_boost_python_module_builder = SCons.Builder.Builder(action = _boost_python_module_action)

def generate(env):
    """Add Builders and construction variables to the Environment."""
    env.Append(LIBS = 'boost_python')
    env.AppendUnique(CPPDEFINES = ['BOOST_PYTHON_DYNAMIC_LIB', 'BOOST_ALL_NO_LIB'])
    env['BUILDERS']['BoostPythonModule'] = _boost_python_module_builder

def exists(env):
    return 1<commit_msg>Correct boost_python_module_builder<commit_after>import platform
from distutils.version import StrictVersion
from SCons.Builder import Builder

def _boost_python_module_action(target, source, env):
    # Code to build "target" from "source"
    headers = [header for header in source if header.suffix == '.h']
    sources = [source for source in source if source.suffix in ['.cpp', '.cxx', '.c++']]
    targets = list(itertools.chain(*[env.SharedObject(None, source) for source in sources]))

    if system == 'linux' and len(headers) == 1:
        if len(header) == 1:
            cmd = env.Command(header[0].target_from_source('', '.h.gch'), header, '$CXX -o $TARGET -x c++-header -c -fPIC $SHCXXFLAGS $_CCCOMCOM $SOURCE')
            env.Depends(targets, cmd)

    source = env.File('response_file.rsp')
    with open(source.abspath, 'w') as filehandler:
        filehandler.write(' '.join(target.abspath.replace('\\','/') + ' ' for target in targets))

    env.Append(LINKFLAGS = '@' + source.abspath)

    kwargs = dict(SHLIBSUFFIX = '.so',
                  SHLIBPREFIX = '')

    if system == 'darwin':
        bpm = env.LoadableModule(target, [], LDMODULESUFFIX='.so',
            FRAMEWORKSFLAGS = '-flat_namespace -undefined suppress', **kwargs)
    else:
        bpm = env.LoadableModule(target, [], **kwargs)

    return bpm

_boost_python_module_builder = .uilder(action = _boost_python_module_action)

def generate(env):
    """Add Builders and construction variables to the Environment."""
    env.Append(LIBS = 'boost_python')
    env.AppendUnique(CPPDEFINES = ['BOOST_PYTHON_DYNAMIC_LIB', 'BOOST_ALL_NO_LIB'])
    env['BUILDERS']['BoostPythonModule'] = _boost_python_module_builder

def exists(env):
    return 1<|endoftext|>


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

# BenchExec is a framework for reliable benchmarking.
# This file is part of BenchExec.
#
# Copyright (C) 2007-2016  Dirk Beyer
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
sys.dont_write_bytecode = True # prevent creation of .pyc files

import os
import io
import xml.etree.ElementTree as ET
import bz2

from benchexec import util
import benchexec.tablegenerator as tablegenerator
import benchexec

def xml_to_string(elem, qualified_name=None, public_id=None, system_id=None):
    """
    Return a pretty-printed XML string for the Element.
    Also allows setting a document type.
    """
    from xml.dom import minidom
    rough_string = ET.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    if qualified_name:
        doctype = minidom.DOMImplementation().createDocumentType(
                qualified_name, public_id, system_id)
        reparsed.insertBefore(doctype, reparsed.documentElement)
    return reparsed.toprettyxml(indent="  ")

def getWitnesses(witnessXML):
    witnesses = {}
    for result in witnessXML.findall('run'):
        run = result.get('name')
        witnesses[run] = result
    return witnesses

def getWitnessResult(witness, expected_result):

    if witness is None:
        return ('witness missing', 'error')

    sourcefile = witness.get('name')
    status = witness.findall('column[@title="status"]')[0].get('value')
    category = witness.findall('column[@title="category"]')[0].get('value')

    # remove 's' for seconds and parse time as float
    wallTime = float(witness.findall('column[@title="walltime"]')[0].get('value')[:-1])
    cpuTime = float(witness.findall('column[@title="cputime"]')[0].get('value')[:-1])

    if expected_result == False:
        if status.startswith('true') or status.startswith('unknown'):
            return ('witness unconfirmed', 'error')
        if max(wallTime, cpuTime) > 90:
            return ('witness timeout', 'error')
        if status.startswith('false('):
            return (status, category)
    if expected_result == True:
        if status.startswith('false(') or status.startswith('unknown'):
            return ('witness unconfirmed', 'error')
        if max(wallTime, cpuTime) > 900:
            return ('witness timeout', 'error')
        if status.startswith('true'):
            return (status, category)
    if status.startswith('OUT OF MEMORY'):
        return ('witness out of memory', 'error')

    return ('witness invalid (' + status + ')', 'error')

def main(argv=None):

    if argv is None:
        argv = sys.argv

    if len(argv) < 3:
        sys.exit('Usage: ' + argv[0] + ' <results-xml> [<witness-xml>]* [--no-overwrite-status].\n')

    resultFile   = argv[1]
    witnessFiles = []
    isOverwrite = True
    for i in range(2, len(argv)):
        if len(argv) > i and not argv[i].startswith('--'):
            witnessFiles.append(argv[i])
        if argv[i] == '--no-overwrite-status':
            isOverwrite = False

    if not os.path.exists(resultFile) or not os.path.isfile(resultFile):
        sys.exit('File {0} does not exist.'.format(repr(resultFile)))
    resultXML   = tablegenerator.parse_results_file(resultFile)
    witnessSets = []
    for witnessFile in witnessFiles:
        if not os.path.exists(witnessFile) or not os.path.isfile(witnessFile):
            sys.exit('File {0} does not exist.'.format(repr(witnessFile)))
        witnessXML = tablegenerator.parse_results_file(witnessFile)
        witnessSets.append(getWitnesses(witnessXML))
        resultXML.set('options', '' + resultXML.get('options', default='') + ' [[ ' + witnessXML.get('options', default='') + ' ]]')
        resultXML.set('date',    '' + resultXML.get('date', default='')    + ' [[ ' + witnessXML.get('date', default='')    + ' ]]')

    for result in resultXML.findall('run'):
        run = result.get('name')
        basename = os.path.basename(run)
        if 'correct' == result.findall('column[@title="category"]')[0].get('value'):
                statusVer   = result.findall('column[@title="status"]')[0]
                categoryVer = result.findall('column[@title="category"]')[0]
                properties = result.get('properties').split(' ');
                expected_result = benchexec.result.satisfies_file_property(basename, properties);

                statusWit, categoryWit = (None, None)
                i = 0
                for witnessSet in witnessSets:
                    i = i + 1
                    witness = witnessSet.get(run, None)
                    # copy data from witness
                    if witness is not None:
                        for column in witness:
                            newColumn = ET.Element('column', {
                                 'title': 'wit' + str(i) + '_' + column.get('title'),
                                 'value':  column.get('value'),
                                 'hidden': column.get('hidden','false')
                                 })
                            result.append(newColumn)
                        witnessSet.pop(run)
                        statusWitNew, categoryWitNew = getWitnessResult(witness, expected_result)
                        if expected_result == False:
                            if statusWitNew.startswith('false(') or statusWit is None:
                                statusWit, categoryWit = (statusWitNew, categoryWitNew)
                        if expected_result == True:
                            if statusWitNew.startswith('true') or statusWit is None:
                                statusWit, categoryWit = (statusWitNew, categoryWitNew)
                # Overwrite status with status from witness
                if isOverwrite and statusWit is not None and categoryWit is not None:
                    result.findall('column[@title="status"]')[0].set('value', statusWit)
                    result.findall('column[@title="category"]')[0].set('value', categoryWit)
        # Clean-up an entry that can be inferred by table-generator automatically, avoids path confusion
        del result.attrib['logfile']

    filename = resultFile + '.merged.xml.bz2'
    print ('    ' + filename)
    open_func = bz2.BZ2File if hasattr(bz2.BZ2File, 'writable') else util.BZ2FileHack
    with io.TextIOWrapper(open_func(filename, 'wb'), encoding='utf-8') as xml_file:
        xml_file.write(xml_to_string(resultXML).replace('    \n','').replace('  \n',''))

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>Unconfirmed witnesses count as 'unknown', invalid witnesses count as 'error'.<commit_after>#!/usr/bin/env python

# BenchExec is a framework for reliable benchmarking.
# This file is part of BenchExec.
#
# Copyright (C) 2007-2016  Dirk Beyer
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
sys.dont_write_bytecode = True # prevent creation of .pyc files

import os
import io
import xml.etree.ElementTree as ET
import bz2

from benchexec import util
import benchexec.tablegenerator as tablegenerator
import benchexec

def xml_to_string(elem, qualified_name=None, public_id=None, system_id=None):
    """
    Return a pretty-printed XML string for the Element.
    Also allows setting a document type.
    """
    from xml.dom import minidom
    rough_string = ET.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    if qualified_name:
        doctype = minidom.DOMImplementation().createDocumentType(
                qualified_name, public_id, system_id)
        reparsed.insertBefore(doctype, reparsed.documentElement)
    return reparsed.toprettyxml(indent="  ")

def getWitnesses(witnessXML):
    witnesses = {}
    for result in witnessXML.findall('run'):
        run = result.get('name')
        witnesses[run] = result
    return witnesses

def getWitnessResult(witness, expected_result):

    if witness is None:
        # If there is no witness, then this is an error of the verifier.
        return ('witness missing', 'error')

    sourcefile = witness.get('name')
    status = witness.findall('column[@title="status"]')[0].get('value')
    category = witness.findall('column[@title="category"]')[0].get('value')

    # remove 's' for seconds and parse time as float
    wallTime = float(witness.findall('column[@title="walltime"]')[0].get('value')[:-1])
    cpuTime = float(witness.findall('column[@title="cputime"]')[0].get('value')[:-1])

    # Unconfirmed witnesses count as 'unknown'.
    if expected_result == False:
        if status.startswith('true') or status.startswith('unknown'):
            return ('witness unconfirmed', 'unknown')
        if max(wallTime, cpuTime) > 90:
            return ('witness timeout', 'unknown')
        if status.startswith('false('):
            return (status, category)
    if expected_result == True:
        if status.startswith('false(') or status.startswith('unknown'):
            return ('witness unconfirmed', 'unknown')
        if max(wallTime, cpuTime) > 900:
            return ('witness timeout', 'unknown')
        if status.startswith('true'):
            return (status, category)
    if status.startswith('OUT OF MEMORY'):
        return ('witness out of memory', 'unknown')

    # An invalid witness counts as error of the verifier.
    return ('witness invalid (' + status + ')', 'error')

def main(argv=None):

    if argv is None:
        argv = sys.argv

    if len(argv) < 3:
        sys.exit('Usage: ' + argv[0] + ' <results-xml> [<witness-xml>]* [--no-overwrite-status].\n')

    resultFile   = argv[1]
    witnessFiles = []
    isOverwrite = True
    for i in range(2, len(argv)):
        if len(argv) > i and not argv[i].startswith('--'):
            witnessFiles.append(argv[i])
        if argv[i] == '--no-overwrite-status':
            isOverwrite = False

    if not os.path.exists(resultFile) or not os.path.isfile(resultFile):
        sys.exit('File {0} does not exist.'.format(repr(resultFile)))
    resultXML   = tablegenerator.parse_results_file(resultFile)
    witnessSets = []
    for witnessFile in witnessFiles:
        if not os.path.exists(witnessFile) or not os.path.isfile(witnessFile):
            sys.exit('File {0} does not exist.'.format(repr(witnessFile)))
        witnessXML = tablegenerator.parse_results_file(witnessFile)
        witnessSets.append(getWitnesses(witnessXML))
        resultXML.set('options', '' + resultXML.get('options', default='') + ' [[ ' + witnessXML.get('options', default='') + ' ]]')
        resultXML.set('date',    '' + resultXML.get('date', default='')    + ' [[ ' + witnessXML.get('date', default='')    + ' ]]')

    for result in resultXML.findall('run'):
        run = result.get('name')
        basename = os.path.basename(run)
        if 'correct' == result.findall('column[@title="category"]')[0].get('value'):
                statusVer   = result.findall('column[@title="status"]')[0]
                categoryVer = result.findall('column[@title="category"]')[0]
                properties = result.get('properties').split(' ');
                expected_result = benchexec.result.satisfies_file_property(basename, properties);

                statusWit, categoryWit = (None, None)
                i = 0
                for witnessSet in witnessSets:
                    i = i + 1
                    witness = witnessSet.get(run, None)
                    # copy data from witness
                    if witness is not None:
                        for column in witness:
                            newColumn = ET.Element('column', {
                                 'title': 'wit' + str(i) + '_' + column.get('title'),
                                 'value':  column.get('value'),
                                 'hidden': column.get('hidden','false')
                                 })
                            result.append(newColumn)
                        witnessSet.pop(run)
                        statusWitNew, categoryWitNew = getWitnessResult(witness, expected_result)
                        if expected_result == False:
                            if statusWitNew.startswith('false(') or statusWit is None:
                                statusWit, categoryWit = (statusWitNew, categoryWitNew)
                        if expected_result == True:
                            if statusWitNew.startswith('true') or statusWit is None:
                                statusWit, categoryWit = (statusWitNew, categoryWitNew)
                # Overwrite status with status from witness
                if isOverwrite and statusWit is not None and categoryWit is not None:
                    result.findall('column[@title="status"]')[0].set('value', statusWit)
                    result.findall('column[@title="category"]')[0].set('value', categoryWit)
        # Clean-up an entry that can be inferred by table-generator automatically, avoids path confusion
        del result.attrib['logfile']

    filename = resultFile + '.merged.xml.bz2'
    print ('    ' + filename)
    open_func = bz2.BZ2File if hasattr(bz2.BZ2File, 'writable') else util.BZ2FileHack
    with io.TextIOWrapper(open_func(filename, 'wb'), encoding='utf-8') as xml_file:
        xml_file.write(xml_to_string(resultXML).replace('    \n','').replace('  \n',''))

if __name__ == '__main__':
    sys.exit(main())
<|endoftext|>


<commit_before># Copyright 2020 The Flax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Early stopping for model training."""

import numpy as np
from flax.training import checkpoints

class EarlyStopping:
  """Prevents overfitting by ending training early if validation loss
  does not improve.

  Attributes:
    ckpt_dir: str: path to store checkpoint files in.
    prefix: str: checkpoint file name prefix.
    patience: number of steps of no improvement before stopping.
    keep: number of past checkpoint files to keep.
  """
  def __init__(self,
               ckpt_dir,
               prefix='checkpoint_',
               min_delta=0,
               patience=0,
               keep=1):
    self.ckpt_dir = ckpt_dir
    self.min_delta = min_delta
    self.patience = patience
    self.prefix = prefix
    self.keep = keep

    self.count = 0
    self.best_loss = np.Inf
    self._should_stop = False

  @property
  def stop(self):
      return self.should_stop

  def save_checkpoint(self, target, step, metric):
    """Save a checkpoint of the model.

    Args:
      target: serializable flax object, usually a flax optimizer.
      step: int or float: training step number or other metric number.
      metric: int or float: metric (i.e. validation loss) to determine
          improvement.

    Returns:
      Filename of saved checkpoint or None if there was no improvement.
    """
    output = None
    if self.best_loss - val_loss > self.min_delta:
        output = checkpoints.save_checkpoint(self.ckpt_dir,
                                             target=target,
                                             step=step,
                                             prefix=self.prefix,
                                             keep=self.keep)
        self.count = 0
        self.best_loss = val_loss
    else:
        self.count += 1
        if self.count >= self.patience:
            self.should_stop = True

    return output
<commit_msg>Update early_stopping.py<commit_after># Copyright 2020 The Flax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Early stopping for model training."""

import numpy as np
from flax.training import checkpoints

class EarlyStopping:
  """Prevents overfitting by ending training early if validation loss
  does not improve.

  Attributes:
    ckpt_dir: str: path to store checkpoint files in.
    prefix: str: checkpoint file name prefix.
    patience: number of steps of no improvement before stopping.
    keep: number of past checkpoint files to keep.
  """
  def __init__(self,
               ckpt_dir,
               prefix='checkpoint_',
               min_delta=0,
               patience=0,
               keep=1):
    self.ckpt_dir = ckpt_dir
    self.min_delta = min_delta
    self.patience = patience
    self.prefix = prefix
    self.keep = keep

    self.count = 0
    self.best_loss = np.Inf
    self._should_stop = False

  @property
  def stop(self):
      return self.should_stop

  def save_checkpoint(self, target, step, metric):
    """Save a checkpoint of the model.

    Args:
      target: serializable flax object, usually a flax optimizer.
      step: int or float: training step number or other metric number.
      metric: int or float: metric (i.e. validation loss) to determine
          improvement.

    Returns:
      Filename of saved checkpoint or None if there was no improvement.
    """
    output = None
    if self.best_loss - metric > self.min_delta:
        output = checkpoints.save_checkpoint(self.ckpt_dir,
                                             target=target,
                                             step=step,
                                             prefix=self.prefix,
                                             keep=self.keep)
        self.count = 0
        self.best_loss = metric
    else:
        self.count += 1
        if self.count >= self.patience:
            self.should_stop = True

    return output
<|endoftext|>