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

from __future__ import unicode_literals

import six

from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import override, ugettext_lazy as _

from aldryn_apphooks_config.models import AppHookConfig
from aldryn_reversion.core import version_controlled_content
from aldryn_translation_tools import TranslationHelperMixin

from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin
from cms.utils.i18n import get_current_language
from djangocms_text_ckeditor.fields import HTMLField
from parler.models import TranslatableModel, TranslatedFields
from sortedm2m.fields import SortedManyToManyField

from .managers import CategoryManager, RelatedManager

def get_translation(obj, language_code):
    """
    This is an adapter from django-hvad.utils.get_translation(), a function
    to django-parler.models.get_translation() (a model instance method).
    """
    if not obj or not hasattr(obj, "get_translation"):
        return None
    return obj.get_translation(language_code)

def get_slug_in_language(record, language):
    """This is an adapter from django-hvad's lazy_translation_getter."""
    if not record or not hasattr(record, "safe_translation_getter"):
        return None
    return record.safe_translation_getter(
        field="slug", language_code=language, default=None, )

class FaqConfig(TranslatableModel, AppHookConfig):
    """Adds some translatable, per-app-instance fields."""
    translations = TranslatedFields(
        app_title=models.CharField(_('application title'), max_length=234),
    )

@python_2_unicode_compatible
@version_controlled_content
class Category(TranslationHelperMixin, TranslatableModel):
    translations = TranslatedFields(
        name=models.CharField(max_length=255),
        slug=models.SlugField(verbose_name=_('Slug'), max_length=255),
    )
    appconfig = models.ForeignKey(FaqConfig, verbose_name=_('appconfig'),
        blank=True, null=True)
    objects = CategoryManager()

    class Meta:
        verbose_name = _('category')
        verbose_name_plural = _('categories')

    def __str__(self):
        if six.PY2:
            return self.safe_translation_getter('name', default=unicode(self.pk))
        else:
            return self.safe_translation_getter('name', default=str(self.pk))

    def model_type_id(self):
        return ContentType.objects.get_for_model(self.__class__).id

    def get_absolute_url(self, language=None):
        language = language or get_current_language()
        slug, language = self.known_translation_getter(
            'slug', default=None, language_code=language)
        kwargs = {'category_slug': slug}
        with override(language):
            return reverse(
                '{0}:faq-category'.format(self.appconfig.namespace),
                kwargs=kwargs
            )

@python_2_unicode_compatible
@version_controlled_content
class Question(TranslatableModel):
    translations = TranslatedFields(
        title=models.CharField(_('Title'), max_length=255),
        answer_text=HTMLField(_('answer'))
    )
    category = models.ForeignKey(Category, related_name='questions')

    answer = PlaceholderField(
        'faq_question_answer', related_name='faq_questions')
    is_top = models.BooleanField(default=False)
    number_of_visits = models.PositiveIntegerField(default=0, editable=False)

    objects = RelatedManager()

    order = models.PositiveIntegerField(default=1, db_index=True)

    class Meta:
        verbose_name = _('question')
        verbose_name_plural = _('questions')
        ordering = ('order', )

    def __str__(self):
        return self.safe_translation_getter('title', default=str(self.pk))

    def model_type_id(self):
        return ContentType.objects.get_for_model(self.__class__).id

    def get_absolute_url(self, language=None):
        language = language or get_current_language()
        category = self.category
        try:
            translation = get_translation(self, language_code=language)
        except models.ObjectDoesNotExist:
            translation = None
        cat_slug = get_slug_in_language(category, language)
        if translation and cat_slug:
            with override(language):
                return reverse(
                    'aldryn_faq:faq-answer', args=(cat_slug, self.pk))
        else:
            return category.get_absolute_url(language)

class QuestionsPlugin(models.Model):
    questions = models.IntegerField(
        default=5,
        help_text=_('The number of questions to be displayed.')
    )

    def get_queryset(self):
        return Question.objects.filter_by_language(self.language)

    def get_questions(self):
        questions = self.get_queryset()
        return questions[:self.questions]

    class Meta:
        abstract = True

@python_2_unicode_compatible
class QuestionListPlugin(CMSPlugin):
    questions = SortedManyToManyField(Question)

    def __str__(self):
        return str(self.questions.count())

    def copy_relations(self, oldinstance):
        self.questions = oldinstance.questions.all()

    def get_questions(self):
        return self.questions.all()

class CategoryListPlugin(CMSPlugin):

    def copy_relations(self, oldinstance):
        for category in oldinstance.selected_categories.all():
            category.pk = None
            category.cms_plugin = self
            category.save()

    def get_categories(self):
        """
        By default, if no categories were chosen return all categories.
        Otherwise, return the chosen categories.
        """
        categories = Category.objects.get_categories(language=self.language)

        if self.selected_categories.exists():
            category_ids = self.selected_categories.values_list(
                'category__pk', flat=True)
            # categories is a list, and a sorted one so no need for another db
            # call.
            selected_categories = []
            for id in category_ids:
                category = next((x for x in categories if x.pk == id), None)
                if category:
                    selected_categories.append(category)
            return selected_categories
        return categories

class LatestQuestionsPlugin(CMSPlugin, QuestionsPlugin):

    def get_queryset(self):
        qs = super(LatestQuestionsPlugin, self).get_queryset()
        return qs.order_by('-id')

@python_2_unicode_compatible
class SelectedCategory(models.Model):
    category = models.ForeignKey(to=Category, verbose_name=_('category'))
    position = models.PositiveIntegerField(
        verbose_name=_('position'), blank=True, null=True)
    cms_plugin = models.ForeignKey(
        to=CategoryListPlugin, related_name='selected_categories')

    class Meta:
        ordering = ['position']
        verbose_name = _('Category')
        verbose_name_plural = _('Categories')

    def __str__(self):
        return self.category.name

class TopQuestionsPlugin(CMSPlugin, QuestionsPlugin):

    def get_queryset(self):
        qs = super(TopQuestionsPlugin, self).get_queryset()
        return qs.filter(is_top=True)

class MostReadQuestionsPlugin(CMSPlugin, QuestionsPlugin):

    def get_queryset(self):
        qs = super(MostReadQuestionsPlugin, self).get_queryset()
        return qs.order_by('-number_of_visits')
<commit_msg>Fix import<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import six

from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import override, ugettext_lazy as _

from aldryn_apphooks_config.models import AppHookConfig
from aldryn_reversion.core import version_controlled_content
from aldryn_translation_tools.models import TranslationHelperMixin

from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin
from cms.utils.i18n import get_current_language
from djangocms_text_ckeditor.fields import HTMLField
from parler.models import TranslatableModel, TranslatedFields
from sortedm2m.fields import SortedManyToManyField

from .managers import CategoryManager, RelatedManager

def get_translation(obj, language_code):
    """
    This is an adapter from django-hvad.utils.get_translation(), a function
    to django-parler.models.get_translation() (a model instance method).
    """
    if not obj or not hasattr(obj, "get_translation"):
        return None
    return obj.get_translation(language_code)

def get_slug_in_language(record, language):
    """This is an adapter from django-hvad's lazy_translation_getter."""
    if not record or not hasattr(record, "safe_translation_getter"):
        return None
    return record.safe_translation_getter(
        field="slug", language_code=language, default=None, )

class FaqConfig(TranslatableModel, AppHookConfig):
    """Adds some translatable, per-app-instance fields."""
    translations = TranslatedFields(
        app_title=models.CharField(_('application title'), max_length=234),
    )

@python_2_unicode_compatible
@version_controlled_content
class Category(TranslationHelperMixin, TranslatableModel):
    translations = TranslatedFields(
        name=models.CharField(max_length=255),
        slug=models.SlugField(verbose_name=_('Slug'), max_length=255),
    )
    appconfig = models.ForeignKey(FaqConfig, verbose_name=_('appconfig'),
        blank=True, null=True)
    objects = CategoryManager()

    class Meta:
        verbose_name = _('category')
        verbose_name_plural = _('categories')

    def __str__(self):
        if six.PY2:
            return self.safe_translation_getter('name', default=unicode(self.pk))
        else:
            return self.safe_translation_getter('name', default=str(self.pk))

    def model_type_id(self):
        return ContentType.objects.get_for_model(self.__class__).id

    def get_absolute_url(self, language=None):
        language = language or get_current_language()
        slug, language = self.known_translation_getter(
            'slug', default=None, language_code=language)
        kwargs = {'category_slug': slug}
        with override(language):
            return reverse(
                '{0}:faq-category'.format(self.appconfig.namespace),
                kwargs=kwargs
            )

@python_2_unicode_compatible
@version_controlled_content
class Question(TranslatableModel):
    translations = TranslatedFields(
        title=models.CharField(_('Title'), max_length=255),
        answer_text=HTMLField(_('answer'))
    )
    category = models.ForeignKey(Category, related_name='questions')

    answer = PlaceholderField(
        'faq_question_answer', related_name='faq_questions')
    is_top = models.BooleanField(default=False)
    number_of_visits = models.PositiveIntegerField(default=0, editable=False)

    objects = RelatedManager()

    order = models.PositiveIntegerField(default=1, db_index=True)

    class Meta:
        verbose_name = _('question')
        verbose_name_plural = _('questions')
        ordering = ('order', )

    def __str__(self):
        return self.safe_translation_getter('title', default=str(self.pk))

    def model_type_id(self):
        return ContentType.objects.get_for_model(self.__class__).id

    def get_absolute_url(self, language=None):
        language = language or get_current_language()
        category = self.category
        try:
            translation = get_translation(self, language_code=language)
        except models.ObjectDoesNotExist:
            translation = None
        cat_slug = get_slug_in_language(category, language)
        if translation and cat_slug:
            with override(language):
                return reverse(
                    'aldryn_faq:faq-answer', args=(cat_slug, self.pk))
        else:
            return category.get_absolute_url(language)

class QuestionsPlugin(models.Model):
    questions = models.IntegerField(
        default=5,
        help_text=_('The number of questions to be displayed.')
    )

    def get_queryset(self):
        return Question.objects.filter_by_language(self.language)

    def get_questions(self):
        questions = self.get_queryset()
        return questions[:self.questions]

    class Meta:
        abstract = True

@python_2_unicode_compatible
class QuestionListPlugin(CMSPlugin):
    questions = SortedManyToManyField(Question)

    def __str__(self):
        return str(self.questions.count())

    def copy_relations(self, oldinstance):
        self.questions = oldinstance.questions.all()

    def get_questions(self):
        return self.questions.all()

class CategoryListPlugin(CMSPlugin):

    def copy_relations(self, oldinstance):
        for category in oldinstance.selected_categories.all():
            category.pk = None
            category.cms_plugin = self
            category.save()

    def get_categories(self):
        """
        By default, if no categories were chosen return all categories.
        Otherwise, return the chosen categories.
        """
        categories = Category.objects.get_categories(language=self.language)

        if self.selected_categories.exists():
            category_ids = self.selected_categories.values_list(
                'category__pk', flat=True)
            # categories is a list, and a sorted one so no need for another db
            # call.
            selected_categories = []
            for id in category_ids:
                category = next((x for x in categories if x.pk == id), None)
                if category:
                    selected_categories.append(category)
            return selected_categories
        return categories

class LatestQuestionsPlugin(CMSPlugin, QuestionsPlugin):

    def get_queryset(self):
        qs = super(LatestQuestionsPlugin, self).get_queryset()
        return qs.order_by('-id')

@python_2_unicode_compatible
class SelectedCategory(models.Model):
    category = models.ForeignKey(to=Category, verbose_name=_('category'))
    position = models.PositiveIntegerField(
        verbose_name=_('position'), blank=True, null=True)
    cms_plugin = models.ForeignKey(
        to=CategoryListPlugin, related_name='selected_categories')

    class Meta:
        ordering = ['position']
        verbose_name = _('Category')
        verbose_name_plural = _('Categories')

    def __str__(self):
        return self.category.name

class TopQuestionsPlugin(CMSPlugin, QuestionsPlugin):

    def get_queryset(self):
        qs = super(TopQuestionsPlugin, self).get_queryset()
        return qs.filter(is_top=True)

class MostReadQuestionsPlugin(CMSPlugin, QuestionsPlugin):

    def get_queryset(self):
        qs = super(MostReadQuestionsPlugin, self).get_queryset()
        return qs.order_by('-number_of_visits')
<|endoftext|>


<commit_before>from django.contrib import admin

from .models import (
    Contest,
    Contestant,
    Performance,
    Profile,
    Note,
)

class PerformanceInline(admin.TabularInline):
    model = Performance

    fields = (
        'contestant',
        'contest_round',
        'session',
        'appearance',
    )

    extra = 10

class ScoreInline(admin.TabularInline):
    model = Performance

    fields = (
        'contestant',
        'contest_round',
        'mus1',
        'prs1',
        'sng1',
        'mus2',
        'prs2',
        'sng2',
    )

    max_num = 0

class SongInline(admin.TabularInline):
    model = Performance

    fields = (
        'contestant',
        'contest_round',
        'song1',
        'song2',
    )

    max_num = 0

@admin.register(Contest)
class ContestAdmin(admin.ModelAdmin):
    fieldsets = (
        ("Contest", {
            'fields': (
                'year',
                'contest_level',
                'contest_type',
            ),
        }),
    )

    list_filter = (
        'year',
        'contest_level',
        'contest_type',
    )

    inlines = (
        PerformanceInline,
        # ScoreInline,
    )

    save_on_top = True

@admin.register(Contestant)
class ContestantAdmin(admin.ModelAdmin):
    save_on_top = True

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'contestant_type',
            ),
        }),
        ('Details', {
            'classes': ('collapse',),
            'fields': (
                ('website', 'facebook', 'twitter'),
                ('phone', 'email'),
                ('tenor', 'lead', 'baritone', 'bass', 'director',),
                ('district', 'location'),
                ('prelim', 'rank'),
                'picture',
                'blurb',
            ),
        }),
    )

    inlines = (
        SongInline,
    )

    search_fields = (
        'name',
    )

    list_display = (
        'name',
        'website',
        'facebook',
        'phone',
        'prelim',
    )

    list_filter = (
        'contestant_type',
    )

    ordering = (
        'name',
    )

@admin.register(Performance)
class PerformanceAdmin(admin.ModelAdmin):
    save_on_top = True

    fields = (
        ('contestant', 'contest'),
        ('contest_round', 'session'),
        ('appearance', 'stagetime'),
        ('song1', 'song2'),
        ('mus1', 'prs1', 'sng1'),
        ('mus2', 'prs2', 'sng2'),
        ('place', 'men_on_stage'),
    )

    list_display = (
        '__unicode__',
        'stagetime',
        'song1',
        'song2',
        'appearance',
    )

    list_filter = (
        'contest',
        'contest_round',
    )

    search_fields = (
        'contestant__name',
    )

@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    list_display = (
        '__unicode__',
        'timezone',
    )

    save_on_top = True

@admin.register(Note)
class NoteAdmin(admin.ModelAdmin):
    save_on_top = True
<commit_msg>Score review admin view<commit_after>from django.contrib import admin

from .models import (
    Contest,
    Contestant,
    Performance,
    Profile,
    Note,
)

class PerformanceInline(admin.TabularInline):
    model = Performance

    fields = (
        'contestant',
        'contest_round',
        'session',
        'appearance',
    )

    extra = 10

class ScoreInline(admin.TabularInline):
    model = Performance

    fields = (
        'contestant',
        'contest_round',
        'mus1',
        'prs1',
        'sng1',
        'mus2',
        'prs2',
        'sng2',
    )

    max_num = 0

class SongInline(admin.TabularInline):
    model = Performance

    fields = (
        'contestant',
        'contest_round',
        'song1',
        'song2',
    )

    max_num = 0

@admin.register(Contest)
class ContestAdmin(admin.ModelAdmin):
    fieldsets = (
        ("Contest", {
            'fields': (
                'year',
                'contest_level',
                'contest_type',
            ),
        }),
    )

    list_filter = (
        'year',
        'contest_level',
        'contest_type',
    )

    inlines = (
        PerformanceInline,
        # ScoreInline,
    )

    save_on_top = True

@admin.register(Contestant)
class ContestantAdmin(admin.ModelAdmin):
    save_on_top = True

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'contestant_type',
            ),
        }),
        ('Details', {
            'classes': ('collapse',),
            'fields': (
                ('website', 'facebook', 'twitter'),
                ('phone', 'email'),
                ('tenor', 'lead', 'baritone', 'bass', 'director',),
                ('district', 'location'),
                ('prelim', 'rank'),
                'picture',
                'blurb',
            ),
        }),
    )

    inlines = (
        SongInline,
    )

    search_fields = (
        'name',
    )

    list_display = (
        'name',
        'website',
        'facebook',
        'phone',
        'prelim',
    )

    list_filter = (
        'contestant_type',
    )

    ordering = (
        'name',
    )

@admin.register(Performance)
class PerformanceAdmin(admin.ModelAdmin):
    save_on_top = True

    fields = (
        ('contestant', 'contest'),
        ('contest_round', 'session'),
        ('appearance', 'stagetime'),
        ('song1', 'song2'),
        ('mus1', 'prs1', 'sng1'),
        ('mus2', 'prs2', 'sng2'),
        ('place', 'men_on_stage'),
    )

    list_display = (
        '__unicode__',
        # 'stagetime',
        'song1',
        'mus1',
        'prs1',
        'sng1',
        'song2',
        'mus2',
        'prs2',
        'sng2',
        'place',
    )

    list_filter = (
        'contest',
        'contest_round',
    )

    search_fields = (
        'contestant__name',
    )

@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    list_display = (
        '__unicode__',
        'timezone',
    )

    save_on_top = True

@admin.register(Note)
class NoteAdmin(admin.ModelAdmin):
    save_on_top = True
<|endoftext|>


<commit_before>import quickbot_config as config
import Adafruit_BBIO.ADC as ADC
import Adafruit_BBIO.PWM as PWM
import Adafruit_BBIO.GPIO as GPIO
import time

LEFT = 0
RIGHT = 1
MIN = 0
MAX = 1

ADC.setup()

# Motor Pins -- (LEFT, RIGHT)
dir1Pin = (config.INl1, config.INr1)
dir2Pin = (config.INl2, config.INr2)
pwmPin = (config.PWMl, config.PWMr)

"""Initialize GPIO pins"""
GPIO.setup(dir1Pin[LEFT], GPIO.OUT)
GPIO.setup(dir2Pin[LEFT], GPIO.OUT)
GPIO.setup(dir1Pin[RIGHT], GPIO.OUT)
GPIO.setup(dir2Pin[RIGHT], GPIO.OUT)

PWM.start(pwmPin[LEFT], 0)#, frequency=frequency)
PWM.start(pwmPin[RIGHT], 0)#, frequency=frequency)

obstacle = False
while not obstacle:
    GPIO.output(dir1Pin[LEFT], GPIO.HIGH)
    GPIO.output(dir2Pin[LEFT], GPIO.LOW)
    PWM.set_duty_cycle(pwmPin[LEFT], 50)
    GPIO.output(dir1Pin[RIGHT], GPIO.HIGH)
    GPIO.output(dir2Pin[RIGHT], GPIO.LOW)
    PWM.set_duty_cycle(pwmPin[RIGHT], 50)

    reading1 = ADC.read(config.IRfl)
    reading2 = ADC.read(config.IRfr)
    reading3 = ADC.read(config.IRfm)
    print("sensor left " + str(reading1))
    print("sensor right " + str(reading2))
    print("sensor middle " + str(reading3))

    if max(reading1, reading2, reading3) > 0.5:
        obstacle = True

# while True:
#     leftIR = ADC.read(config.IRfl)
#     rightIR = ADC.read(config.IRfr)
#     middleIR = ADC.read(config.IRfm)
#     print("sensor left " + str(reading1))
#     print("sensor right " + str(reading2))
#     print("sensor middle " + str(reading3))
#
#     if leftIR > 0.3:
#         GPIO.output(dir1Pin[LEFT], GPIO.HIGH)
#         GPIO.output(dir2Pin[LEFT], GPIO.LOW)
#         PWM.set_duty_cycle(pwmPin[LEFT], 0)
#     elif rightIR > 0.3:
#         GPIO.output(dir1Pin[RIGHT], GPIO.HIGH)
#         GPIO.output(dir2Pin[RIGHT], GPIO.LOW)
#         PWM.set_duty_cycle(pwmPin[RIGHT], 0)

PWM.stop(pwmPin[LEFT])
PWM.stop(pwmPin[RIGHT])
PWM.cleanup()

GPIO.cleanup()<commit_msg>obstacle avoidance first try<commit_after>import quickbot_config as config
import Adafruit_BBIO.ADC as ADC
import Adafruit_BBIO.PWM as PWM
import Adafruit_BBIO.GPIO as GPIO
import time

LEFT = 0
RIGHT = 1
MIN = 0
MAX = 1

ADC.setup()

# Motor Pins -- (LEFT, RIGHT)
dir1Pin = (config.INl1, config.INr1)
dir2Pin = (config.INl2, config.INr2)
pwmPin = (config.PWMl, config.PWMr)

"""Initialize GPIO pins"""
GPIO.setup(dir1Pin[LEFT], GPIO.OUT)
GPIO.setup(dir2Pin[LEFT], GPIO.OUT)
GPIO.setup(dir1Pin[RIGHT], GPIO.OUT)
GPIO.setup(dir2Pin[RIGHT], GPIO.OUT)

PWM.start(pwmPin[LEFT], 0)#, frequency=frequency)
PWM.start(pwmPin[RIGHT], 0)#, frequency=frequency)

# obstacle = False
# while not obstacle:
#     GPIO.output(dir1Pin[LEFT], GPIO.HIGH)
#     GPIO.output(dir2Pin[LEFT], GPIO.LOW)
#     PWM.set_duty_cycle(pwmPin[LEFT], 50)
#     GPIO.output(dir1Pin[RIGHT], GPIO.HIGH)
#     GPIO.output(dir2Pin[RIGHT], GPIO.LOW)
#     PWM.set_duty_cycle(pwmPin[RIGHT], 50)
#
#     reading1 = ADC.read(config.IRfl)
#     reading2 = ADC.read(config.IRfr)
#     reading3 = ADC.read(config.IRfm)
#     print("sensor left " + str(reading1))
#     print("sensor right " + str(reading2))
#     print("sensor middle " + str(reading3))
#
#     if max(reading1, reading2, reading3) > 0.5:
#         obstacle = True

middleIR = 0

while middleIR < 0.6:
    leftIR = ADC.read(config.IRfl)
    rightIR = ADC.read(config.IRfr)
    middleIR = ADC.read(config.IRfm)
    print("sensor left " + str(leftIR))
    print("sensor right " + str(rightIR))
    print("sensor middle " + str(middleIR))

    if rightIR > 0.5:
        GPIO.output(dir1Pin[LEFT], GPIO.HIGH)
        GPIO.output(dir2Pin[LEFT], GPIO.LOW)
        PWM.set_duty_cycle(pwmPin[LEFT], 50)
    elif leftIR > 0.5:
        GPIO.output(dir1Pin[RIGHT], GPIO.HIGH)
        GPIO.output(dir2Pin[RIGHT], GPIO.LOW)
        PWM.set_duty_cycle(pwmPin[RIGHT], 50)
    else:
        GPIO.output(dir1Pin[LEFT], GPIO.HIGH)
        GPIO.output(dir2Pin[LEFT], GPIO.LOW)
        PWM.set_duty_cycle(pwmPin[LEFT], 50)
        GPIO.output(dir1Pin[RIGHT], GPIO.HIGH)
        GPIO.output(dir2Pin[RIGHT], GPIO.LOW)
        PWM.set_duty_cycle(pwmPin[RIGHT], 50)

PWM.stop(pwmPin[LEFT])
PWM.stop(pwmPin[RIGHT])
PWM.cleanup()

GPIO.cleanup()<|endoftext|>


<commit_before>import dis
from types import CodeType
from opcode import opmap

MAX_BYTE = 255

def _add_attr_values_from_insert_to_original(original_code, insert_code, insert_code_obj, attribute_name, op_list):
    """
    This function appends values of the attribute `attribute_name` of the inserted code to the original values,
     and changes indexes inside inserted code. If some bytecode instruction in the inserted code used to call argument
     number i, after modification it calls argument n + i, where n - length of the values in the original code.
     So it helps to avoid variables mixing between two pieces of code.

    :param original_code: code to modify
    :param insert_code: code to insert
    :param insert_code_obj: bytes sequence of inserted code, which should be modified too
    :param attribute_name: name of attribute to modify ('co_names', 'co_consts' or 'co_varnames')
    :param op_list: sequence of bytecodes whose arguments should be changed
    :return: modified bytes sequence of the code to insert and new values of the attribute `attribute_name` for original code
    """
    orig_value = getattr(original_code, attribute_name)
    insert_value = getattr(insert_code, attribute_name)
    orig_names_len = len(orig_value)
    code_with_new_values = list(insert_code_obj)
    for offset, op, arg in dis._unpack_opargs(insert_code_obj):
        if op in op_list:
            if code_with_new_values[offset + 1] + orig_names_len > MAX_BYTE:
                raise ValueError("Bad number of arguments")
            code_with_new_values[offset + 1] += orig_names_len
    new_values = orig_value + insert_value
    return bytes(code_with_new_values), new_values

def _modify_new_lines(code_to_modify, code_insert, offset_of_inserted_code):
    """
    Update new lines in order to hide inserted code inside the original code
    :param code_to_modify: code to modify
    :param code_insert: code to insert
    :param offset_of_inserted_code: the offset of the inserted code
    :return: bytes sequence of code with updated lines offsets
    """
    new_list = list(code_to_modify.co_lnotab)
    abs_offset = 0
    for i in range(0, len(new_list), 2):
        abs_offset += new_list[i]
        if abs_offset == offset_of_inserted_code and (i + 2) < len(new_list):
            if new_list[i + 2] + len(code_insert) > MAX_BYTE:
                raise ValueError("Bad number of arguments")
            new_list[i + 2] += len(code_insert)
    return bytes(new_list)

def _update_label_offsets(code_obj, offset_of_inserted_code, size_of_inserted_code):
    """
    Update labels for the relative and absolute jump targets
    :param code_obj: code to modify
    :param offset_of_inserted_code: offset for the inserted code
    :param offset_of_inserted_code: size of the inserted code
    :return: bytes sequence with modified labels
    """
    offsets_for_modification = []
    for offset, op, arg in dis._unpack_opargs(code_obj):
        if arg is not None:
            if op in dis.hasjrel:
                # has relative jump target
                label = offset + 2 + arg
                if offset < offset_of_inserted_code < label:
                    # change labels for relative jump targets if code was inserted inside
                    offsets_for_modification.append(offset)
            elif op in dis.hasjabs:
                # change label for absolute jump if code was inserted before it
                if offset_of_inserted_code <= arg:
                    offsets_for_modification.append(offset)
    code_list = list(code_obj)
    for i in range(0, len(code_obj), 2):
        op = code_list[i]
        if i in offsets_for_modification and op >= dis.HAVE_ARGUMENT:
            if code_list[i + 1] + size_of_inserted_code > MAX_BYTE:
                raise ValueError("Bad jump argument")
            code_list[i + 1] += size_of_inserted_code
    return bytes(code_list)

def _return_none_fun():
    return None

def insert_code(code_to_modify, code_to_insert, before_line):
    """
    Insert piece of code `code_to_insert` to `code_to_modify` right inside the line `before_line` before the
    instruction on this line by modifying original bytecode

    :param code_to_modify: Code to modify
    :param code_to_insert: Code to insert
    :param before_line: Number of line for code insertion
    :return: modified code
    """
    linestarts = dict(dis.findlinestarts(code_to_modify))
    if before_line not in linestarts.values():
        return code_to_modify
    offset = None
    for off, line_no in linestarts.items():
        if line_no == before_line:
            offset = off

    return_none_size = len(_return_none_fun.__code__.co_code)
    code_to_insert_obj = code_to_insert.co_code[:-return_none_size]
    try:
        code_to_insert_obj, new_names = \
            _add_attr_values_from_insert_to_original(code_to_modify, code_to_insert, code_to_insert_obj, 'co_names', dis.hasname)
        code_to_insert_obj, new_consts = \
            _add_attr_values_from_insert_to_original(code_to_modify, code_to_insert, code_to_insert_obj, 'co_consts', [opmap['LOAD_CONST']])
        code_to_insert_obj, new_vars = \
            _add_attr_values_from_insert_to_original(code_to_modify, code_to_insert, code_to_insert_obj, 'co_varnames', dis.haslocal)
        modified_code = _update_label_offsets(code_to_modify.co_code, offset, len(code_to_insert_obj))
        new_bytes = modified_code[:offset] + code_to_insert_obj + modified_code[offset:]

        new_lnotab = _modify_new_lines(code_to_modify, code_to_insert_obj, offset)
    except ValueError:
        return False, code_to_modify

    new_code = CodeType(
        code_to_modify.co_argcount,  # integer
        code_to_modify.co_kwonlyargcount,  # integer
        len(new_vars),  # integer
        code_to_modify.co_stacksize,  # integer
        code_to_modify.co_flags,  # integer
        new_bytes,  # bytes
        new_consts,  # tuple
        new_names,  # tuple
        new_vars,  # tuple
        code_to_modify.co_filename,  # string
        code_to_modify.co_name,  # string
        code_to_modify.co_firstlineno,  # integer
        new_lnotab,  # bytes
        code_to_modify.co_freevars,  # tuple
        code_to_modify.co_cellvars  # tuple
    )
    return True, new_code
<commit_msg>A proper fix for bytecode overflow (PY-23022)<commit_after>import dis
import traceback
from opcode import opmap, EXTENDED_ARG
from types import CodeType

MAX_BYTE = 255

def _add_attr_values_from_insert_to_original(original_code, insert_code, insert_code_obj, attribute_name, op_list):
    """
    This function appends values of the attribute `attribute_name` of the inserted code to the original values,
     and changes indexes inside inserted code. If some bytecode instruction in the inserted code used to call argument
     number i, after modification it calls argument n + i, where n - length of the values in the original code.
     So it helps to avoid variables mixing between two pieces of code.

    :param original_code: code to modify
    :param insert_code: code to insert
    :param insert_code_obj: bytes sequence of inserted code, which should be modified too
    :param attribute_name: name of attribute to modify ('co_names', 'co_consts' or 'co_varnames')
    :param op_list: sequence of bytecodes whose arguments should be changed
    :return: modified bytes sequence of the code to insert and new values of the attribute `attribute_name` for
    original code
    """
    orig_value = getattr(original_code, attribute_name)
    insert_value = getattr(insert_code, attribute_name)
    orig_names_len = len(orig_value)
    code_with_new_values = list(insert_code_obj)
    offset = 0
    while offset < len(code_with_new_values):
        op = code_with_new_values[offset]
        if op in op_list:
            new_val = code_with_new_values[offset + 1] + orig_names_len
            if new_val > MAX_BYTE:
                code_with_new_values[offset + 1] = new_val & MAX_BYTE
                code_with_new_values = code_with_new_values[:offset] + [EXTENDED_ARG, new_val >> 8] + \
                                       code_with_new_values[offset:]
                offset += 2
            else:
                code_with_new_values[offset + 1] = new_val
        offset += 2
    new_values = orig_value + insert_value
    return bytes(code_with_new_values), new_values

def _modify_new_lines(code_to_modify, all_inserted_code):
    """
    Update new lines in order to hide inserted code inside the original code
    :param code_to_modify: code to modify
    :param all_inserted_code: list of tuples (offset, list of code instructions) with all inserted pieces of code
    :return: bytes sequence of code with updated lines offsets
    """
    new_list = list(code_to_modify.co_lnotab)
    abs_offset = prev_abs_offset = 0
    i = 0
    while i < len(new_list):
        prev_abs_offset = abs_offset
        abs_offset += new_list[i]
        for (inserted_offset, inserted_code) in all_inserted_code:
            if prev_abs_offset <= inserted_offset < abs_offset:
                size_of_inserted = len(inserted_code)
                new_list[i] += size_of_inserted
                abs_offset += size_of_inserted
        if new_list[i] > MAX_BYTE:
            new_list[i] = new_list[i] - MAX_BYTE
            new_list = new_list[:i] + [MAX_BYTE, 0] + new_list[i:]
        i += 2
    return bytes(new_list)

def _update_label_offsets(code_obj, breakpoint_offset, breakpoint_code_list):
    """
    Update labels for the relative and absolute jump targets
    :param code_obj: code to modify
    :param breakpoint_offset: offset for the inserted code
    :param breakpoint_code_list: size of the inserted code
    :return: bytes sequence with modified labels; list of tuples (resulting offset, list of code instructions) with
    information about all inserted pieces of code
    """
    inserted_code = list()
    # the list with all inserted pieces of code
    inserted_code.append((breakpoint_offset, breakpoint_code_list))
    code_list = list(code_obj)
    j = 0

    while j < len(inserted_code):
        current_offset, current_code_list = inserted_code[j]
        offsets_for_modification = []

        for offset, op, arg in dis._unpack_opargs(code_list):
            if arg is not None:
                if op in dis.hasjrel:
                    # has relative jump target
                    label = offset + 2 + arg
                    if offset < current_offset < label:
                        # change labels for relative jump targets if code was inserted inside
                        offsets_for_modification.append(offset)
                elif op in dis.hasjabs:
                    # change label for absolute jump if code was inserted before it
                    if current_offset <= arg:
                        offsets_for_modification.append(offset)
        for i in range(0, len(code_list), 2):
            op = code_list[i]
            if i in offsets_for_modification and op >= dis.HAVE_ARGUMENT:
                new_arg = code_list[i + 1] + len(current_code_list)
                if new_arg <= MAX_BYTE:
                    code_list[i + 1] = new_arg
                else:
                    # if new argument > 255 we need to insert the new operator EXTENDED_ARG
                    extended_arg_code = [EXTENDED_ARG, new_arg >> 8]
                    code_list[i + 1] = new_arg & MAX_BYTE
                    inserted_code.append((i, extended_arg_code))

        code_list = code_list[:current_offset] + current_code_list + code_list[current_offset:]

        for k in range(len(inserted_code)):
            offset, inserted_code_list = inserted_code[k]
            if current_offset < offset:
                inserted_code[k] = (offset + len(current_code_list), inserted_code_list)
        j += 1

    return bytes(code_list), inserted_code

def _return_none_fun():
    return None

def insert_code(code_to_modify, code_to_insert, before_line):
    """
    Insert piece of code `code_to_insert` to `code_to_modify` right inside the line `before_line` before the
    instruction on this line by modifying original bytecode

    :param code_to_modify: Code to modify
    :param code_to_insert: Code to insert
    :param before_line: Number of line for code insertion
    :return: boolean flag whether insertion was successful, modified code
    """
    linestarts = dict(dis.findlinestarts(code_to_modify))
    if before_line not in linestarts.values():
        return code_to_modify
    offset = None
    for off, line_no in linestarts.items():
        if line_no == before_line:
            offset = off

    return_none_size = len(_return_none_fun.__code__.co_code)
    code_to_insert_obj = code_to_insert.co_code[:-return_none_size]
    try:
        code_to_insert_obj, new_names = \
            _add_attr_values_from_insert_to_original(code_to_modify, code_to_insert, code_to_insert_obj, 'co_names',
                                                     dis.hasname)
        code_to_insert_obj, new_consts = \
            _add_attr_values_from_insert_to_original(code_to_modify, code_to_insert, code_to_insert_obj, 'co_consts',
                                                     [opmap['LOAD_CONST']])
        code_to_insert_obj, new_vars = \
            _add_attr_values_from_insert_to_original(code_to_modify, code_to_insert, code_to_insert_obj, 'co_varnames',
                                                     dis.haslocal)
        new_bytes, all_inserted_code = _update_label_offsets(code_to_modify.co_code, offset, list(code_to_insert_obj))

        new_lnotab = _modify_new_lines(code_to_modify, all_inserted_code)
    except ValueError:
        traceback.print_exc()
        return False, code_to_modify

    new_code = CodeType(
        code_to_modify.co_argcount,  # integer
        code_to_modify.co_kwonlyargcount,  # integer
        len(new_vars),  # integer
        code_to_modify.co_stacksize,  # integer
        code_to_modify.co_flags,  # integer
        new_bytes,  # bytes
        new_consts,  # tuple
        new_names,  # tuple
        new_vars,  # tuple
        code_to_modify.co_filename,  # string
        code_to_modify.co_name,  # string
        code_to_modify.co_firstlineno,  # integer
        new_lnotab,  # bytes
        code_to_modify.co_freevars,  # tuple
        code_to_modify.co_cellvars  # tuple
    )
    return True, new_code
<|endoftext|>


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

"""Here, I'll create two types of feeds:
    1. Feeds that return the content of a search.
    2. Feeds that return all of the cases for a court or for all courts
I'll do this in two formats:
    - first, I'll do Atom
    - second, if Erik Wilde thinks it's helpful, I'll do RSS 2.01 r2."""

from django.contrib.syndication.views import Feed
from django.contrib.syndication.views import FeedDoesNotExist
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import get_object_or_404
from django.utils import feedgenerator
from django.utils.feedgenerator import Atom1Feed

from alert.alertSystem.models import Court, Document

class searchFeed(Feed):
    """This feed returns the results of a search feed. It lacks a second
    argument in the method b/c it gets its search query from a GET request."""
    feed_type = Atom1Feed
##    try:
#    query = Feed[request].GET['q']
#    print query
##    except:
##        query = ""
###        print "2" + query
##    print query

    # get the court info from the URL
    def get_object(self, request, query):
        try:
            query = request.GET['q']
        except:
            query = ''
        return query

    def title(self, obj):
        return "CourtListener.com results for the query: \"" + obj + "\""

    def link(self, obj):
        return '/feed/search/' + obj + '/'

    author_name = "CourtListener.com"
    author_email = "feeds@courtlistener.com"

    def items(self, obj):
        # Do a Sphinx query here. Return the first 20 results
        try:
            queryset = Document.search.query(obj)
            results = queryset.set_options(mode="SPH_MATCH_EXTENDED2")\
                .order_by('-dateFiled')
        except:
            results = []
        return results

    def item_author_name(self, item):
        return item.court

    def item_author_link(self, item):
        return item.court.courtURL

    def item_pubdate(self, item):
        import datetime
        return datetime.datetime.combine(item.dateFiled, datetime.time())

    def item_categories(self, item):
        cat = [item.get_documentType_display(),]
        return cat

    description_template = 'feeds/template.html'

class courtFeed(Feed):
    """This feed returns the cases for a court, and accepts courts of the value:
    ca1, ca2..."""
    feed_type = Atom1Feed

    # get the court info from the URL
    def get_object(self, request, court):
        return get_object_or_404(Court, courtUUID=court)

    def title(self, obj):
        return "CourtListener.com: All opinions for the " + obj.get_courtUUID_display()

    def link(self, obj):
        return '/feed/court/' + obj.courtUUID + '/'

    author_name = "CourtListener.com"
    author_email = "feeds@courtlistener.com"

    def items(self, obj):
        return Document.objects.filter(court = obj.courtUUID).order_by("-dateFiled")[:20]

    def item_author_name(self, item):
        return item.court

    def item_author_link(self, item):
        return item.court.courtURL

    def item_pubdate(self, item):
        import datetime
        return datetime.datetime.combine(item.dateFiled, datetime.time())

    def item_categories(self, item):
        cat = [item.get_documentType_display(),]
        return cat

    description_template = 'feeds/template.html'

class allCourtsFeed(Feed):
    """This feed returns the cases for all courts"""
    feed_type = Atom1Feed

    def title(self):
        return "CourtListener.com: All opinions for the circuit courts"

    def link(self):
        return '/feed/court/all/'

    author_name = "CourtListener.com"
    author_email = "feeds@courtlistener.com"

    def items(self, obj):
        return Document.objects.all().order_by("-dateFiled")[:20]

    def item_author_name(self, item):
        return item.court

    def item_author_link(self, item):
        return item.court.courtURL

    def item_pubdate(self, item):
        import datetime
        return datetime.datetime.combine(item.dateFiled, datetime.time())

    def item_categories(self, item):
        cat = [item.get_documentType_display(),]
        return cat

    description_template = 'feeds/template.html'
<commit_msg>Limiting search feeds to 20 items.<commit_after># This software and any associated files are copyright 2010 Brian Carver and
# Michael Lissner.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Here, I'll create two types of feeds:
    1. Feeds that return the content of a search.
    2. Feeds that return all of the cases for a court or for all courts
I'll do this in two formats:
    - first, I'll do Atom
    - second, if Erik Wilde thinks it's helpful, I'll do RSS 2.01 r2."""

from django.contrib.syndication.views import Feed
from django.contrib.syndication.views import FeedDoesNotExist
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import get_object_or_404
from django.utils import feedgenerator
from django.utils.feedgenerator import Atom1Feed

from alert.alertSystem.models import Court, Document

class searchFeed(Feed):
    """This feed returns the results of a search feed. It lacks a second
    argument in the method b/c it gets its search query from a GET request."""
    feed_type = Atom1Feed
##    try:
#    query = Feed[request].GET['q']
#    print query
##    except:
##        query = ""
###        print "2" + query
##    print query

    # get the court info from the URL
    def get_object(self, request, query):
        try:
            query = request.GET['q']
        except:
            query = ''
        return query

    def title(self, obj):
        return "CourtListener.com results for the query: \"" + obj + "\""

    def link(self, obj):
        return '/feed/search/' + obj + '/'

    author_name = "CourtListener.com"
    author_email = "feeds@courtlistener.com"

    def items(self, obj):
        # Do a Sphinx query here. Return the first 20 results
        try:
            queryset = Document.search.query(obj)
            results = queryset.set_options(mode="SPH_MATCH_EXTENDED2")\
                .order_by('-dateFiled')[:20]
        except:
            results = []
        return results

    def item_author_name(self, item):
        return item.court

    def item_author_link(self, item):
        return item.court.courtURL

    def item_pubdate(self, item):
        import datetime
        return datetime.datetime.combine(item.dateFiled, datetime.time())

    def item_categories(self, item):
        cat = [item.get_documentType_display(),]
        return cat

    description_template = 'feeds/template.html'

class courtFeed(Feed):
    """This feed returns the cases for a court, and accepts courts of the value:
    ca1, ca2..."""
    feed_type = Atom1Feed

    # get the court info from the URL
    def get_object(self, request, court):
        return get_object_or_404(Court, courtUUID=court)

    def title(self, obj):
        return "CourtListener.com: All opinions for the " + obj.get_courtUUID_display()

    def link(self, obj):
        return '/feed/court/' + obj.courtUUID + '/'

    author_name = "CourtListener.com"
    author_email = "feeds@courtlistener.com"

    def items(self, obj):
        return Document.objects.filter(court = obj.courtUUID).order_by("-dateFiled")[:20]

    def item_author_name(self, item):
        return item.court

    def item_author_link(self, item):
        return item.court.courtURL

    def item_pubdate(self, item):
        import datetime
        return datetime.datetime.combine(item.dateFiled, datetime.time())

    def item_categories(self, item):
        cat = [item.get_documentType_display(),]
        return cat

    description_template = 'feeds/template.html'

class allCourtsFeed(Feed):
    """This feed returns the cases for all courts"""
    feed_type = Atom1Feed

    def title(self):
        return "CourtListener.com: All opinions for the circuit courts"

    def link(self):
        return '/feed/court/all/'

    author_name = "CourtListener.com"
    author_email = "feeds@courtlistener.com"

    def items(self, obj):
        return Document.objects.all().order_by("-dateFiled")[:20]

    def item_author_name(self, item):
        return item.court

    def item_author_link(self, item):
        return item.court.courtURL

    def item_pubdate(self, item):
        import datetime
        return datetime.datetime.combine(item.dateFiled, datetime.time())

    def item_categories(self, item):
        cat = [item.get_documentType_display(),]
        return cat

    description_template = 'feeds/template.html'
<|endoftext|>


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

import os
import random
import sys

import argparse
import logging
import numpy as np
import pandas as pd

from keras.backend import _BACKEND
from keras.models import Model
from keras.optimizers import Adam

from keras import callbacks as kcbk

from deepcpg import callbacks as cbk
from deepcpg import data as dat
from deepcpg import models as mod
from deepcpg import utils as ut

# TODO:
# Check class weights

def get_class_weights(data_files, output_names):
    names = {'outputs': output_names}
    weights = {}
    for name, output in dat.h5_read(data_files, names).items():
        output = output[output != dat.CPG_NAN]
        frac_ones = np.sum(output == 1) / len(output)
        weights[name.replace('outputs/', '')] = {0: 1 - frac_ones, 1: frac_ones}
    return weights

def perf_logs_str(logs):
    t = logs.to_csv(None, sep='\t', float_format='%.4f', index=False)
    return t

class App(object):

    def run(self, args):
        name = os.path.basename(args[0])
        parser = self.create_parser(name)
        opts = parser.parse_args(args[1:])
        return self.main(name, opts)

    def create_parser(self, name):
        p = argparse.ArgumentParser(
            prog=name,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            description='Train model')
        p.add_argument(
            'train_files',
            nargs='+',
            help='Training data files')
        p.add_argument(
            '--val_files',
            nargs='+',
            help='Validation data files')
        p.add_argument(
            '-o', '--out_dir',
            default='./train',
            help='Output directory')
        p.add_argument(
            '--output_names',
            help='List of regex to filter outputs',
            nargs='+')
        p.add_argument(
            '--nb_output',
            type=int,
            help='Maximum number of outputs')
        p.add_argument(
            '--replicate_names',
            help='List of regex to filter CpG context units',
            nargs='+')
        p.add_argument(
            '--model_name',
            help='Model name',
            default='dna01')
        p.add_argument(
            '--nb_epoch',
            help='Maximum # training epochs',
            type=int,
            default=100)
        p.add_argument(
            '--batch_size',
            help='Batch size',
            type=int,
            default=128)
        p.add_argument(
            '--early_stopping',
            help='Early stopping patience',
            type=int,
            default=3)
        p.add_argument(
            '--lr',
            help='Learning rate',
            type=float,
            default=0.0001)
        p.add_argument(
            '--lr_decay',
            help='Exponential learning rate decay factor',
            type=float,
            default=0.95)
        p.add_argument(
            '--nb_train_sample',
            help='Maximum # training samples per epoch',
            type=int)
        p.add_argument(
            '--nb_val_sample',
            help='Maximum # validation samples per epoch',
            type=int)
        p.add_argument(
            '--max_time',
            help='Maximum training time in hours',
            type=float)
        p.add_argument(
            '--seed',
            help='Seed of rng',
            type=int,
            default=0)
        p.add_argument(
            '--verbose',
            help='More detailed log messages',
            action='store_true')
        p.add_argument(
            '--log_file',
            help='Write log messages to file')
        p.add_argument(
            '--dna_wlen',
            help='DNA window length',
            type=int)
        p.add_argument(
            '--cpg_wlen',
            help='CpG window length',
            type=int)
        p.add_argument(
            '--dropout',
            help='Dropout rate',
            type=float,
            default=0.0)
        p.add_argument(
            '--l1_decay',
            help='L1 weight decay',
            type=float,
            default=0.0)
        p.add_argument(
            '--l2_decay',
            help='L2 weight decay',
            type=float,
            default=0.0)
        p.add_argument(
            '--data_q_size',
            help='Size of data generator queue',
            type=int,
            default=10)
        p.add_argument(
            '--data_nb_worker',
            help='Number of worker for data generator queue',
            type=int,
            default=1)
        return p

    def callbacks(self):
        opts = self.opts
        cbacks = []

        if opts.val_files:
            h = kcbk.EarlyStopping('val_loss' if opts.val_files else 'loss',
                                   patience=opts.early_stopping,
                                   verbose=1)
            cbacks.append(h)

        h = kcbk.ModelCheckpoint(os.path.join(opts.out_dir,
                                              'model_weights_last.h5'),
                                 save_best_only=False)
        cbacks.append(h)
        monitor = 'val_loss' if opts.val_files else 'loss'
        h = kcbk.ModelCheckpoint(os.path.join(opts.out_dir, 'model_weights.h5'),
                                 monitor=monitor,
                                 save_best_only=True, verbose=1)
        cbacks.append(h)

        if opts.max_time is not None:
            cbacks.append(cbk.Timer(opts.max_time * 3600 * 0.8))

        h = kcbk.LearningRateScheduler(
            lambda epoch: opts.lr * opts.lr_decay**epoch)
        cbacks.append(h)

        def save_lc(epoch, epoch_logs, val_epoch_logs):
            logs = {'lc.csv': epoch_logs, 'lc_val.csv': val_epoch_logs}
            for name, logs in logs.items():
                if not logs:
                    continue
                logs = pd.DataFrame(logs)
                with open(os.path.join(opts.out_dir, name), 'w') as f:
                    f.write(perf_logs_str(logs))

        self.perf_logger = cbk.PerformanceLogger(callbacks=[save_lc])
        cbacks.append(self.perf_logger)

        if _BACKEND == 'tensorflow':
            h = kcbk.TensorBoard(
                log_dir=os.path.join(opts.out_dir, 'logs'),
                histogram_freq=1,
                write_graph=True,
                write_images=False)
            cbacks.append(h)

        return cbacks

    def main(self, name, opts):
        logging.basicConfig(filename=opts.log_file,
                            format='%(levelname)s (%(asctime)s): %(message)s')
        log = logging.getLogger(name)
        if opts.verbose:
            log.setLevel(logging.DEBUG)
        else:
            log.setLevel(logging.INFO)

        if opts.seed is not None:
            np.random.seed(opts.seed)
            random.seed(opts.seed)

        self.log = log
        self.opts = opts

        # Create output directory if not existing
        if not os.path.exists(opts.out_dir):
            os.makedirs(opts.out_dir, exist_ok=True)

        # Setup callbacks
        log.info('Initializing callbacks ...')
        cbacks = self.callbacks()

        log.info('Building model ...')
        output_names = dat.h5_ls(opts.train_files[0], 'outputs',
                                 opts.output_names, opts.nb_output)
        class_weights = get_class_weights(opts.train_files, output_names)

        model_builder = mod.get_class(opts.model_name)

        if opts.model_name.lower().startswith('dna'):
            dna_wlen = dat.get_dna_wlen(opts.train_files[0], opts.dna_wlen)
            model_builder = model_builder(dna_wlen=dna_wlen,
                                          dropout=opts.dropout,
                                          l1_decay=opts.l1_decay,
                                          l2_decay=opts.l2_decay)

        elif opts.model_name.lower().startswith('cpg'):
            cpg_wlen = dat.get_cpg_wlen(opts.train_files[0], opts.cpg_wlen)
            replicate_names = dat.h5_ls(opts.train_files[0], 'inputs/cpg',
                                        opts.replicate_names)
            model_builder = model_builder(replicate_names,
                                          cpg_wlen=cpg_wlen,
                                          dropout=opts.dropout,
                                          l1_decay=opts.l1_decay,
                                          l2_decay=opts.l2_decay)
        else:
            dna_wlen = dat.get_dna_wlen(opts.train_files[0], opts.dna_wlen)
            cpg_wlen = dat.get_cpg_wlen(opts.train_files[0], opts.cpg_wlen)
            replicate_names = dat.h5_ls(opts.train_files[0], 'inputs/cpg',
                                        opts.replicate_names)
            model_builder = model_builder(replicate_names=replicate_names,
                                          cpg_wlen=cpg_wlen,
                                          dna_wlen=dna_wlen,
                                          dropout=opts.dropout,
                                          l1_decay=opts.l1_decay,
                                          l2_decay=opts.l2_decay)

        inputs = model_builder.inputs()
        stem = model_builder(inputs)
        outputs = mod.add_outputs(stem, output_names)
        model = Model(input=inputs, output=outputs, name=opts.model_name)
        model.summary()
        mod.save_model(model, os.path.join(opts.out_dir, 'model.json'))

        optimizer = Adam(lr=opts.lr)
        log.info('Compile model ...')
        model.compile(optimizer=optimizer, loss='binary_crossentropy',
                      metrics=['accuracy'])

        log.info('Reading data ...')
        nb_train_sample = dat.get_nb_sample(opts.train_files,
                                            opts.nb_train_sample,
                                            opts.batch_size)
        train_data = model_builder.reader(opts.train_files,
                                          output_names=output_names,
                                          batch_size=opts.batch_size,
                                          nb_sample=nb_train_sample,
                                          loop=True, shuffle=True,
                                          class_weights=class_weights)
        if opts.val_files:
            nb_val_sample = dat.get_nb_sample(opts.val_files,
                                              opts.nb_val_sample,
                                              opts.batch_size)
            val_data = model_builder.reader(opts.val_files,
                                            output_names=output_names,
                                            batch_size=opts.batch_size,
                                            nb_sample=nb_val_sample,
                                            loop=True, shuffle=False,
                                            class_weights=class_weights)
        else:
            val_data = None
            nb_val_sample = None

        # Train model
        log.info('Training model ...')
        print()
        model.fit_generator(
            train_data, nb_train_sample, opts.nb_epoch,
            callbacks=cbacks,
            validation_data=val_data,
            nb_val_samples=nb_val_sample,
            max_q_size=opts.data_q_size,
            nb_worker=opts.data_nb_worker,
            verbose=1 if opts.verbose else 0)

        # Use best weights on validation set
        h = os.path.join(opts.out_dir, 'model_weights.h5')
        if os.path.isfile(h):
            model.load_weights(h)

        model.save(os.path.join(opts.out_dir, 'model.h5'))

        print('\nTraining set performance:')
        print(ut.format_table(self.perf_logger.epoch_logs))

        if self.perf_logger.val_epoch_logs:
            print('\nValidation set performance:')
            print(ut.format_table(self.perf_logger.val_epoch_logs))

        log.info('Done!')

        return 0

if __name__ == '__main__':
    app = App()
    app.run(sys.argv)
<commit_msg>Minor changes<commit_after>#!/usr/bin/env python

import os
import random
import sys

import argparse
import logging
import numpy as np
import pandas as pd

from keras.backend import _BACKEND
from keras.models import Model
from keras.optimizers import Adam

from keras import callbacks as kcbk

from deepcpg import callbacks as cbk
from deepcpg import data as dat
from deepcpg import models as mod
from deepcpg import utils as ut

# TODO:
# Check class weights

def get_class_weights(data_files, output_names):
    names = {'outputs': output_names}
    weights = {}
    for name, output in dat.h5_read(data_files, names).items():
        output = output[output != dat.CPG_NAN]
        frac_ones = np.sum(output == 1) / len(output)
        weights[name.replace('outputs/', '')] = {0: 1 - frac_ones, 1: frac_ones}
    return weights

def perf_logs_str(logs):
    t = logs.to_csv(None, sep='\t', float_format='%.4f', index=False)
    return t

class App(object):

    def run(self, args):
        name = os.path.basename(args[0])
        parser = self.create_parser(name)
        opts = parser.parse_args(args[1:])
        return self.main(name, opts)

    def create_parser(self, name):
        p = argparse.ArgumentParser(
            prog=name,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            description='Train model')
        p.add_argument(
            'train_files',
            nargs='+',
            help='Training data files')
        p.add_argument(
            '--val_files',
            nargs='+',
            help='Validation data files')
        p.add_argument(
            '-o', '--out_dir',
            default='./train',
            help='Output directory')
        p.add_argument(
            '--output_names',
            help='List of regex to filter outputs',
            nargs='+')
        p.add_argument(
            '--nb_output',
            type=int,
            help='Maximum number of outputs')
        p.add_argument(
            '--replicate_names',
            help='List of regex to filter CpG context units',
            nargs='+')
        p.add_argument(
            '--model_name',
            help='Model name',
            default='dna01')
        p.add_argument(
            '--nb_epoch',
            help='Maximum # training epochs',
            type=int,
            default=100)
        p.add_argument(
            '--batch_size',
            help='Batch size',
            type=int,
            default=128)
        p.add_argument(
            '--early_stopping',
            help='Early stopping patience',
            type=int,
            default=3)
        p.add_argument(
            '--lr',
            help='Learning rate',
            type=float,
            default=0.0001)
        p.add_argument(
            '--lr_decay',
            help='Exponential learning rate decay factor',
            type=float,
            default=0.95)
        p.add_argument(
            '--nb_train_sample',
            help='Maximum # training samples per epoch',
            type=int)
        p.add_argument(
            '--nb_val_sample',
            help='Maximum # validation samples per epoch',
            type=int)
        p.add_argument(
            '--max_time',
            help='Maximum training time in hours',
            type=float)
        p.add_argument(
            '--seed',
            help='Seed of rng',
            type=int,
            default=0)
        p.add_argument(
            '--verbose',
            help='More detailed log messages',
            action='store_true')
        p.add_argument(
            '--log_file',
            help='Write log messages to file')
        p.add_argument(
            '--dna_wlen',
            help='DNA window length',
            type=int)
        p.add_argument(
            '--cpg_wlen',
            help='CpG window length',
            type=int)
        p.add_argument(
            '--dropout',
            help='Dropout rate',
            type=float,
            default=0.0)
        p.add_argument(
            '--l1_decay',
            help='L1 weight decay',
            type=float,
            default=0.0)
        p.add_argument(
            '--l2_decay',
            help='L2 weight decay',
            type=float,
            default=0.0)
        p.add_argument(
            '--data_q_size',
            help='Size of data generator queue',
            type=int,
            default=10)
        p.add_argument(
            '--data_nb_worker',
            help='Number of worker for data generator queue',
            type=int,
            default=1)
        return p

    def callbacks(self):
        opts = self.opts
        cbacks = []

        if opts.val_files:
            h = kcbk.EarlyStopping('val_loss' if opts.val_files else 'loss',
                                   patience=opts.early_stopping,
                                   verbose=1)
            cbacks.append(h)

        h = kcbk.ModelCheckpoint(os.path.join(opts.out_dir,
                                              'model_weights_last.h5'),
                                 save_best_only=False)
        cbacks.append(h)
        monitor = 'val_loss' if opts.val_files else 'loss'
        h = kcbk.ModelCheckpoint(os.path.join(opts.out_dir, 'model_weights.h5'),
                                 monitor=monitor,
                                 save_best_only=True, verbose=1)
        cbacks.append(h)

        if opts.max_time is not None:
            cbacks.append(cbk.Timer(opts.max_time * 3600 * 0.8))

        h = kcbk.LearningRateScheduler(
            lambda epoch: opts.lr * opts.lr_decay**epoch)
        cbacks.append(h)

        def save_lc(epoch, epoch_logs, val_epoch_logs):
            logs = {'lc.csv': epoch_logs, 'lc_val.csv': val_epoch_logs}
            for name, logs in logs.items():
                if not logs:
                    continue
                logs = pd.DataFrame(logs)
                with open(os.path.join(opts.out_dir, name), 'w') as f:
                    f.write(perf_logs_str(logs))

        self.perf_logger = cbk.PerformanceLogger(callbacks=[save_lc])
        cbacks.append(self.perf_logger)

        if _BACKEND == 'tensorflow':
            h = kcbk.TensorBoard(
                log_dir=os.path.join(opts.out_dir, 'logs'),
                histogram_freq=1,
                write_graph=True,
                write_images=True)
            cbacks.append(h)

        return cbacks

    def main(self, name, opts):
        logging.basicConfig(filename=opts.log_file,
                            format='%(levelname)s (%(asctime)s): %(message)s')
        log = logging.getLogger(name)
        if opts.verbose:
            log.setLevel(logging.DEBUG)
        else:
            log.setLevel(logging.INFO)

        if opts.seed is not None:
            np.random.seed(opts.seed)
            random.seed(opts.seed)

        self.log = log
        self.opts = opts

        # Create output directory if not existing
        if not os.path.exists(opts.out_dir):
            os.makedirs(opts.out_dir, exist_ok=True)

        # Setup callbacks
        log.info('Initializing callbacks ...')
        cbacks = self.callbacks()

        log.info('Building model ...')
        output_names = dat.h5_ls(opts.train_files[0], 'outputs',
                                 opts.output_names, opts.nb_output)
        class_weights = get_class_weights(opts.train_files, output_names)

        model_builder = mod.get_class(opts.model_name)

        if opts.model_name.lower().startswith('dna'):
            dna_wlen = dat.get_dna_wlen(opts.train_files[0], opts.dna_wlen)
            model_builder = model_builder(dna_wlen=dna_wlen,
                                          dropout=opts.dropout,
                                          l1_decay=opts.l1_decay,
                                          l2_decay=opts.l2_decay)

        elif opts.model_name.lower().startswith('cpg'):
            cpg_wlen = dat.get_cpg_wlen(opts.train_files[0], opts.cpg_wlen)
            replicate_names = dat.h5_ls(opts.train_files[0], 'inputs/cpg',
                                        opts.replicate_names)
            model_builder = model_builder(replicate_names,
                                          cpg_wlen=cpg_wlen,
                                          dropout=opts.dropout,
                                          l1_decay=opts.l1_decay,
                                          l2_decay=opts.l2_decay)
        else:
            dna_wlen = dat.get_dna_wlen(opts.train_files[0], opts.dna_wlen)
            cpg_wlen = dat.get_cpg_wlen(opts.train_files[0], opts.cpg_wlen)
            replicate_names = dat.h5_ls(opts.train_files[0], 'inputs/cpg',
                                        opts.replicate_names)
            model_builder = model_builder(replicate_names=replicate_names,
                                          cpg_wlen=cpg_wlen,
                                          dna_wlen=dna_wlen,
                                          dropout=opts.dropout,
                                          l1_decay=opts.l1_decay,
                                          l2_decay=opts.l2_decay)

        inputs = model_builder.inputs()
        stem = model_builder(inputs)
        outputs = mod.add_outputs(stem, output_names)
        model = Model(input=inputs, output=outputs, name=opts.model_name)
        model.summary()
        mod.save_model(model, os.path.join(opts.out_dir, 'model.json'))

        optimizer = Adam(lr=opts.lr)
        log.info('Compile model ...')
        model.compile(optimizer=optimizer, loss='binary_crossentropy',
                      metrics=['accuracy'])

        log.info('Reading data ...')
        nb_train_sample = dat.get_nb_sample(opts.train_files,
                                            opts.nb_train_sample,
                                            opts.batch_size)
        train_data = model_builder.reader(opts.train_files,
                                          output_names=output_names,
                                          batch_size=opts.batch_size,
                                          nb_sample=nb_train_sample,
                                          loop=True, shuffle=True,
                                          class_weights=class_weights)
        if opts.val_files:
            nb_val_sample = dat.get_nb_sample(opts.val_files,
                                              opts.nb_val_sample,
                                              opts.batch_size)
            val_data = model_builder.reader(opts.val_files,
                                            output_names=output_names,
                                            batch_size=opts.batch_size,
                                            nb_sample=nb_val_sample,
                                            loop=True, shuffle=False,
                                            class_weights=class_weights)
        else:
            val_data = None
            nb_val_sample = None

        # Train model
        log.info('Training model ...')
        print()
        model.fit_generator(
            train_data, nb_train_sample, opts.nb_epoch,
            callbacks=cbacks,
            validation_data=val_data,
            nb_val_samples=nb_val_sample,
            max_q_size=opts.data_q_size,
            nb_worker=opts.data_nb_worker,
            verbose=1 if opts.verbose else 0)

        # Use best weights on validation set
        h = os.path.join(opts.out_dir, 'model_weights.h5')
        if os.path.isfile(h):
            model.load_weights(h)

        model.save(os.path.join(opts.out_dir, 'model.h5'))

        print('\nTraining set performance:')
        print(ut.format_table(self.perf_logger.epoch_logs))

        if self.perf_logger.val_epoch_logs:
            print('\nValidation set performance:')
            print(ut.format_table(self.perf_logger.val_epoch_logs))

        log.info('Done!')

        return 0

if __name__ == '__main__':
    app = App()
    app.run(sys.argv)
<|endoftext|>


<commit_before># -*- Mode: Python; test-case-name: flumotion.test.test_dialogs -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# Flumotion - a streaming media server
# Copyright (C) 2004,2005 Fluendo, S.L. (www.fluendo.com). All rights reserved.

# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "LICENSE.GPL" in the source distribution for more information.

# Licensees having purchased or holding a valid Flumotion Advanced
# Streaming Server license may use this file in accordance with the
# Flumotion Advanced Streaming Server Commercial License Agreement.
# See "LICENSE.Flumotion" in the source distribution for more information.

# Headers in this file shall remain intact.

from twisted.trial import unittest

import common

import gobject
import gtk

from flumotion.admin.gtk import dialogs

INTERVAL = 100 # in ms

class TestProgressDialog(unittest.TestCase):
    def setUp(self):
        self.window = gtk.Window()

    def tearDown(self):
        self.window.destroy()

    def testDialog(self):
        dialog = dialogs.ProgressDialog("I am busy",
            'Doing lots of complicated stuff', self.window)
        dialog.start()

        def stop(dialog):
            dialog.stop()
            gtk.main_quit()

        gobject.timeout_add(1 * INTERVAL,
            lambda dialog: dialog.message('Step 1'), dialog)
        gobject.timeout_add(2 * INTERVAL,
            lambda dialog: dialog.message(
            'Step 2 but with a lot longer text so we test shrinking'), dialog)
        gobject.timeout_add(3 * INTERVAL,
            lambda dialog: dialog.message('Step 3'), dialog)
        gobject.timeout_add(5 * INTERVAL, stop, dialog)
        gtk.main()

class TestPropertyChangeDialog(unittest.TestCase):
    def setUp(self):
        self.window = gtk.Window()

    def tearDown(self):
        self.window.destroy()

    def testDialog(self):
        dialog = dialogs.PropertyChangeDialog("element", self.window)
        dialog.connect('get', self.get_cb)
        dialog.connect('set', self.set_cb)
        dialog.show_all()

        dialog.element_entry.set_text('fakesrc')
        dialog.property_entry.set_text('silent')
        gobject.timeout_add(1 * INTERVAL,
            lambda b: b.emit('clicked'), dialog._fetch)
        gtk.main()

        # make sure it was hidden
        self.failUnlessEqual(dialog.get_property('visible'), gtk.FALSE)

    def get_cb(self, dialog, element, property):
        self.failUnlessEqual(dialog.get_property('visible'), gtk.TRUE)

        self.failUnlessEqual(element, 'fakesrc')
        self.failUnlessEqual(property, 'silent')
        dialog.update_value_entry(gtk.FALSE)
        dialog.value_entry.set_text('True')
        dialog._set.emit('clicked')

    def set_cb(self, dialog, element, property, value):
        self.failUnlessEqual(element, 'fakesrc')
        self.failUnlessEqual(property, 'silent')
        # FIXME: should be possible to do gtk.TRUE
        self.failUnlessEqual(value, 'True')
        dialog._close.emit('clicked')
        gtk.main_quit()

class TestErrorDialog(unittest.TestCase):
    def setUp(self):
        self.window = gtk.Window()

    def tearDown(self):
        self.window.destroy()

    def testDialogMain(self):
        dialog = dialogs.ErrorDialog("I am a test error message", self.window)
        self.failUnlessEqual(dialog.get_property('visible'), gtk.FALSE)
        dialog.show_all()
        self.failUnlessEqual(dialog.get_property('visible'), gtk.TRUE)

        # find the button and "click" it
        hbox = dialog.action_area
        list = hbox.get_children()
        button = list[0]
        self.failUnless(isinstance(button, gtk.Button))

        gobject.timeout_add(1 * INTERVAL, lambda b: b.emit('clicked'), button)
        dialog.connect('unmap', lambda w: gtk.main_quit())

        gtk.main()

        self.failUnlessEqual(dialog.get_property('visible'), gtk.FALSE)

    def testDialogRun(self):
        dialog = dialogs.ErrorDialog("I am a test error message", self.window)
        self.failUnlessEqual(dialog.get_property('visible'), gtk.FALSE)
        dialog.show_all()
        self.failUnlessEqual(dialog.get_property('visible'), gtk.TRUE)

        # find the button and "click" it
        hbox = dialog.action_area
        list = hbox.get_children()
        button = list[0]
        self.failUnless(isinstance(button, gtk.Button))

        gobject.timeout_add(1 * INTERVAL, lambda b: b.emit('clicked'), button)
        dialog.run()

        self.failUnlessEqual(dialog.get_property('visible'), gtk.FALSE)

<commit_msg>remove some deprecations<commit_after># -*- Mode: Python; test-case-name: flumotion.test.test_dialogs -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# Flumotion - a streaming media server
# Copyright (C) 2004,2005 Fluendo, S.L. (www.fluendo.com). All rights reserved.

# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "LICENSE.GPL" in the source distribution for more information.

# Licensees having purchased or holding a valid Flumotion Advanced
# Streaming Server license may use this file in accordance with the
# Flumotion Advanced Streaming Server Commercial License Agreement.
# See "LICENSE.Flumotion" in the source distribution for more information.

# Headers in this file shall remain intact.

from twisted.trial import unittest

import common

import gobject
import gtk

from flumotion.admin.gtk import dialogs

INTERVAL = 100 # in ms

class TestProgressDialog(unittest.TestCase):
    def setUp(self):
        self.window = gtk.Window()

    def tearDown(self):
        self.window.destroy()

    def testDialog(self):
        dialog = dialogs.ProgressDialog("I am busy",
            'Doing lots of complicated stuff', self.window)
        dialog.start()

        def stop(dialog):
            dialog.stop()
            gtk.main_quit()

        gobject.timeout_add(1 * INTERVAL,
            lambda dialog: dialog.message('Step 1'), dialog)
        gobject.timeout_add(2 * INTERVAL,
            lambda dialog: dialog.message(
            'Step 2 but with a lot longer text so we test shrinking'), dialog)
        gobject.timeout_add(3 * INTERVAL,
            lambda dialog: dialog.message('Step 3'), dialog)
        gobject.timeout_add(5 * INTERVAL, stop, dialog)
        gtk.main()

class TestPropertyChangeDialog(unittest.TestCase):
    def setUp(self):
        self.window = gtk.Window()

    def tearDown(self):
        self.window.destroy()

    def testDialog(self):
        dialog = dialogs.PropertyChangeDialog("element", self.window)
        dialog.connect('get', self.get_cb)
        dialog.connect('set', self.set_cb)
        dialog.show_all()

        dialog.element_entry.set_text('fakesrc')
        dialog.property_entry.set_text('silent')
        gobject.timeout_add(1 * INTERVAL,
            lambda b: b.emit('clicked'), dialog._fetch)
        gtk.main()

        # make sure it was hidden
        self.failUnlessEqual(dialog.get_property('visible'), False)

    def get_cb(self, dialog, element, property):
        self.failUnlessEqual(dialog.get_property('visible'), True)

        self.failUnlessEqual(element, 'fakesrc')
        self.failUnlessEqual(property, 'silent')
        dialog.update_value_entry(False)
        dialog.value_entry.set_text('True')
        dialog._set.emit('clicked')

    def set_cb(self, dialog, element, property, value):
        self.failUnlessEqual(element, 'fakesrc')
        self.failUnlessEqual(property, 'silent')
        # FIXME: should be possible to do gtk.TRUE
        self.failUnlessEqual(value, 'True')
        dialog._close.emit('clicked')
        gtk.main_quit()

class TestErrorDialog(unittest.TestCase):
    def setUp(self):
        self.window = gtk.Window()

    def tearDown(self):
        self.window.destroy()

    def testDialogMain(self):
        dialog = dialogs.ErrorDialog("I am a test error message", self.window)
        self.failUnlessEqual(dialog.get_property('visible'), False)
        dialog.show_all()
        self.failUnlessEqual(dialog.get_property('visible'), True)

        # find the button and "click" it
        hbox = dialog.action_area
        list = hbox.get_children()
        button = list[0]
        self.failUnless(isinstance(button, gtk.Button))

        gobject.timeout_add(1 * INTERVAL, lambda b: b.emit('clicked'), button)
        dialog.connect('unmap', lambda w: gtk.main_quit())

        gtk.main()

        self.failUnlessEqual(dialog.get_property('visible'), False)

    def testDialogRun(self):
        dialog = dialogs.ErrorDialog("I am a test error message", self.window)
        self.failUnlessEqual(dialog.get_property('visible'), False)
        dialog.show_all()
        self.failUnlessEqual(dialog.get_property('visible'), True)

        # find the button and "click" it
        hbox = dialog.action_area
        list = hbox.get_children()
        button = list[0]
        self.failUnless(isinstance(button, gtk.Button))

        gobject.timeout_add(1 * INTERVAL, lambda b: b.emit('clicked'), button)
        dialog.run()

        self.failUnlessEqual(dialog.get_property('visible'), False)

<|endoftext|>