Example #1
1
    def by(self, name, index=None, field=None):
        """ facet by given function

            Examples:

            .by(index=lambda x: x)
            .by(field=lambda x: ('T_high' if x['T'] > 1000 else 'T_low'))
        """
        if index:
            ret = OrderedDict(
                [
                    (index(val), self[self.index.get_level_values(name) == val])
                    for val in self.index.get_level_values(name)
                ]
            )
            for _ in ret.itervalues():
                _.properties = self.properties
            return MultiFrame(ret)
        else:
            self["cat"] = map(field, self[name])
            cats = self.groupby("cat").groups.keys()
            ret = OrderedDict([(cat, self[self["cat"] == cat]) for cat in cats])
            for _ in ret.itervalues():
                _.properties = self.properties
            return MultiFrame(ret)
Example #2
1
    def process_static_data(cls, data):
        # create instances
        ret = OrderedDict()
        for t in sorted(data, key=lambda x: x["name"]):
            name = str(t["name"])
            ret[name] = Type(name, t["effectiveAgainst"], t["weakAgainst"])

        # additional manipulations
        size = len(ret)
        by_effectiveness = {}
        by_resistance = {}
        for t in ret.itervalues():  # type: Type
            t.attack_effective_against = [ret[name] for name in t.attack_effective_against]
            t.attack_weak_against = [ret[name] for name in t.attack_weak_against]

            # group types effective against, weak against specific types
            for l, d in (t.attack_effective_against, by_effectiveness), (t.attack_weak_against, by_resistance):
                for tt in l:
                    if tt not in d:
                        d[tt] = set()
                    d[tt].add(t)

            # calc average factor for damage of this type relative to all types
            t.rate = (
                size
                + ((EFFECTIVENESS_FACTOR - 1) * len(t.attack_effective_against))
                - ((1 - RESISTANCE_FACTOR) * len(t.attack_weak_against))
            ) / size

        # set pokemon type resistance/weakness info
        for t in ret.itervalues():  # type: Type
            t.pokemon_resistant_to = by_resistance[t]
            t.pokemon_vulnerable_to = by_effectiveness[t]

        return ret
Example #3
1
 def run_cloners(cls, old_event, new_event):
     selected = {x[7:] for x in request.values.getlist("cloners") if x.startswith("cloner_")}
     all_cloners = OrderedDict((name, cloner_cls(old_event)) for name, cloner_cls in get_event_cloners().iteritems())
     if any(cloner.is_internal for name, cloner in all_cloners.iteritems() if name in selected):
         raise Exception("An internal cloner was selected")
     # enable internal cloners that are enabled by default or required by another cloner
     selected |= {
         c.name
         for c in all_cloners.itervalues()
         if c.is_internal and (c.is_default or c.required_by_deep & selected)
     }
     # enable unavailable cloners that may be pulled in as a dependency nonetheless
     extra = {
         c.name
         for c in all_cloners.itervalues()
         if not c.is_available and c.always_available_dep and c.required_by_deep & selected
     }
     selected |= extra
     active_cloners = OrderedDict((name, cloner) for name, cloner in all_cloners.iteritems() if name in selected)
     if not all(
         (c.is_internal or c.is_visible) and c.is_available
         for c in active_cloners.itervalues()
         if c.name not in extra
     ):
         raise Exception("An invisible/unavailable cloner was selected")
     for name, cloner in active_cloners.iteritems():
         if not (selected >= cloner.requires_deep):
             raise Exception("Cloner {} requires {}".format(name, ", ".join(cloner.requires_deep - selected)))
     shared_data = {}
     cloner_names = set(active_cloners)
     for name, cloner in active_cloners.iteritems():
         shared_data[name] = cloner.run(new_event, cloner_names, cloner._prepare_shared_data(shared_data))
Example #4
1
class ResourceLoader(object):
    def __init__(self):
        self.resources = OrderedDict()

    def require(self, name):
        if name in self.resources:
            return

        resource = resources.get(name)
        if not resource:
            raise LoaderError("There is no resource by the name %s." % name)
        for requirement in resource.requirements:
            self.require(requirement)
        self.resources[resource.name] = resource

    @property
    def styles(self):
        s = HtmlList()
        for resource in self.resources.itervalues():
            for style in resource.styles:
                s.append(Style(**style))
        return s

    @property
    def scripts(self):
        s = HtmlList()
        for resource in self.resources.itervalues():
            for script in resource.scripts:
                s.append(Script(**script))
        return s
Example #5
1
    def add_leveled_toc_items(self):
        added = OrderedDict()
        added2 = OrderedDict()
        counter = 1

        def find_matches(expr, doc):
            try:
                ans = XPath(expr)(doc)
                len(ans)
                return ans
            except:
                self.log.warn("Invalid ToC expression, ignoring: %s" % expr)
                return []

        for document in self.oeb.spine:
            previous_level1 = list(added.itervalues())[-1] if added else None
            previous_level2 = list(added2.itervalues())[-1] if added2 else None

            for elem in find_matches(self.opts.level1_toc, document.data):
                text, _href = self.elem_to_link(document, elem, counter)
                counter += 1
                if text:
                    node = self.oeb.toc.add(text, _href, play_order=self.oeb.toc.next_play_order())
                    added[elem] = node
                    # node.add(_('Top'), _href)

            if self.opts.level2_toc is not None and added:
                for elem in find_matches(self.opts.level2_toc, document.data):
                    level1 = None
                    for item in document.data.iterdescendants():
                        if item in added:
                            level1 = added[item]
                        elif item == elem:
                            if level1 is None:
                                if previous_level1 is None:
                                    break
                                level1 = previous_level1
                            text, _href = self.elem_to_link(document, elem, counter)
                            counter += 1
                            if text:
                                added2[elem] = level1.add(text, _href, play_order=self.oeb.toc.next_play_order())
                            break

                if self.opts.level3_toc is not None and added2:
                    for elem in find_matches(self.opts.level3_toc, document.data):
                        level2 = None
                        for item in document.data.iterdescendants():
                            if item in added2:
                                level2 = added2[item]
                            elif item == elem:
                                if level2 is None:
                                    if previous_level2 is None:
                                        break
                                    level2 = previous_level2
                                text, _href = self.elem_to_link(document, elem, counter)
                                counter += 1
                                if text:
                                    level2.add(text, _href, play_order=self.oeb.toc.next_play_order())
                                break
Example #6
1
class ForgetfulStorage(object):
    def __init__(self, ttl=604800):
        """
		Implements a storage class for distributed log messages.
		Default max age is a week.
		"""
        self.data = OrderedDict()
        self.ttl = ttl

    def __setitem__(self, key, value):
        # 		if key in self.data:
        # 			self.data[key] = (time.time(),value)
        self.data[key] = (time.time(), value)
        self.cull()

    def __getitem__(self, key):
        self.cull()
        return self.data[key][1]

    def __iter__(self):
        self.cull()
        return iter(self.data)

    def __repr__(self):
        self.cull()
        return repr(self.data)

    def get(self, key, default=None):
        self.cull()
        if key in self.data:
            return self[key]
        return default

    def cull(self):
        """
		Note that it may be useful to track what we evict.
		"""
        for k, v in self.iteritems_older_than(self.ttl):
            self.data.popitem(last=False)

    def iteritems_older_than(self, seconds_old):
        min_birthday = time.time() - seconds_old
        zipped = self._triple_iterable()
        matches = takewhile(lambda r: min_birthday >= r[1], zipped)
        return imap(operator.itemgetter(0, 2), matches)

    def _triple_iterable(self):
        ikeys = self.data.iterkeys()
        ibirthday = imap(operator.itemgetter(0), self.data.itervalues())
        ivalues = imap(operator.itemgetter(1), self.data.itervalues())
        return izip(ikeys, ibirthday, ivalues)

    def iteritems(self):
        self.cull()
        ikeys = self.data.iterkeys()
        ivalues = imap(operator.itemgetter(1), self.data.itervalues())
        return izip(ikeys, ivalues)
Example #7
1
class ForgetfulStorage(object):
    implements(IStorage)

    def __init__(self, ttl=604800):
        """
        By default, max age is a week.
        """
        self.data = OrderedDict()
        self.ttl = ttl

    def __setitem__(self, key, value):
        if key in self.data:
            del self.data[key]
        self.data[key] = (time.time(), value)
        self.cull()

    def cull(self):
        for k, v in self.iteritemsOlderThan(self.ttl):
            self.data.popitem(last=False)

    def get(self, key, default=None):
        self.cull()
        if key in self.data:
            return False, self[key]
        return False, default

    def __getitem__(self, key):
        self.cull()
        return self.data[key][1]

    def __iter__(self):
        self.cull()
        return iter(self.data)

    def __repr__(self):
        self.cull()
        return repr(self.data)

    def iteritemsOlderThan(self, secondsOld):
        minBirthday = time.time() - secondsOld
        zipped = self._tripleIterable()
        matches = takewhile(lambda r: minBirthday >= r[1], zipped)
        return imap(operator.itemgetter(0, 2), matches)

    def _tripleIterable(self):
        ikeys = self.data.iterkeys()
        ibirthday = imap(operator.itemgetter(0), self.data.itervalues())
        ivalues = imap(operator.itemgetter(1), self.data.itervalues())
        return izip(ikeys, ibirthday, ivalues)

    def iteritems(self):
        self.cull()
        ikeys = self.data.iterkeys()
        ivalues = imap(operator.itemgetter(1), self.data.itervalues())
        return izip(ikeys, ivalues)
def smallest_subarray_covering_set(A, Q):
    """Find smallest range (i,j) s.t. all q in Q are in A[i..j]."""

    # handle 0-length covering
    if not Q:
        return 0, -1

    # start with no best and an empty candidate covering range
    Q = set(Q)  # want O(1) membership test
    min_size = None
    min_covering_range = None
    cand_locs = OrderedDict()

    # stream elements of A, maintaining the shortest covering range
    # that ends at the current element
    for j, elem in enumerate(A):

        # skip elements that can't contribute to a covering
        if elem not in Q:
            continue

        # extend the candidate range with the current elem,
        # removing any previous instance of the same elem
        cand_locs.pop(elem, None)
        cand_locs[elem] = j  # will be added in final position

        # if the new candidate is legal and better, make it the current best
        if len(cand_locs) == len(Q):
            i = cand_locs.itervalues().next()  # get front position, O(1) time
            if min_size is None or j - i < min_size:
                min_size = j - i
                min_covering_range = i, j

    return min_covering_range
Example #9
1
    def check_for_add_to_toolbars(self, plugin):
        from calibre.gui2.preferences.toolbar import ConfigWidget
        from calibre.customize import InterfaceActionBase

        if not isinstance(plugin, InterfaceActionBase):
            return

        all_locations = OrderedDict(ConfigWidget.LOCATIONS)
        plugin_action = plugin.load_actual_plugin(self.gui)
        installed_actions = OrderedDict([(key, list(gprefs.get("action-layout-" + key, []))) for key in all_locations])

        # If already installed in a GUI container, do nothing
        for action_names in installed_actions.itervalues():
            if plugin_action.name in action_names:
                return

        allowed_locations = [
            (key, text) for key, text in all_locations.iteritems() if key not in plugin_action.dont_add_to
        ]
        if not allowed_locations:
            return  # This plugin doesn't want to live in the GUI

        from calibre.gui2.dialogs.choose_plugin_toolbars import ChoosePluginToolbarsDialog

        d = ChoosePluginToolbarsDialog(self, plugin_action, allowed_locations)
        if d.exec_() == d.Accepted:
            for key, text in d.selected_locations():
                installed_actions = list(gprefs.get("action-layout-" + key, []))
                installed_actions.append(plugin_action.name)
                gprefs["action-layout-" + key] = tuple(installed_actions)
Example #10
0
    class View(object):
        """ Presents a view of the set of `Option` arguments to a search command

        TODO: Description

        """

        def __init__(self, command):
            self._items = OrderedDict(
                [
                    (option.name, Option.Item(command, option))
                    for member_name, option in type(command).option_definitions
                ]
            )
            return

        def __contains__(self, name):
            return name in self._items

        def __getitem__(self, name):
            return self._items[name]

        def __iter__(self):
            return self._items.__iter__()

        def __len__(self):
            return len(self._items)

        def __repr__(self):
            text = "".join(["Option.View(", ",".join([repr(item) for item in self.itervalues()]), ")"])
            return text

        def __str__(self):
            text = " ".join([str(item) for item in self.itervalues() if item.is_set])
            return text

        # region Methods

        def get_missing(self):
            missing = [item.name for item in self._items.itervalues() if item.is_required and not item.is_set]
            return missing if len(missing) > 0 else None

        def iteritems(self):
            return self._items.iteritems()

        def iterkeys(self):
            return self.__iter__()

        def itervalues(self):
            return self._items.itervalues()

        def reset(self):
            for value in self.itervalues():
                value.reset()
            return
Example #11
0
class Region(object):
    """ Holds a region's layers along the blade.

    :param layers: Dictionary of Layer3D objects
    :type layers: dict
    """

    def __init__(self):
        self.layers = OrderedDict()

    def add_layer(self, name):
        """ Inserts a layer into layers dict.

        :param name: Name of the material
        :return: The layer added to the region
        """
        dubl = 0
        for k in self.layers.iterkeys():
            if name == k[:-2]:
                dubl += 1

        lname = "%s%02d" % (name, dubl)

        layer = Layer()
        self.layers[lname] = layer
        return layer

    def init_stack(self):
        """ Initiates thickness and angle matrices of the region plus the max
            thickness of each layer
        :return: thick_matrix: 2d np.array containing thicknesses of all layers (size: no_layers,s)
        :return: thick_max: 1d np.array max thickness per layer
        :return: angle_matrix: 2d np.array containing angles of all layers (size: no_layers,s)
        """
        thickmatdata = []
        for v in self.layers.itervalues():
            thickmatdata.append(v.thickness)
        self.thick_matrix = np.fliplr(np.rot90(np.array(thickmatdata), -1))

        thickmaxdata = []
        for l in range(self.thick_matrix.shape[1]):
            thickmaxdata.append(np.max(self.thick_matrix[:, l]))
        self.thick_max = np.array(thickmaxdata)

        anglematdata = []
        for v in self.layers.itervalues():
            anglematdata.append(v.angle)
        self.angle_matrix = np.fliplr(np.rot90(np.array(anglematdata), -1))

        return self.thick_matrix, self.thick_max, self.angle_matrix
Example #12
0
    def _make_raw_brackets_from_divisions(self):
        """Returns an OrderedDict mapping bracket names (normally numbers)
        to lists."""
        brackets = OrderedDict()
        teams = list(self.teams)
        for team in teams:
            # Converting from bracket's name to a float (so it can pretend to be a Bracket)
            division = float(team.division.name)
            if division in brackets:
                brackets[division].append(team)
            else:
                brackets[division] = [team]

        print "------"

        # Assigning bye teams as needed
        for bracket in brackets.itervalues():
            if len(bracket) % 2 != 0:
                from debate.models import Institution,Team
                bye_tournament = bracket[0].tournament
                bye_institution, created = Institution.objects.get_or_create(
                    name="Byes"
                )
                bye_reference = "Bye %s" % bracket[0].division
                bye_division = bracket[0].division
                bye_team = Team(
                    institution = bye_institution,
                    reference = bye_reference,
                    short_reference = "Bye",
                    tournament= bye_tournament,
                    type = "B",
                    use_institution_prefix = False,
                    division = bye_division,
                    cannot_break = True
                )
                bye_team.aff_count = 0
                bye_team.neg_count = 0
                bye_team.save()
                bracket.append(bye_team)
                print "\t Created a bye team for divison %s" % bracket[0].division

        # Assigning subranks - fixed based on alphabetical
        for bracket in brackets.itervalues():
            bracket.sort(key=lambda x: x.short_name, reverse=False)
            for i, team in enumerate(bracket):
                i += 1
                team.subrank = i

        return brackets
Example #13
0
    def add_leveled_toc_items(self):
        added = OrderedDict()
        added2 = OrderedDict()
        counter = 1
        for document in self.oeb.spine:
            previous_level1 = list(added.itervalues())[-1] if added else None
            previous_level2 = list(added2.itervalues())[-1] if added2 else None

            for elem in XPath(self.opts.level1_toc)(document.data):
                text, _href = self.elem_to_link(document, elem, counter)
                counter += 1
                if text:
                    node = self.oeb.toc.add(text, _href, play_order=self.oeb.toc.next_play_order())
                    added[elem] = node
                    # node.add(_('Top'), _href)

            if self.opts.level2_toc is not None and added:
                for elem in XPath(self.opts.level2_toc)(document.data):
                    level1 = None
                    for item in document.data.iterdescendants():
                        if item in added:
                            level1 = added[item]
                        elif item == elem:
                            if level1 is None:
                                if previous_level1 is None:
                                    break
                                level1 = previous_level1
                            text, _href = self.elem_to_link(document, elem, counter)
                            counter += 1
                            if text:
                                added2[elem] = level1.add(text, _href, play_order=self.oeb.toc.next_play_order())
                            break

                if self.opts.level3_toc is not None and added2:
                    for elem in XPath(self.opts.level3_toc)(document.data):
                        level2 = None
                        for item in document.data.iterdescendants():
                            if item in added2:
                                level2 = added2[item]
                            elif item == elem:
                                if level2 is None:
                                    if previous_level2 is None:
                                        break
                                    level2 = previous_level2
                                text, _href = self.elem_to_link(document, elem, counter)
                                counter += 1
                                if text:
                                    level2.add(text, _href, play_order=self.oeb.toc.next_play_order())
                                break
Example #14
0
class Table(object):
    def __init__(self, notset=None):
        self.headers = OrderedDict()
        self.data = []
        self.notset = notset

    def get_header_id(self, k):
        try:
            return self.headers[k]
        except KeyError:
            header_id = self.headers[k] = len(self.headers)
            return header_id

    def add(self, data):
        row = {}
        for k, v in data.iteritems():
            row[self.get_header_id(k)] = v
        self.data.append(row)

    def rows(self):
        for row in self.data:
            l = []
            for index in self.headers.itervalues():
                l.append(row.get(index, self.notset))
            yield tuple(l)

    def dataset(self):
        dataset = tablib.Dataset(headers=self.headers.keys())
        for row in self.rows():
            dataset.append(row)
        return dataset
class FieldSet(object):
    def __init__(self, delegate, schema_type):
        from pygtkhelpers.proxy import ProxyGroup, proxy_for

        self.delegate = delegate
        self.schema = schema_type()
        self.proxies = ProxyGroup()
        self.fields = OrderedDict()
        self.proxies.connect("changed", self._on_proxies_changed)
        for name, element in self.schema.items():
            self._setup_widget(name, element)

    def _setup_widget(self, name, element):
        widget = getattr(self.delegate, name, None)
        # XXX (AA) this will always be the case, we are running too soon
        if widget is None:
            widget = widget_for(element)
            setattr(self.delegate, name, widget)
        field = self.fields[name] = Field(element, widget=widget)
        field_name = element.properties.get("label", name)
        field.set_label(field_name.capitalize())
        self.proxies.add_proxy(name, field.proxy)

    def _on_proxies_changed(self, group, proxy, name, value):
        self.schema[name].set(value)

    def layout_as_table(self):
        # XXX: turn to utility function
        table = gtk.Table(len(self.fields), 2)
        table.set_row_spacings(6)
        table.set_col_spacings(6)
        table.set_border_width(6)
        for i, field_i in enumerate(self.fields.itervalues()):
            field_i.layout_as_table(table, i)
        return table
Example #16
0
    def getDeviceShortNames(self, devicepaths, pciids):
        shortnames = OrderedDict()
        namecount = {}
        try:
            # Initialise PciIdsParser, throws customExceptions.PciIdsFileNotFound
            # if fail
            pciIdsParser = parseutil.PciIdsParser(pciids)
        except customExceptions.PciIdsFileNotFound:
            message.addError(
                "pci.ids file could not be located in tool "
                "directory: %s. " % paths.CURRENTDIR + "Device "
                "names could not be obtained. Please ensure that "
                "the file is in the directory.",
                False,
            )
        else:

            for devicepath in devicepaths:
                # Add entry to dictionary shortnames
                shortnames[devicepath] = self.getClassName(devicepath, pciIdsParser)

            namelist = []
            for value in shortnames.itervalues():
                namelist.append(value)

            listnumberer = util.ListNumberer(namelist)
            for devicepath in shortnames.iterkeys():
                shortnames[devicepath] = listnumberer.getName(shortnames[devicepath])

        return shortnames
def build(master_dict, path, l, fh):
    master = ()
    master = sorted(master_dict)

    m_dict = OrderedDict()
    for item in master:
        m_dict[item] = 0

    fhl = open(path, "r")
    lines = fhl.read().split()
    # lines = re.sub("[()+.,\']",'',lines)
    # words = nltk.tokenize.word_tokenize(lines)
    bi_lines = bigrams(lines)
    temp = FreqDist(bi_lines)
    fhl.close()

    # temp = get_bigram_file(path)
    for key in m_dict.iterkeys():
        if temp.has_key(key):
            m_dict[key] = temp[key]
        else:
            m_dict[key] = 0
            # m_dict.update(temp)
    values = list()
    for val in m_dict.itervalues():
        values.append(str(val))
        # print len(values)
    fh.write(l + "," + ",".join(values))
    fh.write("\n")
Example #18
0
class TCPClient(Sending, HasPeers):
    """
    This module keeps a list of peers an connects to them if a message must be broadcast
    The tcp connection will be closed right after the messages were sent
    """

    def __init__(self):
        self.peers = OrderedDict()

        self.info = None

    def connect_widget(self, widget):
        self.info = widget
        widget.connectionChanged.connect(self.update_peer)

    def add_peer(self, name, address):
        host, port = address
        key = (host, name)
        if key not in self.peers:
            self.peers[key] = {
                "enabled": ilastik_config.getboolean("ipc raw tcp", "autoaccept"),
                "address": [host, port],
            }

        elif self.peers[key]["address"][1] != port:
            self.peers[key]["address"][1] = port
        else:
            return
        self.info.update_connections(self.peers)

    def update_peer(self, key, **kvargs):
        enabled = kvargs["enabled"]
        self.peers.values()[key]["enabled"] = enabled

    def _broadcast(self, message):
        count = 0
        for peer in filter(itemgetter("enabled"), self.peers.itervalues()):
            try:
                with socket() as s:
                    # noinspection PyTypeChecker
                    host, port = peer["address"]
                    s.connect((host, port))
                    s.sendall(message)
            except socket_error as e:
                logger.exception(e)
            else:
                count += 1
        return count

    @property
    def widget(self):
        return self.info

    @property
    def running(self):
        return bool(self.peers)

    @staticmethod
    def available(mode=None):
        return True
Example #19
0
class SVDPeripheralRegister:
    def __init__(self, svd_elem, parent):
        self.parent = parent
        self.name = str(svd_elem.name)
        self.description = str(svd_elem.description)
        self.offset = int(str(svd_elem.addressOffset), 0)
        self.size = int(str(svd_elem.size), 0)
        fields = svd_elem.fields.getchildren()
        self.fields = OrderedDict()
        for f in fields:
            self.fields[str(f.name)] = SVDPeripheralRegisterField(f, self)

    def refactor_parent(self, parent):
        self.parent = parent
        try:
            fields = self.fields.itervalues()
        except AttributeError:
            fields = self.fields.values()
        for f in fields:
            f.refactor_parent(self)

    def address(self):
        return self.parent.base_address + self.offset

    def __unicode__(self):
        return str(self.name)
Example #20
0
    def var_dump(self, space, indent, recursion):
        if self in recursion:
            return "%s*RECURSION*\n" % indent
        s = StringBuilder()
        recursion[self] = None
        header = "object(%s)#%d " % (self.getclass().name, self.get_instance_number())
        orig_indent = indent
        if indent.endswith("&"):
            indent = indent[:-1]
        subindent = indent + "  "
        counter = 0

        all_names = []
        all_values_w = []
        self.enum_properties(space.ec.interpreter, all_names, all_values_w)

        properties = OrderedDict()
        for i in range(len(all_names)):
            name, access = demangle_property(all_names[i])
            key = dump_property(name, access)

            properties[key] = "%s[%s]=>\n%s" % (subindent, key, all_values_w[i].var_dump(space, subindent, recursion))

        for part in properties.itervalues():
            counter += 1
            s.append(part)

        s.append("%s}\n" % indent)

        del recursion[self]

        return "%s%s(%d) {\n" % (orig_indent, header, counter) + s.build()
Example #21
0
def box_data(keys, data=None):
    """
    Box Data into appropiate pandas object

    Parameters
    ----------
    keys : dict or iterable
        Either keys list or Result Dict
    data : iterable
        list of data
    """
    if isinstance(keys, dict):
        rdict = keys
    else:
        rdict = OrderedDict(zip(keys, data))

    test = next(rdict.itervalues())
    if np.isscalar(test):
        return Series(rdict)

    if isinstance(test, Series):
        return DataFrame(rdict)

    if isinstance(test, DataFrame):
        return Panel(rdict)

    if isinstance(test, ColumnPanel):
        data = OrderedDict([(k, v.to_panel()) for k, v in rdict.iteritems()])
        return Panel4D(data)

    if isinstance(test, Panel):
        return Panel4D(data)

    return rdict
Example #22
0
def show_navigation(context):
    site = context.get("nexus_site", NexusModule.get_global("site"))
    request = NexusModule.get_request()

    category_link_set = OrderedDict([(k, {"label": v, "links": []}) for k, v in site.get_categories()])

    for namespace, module in site._registry.iteritems():
        module, category = module

        if module.permission and not request.user.has_perm(module.permission):
            continue

        home_url = None
        if "request" in context:
            home_url = module.get_home_url(context["request"])

        if not home_url:
            continue

        active = request.path.startswith(home_url)

        if category not in category_link_set:
            if category:
                label = site.get_category_label(category)
            else:
                label = None
            category_link_set[category] = {"label": label, "links": []}

        category_link_set[category]["links"].append((module.get_title(), home_url, active))

        category_link_set[category]["active"] = active

    return {"nexus_site": site, "category_link_set": category_link_set.itervalues()}
def uniquify_points_and_return_input_index_to_unique_index_map(pts, threshold=0):
    """
	Given a sequence of N points 'pts',
	and an optional 'threshold' indicating how many decimal places of accuracy (default: 0)
	returns two items:
	   a sequence of all the unique elements in 'pts'
	   and
	   a list of length N where the i-th item in the list tells you where
	   pts[i] can be found in the unique elements.
	"""

    from collections import OrderedDict

    unique_pts = OrderedDict()
    pts_map = []
    ## Add rounded points to a dictionary and set the key to
    ## ( the index into the ordered dictionary, the non-rounded point )
    for i, (pt, rounded_pt) in enumerate(zip(pts, map(tuple, asarray(pts).round(threshold)))):
        index = unique_pts.setdefault(rounded_pt, (len(unique_pts), pt))[0]
        ## For fancier schemes:
        # index = unique_pts.setdefault( rounded_pt, ( len( unique_pts ), [] ) )[0]
        # unique_pts[ rounded_pt ][1].append( pt )
        pts_map.append(index)

        ## Return the original resolution points.
        ## The average of all points that round:
        # return [ tuple( average( pt, axis = 0 ) ) for i, pt in unique_pts.itervalues() ], pts_map
        ## The closest point to the rounded point:
        # return [ tuple( pt[ abs( asarray( pt ).round( threshold ) - asarray( pt ) ).sum(axis=1).argmin() ] ) for i, pt in unique_pts.itervalues() ], pts_map
        ## Simplest, the first rounded point:
    return [tuple(pt) for i, pt in unique_pts.itervalues()], pts_map
Example #24
0
class InterfaceSection(object):
    def __init__(self, properties=None, traces=None, controls=None, title="", name=None):
        self.name = name or title
        self.title = title
        self.properties = properties or []
        self.controls = controls or []
        self.traces = OrderedDict()

        if traces is not None:
            for t in traces:
                try:
                    self.traces[t["name"]] = t
                except KeyError:
                    self.traces[t["title"]] = t

    def __getitem__(self, key):
        if key in ("name", "title", "properties", "controls", "traces"):
            return getattr(self, key)
        else:
            raise KeyError

    def output(self):
        return {
            "name": self.name,
            "title": self.title,
            "traces": [_trace(t) for t in self.traces.itervalues()],
            "properties": [_prop(p) for p in self.properties if p.type not in ("Image",)],
            "images": [_img(p) for p in self.properties if p.type == "Image"],
            "controls": [_control(c) for c in self.controls],
        }
Example #25
0
class DictVal(Collapsible):
	def __init__(self, expanded=True):
		super(Dict, self).__init__(expanded)
		self.items = OrderedDict()
		
	def render_items(self):
		r = []
		for key, item in self.items.iteritems():
			r += [t(key), t(":"), indent(), nl()]
			r += [ElementTag(item)]
			r += [dedent(), nl()]
		return r

	def __getitem__(self, i):
		return self.items[i]

	def __setitem__(self, i, v):
		self.items[i] = v
		v.parent = self

	def fix_parents(self):
		super(Dict, self).fix_parents()
		self._fix_parents(self.items.values())

	def flatten(self):
		return [self] + [v.flatten() for v in self.items.itervalues() if isinstance(v, Node)]
		#skip Widgets, as in settings

	def add(self, (key, val)):
		assert(not self.items.has_key(key))
		self.items[key] = val
		assert(isinstance(key, str))
		assert(isinstance(val, element.Element))
		val.parent = self
Example #26
0
class DocumentLengthTable:
    def __init__(self):
        self.table = OrderedDict()

    def __len__(self):
        return len(self.table)

    def read(self, filename):
        with open(filename) as f:
            lines = f.readlines()
        for line in lines:
            docid, length = line.split(" ")
            self.table[docid] = int(length)

    def write(self, filename="default.dlt"):
        with open(filename, "w") as f:
            for docid in self.table:
                line = "%s %d\n" % (docid, self.table[docid])
                f.write(line)

    def add(self, docid, length):
        self.table[docid] = length

    def get_length(self, docid):
        if docid in self.table:
            return self.table[docid]
        else:
            raise LookupError("%s not found in table" % str(docid))

    def get_average_length(self):
        sum = 0
        for length in self.table.itervalues():
            sum += length
        return float(sum) / float(len(self.table))
Example #27
0
    def get_supported_books(self, book_ids):
        from calibre.ebooks.oeb.polish.main import SUPPORTED

        db = self.gui.library_view.model().db
        supported = set(SUPPORTED)
        for x in SUPPORTED:
            supported.add("ORIGINAL_" + x)
        ans = [(x, set((db.formats(x, index_is_id=True) or "").split(",")).intersection(supported)) for x in book_ids]
        ans = [x for x in ans if x[1]]
        if not ans:
            error_dialog(
                self.gui,
                _("Cannot polish"),
                _(
                    "Polishing is only supported for books in the %s"
                    " formats. Convert to one of those formats before polishing."
                )
                % _(" or ").join(sorted(SUPPORTED)),
                show=True,
            )
        ans = OrderedDict(ans)
        for fmts in ans.itervalues():
            for x in SUPPORTED:
                if ("ORIGINAL_" + x) in fmts:
                    fmts.discard(x)
        return ans
Example #28
0
class ChannelManager(object):
    def __init__(self, cid_gen=None):
        self.channels = OrderedDict()
        if not cid_gen:
            import random

            def cid_gen():
                gen = lambda: "%016x" % random.getrandbits(128)
                key = gen()
                while key in self.channels:
                    key = gen()
                return key

        self.cid_gen = cid_gen

        try:
            from simplecron.api import register, start_cron

            register(self.clean_up)
            start_cron()
        except ImportError:
            pass

    def iterchannels(self):
        return self.channels.itervalues()

    def __len__(self):
        return len(self.channels)

    def create(self):
        cid = self.cid_gen()
        self.channels[cid] = Channel(cid, parent=self)
        return cid

    def refresh(self, ch):
        cid = ch.cid
        del self.channels[cid]
        self.channels[cid] = ch
        ch.timestamp = time.time()

    def get(self, cid):
        return self.channels[cid]

    def has(self, cid):
        return cid in self.channels

    def clean_up(self, time_diff=30 * 60):
        """O(1) so don't be afraid to use it"""
        to_del = []
        t = time.time()
        for cid, ch in self.channels.iteritems():
            if t - ch.timestamp < time_diff:
                break
            else:
                to_del.append((cid, ch))
        for cid, ch in to_del:
            del self.channels[cid]
            if ch.client_d:
                ch.client_d.errback(ChannelDead())
        return len(to_del)
Example #29
0
class FIXTrailer(FIXMixIn):
    """
    A FIX Trailer component.

    :members: _all, _initialized, _message, mutations

    """

    __metaclass__ = FIXMeta

    def __init__(self, message=None, *args, **kwargs):
        self._initialized = OrderedDict()
        self._message = message

        # we loop over _all items in order because the spec says so

        for name, tag in self._all.iteritems():
            if name in kwargs:
                value = kwargs[name]
                field = tag(value)
                self._initialized[name] = field

        self.mutations = {}

    def __iter__(self):
        for field in _flatten(*self._initialized.itervalues()):
            if field.name == "CheckSum":
                continue
            yield field

    def __repr__(self):
        return "".join(imap(repr, self))

    def __str__(self):
        return "".join(imap(str, self))

    def initialize(self, optional=False, **kwargs):
        """
        Initializes a FIXTrailer, with optionally supplied kwargs.

        The following fields cannot be initialized through this method:

            - CheckSum

        """
        for name, field in self._all.iteritems():
            if name in self._initialized:
                continue

            if name in ("CheckSum",):
                continue

            if field.required or optional or name in kwargs:
                if name in kwargs:
                    self._initialized[name] = field(kwargs[name], **kwargs)
                else:
                    self._initialized[name] = field(**kwargs)

        return
Example #30
0
def AccumulateGenericResults(graph_names, suite_units, iter_output):
    """Iterates over the output of multiple benchmark reruns and accumulates
  generic results.

  Args:
    graph_names: List of names that configure the base path of the traces. E.g.
                 ['v8', 'Octane'].
    suite_units: Measurement default units as defined by the benchmark suite.
    iter_output: Iterator over the standard output of each test run.
  Returns: A "Results" object.
  """
    traces = OrderedDict()
    for stdout in iter_output():
        if stdout is None:
            # The None value is used as a null object to simplify logic.
            continue
        for line in stdout.strip().splitlines():
            match = GENERIC_RESULTS_RE.match(line)
            if match:
                stddev = ""
                graph = match.group(1)
                trace = match.group(2)
                body = match.group(3)
                units = match.group(4)
                match_stddev = RESULT_STDDEV_RE.match(body)
                match_list = RESULT_LIST_RE.match(body)
                errors = []
                if match_stddev:
                    result, stddev = map(str.strip, match_stddev.group(1).split(","))
                    results = [result]
                elif match_list:
                    results = map(str.strip, match_list.group(1).split(","))
                else:
                    results = [body.strip()]

                try:
                    results = map(lambda r: str(float(r)), results)
                except ValueError:
                    results = []
                    errors = ["Found non-numeric in %s" % "/".join(graph_names + [graph, trace])]

                trace_result = traces.setdefault(
                    trace,
                    Results(
                        [
                            {
                                "graphs": graph_names + [graph, trace],
                                "units": (units or suite_units).strip(),
                                "results": [],
                                "stddev": "",
                            }
                        ],
                        errors,
                    ),
                )
                trace_result.traces[0]["results"].extend(results)
                trace_result.traces[0]["stddev"] = stddev

    return reduce(lambda r, t: r + t, traces.itervalues(), Results())