Example #1
def _diff_trees(lhs, rhs, index, *path):
    def _fq(name):
        return "/".join(reversed((name,) + path))

    # Diff the trees (and keep deterministic order)
    rhs_tree_ids = OrderedDict((o.name, o.id) for o in rhs.tree_ids)
    for o in lhs.tree_ids:
        # remove so won't be picked up as added, below
        rhs_id = rhs_tree_ids.pop(o.name, None)
        if rhs_id == o.id:  # no change
        elif rhs_id is None:  # removed
            yield (_fq(o.name), o.id, None)
            rhs_tree = Object(_id=None, tree_ids=[], blob_ids=[], other_ids=[])
        else:  # changed
            rhs_tree = index[rhs_id]
        for difference in _diff_trees(index[o.id], rhs_tree, index, o.name, *path):
            yield difference
    for name, id in rhs_tree_ids.items():  # added
        yield (_fq(name), None, id)
        lhs_tree = Object(_id=None, tree_ids=[], blob_ids=[], other_ids=[])
        for difference in _diff_trees(lhs_tree, index[id], index, name, *path):
            yield difference
    # Diff the blobs (and keep deterministic order)
    rhs_blob_ids = OrderedDict((o.name, o.id) for o in rhs.blob_ids)
    for o in lhs.blob_ids:
        rhs_id = rhs_blob_ids.pop(o.name, None)
        if rhs_id == o.id:
            continue  # no change
        elif rhs_id is None:
            yield (_fq(o.name), o.id, None)
            yield (_fq(o.name), o.id, rhs_id)
    for name, id in rhs_blob_ids.items():
        yield (_fq(name), None, id)
Example #2
class LRUCache(OrderedDict):

    def __init__(self, capacity):
        self.capacity = capacity
        self.cache = OrderedDict()

    def get(self, key):
        if self.cache.has_key(key):
            value = self.cache.pop(key)
            self.cache[key] = value
            value = None

        return value

    def set(self, key, value):
        if self.cache.has_key(key):
            value = self.cache.pop(key)
            self.cache[key] = value
            if len(self.cache) == self.capacity:
                self.cache.popitem(last=False)  # pop出第一个item
                self.cache[key] = value
                self.cache[key] = value
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:

        # 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 #4
def traverse_and_remove_path(obj, path=None, match="First"):
    if path is None:
        path = []
    if isinstance(obj, dict):
        res = OrderedDict()
        for k, v in obj.items():
            cmatch = False
            if match and len(path) > 0 and path[0] == k:
                cmatch = True
            res.update({k: traverse_and_remove_path(v, path=path[1:], match=cmatch)})
        if len(path) == 1 and path[0] in res.keys() and match:
            del res[path[0]]
        return res
    elif isinstance(obj, list):
        res = []
        for i, elem in enumerate(obj):
            cmatch = False
            if match and len(path) >= 1 and isinstance(path[0], int) and path[0] < len(obj) and i == path[0]:
                cmatch = True
            res.append(traverse_and_remove_path(elem, path=path[1:], match=cmatch))
        if len(path) == 1 and isinstance(path[0], int) and path[0] < len(res) and match:
        return res
        return obj  # no container, just values (str, int, float)
Example #5
    def bismark_strand_chart(self):
        """ Make the strand alignment plot """

        # Specify the order of the different possible categories
        keys = OrderedDict()
        keys["strand_ob"] = {"name": "Original bottom strand"}
        keys["strand_ctob"] = {"name": "Complementary to original bottom strand"}
        keys["strand_ctot"] = {"name": "Complementary to original top strand"}
        keys["strand_ot"] = {"name": "Original top strand"}

        # See if we have any directional samples
        directional = 0
        d_mode = ""
        for sn in self.bismark_data["merged"].values():
            if "strand_directional" in sn.keys():
                directional += 1
        if directional == len(self.bismark_data["merged"]):
            keys.pop("strand_ctob", None)
            keys.pop("strand_ctot", None)
            d_mode = "<p>All samples were run with <code>--directional</code> mode; alignments to complementary strands (CTOT, CTOB) were ignored.</p>"
        elif directional > 0:
            d_mode = "<p>{} samples were run with <code>--directional</code> mode; alignments to complementary strands (CTOT, CTOB) were ignored.</p>".format(

        # Config for the plot
        config = {
            "title": "Alignment to Individual Bisulfite Strands",
            "ylab": "% Reads",
            "cpswitch_c_active": False,
            "cpswitch_counts_label": "Number of Reads",

        return d_mode + self.plot_bargraph(self.bismark_data["merged"], keys, config)
Example #6
    def parse_strand_chart_data(self):
        """ Make a data structure suitable for HighCharts for the strand alignment plot """
        self.bismark_strand_samples = list()
        self.bismark_directional_mode = 0
        series = OrderedDict()
        series["OB"] = list()
        series["CTOB"] = list()
        series["CTOT"] = list()
        series["OT"] = list()
        for sn in sorted(self.bismark_data.keys()):
            series["OB"].append(int(self.bismark_data[sn].get("aln_strand_ob", 0)))
            series["CTOB"].append(int(self.bismark_data[sn].get("aln_strand_ctob", 0)))
            series["CTOT"].append(int(self.bismark_data[sn].get("aln_strand_ctot", 0)))
            series["OT"].append(int(self.bismark_data[sn].get("aln_strand_ot", 0)))
            if "aln_strand_directional" in self.bismark_data[sn]:
                self.bismark_directional_mode += 1

        if self.bismark_directional_mode == len(self.bismark_strand_samples):
            series.pop("CTOB", None)
            series.pop("CTOT", None)

        self.bismark_strand_plot_series = list()
        for cat in series:
            self.bismark_strand_plot_series.append({"name": cat, "data": series[cat]})
Example #7
class MissingPonies(object):
    def __init__(self, owned_ponies, inventory_ponies, all_ponies):
        owned_ponies_id = set(owned_ponies.keys()) | set(inventory_ponies.keys())
        all_ponies_id = set(all_ponies.keys())
        not_owned = all_ponies_id - owned_ponies_id
        self._ponies = OrderedDict([(ID, name) for ID, name in all_ponies.items() if ID in not_owned])

    def items(self):
        return self._ponies.items()

    def values(self):
        return self._ponies.values()

    def keys(self):
        return self._ponies.keys()

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

    def __isub__(self, pony_id):
        self._ponies.pop(pony_id, None)
        return self

    def remove(self, pony_id):
        self -= pony_id
Example #8
class Cache(object):
    def __init__(self):

    def reset_caches(self):
        self._category_cache = OrderedDict()
        self._search_cache = OrderedDict()

    def search_cache(self, search):
        old = self._search_cache.pop(search, None)
        if old is None or old[0] <= self.db.last_modified():
            matches = self.search_for_books(search) or []
            self._search_cache[search] = (utcnow(), frozenset(matches))
            if len(self._search_cache) > 50:
            self._search_cache[search] = old
        return self._search_cache[search][1]

    def categories_cache(self, restrict_to=frozenset([])):
        base_restriction = self.search_cache("")
        if restrict_to:
            restrict_to = frozenset(restrict_to).intersection(base_restriction)
            restrict_to = base_restriction
        old = self._category_cache.pop(frozenset(restrict_to), None)
        if old is None or old[0] <= self.db.last_modified():
            categories = self.db.get_categories(ids=restrict_to)
            self._category_cache[restrict_to] = (utcnow(), categories)
            if len(self._category_cache) > 20:
            self._category_cache[frozenset(restrict_to)] = old
        return self._category_cache[restrict_to][1]
Example #9
def extract_extra_ingredients(nodes, is_section_header):
    section = None
    sections = OrderedDict()

    for node in nodes:
        text = node.extract() if isinstance(node, XPathSelector) else node
        text = html_to_text(text).strip()

        if not text:

        if is_section_header(node):
            section = text

        sections.setdefault(section, []).append(text)

    if None in sections:
        ingredients = sections.pop(None)
    elif sections:
        ingredients = sections.pop(sections.keys()[-1])
        ingredients = []

    extra_ingredients = [x for y in sections.values() for x in y]

    return (ingredients, extra_ingredients)
Example #10
    def get_ip_info(self, queue, apis):
        while not queue.empty():
            data = OrderedDict()
            data["ip"] = queue.get()

            for api in apis:
                if api.__class__.__name__ == "Shodan":
                    data["shodan"] = self.search_shodan(data["ip"], api)
                    if self.__errors == False:
                        if "error" in data["shodan"]:
                            data.pop("shodan", None)
                elif api.__class__.__name__ == "CensysIPv4":
                    data["censys"] = self.search_censys(data["ip"], api)
                    if self.__errors == False:
                        if "error" in data["censys"]:
                            data.pop("censys", None)

            if self.__errors == False:
                if not "shodan" in data and not "censys" in data:
                    data = None

            if data:

class LRUCache:
    def __init__(self, capacity):
        self.capacity = capacity
        self.cache = OrderedDict()
        # print("LRUCache initialized")

    def dumpdata(self):
        print("Dumping data")
        for key in self.cache:
            p1 = pickle.dumps(self.cache[key])
            db.files.replace_one({"name": key}, {"name": key, "data": p1})
        self.cache = OrderedDict()

    def get(self, key):
            value = self.cache.pop(key)
            self.cache[key] = value
            return value
        except KeyError:
            return -1

    def set(self, key, value):
        except KeyError:
            if len(self.cache) >= self.capacity:
                cacheItem = self.cache.popitem(last=False)
                p1 = pickle.dumps(cacheItem[1])
                db.files.replace_one({"name": cacheItem[0]}, {"name": cacheItem[0], "data": p1})
        self.cache[key] = value
Example #12
def find_unique_url_multi_dict(file):
    current_dict = OrderedDict()
    dicts = [current_dict]

    with open(file) as f:
        line_no = 0
        for url in f:
            line_no += 1
            if len(current_dict) > 100:
                current_dict = OrderedDict()
            if url in current_dict:
                current_dict[url] = line_no

    while dicts:
        d = dicts.pop(0)
        for k, v in d.iteritems():
            not_found = True
            for d1 in dicts:
                if k not in d1:
                    not_found = False
            if not_found:
                return k, v
Example #13
class LRUCache(object):
    def __init__(self, capacity):
        :type capacity: int
        self.capacity = capacity
        self.cache = OrderedDict()

    def get(self, key):
        :rtype: int
            value = self.cache.pop(key)
            self.cache[key] = value
            return value
        except KeyError:
            return -1

    def set(self, key, value):
        :type key: int
        :type value: int
        :rtype: nothing
        except KeyError:
            if len(self.cache) >= self.capacity:
        self.cache[key] = value
Example #14
  def _topological_sort(self, goal_info_by_goal):
    dependees_by_goal = OrderedDict()

    def add_dependee(goal, dependee=None):
      dependees = dependees_by_goal.get(goal)
      if dependees is None:
        dependees = set()
        dependees_by_goal[goal] = dependees
      if dependee:

    for goal, goal_info in goal_info_by_goal.items():
      for dependency in goal_info.goal_dependencies:
        add_dependee(dependency, goal)

    satisfied = set()
    while dependees_by_goal:
      count = len(dependees_by_goal)
      for goal, dependees in dependees_by_goal.items():
        unsatisfied = len(dependees - satisfied)
        if unsatisfied == 0:
          yield goal_info_by_goal[goal]
      if len(dependees_by_goal) == count:
        for dependees in dependees_by_goal.values():
        # TODO(John Sirois): Do a better job here and actually collect and print cycle paths
        # between Goals/Tasks.  The developer can most directly address that data.
        raise self.GoalCycleError('Cycle detected in goal dependencies:\n\t{0}'
                                  .format('\n\t'.join('{0} <- {1}'.format(goal, list(dependees))
                                                      for goal, dependees
                                                      in dependees_by_goal.items())))
Example #15
class LRUCache(object):

    """ Simple LRU cache for template instance caching.

        in fact, the OrderedDict in collections module or
        @functools.lru_cache is working well too.


    def __init__(self, capacity):
        self.capacity = capacity
        self.cache = OrderedDict()

    def get(self, key):
        """ Return -1 if catched KeyError exception.

            value = self.cache.pop(key)
            self.cache[key] = value
            return value
        except KeyError:
            return -1

    def set(self, key, value):
        except KeyError:
            if len(self.cache) >= self.capacity:
        self.cache[key] = value
Example #16
class LRUCache(object):
    def __init__(self, capacity):
        :type capacity: int
        self.capacity = capacity
        self.cache = OrderedDict()

    def get(self, key):
        :rtype: int
        if key in self.cache:
            val = self.cache.pop(key)
            self.cache[key] = val
            return val
            return -1

    def set(self, key, value):
        :type key: int
        :type value: int
        :rtype: nothing
        if key not in self.cache:
            if self.capacity > 0:
                self.capacity -= 1
        self.cache[key] = value
def extract_properties_from_manifest(manifest_src):
    """return a dictionary with the properties of a task read from a task manifest if it exists"""

    if not any(key.lower() in manifest_src.lower() for key in MANIFEST_ALIASES_GROUPED["manifest_version"]):
        raise VersionError("Missing Task Manifest Version, must be v0")

    props = ordered_load(manifest_src)

    if not isinstance(props, dict):
        raise TypeError("Docstring should be a yaml dictionary. Got %s instead." % type(props).__name__)

    props = OrderedDict((MANIFEST_ALIASING.get(p.lower(), p.lower()), v) for p, v in props.items())

    if "manifest_version" not in props:
        # note that it MUST be present because we check for it in the entire string just above
        # probably this is broken yaml
        raise KeyError("Missing Task Manifest Version, must be present")

    if props["manifest_version"] != 1:
        raise VersionError("Unknown Task Manifest Version %s" % props["manifest_version"])

    props = parse_args_from_manifest(props)


    return props
Example #18
class BlobCache(object):
    def __init__(self, max_bytes=10000000, max_time=5400):
        self.max_bytes = max_bytes
        self.max_time = max_time
        self.cache = OrderedDict()
        self.cache_size = 0

    def get_blob(self, blob_id):
        """Return (mod_time, headers, content)"""
        return self.cache.get(blob_id) or (None, None, None)

    def add_blob(self, blob_id, headers, content):
        """Add blob, refreshing cache, if need be"""
        if blob_id in self.cache:

        self.cache_size += len(content)
        cur_time = time.time()
        for bid in self.cache.keys():
            btime, bheaders, bcontent = self.cache[bid]
            if (cur_time - btime) > self.max_time or self.cache_size > self.max_bytes:
                self.cache_size -= len(bcontent)
        self.cache[blob_id] = (cur_time, headers, content)

    def delete_blob(self, blob_id):
        if blob_id in self.cache:
            btime, bheaders, bcontent = self.cache.pop(blob_id)
            self.cache_size -= len(bcontent)
def SLRU(listamd, cacheprotegida):

    # Declaracion de Caches
    cprueba = OrderedDict()
    cprotegida = OrderedDict()

    hits = 0
    misses = 0
    cantidad = len(listamd)

    for elemento in listamd:
        if len(cprotegida) == cacheprotegida:
            cprueba.popitem(last=False)  # Elimina el LRU si la cache protegida esta llena

            # Si la cache tiene elementos y no esta llena
        if hash(elemento) in cprueba:  # Busca de hay un hit en la cache de prueba
            cprotegida[hash(elemento)] = 0
            hits += 1
        elif hash(elemento) in cprotegida:  # Busca si el elemento se encuentra en la cache protegida se produce un hit
            cprotegida[hash(cprotegida.pop(hash(elemento)))] = 0
            hits += 1
        else:  # se produce un miss
            if len(cprueba) < cacheprueba:
                cprueba[hash(elemento)] = 0
                misses += 1
            elif len(cprueba) == cacheprueba:
                cprueba[hash(elemento)] = 0
                misses += 1
            if cacheprotegida == len(cprotegida) and len(cprueba) == 0:
    return (hits, misses, cantidad)
Example #20
def run(state, include=None, exclude=None):
    Run data validations for state.

    State is required. Optionally filter validations using include/exclude flags.
    if include and exclude:
        sys.exit("ERROR: You can not use both include and exclude flags!")

    state_mod = load_module(state, ["validate"])
    # Load all validations in order found
    validations = OrderedDict()
    for name in dir(state_mod.validate):
        if name.startswith("validate_"):
            func = getattr(state_mod.validate, name)
            validations[name] = func

    # Filter validations based in include/exclude flags
    if include:
        to_run = split_args(include)
        for val in validations:
            if val not in to_run:
    if exclude:
        to_skip = split_args(exclude)
        for val in validations:
            if val in to_skip:

    # Run remaining validations
    run_validation(state, validations.values())
Example #21
class LruCache(object):
	Least-recently-used cache mapping of
	size @maxsiz

    def __init__(self, maxsiz):
        self.d = OrderedDict()
        self.maxsiz = maxsiz

    def __contains__(self, key):
        return key in self.d

    def __setitem__(self, key, value):
        self.d.pop(key, None)
        self.d[key] = value
        if len(self.d) > self.maxsiz:
            # remove the first item (was inserted longest time ago)
            lastkey = next(iter(self.d))

    def __getitem__(self, key):
            value = self.d.pop(key)
        except KeyError:
            # reinsert the value, puts it "last" in the order
        self.d[key] = value
        return value
Example #22
class Preset(object):

    def __init__(self):
        self.home_dir = expanduser("~")
        self.config_dir_path = os.path.abspath(os.path.join(str(self.home_dir), ".open-labels"))
        self.presets_file_path = os.path.abspath(os.path.join(str(self.config_dir_path), "presets.json"))
        self.preset_data = {}

        if os.path.exists(self.config_dir_path) == False:

    def write_default_preset(self):
        presets = {
            "30x20 label": {"name": "30x20 label", "width": 30, "height": 20},
            "40x20 label": {"name": "40x20 label", "width": 40, "height": 20},
        self.preset_data = presets

    def get_preset(self, name):
        return self.preset_data.get(name)

    def save_preset(self, ps):
        if "width" in ps and str(ps["width"]) == "":
            ps["width"] = 0
        if "height" in ps and str(ps["height"]) == "":
            ps["height"] = 0
        if "x_gap" in ps and str(ps["x_gap"]) == "":
            ps["x_gap"] = 0
        if "y_gap" in ps and str(ps["y_gap"]) == "":
            ps["y_gap"] = 0
        if "paper_width" in ps and str(ps["paper_width"]) == "":
            ps["paper_width"] = 0
        if "labels_per_row" in ps and str(ps["labels_per_row"]) == "":
            ps["labels_per_row"] = 0
        if "x_offset" in ps and str(ps["x_offset"]) == "":
            ps["x_offset"] = 0
        if "y_offset" in ps and str(ps["y_offset"]) == "":
            ps["y_offset"] = 0

        with open(self.presets_file_path, "w") as tfile:
            json.dump(self.preset_data, tfile)

    def read_presets(self):
        if os.path.exists(self.presets_file_path) and os.path.isfile(self.presets_file_path):
            with open(self.presets_file_path, "r") as data_file:
                file_content = data_file.read()
                self.preset_data = json.loads(file_content)

        self.preset_data = OrderedDict(sorted(self.preset_data.items(), key=lambda t: t[0]))

    def delete_preset(self, name):
Example #23
    def get_rack_units(self, face=RACK_FACE_FRONT, exclude=None, remove_redundant=False):
        Return a list of rack units as dictionaries. Example: {'device': None, 'face': 0, 'id': 48, 'name': 'U48'}
        Each key 'device' is either a Device or None. By default, multi-U devices are repeated for each U they occupy.

        :param face: Rack face (front or rear)
        :param exclude: PK of a Device to exclude (optional); helpful when relocating a Device within a Rack
        :param remove_redundant: If True, rack units occupied by a device already listed will be omitted

        elevation = OrderedDict()
        for u in reversed(range(1, self.u_height + 1)):
            elevation[u] = {"id": u, "name": "U{}".format(u), "face": face, "device": None}

        # Add devices to rack units list
        if self.pk:
            for device in (
                Device.objects.select_related("device_type__manufacturer", "device_role")
                .filter(rack=self, position__gt=0)
                .filter(Q(face=face) | Q(device_type__is_full_depth=True))
                if remove_redundant:
                    elevation[device.position]["device"] = device
                    for u in range(device.position + 1, device.position + device.device_type.u_height):
                        elevation.pop(u, None)
                    for u in range(device.position, device.position + device.device_type.u_height):
                        elevation[u]["device"] = device

        return [u for u in elevation.values()]
Example #24
class OrderedDict(object):
    def __init__(self, eq_func=None, hash_func=None):
        self.contents = PyOrderedDict()
        self.eq_func = eq_func or operator.eq
        self.hash_func = hash_func or hash

    def __getitem__(self, key):
        return self.contents[self._key(key)]

    def __setitem__(self, key, value):
        self.contents[self._key(key)] = value

    def __delitem__(self, key):
        del self.contents[self._key(key)]

    def __contains__(self, key):
        return self._key(key) in self.contents

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

    def _key(self, key):
        return DictKey(self, key)

    def keys(self):
        return [k.key for k in self.contents.keys()]

    def values(self):
        return self.contents.values()

    def iteritems(self):
        for k, v in self.contents.iteritems():
            yield k.key, v

    def get(self, key, default):
        return self.contents.get(self._key(key), default)

    def pop(self, key, default=MARKER):
        if default is MARKER:
            return self.contents.pop(self._key(key))
            return self.contents.pop(self._key(key), default)

    def popitem(self):
        if not self:
            raise KeyError
        k, v = self.contents.popitem()
        return k.key, v

    def update(self, d):

    def clear(self):

    def copy(self):
        d = OrderedDict(self.eq_func, self.hash_func)
        for k, v in self.iteritems():
            d[k] = v
        return d
Example #25
class PointSpace(object):
    """Given a point will assign numeric IDs"""

    def __init__(self):
        self.points = OrderedDict()
        self.point_to_id = {}
        self._updated = True

    def _update(self):
        if self._updated:
        current_id = 0
        for point in self.points:
            self.point_to_id[point] = current_id
            current_id += 1
        self._updated = True

    def add(self, point):
        self.points[point] = True
        self._updated = False

    def delete(self, point):
        self.points.pop(point, None)
        self._updated = False

    def get_id(self, point):
        return self.point_to_id.get(point)
    def longestConsecutiveSeq(self, num):
        seqDic = OrderedDict()

        for each in num:
            seqDic[each] = each

        maxlength = 0

        for key in num:
            left = key - 1
            right = key + 1
            cnt = 1

            while left in seqDic:
                cnt += 1
                left -= 1

            while right in seqDic:
                cnt += 1
                right += 1

            if cnt > maxlength:
                maxlength = cnt

        return maxlength
Example #27
class SessionBotRunner(object):
    def __init__(self, bots, session_code):
        self.bots = OrderedDict()

        for bot in bots:
            self.bots[bot.participant.id] = bot

    def play_until_end(self):
        loops_without_progress = 0
        while True:
            if len(self.bots) == 0:
                print("Bots done!")
            if loops_without_progress > 2:
                raise AssertionError("Bots got stuck")
            # store in a separate list so we don't mutate the iterable
            playable_ids = list(self.bots.keys())
            progress_made = False
            for pk in playable_ids:
                bot = self.bots[pk]
                if bot.on_wait_page():
                        submission = next(bot.submits_generator)
                    except StopIteration:
                        # this bot is finished
                        progress_made = True
                        progress_made = True
            if not progress_made:
                loops_without_progress += 1
Example #28
    def __new__(mcs, name, bases, attrs):
        # Collect fields from current class.
        current_fields = []
        for key, value in list(attrs.items()):
            if isinstance(value, Field):
                current_fields.append((key, value))
        current_fields.sort(key=lambda x: x[1].creation_counter)
        attrs["declared_fields"] = OrderedDict(current_fields)

        new_class = super(DeclarativeFieldsMetaclass, mcs).__new__(mcs, name, bases, attrs)

        # Walk through the MRO.
        declared_fields = OrderedDict()
        for base in reversed(new_class.__mro__):
            # Collect fields from base class.
            if hasattr(base, "declared_fields"):

            # Field shadowing.
            for attr, value in base.__dict__.items():
                if value is None and attr in declared_fields:

        new_class.base_fields = declared_fields
        new_class.declared_fields = declared_fields

        return new_class
Example #29
    def set_attribute_order(self, attribute_order=None, sort=False):
        Re-order attributes based on attribute_order list. Any attributes
        listed in attribute_order will appear first (and in that order); any
        remaining attributes will follow in original order. If sort is set
        to true, remaining attributes will appear in case-insensitive sorted

        d = OrderedDict(self)
        if attribute_order:
            for attribute_name in attribute_order:
                if attribute_name in d:
                    self[attribute_name] = d[attribute_name]
        if sort and d:
            # Do a case-insensitive sort on remaining attributes.
            for key in sorted(d, key=str.lower):
                self[key] = d[key]
        elif d:
            # If there are any remaining attribute names in d, add them now.
            for key in d:
                self[key] = d[key]
        del d
        if self.token:
Example #30
class Basic(object):
    """ Basic object deals with Basic HTTP Authorization configuration file.
    It is passed the path to userdb file. """

    def __init__(self, userdb):
        self.userdb = userdb
        self.initial_users = OrderedDict()
        self.new_users = OrderedDict()

    def __enter__(self):
        with open(self.userdb, "r") as users:
            for i in users:
                user, password = i.split(":", 1)
                self.initial_users[user] = password
        self.new_users = self.initial_users.copy()
        return self

    def __exit__(self, type, value, traceback):
        if self.new_users == self.initial_users:
        with open(self.userdb, "w") as userdb:
            for user in self.new_users:
                userdb.write("%s:%s" % (user, self.new_users[user]))

    def __contains__(self, user):
        return user in self.users

    def users(self):
        """ Returns users in a tuple """
        return list(self.new_users.keys())

    def add(self, user, password):
        """ Adds a user with password """
        if self.__contains__(user):
            raise UserExists
        self.new_users[user] = self._crypt_password(password) + "\n"

    def pop(self, user):
        """ Deletes a user """
        if not self.__contains__(user):
            raise UserNotExists

    def change_password(self, user, password):
        """ Changes user password """
        if not self.__contains__(user):
            raise UserNotExists
        self.new_users[user] = self._crypt_password(password) + "\n"

    def _crypt_password(self, password):
        """ Crypts password """

        def salt():
            """ Generates some salt """
            symbols = ascii_letters + digits
            return choice(symbols) + choice(symbols)

        return crypt(password, salt())