Example #1
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 #2
1
    def get_with_data(*args, **kwargs):
        filters = kwargs.pop("filters", None)
        limit = kwargs.pop("limit", None)
        offset = kwargs.pop("offset", 0)
        order = kwargs.pop("order", Reservation.start_dt)
        limit_per_room = kwargs.pop("limit_per_room", False)
        if kwargs:
            raise ValueError("Unexpected kwargs: {}".format(kwargs))

        query = Reservation.query.options(joinedload(Reservation.room))
        if filters:
            query = query.filter(*filters)
        if limit_per_room and (limit or offset):
            query = limit_groups(query, Reservation, Reservation.room_id, order, limit, offset)

        query = query.order_by(order, Reservation.created_dt)

        if not limit_per_room:
            if limit:
                query = query.limit(limit)
            if offset:
                query = query.offset(offset)

        result = OrderedDict((r.id, {"reservation": r}) for r in query)

        if "vc_equipment" in args:
            vc_id_subquery = (
                db.session.query(EquipmentType.id)
                .correlate(Reservation)
                .filter_by(name="Video conference")
                .join(RoomEquipmentAssociation)
                .filter(RoomEquipmentAssociation.c.room_id == Reservation.room_id)
                .as_scalar()
            )

            # noinspection PyTypeChecker
            vc_equipment_data = dict(
                db.session.query(Reservation.id, static_array.array_agg(EquipmentType.name))
                .join(ReservationEquipmentAssociation, EquipmentType)
                .filter(Reservation.id.in_(result.iterkeys()))
                .filter(EquipmentType.parent_id == vc_id_subquery)
                .group_by(Reservation.id)
            )

            for id_, data in result.iteritems():
                data["vc_equipment"] = vc_equipment_data.get(id_, ())

        if "occurrences" in args:
            occurrence_data = OrderedMultiDict(
                db.session.query(ReservationOccurrence.reservation_id, ReservationOccurrence)
                .filter(ReservationOccurrence.reservation_id.in_(result.iterkeys()))
                .order_by(ReservationOccurrence.start_dt)
            )
            for id_, data in result.iteritems():
                data["occurrences"] = occurrence_data.getlist(id_)

        return result.values()
Example #3
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)
Example #4
1
class Table:
    """
    Creates a table from a file and provides method to access elements from that table.
    """

    def __init__(self, filename):
        f = open(filename, "r")
        for line in f:
            if line.strip() == "":
                continue
            if line[0] == "#":
                continue
            self.header = line.split()
            nlines = len(self.header)
            break
        i = 0
        self.content = OrderedDict()
        for line in f:
            i += 1
            if line.strip() == "":
                continue
            if line[0] == "#":
                continue
            linelist = line.split()
            if len(linelist) != nlines:
                raise NameError("Wrong number of columns in file " + filename + " line " + str(i))
            self.content[linelist[0]] = linelist

    def get(self, row, col):
        return self.content[row][self.header.index(col)]

    def getFloat(self, row, col):
        return float(self.get(row, col))

    def getCols(self):
        return self.header

    def getRows(self):
        b = list()
        for a in self.content.iterkeys():
            b.append(a)
        return b

    def getColDictFloat(self, col):
        b = dict()
        for a in self.content.iterkeys():
            b[a] = float(self.content[a][self.header.index(col)])
        return b

    cols = property(getCols)
    rows = property(getRows)
Example #5
1
class Struct(object):
    def __init__(self):
        self.fields = OrderedDict()

    @classmethod
    def unpack(cls, u):
        s = cls()
        for field in cls._unpack(u):
            s.add_field(*field)
        return s

    def add_field(self, name, value, fmt=None):
        if hasattr(self, name):
            raise StructError("Internal error: Duplicate Struct field name {}".format(name))
        if fmt is None:
            if isinstance(value, (int, long)) and not isinstance(value, bool):
                fmt = "{:#x}".format
            else:
                fmt = "{!r}".format
        elif isinstance(fmt, str):
            fmt = fmt.format
        elif not callable(fmt):
            raise StructError("Internal error: Expected a format string or callable, but got: {}".format(fmt))
        setattr(self, name, value)
        self.fields[name] = fmt

    def format_field(self, name):
        return self.fields[name](getattr(self, name))

    def __repr__(self):
        return "{}({})".format(
            self.__class__.__name__, ", ".join("{}={}".format(k, self.format_field(k)) for k in self.fields.iterkeys())
        )

    def __iter__(self):
        return (getattr(self, k) for k in self.fields.iterkeys())

    def __eq__(self, other):
        if type(self) is not type(other):
            return NotImplemented
        return self.fields.keys() == other.fields.keys() and all(
            getattr(self, name) == getattr(other, name) for name in self.fields.iterkeys()
        )

    def __ne__(self, other):
        return not self == other

    def __hash__(self):
        return hash(tuple((name, getattr(self, name)) for name in self.fields.iterkeys()))
Example #6
1
class BoardConfig(object):

    """Board configuration component"""

    def __init__(self, board):
        """Initialization

        In:
            - ``board`` -- the board object will want to configure
        """
        self.board = board
        self.menu = OrderedDict()
        self.menu["profile"] = MenuEntry(_(u"Profile"), "icon-profile", BoardProfile)
        if security.has_permissions("manage", self.board):
            self.menu["labels"] = MenuEntry(_(u"Card labels"), "icon-price-tag", BoardLabels)
            self.menu["weights"] = MenuEntry(_(u"Card weights"), "icon-meter", BoardWeights)
            self.menu["background"] = MenuEntry(_(u"Background"), "icon-paint-format", BoardBackground)
        self.selected = None
        self.content = component.Component(None)
        self.select(self.menu.iterkeys().next())

    def select(self, v):
        """Select a configuration menu item

        In:
            - ``v`` -- the id_ of the menu item we want to show
        """
        self.selected = v
        self.content.becomes(self.menu[v].content(self.board))
Example #7
0
def delete_duplicates(input_file, my_file):
    content = ""
    last_first = re.compile("\w+,\w+")
    last_first_middle = re.compile("\w+,\w+ \w+")
    fid = open(input_file)
    fid_wrt = open(my_file, "w+")
    lines = fid.readlines()
    dict = OrderedDict()

    for line in lines:
        if len(re.split(":", line)) != 1:
            name, SSN = re.split(":", line)
            SSN = SSN.strip("\n")
            if dict.has_key(SSN):
                dict[SSN].append(name)
            else:
                dict.update({SSN: []})
                dict[SSN].append(name)

    for SSN in dict.iterkeys():
        name_array = dict[SSN]
        split_name = []
        name_array.sort(key=lambda item: (-len(item), item))
        if re.match(last_first_middle, name_array[0]) or re.match(last_first, name_array[0]):
            split_name = re.findall(r"[\w']+", name_array[0])
            if re.match(last_first_middle, name_array[0]):
                normal_form = split_name[1] + " " + split_name[2] + " " + split_name[0]
            elif re.match(last_first, name_array[0]):
                normal_form = split_name[1] + " " + split_name[0]
        else:
            normal_form = name_array[0]
        content += normal_form + ":" + SSN + "\n"

    fid_wrt.write(content.rstrip("\n"))
    fid_wrt.close()
Example #8
0
    def build_sprite_list(self):
        """
        Builds a lookup table of sprite lumps, and loads a PLAYPAL palette from the current WAD list.
        """

        sprite_lumps = OrderedDict()
        for wad in self.wads:
            sprite_lumps.update(wad.get_sprite_lumps())

        # Create a list of sprite names.
        for name in sprite_lumps.iterkeys():
            sprite_name = name[0:4]
            self.sprites[sprite_name] = SpriteEntry(sprite_name)

        # Add sprite lumps for each sprite.
        for sprite_key, sprite_entry in self.sprites.iteritems():

            for lump_name, lump in sprite_lumps.iteritems():
                if lump_name[0:4] != sprite_key:
                    continue

                frame = lump_name[4]
                rotation = lump_name[5]
                sprite_entry.add_frame(frame, rotation, lump, False)

                # Mirrored sprite lump.
                if len(lump_name) == 8:
                    frame = lump_name[6]
                    rotation = lump_name[7]
                    sprite_entry.add_frame(frame, rotation, lump, True)

        # Find a PLAYPAL lump to use as palette.
        playpal = self.get_lump("PLAYPAL")
        if playpal is not None:
            self.palette = graphics.Palette(playpal.get_data())
def get_dict(tax_ass):
    phylum_reg = re.compile("p__(.+)")
    genus_reg = re.compile("g__(.+)")
    genus_in_phylum = OrderedDict()
    phylum_genus = OrderedDict()
    with open(tax_ass) as fp:
        for line in fp:
            tabs = line.rstrip().split("\t")
            tax_line = tabs[1]
            taxes = tax_line.split(";")
            p = None
            g = None
            for tax in taxes:
                try:
                    p = phylum_reg.search(tax).group(1).replace(" ", "_")
                except AttributeError:
                    pass
                try:
                    g = genus_reg.search(tax).group(1).replace(" ", "_")
                except AttributeError:
                    pass
            if g is not None and p is not None:
                genus_in_phylum[g] = p
                phylum_genus[p] = g
    color_dict = OrderedDict()
    for ind, p in enumerate(list(phylum_genus.iterkeys())):
        color_dict[p] = COLS_BREWER[ind % 20]
    return color_dict, genus_in_phylum
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")
def sentimentList(sentiment_list):
    """Summary

    Args:
        sentiment_list (TYPE): A list of Reviews

    Returns:
        TYPE: A list of binary lists for the provided list of key words from the assignment
    """
    new_list = []
    for file in sentiment_list:
        with open(file) as f:
            sentiment_list = [
                ("awful", 0),
                ("bad", 0),
                ("boring", 0),
                ("dull", 0),
                ("effective", 0),
                ("enjoyable", 0),
                ("great", 0),
                ("hilarious", 0),
            ]
            sentiment_dictionary = OrderedDict(sentiment_list)
            lines = f.readlines()
            lower_lines = [x.lower() for x in lines]
            for key in sentiment_dictionary.iterkeys():
                if any(key in s for s in lines):
                    sentiment_dictionary[key] = 1
            new_list.append(sentiment_dictionary.values())
    return new_list
def build(master_dict, path, l, fh):
    """	
	master = ()
	master = sorted(master_dict)

	m_dict = OrderedDict()
	for item in master:
		m_dict[item] = 0
	"""
    print "12"
    temp_dict = get_bigram_file(path)
    temp_dict = OrderedDict(sorted(temp_dict.items()))
    print "13"
    # m_dict.update(temp)
    if l == "pos":
        lab = "+1"
    else:
        lab = "-1"
    print "14"
    # fh.write(lab)
    temp_str = lab
    for key in temp_dict.iterkeys():
        # print " "+str(key)+":"+str(temp_dict[key])
        # fh.write(" "+str(master_dict.keys().index(key))+":"+str(temp_dict[key]))
        temp_str += " " + str(master_dict[key]) + ":" + str(temp_dict[key])
    fh.write(temp_str + "\n")
    print "15"
Example #13
0
    def calc_good(g):
        if g.key in stats_cache:
            return

        if g.level is None:
            # This is a base good, so a little special handling.
            stats_cache[g.key] = {
                "value": g.base_value,
                "total_multiplier": 1.0,
                "materials": OrderedDict(
                    [("good_water", 0), ("good_food", 0), ("good_wood", 0), ("good_stone", 0), ("goodtype_crystal", 0)]
                ),
            }
            stats_cache[g.key]["materials"][g.key] += 1
            return

        value = 0
        materials = OrderedDict(
            [("good_water", 0), ("good_food", 0), ("good_wood", 0), ("good_stone", 0), ("goodtype_crystal", 0)]
        )
        materials_value = 0
        for i in filter(
            lambda x: x is not None, [g.ingredient_0, g.ingredient_1, g.ingredient_2, g.ingredient_3, g.ingredient_4]
        ):
            if i not in stats_cache:
                calc_good(goods[i])
            value += stats_cache[i]["value"]
            materials_value += float(stats_cache[i]["value"]) / float(stats_cache[i]["total_multiplier"])
            for k in materials.iterkeys():
                materials[k] += stats_cache[i]["materials"][k]
        value = int(ceil(value * g.multiplier))
        total_multiplier = float(value) / float(materials_value)
        stats_cache[g.key] = {"value": value, "total_multiplier": total_multiplier, "materials": materials}
Example #14
0
def do_single_plot(output_fpath, alg_data, plot_title, an_pred, nk_pred):
    import matplotlib.pyplot as plt
    from collections import OrderedDict

    # gets the data to be used
    plot_alg_data = dict(
        (an, dict((n, t) for (n, k), t in ad.iteritems() if nk_pred(n, k)))
        for an, ad in alg_data.iteritems()
        if an_pred(an)
    )

    # makes the data vectors
    n_vec = sorted(n for n in plot_alg_data.itervalues().next().iterkeys())
    data_by_alg = dict((an, [sorted(av[n]) for n in n_vec]) for an, av in plot_alg_data.iteritems())
    data_vecs = OrderedDict(
        (an, zip(*[(t[2], t[4] - t[2], t[2] - t[0]) for t in av]))
        for an, av in sorted(data_by_alg.iteritems(), key=lambda ai: AN_ORDER[ai[0]])
    )

    plt.figure()
    plt.xscale("log")
    plt.yscale("log")
    for an, ad in data_vecs.iteritems():
        # plt.errorbar(n_vec, ad[0], yerr=ad[1:]) -- Error bars are too small
        plt.errorbar(n_vec, ad[0], fmt=FMT_BY_AN[an])
    plt.legend([FULL_NAME_BY_AN[an] for an in data_vecs.iterkeys()], "lower right")
    plt.title(plot_title)

    plt.xlabel("$n$")
    plt.ylabel("Execution time (seconds)")

    plt.savefig(output_fpath)
Example #15
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
Example #16
0
    def lex(self):
        cls = self.__class__

        _rule_cache = OrderedDict((name, re.compile(rule)) for name, rule in cls.RULES.iteritems())
        _handlers = {name: getattr(self, "t_%s" % name) for name in _rule_cache.iterkeys()}

        self._tokens = []
        while self._pos <= self._length:
            matched = False
            for name, rule in _rule_cache.iteritems():
                match = rule.match(self._input, self._pos)
                if match is not None:
                    matched = True
                    match_span = match.span()
                    # advance the pointer
                    self._pos = match_span[1]

                    # generate the token(s)
                    frag = self._input[match_span[0] : match_span[1]]
                    skip, result = _handlers[name](frag, self, match)
                    if skip:
                        continue

                    # print result
                    self._tokens.extend(result)

                    # update previous token
                    self._prev = self._tokens[-1].type
                    break

            if not matched:
                raise ValueError("could not advance any more")
Example #17
0
    def get_image_urls(self, title, author, log, abort, timeout):
        from calibre.utils.cleantext import clean_ascii_chars
        from urllib import urlencode
        import html5lib
        import json
        from collections import OrderedDict

        ans = OrderedDict()
        br = self.browser
        q = urlencode({"as_q": ("%s %s" % (title, author)).encode("utf-8")}).decode("utf-8")
        sz = self.prefs["size"]
        if sz == "any":
            sz = ""
        elif sz == "l":
            sz = "isz:l,"
        else:
            sz = "isz:lt,islt:%s," % sz
        # See https://www.google.com/advanced_image_search to understand this
        # URL scheme
        url = "https://www.google.com/search?as_st=y&tbm=isch&{}&as_epq=&as_oq=&as_eq=&cr=&as_sitesearch=&safe=images&tbs={}iar:t,ift:jpg".format(
            q, sz
        )
        log("Search URL: " + url)
        raw = br.open(url).read().decode("utf-8")
        root = html5lib.parse(clean_ascii_chars(raw), treebuilder="lxml", namespaceHTMLElements=False)
        for div in root.xpath('//div[@class="rg_meta"]'):
            try:
                data = json.loads(div.text)
            except Exception:
                continue
            if "ou" in data:
                ans[data["ou"]] = True
        return list(ans.iterkeys())
    def _receive_packet_callback(self, packet):
        try:
            header = packet.eieio_header
            if not header.is_time:
                raise Exception("Only packets with a timestamp are currently considered")

            key_times_labels = OrderedDict()
            while packet.is_next_element:
                element = packet.next_element
                time = element.payload
                key = element.key
                if key in self._key_to_atom_id_and_label:
                    (atom_id, label_id) = self._key_to_atom_id_and_label[key]
                    if time not in key_times_labels:
                        key_times_labels[time] = dict()
                    if label_id not in key_times_labels[time]:
                        key_times_labels[time][label_id] = list()
                    key_times_labels[time][label_id].append(atom_id)

            for time in key_times_labels.iterkeys():
                for label_id in key_times_labels[time].iterkeys():
                    label = self._receive_labels[label_id]
                    for callback in self._live_event_callbacks[label_id]:
                        callback(label, time, key_times_labels[time][label_id])
        except:
            traceback.print_exc()
Example #19
0
class LRUCache:
    # @param capacity, an integer
    def __init__(self, capacity):
        self.capacity = capacity
        self.map = OrderedDict()
        self.size = 0

    # @return an integer
    def get(self, key):
        if key in self.map:
            value = self.map[key]
            del self.map[key]
            self.map[key] = value
            return value
        return -1

    # @param key, an integer
    # @param value, an integer
    # @return nothing
    def set(self, key, value):
        if key in self.map:
            self.map[key] = value
            self.get(key)
            return

        if self.size >= self.capacity:
            self.removeOldest()
        self.map[key] = value
        self.size += 1

    def removeOldest(self):
        for k in self.map.iterkeys():
            break
        self.size -= 1
        del self.map[k]
Example #20
0
def logging(results, f=None):
    """ logging the monitoring result for each interval"""
    # TODO find the way to organize the result and print
    time.sleep(LOGGING_INTERVAL)

    # TODO this is the easy way to organize data, buy not neat
    compact_results = OrderedDict()
    for name in CAPTURE_NAME:
        compact_results["CPU-" + name] = 0
    for name in CAPTURE_NAME:
        compact_results["Mem-" + name] = 0

    for k, v in results.iteritems():
        for key in compact_results.iterkeys():
            if key in k:
                compact_results[key] += v
                break
        else:
            print "some problems"

    # printing to a file if applicable
    list_a = zip(*compact_results.items())  # list_a = zip(*[(k, v) for k,v in summary.iteritems()])
    header = str(list_a[0]).strip("()").replace("'", "") + "\n"
    content = str(list_a[1]).strip("()").replace("'", "") + "\n"
    if f:
        if f.tell() == 0:
            f.write(header)
        f.write(content)
    else:
        print list_a
Example #21
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 in k:
                dubl += 1

        if dubl > 0:
            lname = "%s%02d" % (name, dubl)
        else:
            lname = name

        layer = Layer()
        self.layers[lname] = layer
        return layer
def word_rank(corpus, set_of_words, list1, ngram="word"):
    c = consolidate_freq_dict(corpus, list1, ngram)
    o = dict((word, c[word]) for word in set_of_words)
    o = OrderedDict(sorted(o.items(), key=lambda t: t[1]))
    o_rank = {}
    for (ii, k) in enumerate(o.iterkeys()):
        o_rank[k] = ii
    return o_rank
def chapters():
    """
    Load chapter data from markdown source

    Returns:
        An ordered dictionary indexed on chapter ID, where each entry has:
            content: HTML content of chapter
            sequence: The numerical index of the chapter
    """

    global _chapters, _toc

    # if already loaded, use that
    if _chapters:
        return _chapters

    # if build exists for chapters, then use that
    if os.path.exists(settings.CHAPTER_BUILD):
        with open(settings.CHAPTER_BUILD, "r") as f:
            _chapters = OrderedDict(sorted(json.load(f).items(), key=lambda e: int(e[1]["sequence"])))
            return _chapters

    # make sure table of contents exists
    toc()

    # load chapter metadata
    _chapters = OrderedDict()
    with open(settings.CHAPTER_METADATA) as f:
        info = yaml.load(f)
        for i, chapter in enumerate(info):
            # add entry for each chapter
            chapter_id = chapter["id"]
            _chapters[chapter_id] = {"sequence": i}

            # add other chapter metadata
            for k, v in chapter.iteritems():
                _chapters[chapter_id][k] = v

    # iterate over markdown source for each chapter in metadata
    for i in _chapters.iterkeys():
        with open(settings.CHAPTER_SRC + "/" + i + ".markdown") as f:
            # compile markdown to html
            content = f.readlines()
            for line in content[:]:
                # extract sections
                if re.match(r"^## ", line):
                    _toc[i]["sections"].append({"id": subheading(line), "title": line[3:-1]})

            _chapters[i]["content"] = markdown.markdown("".join(content))

    # write build files
    with open(settings.CHAPTER_BUILD, "w") as f:
        json.dump(_chapters, f)
    with open(settings.TOC_BUILD, "w") as f:
        json.dump(_toc, f)

    return _chapters
class Allergy:
    best = 987654321

    def __init__(self):
        self.dictionary = defaultdict(list)
        self.eater = {}

    def clear(self):
        self.dictionary.clear()
        self.eater.clear()
        self.best = 987654321

    def start(self):
        testCase = int(rl())
        for tc in xrange(testCase):
            people_num, food_num = map(int, rl().strip().split())
            people = rl().strip().split()
            for food_index in xrange(food_num):
                can_eat_people = rl().split()
                # print can_eat_people[1:]
                self.set_food_list(food_index, can_eat_people[1:])
            self.dictionary = OrderedDict(sorted(self.dictionary.items(), key=lambda x: len(x[1])))

            self.check(0)
            print self.best
            self.clear()

    def set_food_list(self, food_idx, people):
        for person in people:
            self.dictionary.setdefault(person, []).append(food_idx)

    def check(self, cnt):
        # 가지치기
        # print "cnt = {} , best = {}".format(cnt,self.best)
        if cnt >= self.best:
            return

        # 모든 사람들이 다 먹었다면 종료
        if not bool(self.dictionary):
            self.best = cnt
            return

        key = next(self.dictionary.iterkeys())
        # print "key =  {} ".format(key)
        # print key
        # print self.dictionary[key]

        # 먹은 음식 체크
        for food_idx in self.dictionary[key]:
            back = OrderedDict([(key, values) for key, values in self.dictionary.items() if food_idx in values])
            for key in back.keys():
                self.dictionary.pop(key)
            cnt += 1
            self.check(cnt)
            cnt -= 1
            self.dictionary.update(back)
Example #25
0
    def get(self):
        if self.logged_in:
            sites = SitesModel.query().order().fetch()
            changed_sites = OrderedDict([[x, " "] for x in URLS])
            for site in sites:
                results = site.results
                lasts = results.split("/")
                if len(lasts) < 2:
                    # Only one result
                    continue

                last = lasts[-1].split("|")[0]  # [1] - timestamp
                last_result = self.get_data(last)

                i = -2
                while True:
                    prev = lasts[i].split("|")[0]
                    prev_result = self.get_data(prev)
                    if prev_result:
                        break
                    i -= 1
                # Now we have IDs

                changes = self.compare_data(last_result, prev_result)
                if len(changes) > 0:
                    changed_sites[site.site_name] = changes

            # Mail results
            from mailer.mail_send import SendStatistics

            stat = False
            for val in changed_sites.itervalues():
                if val is not " ":
                    stat = True
                    break

            if stat:
                result = ""
                for k in changed_sites.iterkeys():
                    changes = changed_sites[k]
                    changed_cost = ""
                    for change in changes:
                        change = " ".join(get_data_from_html(change).split("/$"))
                        if change:
                            changed_cost = "; ".join([change, changed_cost])
                    if changed_cost:
                        line = " ".join([k, changed_cost])
                        result = "\n".join([result, line])

                stats = SendStatistics()
                stats.post(data=result)
            values = {"user": self.current_user, "site_names": URLS, "sites": changed_sites, "site": ""}
            # self.session.add_flash('Some message', level='error')
            self.render("index.html", values)
        else:
            self.redirect("/login")
Example #26
0
class LRUCache(object):
    """
    LRU Cache based on a Python OrderedDict

    The OrderedDict is a dict that maintains a doubly linked list which saves the order
    that keys are inserted.  When a cache item is accessed, the item is popped
    and reinserted in the OrderedDict so that the recently used order is preserved.

    The internal OrderedDict is accessible as `self.data`

    OrderedDict references
    http://docs.python.org/library/collections.html#collections.OrderedDict
    http://hg.python.org/cpython/file/70274d53c1dd/Lib/collections.py
    """

    def __init__(self, size):
        self.size = size
        self.data = OrderedDict()

    def __getitem__(self, key):
        # pop and reinsert so that this item is now last
        value = self.data.pop(key)
        self.data[key] = value
        return value

    def __setitem__(self, key, value):
        if key in self.data:
            self.data.pop(key)
        elif len(self.data) == self.size:
            # cache full: delete the least recently used item (first item)
            self.data.popitem(last=False)
        self.data[key] = value

    def __contains__(self, item):
        return item in self.data

    def __str__(self):
        return "LRUCache (size={size}, length={length}) {data}".format(
            size=self.size, length=len(self.data), data=str(self.data)
        )

    def lru(self):
        """
        Returns the least recently used item's (key, value) tuple
        """
        if len(self.data) == 0:
            return None
        key = self.data.iterkeys().next()
        return (key, self.data[key])

    def keys(self):
        """
        Returns the keys in the cache ordered from least recently used to 
        most recently used
        """
        return self.data.keys()
 def test_iterators(self):
     pairs = [("c", 1), ("b", 2), ("a", 3), ("d", 4), ("e", 5), ("f", 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     self.assertEqual(list(od), [t[0] for t in pairs])
     self.assertEqual(od.keys()[:], [t[0] for t in pairs])
     self.assertEqual(od.values()[:], [t[1] for t in pairs])
     self.assertEqual(od.items()[:], pairs)
     self.assertEqual(list(od.iterkeys()), [t[0] for t in pairs])
     self.assertEqual(list(od.itervalues()), [t[1] for t in pairs])
     self.assertEqual(list(od.iteritems()), pairs)
     self.assertEqual(list(reversed(od)), [t[0] for t in reversed(pairs)])
Example #28
0
def from_file_interevents_aggregated_1week(file_name, usr):

    date7s = [0, 0, 0, 0, 0, 0, 0]
    interevents = []

    interval = defaultdict(int)

    # a loop where we populate those two arrays from the file
    i = 0
    f = open(file_name, "r")
    # read the file
    for line in f:
        i = i + 1
        dt, it = line.split("\t")
        dt = datetime.datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
        it = int(it)
        # date7s.append(dt)
        # interevents.append(it)
        interval[dt] = it

    ordered = OrderedDict(sorted(interval.items(), key=lambda t: t[0]))

    print dt

    for dt in ordered.iterkeys():
        date7s[dt.weekday()] += interval[dt]
    #
    print i

    print date7s
    print interevents

    fig = figure()
    fig.subplots_adjust(bottom=0.2)
    ax = fig.add_subplot(111)
    ax.step([0, 1, 2, 3, 4, 5, 6], date7s, "-")
    ax.axvspan(5, 7, color="yellow", alpha=0.5)
    ax.set_xlim(-1, 7)

    print (date7s[0])

    #    show()

    figure_name = (
        "/home/sscepano/D4D res/allstuff/SET3 intervals from python/1/single usr stats/1week_agr_usr_"
        + str(usr)
        + ".png"
    )

    print (figure_name)
    plt.savefig(figure_name, format="png")

    return
Example #29
0
    def plot_setpnt_dev_assist(self, setpntDev, sortedFlag):
        avgs = dict()
        stds = dict()
        for key in setpntDev.iterkeys():
            avgs[key] = np.mean(setpntDev[key])
            stds[key] = np.std(setpntDev[key])

        sortedAvgs = dict(zip(setpntDev.keys(), avgs.values()))
        sortedStds = list()
        if sortedFlag:
            sortedAvgs = OrderedDict(sorted(sortedAvgs.items(), key=operator.itemgetter(1)))
            for key in sortedAvgs.iterkeys():
                sortedStds.append(stds[key])
        else:
            for key in sortedAvgs.iterkeys():
                sortedStds.append(stds[key])

        fig = plt.figure(figsize=(4, 2))
        plt.bar(range(0, len(sortedAvgs)), sortedAvgs.values(), yerr=sortedStds)
        plt.ylabel(u"Temperature \ndifference ($^\circ$F)", labelpad=-2)
        plt.tight_layout()
        plt.show()
        return fig
Example #30
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