<commit_before>"""
Module to access lists of recent phone calls: incoming, outgoing and
missed ones.
"""
# This module is part of the FritzConnection package.
# https://github.com/kbr/fritzconnection
# License: MIT (https://opensource.org/licenses/MIT)
# Author: Klaus Bremer

import datetime

from ..core.processor import (
    processor,
    process_node,
    InstanceAttributeFactory,
    Storage,
)
from ..core.utils import get_xml_root
from .fritzbase import AbstractLibraryBase

__all__ = ['FritzCall', 'Call']

ALL_CALL_TYPES = 0
RECEIVED_CALL_TYPE = 1
MISSED_CALL_TYPE = 2
OUT_CALL_TYPE = 3

SERVICE = 'X_AVM-DE_OnTel1'

def datetime_converter(date_string):
    if not date_string:
        return date_string
    return datetime.datetime.strptime(date_string, '%d.%m.%y %H:%M')

def timedelta_converter(duration_string):
    if not duration_string:
        return duration_string
    hours, minutes = [int(part) for part in duration_string.split(':', 1)]
    return datetime.timedelta(hours=hours, minutes=minutes)

class FritzCall(AbstractLibraryBase):
    """
    Can dial phone numbers and gives access to lists of recent phone
    calls: incoming, outgoing and missed ones. All parameters are
    optional. If given, they have the following meaning: `fc` is an
    instance of FritzConnection, `address` the ip of the Fritz!Box,
    `port` the port to connect to, `user` the username, `password` the
    password, `timeout` a timeout as floating point number in seconds,
    `use_tls` a boolean indicating to use TLS (default False).
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.calls = None

    def _update_calls(self, num=None, days=None):
        result = self.fc.call_action(SERVICE, 'GetCallList')
        url = result['NewCallListURL']
        if days:
            url += f'&days={days}'
        elif num:
            url += f'&max={num}'
        root = get_xml_root(url, session=self.fc.session)
        self.calls = CallCollection(root)

    def get_calls(self, calltype=ALL_CALL_TYPES, update=True,
                        num=None, days=None):
        """
        Return a list of Call instances of type calltypes. If calltype
        is 0 all calls are listet. If *update* is True, all calls are
        reread from the router. *num* maximum number of entries in call
        list. *days* number of days to look back for calls e.g. 1: calls
        from today and yesterday, 7: calls from the complete last week.
        """
        if not self.calls:
            update = True
        if update:
            self._update_calls(num, days)
        if calltype == ALL_CALL_TYPES:
            return self.calls.calls
        return [call for call in self.calls if call.type == calltype]

    def get_received_calls(self, update=True, num=None, days=None):
        """
        Return a list of Call instances of received calls. If *update*
        is True, all calls are reread from the router. *num* maximum
        number of entries in call list. *days* number of days to look
        back for calls e.g. 1: calls from today and yesterday, 7: calls
        from the complete last week.
        """
        return self.get_calls(RECEIVED_CALL_TYPE, update, num, days)

    def get_missed_calls(self, update=True, num=None, days=None):
        """
        Return a list of Call instances of missed calls. If *update* is
        True, all calls are reread from the router. *num* maximum number
        of entries in call list. *days* number of days to look back for
        calls e.g. 1: calls from today and yesterday, 7: calls from the
        complete last week.
        """
        return self.get_calls(MISSED_CALL_TYPE, update, num, days)

    def get_out_calls(self, update=True, num=None, days=None):
        """
        Return a list of Call instances of outgoing calls. If *update*
        is True, all calls are reread from the router. *num* maximum
        number of entries in call list. *days* number of days to look
        back for calls e.g. 1: calls from today and yesterday, 7: calls
        from the complete last week.
        """
        return self.get_calls(OUT_CALL_TYPE, update, num, days)

    def dial(self, number):
        """
        Dials the given *number* (number must be a string, as phone
        numbers are allowed to start with leading zeros). This method
        has no return value, but will raise an error reported from the
        Fritz!Box on failure. **Note:** The dial-help of the Fritz!Box
        must be activated to make this work.
        """
        arg = {'NewX_AVM-DE_PhoneNumber': number}
        self.fc.call_action('X_VoIP1', 'X_AVM-DE_DialNumber', arguments=arg)

class AttributeConverter:
    """
    Data descriptor returning converted attribute values.
    """
    def __init__(self, attribute_name, converter=str):
        self.attribute_name = attribute_name
        self.converter = converter

    def __set__(self, obj, value):
        return NotImplemented

    def __get__(self, obj, objtype):
        attr = getattr(obj, self.attribute_name)
        try:
            attr = self.converter(attr)
        except (TypeError, ValueError):
            pass
        return attr

@processor
class Call:
    """
    Represents a call with the attributes provided by AVM. Instance
    attributes are *Id*, *Type*, *Called*, *Caller*, *CallerNumber*,
    *CalledNumber*, *Name*, *Device*, *Port*, *Date*, *Duration* and
    *Count*. The spelling represents the original xml-node names.
    Additionally the following attributes can be accessed by lowercase
    names: *id* returning the Id as integer, *type* returning the Type
    as integer, *date* returning the Date as datetime-instance,
    *duration* returning the Duration as timedelta-instance.
    """
    id = AttributeConverter('Id', int)
    type = AttributeConverter('Type', int)
    date = AttributeConverter('Date', datetime_converter)
    duration = AttributeConverter('Duration', timedelta_converter)

    def __init__(self):
        self.Id = None
        self.Type = None
        self.Called = None
        self.Caller = None
        self.CallerNumber = None
        self.CalledNumber = None
        self.Name = None
        self.Device = None
        self.Port = None
        self.Date = None
        self.Duration = None
        self.Count = None

    def __str__(self):
        number = self.Called if self.type == 3 else self.Caller
        duration = self.Duration if self.type != 2 else "-"
        if not number:
            number = "-"
        return f'{self.Type:>6}   {number:24}{self.Date:>18}{duration:>12}'

class CallCollection(Storage):
    """
    Container for a sequence of Call instances.
    """
    Call = InstanceAttributeFactory(Call)

    def __init__(self, root):
        self.timestamp = None
        self.calls = list()
        super().__init__(self.calls)
        process_node(self, root)

    def __iter__(self):
        return iter(self.calls)
<commit_msg>Fix issue 54 for completion add all documented call types (#57)<commit_after>"""
Module to access lists of recent phone calls: incoming, outgoing and
missed ones.
"""
# This module is part of the FritzConnection package.
# https://github.com/kbr/fritzconnection
# License: MIT (https://opensource.org/licenses/MIT)
# Author: Klaus Bremer

import datetime

from ..core.processor import (
    processor,
    process_node,
    InstanceAttributeFactory,
    Storage,
)
from ..core.utils import get_xml_root
from .fritzbase import AbstractLibraryBase

__all__ = ['FritzCall', 'Call']

ALL_CALL_TYPES = 0
RECEIVED_CALL_TYPE = 1
MISSED_CALL_TYPE = 2
OUT_CALL_TYPE = 3
ACTIVE_RECEIVED_CALL_TYPE = 9
REJECTED_CALL_TYPE = 10
ACTIVE_OUT_CALL_TYPE = 11

SERVICE = 'X_AVM-DE_OnTel1'

def datetime_converter(date_string):
    if not date_string:
        return date_string
    return datetime.datetime.strptime(date_string, '%d.%m.%y %H:%M')

def timedelta_converter(duration_string):
    if not duration_string:
        return duration_string
    hours, minutes = [int(part) for part in duration_string.split(':', 1)]
    return datetime.timedelta(hours=hours, minutes=minutes)

class FritzCall(AbstractLibraryBase):
    """
    Can dial phone numbers and gives access to lists of recent phone
    calls: incoming, outgoing and missed ones. All parameters are
    optional. If given, they have the following meaning: `fc` is an
    instance of FritzConnection, `address` the ip of the Fritz!Box,
    `port` the port to connect to, `user` the username, `password` the
    password, `timeout` a timeout as floating point number in seconds,
    `use_tls` a boolean indicating to use TLS (default False).
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.calls = None

    def _update_calls(self, num=None, days=None):
        result = self.fc.call_action(SERVICE, 'GetCallList')
        url = result['NewCallListURL']
        if days:
            url += f'&days={days}'
        elif num:
            url += f'&max={num}'
        root = get_xml_root(url, session=self.fc.session)
        self.calls = CallCollection(root)

    def get_calls(self, calltype=ALL_CALL_TYPES, update=True,
                        num=None, days=None):
        """
        Return a list of Call instances of type calltypes. If calltype
        is 0 all calls are listet. If *update* is True, all calls are
        reread from the router. *num* maximum number of entries in call
        list. *days* number of days to look back for calls e.g. 1: calls
        from today and yesterday, 7: calls from the complete last week.
        """
        if not self.calls:
            update = True
        if update:
            self._update_calls(num, days)
        if calltype == ALL_CALL_TYPES:
            return self.calls.calls
        return [call for call in self.calls if call.type == calltype]

    def get_received_calls(self, update=True, num=None, days=None):
        """
        Return a list of Call instances of received calls. If *update*
        is True, all calls are reread from the router. *num* maximum
        number of entries in call list. *days* number of days to look
        back for calls e.g. 1: calls from today and yesterday, 7: calls
        from the complete last week.
        """
        return self.get_calls(RECEIVED_CALL_TYPE, update, num, days)

    def get_missed_calls(self, update=True, num=None, days=None):
        """
        Return a list of Call instances of missed calls. If *update* is
        True, all calls are reread from the router. *num* maximum number
        of entries in call list. *days* number of days to look back for
        calls e.g. 1: calls from today and yesterday, 7: calls from the
        complete last week.
        """
        return self.get_calls(MISSED_CALL_TYPE, update, num, days)

    def get_out_calls(self, update=True, num=None, days=None):
        """
        Return a list of Call instances of outgoing calls. If *update*
        is True, all calls are reread from the router. *num* maximum
        number of entries in call list. *days* number of days to look
        back for calls e.g. 1: calls from today and yesterday, 7: calls
        from the complete last week.
        """
        return self.get_calls(OUT_CALL_TYPE, update, num, days)

    def dial(self, number):
        """
        Dials the given *number* (number must be a string, as phone
        numbers are allowed to start with leading zeros). This method
        has no return value, but will raise an error reported from the
        Fritz!Box on failure. **Note:** The dial-help of the Fritz!Box
        must be activated to make this work.
        """
        arg = {'NewX_AVM-DE_PhoneNumber': number}
        self.fc.call_action('X_VoIP1', 'X_AVM-DE_DialNumber', arguments=arg)

class AttributeConverter:
    """
    Data descriptor returning converted attribute values.
    """
    def __init__(self, attribute_name, converter=str):
        self.attribute_name = attribute_name
        self.converter = converter

    def __set__(self, obj, value):
        return NotImplemented

    def __get__(self, obj, objtype):
        attr = getattr(obj, self.attribute_name)
        try:
            attr = self.converter(attr)
        except (TypeError, ValueError):
            pass
        return attr

@processor
class Call:
    """
    Represents a call with the attributes provided by AVM. Instance
    attributes are *Id*, *Type*, *Called*, *Caller*, *CallerNumber*,
    *CalledNumber*, *Name*, *Device*, *Port*, *Date*, *Duration* and
    *Count*. The spelling represents the original xml-node names.
    Additionally the following attributes can be accessed by lowercase
    names: *id* returning the Id as integer, *type* returning the Type
    as integer, *date* returning the Date as datetime-instance,
    *duration* returning the Duration as timedelta-instance.
    """
    id = AttributeConverter('Id', int)
    type = AttributeConverter('Type', int)
    date = AttributeConverter('Date', datetime_converter)
    duration = AttributeConverter('Duration', timedelta_converter)

    def __init__(self):
        self.Id = None
        self.Type = None
        self.Called = None
        self.Caller = None
        self.CallerNumber = None
        self.CalledNumber = None
        self.Name = None
        self.Device = None
        self.Port = None
        self.Date = None
        self.Duration = None
        self.Count = None

    def __str__(self):
        number = self.Called if self.type == 3 else self.Caller
        duration = self.Duration if self.type != 2 else "-"
        if not number:
            number = "-"
        return f'{self.Type:>6}   {number:24}{self.Date:>18}{duration:>12}'

class CallCollection(Storage):
    """
    Container for a sequence of Call instances.
    """
    Call = InstanceAttributeFactory(Call)

    def __init__(self, root):
        self.timestamp = None
        self.calls = list()
        super().__init__(self.calls)
        process_node(self, root)

    def __iter__(self):
        return iter(self.calls)
<|endoftext|>


<commit_before>import logging

from fontTools import ttLib
from fontTools.pens.hashPointPen import HashPointPen
from fontTools.ttLib.tables._g_l_y_f import (
    OVERLAP_COMPOUND,
    ROUND_XY_TO_GRID,
    USE_MY_METRICS,
)

from ufo2ft.constants import (
    OBJECT_LIBS_KEY,
    TRUETYPE_INSTRUCTIONS_KEY,
    TRUETYPE_METRICS_KEY,
    TRUETYPE_OVERLAP_KEY,
    TRUETYPE_ROUND_KEY,
)

logger = logging.getLogger(__name__)

class InstructionCompiler(object):
    def __init__(self, ufo, ttf):
        self.ufo = ufo
        self.font = ttf

    def _compile_program(self, key, table_tag):
        assert table_tag in ("prep", "fpgm")
        ttdata = self.ufo.lib.get(TRUETYPE_INSTRUCTIONS_KEY, None)
        if ttdata:
            formatVersion = ttdata.get("formatVersion", None)
            if int(formatVersion) != 1:
                raise NotImplementedError(
                    f"Unknown formatVersion {formatVersion} "
                    f"for instructions in lib key '{key}'."
                )
            asm = ttdata.get(key, None)
            if asm is not None:
                self.font[table_tag] = table = ttLib.newTable(table_tag)
                table.program = ttLib.tables.ttProgram.Program()
                table.program.fromAssembly(asm)

    def compile_fpgm(self):
        self._compile_program("fontProgram", "fpgm")

    def compile_glyf(self):
        for name in sorted(self.ufo.keys()):
            self._compile_tt_glyph(name)

    def _compile_tt_glyph(self, name):
        glyph = self.ufo[name]
        ttdata = glyph.lib.get(TRUETYPE_INSTRUCTIONS_KEY, None)
        if name not in self.font["glyf"]:
            if ttdata is not None:
                logger.debug(
                    f"Glyph '{name}' not found in font, "
                    "skipping compilation of TrueType instructions "
                    "for this glyph."
                )
            return

        ttglyph = self.font["glyf"][name]
        if ttdata is not None:
            self._compile_tt_glyph_program(glyph, ttglyph, ttdata)
        if ttglyph.isComposite():
            self._set_composite_flags(glyph, ttglyph)

    def _compile_tt_glyph_program(self, glyph, ttglyph, ttdata):
        formatVersion = ttdata.get("formatVersion", None)
        if int(formatVersion) != 1:
            raise NotImplementedError(
                f"Unknown formatVersion {formatVersion} "
                "for instructions in glyph '{name}'."
            )
            return

        # Check if glyph hash matches the current outlines
        hash_pen = HashPointPen(glyph.width, self.ufo)
        glyph.drawPoints(hash_pen)
        glyph_id = ttdata.get("id", None)
        if glyph_id is None:
            # The glyph hash is required
            logger.error(
                f"Glyph hash missing, glyph '{glyph.name}' will have "
                "no instructions in font."
            )
            return

        if glyph_id != hash_pen.hash:
            logger.error(
                f"Glyph hash mismatch, glyph '{glyph.name}' will have "
                "no instructions in font."
            )
            return

        # Compile the glyph program
        asm = ttdata.get("assembly", None)
        if asm is None:
            # The "assembly" key is required.
            logger.error(
                f"Glyph assembly missing, glyph '{glyph.name}' will have "
                "no instructions in font."
            )
            return

        ttglyph.program = ttLib.tables.ttProgram.Program()
        ttglyph.program.fromAssembly(asm)

    def _set_composite_flags(self, glyph, ttglyph):
        # Remove empty glyph programs from composite glyphs
        if hasattr(ttglyph, "program") and not ttglyph.program:
            delattr(ttglyph, "program")

        # Set component flags

        # We need to decide when to set the flags.
        # Let's assume if any lib key is not there, or the component
        # doesn't have an identifier, we should leave the flags alone.
        use_my_metrics_comp = None
        if len(ttglyph.components) != len(glyph.components):
            logger.error(
                "Number of components differ between UFO and TTF "
                f"in glyph '{glyph.name}' ({len(glyph.components)} vs. "
                f"{len(ttglyph.components)}, not setting component flags."
            )
            return

        for i, c in enumerate(ttglyph.components):
            ufo_component_id = glyph.components[i].identifier
            if (
                ufo_component_id is not None
                and OBJECT_LIBS_KEY in glyph.lib
                and ufo_component_id in glyph.lib[OBJECT_LIBS_KEY]
                and (
                    TRUETYPE_ROUND_KEY in glyph.lib[OBJECT_LIBS_KEY][ufo_component_id]
                    or TRUETYPE_METRICS_KEY
                    in glyph.lib[OBJECT_LIBS_KEY][ufo_component_id]
                )
            ):
                component_lib = glyph.lib[OBJECT_LIBS_KEY][ufo_component_id]

                # https://github.com/googlefonts/ufo2ft/pull/425 recommends
                # to always set the ROUND_XY_TO_GRID flag, so we only
                # unset it if explicitly done so in the lib
                if component_lib.get(TRUETYPE_ROUND_KEY, True):
                    c.flags |= ROUND_XY_TO_GRID
                else:
                    c.flags &= ~ROUND_XY_TO_GRID

                if component_lib.get(TRUETYPE_METRICS_KEY, False):
                    c.flags &= ~USE_MY_METRICS
                    if use_my_metrics_comp:
                        logger.warning(
                            "Ignoring USE_MY_METRICS flag on component "
                            f"'{ufo_component_id}' because it has been set on "
                            f"component '{use_my_metrics_comp}' already "
                            f"in glyph {glyph.name}."
                        )
                    else:
                        c.flags |= USE_MY_METRICS
                        use_my_metrics_comp = ufo_component_id

            if i == 0 and TRUETYPE_OVERLAP_KEY in glyph.lib:
                # Set OVERLAP_COMPOUND on the first component only
                if glyph.lib.get(TRUETYPE_OVERLAP_KEY, False):
                    c.flags |= OVERLAP_COMPOUND
                else:
                    c.flags &= ~OVERLAP_COMPOUND

    def compile_maxp(self):
        maxp = self.font["maxp"]
        ttdata = self.ufo.lib.get(TRUETYPE_INSTRUCTIONS_KEY, None)
        if ttdata:
            for name in (
                "maxStorage",
                "maxFunctionDefs",
                "maxInstructionDefs",
                "maxStackElements",
                # "maxSizeOfInstructions",  # Is recalculated below
                "maxZones",
                "maxTwilightPoints",
            ):
                value = ttdata.get(name, None)
                if value is not None:
                    setattr(maxp, name, value)

        # Recalculate maxp.maxSizeOfInstructions
        sizes = [
            len(ttglyph.program.getBytecode())
            for ttglyph in self.font["glyf"].glyphs.values()
            if hasattr(ttglyph, "program")
        ]
        maxp.maxSizeOfInstructions = max(sizes, default=0)

    def compile_prep(self):
        self._compile_program("controlValueProgram", "prep")

    def compile(self):
        self.compile_fpgm()
        self.compile_prep()
        self.compile_glyf()
        # maxp depends on the other programs, to it needs to be last
        self.compile_maxp()
<commit_msg>Sort methods<commit_after>import logging

from fontTools import ttLib
from fontTools.pens.hashPointPen import HashPointPen
from fontTools.ttLib.tables._g_l_y_f import (
    OVERLAP_COMPOUND,
    ROUND_XY_TO_GRID,
    USE_MY_METRICS,
)

from ufo2ft.constants import (
    OBJECT_LIBS_KEY,
    TRUETYPE_INSTRUCTIONS_KEY,
    TRUETYPE_METRICS_KEY,
    TRUETYPE_OVERLAP_KEY,
    TRUETYPE_ROUND_KEY,
)

logger = logging.getLogger(__name__)

class InstructionCompiler(object):
    def __init__(self, ufo, ttf):
        self.ufo = ufo
        self.font = ttf

    def _compile_program(self, key, table_tag):
        assert table_tag in ("prep", "fpgm")
        ttdata = self.ufo.lib.get(TRUETYPE_INSTRUCTIONS_KEY, None)
        if ttdata:
            formatVersion = ttdata.get("formatVersion", None)
            if int(formatVersion) != 1:
                raise NotImplementedError(
                    f"Unknown formatVersion {formatVersion} "
                    f"for instructions in lib key '{key}'."
                )
            asm = ttdata.get(key, None)
            if asm is not None:
                self.font[table_tag] = table = ttLib.newTable(table_tag)
                table.program = ttLib.tables.ttProgram.Program()
                table.program.fromAssembly(asm)

    def _compile_tt_glyph(self, name):
        glyph = self.ufo[name]
        ttdata = glyph.lib.get(TRUETYPE_INSTRUCTIONS_KEY, None)
        if name not in self.font["glyf"]:
            if ttdata is not None:
                logger.debug(
                    f"Glyph '{name}' not found in font, "
                    "skipping compilation of TrueType instructions "
                    "for this glyph."
                )
            return

        ttglyph = self.font["glyf"][name]
        if ttdata is not None:
            self._compile_tt_glyph_program(glyph, ttglyph, ttdata)
        if ttglyph.isComposite():
            self._set_composite_flags(glyph, ttglyph)

    def _compile_tt_glyph_program(self, glyph, ttglyph, ttdata):
        formatVersion = ttdata.get("formatVersion", None)
        if int(formatVersion) != 1:
            raise NotImplementedError(
                f"Unknown formatVersion {formatVersion} "
                "for instructions in glyph '{name}'."
            )
            return

        # Check if glyph hash matches the current outlines
        hash_pen = HashPointPen(glyph.width, self.ufo)
        glyph.drawPoints(hash_pen)
        glyph_id = ttdata.get("id", None)
        if glyph_id is None:
            # The glyph hash is required
            logger.error(
                f"Glyph hash missing, glyph '{glyph.name}' will have "
                "no instructions in font."
            )
            return

        if glyph_id != hash_pen.hash:
            logger.error(
                f"Glyph hash mismatch, glyph '{glyph.name}' will have "
                "no instructions in font."
            )
            return

        # Compile the glyph program
        asm = ttdata.get("assembly", None)
        if asm is None:
            # The "assembly" key is required.
            logger.error(
                f"Glyph assembly missing, glyph '{glyph.name}' will have "
                "no instructions in font."
            )
            return

        ttglyph.program = ttLib.tables.ttProgram.Program()
        ttglyph.program.fromAssembly(asm)

    def _set_composite_flags(self, glyph, ttglyph):
        # Remove empty glyph programs from composite glyphs
        if hasattr(ttglyph, "program") and not ttglyph.program:
            delattr(ttglyph, "program")

        # Set component flags

        # We need to decide when to set the flags.
        # Let's assume if any lib key is not there, or the component
        # doesn't have an identifier, we should leave the flags alone.
        use_my_metrics_comp = None
        if len(ttglyph.components) != len(glyph.components):
            logger.error(
                "Number of components differ between UFO and TTF "
                f"in glyph '{glyph.name}' ({len(glyph.components)} vs. "
                f"{len(ttglyph.components)}, not setting component flags."
            )
            return

        for i, c in enumerate(ttglyph.components):
            ufo_component_id = glyph.components[i].identifier
            if (
                ufo_component_id is not None
                and OBJECT_LIBS_KEY in glyph.lib
                and ufo_component_id in glyph.lib[OBJECT_LIBS_KEY]
                and (
                    TRUETYPE_ROUND_KEY in glyph.lib[OBJECT_LIBS_KEY][ufo_component_id]
                    or TRUETYPE_METRICS_KEY
                    in glyph.lib[OBJECT_LIBS_KEY][ufo_component_id]
                )
            ):
                component_lib = glyph.lib[OBJECT_LIBS_KEY][ufo_component_id]

                # https://github.com/googlefonts/ufo2ft/pull/425 recommends
                # to always set the ROUND_XY_TO_GRID flag, so we only
                # unset it if explicitly done so in the lib
                if component_lib.get(TRUETYPE_ROUND_KEY, True):
                    c.flags |= ROUND_XY_TO_GRID
                else:
                    c.flags &= ~ROUND_XY_TO_GRID

                if component_lib.get(TRUETYPE_METRICS_KEY, False):
                    c.flags &= ~USE_MY_METRICS
                    if use_my_metrics_comp:
                        logger.warning(
                            "Ignoring USE_MY_METRICS flag on component "
                            f"'{ufo_component_id}' because it has been set on "
                            f"component '{use_my_metrics_comp}' already "
                            f"in glyph {glyph.name}."
                        )
                    else:
                        c.flags |= USE_MY_METRICS
                        use_my_metrics_comp = ufo_component_id

            if i == 0 and TRUETYPE_OVERLAP_KEY in glyph.lib:
                # Set OVERLAP_COMPOUND on the first component only
                if glyph.lib.get(TRUETYPE_OVERLAP_KEY, False):
                    c.flags |= OVERLAP_COMPOUND
                else:
                    c.flags &= ~OVERLAP_COMPOUND

    def compile_fpgm(self):
        self._compile_program("fontProgram", "fpgm")

    def compile_glyf(self):
        for name in sorted(self.ufo.keys()):
            self._compile_tt_glyph(name)

    def compile_maxp(self):
        maxp = self.font["maxp"]
        ttdata = self.ufo.lib.get(TRUETYPE_INSTRUCTIONS_KEY, None)
        if ttdata:
            for name in (
                "maxStorage",
                "maxFunctionDefs",
                "maxInstructionDefs",
                "maxStackElements",
                # "maxSizeOfInstructions",  # Is recalculated below
                "maxZones",
                "maxTwilightPoints",
            ):
                value = ttdata.get(name, None)
                if value is not None:
                    setattr(maxp, name, value)

        # Recalculate maxp.maxSizeOfInstructions
        sizes = [
            len(ttglyph.program.getBytecode())
            for ttglyph in self.font["glyf"].glyphs.values()
            if hasattr(ttglyph, "program")
        ]
        maxp.maxSizeOfInstructions = max(sizes, default=0)

    def compile_prep(self):
        self._compile_program("controlValueProgram", "prep")

    def compile(self):
        self.compile_fpgm()
        self.compile_prep()
        self.compile_glyf()
        # maxp depends on the other programs, to it needs to be last
        self.compile_maxp()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals

from fontTools.ttLib.tables._g_a_s_p import \
    GASP_SYMMETRIC_GRIDFIT, GASP_SYMMETRIC_SMOOTHING, GASP_DOGRAY, GASP_GRIDFIT
from math import log2

import logging

logger = logging.getLogger(__name__)

# Calculate the bit numbers for gasp flags
doGray = log2(GASP_DOGRAY)
doGridfit = log2(GASP_GRIDFIT)
symGridfit = log2(GASP_SYMMETRIC_GRIDFIT)
symSmoothing = log2(GASP_SYMMETRIC_SMOOTHING)

hti_flags = """flags {
      X 1     # X axis
      Y 0     # Y axis
      O 1     # Use original outline
      N 0     # Use gridfitted outline
      R 1     # Round distance; or perpendicular to line
      r 0     # Do not round distance; or parallel to line
      M 1     # Set rp0 to point number on the stack
      m 0     # Do not set rp0
      1 1     # Use rp1
      2 0     # Use rp2
      > 1     # Obey minimum distance
      < 0     # Do not obey minimum distance
     Gr 00    # Gray
     Bl 01    # Black
     Wh 10    # White

  # Combined flags
  m<rGr 00000
  m<rBl 00001
  m<rWh 00010
  m<RGr 00100
  m<RBl 00101
  m<RWh 00110
  m>rGr 01000
  m>rBl 01001
  m>rWh 01010
  m>RGr 01100
  m>RBl 01101
  m>RWh 01110
  M<rGr 10000
  M<rBl 10001
  M<rWh 10010
  M<RGr 10100
  M<RBl 10101
  M<RWh 10110
  M>rGr 11000
  M>rBl 11001
  M>rWh 11010
  M>RGr 11100
  M>RBl 11101
  M>RWh 11110
}
"""

class InstructionCompiler(object):
    def __init__(self, ufo, ttf):
        self.ufo = ufo
        self.font = ttf

    def compile_cvt(self):
        pass

    def compile_flags(self):
        return hti_flags

    def compile_fpgm(self):
        pass

    def compile_gasp(self):
        gasp = ""
        if hasattr(self.ufo.info, "openTypeGaspRangeRecords"):
            ufo_gasp = self.ufo.info.openTypeGaspRangeRecords
            if ufo_gasp is not None:
                for r in self.ufo.info.openTypeGaspRangeRecords:
                    bits = r["rangeGaspBehavior"]
                    gasp += "%7i %9s %6s %10s %12s\n" % (
                        r["rangeMaxPPEM"],
                        "doGridfit" if doGridfit in bits else "",
                        "doGray" if doGray in bits else "",
                        "symGridfit" if symGridfit in bits else "",
                        "symSmoothing" if symSmoothing in bits else "",
                    )
        if gasp:
            return "gasp {\n%s}\n" % gasp
        else:
            return ""

    def compile_glyf(self):
        pass

    def compile_head(self):
        pass

    def compile_prep(self):
        pass

    def compile(self):
        flags = self.compile_flags()
        gasp = self.compile_gasp()
        hti = "\n".join([
            flags,
            gasp,
        ]) + "\n"
        return hti
<commit_msg>Implement head<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals

from fontTools.ttLib.tables._g_a_s_p import \
    GASP_SYMMETRIC_GRIDFIT, GASP_SYMMETRIC_SMOOTHING, GASP_DOGRAY, GASP_GRIDFIT
from math import log2

import logging

logger = logging.getLogger(__name__)

# Calculate the bit numbers for gasp flags
doGray = log2(GASP_DOGRAY)
doGridfit = log2(GASP_GRIDFIT)
symGridfit = log2(GASP_SYMMETRIC_GRIDFIT)
symSmoothing = log2(GASP_SYMMETRIC_SMOOTHING)

hti_flags = """flags {
      X 1     # X axis
      Y 0     # Y axis
      O 1     # Use original outline
      N 0     # Use gridfitted outline
      R 1     # Round distance; or perpendicular to line
      r 0     # Do not round distance; or parallel to line
      M 1     # Set rp0 to point number on the stack
      m 0     # Do not set rp0
      1 1     # Use rp1
      2 0     # Use rp2
      > 1     # Obey minimum distance
      < 0     # Do not obey minimum distance
     Gr 00    # Gray
     Bl 01    # Black
     Wh 10    # White

  # Combined flags
  m<rGr 00000
  m<rBl 00001
  m<rWh 00010
  m<RGr 00100
  m<RBl 00101
  m<RWh 00110
  m>rGr 01000
  m>rBl 01001
  m>rWh 01010
  m>RGr 01100
  m>RBl 01101
  m>RWh 01110
  M<rGr 10000
  M<rBl 10001
  M<rWh 10010
  M<RGr 10100
  M<RBl 10101
  M<RWh 10110
  M>rGr 11000
  M>rBl 11001
  M>rWh 11010
  M>RGr 11100
  M>RBl 11101
  M>RWh 11110
}
"""

class InstructionCompiler(object):
    def __init__(self, ufo, ttf):
        self.ufo = ufo
        self.font = ttf

    def compile_cvt(self):
        pass

    def compile_flags(self):
        return hti_flags

    def compile_fpgm(self):
        pass

    def compile_gasp(self):
        gasp = ""
        if hasattr(self.ufo.info, "openTypeGaspRangeRecords"):
            ufo_gasp = self.ufo.info.openTypeGaspRangeRecords
            if ufo_gasp is not None:
                for r in self.ufo.info.openTypeGaspRangeRecords:
                    bits = r["rangeGaspBehavior"]
                    gasp += "%7i %9s %6s %10s %12s\n" % (
                        r["rangeMaxPPEM"],
                        "doGridfit" if doGridfit in bits else "",
                        "doGray" if doGray in bits else "",
                        "symGridfit" if symGridfit in bits else "",
                        "symSmoothing" if symSmoothing in bits else "",
                    )
        if gasp:
            return "gasp {\n%s}\n" % gasp
        else:
            return ""

    def compile_glyf(self):
        pass

    def compile_head(self):
        head = ""

        # Head flags
        if hasattr(self.ufo.info, "openTypeHeadFlags"):
            flags = self.ufo.info.openTypeHeadFlags
            if flags is not None:
                head += "# %4i flags_instructionsMayDependOnPointSize\n" % (
                    1 if 2 in flags else 0
                )
                head += "# %4i flags_forceIntegerPPEM\n" % (
                    1 if 3 in flags else 0
                )
                head += "# %4i flags_instructionsMayAlterAdvanceWidth\n" % (
                    1 if 4 in flags else 0
                )
                head += "# %4i flags_optimizedForClearType\n" % (
                    1 if 13 in flags else 0
                )

        # Lowest rec PPEM
        if hasattr(self.ufo.info, "openTypeHeadLowestRecPPEM"):
            lowestRecPPEM = self.ufo.info.openTypeHeadLowestRecPPEM
            if lowestRecPPEM is not None:
                head += "# %4i lowestRecPPEM\n" % lowestRecPPEM

        if head:
            return "# head {\n%s# }\n" % head
        else:
            return ""

    def compile_prep(self):
        pass

    def compile(self):
        flags = self.compile_flags()
        gasp = self.compile_gasp()
        head = self.compile_head()
        hti = "\n".join([
            flags,
            gasp,
            head,
        ]) + "\n"
        return hti
<|endoftext|>


<commit_before>#!/usr/bin/env python3
import sys

#SEQ homo_sapiens ENSP00000339726 1 248681369 248682304 -1 ENSG00000189181 OR14I1
sp_name2code = dict()
f_species = open('MODtree.species_list.txt', 'r')
for line in f_species:
    tokens = line.strip().split("\t")
    sp_code = tokens[0]
    sp_name = tokens[1]
    sp_name2code[sp_name] = sp_code
f_species.close()

ens_version = 95

# from ftp://ftp.ensembl.org/pub/release-95/emf/ensembl-compara/
filename_fa = 'Compara.%d.protein_default.aa.fasta' % ens_version
filename_nh = 'Compara.%d.protein_default.nh.emf' % ens_version

filename_out_txt = 'MODtree_ens%d_compara.txt' % (ens_version)
filename_out_fa = 'MODtree_ens%d_compara.fa' % (ens_version)

f_out_txt = open(filename_out_txt, 'w')
family_id = 1
prot_info = dict()
tmp_prot_list = []
f_nh = open(filename_nh, 'r')
for line in f_nh:
    line = line.strip()
    if line == '':
        continue

    if line.startswith('SEQ'):
        tokens = line.strip().split()
        sp_name = tokens[1]
        p_id = tokens[2]
        gene_name = tokens[8]
        if sp_name in sp_name2code:
            sp_code = sp_name2code[sp_name]
            prot_info[p_id] = {'name': gene_name, 'sp_name': sp_name, 'sp_code': sp_code}
            tmp_prot_list.append(p_id)

    elif line.startswith('DATA'):
        tmp_tree_str = next(f_nh).strip()
        for tmp_p in tmp_prot_list:
            if tmp_tree_str.find(tmp_p) >= 0:
                # MODTree Family ID
                mtf_id = 'MTF%05d' % family_id

                sp_code = prot_info[tmp_p]['sp_code']
                gene_name = prot_info[tmp_p]['name']
                prot_info[tmp_p]['header'] = '%s_%s %s %s' % (tmp_p, sp_code, mtf_id, gene_name)
                f_out_txt.write("%s\t%s\t%s\t%s\n" % (mtf_id, tmp_p, sp_code, gene_name))
        family_id += 1

    elif line.startswith('//'):
        tmp_prot_list = []

    else:
        sys.stderr.write('Error.\n')
f_nh.close()
f_out_txt.close()

is_print = 0
seq_list = dict()
f_fa = open(filename_fa, 'r')
for line in f_fa:
    if line.startswith('>'):
        seq_h = line.strip().lstrip('>')
        if seq_h in prot_info:
            tmp_h = '%s' % prot_info[seq_h]['header']
            seq_list[tmp_h] = []
            is_print = 1
        else:
            is_print = 0
    elif is_print > 0:
        seq_list[tmp_h].append(line.strip())
f_fa.close()
f_out_fa = open(filename_out_fa, 'w')
for tmp_h in seq_list.keys():
    tmp_seq = ''.join(seq_list[tmp_h]).replace('-','').replace('//','').replace('*','')
    f_out_fa.write('>%s\n%s\n' % (tmp_h, tmp_seq))
f_out_fa.close()

#SEQ scophthalmus_maximus ENSSMAP00000020411 9 21811542 21814458 1 ENSSMAG00000012520 NULL
<commit_msg>Change compara MODtree name for manual curation<commit_after>#!/usr/bin/env python3
import os
import sys

#SEQ homo_sapiens ENSP00000339726 1 248681369 248682304 -1 ENSG00000189181 OR14I1
sp_name2code = dict()
f_species = open('MODtree_ensembl.species_list.txt', 'r')
for line in f_species:
    tokens = line.strip().split("\t")
    sp_code = tokens[0]
    sp_name = tokens[1]
    sp_name2code[sp_name] = sp_code
f_species.close()

ens_version = 95

# from ftp://ftp.ensembl.org/pub/release-95/emf/ensembl-compara/
dir_compara = '/home/taejoon/pub/ens/95.compara/'
filename_fa = 'Compara.%d.protein_default.aa.fasta' % ens_version
filename_fa = os.path.join(dir_compara, filename_fa)
filename_nh = 'Compara.%d.protein_default.nh.emf' % ens_version
filename_nh = os.path.join(dir_compara, filename_nh)

filename_out_txt = 'MODtree_compara_ens%d.raw.txt' % (ens_version)
filename_out_fa = 'MODtree_compara_ens%d.raw.fa' % (ens_version)

f_out_txt = open(filename_out_txt, 'w')
family_id = 1
prot_info = dict()
tmp_prot_list = []
sys.stderr.write('Read %s\n' % filename_nh)
f_nh = open(filename_nh, 'r')
for line in f_nh:
    line = line.strip()
    if line == '':
        continue

    if line.startswith('SEQ'):
        tokens = line.strip().split()
        sp_name = tokens[1]
        p_id = tokens[2]
        gene_name = tokens[8]
        if gene_name == 'NULL':
            gene_name = 'NotAvail'
        if sp_name in sp_name2code:
            sp_code = sp_name2code[sp_name]
            prot_info[p_id] = {'name': gene_name, 'sp_name': sp_name, 'sp_code': sp_code}
            tmp_prot_list.append(p_id)

    elif line.startswith('DATA'):
        tmp_tree_str = next(f_nh).strip()
        for tmp_p in tmp_prot_list:
            if tmp_tree_str.find(tmp_p) >= 0:
                # MODTree Family ID
                mtf_id = 'E%sF%05d' % (ens_version, family_id)

                sp_code = prot_info[tmp_p]['sp_code']
                gene_name = prot_info[tmp_p]['name']
                tmp_h = '%s|%s|%s|%s' % (mtf_id, sp_code, gene_name, tmp_p)
                prot_info[tmp_p]['header'] = tmp_h
                f_out_txt.write("%s\t%s\t%s\t%s\t%s\n" % (tmp_h, mtf_id, tmp_p, sp_code, gene_name))
        family_id += 1

    elif line.startswith('//'):
        tmp_prot_list = []

    else:
        sys.stderr.write('Error.\n')
f_nh.close()
f_out_txt.close()

is_print = 0
seq_list = dict()
sys.stderr.write('Read %s\n' % filename_fa)
f_fa = open(filename_fa, 'r')
for line in f_fa:
    if line.startswith('>'):
        seq_h = line.strip().lstrip('>')
        if seq_h in prot_info:
            tmp_h = '%s' % prot_info[seq_h]['header']
            seq_list[tmp_h] = []
            is_print = 1
        else:
            is_print = 0
    elif is_print > 0:
        seq_list[tmp_h].append(line.strip())
f_fa.close()
f_out_fa = open(filename_out_fa, 'w')
for tmp_h in seq_list.keys():
    tmp_seq = ''.join(seq_list[tmp_h]).replace('-','').replace('//','').replace('*','')
    f_out_fa.write('>%s\n%s\n' % (tmp_h, tmp_seq))
f_out_fa.close()

#SEQ scophthalmus_maximus ENSSMAP00000020411 9 21811542 21814458 1 ENSSMAG00000012520 NULL
<|endoftext|>


<commit_before>from datetime import datetime
from django.test import TestCase

from talks.events.models import (Event, Person, PersonEvent,
                                 ROLES_SPEAKER, EventGroup)
from .models import (event_to_old_talk, group_to_old_series,
                     get_list_id)

class TestOldTalks(TestCase):

    def test_event_to_old_talk(self):
        event = Event()
        event.title = u"TITLE"
        event.start = datetime(2014, 1, 1, 10, 30)
        event.end = datetime(2014, 1, 1, 11, 30)
        event.save()

        s1 = Person()
        s1.name = "A"
        s1.save()

        s2 = Person()
        s2.name = "B"
        s2.save()

        event.save()

        PersonEvent.objects.create(person=s1, event=event, role=ROLES_SPEAKER)
        PersonEvent.objects.create(person=s2, event=event, role=ROLES_SPEAKER)

        data = event_to_old_talk(event, None)
        d = dict(data)

        self.assertEquals(len(data), 7)
        self.assertEquals(d["talk[title]"], event.title)
        self.assertEquals(d["talk[name_of_speaker]"], "A, B")
        self.assertEquals(d["talk[abstract]"], "")
        self.assertEquals(d["talk[date_string]"], "2014/01/01")
        self.assertEquals(d["talk[start_time_string]"], "10:30")
        self.assertEquals(d["talk[end_time_string]"], "11:30")

    def test_event_with_group_to_old_talk(self):
        event = Event()
        event.title = u"TITLE 2"
        event.description = "description"
        event.start = datetime(2014, 1, 1, 11, 00)
        event.end = datetime(2014, 1, 1, 12, 00)
        event.save()

        s = Person()
        s.name = "Personne"
        s.save()

        PersonEvent.objects.create(person=s, event=event, role=ROLES_SPEAKER)

        data = event_to_old_talk(event, "22")
        d = dict(data)

        self.assertEquals(len(data), 8)
        self.assertEquals(d["talk[title]"], event.title)
        self.assertEquals(d["talk[series_id_string]"], "22")
        self.assertEquals(d["talk[abstract]"], event.description+"\n")
        self.assertEquals(d["talk[name_of_speaker]"], s.name)

    def test_group_to_old_series(self):
        group = EventGroup()
        group.title = "Group"
        group.save()

        data = group_to_old_series(group)
        d = dict(data)

        self.assertEquals(len(data), 1)
        self.assertEquals(d["list[name]"], group.title)

    def test_get_list_id(self):
        doc = """<?xml version="1.0" encoding="UTF-8"?>
                    <list>
                      <id>32</id>
                      <name>oOo</name>

                      <list_type></list_type>
                    </list>"""
        ident = get_list_id(doc)
        self.assertEquals(ident, "32")
<commit_msg>Updated tests<commit_after>from datetime import datetime
from django.test import TestCase

from talks.events.models import (Event, Person, PersonEvent,
                                 ROLES_SPEAKER, EventGroup)
from .models import (event_to_old_talk, group_to_old_series,
                     get_list_id)

class TestOldTalks(TestCase):

    def test_event_to_old_talk(self):
        event = Event()
        event.title = u"TITLE"
        event.start = datetime(2014, 1, 1, 10, 30)
        event.end = datetime(2014, 1, 1, 11, 30)
        event.save()

        s1 = Person()
        s1.name = "A"
        s1.save()

        s2 = Person()
        s2.name = "B"
        s2.save()

        event.save()

        PersonEvent.objects.create(person=s1, event=event, role=ROLES_SPEAKER)
        PersonEvent.objects.create(person=s2, event=event, role=ROLES_SPEAKER)

        data = event_to_old_talk(event, None)
        d = dict(data)

        self.assertEquals(len(data), 7)
        self.assertEquals(d["talk[title]"], event.title)
        self.assertEquals(d["talk[name_of_speaker]"], "A, B")
        self.assertEquals(d["talk[abstract]"], "")
        self.assertEquals(d["talk[date_string]"], "2014/01/01")
        self.assertEquals(d["talk[start_time_string]"], "10:30")
        self.assertEquals(d["talk[end_time_string]"], "11:30")

    def test_event_with_group_to_old_talk(self):
        event = Event()
        event.title = u"TITLE 2"
        event.description = "description"
        event.start = datetime(2014, 1, 1, 11, 00)
        event.end = datetime(2014, 1, 1, 12, 00)
        event.save()

        s = Person()
        s.name = "Personne"
        s.save()

        PersonEvent.objects.create(person=s, event=event, role=ROLES_SPEAKER)

        data = event_to_old_talk(event, "22")
        d = dict(data)

        self.assertEquals(len(data), 8)
        self.assertEquals(d["talk[title]"], event.title)
        self.assertEquals(d["talk[series_id_string]"], "22")
        self.assertEquals(d["talk[abstract]"], event.description+"\n")
        self.assertEquals(d["talk[name_of_speaker]"], s.name)
        self.assertEquals(d["talk[date_string]"], "2014/01/01")
        self.assertEquals(d["talk[start_time_string]"], "11:00")
        self.assertEquals(d["talk[end_time_string]"], "12:00")

    def test_group_to_old_series(self):
        group = EventGroup()
        group.title = "Group"
        group.save()

        data = group_to_old_series(group)
        d = dict(data)

        self.assertEquals(len(data), 1)
        self.assertEquals(d["list[name]"], group.title)

    def test_get_list_id(self):
        doc = """<?xml version="1.0" encoding="UTF-8"?>
                    <list>
                      <id>32</id>
                      <name>oOo</name>

                      <list_type></list_type>
                    </list>"""
        ident = get_list_id(doc)
        self.assertEquals(ident, "32")
<|endoftext|>


<commit_before># Problem:  Palindrome Number
# Difficulty: Easy
# Category: Math
# Leetcode 009: https://leetcode.com/problems/palindrome-number/discuss/
# Description:
"""
Determine whether an integer is a palindrome. Do this without extra space
"""

class Solution(object):

	def is_panlindrome(self, x):
		if x < 0 or (x >0 and x % 10 == 0):
			return False
		num = x
		rev = 0

		while x != 0:
			tail = x % 10
			rev = rev * 10 + tail
			x = x // 10

		return num == rev

obj = Solution()
print(obj.is_panlindrome(121))<commit_msg>leetcode<commit_after># Problem:  Palindrome Number
# Difficulty: Easy
# Category: Math
# Leetcode 009: https://leetcode.com/problems/palindrome-number/discuss/
# Description:
"""
Determine whether an integer is a palindrome. Do this without extra space
"""

class Solution(object):

	def is_panlindrome(self, x):
		if x < 0 or (x > 0 and x % 10 == 0):
			return False
		num = x
		rev = 0

		while x != 0:
			tail = x % 10
			rev = rev * 10 + tail
			x //= 10

		return num == rev

obj = Solution()
print(obj.is_panlindrome(121))
<|endoftext|>


<commit_before># -*- coding: utf-8 -
from datetime import datetime, timedelta
from dateutil.parser import parse
from dpath.util import set as xpathset
from iso8601 import parse_date
from json import load
from jsonpath_rw import parse as parse_path
from munch import fromYAML, Munch, munchify
from pytz import timezone
from robot.errors import HandlerExecutionFailed
from robot.libraries.BuiltIn import BuiltIn
from robot.output import LOGGER
from robot.output.loggerhelper import Message
# These imports are not pointless. Robot's resource and testsuite files
# can access them by simply importing library "service_keywords".
# Please ignore the warning given by Flake8 or other linter.
from .initial_data import (
    auction_bid, create_fake_doc,
    test_award_data, test_bid_data, test_complaint_data,
    test_complaint_reply_data, test_question_answer_data,
    test_question_data, test_tender_data, test_tender_data_multiple_lots
)
import os

TZ = timezone(os.environ['TZ'] if 'TZ' in os.environ else 'Europe/Kiev')

def get_now():
    return datetime.now(TZ)

def get_date():
    return get_now().isoformat()

def get_file_contents(path):
    with open(path, 'r') as f:
        return unicode(f.read()) or u''

def change_state(arguments):
    try:
        if arguments[0] == "shouldfail":
            return "shouldfail"
        return "pass"
    except IndexError:
        return "pass"

def prepare_prom_test_tender_data(period_interval, mode):
    t_data = prepare_test_tender_data(period_interval, mode)
    return munchify({'data': t_data})

def compare_date(data1, data2):
    data1 = parse(data1)
    data2 = parse(data2)
    if data1.tzinfo is None:
        data1 = TZ.localize(data1)
    if data2.tzinfo is None:
        data2 = TZ.localize(data2)

    delta = (data1 - data2).total_seconds()
    if abs(delta) > 60:
        return False
    return True

def log_object_data(data, file_name=None, format="yaml"):
    """Log object data in pretty format (JSON or YAML)

    Two output formats are supported: "yaml" and "json".

    If a file name is specified, the output is written into that file.

    If you would like to get similar output everywhere,
    use the following snippet somewhere in your code
    before actually using Munch. For instance,
    put it into your __init__.py, or, if you use zc.buildout,
    specify it in "initialization" setting of zc.recipe.egg.

    from munch import Munch
    Munch.__str__ = lambda self: Munch.toYAML(self, allow_unicode=True,
                                              default_flow_style=False)
    Munch.__repr__ = Munch.__str__
    """
    if not isinstance(data, Munch):
        data = munchify(data)
    if format.lower() == 'json':
        data = data.toJSON(indent=2)
    else:
        data = data.toYAML(allow_unicode=True, default_flow_style=False)
        format = 'yaml'
    LOGGER.log_message(Message(data.decode('utf-8'), "INFO"))
    if file_name:
        output_dir = BuiltIn().get_variable_value("${OUTPUT_DIR}")
        with open(os.path.join(output_dir, file_name + '.' + format), "w") as file_obj:
            file_obj.write(data)

def load_initial_data_from(file_name):
    if not os.path.exists(file_name):
        file_name = os.path.join(os.path.dirname(__file__), 'data/{}'.format(file_name))
    with open(file_name) as file_obj:
        if file_name.endswith(".json"):
            return Munch.fromDict(load(file_obj))
        elif file_name.endswith(".yaml"):
            return fromYAML(file_obj)

def prepare_test_tender_data(period_interval, mode):
    if mode == 'single':
        return munchify({'data': test_tender_data(period_interval=period_interval)})
    elif mode == 'multi':
        return munchify({'data': test_tender_data_multiple_lots(period_interval=period_interval)})
    raise ValueError('A very specific bad thing happened')

def run_keyword_and_ignore_keyword_definitions(name, *args):
    """Runs the given keyword with given arguments and returns the status as a Boolean value.

    This keyword returns `True` if the keyword that is executed succeeds and
    `False` if it fails. This is useful, for example, in combination with
    `Run Keyword If`. If you are interested in the error message or return
    value, use `Run Keyword And Ignore Error` instead.

    The keyword name and arguments work as in `Run Keyword`.

    Example:
    | ${passed} = | `Run Keyword And Return Status` | Keyword | args |
    | `Run Keyword If` | ${passed} | Another keyword |

    New in Robot Framework 2.7.6.
    """
    try:
        status, _ = BuiltIn().run_keyword_and_ignore_error(name, *args)
    except HandlerExecutionFailed:
        LOGGER.log_message(Message("Keyword {} not implemented", "ERROR"))
        return "FAIL", ""
    return status, _

def set_tender_periods(tender):
    now = get_now()
    tender.data.enquiryPeriod.endDate = (now + timedelta(minutes=2)).isoformat()
    tender.data.tenderPeriod.startDate = (now + timedelta(minutes=2)).isoformat()
    tender.data.tenderPeriod.endDate = (now + timedelta(minutes=4)).isoformat()
    return tender

def set_access_key(tender, access_token):
    tender.access = munchify({"token": access_token})
    return tender

def set_to_object(obj, attribute, value):
    xpathset(obj, attribute.replace('.', '/'), value)
    return obj

def get_from_object(obj, attribute):
    """Gets data from a dictionary using a dotted accessor-string"""
    jsonpath_expr = parse_path(attribute)
    return_list = [i.value for i in jsonpath_expr.find(obj)]
    if return_list:
        return return_list[0]
    return None

def wait_to_date(date_stamp):
    date = parse(date_stamp)
    LOGGER.log_message(Message("date: {}".format(date.isoformat()), "INFO"))
    now = get_now()
    LOGGER.log_message(Message("now: {}".format(now.isoformat()), "INFO"))
    wait_seconds = (date - now).total_seconds()
    wait_seconds += 2
    if wait_seconds < 0:
        return 0
    return wait_seconds

# GUI Frontends common
def add_data_for_gui_frontends(tender_data):
    now = datetime.now()
    # tender_data.data.enquiryPeriod['startDate'] = (now + timedelta(minutes=2)).isoformat()
    tender_data.data.enquiryPeriod['endDate'] = (now + timedelta(minutes=6)).isoformat()
    tender_data.data.tenderPeriod['startDate'] = (now + timedelta(minutes=7)).isoformat()
    tender_data.data.tenderPeriod['endDate'] = (now + timedelta(minutes=11)).isoformat()
    return tender_data

def convert_date_to_slash_format(isodate):
    iso_dt = parse_date(isodate)
    date_string = iso_dt.strftime("%d/%m/%Y")
    return date_string

def convert_datetime_to_dot_format(isodate):
    iso_dt = parse_date(isodate)
    day_string = iso_dt.strftime("%d.%m.%Y %H:%M")
    return day_string

def local_path_to_file(file_name):
    return os.path.join(os.path.dirname(__file__), 'documents', file_name)
<commit_msg>Change error description to more sensible<commit_after># -*- coding: utf-8 -
from datetime import datetime, timedelta
from dateutil.parser import parse
from dpath.util import set as xpathset
from iso8601 import parse_date
from json import load
from jsonpath_rw import parse as parse_path
from munch import fromYAML, Munch, munchify
from pytz import timezone
from robot.errors import HandlerExecutionFailed
from robot.libraries.BuiltIn import BuiltIn
from robot.output import LOGGER
from robot.output.loggerhelper import Message
# These imports are not pointless. Robot's resource and testsuite files
# can access them by simply importing library "service_keywords".
# Please ignore the warning given by Flake8 or other linter.
from .initial_data import (
    auction_bid, create_fake_doc,
    test_award_data, test_bid_data, test_complaint_data,
    test_complaint_reply_data, test_question_answer_data,
    test_question_data, test_tender_data, test_tender_data_multiple_lots
)
import os

TZ = timezone(os.environ['TZ'] if 'TZ' in os.environ else 'Europe/Kiev')

def get_now():
    return datetime.now(TZ)

def get_date():
    return get_now().isoformat()

def get_file_contents(path):
    with open(path, 'r') as f:
        return unicode(f.read()) or u''

def change_state(arguments):
    try:
        if arguments[0] == "shouldfail":
            return "shouldfail"
        return "pass"
    except IndexError:
        return "pass"

def prepare_prom_test_tender_data(period_interval, mode):
    t_data = prepare_test_tender_data(period_interval, mode)
    return munchify({'data': t_data})

def compare_date(data1, data2):
    data1 = parse(data1)
    data2 = parse(data2)
    if data1.tzinfo is None:
        data1 = TZ.localize(data1)
    if data2.tzinfo is None:
        data2 = TZ.localize(data2)

    delta = (data1 - data2).total_seconds()
    if abs(delta) > 60:
        return False
    return True

def log_object_data(data, file_name=None, format="yaml"):
    """Log object data in pretty format (JSON or YAML)

    Two output formats are supported: "yaml" and "json".

    If a file name is specified, the output is written into that file.

    If you would like to get similar output everywhere,
    use the following snippet somewhere in your code
    before actually using Munch. For instance,
    put it into your __init__.py, or, if you use zc.buildout,
    specify it in "initialization" setting of zc.recipe.egg.

    from munch import Munch
    Munch.__str__ = lambda self: Munch.toYAML(self, allow_unicode=True,
                                              default_flow_style=False)
    Munch.__repr__ = Munch.__str__
    """
    if not isinstance(data, Munch):
        data = munchify(data)
    if format.lower() == 'json':
        data = data.toJSON(indent=2)
    else:
        data = data.toYAML(allow_unicode=True, default_flow_style=False)
        format = 'yaml'
    LOGGER.log_message(Message(data.decode('utf-8'), "INFO"))
    if file_name:
        output_dir = BuiltIn().get_variable_value("${OUTPUT_DIR}")
        with open(os.path.join(output_dir, file_name + '.' + format), "w") as file_obj:
            file_obj.write(data)

def load_initial_data_from(file_name):
    if not os.path.exists(file_name):
        file_name = os.path.join(os.path.dirname(__file__), 'data/{}'.format(file_name))
    with open(file_name) as file_obj:
        if file_name.endswith(".json"):
            return Munch.fromDict(load(file_obj))
        elif file_name.endswith(".yaml"):
            return fromYAML(file_obj)

def prepare_test_tender_data(period_interval, mode):
    if mode == 'single':
        return munchify({'data': test_tender_data(period_interval=period_interval)})
    elif mode == 'multi':
        return munchify({'data': test_tender_data_multiple_lots(period_interval=period_interval)})
    raise ValueError('Invalid mode for test_tender_data')

def run_keyword_and_ignore_keyword_definitions(name, *args):
    """Runs the given keyword with given arguments and returns the status as a Boolean value.

    This keyword returns `True` if the keyword that is executed succeeds and
    `False` if it fails. This is useful, for example, in combination with
    `Run Keyword If`. If you are interested in the error message or return
    value, use `Run Keyword And Ignore Error` instead.

    The keyword name and arguments work as in `Run Keyword`.

    Example:
    | ${passed} = | `Run Keyword And Return Status` | Keyword | args |
    | `Run Keyword If` | ${passed} | Another keyword |

    New in Robot Framework 2.7.6.
    """
    try:
        status, _ = BuiltIn().run_keyword_and_ignore_error(name, *args)
    except HandlerExecutionFailed:
        LOGGER.log_message(Message("Keyword {} not implemented", "ERROR"))
        return "FAIL", ""
    return status, _

def set_tender_periods(tender):
    now = get_now()
    tender.data.enquiryPeriod.endDate = (now + timedelta(minutes=2)).isoformat()
    tender.data.tenderPeriod.startDate = (now + timedelta(minutes=2)).isoformat()
    tender.data.tenderPeriod.endDate = (now + timedelta(minutes=4)).isoformat()
    return tender

def set_access_key(tender, access_token):
    tender.access = munchify({"token": access_token})
    return tender

def set_to_object(obj, attribute, value):
    xpathset(obj, attribute.replace('.', '/'), value)
    return obj

def get_from_object(obj, attribute):
    """Gets data from a dictionary using a dotted accessor-string"""
    jsonpath_expr = parse_path(attribute)
    return_list = [i.value for i in jsonpath_expr.find(obj)]
    if return_list:
        return return_list[0]
    return None

def wait_to_date(date_stamp):
    date = parse(date_stamp)
    LOGGER.log_message(Message("date: {}".format(date.isoformat()), "INFO"))
    now = get_now()
    LOGGER.log_message(Message("now: {}".format(now.isoformat()), "INFO"))
    wait_seconds = (date - now).total_seconds()
    wait_seconds += 2
    if wait_seconds < 0:
        return 0
    return wait_seconds

# GUI Frontends common
def add_data_for_gui_frontends(tender_data):
    now = datetime.now()
    # tender_data.data.enquiryPeriod['startDate'] = (now + timedelta(minutes=2)).isoformat()
    tender_data.data.enquiryPeriod['endDate'] = (now + timedelta(minutes=6)).isoformat()
    tender_data.data.tenderPeriod['startDate'] = (now + timedelta(minutes=7)).isoformat()
    tender_data.data.tenderPeriod['endDate'] = (now + timedelta(minutes=11)).isoformat()
    return tender_data

def convert_date_to_slash_format(isodate):
    iso_dt = parse_date(isodate)
    date_string = iso_dt.strftime("%d/%m/%Y")
    return date_string

def convert_datetime_to_dot_format(isodate):
    iso_dt = parse_date(isodate)
    day_string = iso_dt.strftime("%d.%m.%Y %H:%M")
    return day_string

def local_path_to_file(file_name):
    return os.path.join(os.path.dirname(__file__), 'documents', file_name)
<|endoftext|>


<commit_before># -*- coding:utf8 -*-
# File   : node.py
# Author : Jiayuan Mao
# Email  : maojiayuan@gmail.com
# Date   : 12/31/16
#
# This file is part of TensorArtist

from ...core.utils.meta import assert_instance, assert_notnone

import numpy as np
import tensorflow as tf

__all__ = [
    '__valid_tensor_types__', '__valid_tf_tensor_types__',
    'VarNode', 'as_varnode', 'as_tftensor'
]

class VarNodeStore(object):
    def __init__(self):
        self.__kv = {}

    def get(self, tensor):
        if tensor in self.__kv:
            return self.__kv[tensor]
        v = self._make(tensor)
        self.__kv[tensor] = v
        return v

    def _make(self, tensor):
        return VarNode(tensor)

varnode_store = VarNodeStore()

class VarNodeOpDecl(object):
    def __binary(self, rhs, op_name):
        rhs = as_varnode(rhs)
        return as_varnode(getattr(tf, op_name)(self, rhs))

    def __rbinary(self, lhs, op_name):
        lhs = as_varnode(lhs)
        return as_varnode(getattr(tf, op_name)(self, lhs))

    def __add__(self, rhs):
        return self.__binary(rhs, 'add')
    def __radd__(self, lhs):
        return self.__rbinary(lhs, 'add')

    def __sub__(self, rhs):
        return self.__binary(rhs, 'subtract')
    def __rsub__(self, lhs):
        return self.__rbinary(lhs, 'subtract')

    def __mul__(self, rhs):
        return self.__binary(rhs, 'multiply')
    def __rmul__(self, lhs):
        return self.__rbinary(lhs, 'multiply')

    def __matmul__(self, rhs):
        return self.__binary(rhs, 'matmul')
    def __rmatmul__(self, lhs):
        return self.__rbinary(lhs, 'matmul')

    def __truediv__(self, rhs):
        return self.__binary(rhs, 'truediv')
    def __rtruediv__(self, lhs):
        return self.__rbinary(lhs, 'truediv')

    def __floordiv__(self, rhs):
        return self.__binary(rhs, 'floordiv')
    def __rfloordiv__(self, lhs):
        return self.__rbinary(lhs, 'floordiv')

    def __mod__(self, rhs):
        return self.__binary(rhs, 'mod')
    def __rmod__(self, lhs):
        return self.__rbinary(lhs, 'mod')

    def __pow__(self, rhs):
        return self.__binary(rhs, 'pow')
    def __rpow__(self, lhs):
        return self.__rbinary(lhs, 'pow')

    def __lt__(self, rhs):
        return self.__binary(rhs, 'less')

    def __le__(self, rhs):
        return self.__binary(rhs, 'less_equal')

    def __gt__(self, rhs):
        return self.__binary(rhs, 'greater')

    def __ge__(self, rhs):
        return self.__binary(rhs, 'greater_equal')

    # do not define __eq__ because it would make the node unhashable
    def eq(self, rhs):
        return self.__binary(rhs, 'equal')

    def neq(self, rhs):
        return self.__binary(rhs, 'not_equal')

    def __neg__(self):
        return as_varnode(tf.neg(self))

    def __getitem__(self, slices):
        return as_varnode(as_tftensor(self)[slices])

    @property
    def set_sub(self):
        raise NotImplementedError()

    @property
    def ai(self):
        raise NotImplementedError()

    @property
    def set_ai(self):
        raise NotImplementedError()

    def __iter__(self):
        raise ValueError('iterating over {} is not allowed'.format(type(self).__name__))

    def astype(self, dtype):
        return as_varnode(tf.cast(self, dtype))

    def reshape(self, *tshape, name=None):
        if len(tshape) == 1:
            tshape, = tshape
        from ..opr.shape import reshape
        return as_varnode(reshape(self, tshape=tshape, name=name))

    def broadcast(self, *tshape, name=None):
        if len(tshape) == 1:
            tshape, = tshape
        from ..opr.shape import broadcast
        return as_varnode(broadcast(self, tshape=tshape, name=name))

    def dimshuffle(self, *pattern, name=None):
        from ..opr.shape import dimshuffle
        return as_varnode(Dimshuffle(src=self, pattern=pattern, name=name))

    def add_axis(self, axis):
        from ..opr.shape import add_axis
        return as_varnode(add_axis(axis=axis))

    def remove_axis(self, axis):
        from ..opr.shape import remove_axis
        return as_varnode(remove_axis(axis=axis))

    def sum(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_sum(self, axis=axis, keep_dims=keepdims, name=name))

    def mean(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_mean(self, axis=axis, keep_dims=keepdims, name=name))

    def max(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_max(self, axis=axis, keep_dims=keepdims, name=name))

    def min(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_min(self, axis=axis, keep_dims=keepdims, name=name))

    def argmax(self, axis=None, name=None):
        return as_varnode(tf.argmax(self, axis=axis, name=name))

    def argmin(self, axis=None, name=None):
        return as_varnode(tf.argmin(self, axis=axis, name=name))

    def prod(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_prod(self, axis=axis, keep_dims=keepdims, name=name))

    def flatten(self):
        from ..opr.shape import flatten
        return as_varnode(flatten(self))

    def flatten2(self):
        from ..opr.shape import flatten2
        return as_varnode(flatten2(self))

    def eval(self, session=None, **feed_dict):
        return as_tftensor.eval(feed_dict=feed_dict, session=session)

class VarNode(VarNodeOpDecl):
    class Flags:
        def __init__(self, **kwargs):
            for k, v in kwargs.items():
                setattr(self, k, v)

        data_parallel_reduce_method = 'CONCAT'

    def __init__(self, impl, **flags):
        self.__impl = impl
        self.__taop = None
        self.__flags = type(self).Flags(**flags)
        assert_instance(impl, __valid_tf_tensor_types__)

    @property
    def impl(self):
        return self.__impl

    @property
    def flags(self):
        return self.__flags

    @property
    def static_shape(self):
        ss = self.__impl.get_shape()
        if ss.ndims is None:
            return None
        return tuple(ss.as_list())

    @property
    def ndims(self):
        return self.__impl.get_shape().ndims

    @property
    def shape(self):
        return tf.shape(self.__impl)

    @property
    def dtype(self):
        return self.__impl.dtype

    @property
    def name(self):
        return self.__impl.name

    @property
    def op(self):
        return self.__impl.op

    @property
    def taop(self):
        return self.__taop

    def set_taop(self, op):
        self.__taop = op
        assert_instance(op, OprNode)

class OprNode(object):
    def __init__(self, name):
        self.__name = tf.get_default_graph().unique_name(name, mark_as_used=False)

    @property
    def name(self):
        return self.__name

__valid_tensor_types__ = (VarNode, tf.Tensor, tf.Variable, tf.Operation)
__valid_tf_tensor_types__ = (tf.Tensor, tf.Variable, tf.Operation)

def as_varnode(tensor):
    if isinstance(tensor, VarNode):
        return tensor

    if isinstance(tensor, (np.ndarray, int, float, tuple, list)):
        from ..opr.netsrc import constant
        return constant(tensor)

    return varnode_store.get(tensor)

def as_tftensor(tensor):
    if isinstance(tensor, __valid_tf_tensor_types__):
        return tensor
    assert_instance(tensor, VarNode)
    return tensor.impl

<commit_msg>[master] AttrObject<commit_after># -*- coding:utf8 -*-
# File   : node.py
# Author : Jiayuan Mao
# Email  : maojiayuan@gmail.com
# Date   : 12/31/16
#
# This file is part of TensorArtist

from ...core.utils.meta import assert_instance, assert_notnone, AttrObject

import numpy as np
import tensorflow as tf

__all__ = [
    '__valid_tensor_types__', '__valid_tf_tensor_types__',
    'VarNode', 'as_varnode', 'as_tftensor'
]

class VarNodeStore(object):
    def __init__(self):
        self.__kv = {}

    def get(self, tensor):
        if tensor in self.__kv:
            return self.__kv[tensor]
        v = self._make(tensor)
        self.__kv[tensor] = v
        return v

    def _make(self, tensor):
        return VarNode(tensor)

varnode_store = VarNodeStore()

class VarNodeOpDecl(object):
    def __binary(self, rhs, op_name):
        rhs = as_varnode(rhs)
        return as_varnode(getattr(tf, op_name)(self, rhs))

    def __rbinary(self, lhs, op_name):
        lhs = as_varnode(lhs)
        return as_varnode(getattr(tf, op_name)(self, lhs))

    def __add__(self, rhs):
        return self.__binary(rhs, 'add')
    def __radd__(self, lhs):
        return self.__rbinary(lhs, 'add')

    def __sub__(self, rhs):
        return self.__binary(rhs, 'subtract')
    def __rsub__(self, lhs):
        return self.__rbinary(lhs, 'subtract')

    def __mul__(self, rhs):
        return self.__binary(rhs, 'multiply')
    def __rmul__(self, lhs):
        return self.__rbinary(lhs, 'multiply')

    def __matmul__(self, rhs):
        return self.__binary(rhs, 'matmul')
    def __rmatmul__(self, lhs):
        return self.__rbinary(lhs, 'matmul')

    def __truediv__(self, rhs):
        return self.__binary(rhs, 'truediv')
    def __rtruediv__(self, lhs):
        return self.__rbinary(lhs, 'truediv')

    def __floordiv__(self, rhs):
        return self.__binary(rhs, 'floordiv')
    def __rfloordiv__(self, lhs):
        return self.__rbinary(lhs, 'floordiv')

    def __mod__(self, rhs):
        return self.__binary(rhs, 'mod')
    def __rmod__(self, lhs):
        return self.__rbinary(lhs, 'mod')

    def __pow__(self, rhs):
        return self.__binary(rhs, 'pow')
    def __rpow__(self, lhs):
        return self.__rbinary(lhs, 'pow')

    def __lt__(self, rhs):
        return self.__binary(rhs, 'less')

    def __le__(self, rhs):
        return self.__binary(rhs, 'less_equal')

    def __gt__(self, rhs):
        return self.__binary(rhs, 'greater')

    def __ge__(self, rhs):
        return self.__binary(rhs, 'greater_equal')

    # do not define __eq__ because it would make the node unhashable
    def eq(self, rhs):
        return self.__binary(rhs, 'equal')

    def neq(self, rhs):
        return self.__binary(rhs, 'not_equal')

    def __neg__(self):
        return as_varnode(tf.neg(self))

    def __getitem__(self, slices):
        return as_varnode(as_tftensor(self)[slices])

    @property
    def set_sub(self):
        raise NotImplementedError()

    @property
    def ai(self):
        raise NotImplementedError()

    @property
    def set_ai(self):
        raise NotImplementedError()

    def __iter__(self):
        raise ValueError('iterating over {} is not allowed'.format(type(self).__name__))

    def astype(self, dtype):
        return as_varnode(tf.cast(self, dtype))

    def reshape(self, *tshape, name=None):
        if len(tshape) == 1:
            tshape, = tshape
        from ..opr.shape import reshape
        return as_varnode(reshape(self, tshape=tshape, name=name))

    def broadcast(self, *tshape, name=None):
        if len(tshape) == 1:
            tshape, = tshape
        from ..opr.shape import broadcast
        return as_varnode(broadcast(self, tshape=tshape, name=name))

    def dimshuffle(self, *pattern, name=None):
        from ..opr.shape import dimshuffle
        return as_varnode(Dimshuffle(src=self, pattern=pattern, name=name))

    def add_axis(self, axis):
        from ..opr.shape import add_axis
        return as_varnode(add_axis(axis=axis))

    def remove_axis(self, axis):
        from ..opr.shape import remove_axis
        return as_varnode(remove_axis(axis=axis))

    def sum(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_sum(self, axis=axis, keep_dims=keepdims, name=name))

    def mean(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_mean(self, axis=axis, keep_dims=keepdims, name=name))

    def max(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_max(self, axis=axis, keep_dims=keepdims, name=name))

    def min(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_min(self, axis=axis, keep_dims=keepdims, name=name))

    def argmax(self, axis=None, name=None):
        return as_varnode(tf.argmax(self, axis=axis, name=name))

    def argmin(self, axis=None, name=None):
        return as_varnode(tf.argmin(self, axis=axis, name=name))

    def prod(self, axis=None, keepdims=False, name=None):
        return as_varnode(tf.reduce_prod(self, axis=axis, keep_dims=keepdims, name=name))

    def flatten(self):
        from ..opr.shape import flatten
        return as_varnode(flatten(self))

    def flatten2(self):
        from ..opr.shape import flatten2
        return as_varnode(flatten2(self))

    def eval(self, session=None, **feed_dict):
        return as_tftensor.eval(feed_dict=feed_dict, session=session)

class VarNode(VarNodeOpDecl):
    class Flags(AttrObject):
        data_parallel_reduce_method = 'CONCAT'

    def __init__(self, impl, **flags):
        self.__impl = impl
        self.__taop = None
        self.__flags = type(self).Flags(**flags)
        assert_instance(impl, __valid_tf_tensor_types__)

    @property
    def impl(self):
        return self.__impl

    @property
    def flags(self):
        return self.__flags

    @property
    def static_shape(self):
        ss = self.__impl.get_shape()
        if ss.ndims is None:
            return None
        return tuple(ss.as_list())

    @property
    def ndims(self):
        return self.__impl.get_shape().ndims

    @property
    def shape(self):
        return tf.shape(self.__impl)

    @property
    def dtype(self):
        return self.__impl.dtype

    @property
    def name(self):
        return self.__impl.name

    @property
    def op(self):
        return self.__impl.op

    @property
    def taop(self):
        return self.__taop

    def set_taop(self, op):
        self.__taop = op
        assert_instance(op, OprNode)

class OprNode(object):
    def __init__(self, name):
        self.__name = tf.get_default_graph().unique_name(name, mark_as_used=False)

    @property
    def name(self):
        return self.__name

__valid_tensor_types__ = (VarNode, tf.Tensor, tf.Variable, tf.Operation)
__valid_tf_tensor_types__ = (tf.Tensor, tf.Variable, tf.Operation)

def as_varnode(tensor):
    if isinstance(tensor, VarNode):
        return tensor

    if isinstance(tensor, (np.ndarray, int, float, tuple, list)):
        from ..opr.netsrc import constant
        return constant(tensor)

    return varnode_store.get(tensor)

def as_tftensor(tensor):
    if isinstance(tensor, __valid_tf_tensor_types__):
        return tensor
    assert_instance(tensor, VarNode)
    return tensor.impl

<|endoftext|>


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

from openfisca_france_data.input_data_builders import get_input_data_frame, get_erfs_fpr_input_data_frame
from openfisca_france_data.surveys import SurveyScenario

def test_erfs_fpr_survey_simulation(year = 2012):
    input_data_frame = get_erfs_fpr_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    simulation = survey_scenario.new_simulation(trace = True)
    data_frame_by_entity_key_plural = survey_scenario.create_data_frame_by_entity_key_plural(
        variables = [
            'aspa',
            'aide_logement_montant_brut',
            'idmen',
            'quimen',
            'idfoy',
            'quifoy',
            'idfam',
            'quifam',
            'age',
            'activite',
            'champm_individus',
            'pensions_alimentaires_percues',
            'salaire_imposable',
            'salaire_net',
            'autonomie_financiere',
            'af_nbenf',
            'af',
            'rsa_base_ressources',
            'rsa',
            'weight_familles',
            'revdisp',
            ]
        )
    print data_frame_by_entity_key_plural.keys()
    print data_frame_by_entity_key_plural['familles']
    print data_frame_by_entity_key_plural['individus']

    print (
        data_frame_by_entity_key_plural['familles'].weight_familles * data_frame_by_entity_key_plural['familles'].af
        ).sum()

    return data_frame_by_entity_key_plural, simulation

def test_survey_simulation():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    simulation = survey_scenario.new_simulation(trace = True)
    data_frame_by_entity_key_plural = survey_scenario.create_data_frame_by_entity_key_plural(
        variables = [
            'aspa',
            'aide_logement_montant_brut',
            'idmen',
            'quimen',
            'idfoy',
            'quifoy',
            'idfam',
            'quifam',
            'age',
            'activite',
            'champm_individus',
            'pensions_alimentaires_percues',
            'salaire_imposable',
            'salaire_net',
            'autonomie_financiere',
            'af_nbenf',
            'af',
            'rsa_base_ressources',
            'rsa',
            'weight_familles',
            'revdisp',
            ]
        )
    assert (
        data_frame_by_entity_key_plural['familles'].weight_familles * data_frame_by_entity_key_plural['familles'].af
        ).sum() / 1e9 > 10

    return data_frame_by_entity_key_plural, simulation

def test_weights_building():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    survey_scenario.new_simulation()
    return survey_scenario.simulation

if __name__ == '__main__':
    import logging
    import time
    log = logging.getLogger(__name__)
    import sys
    logging.basicConfig(level = logging.INFO, stream = sys.stdout)

    start = time.time()
    # year = 2009
    # input_data_frame = get_input_data_frame(year)
    data_frame_by_entity_key_plural, simulation = test_erfs_fpr_survey_simulation(year = 2012)
    data_frame_individus = data_frame_by_entity_key_plural['individus']
    data_frame_menages = data_frame_by_entity_key_plural['menages']
    data_frame_familles = data_frame_by_entity_key_plural['familles']
    # ra_rsa = simulation.calculate('ra_rsa', "2009-01")
    # salaire_net = simulation.calculate('salaire_net', "2009-01")
    print time.time() - start
<commit_msg>Clean whitespace<commit_after># -*- coding: utf-8 -*-

from openfisca_france_data.input_data_builders import get_input_data_frame, get_erfs_fpr_input_data_frame
from openfisca_france_data.surveys import SurveyScenario

def test_erfs_fpr_survey_simulation(year = 2012):
    input_data_frame = get_erfs_fpr_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    simulation = survey_scenario.new_simulation(trace = True)
    data_frame_by_entity_key_plural = survey_scenario.create_data_frame_by_entity_key_plural(
        variables = [
            'aspa',
            'aide_logement_montant_brut',
            'idmen',
            'quimen',
            'idfoy',
            'quifoy',
            'idfam',
            'quifam',
            'age',
            'activite',
            'champm_individus',
            'pensions_alimentaires_percues',
            'salaire_imposable',
            'salaire_net',
            'autonomie_financiere',
            'af_nbenf',
            'af',
            'rsa_base_ressources',
            'rsa',
            'weight_familles',
            'revdisp',
            ]
        )
    print data_frame_by_entity_key_plural.keys()
    print data_frame_by_entity_key_plural['familles']
    print data_frame_by_entity_key_plural['individus']

    print(
        data_frame_by_entity_key_plural['familles'].weight_familles * data_frame_by_entity_key_plural['familles'].af
        ).sum()

    return data_frame_by_entity_key_plural, simulation

def test_survey_simulation():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    simulation = survey_scenario.new_simulation(trace = True)
    data_frame_by_entity_key_plural = survey_scenario.create_data_frame_by_entity_key_plural(
        variables = [
            'aspa',
            'aide_logement_montant_brut',
            'idmen',
            'quimen',
            'idfoy',
            'quifoy',
            'idfam',
            'quifam',
            'age',
            'activite',
            'champm_individus',
            'pensions_alimentaires_percues',
            'salaire_imposable',
            'salaire_net',
            'autonomie_financiere',
            'af_nbenf',
            'af',
            'rsa_base_ressources',
            'rsa',
            'weight_familles',
            'revdisp',
            ]
        )
    assert (
        data_frame_by_entity_key_plural['familles'].weight_familles * data_frame_by_entity_key_plural['familles'].af
        ).sum() / 1e9 > 10

    return data_frame_by_entity_key_plural, simulation

def test_weights_building():
    year = 2009
    input_data_frame = get_input_data_frame(year)
    survey_scenario = SurveyScenario().init_from_data_frame(
        input_data_frame = input_data_frame,
        year = year,
        )
    survey_scenario.new_simulation()
    return survey_scenario.simulation

if __name__ == '__main__':
    import logging
    import time
    log = logging.getLogger(__name__)
    import sys
    logging.basicConfig(level = logging.INFO, stream = sys.stdout)

    start = time.time()
    # year = 2009
    # input_data_frame = get_input_data_frame(year)
    data_frame_by_entity_key_plural, simulation = test_erfs_fpr_survey_simulation(year = 2012)
    data_frame_individus = data_frame_by_entity_key_plural['individus']
    data_frame_menages = data_frame_by_entity_key_plural['menages']
    data_frame_familles = data_frame_by_entity_key_plural['familles']
    # ra_rsa = simulation.calculate('ra_rsa', "2009-01")
    # salaire_net = simulation.calculate('salaire_net', "2009-01")
    print time.time() - start
<|endoftext|>


<commit_before>from django.template import Library, Context
from django.template.loader import get_template
from django.utils.translation import ugettext_lazy as _

from openinghours.models import WEEKDAYS, OpeningHours
from openinghours import utils

register = Library()

@register.filter(expects_localtime=True)
def iso_day_to_weekday(d):
    """
    Returns the weekday's name given a ISO weekday number;
    "today" if today is the same weekday.
    """
    if int(d) == utils.get_now().isoweekday():
        return _("today")
    for w in WEEKDAYS:
        if w[0] == int(d):
            return w[1]

@register.filter(expects_localtime=True)
def to_weekday(date_obj_tpl):
    oh, date_obj = date_obj_tpl
    now = utils.get_now()
    day = date_obj.isoweekday()
    if day == now.isoweekday() and (date_obj - now).days == 0:
        return _("today")
    for w in WEEKDAYS:
        if w[0] == int(day):
            return w[1]

@register.assignment_tag
def is_open(location=None, attr=None):
    """
    Returns False if the location is closed, or the OpeningHours object
    to show the location is currently open.
    """
    obj = utils.is_open(location)
    if obj is False:
        return False
    if attr is not None:
        return getattr(obj, attr)
    return obj

@register.assignment_tag
def next_time_open(location):
    """
    Returns the next possible OpeningHours object, or False
    if the location is currently open or if there is no such object.
    """
    obj, ts = utils.next_time_open(location)
    return obj

@register.filter(expects_localtime=True)
def has_closing_rule_for_now(location, attr=None):
    obj = utils.has_closing_rule_for_now(location)
    if obj is False:
        return False
    if attr is not None:
        return getattr(obj, attr)
    return obj

@register.filter(expects_localtime=True)
def get_closing_rule_for_now(location, attr=None):
    obj = utils.get_closing_rule_for_now(location)
    if obj is False:
        return False
    if attr is not None:
        return getattr(obj[0], attr)
    return obj

@register.simple_tag
def opening_hours(location=None, concise=False):
    """
    Creates a rendered listing of hours.
    """
    template_name = 'openinghours/opening_hours_list.html'
    days = []  # [{'hours': '9:00am to 5:00pm', 'name': u'Monday'}, {'hours...

    # Without `location`, choose the first company.
    if location:
        ohrs = OpeningHours.objects.filter(company=location)
    else:
        try:
            Location = utils.get_premises_model()
            ohrs = Location.objects.first().openinghours_set.all()
        except AttributeError:
            raise Exception("You must define some opening hours"
                            " to use the opening hours tags.")

    ohrs.order_by('weekday', 'from_hour')

    for o in ohrs:
        days.append({
            'day_number': o.weekday,
            'name': o.get_weekday_display(),
            'from_hour': o.from_hour,
            'to_hour': o.to_hour,
            'hours': '%s%s to %s%s' % (
                o.from_hour.strftime('%I:%M').lstrip('0'),
                o.from_hour.strftime('%p').lower(),
                o.to_hour.strftime('%I:%M').lstrip('0'),
                o.to_hour.strftime('%p').lower()
            )
        })

    open_days = [o.weekday for o in ohrs]
    for day_number, day_name in WEEKDAYS:
        if day_number not in open_days:
            days.append({
                'day_number': day_number,
                'name': day_name,
                'hours': 'Closed'
            })
    days = sorted(days, key=lambda k: k['day_number'])

    if concise:
        # [{'hours': '9:00am to 5:00pm', 'day_names': u'Monday to Friday'},
        #  {'hours':...
        template_name = 'openinghours/opening_hours_list_concise.html'
        concise_days = []
        current_set = {}
        for day in days:
            if 'hours' not in current_set.keys():
                current_set = {'day_names': [day['name']],
                               'hours': day['hours']}
            elif day['hours'] != current_set['hours']:
                concise_days.append(current_set)
                current_set = {'day_names': [day['name']],
                               'hours': day['hours']}
            else:
                current_set['day_names'].append(day['name'])
        concise_days.append(current_set)

        for day_set in concise_days:
            if len(day_set['day_names']) > 2:
                day_set['day_names'] = '%s to %s' % (day_set['day_names'][0],
                                                     day_set['day_names'][-1])
            elif len(day_set['day_names']) > 1:
                day_set['day_names'] = '%s and %s' % (day_set['day_names'][0],
                                                      day_set['day_names'][-1])
            else:
                day_set['day_names'] = '%s' % day_set['day_names'][0]

        days = concise_days

    template = get_template(template_name)
    return template.render(Context({'days': days}))
<commit_msg>Fix TypeError in template tags.<commit_after>from django.template import Library
from django.template.loader import get_template
from django.utils.translation import ugettext_lazy as _

from openinghours.models import WEEKDAYS, OpeningHours
from openinghours import utils

register = Library()

@register.filter(expects_localtime=True)
def iso_day_to_weekday(d):
    """
    Returns the weekday's name given a ISO weekday number;
    "today" if today is the same weekday.
    """
    if int(d) == utils.get_now().isoweekday():
        return _("today")
    for w in WEEKDAYS:
        if w[0] == int(d):
            return w[1]

@register.filter(expects_localtime=True)
def to_weekday(date_obj_tpl):
    oh, date_obj = date_obj_tpl
    now = utils.get_now()
    day = date_obj.isoweekday()
    if day == now.isoweekday() and (date_obj - now).days == 0:
        return _("today")
    for w in WEEKDAYS:
        if w[0] == int(day):
            return w[1]

@register.assignment_tag
def is_open(location=None, attr=None):
    """
    Returns False if the location is closed, or the OpeningHours object
    to show the location is currently open.
    """
    obj = utils.is_open(location)
    if obj is False:
        return False
    if attr is not None:
        return getattr(obj, attr)
    return obj

@register.assignment_tag
def next_time_open(location):
    """
    Returns the next possible OpeningHours object, or False
    if the location is currently open or if there is no such object.
    """
    obj, ts = utils.next_time_open(location)
    return obj

@register.filter(expects_localtime=True)
def has_closing_rule_for_now(location, attr=None):
    obj = utils.has_closing_rule_for_now(location)
    if obj is False:
        return False
    if attr is not None:
        return getattr(obj, attr)
    return obj

@register.filter(expects_localtime=True)
def get_closing_rule_for_now(location, attr=None):
    obj = utils.get_closing_rule_for_now(location)
    if obj is False:
        return False
    if attr is not None:
        return getattr(obj[0], attr)
    return obj

@register.simple_tag
def opening_hours(location=None, concise=False):
    """
    Creates a rendered listing of hours.
    """
    template_name = 'openinghours/opening_hours_list.html'
    days = []  # [{'hours': '9:00am to 5:00pm', 'name': u'Monday'}, {'hours...

    # Without `location`, choose the first company.
    if location:
        ohrs = OpeningHours.objects.filter(company=location)
    else:
        try:
            Location = utils.get_premises_model()
            ohrs = Location.objects.first().openinghours_set.all()
        except AttributeError:
            raise Exception("You must define some opening hours"
                            " to use the opening hours tags.")

    ohrs.order_by('weekday', 'from_hour')

    for o in ohrs:
        days.append({
            'day_number': o.weekday,
            'name': o.get_weekday_display(),
            'from_hour': o.from_hour,
            'to_hour': o.to_hour,
            'hours': '%s%s to %s%s' % (
                o.from_hour.strftime('%I:%M').lstrip('0'),
                o.from_hour.strftime('%p').lower(),
                o.to_hour.strftime('%I:%M').lstrip('0'),
                o.to_hour.strftime('%p').lower()
            )
        })

    open_days = [o.weekday for o in ohrs]
    for day_number, day_name in WEEKDAYS:
        if day_number not in open_days:
            days.append({
                'day_number': day_number,
                'name': day_name,
                'hours': 'Closed'
            })
    days = sorted(days, key=lambda k: k['day_number'])

    if concise:
        # [{'hours': '9:00am to 5:00pm', 'day_names': u'Monday to Friday'},
        #  {'hours':...
        template_name = 'openinghours/opening_hours_list_concise.html'
        concise_days = []
        current_set = {}
        for day in days:
            if 'hours' not in current_set.keys():
                current_set = {'day_names': [day['name']],
                               'hours': day['hours']}
            elif day['hours'] != current_set['hours']:
                concise_days.append(current_set)
                current_set = {'day_names': [day['name']],
                               'hours': day['hours']}
            else:
                current_set['day_names'].append(day['name'])
        concise_days.append(current_set)

        for day_set in concise_days:
            if len(day_set['day_names']) > 2:
                day_set['day_names'] = '%s to %s' % (day_set['day_names'][0],
                                                     day_set['day_names'][-1])
            elif len(day_set['day_names']) > 1:
                day_set['day_names'] = '%s and %s' % (day_set['day_names'][0],
                                                      day_set['day_names'][-1])
            else:
                day_set['day_names'] = '%s' % day_set['day_names'][0]

        days = concise_days

    template = get_template(template_name)
    return template.render({'days': days})
<|endoftext|>