Returns a truncated random walk.

        path_length: Length of the random walk.
        alpha: probability of restarts.
        start: the start node of the random walk.
545567
def random_walk(self, path_length, alpha=0, rand=random.Random(), start=None):
    
    G = self
    if start:
      path = [start]
    else:
      # Sampling is uniform w.r.t V, and not w.r.t E
      path = [rand.choice(list(G.keys()))]

    while len(path) < path_length:
      cur = path[-1]
      if len(G[cur]) > 0:
        if rand.random() >= alpha:
          path.append(rand.choice(G[cur]))
        else:
          path.append(path[0])
      else:
        break
    return [str(node) for node in path]
598550
def calculate_tree_length(self):
        
        tree_length = 0

        for ext in self.tree:
            tree_length += len(ext) + 2

            for relpath in self.tree[ext]:
                tree_length += len(relpath) + 2

                for filename in self.tree[ext][relpath]:
                    tree_length += len(filename) + 1 + 18

        return tree_length + 1
920550
def _find_short_paths(self, paths):
        
        # Split each path to parts.
        # E.g. '/home/aoik' to ['', 'home', 'aoik']
        path_parts_s = [path.split(os.path.sep) for path in paths]

        # Root node
        root_node = {}

        # Sort these path parts by length, with the longest being the first.
        #
        # Longer paths appear first so that their extra parts are discarded
        # when a shorter path is found at 5TQ8L.
        #
        # Then for each path's parts.
        for parts in sorted(path_parts_s, key=len, reverse=True):
            # Start from the root node
            node = root_node

            # For each part of the path
            for part in parts:
                # Create node of the path
                node = node.setdefault(part, {})

            # 5TQ8L
            # Clear the last path part's node's child nodes.
            #
            # This aims to keep only the shortest path that needs be watched.
            #
            node.clear()

        # Short paths
        short_path_s = set()

        # Collect leaf paths
        self._collect_leaf_paths(
            node=root_node,
            path_parts=(),
            leaf_paths=short_path_s,
        )

        # Return short paths
        return short_path_s
155420
def p_length(self, p):
        ''
        p[0] = Length(p[2], p[4], lineno=p.lineno(1))
        p.set_lineno(0, p.lineno(1))
981205
def relpath(self, path):
        
        return os.path.relpath(path, start=self.path)
913173
def _walker(self, path):
        """"""
        for root, dnames, fnames in _walk(self.recursion)(path):
            yield (root, dnames, fnames)
181030
def length(self, error=ERROR, min_depth=MIN_DEPTH):
        """"""
        start_point = self.point(0)
        end_point = self.point(1)
        return segment_length(self, 0, 1, start_point, end_point, error, min_depth, 0)
1116219
def vertical_random_walk(size):
    
    width, _ = size
    distribution = {(dx, 1): 1 / 3.0 for dx in [-1, 0, 1]}
    start_points = [(x, 0) for x in range(width)]
    return random_walk_path(size, distribution, start_points)
398983
def relpath(path, start):
    
    path_items = path_list(path)
    start_items = path_list(start)

    # Find common parts of path.
    common = []
    for pth, stt in zip(path_items, start_items):
        if pth != stt:
            break
        common.append(pth)

    # Shared parts index in both lists.
    common_ind = len(common)
    parent_num = len(start_items) - common_ind

    # Start with parent traversal and add relative parts.
    rel_items = [PARENT] * parent_num + path_items[common_ind:]
    return path_join(*rel_items)
394561
def length(self, t0=0, t1=1, error=None, min_depth=None):
        """"""
        return abs(self.end - self.start)*(t1-t0)
997315
def parallel_generate_walks(d_graph, global_walk_length, num_walks, cpu_num, sampling_strategy=None,
                            num_walks_key=None, walk_length_key=None, neighbors_key=None, probabilities_key=None,
                            first_travel_key=None, quiet=False):
    

    walks = list()



    if not quiet:
        pbar = tqdm(total=num_walks, desc='Generating walks (CPU: {})'.format(cpu_num))

    for n_walk in range(num_walks):

        # Update progress bar
        if not quiet:
            pbar.update(1)

        # Shuffle the nodes
        shuffled_nodes = list(d_graph.keys())
        random.shuffle(shuffled_nodes)

        # Start a random walk from every node
        for source in shuffled_nodes:

            # Skip nodes with specific num_walks
            if source in sampling_strategy and \
                    num_walks_key in sampling_strategy[source] and \
                    sampling_strategy[source][num_walks_key] <= n_walk:
                continue

            # Start walk
            walk = [source]

            # Calculate walk length
            if source in sampling_strategy:
                walk_length = sampling_strategy[source].get(walk_length_key, global_walk_length)
            else:
                walk_length = global_walk_length

            # Perform walk
            while len(walk) < walk_length:

                walk_options = d_graph[walk[-1]].get(neighbors_key, None)

                # Skip dead end nodes
                if not walk_options:
                    break

                if len(walk) == 1:  # For the first step
                    probabilities = d_graph[walk[-1]][first_travel_key]
                    walk_to = np.random.choice(walk_options, size=1, p=probabilities)[0]
                else:
                    probabilities = d_graph[walk[-1]][probabilities_key][walk[-2]]
                    walk_to = np.random.choice(walk_options, size=1, p=probabilities)[0]

                walk.append(walk_to)

            walk = list(map(str, walk))  # Convert all to strings

            walks.append(walk)

    if not quiet:
        pbar.close()

    return walks
348147
def os_path_relpath(path, start=os.path.curdir):
    """"""

    if not path:
        raise ValueError("no path specified")

    start_list = [x for x in os.path.abspath(start).split(os.path.sep) if x]
    path_list = [x for x in os.path.abspath(path).split(os.path.sep) if x]

    # Work out how much of the filepath is shared by start and path.
    i = len(os.path.commonprefix([start_list, path_list]))

    rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
    if not rel_list:
        return os.path.curdir
    return os.path.join(*rel_list)
270120
def extract_length(self, lower_bound=None, upper_bound=None, new_path=None):
        """"""
        # Temporary path #
        if new_path is None: fraction = self.__class__(new_temp_path())
        elif isinstance(new_path, FASTA): fraction = new_path
        else:                fraction = self.__class__(new_path)
        # Generator #
        if lower_bound is None: lower_bound = 0
        if upper_bound is None: upper_bound = sys.maxint
        def fraction_iterator():
            for read in self:
                if lower_bound <= len(read) <= upper_bound:
                    yield read
        # Do it #
        fraction.write(fraction_iterator())
        fraction.close()
        return fraction
1156517
def request_length(self):
        
        remainder = self.stop_at - self.offset
        return self.chunk_size if remainder > self.chunk_size else remainder
524486
def size(self, type=None, failed=False):
        
        return len(self.nodes(type=type, failed=failed))
481369
def find_path(self, start, end, grid):
        
        self.start_time = time.time()  # execution time limitation
        self.runs = 0  # count number of iterations

        start_open_list = [start]
        start.g = 0
        start.f = 0
        start.opened = BY_START

        end_open_list = [end]
        end.g = 0
        end.f = 0
        end.opened = BY_END

        while len(start_open_list) > 0 and len(end_open_list) > 0:
            self.runs += 1
            self.keep_running()
            path = self.check_neighbors(start, end, grid, start_open_list,
                                        open_value=BY_START,
                                        backtrace_by=BY_END)
            if path:
                return path, self.runs

            self.runs += 1
            self.keep_running()
            path = self.check_neighbors(end, start, grid, end_open_list,
                                        open_value=BY_END,
                                        backtrace_by=BY_START)
            if path:
                return path, self.runs

        # failed to find path
        return [], self.runs
1172692
def length(self):
        
        length = 0
        for r in self._routes:
            length = length + r.length()

        return length