<commit_before>from sys import version_info, getfilesystemencoding
from os import strerror, read
from enum import Enum, IntEnum
from collections import namedtuple
from struct import unpack_from, calcsize
from select import poll
from time import sleep
from ctypes import cdll, get_errno, c_int
from errno import EINTR
from termios import FIONREAD
from fcntl import ioctl
from io import FileIO

PY2 = version_info.major < 3
if PY2:
    fsencode = lambda s: s if isinstance(s, str) else s.encode(getfilesystemencoding())
    # In 32-bit Python < 3 the inotify constants don't fit in an IntEnum:
    IntEnum = type('IntEnum', (long, Enum), {})
else:
    from os import fsencode, fsdecode

__version__ = '1.3.2'

__all__ = ['Event', 'INotify', 'flags', 'masks', 'parse_events']

_libc = None

def _libc_call(function, *args):
    """Wrapper which raises errors and retries on EINTR."""
    while True:
        rc = function(*args)
        if rc != -1:
            return rc
        errno = get_errno()
        if errno != EINTR:
            raise OSError(errno, strerror(errno))

#: A ``namedtuple`` (wd, mask, cookie, name) for an inotify event. On Python 3 the
#: :attr:`~inotify_simple.Event.name`  field is a ``str`` decoded with
#: ``os.fsdecode()``, on Python 2 it is ``bytes``.
Event = namedtuple('Event', ['wd', 'mask', 'cookie', 'name'])

_EVENT_FMT = 'iIII'
_EVENT_SIZE = calcsize(_EVENT_FMT)

CLOEXEC = 0o2000000
NONBLOCK = 0o0004000

class INotify(FileIO):

    #: The inotify file descriptor returned by ``inotify_init()``. You are
    #: free to use it directly with ``os.read`` if you'd prefer not to call
    #: :func:`~inotify_simple.INotify.read` for some reason. Also available as
    #: :func:`~inotify_simple.INotify.fileno`
    fd = property(FileIO.fileno)

    def __init__(self, inheritable=False, nonblocking=False):
        """File-like object wrapping ``inotify_init1()``. Raises ``OSError`` on failure.
        :func:`~inotify_simple.INotify.close` should be called when no longer needed.
        Can be used as a context manager to ensure it is closed, and can be used
        directly by functions expecting a file-like object, such as ``select``, or with
        functions expecting a file descriptor via
        :func:`~inotify_simple.INotify.fileno`.

        Args:
            inheritable (bool): whether the inotify file descriptor will be inherited by
                child processes. The default,``False``, corresponds to passing the
                ``IN_CLOEXEC`` flag to ``inotify_init1()``. Setting this flag when
                opening filedescriptors is the default behaviour of Python standard
                library functions since PEP 446.

            nonblocking (bool): whether to open the inotify file descriptor in
                nonblocking mode, corresponding to passing the ``IN_NONBLOCK`` flag to
                ``inotify_init1()``. This does not affect the normal behaviour of
                :func:`~inotify_simple.INotify.read`, which uses ``poll()`` to control
                blocking behaviour according to the given timeout, but will cause other
                reads of the file descriptor (for example if the application reads data
                manually with ``os.read(fd)``) to raise ``BlockingIOError`` if no data
                is available."""
        global _libc; _libc = _libc or cdll.LoadLibrary('libc.so.6')
        flags = (not inheritable) * CLOEXEC | bool(nonblocking) * NONBLOCK
        FileIO.__init__(self, _libc_call(_libc.inotify_init1, flags), mode='rb')
        self._poller = poll()
        self._poller.register(self.fileno())

    def add_watch(self, path, mask):
        """Wrapper around ``inotify_add_watch()``. Returns the watch
        descriptor or raises an ``OSError`` on failure.

        Args:
            path (str, bytes, or PathLike): The path to watch. Will be encoded with
                ``os.fsencode()`` before being passed to ``inotify_add_watch()``.

            mask (int): The mask of events to watch for. Can be constructed by
                bitwise-ORing :class:`~inotify_simple.flags` together.

        Returns:
            int: watch descriptor"""
        # Explicit conversion of Path to str required on Python < 3.6
        path = str(path) if hasattr(path, 'parts') else path
        return _libc_call(_libc.inotify_add_watch, self.fileno(), fsencode(path), mask)

    def rm_watch(self, wd):
        """Wrapper around ``inotify_rm_watch()``. Raises ``OSError`` on failure.

        Args:
            wd (int): The watch descriptor to remove"""
        _libc_call(_libc.inotify_rm_watch, self.fileno(), wd)

    def read(self, timeout=None, read_delay=None):
        """Read the inotify file descriptor and return the resulting
        :attr:`~inotify_simple.Event` namedtuples (wd, mask, cookie, name).

        Args:
            timeout (int): The time in milliseconds to wait for events if there are
                none. If negative or ``None``, block until there are events. If zero,
                return immediately if there are no events to be read.

            read_delay (int): If there are no events immediately available for reading,
                then this is the time in milliseconds to wait after the first event
                arrives before reading the file descriptor. This allows further events
                to accumulate before reading, which allows the kernel to coalesce like
                events and can decrease the number of events the application needs to
                process. However, this also increases the risk that the event queue will
                overflow due to not being emptied fast enough.

        Returns:
            generator: generator producing :attr:`~inotify_simple.Event` namedtuples

        .. warning::
            If the same inotify file descriptor is being read by multiple threads
            simultaneously, this method may attempt to read the file descriptor when no
            data is available. It may return zero events, or block until more events
            arrive (regardless of the requested timeout), or in the case that the
            :func:`~inotify_simple.INotify` object was instantiated with
            ``nonblocking=True``, raise ``BlockingIOError``.
        """
        data = self._readall()
        if not data and timeout != 0 and self._poller.poll(timeout):
            if read_delay is not None:
                sleep(read_delay / 1000.0)
            data = self._readall()
        return parse_events(data)

    def _readall(self):
        bytes_avail = c_int()
        ioctl(self, FIONREAD, bytes_avail)
        if not bytes_avail.value:
            return b''
        return read(self.fileno(), bytes_avail.value)

def parse_events(data):
    """Unpack data read from an inotify file descriptor into
    :attr:`~inotify_simple.Event` namedtuples (wd, mask, cookie, name). This function
    can be used if the application has read raw data from the inotify file
    descriptor rather than calling :func:`~inotify_simple.INotify.read`.

    Args:
        data (bytes): A bytestring as read from an inotify file descriptor.

    Returns:
        list: list of :attr:`~inotify_simple.Event` namedtuples"""
    pos = 0
    events = []
    while pos < len(data):
        wd, mask, cookie, namesize = unpack_from(_EVENT_FMT, data, pos)
        pos += _EVENT_SIZE + namesize
        name = data[pos - namesize : pos].split(b'\x00', 1)[0]
        events.append(Event(wd, mask, cookie, name if PY2 else fsdecode(name)))
    return events

class flags(IntEnum):
    """Inotify flags as defined in ``inotify.h`` but with ``IN_`` prefix omitted.
    Includes a convenience method :func:`~inotify_simple.flags.from_mask` for extracting
    flags from a mask."""
    ACCESS = 0x00000001  #: File was accessed
    MODIFY = 0x00000002  #: File was modified
    ATTRIB = 0x00000004  #: Metadata changed
    CLOSE_WRITE = 0x00000008  #: Writable file was closed
    CLOSE_NOWRITE = 0x00000010  #: Unwritable file closed
    OPEN = 0x00000020  #: File was opened
    MOVED_FROM = 0x00000040  #: File was moved from X
    MOVED_TO = 0x00000080  #: File was moved to Y
    CREATE = 0x00000100  #: Subfile was created
    DELETE = 0x00000200  #: Subfile was deleted
    DELETE_SELF = 0x00000400  #: Self was deleted
    MOVE_SELF = 0x00000800  #: Self was moved

    UNMOUNT = 0x00002000  #: Backing fs was unmounted
    Q_OVERFLOW = 0x00004000  #: Event queue overflowed
    IGNORED = 0x00008000  #: File was ignored

    ONLYDIR = 0x01000000  #: only watch the path if it is a directory
    DONT_FOLLOW = 0x02000000  #: don't follow a sym link
    EXCL_UNLINK = 0x04000000  #: exclude events on unlinked objects
    MASK_ADD = 0x20000000  #: add to the mask of an already existing watch
    ISDIR = 0x40000000  #: event occurred against dir
    ONESHOT = 0x80000000  #: only send event once

    @classmethod
    def from_mask(cls, mask):
        """Convenience method that returns a list of every flag in a mask."""
        return [flag for flag in cls.__members__.values() if flag & mask]

class masks(IntEnum):
    """Convenience masks as defined in ``inotify.h`` but with ``IN_`` prefix omitted."""
    #: helper event mask equal to ``flags.CLOSE_WRITE | flags.CLOSE_NOWRITE``
    CLOSE = flags.CLOSE_WRITE | flags.CLOSE_NOWRITE
    #: helper event mask equal to ``flags.MOVED_FROM | flags.MOVED_TO``
    MOVE = flags.MOVED_FROM | flags.MOVED_TO

    #: bitwise-OR of all the events that can be passed to
    #: :func:`~inotify_simple.INotify.add_watch`
    ALL_EVENTS  = (flags.ACCESS | flags.MODIFY | flags.ATTRIB | flags.CLOSE_WRITE |
        flags.CLOSE_NOWRITE | flags.OPEN | flags.MOVED_FROM | flags.MOVED_TO |
        flags.CREATE | flags.DELETE| flags.DELETE_SELF | flags.MOVE_SELF)
<commit_msg>Fix bug in which errno was not correctly set<commit_after>from sys import version_info, getfilesystemencoding
from os import strerror, read
from enum import Enum, IntEnum
from collections import namedtuple
from struct import unpack_from, calcsize
from select import poll
from time import sleep
from ctypes import CDLL, get_errno, c_int
from errno import EINTR
from termios import FIONREAD
from fcntl import ioctl
from io import FileIO

PY2 = version_info.major < 3
if PY2:
    fsencode = lambda s: s if isinstance(s, str) else s.encode(getfilesystemencoding())
    # In 32-bit Python < 3 the inotify constants don't fit in an IntEnum:
    IntEnum = type('IntEnum', (long, Enum), {})
else:
    from os import fsencode, fsdecode

__version__ = '1.3.3'

__all__ = ['Event', 'INotify', 'flags', 'masks', 'parse_events']

_libc = None

def _libc_call(function, *args):
    """Wrapper which raises errors and retries on EINTR."""
    while True:
        rc = function(*args)
        if rc != -1:
            return rc
        errno = get_errno()
        if errno != EINTR:
            raise OSError(errno, strerror(errno))

#: A ``namedtuple`` (wd, mask, cookie, name) for an inotify event. On Python 3 the
#: :attr:`~inotify_simple.Event.name`  field is a ``str`` decoded with
#: ``os.fsdecode()``, on Python 2 it is ``bytes``.
Event = namedtuple('Event', ['wd', 'mask', 'cookie', 'name'])

_EVENT_FMT = 'iIII'
_EVENT_SIZE = calcsize(_EVENT_FMT)

CLOEXEC = 0o2000000
NONBLOCK = 0o0004000

class INotify(FileIO):

    #: The inotify file descriptor returned by ``inotify_init()``. You are
    #: free to use it directly with ``os.read`` if you'd prefer not to call
    #: :func:`~inotify_simple.INotify.read` for some reason. Also available as
    #: :func:`~inotify_simple.INotify.fileno`
    fd = property(FileIO.fileno)

    def __init__(self, inheritable=False, nonblocking=False):
        """File-like object wrapping ``inotify_init1()``. Raises ``OSError`` on failure.
        :func:`~inotify_simple.INotify.close` should be called when no longer needed.
        Can be used as a context manager to ensure it is closed, and can be used
        directly by functions expecting a file-like object, such as ``select``, or with
        functions expecting a file descriptor via
        :func:`~inotify_simple.INotify.fileno`.

        Args:
            inheritable (bool): whether the inotify file descriptor will be inherited by
                child processes. The default,``False``, corresponds to passing the
                ``IN_CLOEXEC`` flag to ``inotify_init1()``. Setting this flag when
                opening filedescriptors is the default behaviour of Python standard
                library functions since PEP 446.

            nonblocking (bool): whether to open the inotify file descriptor in
                nonblocking mode, corresponding to passing the ``IN_NONBLOCK`` flag to
                ``inotify_init1()``. This does not affect the normal behaviour of
                :func:`~inotify_simple.INotify.read`, which uses ``poll()`` to control
                blocking behaviour according to the given timeout, but will cause other
                reads of the file descriptor (for example if the application reads data
                manually with ``os.read(fd)``) to raise ``BlockingIOError`` if no data
                is available."""
        global _libc; _libc = _libc or CDLL('libc.so.6', use_errno=True)
        flags = (not inheritable) * CLOEXEC | bool(nonblocking) * NONBLOCK
        FileIO.__init__(self, _libc_call(_libc.inotify_init1, flags), mode='rb')
        self._poller = poll()
        self._poller.register(self.fileno())

    def add_watch(self, path, mask):
        """Wrapper around ``inotify_add_watch()``. Returns the watch
        descriptor or raises an ``OSError`` on failure.

        Args:
            path (str, bytes, or PathLike): The path to watch. Will be encoded with
                ``os.fsencode()`` before being passed to ``inotify_add_watch()``.

            mask (int): The mask of events to watch for. Can be constructed by
                bitwise-ORing :class:`~inotify_simple.flags` together.

        Returns:
            int: watch descriptor"""
        # Explicit conversion of Path to str required on Python < 3.6
        path = str(path) if hasattr(path, 'parts') else path
        return _libc_call(_libc.inotify_add_watch, self.fileno(), fsencode(path), mask)

    def rm_watch(self, wd):
        """Wrapper around ``inotify_rm_watch()``. Raises ``OSError`` on failure.

        Args:
            wd (int): The watch descriptor to remove"""
        _libc_call(_libc.inotify_rm_watch, self.fileno(), wd)

    def read(self, timeout=None, read_delay=None):
        """Read the inotify file descriptor and return the resulting
        :attr:`~inotify_simple.Event` namedtuples (wd, mask, cookie, name).

        Args:
            timeout (int): The time in milliseconds to wait for events if there are
                none. If negative or ``None``, block until there are events. If zero,
                return immediately if there are no events to be read.

            read_delay (int): If there are no events immediately available for reading,
                then this is the time in milliseconds to wait after the first event
                arrives before reading the file descriptor. This allows further events
                to accumulate before reading, which allows the kernel to coalesce like
                events and can decrease the number of events the application needs to
                process. However, this also increases the risk that the event queue will
                overflow due to not being emptied fast enough.

        Returns:
            generator: generator producing :attr:`~inotify_simple.Event` namedtuples

        .. warning::
            If the same inotify file descriptor is being read by multiple threads
            simultaneously, this method may attempt to read the file descriptor when no
            data is available. It may return zero events, or block until more events
            arrive (regardless of the requested timeout), or in the case that the
            :func:`~inotify_simple.INotify` object was instantiated with
            ``nonblocking=True``, raise ``BlockingIOError``.
        """
        data = self._readall()
        if not data and timeout != 0 and self._poller.poll(timeout):
            if read_delay is not None:
                sleep(read_delay / 1000.0)
            data = self._readall()
        return parse_events(data)

    def _readall(self):
        bytes_avail = c_int()
        ioctl(self, FIONREAD, bytes_avail)
        if not bytes_avail.value:
            return b''
        return read(self.fileno(), bytes_avail.value)

def parse_events(data):
    """Unpack data read from an inotify file descriptor into
    :attr:`~inotify_simple.Event` namedtuples (wd, mask, cookie, name). This function
    can be used if the application has read raw data from the inotify file
    descriptor rather than calling :func:`~inotify_simple.INotify.read`.

    Args:
        data (bytes): A bytestring as read from an inotify file descriptor.

    Returns:
        list: list of :attr:`~inotify_simple.Event` namedtuples"""
    pos = 0
    events = []
    while pos < len(data):
        wd, mask, cookie, namesize = unpack_from(_EVENT_FMT, data, pos)
        pos += _EVENT_SIZE + namesize
        name = data[pos - namesize : pos].split(b'\x00', 1)[0]
        events.append(Event(wd, mask, cookie, name if PY2 else fsdecode(name)))
    return events

class flags(IntEnum):
    """Inotify flags as defined in ``inotify.h`` but with ``IN_`` prefix omitted.
    Includes a convenience method :func:`~inotify_simple.flags.from_mask` for extracting
    flags from a mask."""
    ACCESS = 0x00000001  #: File was accessed
    MODIFY = 0x00000002  #: File was modified
    ATTRIB = 0x00000004  #: Metadata changed
    CLOSE_WRITE = 0x00000008  #: Writable file was closed
    CLOSE_NOWRITE = 0x00000010  #: Unwritable file closed
    OPEN = 0x00000020  #: File was opened
    MOVED_FROM = 0x00000040  #: File was moved from X
    MOVED_TO = 0x00000080  #: File was moved to Y
    CREATE = 0x00000100  #: Subfile was created
    DELETE = 0x00000200  #: Subfile was deleted
    DELETE_SELF = 0x00000400  #: Self was deleted
    MOVE_SELF = 0x00000800  #: Self was moved

    UNMOUNT = 0x00002000  #: Backing fs was unmounted
    Q_OVERFLOW = 0x00004000  #: Event queue overflowed
    IGNORED = 0x00008000  #: File was ignored

    ONLYDIR = 0x01000000  #: only watch the path if it is a directory
    DONT_FOLLOW = 0x02000000  #: don't follow a sym link
    EXCL_UNLINK = 0x04000000  #: exclude events on unlinked objects
    MASK_ADD = 0x20000000  #: add to the mask of an already existing watch
    ISDIR = 0x40000000  #: event occurred against dir
    ONESHOT = 0x80000000  #: only send event once

    @classmethod
    def from_mask(cls, mask):
        """Convenience method that returns a list of every flag in a mask."""
        return [flag for flag in cls.__members__.values() if flag & mask]

class masks(IntEnum):
    """Convenience masks as defined in ``inotify.h`` but with ``IN_`` prefix omitted."""
    #: helper event mask equal to ``flags.CLOSE_WRITE | flags.CLOSE_NOWRITE``
    CLOSE = flags.CLOSE_WRITE | flags.CLOSE_NOWRITE
    #: helper event mask equal to ``flags.MOVED_FROM | flags.MOVED_TO``
    MOVE = flags.MOVED_FROM | flags.MOVED_TO

    #: bitwise-OR of all the events that can be passed to
    #: :func:`~inotify_simple.INotify.add_watch`
    ALL_EVENTS  = (flags.ACCESS | flags.MODIFY | flags.ATTRIB | flags.CLOSE_WRITE |
        flags.CLOSE_NOWRITE | flags.OPEN | flags.MOVED_FROM | flags.MOVED_TO |
        flags.CREATE | flags.DELETE| flags.DELETE_SELF | flags.MOVE_SELF)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# coding:utf-8
# vi:tabstop=4:shiftwidth=4:expandtab:sts=4

from macros import macros
from macros import inception
from macros import namespace
from macros import ln
from stacked import softmax

def googlenet(n):
    return macros((
        (0, 64, 7, 2, 0, 0, {'layername': 'conv1/7x7_s2'}),
        (0, 0, 3, 2, 0, 0, {'maxpool': True, 'layername': 'pool1/3x3_s2'}),
        (0, 0, 0, 0, 0, 0, {
            'lru': {'alpha': 0.00002, 'k': 1},
            'layername': 'pool1/norm1'}),
        (0, 64, 1, 1, 0, 0, {'layername': 'conv2/3x3_reduce'}),
        (0, 192, 3, 1, 0, 0, {'layername': 'conv2/3x3'}),
        (0, 0, 0, 0, 0, 0, {
            'lru': {'alpha': 0.00002, 'k': 1},
            'layername': 'conv2/norm2'}),
        (0, 0, 3, 2, 0, 0, {'maxpool': True, 'layername': 'pool2/3x3_s2'}),
        (namespace, 'inception_3a/', (
            (inception, (32,  64,  96,  128,  16,  32,)),
            )),
        (ln,0,'inception_3a'),
        (namespace, 'inception_3b/', (
            (inception, (64,  128,  128,  192,  32,  96, )),
            )),
        (ln,0,'inception_3b'),
        (0, 0, 3, 2, 0, 0, {'maxpool': True, 'layername': 'pool3/3x3_s2'}),
        (namespace, 'inception_4a/', (
            (inception, (64,   192,  96,   208,  16,  48,)),
            )),
        (ln,0,'inception_4a'),
        (namespace, 'inception_4b/', (
            (inception, (64,   160,  112,  224,  24,  64,)),
            )),
        (ln,0,'inception_4b'),
        (namespace, 'inception_4c/', (
            (inception, (64,   128,  128,  256,  24,  64,)),
            )),
        (ln,0,'inception_4c'),
        (namespace, 'inception_4d/', (
            (inception, (64,   112,  144,  288,  32,  64,)),
            )),
        (ln,0,'inception_4d'),
        (namespace, 'inception_4e/', (
            (inception, (128,  256,  160,  320,  32,  128, )),
            )),
        (ln,0,'inception_4e'),
        (0, 0, 3, 2, 0, 0, {'maxpool': True, 'layername': 'pool4/3x3_s2'}),
        (namespace, 'inception_5a/', (
            (inception, (128,  256,  160,  320,  32,  128,)),
            )),
        (ln,0,'inception_5a'),
        (namespace, 'inception_5b/', (
            (inception, (128,  384,  192,  384,  48,  128,)),
            )),
        (ln,0,'inception_5b'),
        (0, 0, 7, 1, 0, 0, {
            'meanpool': True, 'pad': 0, 'layername': 'pool5/7x7_s1'}),
        (0, 0, 0, 0, 0, 0, {
            'dropout': 0.7, 'layername': 'pool5/dropout'}),
        (0, n, 0, 0, 0, 0, {
            'dense': True, 'linear': True, 'layername': 'loss3/classifier'}),
        (0, 0, 0, 0, 0, 0, {
            'nonlinearity': (lambda x:x,softmax), 'layername': 'prob'}),
        ))
<commit_msg>googlenet support auxiliary classifier<commit_after>#!/usr/bin/env python
# coding:utf-8
# vi:tabstop=4:shiftwidth=4:expandtab:sts=4

from macros import macros
from macros import inception
from macros import namespace
from macros import ln
from stacked import softmax

from framework.main import newlayers,layers

def googlenet(n,auxiliary_classifier=True):
    return macros((
        (0, 64, 7, 2, 0, 0, {'layername': 'conv1/7x7_s2'}),
        (0, 0, 3, 2, 0, 0, {'maxpool': True, 'layername': 'pool1/3x3_s2'}),
        (0, 0, 0, 0, 0, 0, {
            'lru': {'alpha': 0.00002, 'k': 1},
            'layername': 'pool1/norm1'}),
        (0, 64, 1, 1, 0, 0, {'layername': 'conv2/3x3_reduce'}),
        (0, 192, 3, 1, 0, 0, {'layername': 'conv2/3x3'}),
        (0, 0, 0, 0, 0, 0, {
            'lru': {'alpha': 0.00002, 'k': 1},
            'layername': 'conv2/norm2'}),
        (0, 0, 3, 2, 0, 0, {'maxpool': True, 'layername': 'pool2/3x3_s2'}),
        (namespace, 'inception_3a/', (
            (inception, (32,  64,  96,  128,  16,  32,)),
            )),
        (ln,0,'inception_3a'),
        (namespace, 'inception_3b/', (
            (inception, (64,  128,  128,  192,  32,  96, )),
            )),
        (ln,0,'inception_3b'),
        (0, 0, 3, 2, 0, 0, {'maxpool': True, 'layername': 'pool3/3x3_s2'}),
        (namespace, 'inception_4a/', (
            (inception, (64,   192,  96,   208,  16,  48,)),
            )),
        (ln,0,'inception_4a'),
        (switch,auxiliary_classifier,(
            (0, 0, 5, 3, 0, 0, {
                'meanpool': True, 'pad': 0, 'layername': 'loss1/ave_pool'}),
            (0, 128, 1, 1, 0, 0, {'layername': 'loss1/conv'}),
            (0, 1024, 0, 0, 'loss1/fc', 0, {
                'dense': True, 'layername': 'loss1/fc'}),
            (0, 0, 0, 0, 0, 0, {
                'dropout': 0.7, 'layername': 'loss1/dropout'}),
            (0, n, 0, 0, 'loss1/classifier', 0, {
                'dense': True, 'linear': True, 'layername': 'loss1/classifier'}),
            (0, 0, 0, 0, 'loss1/prob', 0, {
                'nonlinearity': (lambda x:x,softmax), 'layername': 'loss1/prob'}),
            ),()),
        (ln,'inception_4a'),
        (namespace, 'inception_4b/', (
            (inception, (64,   160,  112,  224,  24,  64,)),
            )),
        (ln,0,'inception_4b'),
        (namespace, 'inception_4c/', (
            (inception, (64,   128,  128,  256,  24,  64,)),
            )),
        (ln,0,'inception_4c'),
        (namespace, 'inception_4d/', (
            (inception, (64,   112,  144,  288,  32,  64,)),
            )),
        (ln,0,'inception_4d'),
        (switch,auxiliary_classifier,(
            (0, 0, 5, 3, 0, 0, {
                'meanpool': True, 'pad': 0, 'layername': 'loss2/ave_pool'}),
            (0, 128, 1, 1, 0, 0, {'layername': 'loss2/conv'}),
            (0, 1024, 0, 0, 'loss2/fc', 0, {
                'dense': True, 'layername': 'loss2/fc'}),
            (0, 0, 0, 0, 0, 0, {
                'dropout': 0.7, 'layername': 'loss2/dropout'}),
            (0, n, 0, 0, 'loss2/classifier', 0, {
                'dense': True, 'linear': True, 'layername': 'loss2/classifier'}),
            (0, 0, 0, 0, 'loss2/prob', 0, {
                'nonlinearity': (lambda x:x,softmax), 'layername': 'loss2/prob'}),
            ),()),
        (ln,'inception_4d'),
        (namespace, 'inception_4e/', (
            (inception, (128,  256,  160,  320,  32,  128, )),
            )),
        (ln,0,'inception_4e'),
        (0, 0, 3, 2, 0, 0, {'maxpool': True, 'layername': 'pool4/3x3_s2'}),
        (namespace, 'inception_5a/', (
            (inception, (128,  256,  160,  320,  32,  128,)),
            )),
        (ln,0,'inception_5a'),
        (namespace, 'inception_5b/', (
            (inception, (128,  384,  192,  384,  48,  128,)),
            )),
        (ln,0,'inception_5b'),
        (0, 0, 7, 1, 0, 0, {
            'meanpool': True, 'pad': 0, 'layername': 'pool5/7x7_s1'}),
        (0, 0, 0, 0, 0, 0, {
            'dropout': 0.7, 'layername': 'pool5/dropout'}),
        (0, n, 0, 0, 'loss3/classifier', 0, {
            'dense': True, 'linear': True, 'layername': 'loss3/classifier'}),
        (0, 0, 0, 0, 'prob', 0, {
            'nonlinearity': (lambda x:x,softmax), 'layername': 'prob'}),
        ))
<|endoftext|>


<commit_before>from PyQt4 import QtCore, QtGui
from qt_interfaces.single_file_download_ui import Ui_SingleFileDownload
from crypto.file_crypto_tools import FileCrypto
from engine import StorjEngine
import storj

class SingleFileDownloadUI(QtGui.QMainWindow):

    def __init__(self, parent=None, bucketid=None, fileid=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_download = Ui_SingleFileDownload()
        self.ui_single_file_download.setupUi(self)
        # QtCore.QObject.connect(self.ui_single_file_download., QtCore.SIGNAL("clicked()"), self.save_config) # open bucket manager
        self.storj_engine = StorjEngine()  # init StorjEngine

        # self.initialize_shard_queue_table(file_pointers)

        QtCore.QObject.connect(self.ui_single_file_download.file_save_path_bt, QtCore.SIGNAL("clicked()"),
                               self.select_file_save_path)  # open file select dialog
        QtCore.QObject.connect(self.ui_single_file_download.tmp_dir_bt, QtCore.SIGNAL("clicked()"),
                               self.select_tmp_directory)  # open tmp directory select dialog
        QtCore.QObject.connect(self.ui_single_file_download.start_download_bt, QtCore.SIGNAL("clicked()"),
                               self.initialize_shard_queue_table)  # begin file downloading process

        file_metadata = self.storj_engine.storj_client.file_metadata("dc4778cc186192af49475b49", "07a2a9ebff6b7785b4bb18fd")

        self.ui_single_file_download.file_name.setText(html_format_begin + str(file_metadata.filename) + html_format_end)

    def set_current_status(self, current_status):
        self.ui_single_file_download.current_state.setText(html_format_begin + current_status + html_format_end)

    def select_tmp_directory(self):
        self.selected_tmp_dir = QtGui.QFileDialog.getExistingDirectory(None, 'Select a folder:', '',
                                                                       QtGui.QFileDialog.ShowDirsOnly)
        self.ui_single_file_download.tmp_dir.setText(str(self.selected_tmp_dir))

    def select_file_save_path(self):
        self.ui_single_file_download.file_save_path.setText(QFileDialog.getOpenFileName())

    def initialize_shard_queue_table(self, file_pointers=None):
        file_pointers = self.storj_engine.storj_client.file_pointers("dc4778cc186192af49475b49", "82bb813584496eb91080ca1f")
        options_array = {}

        i = 0
        model = QStandardItemModel(1, 1)  # initialize model for inserting to table

        model.setHorizontalHeaderLabels(['Progress', 'Hash', 'Farmer addres', 'State', 'Shard index'])
        for pointer in file_pointers:
            item = QStandardItem(str(""))
            model.setItem(i, 0, item)  # row, column, item (QStandardItem)

            item = QStandardItem(str(pointer["hash"]))
            model.setItem(i, 1, item)  # row, column, item (QStandardItem)

            item = QStandardItem(str(pointer["farmer"]["address"] + ":" + str(pointer["farmer"]["port"])))
            model.setItem(i, 2, item)  # row, column, item (QStandardItem)

            item = QStandardItem(str("Waiting..."))
            model.setItem(i, 3, item)  # row, column, item (QStandardItem)

            item = QStandardItem(str(pointer["index"]))
            model.setItem(i, 4, item)  # row, column, item (QStandardItem)

            options_array["file_size_shard_" + str(i)] = pointer["size"]
            i = i + 1
            # print  str(pointer["index"])+"index"

        self.ui_single_file_download.shard_queue_table.clearFocus()
        self.ui_single_file_download.shard_queue_table.setModel(model)
        self.ui_single_file_download.shard_queue_table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)

        i2 = 0
        progressbar_list = []
        for pointer in file_pointers:
            tablemodel = self.ui_single_file_download.shard_queue_table.model()
            index = tablemodel.index(i2, 0)
            progressbar_list.append(QProgressBar())
            self.ui_single_file_download.shard_queue_table.setIndexWidget(index, progressbar_list[i2])
            i2 = i2 + 1

        options_array["file_pointers"] = file_pointers
        options_array["file_pointers_is_given"] = "1"
        options_array["progressbars_enabled"] = "1"
        options_array["file_size_is_given"] = "1"
        options_array["shards_count"] = i

        self.ui_single_file_download.total_shards.setText(html_format_begin + str(i) + html_format_end)

        # storj_sdk_overrides = StorjSDKImplementationsOverrides()

        self.file_download(None, None, "/home/lakewik/rudasek1", options_array, progressbar_list)
        # progressbar_list[0].setValue(20)
        # progressbar_list[2].setValue(17)

    def calculate_final_hmac(self):
        return 1

    def create_download_connection(self, url, path_to_save, options_chain, progress_bar):
        local_filename = path_to_save
        downloaded = False

        while True:
            try:
                if options_chain["handle_progressbars"] != "1":
                    r = requests.get(url)
                    # requests.
                    with open(local_filename, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=1024):
                            if chunk:  # filter out keep-alive new chunks
                                f.write(chunk)
                else:
                    r = requests.get(url, stream=True)
                    f = open(local_filename, 'wb')
                    if options_chain["file_size_is_given"] == "1":
                        file_size = options_chain["shard_file_size"]
                    else:
                        file_size = int(r.headers['Content-Length'])

                    chunk = 1
                    num_bars = file_size / chunk
                    t1 = float(file_size) / float((32 * 1024))
                    print t1

                    if file_size <= (32 * 1024):
                        t1 = 1

                    i = 0
                    print file_size
                    print str(t1) + "kotek"
                    for chunk in r.iter_content(32 * 1024):
                        i += 1
                        f.write(chunk)
                        print str(i) + " " + str(t1)
                        print round(float(i) / float(t1), 1)
                        print str(int(round((100.0 * i) / t1))) + " %"
                        if int(round((100.0 * i) / t1)) > 100:
                            percent_downloaded = 100
                        else:
                            percent_downloaded = int(round((100.0 * i) / t1))
                        progress_bar.setValue(percent_downloaded)

                    f.close()
                    downloaded = True
            except Exception:
                continue
            else:
                downloaded = True
                break

        if not downloaded:
            self.emit(SIGNAL("retryWithNewDownloadPointer"), options_chain["shard_index"])  # retry download with new download pointer
        else:
            self.emit(SIGNAL("incrementShardsDownloadProgressCounters"))  # update already uploaded shards count
            self.emit(SIGNAL("updateDownloadTaskState"), options_chain["rowposition"], "Downloaded!")  # update shard upload state

            return

    def createNewDownloadThread(self, url, filelocation, options_chain, progress_bars_list):
        # self.download_thread = DownloadTaskQtThread(url, filelocation, options_chain, progress_bars_list)
        # self.download_thread.start()
        # self.download_thread.connect(self.download_thread, SIGNAL('setStatus'), self.test1, Qt.QueuedConnection)
        # self.download_thread.tick.connect(progress_bars_list.setValue)

        # Refactor to QtTrhead
        download_thread = threading.Thread(target=self.create_download_connection,
                                           args=(url, filelocation, options_chain, progress_bars_list))
        download_thread.start()

    def test1(self, value1, value2):
        print str(value1) + " aaa " + str(value2)

    def upload_file(self):
        print 1

    def file_download(self, bucket_id, file_id, file_save_path, options_array, progress_bars_list):
        options_chain = {}
        self.storj_engine.storj_client.logger.info('file_pointers(%s, %s)', bucket_id, file_id)

        # Determine file pointers
        if options_array["file_pointers_is_given"] == "1":
            pointers = options_array["file_pointers"]
        else:
            pointers = self.storj_engine.storj_client.file_pointers(bucket_id=bucket_id, file_id=file_id)

        if options_array["progressbars_enabled"] == "1":
            options_chain["handle_progressbars"] = "1"

        if options_array["file_size_is_given"] == "1":
            options_chain["file_size_is_given"] = "1"

        shards_count = int(options_array["shards_count"])

        i = 0
        shard_size_array = []
        while i < shards_count:
            shard_size_array.append(int(options_array["file_size_shard_" + str(i)]))
            i += 1
        print shard_size_array
        part = 0
        self.set_current_status("Starting download threads...")
        for pointer in pointers:
            self.set_current_status("Downloading shard at index " + str(part) + "...")

            print pointer
            options_chain["shard_file_size"] = shard_size_array[part]
            url = "http://" + pointer.get('farmer')['address'] + ":" + str(pointer.get('farmer')['port']) + "/shards/" + \
                  pointer["hash"] + "?token=" + pointer["token"]
            print url
            self.createNewDownloadThread(url, file_save_path + "part" + str(part), options_chain, progress_bars_list[part])
            part = part + 1

        # Join shards

        fileisencrypted = True

        if fileisencrypted:
            # decrypt file
            self.set_current_status("Decrypting file...")
            # self.set_current_status()
            file_crypto_tools = FileCrypto()
            # file_crypto_tools.decrypt_file("AES", str(file_path), self.parametrs.tmpPath + bname , "kotecze57") # begin file encryption

        print "pobrano"

        return True
<commit_msg>Improve and fix sigsegvs in download backend<commit_after>from PyQt4 import QtCore, QtGui
from qt_interfaces.single_file_download_ui import Ui_SingleFileDownload
from crypto.file_crypto_tools import FileCrypto
from engine import StorjEngine
import storj

class SingleFileDownloadUI(QtGui.QMainWindow):
    def __init__(self, parent=None, bucketid=None, fileid=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_download = Ui_SingleFileDownload()
        self.ui_single_file_download.setupUi(self)
        # QtCore.QObject.connect(self.ui_single_file_download., QtCore.SIGNAL("clicked()"), self.save_config) # open bucket manager
        self.storj_engine = StorjEngine()  # init StorjEngine

        #self.initialize_shard_queue_table(file_pointers)

        QtCore.QObject.connect(self.ui_single_file_download.file_save_path_bt, QtCore.SIGNAL("clicked()"),
                               self.select_file_save_path)  # open file select dialog
        QtCore.QObject.connect(self.ui_single_file_download.tmp_dir_bt, QtCore.SIGNAL("clicked()"),
                               self.select_tmp_directory)  # open tmp directory select dialog
        QtCore.QObject.connect(self.ui_single_file_download.start_download_bt, QtCore.SIGNAL("clicked()"),
                               lambda: self.createNewDownloadInitThread(bucketid, fileid))  # begin file downloading process

        self.connect(self, SIGNAL("incrementShardsDownloadProgressCounters"), self.increment_shards_download_progress_counters)
        self.connect(self, SIGNAL("updateShardDownloadProgress"), self.update_shard_download_progess)
        self.connect(self, SIGNAL("beginDownloadProccess"), self.download_begin)

        self.shards_already_downloaded = 0

        self.createNewInitializationThread(bucketid, fileid)

    def download_begin(self, file_pointers):
        self.destination_file_path = str(self.ui_single_file_download.file_save_path.text())
        self.tmp_path = str(self.ui_single_file_download.tmp_dir.text())

        options_array = {}
        options_array["tmp_path"] = self.tmp_path

        i = 0
        # model = QStandardItemModel(1, 1)  # initialize model for inserting to table

        self.ui_single_file_download.shard_queue_table.setColumnCount(5)
        self.ui_single_file_download.shard_queue_table.setRowCount(len(file_pointers))

        self.ui_single_file_download.shard_queue_table.setHorizontalHeaderLabels(
            ['Progress', 'Hash', 'Farmer addres', 'State', 'Shard index'])
        for pointer in file_pointers:
            item = QTableWidgetItem(str(""))
            self.ui_single_file_download.shard_queue_table.setItem(i, 0, item)  # row, column, item (QStandardItem)

            item = QTableWidgetItem(str(pointer["hash"]))
            self.ui_single_file_download.shard_queue_table.setItem(i, 1, item)  # row, column, item (QStandardItem)

            item = QTableWidgetItem(str(pointer["farmer"]["address"] + ":" + str(pointer["farmer"]["port"])))
            self.ui_single_file_download.shard_queue_table.setItem(i, 2, item)  # row, column, item (QStandardItem)

            item = QTableWidgetItem(str("Waiting..."))
            self.ui_single_file_download.shard_queue_table.setItem(i, 3, item)  # row, column, item (QStandardItem)

            item = QTableWidgetItem(str(pointer["index"]))
            self.ui_single_file_download.shard_queue_table.setItem(i, 4, item)  # row, column, item (QStandardItem)

            options_array["file_size_shard_" + str(i)] = pointer["size"]
            i = i + 1
            # print  str(pointer["index"])+"index"

        self.ui_single_file_download.shard_queue_table.clearFocus()
        self.ui_single_file_download.shard_queue_table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)

        i2 = 0
        self.progressbar_list = []
        for pointer in file_pointers:
            tablemodel = self.ui_single_file_download.shard_queue_table.model()
            index = tablemodel.index(i2, 0)
            self.progressbar_list.append(QProgressBar())
            self.ui_single_file_download.shard_queue_table.setIndexWidget(index, self.progressbar_list[i2])
            i2 = i2 + 1

        options_array["file_pointers"] = file_pointers
        options_array["file_pointers_is_given"] = "1"
        options_array["progressbars_enabled"] = "1"
        options_array["file_size_is_given"] = "1"
        options_array["shards_count"] = i

        self.all_shards_count = i

        self.ui_single_file_download.total_shards.setText(html_format_begin + str(i) + html_format_end)

        # storj_sdk_overrides = StorjSDKImplementationsOverrides()

        self.file_download(None, None, "/home/lakewik/rudasek2", options_array, self.progressbar_list)
        # progressbar_list[0].setValue(20)
        # progressbar_list[2].setValue(17)

    def createNewDownloadInitThread(self, bucket_id, file_id):
        file_name_resolve_thread = threading.Thread(target=self.init_download_file_pointers, args=(bucket_id, file_id))
        file_name_resolve_thread.start()

    def createNewInitializationThread(self, bucket_id, file_id):
        file_name_resolve_thread = threading.Thread(target=self.set_file_metadata, args=(bucket_id, file_id))
        file_name_resolve_thread.start()

    def set_file_metadata(self, bucket_id, file_id):
        file_metadata = self.storj_engine.storj_client.file_metadata("dc4778cc186192af49475b49", "07a2a9ebff6b7785b4bb18fd")
        self.ui_single_file_download.file_name.setText(html_format_begin + str(file_metadata.filename) + html_format_end)

        tools = Tools()
        self.ui_single_file_download.file_size.setText(html_format_begin + str(tools.human_size(int(file_metadata.size))) + html_format_end)
        self.ui_single_file_download.file_id.setText(html_format_begin + str(file_id) + html_format_end)

    def update_shard_download_progess (self, row_position_index, value):
        self.progressbar_list[row_position_index].setValue(value)
        return 1

    def increment_shards_download_progress_counters(self):
        self.shards_already_downloaded += 1
        self.ui_single_file_download.downloaded_shards.setText(html_format_begin + str(self.shards_already_downloaded) + html_format_end)

    def set_current_status(self, current_status):
        self.ui_single_file_download.current_state.setText(html_format_begin + current_status + html_format_end)

    def select_tmp_directory(self):
        self.selected_tmp_dir = QtGui.QFileDialog.getExistingDirectory(None, 'Select a folder:', '',
                                                                       QtGui.QFileDialog.ShowDirsOnly)
        self.ui_single_file_download.tmp_dir.setText(str(self.selected_tmp_dir))

    def init_download_file_pointers(self, bucket_id, file_id):
        file_pointers = self.storj_engine.storj_client.file_pointers("dc4778cc186192af49475b49", "9af4e2f80e7f334ae651464a")
        self.emit(SIGNAL("beginDownloadProccess"), file_pointers)

    def select_file_save_path(self):
        file_save_path = QtGui.QFileDialog.getSaveFileName(self, 'Save file to...', '')
        self.ui_single_file_download.file_save_path.setText(str(file_save_path))

    def calculate_final_hmac(self):
        return 1

    def create_download_connection(self, url, path_to_save, options_chain, progress_bar, rowposition):
        local_filename = path_to_save
        downloaded = False

        while True:
            try:
                if options_chain["handle_progressbars"] != "1":
                    r = requests.get(url)
                    # requests.
                    with open(local_filename, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=1024):
                            if chunk:  # filter out keep-alive new chunks
                                f.write(chunk)
                else:
                    r = requests.get(url, stream=True)
                    f = open(local_filename, 'wb')
                    if options_chain["file_size_is_given"] == "1":
                        file_size = options_chain["shard_file_size"]
                    else:
                        file_size = int(r.headers['Content-Length'])

                    chunk = 1
                    num_bars = file_size / chunk
                    t1 = float(file_size) / float((32 * 1024))
                    print t1

                    if file_size <= (32 * 1024):
                        t1 = 1

                    i = 0
                    print file_size
                    print str(t1) + "kotek"
                    for chunk in r.iter_content(32 * 1024):
                        i += 1
                        f.write(chunk)
                        print str(i) + " " + str(t1)
                        print round(float(i) / float(t1), 1)
                        print str(int(round((100.0 * i) / t1))) + " %"
                        if int(round((100.0 * i) / t1)) > 100:
                            percent_downloaded = 100
                        else:
                            percent_downloaded = int(round((100.0 * i) / t1))
                        self.emit(SIGNAL("updateShardDownloadProgress"), int(rowposition), percent_downloaded)  # update progress bar in upload queue table
                        print str(rowposition) + "pozycja"

                        #progress_bar.setValue(percent_downloaded)

                    f.close()
                    downloaded = True
            except Exception, e:
                print str(e)
                continue
            else:
                downloaded = True
                break

        if not downloaded:
            self.emit(SIGNAL("retryWithNewDownloadPointer"), options_chain["shard_index"])  # retry download with new download pointer
        else:
            self.emit(SIGNAL("incrementShardsDownloadProgressCounters"))  # update already uploaded shards count
            self.emit(SIGNAL("updateDownloadTaskState"), options_chain["rowposition"], "Downloaded!")  # update shard upload state
            if int(self.all_shards_count) <= int(self.shards_already_downloaded + 1):
                self.emit(SIGNAL("finishDownload"))  # send signal to begin file shards joind and decryption after all shards are downloaded

            return

    def createNewDownloadThread(self, url, filelocation, options_chain, progress_bars_list, rowposition):
        # self.download_thread = DownloadTaskQtThread(url, filelocation, options_chain, progress_bars_list)
        # self.download_thread.start()
        # self.download_thread.connect(self.download_thread, SIGNAL('setStatus'), self.test1, Qt.QueuedConnection)
        # self.download_thread.tick.connect(progress_bars_list.setValue)

        # Refactor to QtTrhead
        download_thread = threading.Thread(target=self.create_download_connection,
                                           args=(url, filelocation, options_chain, progress_bars_list, rowposition))
        download_thread.start()
        print str(options_chain["rowposition"]) + "position"

    def test1(self, value1, value2):
        print str(value1) + " aaa " + str(value2)

    def upload_file(self):
        print 1;

    def file_download(self, bucket_id, file_id, file_save_path, options_array, progress_bars_list):

        #### Begin file download finish function ####
        # Wait for signal to do shards joining and encryption
        def finish_download(self):

            fileisencrypted = False

            # Join shards
            sharing_tools = ShardingTools()
            self.set_current_status("Joining shards...")
            if fileisencrypted:
                sharing_tools.join_shards(self.tmp_path + "/" + str(file_name), "-", file_save_path + ".encrypted")
            else:
                sharing_tools.join_shards(self.tmp_path + "/" + str(file_name), "-", file_save_path)

            print file_save_path + ".encrypted"

            if fileisencrypted:
                # decrypt file
                self.set_current_status("Decrypting file...")
                # self.set_current_status()
                file_crypto_tools = FileCrypto()
                #file_crypto_tools.decrypt_file("AES", str(self.tmp_path + "/" + str(file_name)) + ".encrypted", file_save_path,
                 #                              "kotecze57")  # begin file decryption

            print "pobrano"

            return True

        self.connect(self, SIGNAL("finishDownload"), lambda: finish_download(self))

        ##### End file download finish point #####

        options_chain = {}
        self.storj_engine.storj_client.logger.info('file_pointers(%s, %s)', bucket_id, file_id)
        file_name = os.path.split(file_save_path)[1]
        # Determine file pointers
        if options_array["file_pointers_is_given"] == "1":
            pointers = options_array["file_pointers"]
        else:
            pointers = self.storj_engine.storj_client.file_pointers(bucket_id=bucket_id, file_id=file_id)

        if options_array["progressbars_enabled"] == "1":
            options_chain["handle_progressbars"] = "1"

        if options_array["file_size_is_given"] == "1":
            options_chain["file_size_is_given"] = "1"

        shards_count = int(options_array["shards_count"])

        i = 0
        shard_size_array = []
        while i < shards_count:
            shard_size_array.append(int(options_array["file_size_shard_" + str(i)]))
            i += 1
        print shard_size_array
        part = 0

        self.tmp_path = options_array["tmp_path"]

        self.set_current_status("Starting download threads...")
        for pointer in pointers:
            self.set_current_status("Downloading shard at index " + str(part) + "...")
            options_chain["rowposition"] = part

            print pointer
            options_chain["shard_file_size"] = shard_size_array[part]
            url = "http://" + pointer.get('farmer')['address'] + ":" + str(pointer.get('farmer')['port']) + "/shards/" + \
                  pointer["hash"] + "?token=" + pointer["token"]
            print url
            self.createNewDownloadThread(url, self.tmp_path + "/" + str(file_name) + "-" + str(part), options_chain, progress_bars_list[part], part)
            part = part + 1
<|endoftext|>


<commit_before>"""

Algorithm:
   starting number of schedules
   places all of these in a pq
   do batches of randomizations of schedules, rescore
   in each randomization of a schedule generate more schedules, each in a varying level of randomization
   put them all back along with the original, unchanged schedule
   randomization level based on score
   store number of conflicts, randomize based on that
   high randomization = randomizing days totally randomly
   low randomization = swapping out profs/groups with high conflicts or swapping between

"""

import random
from heapq import *

class Schedule:
    def __init__(self, profs, groups):
        # list of days containing a group professor pair
        # can switch days, groups, and professors around
        # a score and comparisons necessary to sort them
        self.score = 0
        self.num_days = 8
        self.num_rooms = 5
        self.days = [ {} for i in range(self.num_days) ]

        self.profs = profs
        self.groups = groups

    def score_schedule(self):
        # weighted probably
        # same group seeing same prof
        # number of prof lectures
        # low score good
        # 1. 70%
        # (each group: num times seen prof - 1) ** 2
        # 2. 20%
        # average = rooms * days / profs
        # further away from average (floor or ceil = no prob)
        # ( num_lecture_given - avg ) ** 2
        # 3. 10%
        # if over a max num of students add square of difference (num_in_class - avg) ** 2

        pass

    def generate_random_schedule(self):

        for day in self.days:

            for room in self.num_rooms:
                prof = self.profs[random.randint(0, len(self.profs)-1)]
                room[]

    def get_score(self):
        return self.score

    def __gt__(self, other):
        return self.score > other.get_score()

    def __lt__(self, other):
        return self.score < other.get_score()

    def __ge__(self, other):
        return self.score >= other.get_score()

    def __le__(self, other):
        return self.score <= other.get_score()

    def __eq__(self, other):
        return self.score == other.get_score()

    def copy(self):
        pass

class Group:
    def __init__( self, num_students, prof_name ):
        # has a number of students
        # constraints on days
        # what professors they have seen
        self.num_students = num_students
        self.prof_name = prof_name
        self.has_seen = {}

    def watch_lecture(self, prof_name):
        self.has_seen[prof_name] = True

    def has_seen(self, prof_name):
        return prof_name in self.has_seen

class Professor:
    def __init__(self):
        # has constraints on days
        # number of days they have lectured
        self.num_days_lectured = 0

def swap_elements(el1, el2, ls):
    cp = ls[el1]
    ls[el1] = ls[el2]
    ls[el2] = cp

def scramble_list(self, ls):

    for x in range(len(ls)):
        num = random.randint(x, len(ls)-1)
        swap_elements(x, num, ls)

# maybe have a room

        <commit_msg>Fixed interpreter, commented out error<commit_after>"""

Algorithm:
   starting number of schedules
   places all of these in a pq
   do batches of randomizations of schedules, rescore
   in each randomization of a schedule generate more schedules, each in a varying level of randomization
   put them all back along with the original, unchanged schedule
   randomization level based on score
   store number of conflicts, randomize based on that
   high randomization = randomizing days totally randomly
   low randomization = swapping out profs/groups with high conflicts or swapping between

"""

import random
from heapq import *

class Schedule:
    def __init__(self, profs, groups):
        # list of days containing a group professor pair
        # can switch days, groups, and professors around
        # a score and comparisons necessary to sort them
        self.score = 0
        self.num_days = 8
        self.num_rooms = 5
        self.days = [ {} for i in range(self.num_days) ]

        self.profs = profs
        self.groups = groups

    def score_schedule(self):
        # weighted probably
        # same group seeing same prof
        # number of prof lectures
        # low score good
        # 1. 70%
        # (each group: num times seen prof - 1) ** 2
        # 2. 20%
        # average = rooms * days / profs
        # further away from average (floor or ceil = no prob)
        # ( num_lecture_given - avg ) ** 2
        # 3. 10%
        # if over a max num of students add square of difference (num_in_class - avg) ** 2

        pass

    def generate_random_schedule(self):

        for day in self.days:

            for room in self.num_rooms:
                prof = self.profs[random.randint(0, len(self.profs)-1)]
#                 room[]

    def get_score(self):
        return self.score

    def __gt__(self, other):
        return self.score > other.get_score()

    def __lt__(self, other):
        return self.score < other.get_score()

    def __ge__(self, other):
        return self.score >= other.get_score()

    def __le__(self, other):
        return self.score <= other.get_score()

    def __eq__(self, other):
        return self.score == other.get_score()

    def copy(self):
        pass

class Group:
    def __init__( self, num_students, prof_name ):
        # has a number of students
        # constraints on days
        # what professors they have seen
        self.num_students = num_students
        self.prof_name = prof_name
        self.has_seen = {}

    def watch_lecture(self, prof_name):
        self.has_seen[prof_name] = True

    def has_seen(self, prof_name):
        return prof_name in self.has_seen

class Professor:
    def __init__(self):
        # has constraints on days
        # number of days they have lectured
        self.num_days_lectured = 0

def swap_elements(el1, el2, ls):
    cp = ls[el1]
    ls[el1] = ls[el2]
    ls[el2] = cp

def scramble_list(self, ls):

    for x in range(len(ls)):
        num = random.randint(x, len(ls)-1)
        swap_elements(x, num, ls)

# maybe have a room

        <|endoftext|>


<commit_before>'''
Created on 02/11/2015

@author: S41nz
'''

import time

#class TBTAFOrchestrator():
class TBTAFOrchestrator(object):
    '''
    Class that encapsulates the information from a given event within the TBTAF
    '''

    #Fields
	#Declare global variables
	_tBTestSuiteInstance
	_tBTestBedInstance
	_projectName
	TBTAFInterpreter _interpreter

    def __init__(self, nameFile = "nameFileDefault.txt"):
		#El parametro nameFile
        '''
        Constructor
        '''
		#leer archivo los parametros. Si no, tener unos parametros por default.
		#buscar como tirar una initialiation exception
		#buscar como tirar una invalid argument exception

		#Initialize variables
		#TBTAFInterpreter _interpreter
		_tBTestSuiteInstance
		_tBTestBedInstance
		_projectName

    def createNewProject(tBTestSuiteInstance, tBTestBedInstance, projectName):
		_tBTestSuiteInstance = tBTestSuiteInstance
		_tBTestBedInstance = tBTestBedInstance
		_projectName = projectName
		#Invalid Argument Exception

    def parseScript(self, filePath):
		#return _interpreter.parseScript(filePath)

    def createTestBed(self, urlList):
        #if urlList is null
			#urlList = "localhost"

		#Initialize TBTestBed
		#TBTestBed tBtestBedInstance
		#myTestBed = createTestBed(Collection of URLS to the execution nodes)

	#filePath - String containing the filepath where the test cases are located.
	#smartFilePath - Optional string containing the filepath where the production source code is located. This would be useful for TBSmartTestSuite creation.
	#tagList - String containing the list of tags which are desired to be executed among the existing test code within the specified location
	#puede que sea privado, ahorita lo consideramos publico y no lo mandamos a llamar en el constructor
    def createTestSuite(self, filePath, smartFilePath, tagList):

	#Prieor hay que checar que las últimas dos variables no sean nulas
	#Si no son deben contener ubicacion válidas
	if smartFilePath is not null or tagList is not null
		if validateExisitingLocations(smartFilePath) and validateExisitingLocations(tagList)
			tbTestSuiteInstance = createTestSuite()

	else
		#Throw Invalid Argument Exception.

	#tbTestSuiteInstance - Reference to a given TBTestSuite instance from which the test plan will be generated.
	#testPlanLocation - String specifying the location where the test plan wants to be placed.
	#outputFormat - Enumeration flag specifying the output format of the created test plan.
	def publishTestPlan(self, tbTestSuiteInstance, testPlanLocation, outputFormat):
		#TBTAFPublisher tbtafPublisherInstance
		#tbtafPublisherInstance.PublishTestPlan(tbTestSuiteInstance, testPlanLocation, outputFormat)

	#tbTestSuiteInstance - Reference to a given TBTestSuite instance from which the result report will be generated.
	#resultLocation - String specifying the location where the result report wants to be placed.
	#outputFormat - Enumeration flag specifying the output format of the created result report.
	def publishTestResults(self, tbTestSuiteInstance, resultLocation, outputFormat):
		#TBTAFPublisher tbtafPublisherInstance
		#tbtafPublisherInstance.PublishTestResults(tbTestSuiteInstance, resultLocation, outputFormat)

	#tbTestSuiteInstance - Reference to a given TBTestSuite instance from which the result report will be generated.
    #tbTestSuiteInstance - Reference of the TBTestSuite representing the set of tests that will be executed.
    #urlCollection - Optional collection of URLs corresponding to the nodes on which the test execution will be distributed.
    #flagsCollection - Optional collection of flags that can customize the execution of such TBTestSuite.
    #executorListenerCollection - Optional collection of ExecutorListener implementations that would want to listen and react to specific events during the execution of that specific TBTestSuite
	def excuteTestSuite(self, tbTestSuiteInstance, urlCollection, flagsCollection, executorListenerCollection):
		#TBTAFExecutor tbtafExecutorInstance
		return tbtafExecutorInstance.ExecuteTests(tbTestSuiteInstance, urlCollection, flagsCollection, executorListenerCollection)

    #projectName - String identifying the Project from where the query is being made.
    #tagList - Optional List of tags in order to filter the query being made.
    #indicatorEnumeration - If the list of tags is provided, then an additional enumeration can be passed in order to specify if the tags will be used to filter IN or OUT the results found.
	def GetTests(projectName, tagList, inOutList)
		_tBTestSuiteInstance

		return

    def getEventSource(self):
        return self.eventSource<commit_msg>Cambios del dia 10 de noviembre<commit_after>'''
Created on 02/11/2015

@author: S41nz
'''

import time

#class TBTAFOrchestrator():
class TBTAFOrchestrator(object):
    '''
    Class that encapsulates the information from a given event within the TBTAF
    '''

    #Fields
	#Declare global variables
	_tBTestSuiteInstance
	_tBTestBedInstance
	_projectName
	TBTAFInterpreter _interpreter

    def __init__(self, nameFile = "nameFileDefault.txt"):
		#El parametro nameFile
        '''
        Constructor
        '''
		#leer archivo los parametros. Si no, tener unos parametros por default.
		#buscar como tirar una initialiation exception
		#buscar como tirar una invalid argument exception

		#Initialize variables
		#TBTAFInterpreter _interpreter
		_tBTestSuiteInstance
		_tBTestBedInstance
		_projectName

    def createNewProject(tBTestSuiteInstance, tBTestBedInstance, projectName):
		_tBTestSuiteInstance = tBTestSuiteInstance
		_tBTestBedInstance = tBTestBedInstance
		_projectName = projectName
		#Invalid Argument Exception

    def parseScript(self, filePath):
		#return _interpreter.parseScript(filePath)

    def createTestBed(self, urlList):
        #if urlList is null
			#urlList = "localhost"

		#Initialize TBTestBed
		#TBTestBed tBtestBedInstance
		#myTestBed = createTestBed(Collection of URLS to the execution nodes)
		#La lista de urls va a estar asociado a un proyecto.

	#filePath - String containing the filepath where the test cases are located.
	#smartFilePath - Optional string containing the filepath where the production source code is located. This would be useful for TBSmartTestSuite creation.
	#tagList - String containing the list of tags which are desired to be executed among the existing test code within the specified location
	#puede que sea privado, ahorita lo consideramos publico y no lo mandamos a llamar en el constructor
    def createTestSuite(self, filePath, smartFilePath, tagList):

	#Primero hay que checar que las últimas dos variables no sean nulas
	#Si no son deben contener ubicacion válidas
	if smartFilePath is not null or tagList is not null
		if validateExisitingLocations(smartFilePath) and validateExisitingLocations(tagList)
			tbTestSuiteInstance = createTestSuite()

	else
		#Throw Invalid Argument Exception.

	#tbTestSuiteInstance - Reference to a given TBTestSuite instance from which the test plan will be generated.
	#testPlanLocation - String specifying the location where the test plan wants to be placed.
	#outputFormat - Enumeration flag specifying the output format of the created test plan.
	def publishTestPlan(self, tbTestSuiteInstance, testPlanLocation, outputFormat):
		#TBTAFPublisher tbtafPublisherInstance
		#tbtafPublisherInstance.PublishTestPlan(tbTestSuiteInstance, testPlanLocation, outputFormat)

	#tbTestSuiteInstance - Reference to a given TBTestSuite instance from which the result report will be generated.
	#resultLocation - String specifying the location where the result report wants to be placed.
	#outputFormat - Enumeration flag specifying the output format of the created result report.
	def publishTestResults(self, tbTestSuiteInstance, resultLocation, outputFormat):
		#TBTAFPublisher tbtafPublisherInstance
		#tbtafPublisherInstance.PublishTestResults(tbTestSuiteInstance, resultLocation, outputFormat)

	#tbTestSuiteInstance - Reference to a given TBTestSuite instance from which the result report will be generated.
    #tbTestSuiteInstance - Reference of the TBTestSuite representing the set of tests that will be executed.
    #urlCollection - Optional collection of URLs corresponding to the nodes on which the test execution will be distributed.
    #flagsCollection - Optional collection of flags that can customize the execution of such TBTestSuite.
    #executorListenerCollection - Optional collection of ExecutorListener implementations that would want to listen and react to specific events during the execution of that specific TBTestSuite
	def excuteTestSuite(self, tbTestSuiteInstance, urlCollection, flagsCollection, executorListenerCollection):
		#TBTAFExecutor tbtafExecutorInstance
		return tbtafExecutorInstance.ExecuteTests(tbTestSuiteInstance, urlCollection, flagsCollection, executorListenerCollection)

    #projectName - String identifying the Project from where the query is being made.
    #tagList - Optional List of tags in order to filter the query being made.
    #indicatorEnumeration - If the list of tags is provided, then an additional enumeration can be passed in order to specify if the tags will be used to filter IN or OUT the results found.
	def GetTests(projectName, tagList, inOutList)

		#If the parameters being passed are null or contain null then an Invalid Argument Exception will be thrown.
		#If the provided Project key does not exist on the currently connected datasource, then a Not Existing Project Exception will be thrown.
		#If the provided filtering enumeration is provided, then a Not Supported Filter Exception will be thrown.

		return _tBTestSuiteInstance.getTestCases(tagList, inOutList)

    def getEventSource(self):
        return self.eventSource<|endoftext|>


<commit_before>""" Defines the basic Container class """
import warnings

from enable.base import empty_rectangle, intersect_bounds
from enable.events import MouseEvent
from kiva import affine
from traits.api import Bool, HasStrictTraits, Instance, List, Property, Tuple

from .component import Component

class AbstractResolver(HasStrictTraits):
    """
    A Resolver traverses a component DB and matches a specifier.
    """

    def match(self, db, query):
        """ Queries a component DB using a dict of keyword-val conditions.
        Each resolver defines its set of allowed keywords.
        """
        raise NotImplementedError

class Container(Component):
    """
    A Container is a logical container that holds other Components within it and
    provides an origin for Components to position themselves.  Containers can
    be "nested" (although "overlayed" is probably a better term).

    If auto_size is True, the container will automatically update its bounds to
    enclose all of the components handed to it, so that a container's bounds
    serve as abounding box (although not necessarily a minimal bounding box) of
    its contained components.
    """

    # The list of components within this frame
    components = Property    # List(Component)

    # If true, the container get events before its children.  Otherwise, it
    # gets them afterwards.
    intercept_events = Bool(True)

    # Whether or not the container should auto-size itself to fit all of its
    # components.
    auto_size = Bool(False)

    # The default size of this container if it is empty.
    default_size = Tuple(0, 0)

    # The layers that the container will draw first, so that they appear
    # under the component layers of the same name.
    container_under_layers = Tuple('background', 'underlay')

    # The layers that the container will draw last, so that they appear
    # over the component layers of the same name.
    container_over_layers = Tuple('overlay', 'border')

    #------------------------------------------------------------------------
    # DOM-related traits
    # (Note: These are unused as of 8/13/2007)
    #------------------------------------------------------------------------

    # This object resolves queries for components
    resolver = Instance(AbstractResolver)

    #------------------------------------------------------------------------
    # Private traits
    #------------------------------------------------------------------------

    # Shadow trait for self.components
    _components = List    # List(Component)

    # Set of components that last handled a mouse event.  We keep track of
    # this so that we can generate mouse_enter and mouse_leave events of
    # our own.
    _prev_event_handlers = Instance(set, ())

    #------------------------------------------------------------------------
    # Public methods
    #------------------------------------------------------------------------

    def __init__(self, *components, **traits):
        Component.__init__(self, **traits)
        for component in components:
            self.add(component)
        if 'bounds' in traits.keys() and 'auto_size' not in traits.keys():
            self.auto_size = False

        if 'intercept_events' in traits:
            warnings.warn("'intercept_events' is a deprecated trait",
                    warnings.DeprecationWarning)
        return

    def add(self, *components):
        """ Adds components to this container """
        for component in components:
            if component.container is not None:
                component.container.remove(component)
            component.container = self
        self._components.extend(components)

    def components_at(self, x, y):
        """
        Returns a list of the components underneath the given point (given in
        the parent coordinate frame of this container).
        """
        result = []
        if self.is_in(x,y):
            xprime = x - self.position[0]
            yprime = y - self.position[1]
            for component in self._components[::-1]:
                if component.is_in(xprime, yprime):
                    result.append(component)
        return result

    def get(self, **kw):
        """
        Allows for querying of this container's components.
        """
        # TODO: cache requests
        return self.resolver.query(self._components, kw)

    def cleanup(self, window):
        """When a window viewing or containing a component is destroyed,
        cleanup is called on the component to give it the opportunity to
        delete any transient state it may have (such as backbuffers)."""
        if self._components:
            for component in self._components:
                component.cleanup(window)

    #------------------------------------------------------------------------
    # Protected methods
    #------------------------------------------------------------------------

    def _dispatch_draw(self, layer, gc, view_bounds):
        """ Renders the named *layer* of this component.
        """
        new_bounds = self._transform_view_bounds(view_bounds)
        if new_bounds == empty_rectangle:
            return

        if self.layout_needed:
            self.do_layout()

        # Give the container a chance to draw first for the layers that are
        # considered "under" or "at" the main layer level
        if layer in self.container_under_layers:
            self._draw_container_layer(layer, gc, view_bounds)

        # Now transform coordinates and draw the children
        visible_components = self._get_visible_components(new_bounds)
        if visible_components:
            with gc:
                gc.translate_ctm(*self.position)
                for component in visible_components:
                    component._dispatch_draw(layer, gc, new_bounds)

        if layer in self.container_over_layers:
            self._draw_container_layer(layer, gc, view_bounds)

    def _draw_container_layer(self, layer, gc, view_bounds):
            draw = getattr(self, '_draw_container_' + layer, None)
            if draw:
                draw(gc, view_bounds)

    def _draw_container(self, gc):
        "Draw the container background in a specified graphics context"
        pass

    def _draw_container_background(self, gc, view_bounds=None):
        self._draw_background(gc, view_bounds)

    def _draw_container_overlay(self, gc, view_bounds=None):
        self._draw_overlay(gc, view_bounds)

    def _draw_container_underlay(self, gc, view_bounds=None):
        self._draw_underlay(gc, view_bounds)

    def _draw_container_border(self, gc, view_bounds=None):
        self._draw_border(gc, view_bounds)

    def _get_visible_components(self, bounds):
        """ Returns a list of this plot's children that are in the bounds. """
        if bounds is None:
            return [c for c in self.components if c.visible]

        visible_components = []
        for component in self.components:
            if not component.visible:
                continue
            tmp = intersect_bounds(component.outer_position +
                                   component.outer_bounds, bounds)
            if tmp != empty_rectangle:
                visible_components.append(component)
        return visible_components

    def _should_layout(self, component):
        """ Returns True if it is appropriate for the container to lay out
        the component; False if not.
        """
        if not component or \
            (not component.visible and not component.invisible_layout):
            return False
        else:
            return True

    def _transform_view_bounds(self, view_bounds):
        """
        Transforms the given view bounds into our local space and computes a new
        region that can be handed off to our children.  Returns a 4-tuple of
        the new position+bounds, or None (if None was passed in), or the value
        of empty_rectangle (from enable.base) if the intersection resulted
        in a null region.
        """
        # Check if we are visible
        tmp = intersect_bounds(self.position + self.bounds, view_bounds)
        if tmp == empty_rectangle:
            return empty_rectangle
        # Compute new_bounds, which is the view_bounds transformed into
        # our coordinate space
        v = view_bounds
        new_bounds = (v[0]-self.x, v[1]-self.y, v[2], v[3])
        return new_bounds

    def _component_bounds_changed(self, component):
        "Called by contained objects when their bounds change"
        pass

    #------------------------------------------------------------------------
    # Property setters & getters
    #------------------------------------------------------------------------

    def _get_components(self):
        return self._components

    def _get_layout_needed(self):
        # Override the parent implementation to take into account whether any
        # of our contained components need layout.
        if self._layout_needed:
            return True
        else:
            for c in self.components:
                if c.layout_needed:
                    return True
            else:
                return False

    #------------------------------------------------------------------------
    # Event handling
    #------------------------------------------------------------------------

    def get_event_transform(self, event=None, suffix=""):
        return affine.affine_from_translation(-self.x, -self.y)

    def dispatch(self, event, suffix):
        """
        Dispatches a mouse event based on the current event_state.  Overrides
        the default Interactor.dispatch by adding some default
        behavior to send all events to our contained children.

        "suffix" is the name of the mouse event as a suffix to the event state
        name, e.g. "_left_down" or "_window_enter".
        """
        if not event.handled:
            components = self.components_at(event.x, event.y)

            # Translate the event's location to be relative to this container
            transform = self.get_event_transform(event, suffix)
            event.push_transform(transform, caller=self)

            try:
                component_set = set(components)

                components_left = self._prev_event_handlers - component_set
                self._notify_if_mouse_event(components_left, event,
                                            'mouse_leave')

                if suffix != 'mouse_leave':
                    components_entered = (component_set
                                          - self._prev_event_handlers)
                    self._notify_if_mouse_event(components_entered, event,
                                                    'mouse_enter')
                # Handle the actual event
                # Only add event handlers to the list of previous event handlers
                # if they actually receive the event
                self._prev_event_handlers = set()
                for component in components:
                    component.dispatch(event, suffix)
                    self._prev_event_handlers.add(component)
                    if event.handled:
                        break
            finally:
                event.pop(caller=self)

            if not event.handled:
                super(Container, self).dispatch(event, suffix)

    def _notify_if_mouse_event(self, components, event, suffix):
        if len(components) == 0:
            return

        if isinstance(event, MouseEvent):
            for component in components:
                component.dispatch(event, suffix)
                event.handled = False

    #------------------------------------------------------------------------
    # Event handlers
    #------------------------------------------------------------------------

    def _bounds_changed(self, old, new):
        # crappy... calling our parent's handler seems like a common traits
        # event handling problem
        super(Container, self)._bounds_changed(old, new)
        self._layout_needed = True

    def __components_items_changed(self, event):
        self._layout_needed = True
<commit_msg>Refactor `Container.dispatch`<commit_after>""" Defines the basic Container class """
import warnings
from contextlib import contextmanager

from enable.base import empty_rectangle, intersect_bounds
from enable.events import MouseEvent
from kiva import affine
from traits.api import Bool, HasStrictTraits, Instance, List, Property, Tuple

from .component import Component

class AbstractResolver(HasStrictTraits):
    """
    A Resolver traverses a component DB and matches a specifier.
    """

    def match(self, db, query):
        """ Queries a component DB using a dict of keyword-val conditions.
        Each resolver defines its set of allowed keywords.
        """
        raise NotImplementedError

class Container(Component):
    """
    A Container is a logical container that holds other Components within it and
    provides an origin for Components to position themselves.  Containers can
    be "nested" (although "overlayed" is probably a better term).

    If auto_size is True, the container will automatically update its bounds to
    enclose all of the components handed to it, so that a container's bounds
    serve as abounding box (although not necessarily a minimal bounding box) of
    its contained components.
    """

    # The list of components within this frame
    components = Property    # List(Component)

    # If true, the container get events before its children.  Otherwise, it
    # gets them afterwards.
    intercept_events = Bool(True)

    # Whether or not the container should auto-size itself to fit all of its
    # components.
    auto_size = Bool(False)

    # The default size of this container if it is empty.
    default_size = Tuple(0, 0)

    # The layers that the container will draw first, so that they appear
    # under the component layers of the same name.
    container_under_layers = Tuple('background', 'underlay')

    # The layers that the container will draw last, so that they appear
    # over the component layers of the same name.
    container_over_layers = Tuple('overlay', 'border')

    #------------------------------------------------------------------------
    # DOM-related traits
    # (Note: These are unused as of 8/13/2007)
    #------------------------------------------------------------------------

    # This object resolves queries for components
    resolver = Instance(AbstractResolver)

    #------------------------------------------------------------------------
    # Private traits
    #------------------------------------------------------------------------

    # Shadow trait for self.components
    _components = List    # List(Component)

    # Set of components that last handled a mouse event.  We keep track of
    # this so that we can generate mouse_enter and mouse_leave events of
    # our own.
    _prev_event_handlers = Instance(set, ())

    #------------------------------------------------------------------------
    # Public methods
    #------------------------------------------------------------------------

    def __init__(self, *components, **traits):
        Component.__init__(self, **traits)
        for component in components:
            self.add(component)
        if 'bounds' in traits.keys() and 'auto_size' not in traits.keys():
            self.auto_size = False

        if 'intercept_events' in traits:
            warnings.warn("'intercept_events' is a deprecated trait",
                    warnings.DeprecationWarning)
        return

    def add(self, *components):
        """ Adds components to this container """
        for component in components:
            if component.container is not None:
                component.container.remove(component)
            component.container = self
        self._components.extend(components)

    def components_at(self, x, y):
        """
        Returns a list of the components underneath the given point (given in
        the parent coordinate frame of this container).
        """
        result = []
        if self.is_in(x,y):
            xprime = x - self.position[0]
            yprime = y - self.position[1]
            for component in self._components[::-1]:
                if component.is_in(xprime, yprime):
                    result.append(component)
        return result

    def get(self, **kw):
        """
        Allows for querying of this container's components.
        """
        # TODO: cache requests
        return self.resolver.query(self._components, kw)

    def cleanup(self, window):
        """When a window viewing or containing a component is destroyed,
        cleanup is called on the component to give it the opportunity to
        delete any transient state it may have (such as backbuffers)."""
        if self._components:
            for component in self._components:
                component.cleanup(window)

    #------------------------------------------------------------------------
    # Protected methods
    #------------------------------------------------------------------------

    def _dispatch_draw(self, layer, gc, view_bounds):
        """ Renders the named *layer* of this component.
        """
        new_bounds = self._transform_view_bounds(view_bounds)
        if new_bounds == empty_rectangle:
            return

        if self.layout_needed:
            self.do_layout()

        # Give the container a chance to draw first for the layers that are
        # considered "under" or "at" the main layer level
        if layer in self.container_under_layers:
            self._draw_container_layer(layer, gc, view_bounds)

        # Now transform coordinates and draw the children
        visible_components = self._get_visible_components(new_bounds)
        if visible_components:
            with gc:
                gc.translate_ctm(*self.position)
                for component in visible_components:
                    component._dispatch_draw(layer, gc, new_bounds)

        if layer in self.container_over_layers:
            self._draw_container_layer(layer, gc, view_bounds)

    def _draw_container_layer(self, layer, gc, view_bounds):
            draw = getattr(self, '_draw_container_' + layer, None)
            if draw:
                draw(gc, view_bounds)

    def _draw_container(self, gc):
        "Draw the container background in a specified graphics context"
        pass

    def _draw_container_background(self, gc, view_bounds=None):
        self._draw_background(gc, view_bounds)

    def _draw_container_overlay(self, gc, view_bounds=None):
        self._draw_overlay(gc, view_bounds)

    def _draw_container_underlay(self, gc, view_bounds=None):
        self._draw_underlay(gc, view_bounds)

    def _draw_container_border(self, gc, view_bounds=None):
        self._draw_border(gc, view_bounds)

    def _get_visible_components(self, bounds):
        """ Returns a list of this plot's children that are in the bounds. """
        if bounds is None:
            return [c for c in self.components if c.visible]

        visible_components = []
        for component in self.components:
            if not component.visible:
                continue
            tmp = intersect_bounds(component.outer_position +
                                   component.outer_bounds, bounds)
            if tmp != empty_rectangle:
                visible_components.append(component)
        return visible_components

    def _should_layout(self, component):
        """ Returns True if it is appropriate for the container to lay out
        the component; False if not.
        """
        if not component or \
            (not component.visible and not component.invisible_layout):
            return False
        else:
            return True

    def _transform_view_bounds(self, view_bounds):
        """
        Transforms the given view bounds into our local space and computes a new
        region that can be handed off to our children.  Returns a 4-tuple of
        the new position+bounds, or None (if None was passed in), or the value
        of empty_rectangle (from enable.base) if the intersection resulted
        in a null region.
        """
        # Check if we are visible
        tmp = intersect_bounds(self.position + self.bounds, view_bounds)
        if tmp == empty_rectangle:
            return empty_rectangle
        # Compute new_bounds, which is the view_bounds transformed into
        # our coordinate space
        v = view_bounds
        new_bounds = (v[0]-self.x, v[1]-self.y, v[2], v[3])
        return new_bounds

    def _component_bounds_changed(self, component):
        "Called by contained objects when their bounds change"
        pass

    #------------------------------------------------------------------------
    # Property setters & getters
    #------------------------------------------------------------------------

    def _get_components(self):
        return self._components

    def _get_layout_needed(self):
        # Override the parent implementation to take into account whether any
        # of our contained components need layout.
        if self._layout_needed:
            return True
        else:
            for c in self.components:
                if c.layout_needed:
                    return True
            else:
                return False

    #------------------------------------------------------------------------
    # Event handling
    #------------------------------------------------------------------------

    def get_event_transform(self, event=None):
        return affine.affine_from_translation(-self.x, -self.y)

    def dispatch(self, event, suffix):
        """ Dispatches mouse event to child components until it is handled.

        Parameters
        ----------
        event : BaseEvent
            A mouse or key event.
        suffix : string
            The name of the mouse event as a suffix to the event state name,
            e.g. "left_down" or "window_enter".
        """
        if event.handled:
            return

        with self._local_event_transform(event):
            components = self.components_at(event.x, event.y)
            component_set = set(components)

            components_left = self._prev_event_handlers - component_set
            self._notify_if_mouse_event(components_left, event, 'mouse_leave')

            if suffix != 'mouse_leave':
                components_entered = component_set - self._prev_event_handlers
                self._notify_if_mouse_event(components_entered,
                                            event, 'mouse_enter')
            # Dispatch event and add event handlers to the list of previous
            # event handlers if they actually receive the event.
            self._prev_event_handlers = set()
            for component in components:
                component.dispatch(event, suffix)
                self._prev_event_handlers.add(component)
                if event.handled:
                    break

        if not event.handled:
            super(Container, self).dispatch(event, suffix)

    @contextmanager
    def _local_event_transform(self, event):
        # Translate the event's location to be relative to this container.
        try:
            transform = self.get_event_transform(event)
            event.push_transform(transform, caller=self)
            yield
        finally:
            event.pop(caller=self)

    def _notify_if_mouse_event(self, components, event, suffix):
        if len(components) == 0:
            return

        if isinstance(event, MouseEvent):
            for component in components:
                component.dispatch(event, suffix)
                event.handled = False

    #------------------------------------------------------------------------
    # Event handlers
    #------------------------------------------------------------------------

    def _bounds_changed(self, old, new):
        # crappy... calling our parent's handler seems like a common traits
        # event handling problem
        super(Container, self)._bounds_changed(old, new)
        self._layout_needed = True

    def __components_items_changed(self, event):
        self._layout_needed = True
<|endoftext|>