<commit_before># -*- coding: utf-8 -*-
"""
@brief      test log(time=180s)
"""

import sys
import os
import unittest
import warnings
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder
from pyquickhelper.pycode import add_missing_development_version

try:
    import src
except ImportError:
    path = os.path.normpath(
        os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "..",
                "..")))
    if path not in sys.path:
        sys.path.append(path)
    import src

class TestSKIPNotebookRunner2a_cffi (unittest.TestCase):

    def setUp(self):
        add_missing_development_version(["pymyinstall", "pyensae", "pymmails", "jyquickhelper"],
                                        __file__, hide=True)

    def test_notebook_runner_2a_cffi(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        from src.ensae_teaching_cs.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_1a
        temp = get_temp_folder(__file__, "temp_notebook2a_")
        keepnote = ls_notebooks("2a")

        def filter(i, n):
            if "cffi" not in n:
                return False
            return True

        execute_notebooks(temp, keepnote, filter, fLOG=fLOG,
                          clean_function=clean_function_1a,
                          dump=src.ensae_teaching_cs)

if __name__ == "__main__":
    unittest.main()
<commit_msg>remove one warning<commit_after># -*- coding: utf-8 -*-
"""
@brief      test log(time=180s)
"""

import sys
import os
import unittest
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder
from pyquickhelper.pycode import add_missing_development_version

try:
    import src
except ImportError:
    path = os.path.normpath(
        os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "..",
                "..")))
    if path not in sys.path:
        sys.path.append(path)
    import src

class TestSKIPNotebookRunner2a_cffi (unittest.TestCase):

    def setUp(self):
        add_missing_development_version(["pymyinstall", "pyensae", "pymmails", "jyquickhelper"],
                                        __file__, hide=True)

    def test_notebook_runner_2a_cffi(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        from src.ensae_teaching_cs.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_1a
        temp = get_temp_folder(__file__, "temp_notebook2a_")
        keepnote = ls_notebooks("2a")

        def filter(i, n):
            if "cffi" not in n:
                return False
            return True

        execute_notebooks(temp, keepnote, filter, fLOG=fLOG,
                          clean_function=clean_function_1a,
                          dump=src.ensae_teaching_cs)

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


<commit_before># MIT License
# Copyright (c) 2017 MassChallenge, Inc.

from __future__ import unicode_literals

import swapper
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from accelerator_abstract.models.accelerator_model import AcceleratorModel

@python_2_unicode_compatible
class BaseApplicationAnswer(AcceleratorModel):
    application = models.ForeignKey(to=swapper.get_model_name(
        AcceleratorModel.Meta.app_label, "Application"),
        on_delete=models.CASCADE)
    application_question = models.ForeignKey(swapper.get_model_name(
        AcceleratorModel.Meta.app_label, 'ApplicationQuestion'),
        on_delete=models.CASCADE)
    answer_text = models.CharField(max_length=2000, blank=True)

    class Meta(AcceleratorModel.Meta):
        verbose_name_plural = 'Application Answers'
        db_table = '{}_applicationanswer'.format(
            AcceleratorModel.Meta.app_label)
        abstract = True
        unique_together = ('application', 'application_question')

    def __str__(self):
        return "Answer to question %s from %s" % (
            self.application_question.question_number,
            self.application.startup.name)
<commit_msg>[AC-8296] Change field type for answer_text<commit_after># MIT License
# Copyright (c) 2017 MassChallenge, Inc.

from __future__ import unicode_literals

import swapper
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from accelerator_abstract.models.accelerator_model import AcceleratorModel

@python_2_unicode_compatible
class BaseApplicationAnswer(AcceleratorModel):
    application = models.ForeignKey(to=swapper.get_model_name(
        AcceleratorModel.Meta.app_label, "Application"),
        on_delete=models.CASCADE)
    application_question = models.ForeignKey(swapper.get_model_name(
        AcceleratorModel.Meta.app_label, 'ApplicationQuestion'),
        on_delete=models.CASCADE)
    answer_text = models.TextField(blank=True)

    class Meta(AcceleratorModel.Meta):
        verbose_name_plural = 'Application Answers'
        db_table = '{}_applicationanswer'.format(
            AcceleratorModel.Meta.app_label)
        abstract = True
        unique_together = ('application', 'application_question')

    def __str__(self):
        return "Answer to question %s from %s" % (
            self.application_question.question_number,
            self.application.startup.name)
<|endoftext|>


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

from openerp.osv import orm
from openerp.tools.translate import _

class AccountInvoice(orm.Model):
    _inherit = "account.invoice"

    def copy(self, cr, uid, ids, default=None, context=None):
        default = default or {}
        default.update({
            'supplier_invoice_number': '',
        })
        return super(AccountInvoice, self).copy(cr, uid, ids, default, context)

    def _check_unique_supplier_invoice_number_insensitive(self, cr, uid, ids,
                                                          context=None):
        # this function only works with one id
        if ids and len(ids) == 1:
            i_id = ids[0]
        else:
            raise orm.except_orm(
                _('Error'),
                'Cannot check unique supplier ref without id.')

        invoice = self.browse(cr, uid, i_id, context=context)
        invoice_partner = invoice.partner_id

        sr_ids = self.search(cr, uid,
                             [("partner_id", "=", invoice_partner.id)],
                             context=context)
        lst = [
            x.supplier_invoice_number.lower() for x in
            self.browse(cr, uid, sr_ids, context=context)
            if x.supplier_invoice_number and x.id not in ids
        ]
        if (invoice.supplier_invoice_number
                and invoice.supplier_invoice_number.lower() in lst):
            return False
        return True

    def _rec_message(self, cr, uid, ids, context=None):
        return _('The supplier invoice number must be unique \
                 for each supplier !')

    _constraints = [
        (_check_unique_supplier_invoice_number_insensitive,
         _rec_message,
         ['name'])
    ]
<commit_msg>account_invoice_supplier_ref_unique : fix wrong field name to apply the constraint against + make the error message to be translatable<commit_after># -*- encoding: utf-8 -*-
###############################################################################
#
#    OpenERP, Open Source Management Solution
#    This module copyright (C) 2010 - 2014 Savoir-faire Linux
#    (<http://www.savoirfairelinux.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

from openerp.osv import orm
from openerp.tools.translate import _

class AccountInvoice(orm.Model):
    _inherit = "account.invoice"

    def copy(self, cr, uid, ids, default=None, context=None):
        default = default or {}
        default.update({
            'supplier_invoice_number': '',
        })
        return super(AccountInvoice, self).copy(cr, uid, ids, default, context)

    def _check_unique_supplier_invoice_number_insensitive(self, cr, uid, ids,
                                                          context=None):
        # this function only works with one id
        if ids and len(ids) == 1:
            i_id = ids[0]
        else:
            raise orm.except_orm(
                _('Error'),
                _('Cannot check unique supplier ref without id.'))

        invoice = self.browse(cr, uid, i_id, context=context)
        invoice_partner = invoice.partner_id

        sr_ids = self.search(cr, uid,
                             [("partner_id", "=", invoice_partner.id)],
                             context=context)
        lst = [
            x.supplier_invoice_number.lower() for x in
            self.browse(cr, uid, sr_ids, context=context)
            if x.supplier_invoice_number and x.id not in ids
        ]
        if (invoice.supplier_invoice_number
                and invoice.supplier_invoice_number.lower() in lst):
            return False
        return True

    def _rec_message(self, cr, uid, ids, context=None):
        return _('The supplier invoice number must be unique \
                 for each supplier !')

    _constraints = [
        (_check_unique_supplier_invoice_number_insensitive,
         _rec_message,
         ['supplier_invoice_number'])
    ]
<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-
##############################################################################
#
#    Account Payment Blocking module for Odoo
#    Copyright (C) 2014-2015 ACSONE SA/NV (http://acsone.eu)
#    @author Stéphane Bidoul <stephane.bidoul@acsone.eu>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import api, models

class PaymentOrderCreate(models.TransientModel):
    _inherit = 'payment.order.create'

    @api.multi
    def extend_payment_order_domain(
            self, payment_order, domain):
        super(payment_order_create, self).extend_payment_order_domain(
            payment_order, domain)
        domain += [('blocked', '!=', True)]
        return True
<commit_msg>fix previous commit<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
#    Account Payment Blocking module for Odoo
#    Copyright (C) 2014-2015 ACSONE SA/NV (http://acsone.eu)
#    @author Stéphane Bidoul <stephane.bidoul@acsone.eu>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import api, models

class PaymentOrderCreate(models.TransientModel):
    _inherit = 'payment.order.create'

    @api.multi
    def extend_payment_order_domain(
            self, payment_order, domain):
        super(PaymentOrderCreate, self).extend_payment_order_domain(
            payment_order, domain)
        domain += [('blocked', '!=', True)]
        return True
<|endoftext|>


<commit_before>#!/net/kodiak/volumes/lake/shared/users/marghoob/my_env/bin/python
import sys
import os
import argparse
import multiprocessing
import logging
import collections
import itertools
import traceback
from functools import partial
import json
import base64
import vcf
import random
import math

import pysam

def annotate_vcfs(bam, chromosomes, workdir, num_threads, vcfs):
    func_logger = logging.getLogger("%s-%s" % (annotate_vcfs.__name__, multiprocessing.current_process()))
    random.seed(0)

    # Load indexed BAM file
    sam_file = pysam.Samfile(bam.name, "rb")

    if not chromosomes:
        func_logger.info("Chromosome list unspecified. Inferring from the BAMs")
        chromosomes += list(sam_file.references)
        chromosomes = sorted(list(set(chromosomes)))
        func_logger.info("Chromosome list inferred as %s" % (str(chromosomes)))

    if not chromosomes or len(chromosomes) == 0:
        func_logger.error("Chromosome list empty")
        return None

    # Read through samfile and get some statistics
    # hard code this for now
    read_limit = 1000

    # this is temporary, needs to read the reference to be sensible
    num_read = 0.0
    cover_sum = 0.0
    template_list = list()
    first_chr = sam_file.getrname(0)
    for i in xrange(0, read_limit):
        loc = random.randint(0, 30000000)
        alignments = sam_file.fetch(first_chr, loc, loc+1)

        curr_num = 0
        for aln in alignments:
            if aln.mapq < 18:
                continue
            curr_num += 1
            cover_sum += 1
            template_list.append(abs(aln.tlen))

        if curr_num > 0:
            num_read += 1

    template_list.sort()
    num_template = float(len(template_list))
    low_bound = int(math.floor(num_template*0.05))
    upp_bound = int(math.ceil(num_template*0.95))

    insert_count = 0
    insert_sum = 0.0
    insert_sq_sum = 0.0

    for i in xrange(low_bound, upp_bound):
        insert_count += 1
        insert_sum += template_list[i]
        insert_sq_sum += template_list[i] * template_list[i]

    mean_coverage = cover_sum/num_read
    mean_insert_size = insert_sum/insert_count
    sd_insert_size = math.sqrt((insert_sq_sum/insert_count) - (mean_insert_size * mean_insert_size))
    func_logger.info("Estimated coverage mean:      {0:.2f}".format(mean_coverage))
    func_logger.info("Estimated template size mean: {0:.2f}".format(mean_insert_size))
    func_logger.info("Estimated template size sd:   {0:.2f}".format(sd_insert_size))
    func_logger.info("Estimated template size Q5:   {0:.2f}".format(template_list[low_bound]))
    func_logger.info("Estimated template size Q95:  {0:.2f}".format(template_list[upp_bound - 1]))

    # Read though VCF one line at a time

    for inVCF in vcfs:
        vcf_reader = vcf.Reader(open(inVCF.name))
        vcf_template_reader = vcf.Reader(open(inVCF.name))
        vcf_writer = vcf.Writer(open("anno_"+inVCF.name, 'w'), vcf_template_reader)
        num_processed = 0
        for vcf_record in vcf_reader:
            if vcf_record.CHROM not in chromosomes:
                continue
            num_processed += 1
            if num_processed % 100 == 0:
                func_logger.info("{0} read from {1}".format(num_processed,inVCF.name))

            # get the interval that corresponds to the SV
            breakpoints = (vcf_record.start, vcf_record.INFO['END'])

            process_variant = True
            if breakpoints[1] - breakpoints[0] > 1000000:
                process_variant = False

            if process_variant:
                # get reads between breakpoints
                # sample with replacement 100 points
                unique_coverage = 0.0
                total_coverage = 0.0

                num_forward = 0.0

                num_repeat = 10
                for i in xrange(0, num_repeat):
                    loc = random.randint(breakpoints[0], breakpoints[1])
                    alignments = sam_file.fetch(vcf_record.CHROM, loc, loc + 1)
                    for rec in alignments:
                        if rec.mapq >= 18:
                            unique_coverage += 1
                            if not rec.is_reverse:
                                num_forward += 1
                        total_coverage += 1

                # get coverage between the breakpoints
                vcf_record.INFO["AA_UNIQ_COV"] = (unique_coverage/num_repeat)/mean_coverage
                vcf_record.INFO["AA_TOTAL_COV"] = (total_coverage/num_repeat)/mean_coverage

                if unique_coverage > 0:
                    vcf_record.INFO["AA_TOTAL_STRAND"] = (num_forward/unique_coverage - 0.5) ** 2

            # get strand bias

            # get mapping quality stats

            # get clipped reads stats

            # get discordant reads stats

            # get supplementary alignment stats

            vcf_writer.write_record(vcf_record)

        vcf_reader.close()
        vcf_template_reader.close()
        vcf_writer.close()

if __name__ == "__main__":
    FORMAT = '%(levelname)s %(asctime)-15s %(name)-20s %(message)s'
    logging.basicConfig(level=logging.INFO, format=FORMAT)
    logger = logging.getLogger(__name__)

    parser = argparse.ArgumentParser(
        description="Annotate VCF with additional useful features",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--bam", help="BAM file", required=True, type=file)
    parser.add_argument("--chromosomes", nargs="+", help="Chromosomes", default=[])
    parser.add_argument("--workdir", help="Working directory", default="work")
    parser.add_argument("--num_threads", help="Number of threads to use", default=1, type=int)
    parser.add_argument("--vcfs", nargs="+", help="Input VCF files", type=file)

    args = parser.parse_args()

    logger.info("Command-line: " + " ".join(sys.argv))

    annotate_vcfs(args.bam, args.chromosomes, args.workdir, args.num_threads, args.vcfs)
<commit_msg>add more features<commit_after>#!/net/kodiak/volumes/lake/shared/users/marghoob/my_env/bin/python
import sys
import os
import argparse
import multiprocessing
import logging
import collections
import itertools
import traceback
from functools import partial
import json
import base64
import vcf
import random
import math

import pysam

def annotate_vcfs(bam, chromosomes, workdir, num_threads, vcfs):
    func_logger = logging.getLogger("%s-%s" % (annotate_vcfs.__name__, multiprocessing.current_process()))
    random.seed(0)

    # Load indexed BAM file
    sam_file = pysam.Samfile(bam.name, "rb")

    if not chromosomes:
        func_logger.info("Chromosome list unspecified. Inferring from the BAMs")
        chromosomes += list(sam_file.references)
        chromosomes = sorted(list(set(chromosomes)))
        func_logger.info("Chromosome list inferred as %s" % (str(chromosomes)))

    if not chromosomes or len(chromosomes) == 0:
        func_logger.error("Chromosome list empty")
        return None

    # Read through samfile and get some statistics
    # hard code this for now
    read_limit = 1000

    # this is temporary, needs to read the reference to be sensible
    # TODODODODODO!!!
    num_read = 0.0
    cover_sum = 0.0
    template_list = list()
    first_chr = sam_file.getrname(0)
    for i in xrange(0, read_limit):
        loc = random.randint(0, 30000000)
        alignments = sam_file.fetch(first_chr, loc, loc+1)

        curr_num = 0
        for aln in alignments:
            if aln.mapq < 18:
                continue
            curr_num += 1
            cover_sum += 1
            template_list.append(abs(aln.tlen))

        if curr_num > 0:
            num_read += 1

    template_list.sort()
    num_template = float(len(template_list))
    low_bound = int(math.floor(num_template*0.05))
    upp_bound = int(math.ceil(num_template*0.95))

    insert_count = 0
    insert_sum = 0.0
    insert_sq_sum = 0.0

    for i in xrange(low_bound, upp_bound):
        insert_count += 1
        insert_sum += template_list[i]
        insert_sq_sum += template_list[i] * template_list[i]

    mean_coverage = cover_sum/num_read
    mean_insert_size = insert_sum/insert_count
    sd_insert_size = math.sqrt((insert_sq_sum/insert_count) - (mean_insert_size * mean_insert_size))
    func_logger.info("Estimated coverage mean:      {0:.2f}".format(mean_coverage))
    func_logger.info("Estimated template size mean: {0:.2f}".format(mean_insert_size))
    func_logger.info("Estimated template size sd:   {0:.2f}".format(sd_insert_size))
    func_logger.info("Estimated template size Q5:   {0:.2f}".format(template_list[low_bound]))
    func_logger.info("Estimated template size Q95:  {0:.2f}".format(template_list[upp_bound - 1]))

    template_upper_bound = mean_insert_size + (3 * sd_insert_size)
    template_lower_bound = mean_insert_size - (3 * sd_insert_size)

    # Read though VCF one line at a time

    for inVCF in vcfs:
        vcf_reader = vcf.Reader(open(inVCF.name))
        vcf_template_reader = vcf.Reader(open(inVCF.name))
        vcf_writer = vcf.Writer(open("anno_"+inVCF.name, 'w'), vcf_template_reader)
        num_processed = 0
        for vcf_record in vcf_reader:
            if vcf_record.CHROM not in chromosomes:
                continue
            num_processed += 1
            if num_processed % 100 == 0:
                func_logger.info("{0} read from {1}".format(num_processed,inVCF.name))

            # get the interval that corresponds to the SV
            breakpoints = (vcf_record.start, vcf_record.INFO['END'])

            process_variant = True
            if breakpoints[1] - breakpoints[0] > 1000000:
                process_variant = False

            if process_variant:
                # get reads between breakpoints
                # sample with replacement 100 points
                unique_coverage = 0.0
                total_coverage = 0.0

                num_forward = 0.0

                bases_aligned = 0.0
                total_bases = 0.0

                num_discordant_high = 0.0
                num_discordant_low = 0.0

                num_repeat = 10
                for i in xrange(0, num_repeat):
                    loc = random.randint(breakpoints[0], breakpoints[1])
                    alignments = sam_file.fetch(vcf_record.CHROM, loc, loc + 1)
                    for rec in alignments:
                        if rec.mapq >= 18:
                            unique_coverage += 1
                            if not rec.is_reverse:
                                num_forward += 1
                            total_bases += rec.rlen
                            bases_aligned += rec.qlen

                        total_coverage += 1

                # compute number of discordant
                for loc in [max(breakpoints[0] - sd_insert_size, 0), breakpoints[1] + sd_insert_size]:
                    alignments = sam_file.fetch(vcf_record.CHROM, loc, loc + 1)
                    for rec in alignments:
                        if rec.mapq >= 18 and abs(rec.tlen) > template_upper_bound:
                            num_discordant_high += 1
                        if rec.mapq >= 18 and abs(rec.tlen) < template_lower_bound:
                            num_discordant_low += 1

                # get coverage between the breakpoints
                vcf_record.INFO["AA_UNIQ_COV"] = (unique_coverage/num_repeat)/mean_coverage
                vcf_record.INFO["AA_TOTAL_COV"] = (total_coverage/num_repeat)/mean_coverage

                # get strand bias
                if unique_coverage > 0.0:
                    vcf_record.INFO["AA_TOTAL_STRAND"] = (num_forward/unique_coverage - 0.5) ** 2

                # get mapping quality stats
                if total_coverage > 0.0:
                    vcf_record.INFO["AA_PROP_REPEAT"] = unique_coverage/total_coverage

                # get clipped reads stats
                if total_bases > 0.0:
                    vcf_record.INFO["AA_PROP_ALIGNED"] = bases_aligned/total_bases

                # get discordant reads stats
                vcf_record.INFO["AA_DISCORDANT_HIGH"] = num_discordant_high
                vcf_record.INFO["AA_DISCORDANT_LOW"] = num_discordant_low

                # get supplementary alignment stats
                # Skip this for now

            vcf_writer.write_record(vcf_record)

        vcf_reader.close()
        vcf_template_reader.close()
        vcf_writer.close()

if __name__ == "__main__":
    FORMAT = '%(levelname)s %(asctime)-15s %(name)-20s %(message)s'
    logging.basicConfig(level=logging.INFO, format=FORMAT)
    logger = logging.getLogger(__name__)

    parser = argparse.ArgumentParser(
        description="Annotate VCF with additional useful features",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--bam", help="BAM file", required=True, type=file)
    parser.add_argument("--chromosomes", nargs="+", help="Chromosomes", default=[])
    parser.add_argument("--workdir", help="Working directory", default="work")
    parser.add_argument("--num_threads", help="Number of threads to use", default=1, type=int)
    parser.add_argument("--vcfs", nargs="+", help="Input VCF files", type=file)

    args = parser.parse_args()

    logger.info("Command-line: " + " ".join(sys.argv))

    annotate_vcfs(args.bam, args.chromosomes, args.workdir, args.num_threads, args.vcfs)
<|endoftext|>


<commit_before>"""
Node/Link layout.

"""
from operator import attrgetter, add

import numpy

import sip

from PyQt4.QtGui import QGraphicsObject
from PyQt4.QtCore import QRectF, QLineF, QTimer

from .items import NodeItem, LinkItem, SourceAnchorItem, SinkAnchorItem
from .items.utils import typed_signal_mapper, invert_permutation_indices, \
                         linspace
from functools import reduce

LinkItemSignalMapper = typed_signal_mapper(LinkItem)

def composition(f, g):
    """Return a composition of two functions
    """
    def fg(arg):
        return g(f(arg))
    return fg

class AnchorLayout(QGraphicsObject):
    def __init__(self, parent=None, **kwargs):
        QGraphicsObject.__init__(self, parent, **kwargs)
        self.setFlag(QGraphicsObject.ItemHasNoContents)

        self.__layoutPending = False
        self.__isActive = False
        self.__invalidatedAnchors = []
        self.__enabled = True

    def boundingRect(self):
        return QRectF()

    def activate(self):
        if self.isEnabled() and not self.__isActive:
            self.__isActive = True
            try:
                self._doLayout()
            finally:
                self.__isActive = False
                self.__layoutPending = False

    def isActivated(self):
        return self.__isActive

    def _doLayout(self):
        if not self.isEnabled():
            return

        scene = self.scene()
        items = scene.items()
        links = [item for item in items if isinstance(item, LinkItem)]
        point_pairs = [(link.sourceAnchor, link.sinkAnchor) for link in links]
        point_pairs.extend(map(reversed, point_pairs))
        to_other = dict(point_pairs)

        anchors = set(self.__invalidatedAnchors)

        for anchor_item in anchors:
            if sip.isdeleted(anchor_item):
                continue

            points = anchor_item.anchorPoints()
            anchor_pos = anchor_item.mapToScene(anchor_item.pos())
            others = [to_other[point] for point in points]

            if isinstance(anchor_item, SourceAnchorItem):
                others_angle = [-angle(anchor_pos, other.anchorScenePos())
                                for other in others]
            else:
                others_angle = [angle(other.anchorScenePos(), anchor_pos)
                                for other in others]

            indices = list(numpy.argsort(others_angle))
            # Invert the indices.
            indices = invert_permutation_indices(indices)

            positions = numpy.array(linspace(len(points)))
            positions = list(positions[indices])

            anchor_item.setAnchorPositions(positions)

        self.__invalidatedAnchors = []

    def invalidate(self):
        items = self.scene().items()
        nodes = [item for item in items is isinstance(item, NodeItem)]
        anchors = reduce(add,
                         [[node.outputAnchorItem, node.inputAnchorItem]
                          for node in nodes],
                         [])
        self.__invalidatedAnchors.extend(anchors)
        self.scheduleDelayedActivate()

    def invalidateLink(self, link):
        self.invalidateAnchorItem(link.sourceItem.outputAnchorItem)
        self.invalidateAnchorItem(link.sinkItem.inputAnchorItem)

        self.scheduleDelayedActivate()

    def invalidateNode(self, node):
        self.invalidateAnchorItem(node.inputAnchorItem)
        self.invalidateAnchorItem(node.outputAnchorItem)

        self.scheduleDelayedActivate()

    def invalidateAnchorItem(self, anchor):
        self.__invalidatedAnchors.append(anchor)

        scene = self.scene()
        if isinstance(anchor, SourceAnchorItem):
            links = scene.node_output_links(anchor.parentNodeItem())
            getter = composition(attrgetter("sinkItem"),
                                 attrgetter("inputAnchorItem"))
        elif isinstance(anchor, SinkAnchorItem):
            links = scene.node_input_links(anchor.parentNodeItem())
            getter = composition(attrgetter("sourceItem"),
                                 attrgetter("outputAnchorItem"))
        else:
            raise TypeError(type(anchor))

        self.__invalidatedAnchors.extend(map(getter, links))

        self.scheduleDelayedActivate()

    def scheduleDelayedActivate(self):
        if self.isEnabled() and not self.__layoutPending:
            self.__layoutPending = True
            QTimer.singleShot(0, self.__delayedActivate)

    def __delayedActivate(self):
        if self.__layoutPending:
            self.activate()

def angle(point1, point2):
    """Return the angle between the two points in range from -180 to 180.
    """
    angle = QLineF(point1, point2).angle()
    if angle > 180:
        return angle - 360
    else:
        return angle
<commit_msg>Fixed a use of 'reversed' in Orange.canvas.canvas<commit_after>"""
Node/Link layout.

"""
from operator import attrgetter, add

import numpy

import sip

from PyQt4.QtGui import QGraphicsObject
from PyQt4.QtCore import QRectF, QLineF, QTimer

from .items import NodeItem, LinkItem, SourceAnchorItem, SinkAnchorItem
from .items.utils import typed_signal_mapper, invert_permutation_indices, \
                         linspace
from functools import reduce

LinkItemSignalMapper = typed_signal_mapper(LinkItem)

def composition(f, g):
    """Return a composition of two functions
    """
    def fg(arg):
        return g(f(arg))
    return fg

class AnchorLayout(QGraphicsObject):
    def __init__(self, parent=None, **kwargs):
        QGraphicsObject.__init__(self, parent, **kwargs)
        self.setFlag(QGraphicsObject.ItemHasNoContents)

        self.__layoutPending = False
        self.__isActive = False
        self.__invalidatedAnchors = []
        self.__enabled = True

    def boundingRect(self):
        return QRectF()

    def activate(self):
        if self.isEnabled() and not self.__isActive:
            self.__isActive = True
            try:
                self._doLayout()
            finally:
                self.__isActive = False
                self.__layoutPending = False

    def isActivated(self):
        return self.__isActive

    def _doLayout(self):
        if not self.isEnabled():
            return

        scene = self.scene()
        items = scene.items()
        links = [item for item in items if isinstance(item, LinkItem)]
        point_pairs = [(link.sourceAnchor, link.sinkAnchor) for link in links]
        point_pairs += [(a, b) for b, a in point_pairs]
        to_other = dict(point_pairs)

        anchors = set(self.__invalidatedAnchors)

        for anchor_item in anchors:
            if sip.isdeleted(anchor_item):
                continue

            points = anchor_item.anchorPoints()
            anchor_pos = anchor_item.mapToScene(anchor_item.pos())
            others = [to_other[point] for point in points]

            if isinstance(anchor_item, SourceAnchorItem):
                others_angle = [-angle(anchor_pos, other.anchorScenePos())
                                for other in others]
            else:
                others_angle = [angle(other.anchorScenePos(), anchor_pos)
                                for other in others]

            indices = list(numpy.argsort(others_angle))
            # Invert the indices.
            indices = invert_permutation_indices(indices)

            positions = numpy.array(linspace(len(points)))
            positions = list(positions[indices])

            anchor_item.setAnchorPositions(positions)

        self.__invalidatedAnchors = []

    def invalidate(self):
        items = self.scene().items()
        nodes = [item for item in items is isinstance(item, NodeItem)]
        anchors = reduce(add,
                         [[node.outputAnchorItem, node.inputAnchorItem]
                          for node in nodes],
                         [])
        self.__invalidatedAnchors.extend(anchors)
        self.scheduleDelayedActivate()

    def invalidateLink(self, link):
        self.invalidateAnchorItem(link.sourceItem.outputAnchorItem)
        self.invalidateAnchorItem(link.sinkItem.inputAnchorItem)

        self.scheduleDelayedActivate()

    def invalidateNode(self, node):
        self.invalidateAnchorItem(node.inputAnchorItem)
        self.invalidateAnchorItem(node.outputAnchorItem)

        self.scheduleDelayedActivate()

    def invalidateAnchorItem(self, anchor):
        self.__invalidatedAnchors.append(anchor)

        scene = self.scene()
        if isinstance(anchor, SourceAnchorItem):
            links = scene.node_output_links(anchor.parentNodeItem())
            getter = composition(attrgetter("sinkItem"),
                                 attrgetter("inputAnchorItem"))
        elif isinstance(anchor, SinkAnchorItem):
            links = scene.node_input_links(anchor.parentNodeItem())
            getter = composition(attrgetter("sourceItem"),
                                 attrgetter("outputAnchorItem"))
        else:
            raise TypeError(type(anchor))

        self.__invalidatedAnchors.extend(map(getter, links))

        self.scheduleDelayedActivate()

    def scheduleDelayedActivate(self):
        if self.isEnabled() and not self.__layoutPending:
            self.__layoutPending = True
            QTimer.singleShot(0, self.__delayedActivate)

    def __delayedActivate(self):
        if self.__layoutPending:
            self.activate()

def angle(point1, point2):
    """Return the angle between the two points in range from -180 to 180.
    """
    angle = QLineF(point1, point2).angle()
    if angle > 180:
        return angle - 360
    else:
        return angle
<|endoftext|>


<commit_before>from unittest import TestCase

from whylog.teacher.rule_validation_problems import (
    NotSetLogTypeProblem, NotUniqueParserNameProblem, ValidationResult
)

class TestBasic(TestCase):
    def setUp(self):
        self.line1_id = 1
        line2_id = 2
        self.e1 = NotUniqueParserNameProblem(self.line1_id)
        e2 = NotSetLogTypeProblem(line2_id)
        w2 = NotUniqueParserNameProblem(line2_id)
        errors = [e2, self.e1]
        warnings = [w2]
        self.constraint_id = 3
        # TODO: create constraint problems with self.constraint_id
        self.validation_result = ValidationResult(errors, warnings)

    def test_select_parser_problems(self):
        self.validation_result.select_parser_problems(self.line1_id)
        filtered_problems = self.validation_result.select_parser_problems(self.line1_id)
        assert self.e1 in filtered_problems

    def test_select_constraint_problems(self):
        self.validation_result.select_parser_problems(self.constraint_id)
        filtered_problems = self.validation_result.select_constraint_problems(self.constraint_id)
        assert not filtered_problems
<commit_msg>ValidationResult tests removed<commit_after><|endoftext|>


<commit_before>"""Analysis functions (mostly for psychophysics data).
"""

import scipy.stats as ss
import warnings
import numbers

def dprime(h, m, fa, cr, zero_correction=True):
    """Estimates d-prime, with optional correction factor to avoid infinites.

    Parameters
    ----------
    h : int
        Number of detected targets (hits).
    m : int
        Number of undetected targets (misses).
    fa : int
        Number of incorrect non-target detections (false alarms).
    cr : int
        Number of correct non-target detections (correct rejections).
    zero_correction : bool
        Whether to add a correction factor of 0.5 to each category to prevent
        division-by-zero leading to infinite d-prime values.

    Notes
    -----
    For two-alternative forced-choice tasks, it is recommended to enter correct
    trials as hits and incorrect trials as false alarms, and enter misses and
    correct rejections as 0. An alternative is to use ``dprime_2afc()``, which
    wraps to ``dprime()`` and does this assignment for you.
    """
    for x in [h, m, fa, cr]:
        if not isinstance(x, numbers.Number):
            raise TypeError('Arguments to d-prime must be numeric.')
        elif not isinstance(x, int):
            warnings.warn('Non-integer arguments to d-prime were truncated.')

    if zero_correction:
        a = 0.5
    else:
        a = 0.0
    return ss.norm.ppf((int(h) + a) / (int(h) + int(m) + 2 * a)) - \
        ss.norm.ppf((int(fa) + a) / (int(fa) + int(cr) + 2 * a))

def dprime_2afc(h, m, zero_correction=True):
    """Estimates d-prime for two-alternative forced-choice paradigms.

    Parameters
    ----------
    h : int
        Number of correct trials (hits).
    m : int
        Number of incorrect trials (misses).
    zero_correction : bool
        Whether to add a correction factor of 0.5 to each category to prevent
        division-by-zero leading to infinite d-prime values.
    """
    return dprime(h, 0, m, 0, zero_correction)
<commit_msg>ENH: vectorized dprime() and dprime_2afc()<commit_after>"""Analysis functions (mostly for psychophysics data).
"""

import numpy as np
import scipy.stats as ss
import warnings

def dprime(hmfc, zero_correction=True):
    """Estimates d-prime, with optional correction factor to avoid infinites.

    Parameters
    ----------
    hmfc : array-like
        Hits, misses, false-alarms, and correct-rejections, in that order, as a
        four-element list, tuple, or numpy array.  If an Nx4 array is provided,
        it will return an array of dimension (N,).
    zero_correction : bool
        Whether to add a correction factor of 0.5 to each category to prevent
        division-by-zero leading to infinite d-prime values.

    Notes
    -----
    For two-alternative forced-choice tasks, it is recommended to enter correct
    trials as hits and incorrect trials as false alarms, and enter misses and
    correct rejections as 0. An alternative is to use ``dprime_2afc()``, which
    wraps to ``dprime()`` and does this assignment for you.
    """
    vector = False
    hmfc = _check_dprime_inputs(hmfc)
    if len(hmfc.shape) == 1:
        vector = True
        hmfc = np.atleast_2d(hmfc)
    if zero_correction:
        a = 0.5
    else:
        a = 0.0
    dp = ss.norm.ppf((hmfc[:, 0] + a) / (hmfc[:, 0] + hmfc[:, 1] + 2 * a)) \
        - ss.norm.ppf((hmfc[:, 2] + a) / (hmfc[:, 2] + hmfc[:, 3] + 2 * a))
    if vector:
        return dp[0]
    else:
        return dp

def dprime_2afc(hm, zero_correction=True):
    """Estimates d-prime for two-alternative forced-choice paradigms.

    Parameters
    ----------
    hm : array-like
        Correct trials (hits) and incorrect trials (misses), in that order, as
        a two-element list, tuple, or numpy array. If an Nx2 array is provided,
        it will return an array of dimension (N,).
    zero_correction : bool
        Whether to add a correction factor of 0.5 to each category to prevent
        division-by-zero leading to infinite d-prime values.
    """
    hmfc = _check_dprime_inputs(hm, True)
    return dprime(hmfc, zero_correction)

def _check_dprime_inputs(hmfc, tafc=False):
    """Formats input to dprime() and dprime_2afc().

    Parameters
    ----------
    hmfc : array-like
        Hit, miss, false-alarm, correct-rejection; or hit, miss for 2AFC.
    tafc : bool
        Is this a 2AFC design?
    """
    hmfc = np.array(hmfc)
    if len(hmfc.shape) > 2:
        raise ValueError('Argument to dprime() cannot have more than two '
                         'dimensions.')
    elif hmfc.shape[-1] != 2 and tafc:
        raise ValueError('Array dimensions of argument to dprime_2afc() must '
                         'be (2,) or (N, 2).')
    elif hmfc.shape[-1] != 4 and not tafc:
        raise ValueError('Array dimensions of argument to dprime() must be '
                         '(4,) or (N, 4).')

    if len(hmfc.shape) == 1 and tafc:
        hmfc = np.c_[hmfc[0], 0, hmfc[1], 0]
    elif tafc:
        z = np.zeros_like(hmfc[:, 0])
        hmfc = np.c_[hmfc[:, 0], z, hmfc[:, 1], z]
    if hmfc.dtype != np.int64:
        warnings.warn('Argument to dprime() cast to np.int64; floating point '
                      'values will have been truncated.')
        hmfc = hmfc.astype(np.int64)
    return hmfc
<|endoftext|>


<commit_before>import io
import fire
import numpy as np

import logging
logging.basicConfig(
    format='%(asctime)s [%(process)d] [%(levelname)s] %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    level=logging.INFO)

def main(source_file, target_file, s2t_file, t2s_file, output_file, min_score=0.5):
    s2t = np.loadtxt(s2t_file)
    t2s = np.loadtxt(t2s_file)
    source = io.open(source_file, 'r', encoding='utf-8').read().strip().split('\n')
    target = io.open(target_file, 'r', encoding='utf-8').read().strip().split('\n')

    assert len(source) == len(target) == s2t.shape[0] == t2s.shape[0], 'length mismatch'

    with io.open(output_file, 'w', encoding='utf-8') as out:
        for i, (src, tgt) in enumerate(zip(source, target)):
            s2t_score, t2s_score = s2t[i], t2s[i]
            if min([s2t_score, t2s_score]) > min_score:
                out.write('{src} ||| {tgt}\n'.format(src=src, tgt=tgt))
            if i % 1000 == 0:
                logging.info('Processed {0} sentences'.format(i))
        logging.info('Processed {0} sentences'.format(i))

if __name__ == '__main__':
    fire.Fire(main)

<commit_msg>Update filter-sym-align.py<commit_after>import io
import fire

import logging
logging.basicConfig(
    format='%(asctime)s [%(process)d] [%(levelname)s] %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    level=logging.INFO)

def main(source_file, target_file, s2t_file, t2s_file, out_file, min_score=0.5):
    source = io.open(source_file, 'r', encoding='utf-8')
    target = io.open(target_file, 'r', encoding='utf-8')
    s2t = io.open(s2t_file, 'r', encoding='utf-8')
    t2s = io.open(t2s_file, 'r', encoding='utf-8')

    with io.open(out_file, 'w', encoding='utf-8') as out:
        for i, pair in enumerate(zip(source, target, s2t, t2s)):
            (src, tgt, s2t_score, t2s_score) = pair

            if min([float(s2t_score), float(t2s_score)]) > min_score:
                out.write('{src} ||| {tgt}\n'.format(src=src, tgt=tgt))

            if i % 1000 == 0:
                logging.info('Processed {0} sentences'.format(i))
        logging.info('Processed {0} sentences'.format(i))

if __name__ == '__main__':
    fire.Fire(main)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    flask-07-upgrade
    ~~~~~~~~~~~~~~~~

    This command line script scans a whole application tree and attempts to
    output an unified diff with all the changes that are necessary to easily
    upgrade the application to 0.7 and to not yield deprecation warnings.

    This will also attempt to find `after_request` functions that don't modify
    the response and appear to be better suited for `teardown_request`.

    :copyright: (c) Copyright 2011 by Armin Ronacher.
    :license: see LICENSE for more details.
"""
import re
import os
import sys
import inspect
import difflib
import posixpath
from optparse import OptionParser

try:
    import ast
except ImportError:
    ast = None

_from_import_re = re.compile(r'^\s*from flask import\s+')
_string_re_part = r"('([^'\\]*(?:\\.[^'\\]*)*)'" \
                  r'|"([^"\\]*(?:\\.[^"\\]*)*)")'
_url_for_re = re.compile(r'\b(url_for\()(%s)' % _string_re_part)
_render_template_re = re.compile(r'\b(render_template\()(%s)' % _string_re_part)
_after_request_re = re.compile(r'((?:@\S+\.(?:app_)?))(after_request)(\b\s*$)(?m)')
_module_constructor_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\s*=\s*Module'
                                    r'\(__name__\s*(?:,\s*(%s))?' %
                                    _string_re_part)
_mod_route_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\.route')
_blueprint_related = [
    (re.compile(r'request\.module'), 'request.blueprint'),
    (re.compile(r'register_module'), 'register_blueprint')
]

def error(message):
    print >> sys.stderr, 'error:', message
    sys.exit(1)

def make_diff(filename, old, new):
    for line in difflib.unified_diff(old.splitlines(), new.splitlines(),
                     posixpath.normpath(posixpath.join('a', filename)),
                     posixpath.normpath(posixpath.join('b', filename)),
                     lineterm=''):
        print line

def fix_url_for(contents):
    def handle_match(match):
        prefix = match.group(1)
        endpoint = ast.literal_eval(match.group(2))
        if endpoint.startswith('.'):
            endpoint = endpoint[1:]
        else:
            endpoint = '.' + endpoint
        return prefix + repr(endpoint)
    return _url_for_re.sub(handle_match, contents)

def looks_like_teardown_function(node):
    returns = [x for x in ast.walk(node) if isinstance(x, ast.Return)]
    if len(returns) != 1:
        return
    return_def = returns[0]
    resp_name = node.args.args[0]
    if not isinstance(return_def.value, ast.Name) or \
       return_def.value.id != resp_name.id:
        return

    for body_node in node.body:
        for child in ast.walk(body_node):
            if isinstance(child, ast.Name) and \
               child.id == resp_name.id:
                if child is not return_def.value:
                    return

    return resp_name.id

def fix_teardown_funcs(contents):

    def is_return_line(line):
        args = line.strip().split()
        return args and args[0] == 'return'

    def fix_single(match, lines, lineno):
        if not lines[lineno + 1].startswith('def'):
            return
        block_lines = inspect.getblock(lines[lineno + 1:])
        func_code = ''.join(block_lines)
        if func_code[0].isspace():
            node = ast.parse('if 1:\n' + func_code).body[0].body
        else:
            node = ast.parse(func_code).body[0]
        response_param_name = looks_like_teardown_function(node)
        if response_param_name is None:
            return
        before = lines[:lineno]
        decorator = [match.group(1) +
                     match.group(2).replace('after_', 'teardown_') +
                     match.group(3)]
        body = [line.replace(response_param_name, 'exception')
                for line in block_lines if
                not is_return_line(line)]
        after = lines[lineno + len(block_lines) + 1:]
        return before + decorator + body + after

    content_lines = contents.splitlines(True)
    while 1:
        found_one = False
        for idx, line in enumerate(content_lines):
            match = _after_request_re.match(line)
            if match is None:
                continue
            new_content_lines = fix_single(match, content_lines, idx)
            if new_content_lines is not None:
                content_lines = new_content_lines
                break
        else:
            break

    return ''.join(content_lines)

def get_module_autoname(filename):
    directory, filename = os.path.split(filename)
    if filename != '__init__.py':
        return os.path.splitext(filename)[0]
    return os.path.basename(directory)

def rewrite_from_imports(prefix, fromlist, lineiter):
    import_block = [prefix, fromlist]
    if fromlist[0] == '(' and fromlist[-1] != ')':
        for line in lineiter:
            import_block.append(line)
            if line.rstrip().endswith(')'):
                break
    elif fromlist[-1] == '\\':
        for line in lineiter:
            import_block.append(line)
            if line.rstrip().endswith('\\'):
                break

    return ''.join(import_block).replace('Module', 'Blueprint')

def rewrite_blueprint_imports(contents):
    new_file = []
    lineiter = iter(contents.splitlines(True))
    for line in lineiter:
        match = _from_import_re.search(line)
        if match is not None:
            new_file.extend(rewrite_from_imports(match.group(),
                                                 line[match.end():],
                                                 lineiter))
        else:
            new_file.append(line)
    return ''.join(new_file)

def rewrite_render_template_calls(contents, module_declarations):
    mapping = dict(module_declarations)
    annotated_lines = []

    def make_line_annotations():
        if not annotated_lines:
            last_index = 0
            for line in contents.splitlines(True):
                last_index += len(line)
                annotated_lines.append((last_index, line))

    def backtrack_module_name(call_start):
        make_line_annotations()
        for idx, (line_end, line) in enumerate(annotated_lines):
            if line_end > call_start:
                for _, line in reversed(annotated_lines[:idx]):
                    match = _mod_route_re.search(line)
                    if match is not None:
                        shortname = match.group(1)
                        return mapping.get(shortname)

    def handle_match(match):
        template_name = ast.literal_eval(match.group(2))
        modname = backtrack_module_name(match.start())
        if modname is not None and template_name.startswith(modname + '/'):
            template_name = modname + ':' + template_name[len(modname) + 1:]
        return match.group(1) + repr(template_name)
    return _render_template_re.sub(handle_match, contents)

def rewrite_for_blueprints(contents, filename, template_bundles=False):
    modules_declared = []
    def handle_match(match):
        target = match.group(1)
        name_param = match.group(2)
        if name_param is None:
            modname = get_module_autoname(filename)
        else:
            modname = ast.literal_eval(name_param)
        modules_declared.append((target, modname))
        return '%s = %s' % (target, 'Blueprint(%r, __name__' % modname)
    new_contents = _module_constructor_re.sub(handle_match, contents)

    if modules_declared:
        new_contents = rewrite_blueprint_imports(new_contents)
        if template_bundles:
            new_contents = rewrite_render_template_calls(new_contents,
                                                         modules_declared)

    for pattern, replacement in _blueprint_related:
        new_contents = pattern.sub(replacement, new_contents)
    return new_contents

def upgrade_python_file(filename, contents, teardown, template_bundles):
    new_contents = fix_url_for(contents)
    if teardown:
        new_contents = fix_teardown_funcs(new_contents)
    new_contents = rewrite_for_blueprints(new_contents, filename,
                                          template_bundles)
    make_diff(filename, contents, new_contents)

def upgrade_template_file(filename, contents):
    new_contents = fix_url_for(contents)
    make_diff(filename, contents, new_contents)

def walk_path(path):
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            filename = os.path.join(dirpath, filename)
            if filename.endswith('.py'):
                yield filename, 'python'
            else:
                with open(filename) as f:
                    contents = f.read()
                if '{% for' or '{% if' or '{{ url_for' in contents:
                    yield filename, 'template'

def scan_path(path=None, teardown=True, template_bundles=True):
    for filename, type in walk_path(path):
        with open(filename) as f:
            contents = f.read()
        if type == 'python':
            upgrade_python_file(filename, contents, teardown,
                                template_bundles)
        elif type == 'template':
            upgrade_template_file(filename, contents)

def autodetect_template_bundles(paths):
    folders_with_templates = set()
    for path in paths:
        for filename, type in walk_path(path):
            if type == 'template':
                fullpath = filename.replace(os.path.sep, '/')
                index = fullpath.find('/templates/')
                if index >= 0:
                    folder = fullpath[:index]
                else:
                    folder = posixpath.dirname(fullpath)
                folders_with_templates.add(folder)
    return len(folders_with_templates) > 1

def main():
    """Entrypoint"""
    if ast is None:
        error('Python 2.6 or later is required to run the upgrade script.\n'
              'The runtime requirements for Flask 0.7 however are still '
              'Python 2.5.')

    parser = OptionParser()
    parser.add_option('-T', '--no-teardown-detection', dest='no_teardown',
                      action='store_true', help='Do not attempt to '
                      'detect teardown function rewrites.')
    parser.add_option('-b', '--bundled-templates', dest='bundled_tmpl',
                      action='store_true', help='Indicate to the system '
                      'that templates are bundled with modules.  Default '
                      'is auto detect.')
    parser.add_option('-B', '--no-bundled-templates', dest='no_bundled_tmpl',
                      action='store_true', help='Indicate to the system '
                      'that templates are not bundled with modules.  '
                      'Default is auto detect')
    options, args = parser.parse_args()
    if not args:
        args = ['.']

    if options.no_bundled_tmpl:
        template_bundles = False
    elif options.bundled_tmpl:
        template_bundles = True
    else:
        template_bundles = autodetect_template_bundles(args)

    for path in args:
        scan_path(path, teardown=not options.no_teardown,
                  template_bundles=template_bundles)

if __name__ == '__main__':
    main()
<commit_msg>Added disclaimer<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    flask-07-upgrade
    ~~~~~~~~~~~~~~~~

    This command line script scans a whole application tree and attempts to
    output an unified diff with all the changes that are necessary to easily
    upgrade the application to 0.7 and to not yield deprecation warnings.

    This will also attempt to find `after_request` functions that don't modify
    the response and appear to be better suited for `teardown_request`.

    This application is indeed an incredible hack, but because what it
    attempts to accomplish is impossible to do statically it tries to support
    the most common patterns at least.  The diff it generates should be
    hand reviewed and not applied blindly without making backups.

    :copyright: (c) Copyright 2011 by Armin Ronacher.
    :license: see LICENSE for more details.
"""
import re
import os
import sys
import inspect
import difflib
import posixpath
from optparse import OptionParser

try:
    import ast
except ImportError:
    ast = None

_from_import_re = re.compile(r'^\s*from flask import\s+')
_string_re_part = r"('([^'\\]*(?:\\.[^'\\]*)*)'" \
                  r'|"([^"\\]*(?:\\.[^"\\]*)*)")'
_url_for_re = re.compile(r'\b(url_for\()(%s)' % _string_re_part)
_render_template_re = re.compile(r'\b(render_template\()(%s)' % _string_re_part)
_after_request_re = re.compile(r'((?:@\S+\.(?:app_)?))(after_request)(\b\s*$)(?m)')
_module_constructor_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\s*=\s*Module'
                                    r'\(__name__\s*(?:,\s*(%s))?' %
                                    _string_re_part)
_mod_route_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\.route')
_blueprint_related = [
    (re.compile(r'request\.module'), 'request.blueprint'),
    (re.compile(r'register_module'), 'register_blueprint')
]

def error(message):
    print >> sys.stderr, 'error:', message
    sys.exit(1)

def make_diff(filename, old, new):
    for line in difflib.unified_diff(old.splitlines(), new.splitlines(),
                     posixpath.normpath(posixpath.join('a', filename)),
                     posixpath.normpath(posixpath.join('b', filename)),
                     lineterm=''):
        print line

def fix_url_for(contents):
    def handle_match(match):
        prefix = match.group(1)
        endpoint = ast.literal_eval(match.group(2))
        if endpoint.startswith('.'):
            endpoint = endpoint[1:]
        else:
            endpoint = '.' + endpoint
        return prefix + repr(endpoint)
    return _url_for_re.sub(handle_match, contents)

def looks_like_teardown_function(node):
    returns = [x for x in ast.walk(node) if isinstance(x, ast.Return)]
    if len(returns) != 1:
        return
    return_def = returns[0]
    resp_name = node.args.args[0]
    if not isinstance(return_def.value, ast.Name) or \
       return_def.value.id != resp_name.id:
        return

    for body_node in node.body:
        for child in ast.walk(body_node):
            if isinstance(child, ast.Name) and \
               child.id == resp_name.id:
                if child is not return_def.value:
                    return

    return resp_name.id

def fix_teardown_funcs(contents):

    def is_return_line(line):
        args = line.strip().split()
        return args and args[0] == 'return'

    def fix_single(match, lines, lineno):
        if not lines[lineno + 1].startswith('def'):
            return
        block_lines = inspect.getblock(lines[lineno + 1:])
        func_code = ''.join(block_lines)
        if func_code[0].isspace():
            node = ast.parse('if 1:\n' + func_code).body[0].body
        else:
            node = ast.parse(func_code).body[0]
        response_param_name = looks_like_teardown_function(node)
        if response_param_name is None:
            return
        before = lines[:lineno]
        decorator = [match.group(1) +
                     match.group(2).replace('after_', 'teardown_') +
                     match.group(3)]
        body = [line.replace(response_param_name, 'exception')
                for line in block_lines if
                not is_return_line(line)]
        after = lines[lineno + len(block_lines) + 1:]
        return before + decorator + body + after

    content_lines = contents.splitlines(True)
    while 1:
        found_one = False
        for idx, line in enumerate(content_lines):
            match = _after_request_re.match(line)
            if match is None:
                continue
            new_content_lines = fix_single(match, content_lines, idx)
            if new_content_lines is not None:
                content_lines = new_content_lines
                break
        else:
            break

    return ''.join(content_lines)

def get_module_autoname(filename):
    directory, filename = os.path.split(filename)
    if filename != '__init__.py':
        return os.path.splitext(filename)[0]
    return os.path.basename(directory)

def rewrite_from_imports(prefix, fromlist, lineiter):
    import_block = [prefix, fromlist]
    if fromlist[0] == '(' and fromlist[-1] != ')':
        for line in lineiter:
            import_block.append(line)
            if line.rstrip().endswith(')'):
                break
    elif fromlist[-1] == '\\':
        for line in lineiter:
            import_block.append(line)
            if line.rstrip().endswith('\\'):
                break

    return ''.join(import_block).replace('Module', 'Blueprint')

def rewrite_blueprint_imports(contents):
    new_file = []
    lineiter = iter(contents.splitlines(True))
    for line in lineiter:
        match = _from_import_re.search(line)
        if match is not None:
            new_file.extend(rewrite_from_imports(match.group(),
                                                 line[match.end():],
                                                 lineiter))
        else:
            new_file.append(line)
    return ''.join(new_file)

def rewrite_render_template_calls(contents, module_declarations):
    mapping = dict(module_declarations)
    annotated_lines = []

    def make_line_annotations():
        if not annotated_lines:
            last_index = 0
            for line in contents.splitlines(True):
                last_index += len(line)
                annotated_lines.append((last_index, line))

    def backtrack_module_name(call_start):
        make_line_annotations()
        for idx, (line_end, line) in enumerate(annotated_lines):
            if line_end > call_start:
                for _, line in reversed(annotated_lines[:idx]):
                    match = _mod_route_re.search(line)
                    if match is not None:
                        shortname = match.group(1)
                        return mapping.get(shortname)

    def handle_match(match):
        template_name = ast.literal_eval(match.group(2))
        modname = backtrack_module_name(match.start())
        if modname is not None and template_name.startswith(modname + '/'):
            template_name = modname + ':' + template_name[len(modname) + 1:]
        return match.group(1) + repr(template_name)
    return _render_template_re.sub(handle_match, contents)

def rewrite_for_blueprints(contents, filename, template_bundles=False):
    modules_declared = []
    def handle_match(match):
        target = match.group(1)
        name_param = match.group(2)
        if name_param is None:
            modname = get_module_autoname(filename)
        else:
            modname = ast.literal_eval(name_param)
        modules_declared.append((target, modname))
        return '%s = %s' % (target, 'Blueprint(%r, __name__' % modname)
    new_contents = _module_constructor_re.sub(handle_match, contents)

    if modules_declared:
        new_contents = rewrite_blueprint_imports(new_contents)
        if template_bundles:
            new_contents = rewrite_render_template_calls(new_contents,
                                                         modules_declared)

    for pattern, replacement in _blueprint_related:
        new_contents = pattern.sub(replacement, new_contents)
    return new_contents

def upgrade_python_file(filename, contents, teardown, template_bundles):
    new_contents = fix_url_for(contents)
    if teardown:
        new_contents = fix_teardown_funcs(new_contents)
    new_contents = rewrite_for_blueprints(new_contents, filename,
                                          template_bundles)
    make_diff(filename, contents, new_contents)

def upgrade_template_file(filename, contents):
    new_contents = fix_url_for(contents)
    make_diff(filename, contents, new_contents)

def walk_path(path):
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            filename = os.path.join(dirpath, filename)
            if filename.endswith('.py'):
                yield filename, 'python'
            else:
                with open(filename) as f:
                    contents = f.read()
                if '{% for' or '{% if' or '{{ url_for' in contents:
                    yield filename, 'template'

def scan_path(path=None, teardown=True, template_bundles=True):
    for filename, type in walk_path(path):
        with open(filename) as f:
            contents = f.read()
        if type == 'python':
            upgrade_python_file(filename, contents, teardown,
                                template_bundles)
        elif type == 'template':
            upgrade_template_file(filename, contents)

def autodetect_template_bundles(paths):
    folders_with_templates = set()
    for path in paths:
        for filename, type in walk_path(path):
            if type == 'template':
                fullpath = filename.replace(os.path.sep, '/')
                index = fullpath.find('/templates/')
                if index >= 0:
                    folder = fullpath[:index]
                else:
                    folder = posixpath.dirname(fullpath)
                folders_with_templates.add(folder)
    return len(folders_with_templates) > 1

def main():
    """Entrypoint"""
    if ast is None:
        error('Python 2.6 or later is required to run the upgrade script.\n'
              'The runtime requirements for Flask 0.7 however are still '
              'Python 2.5.')

    parser = OptionParser()
    parser.add_option('-T', '--no-teardown-detection', dest='no_teardown',
                      action='store_true', help='Do not attempt to '
                      'detect teardown function rewrites.')
    parser.add_option('-b', '--bundled-templates', dest='bundled_tmpl',
                      action='store_true', help='Indicate to the system '
                      'that templates are bundled with modules.  Default '
                      'is auto detect.')
    parser.add_option('-B', '--no-bundled-templates', dest='no_bundled_tmpl',
                      action='store_true', help='Indicate to the system '
                      'that templates are not bundled with modules.  '
                      'Default is auto detect')
    options, args = parser.parse_args()
    if not args:
        args = ['.']

    if options.no_bundled_tmpl:
        template_bundles = False
    elif options.bundled_tmpl:
        template_bundles = True
    else:
        template_bundles = autodetect_template_bundles(args)

    for path in args:
        scan_path(path, teardown=not options.no_teardown,
                  template_bundles=template_bundles)

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


<commit_before>#!/usr/bin/env python3
# Copyright (c) 2021 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0

# A script to diff between two ram or rom reports generated by
# size_report. When you call call the ram_report or rom_report targets you
# end up with a json file in the build directory that can be used as input
# for this script.

# The output shows which symbols increased and which decreased in size and
# also tracked added/remove symbols as well.

# Example:
#    ./scripts/footprint/fpdiff.py ram1.json ram2.json

from anytree.importer import DictImporter
from anytree import PreOrderIter
from anytree.search  import find

from colorama import Fore
import json
import argparse

importer = DictImporter()

def parse_args():
    parser = argparse.ArgumentParser(
                description="Compare footprint sizes of two builds.")
    parser.add_argument("file1", help="First file")
    parser.add_argument("file2", help="Second file")

    return parser.parse_args()

def main():
    args = parse_args()

    with open(args.file1, "r") as f:
        data = json.load(f)
        root1 = importer.import_(data['symbols'])

    with open(args.file2, "r") as f:
        data = json.load(f)
        root2 = importer.import_(data['symbols'])

    for node in PreOrderIter(root1):
        # pylint: disable=undefined-loop-variable
        n = find(root2, lambda node2: node2.identifier == node.identifier)
        if n:
            if n.size != node.size:
                diff = n.size - node.size
                if diff == 0:
                    continue
                if not n.children or not n.parent:
                    if diff < 0:
                        print(f"{n.identifier} -> {Fore.GREEN}{diff}{Fore.RESET}")
                    else:
                        print(f"{n.identifier} -> {Fore.RED}+{diff}{Fore.RESET}")

        else:
            if not node.children:
                print(f"{node.identifier} ({Fore.GREEN}-{node.size}{Fore.RESET}) disappeared.")

    for node in PreOrderIter(root2):
        n = find(root1, lambda node2: node2.identifier == node.identifier)
        if not n:
            if not node.children and node.size != 0:
                print(f"{node.identifier} ({Fore.RED}+{node.size}{Fore.RESET}) is new.")

if __name__ == "__main__":
    main()
<commit_msg>scripts: fpdiff: iterate on namespaces<commit_after>#!/usr/bin/env python3
# Copyright (c) 2021 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0

# A script to diff between two ram or rom reports generated by
# size_report. When you call call the ram_report or rom_report targets you
# end up with a json file in the build directory that can be used as input
# for this script.

# The output shows which symbols increased and which decreased in size and
# also tracked added/remove symbols as well.

# Example:
#    ./scripts/footprint/fpdiff.py ram1.json ram2.json

from anytree.importer import DictImporter
from anytree import PreOrderIter
from anytree.search  import find

from colorama import Fore
import json
import argparse

importer = DictImporter()

def parse_args():
    parser = argparse.ArgumentParser(
                description="Compare footprint sizes of two builds.")
    parser.add_argument("file1", help="First file")
    parser.add_argument("file2", help="Second file")

    return parser.parse_args()

def main():
    args = parse_args()

    with open(args.file1, "r") as f:
        data1 = json.load(f)

    with open(args.file2, "r") as f:
        data2 = json.load(f)

    for idx, ch in enumerate(data1['symbols']['children']):
        root1 = importer.import_(ch)
        root2 = importer.import_(data2['symbols']['children'][idx])
        print(f"{root1.name}\n+++++++++++++++++++++")

        for node in PreOrderIter(root1):
            # pylint: disable=undefined-loop-variable
            n = find(root2, lambda node2: node2.identifier == node.identifier)
            if n:
                if n.size != node.size:
                    diff = n.size - node.size
                    if diff == 0:
                        continue
                    if not n.children or not n.parent:
                        if diff < 0:
                            print(f"{n.identifier} -> {Fore.GREEN}{diff}{Fore.RESET}")
                        else:
                            print(f"{n.identifier} -> {Fore.RED}+{diff}{Fore.RESET}")

            else:
                if not node.children:
                    print(f"{node.identifier} ({Fore.GREEN}-{node.size}{Fore.RESET}) disappeared.")

        for node in PreOrderIter(root2):
            n = find(root1, lambda node2: node2.identifier == node.identifier)
            if not n:
                if not node.children and node.size != 0:
                    print(f"{node.identifier} ({Fore.RED}+{node.size}{Fore.RESET}) is new.")

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


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

import argparse
import logging
import os
import sys
import re
logger = None

def my_function(blah):
    return
if __name__ == "__main__":
    FORMAT = '%(levelname)s %(asctime)-15s %(name)-20s %(message)s'

    parser = argparse.ArgumentParser(description="program name", formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("input1", type = file)
    parser.add_argument("input2", type = file)
    parser.add_argument("--selection", type = str, default = 'a', choices = ['a', 'b', 'c'], help = 'choose from a,b,c')
    parser.add_argument("--cutoff", type = int, default = 1, help = 'cutoff score')
    parser.add_argument("--variable_args", type = float, action = 'append', nargs = 3,
	                default = [1.0,2.0,1.2], help = '3 scores')
    parser.add_argument("--verbose","-v", action = 'count',  help='increase verbosity')
    args = parser.parse_args()

    if args.verbose >= 1:
        logging.basicConfig(level=logging.DEBUG, format = FORMAT)
    else:
        logging.basicConfig(level=logging.INFO, format=FORMAT)
    logger = logging.getLogger(__name__)

    logger.info("working hard ...")
    my_function(args.input1, args.input2)
    logger.info("Done.")
<commit_msg>better debug logging<commit_after>#!/usr/bin/env python

import argparse
import logging
import os
import sys
import re
logger = None

def my_function(blah):
    return
if __name__ == "__main__":
    INFO_FORMAT = '%(levelname)s %(asctime)-15s %(name)-20s %(message)s'
    DEBUG_FORMAT = '%(levelname)s %(asctime)-15s %(name)-15s %(funcName)-20s %(message)s'

    parser = argparse.ArgumentParser(description="program name", formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("input1", type = file)
    parser.add_argument("input2", type = file)
    parser.add_argument("--selection", type = str, default = 'a', choices = ['a', 'b', 'c'], help = 'choose from a,b,c')
    parser.add_argument("--cutoff", type = int, default = 1, help = 'cutoff score')
    parser.add_argument("--variable_args", type = float, action = 'append', nargs = 3,
	                default = [1.0,2.0,1.2], help = '3 scores')
    parser.add_argument("--log_to_console", action = 'store_true', help = 'log to console instead of a file')
    parser.add_argument("--verbose","-v", action = 'count',  help='increase verbosity')
    args = parser.parse_args()

    if args.verbose >= 1:
        if args.log_to_console:
            logging.basicConfig(level=logging.DEBUG, format = DEBUG_FORMAT)
        else:
            logging.basicConfig(level=logging.DEBUG, format = DEBUG_FORMAT, filename=args.prefix + '.log')
    else:
        if args.log_to_console:
            logging.basicConfig(level=logging.INFO, format= INFO_FORMAT)
        else:
            logging.basicConfig(level=logging.INFO, format= INFO_FORMAT, filename=args.prefix + '.log')
    logger = logging.getLogger(__name__)

    logger.info("working hard ...")
    my_function(args.input1, args.input2)
    logger.info("Done.")
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (C) 2013  Florian da Costa - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from lxml import etree

from openerp import models, fields, api, _
from openerp.exceptions import Warning as UserError

from openerp.addons.l10n_br_account.models.account_invoice import (
    OPERATION_TYPE,
    JOURNAL_TYPE)

class AccountInvoiceRefund(models.TransientModel):
    _inherit = "account.invoice.refund"

    force_fiscal_category_id = fields.Many2one(
        'l10n_br_account.fiscal.category', 'Forçar Categoria Fiscal',
        domain="[('journal_type', '=', 'sale_refund'),\
                ('fiscal_type', '=', 'product')]")

    @api.multi
    def compute_refund(self, mode='refund'):
        inv_obj = self.env['account.invoice']
        context = dict(self.env.context)

        for send_invoice in inv_obj.browse(context.get('active_ids')):
            result = super(AccountInvoiceRefund, self).compute_refund(mode)
            domain = result['domain']

            ids_domain = [x for x in domain if x[0] == 'id'][0]
            invoice_ids = ids_domain[2]
            for invoice in inv_obj.browse(invoice_ids):

                if not self.force_fiscal_category_id and not \
                        invoice.fiscal_category_id.refund_fiscal_category_id:
                    raise UserError(_("Categoria Fiscal: %s não possui uma "
                                      "catégoria de devolução!") %
                                    invoice.fiscal_category_id.name)

                fiscal_category_id = self.force_fiscal_category_id.id or \
                    invoice.fiscal_category_id.refund_fiscal_category_id.id

                onchange = invoice.onchange_fiscal_category_id(
                    invoice.partner_id.id, invoice.partner_id.id,
                    invoice.company_id.id, fiscal_category_id)

                onchange['value']['fiscal_category_id'] = \
                    fiscal_category_id
                onchange['value']['nfe_purpose'] = '4'

                for line in invoice.invoice_line:

                    if not self.force_fiscal_category_id and not \
                            line.fiscal_category_id.refund_fiscal_category_id:
                        raise UserError(_("Categoria Fiscal: %s não possui"
                                          " uma catégoria de devolução!") %
                                        line.fiscal_category_id.name)

                    line_fiscal_category_id = (line.fiscal_category_id
                                               .refund_fiscal_category_id.id)

                    line_onchange = line.onchange_fiscal_position(
                        invoice.partner_id.id, invoice.company_id.id,
                        line.product_id.id, line_fiscal_category_id,
                        line.account_id.id, line.quantity,
                        line.price_unit, line.discount, line.insurance_value,
                        line.freight_value, line.other_costs_value)
                    line_onchange['value']['fiscal_category_id'] = \
                        line_fiscal_category_id
                    line.write(line_onchange['value'])
                invoice.write(onchange['value'])
            return result

    def fields_view_get(self, cr, uid, view_id=None, view_type='form',
                        context=None, toolbar=False, submenu=False):
        result = super(AccountInvoiceRefund, self).fields_view_get(
            cr, uid, view_id, view_type, context, toolbar, submenu)
        if not context:
            context = {}
        type = context.get('type', 'out_invoice')
        journal_type = JOURNAL_TYPE[type]
        type = OPERATION_TYPE[type]
        eview = etree.fromstring(result['arch'])
        fiscal_categ = eview.xpath("//field[@name='fiscal_category_id']")
        for field in fiscal_categ:
            field.set('domain', "[('journal_type', '=', '%s'),\
                                 ('fiscal_type', '=', 'product'),\
                                 ('type', '=', '%s')]" % (journal_type,
                                                          type))
        result['arch'] = etree.tostring(eview)
        return result
<commit_msg>[FIX] account invoice refund method<commit_after># -*- coding: utf-8 -*-
# Copyright (C) 2013  Florian da Costa - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from lxml import etree

from openerp import models, fields, api, _
from openerp.exceptions import Warning as UserError

from openerp.addons.l10n_br_account.models.account_invoice import (
    OPERATION_TYPE,
    JOURNAL_TYPE)

class AccountInvoiceRefund(models.TransientModel):
    _inherit = "account.invoice.refund"

    force_fiscal_category_id = fields.Many2one(
        'l10n_br_account.fiscal.category', 'Forçar Categoria Fiscal',
        domain="[('journal_type', '=', 'sale_refund'),\
                ('fiscal_type', '=', 'product')]")

    @api.multi
    def compute_refund(self, mode='refund'):
        inv_obj = self.env['account.invoice']
        context = dict(self.env.context)

        for send_invoice in inv_obj.browse(context.get('active_ids')):
            result = super(AccountInvoiceRefund, self).compute_refund(mode)
            domain = result['domain']

            ids_domain = [x for x in domain if x[0] == 'id'][0]
            invoice_ids = ids_domain[2]
            for invoice in inv_obj.browse(invoice_ids):

                if not self.force_fiscal_category_id and not \
                        invoice.fiscal_category_id.refund_fiscal_category_id:
                    raise UserError(_("Categoria Fiscal: %s não possui uma "
                                      "catégoria de devolução!") %
                                    invoice.fiscal_category_id.name)

                invoice.fiscal_category_id = (
                    self.force_fiscal_category_id.id or
                    invoice.fiscal_category_id.refund_fiscal_category_id.id)

                invoice.onchange_fiscal()

                invoice_values = {
                    'fiscal_category_id': invoice.fiscal_category_id.id,
                    'fiscal_position': invoice.fiscal_position.id,
                    'nfe_purpose': '4'
                }

                for line in invoice.invoice_line:
                    if not self.force_fiscal_category_id and not \
                            line.fiscal_category_id.refund_fiscal_category_id:
                        raise UserError(_(u"Categoria Fiscal: %s não possui"
                                          u" uma catégoria de devolução!") %
                                        line.fiscal_category_id.name)

                    line.fiscal_category_id = (
                        self.force_fiscal_category_id.id or
                        line.fiscal_category_id.refund_fiscal_category_id.id)

                    line.onchange_fiscal()

                    line_values = {
                        'fiscal_category_id': line.fiscal_category_id.id,
                        'fiscal_position': line.fiscal_position.id,
                        'cfop_id': line.fiscal_position.cfop_id.id
                    }
                    line.write(line_values)
                invoice.write(invoice_values)
            return result

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


<commit_before>from nalaf.learning.lib.sklsvm import SklSVM
from nalaf.structures.data import Dataset
from nalaf.features.stemming import ENGLISH_STEMMER
from loctext.learning.train import read_corpus
from loctext.util import PRO_ID, LOC_ID, ORG_ID, REL_PRO_LOC_ID, repo_path
from loctext.learning.annotators import LocTextDXModelRelationExtractor
from util import my_cv_generator
import time
from collections import Counter
import pickle

corpus = read_corpus("LocText")

mode = "unnormalized"

mention_mode_total_counter = {key: Counter() for key in [PRO_ID, LOC_ID, ORG_ID]}
relation_mode_total_counter = {key: Counter() for key in [PRO_ID, LOC_ID, ORG_ID]}
ratio_mode_total_counter = {key: Counter() for key in [PRO_ID, LOC_ID, ORG_ID]}

#

def entity2text(entity):
    return ENGLISH_STEMMER.stem(entity.text)

def entity2norm(entity):
    n_id, n_val = list(entity.normalisation_dict.items())[0]
    return n_val

def entity2key(entity):
    if mode == "unnormalized":
        return entity2text(entity)
    elif mode == "normalized":
        return entity2norm(entity)

#

for entity in corpus.entities():
    mention_mode_total_counter[entity.class_id].update({entity2key(entity)})

for rel in corpus.relations():
    if rel.class_id != REL_PRO_LOC_ID:
        continue

    relation_mode_total_counter[rel.entity1.class_id].update({entity2key(rel.entity1)})
    relation_mode_total_counter[rel.entity2.class_id].update({entity2key(rel.entity2)})

for type_key, mention_counter in mention_mode_total_counter.items():
    relation_counter = relation_mode_total_counter[type_key]

    for entity_key, mention_count in mention_counter.items():
        relation_count = relation_counter.get(entity_key, 0)
        ratio_mode_total_counter[type_key][entity_key] = (relation_count / mention_count)

#######################################################################################################################

for type_key, ratio_counter in ratio_mode_total_counter.items():
    if type_key == LOC_ID:
        ratio_sorted = sorted(ratio_counter.items(), key=lambda pair: pair[1])

        print()
        for entity_key, val in ratio_sorted:
            print('"{}": {},'.format(entity_key, val))

        print()
        print("Total entity_keys: ", len(ratio_sorted))
        print()

print()

out_path = repo_path(["resources", "features", "corpus_" + mode + "_total_background_loc_rels_ratios.pickle"])
with open(out_path, "wb") as f:
    pickle.dump(ratio_mode_total_counter[LOC_ID], f)

for marker in {"GFP", "RFP", "CYH2", "ALG2", "MSB2", "KSS1", "KRE11", "SER2", "Snf7"}:
    print(marker, ratio_mode_total_counter[PRO_ID].get(ENGLISH_STEMMER.stem(marker)))
<commit_msg>print all<commit_after>from nalaf.learning.lib.sklsvm import SklSVM
from nalaf.structures.data import Dataset
from nalaf.features.stemming import ENGLISH_STEMMER
from loctext.learning.train import read_corpus
from loctext.util import PRO_ID, LOC_ID, ORG_ID, REL_PRO_LOC_ID, repo_path
from loctext.learning.annotators import LocTextDXModelRelationExtractor
from util import my_cv_generator
import time
from collections import Counter
import pickle

corpus = read_corpus("LocText")

mode = "unnormalized"

mention_mode_total_counter = {key: Counter() for key in [PRO_ID, LOC_ID, ORG_ID]}
relation_mode_total_counter = {key: Counter() for key in [PRO_ID, LOC_ID, ORG_ID]}
ratio_mode_total_counter = {key: Counter() for key in [PRO_ID, LOC_ID, ORG_ID]}

#

def entity2text(entity):
    return ENGLISH_STEMMER.stem(entity.text)

def entity2norm(entity):
    n_id, n_val = list(entity.normalisation_dict.items())[0]
    return n_val

def entity2key(entity):
    if mode == "unnormalized":
        return entity2text(entity)
    elif mode == "normalized":
        return entity2norm(entity)

#

for entity in corpus.entities():
    mention_mode_total_counter[entity.class_id].update({entity2key(entity)})

for rel in corpus.relations():
    if rel.class_id != REL_PRO_LOC_ID:
        continue

    relation_mode_total_counter[rel.entity1.class_id].update({entity2key(rel.entity1)})
    relation_mode_total_counter[rel.entity2.class_id].update({entity2key(rel.entity2)})

for type_key, mention_counter in mention_mode_total_counter.items():
    relation_counter = relation_mode_total_counter[type_key]

    for entity_key, mention_count in mention_counter.items():
        relation_count = relation_counter.get(entity_key, 0)
        ratio_mode_total_counter[type_key][entity_key] = (relation_count / mention_count)

#######################################################################################################################

for type_key, ratio_counter in mention_mode_total_counter.items():
    # if type_key == LOC_ID:
        ratio_sorted = sorted(ratio_counter.items(), key=lambda pair: pair[1])

        print()
        for entity_key, val in ratio_sorted:
            print('"{}": {},'.format(entity_key, val))

        print()
        print("Total entity_keys: ", len(ratio_sorted))
        print()

for type_key, ratio_counter in ratio_mode_total_counter.items():
    # if type_key == LOC_ID:
        ratio_sorted = sorted(ratio_counter.items(), key=lambda pair: pair[1])

        print()
        for entity_key, val in ratio_sorted:
            print('"{}": {},'.format(entity_key, val))

        print()
        print("Total entity_keys: ", len(ratio_sorted))
        print()

print()

out_path = repo_path("resources", "features", "corpus_" + mode + "_total_background_loc_rels_ratios.pickle")
with open(out_path, "wb") as f:
    pickle.dump(ratio_mode_total_counter[LOC_ID], f)

for marker in {"GFP", "RFP", "CYH2", "ALG2", "MSB2", "KSS1", "KRE11", "SER2", "Snf7"}:
    print(marker, ratio_mode_total_counter[PRO_ID].get(ENGLISH_STEMMER.stem(marker)))
<|endoftext|>


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

from landlab import RasterModelGrid
from landlab.components.craters.component import CratersComponent

def main():
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('--impact-count', type=int, default=128,
                        help='Number of impacts')
    parser.add_argument('--grid-size', type=int, default=200,
                        help='Number of grid rows and columns')
    parser.add_argument('--grid-spacing', type=float, default=.025,
                        help='Spacing between grid rows and columns')

    seed_parser = parser.add_mutually_exclusive_group()
    seed_parser.add_argument('--with-seed', type=int, default=1973,
                             help='Seed for random number generator')
    seed_parser.add_argument('--without-seed', action='store_true',
                             default=False,
                             help='Seed for random number generator')

    args = parser.parse_args()

    shape = (args.grid_size, args.grid_size)
    spacing = (args.grid_spacing, args.grid_spacing)
    impact_count = args.impact_count
    if args.without_seed:
        seed = None
    else:
        seed = args.with_seed

    grid = RasterModelGrid(shape[0], shape[1], spacing[0])

    craters_comp = CratersComponent(grid, seed=seed)
    for _ in xrange(impact_count):
        craters_comp.update()

    grid.imshow('node', 'planet_surface__elevation', grid_units=('km', 'km'),
                symmetric_cbar=True)

if __name__ == "__main__":
    main()
<commit_msg>Added a comment so users know how to run without default values.<commit_after>#! /usr/bin/env python

from landlab.grid import RasterModelGrid
from landlab.components.craters.component import CratersComponent

def main():
    import argparse
    #nmg comments
    #The argparse library provide an easy way to enter parameter values on
    #the command line when running code.
    #Default values are provided, but they can also be
    #provided at run time.
    #
    #For Example, to specify the number of impacts when running:
    #python craters_component.py --impact-count 200
    #
    #to specify the number of impacts and grid size:
    #python craters_component.py --impact-count 200 --grid-size 500

    parser = argparse.ArgumentParser()
    parser.add_argument('--impact-count', type=int, default=128,
                        help='Number of impacts')
    parser.add_argument('--grid-size', type=int, default=200,
                        help='Number of grid rows and columns')
    parser.add_argument('--grid-spacing', type=float, default=.025,
                        help='Spacing between grid rows and columns')

    seed_parser = parser.add_mutually_exclusive_group()
    seed_parser.add_argument('--with-seed', type=int, default=1973,
                             help='Seed for random number generator')
    seed_parser.add_argument('--without-seed', action='store_true',
                             default=False,
                             help='Seed for random number generator')

    args = parser.parse_args()

    shape = (args.grid_size, args.grid_size)
    spacing = (args.grid_spacing, args.grid_spacing)
    impact_count = args.impact_count
    if args.without_seed:
        seed = None
    else:
        seed = args.with_seed

    grid = RasterModelGrid(shape[0], shape[1], spacing[0])

    craters_comp = CratersComponent(grid, seed=seed)
    for _ in xrange(impact_count):
        craters_comp.update()

    grid.imshow('node', 'planet_surface__elevation', grid_units=('km', 'km'),
                symmetric_cbar=True)

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