if isinstance(ob_space, Discrete):<EOL><INDENT>return tf.to_float(tf.one_hot(placeholder, ob_space.n))<EOL><DEDENT>elif isinstance(ob_space, Box):<EOL><INDENT>return tf.to_float(placeholder)<EOL><DEDENT>elif isinstance(ob_space, MultiDiscrete):<EOL><INDENT>placeholder = tf.cast(placeholder, tf.int32)<EOL>one_hots = [tf.to_float(tf.one_hot(placeholder[..., i], ob_space.nvec[i])) for i in range(placeholder.shape[-<NUM_LIT:1>])]<EOL>return tf.concat(one_hots, axis=-<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>

Encode input in the way that is appropriate to the observation space

Parameters:
----------

ob_space: gym.Space             observation space

placeholder: tf.placeholder     observation input placeholder


def observation_input(ob_space, batch_size=None, name='<STR_LIT>'):

placeholder = observation_placeholder(ob_space, batch_size, name)<EOL>return placeholder, encode_observation(ob_space, placeholder)<EOL>

Create placeholder to feed observations into of the size appropriate to the observation space, and add input
encoder of the appropriate type.


def min(self, start=<NUM_LIT:0>, end=None):

return super(MinSegmentTree, self).reduce(start, end)<EOL>


if not isinstance(data, np.ndarray) and not isinstance(data, list):<EOL><INDENT>return data<EOL><DEDENT>if isinstance(data, list):<EOL><INDENT>data = np.array(data)<EOL><DEDENT>placeholder_shape = [x or -<NUM_LIT:1> for x in placeholder.shape.as_list()]<EOL>assert _check_shape(placeholder_shape, data.shape),'<STR_LIT>'.format(data.shape, placeholder_shape)<EOL>return np.reshape(data, placeholder_shape)<EOL>

adjust shape of the data to the shape of the placeholder if possible.
If shape is incompatible, AssertionError is thrown

Parameters:
    placeholder     tensorflow input placeholder

    data            input data to be (potentially) reshaped to be fed into placeholder

Returns:
    reshaped data


import subprocess<EOL>subprocess.Popen(['<STR_LIT>', '<STR_LIT>', log_dir])<EOL>

To log the Tensorflow graph when using rl-algs
algorithms, you can run the following code
in your main script:
    import threading, time
    def start_tensorboard(session):
        time.sleep(10) # Wait until graph is setup
        tb_path = osp.join(logger.get_dir(), 'tb')
        summary_writer = tf.summary.FileWriter(tb_path, graph=session.graph)
        summary_op = tf.summary.merge_all()
        launch_tensorboard_in_background(tb_path)
    session = tf.get_default_session()
    t = threading.Thread(target=start_tensorboard, args=([session]))
    t.start()


sess = tf.get_default_session()<EOL>if sess is None:<EOL><INDENT>sess = make_session(config=config, make_default=True)<EOL><DEDENT>return sess<EOL>

Get default session or create one with a given config


return True<EOL>squeezed_placeholder_shape = _squeeze_shape(placeholder_shape)<EOL>squeezed_data_shape = _squeeze_shape(data_shape)<EOL>for i, s_data in enumerate(squeezed_data_shape):<EOL><INDENT>s_placeholder = squeezed_placeholder_shape[i]<EOL>if s_placeholder != -<NUM_LIT:1> and s_data != s_placeholder:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>

check if two shapes are compatible (i.e. differ only by dimensions of size 1, or by the batch dimension)


def function(inputs, outputs, updates=None, givens=None):

if isinstance(outputs, list):<EOL><INDENT>return _Function(inputs, outputs, updates, givens=givens)<EOL><DEDENT>elif isinstance(outputs, (dict, collections.OrderedDict)):<EOL><INDENT>f = _Function(inputs, outputs.values(), updates, givens=givens)<EOL>return lambda *args, **kwargs: type(outputs)(zip(outputs.keys(), f(*args, **kwargs)))<EOL><DEDENT>else:<EOL><INDENT>f = _Function(inputs, [outputs], updates, givens=givens)<EOL>return lambda *args, **kwargs: f(*args, **kwargs)[<NUM_LIT:0>]<EOL><DEDENT>

Just like Theano function. Take a bunch of tensorflow placeholders and expressions
    computed based on those placeholders and produces f(inputs) -> outputs. Function f takes
    values to be fed to the input's placeholders and produces the values of the expressions
    in outputs.

    Input values can be passed in the same order as inputs or can be provided as kwargs based
    on placeholder name (passed to constructor or accessible via placeholder.op.name).

    Example:
        x = tf.placeholder(tf.int32, (), name="x")
        y = tf.placeholder(tf.int32, (), name="y")
        z = 3 * x + 2 * y
        lin = function([x, y], z, givens={y: 0})

        with single_threaded_session():
            initialize()

            assert lin(2) == 6
            assert lin(x=3) == 9
            assert lin(2, 2) == 10
            assert lin(x=2, y=3) == 12

    Parameters
    ----------
    inputs: [tf.placeholder, tf.constant, or object with make_feed_dict method]
        list of input arguments
    outputs: [tf.Variable] or tf.Variable
        list of outputs or a single output to be returned from function. Returned
        value will also have the same shape.
    updates: [tf.Operation] or tf.Operation
        list of update functions or single update function that will be run whenever
        the function is called. The return is ignored.


def symmetric_ema(xolds, yolds, low=None, high=None, n=<NUM_LIT>, decay_steps=<NUM_LIT:1.>, low_counts_threshold=<NUM_LIT>):

xs, ys1, count_ys1 = one_sided_ema(xolds, yolds, low, high, n, decay_steps, low_counts_threshold=<NUM_LIT:0>)<EOL>_,  ys2, count_ys2 = one_sided_ema(-xolds[::-<NUM_LIT:1>], yolds[::-<NUM_LIT:1>], -high, -low, n, decay_steps, low_counts_threshold=<NUM_LIT:0>)<EOL>ys2 = ys2[::-<NUM_LIT:1>]<EOL>count_ys2 = count_ys2[::-<NUM_LIT:1>]<EOL>count_ys = count_ys1 + count_ys2<EOL>ys = (ys1 * count_ys1 + ys2 * count_ys2) / count_ys<EOL>ys[count_ys < low_counts_threshold] = np.nan<EOL>return xs, ys, count_ys<EOL>

perform symmetric EMA (exponential moving average)
smoothing and resampling to an even grid with n points.
Does not do extrapolation, so we assume
xolds[0] <= low && high <= xolds[-1]

Arguments:

xolds: array or list  - x values of data. Needs to be sorted in ascending order
yolds: array of list  - y values of data. Has to have the same length as xolds

low: float            - min value of the new x grid. By default equals to xolds[0]
high: float           - max value of the new x grid. By default equals to xolds[-1]

n: int                - number of points in new x grid

decay_steps: float    - EMA decay factor, expressed in new x grid steps.

low_counts_threshold: float or int
                      - y values with counts less than this value will be set to NaN

Returns:
    tuple sum_ys, count_ys where
        xs        - array with new x grid
        ys        - array of EMA of y at each point of the new x grid
        count_ys  - array of EMA of y counts at each point of the new x grid


def one_sided_ema(xolds, yolds, low=None, high=None, n=<NUM_LIT>, decay_steps=<NUM_LIT:1.>, low_counts_threshold=<NUM_LIT>):

low = xolds[<NUM_LIT:0>] if low is None else low<EOL>high = xolds[-<NUM_LIT:1>] if high is None else high<EOL>assert xolds[<NUM_LIT:0>] <= low, '<STR_LIT>'.format(low, xolds[<NUM_LIT:0>])<EOL>assert xolds[-<NUM_LIT:1>] >= high, '<STR_LIT>'.format(high, xolds[-<NUM_LIT:1>])<EOL>assert len(xolds) == len(yolds), '<STR_LIT>'.format(len(xolds), len(yolds))<EOL>xolds = xolds.astype('<STR_LIT>')<EOL>yolds = yolds.astype('<STR_LIT>')<EOL>luoi = <NUM_LIT:0> <EOL>sum_y = <NUM_LIT:0.><EOL>count_y = <NUM_LIT:0.><EOL>xnews = np.linspace(low, high, n)<EOL>decay_period = (high - low) / (n - <NUM_LIT:1>) * decay_steps<EOL>interstep_decay = np.exp(- <NUM_LIT:1.> / decay_steps)<EOL>sum_ys = np.zeros_like(xnews)<EOL>count_ys = np.zeros_like(xnews)<EOL>for i in range(n):<EOL><INDENT>xnew = xnews[i]<EOL>sum_y *= interstep_decay<EOL>count_y *= interstep_decay<EOL>while True:<EOL><INDENT>xold = xolds[luoi]<EOL>if xold <= xnew:<EOL><INDENT>decay = np.exp(- (xnew - xold) / decay_period)<EOL>sum_y += decay * yolds[luoi]<EOL>count_y += decay<EOL>luoi += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT>if luoi >= len(xolds):<EOL><INDENT>break<EOL><DEDENT><DEDENT>sum_ys[i] = sum_y<EOL>count_ys[i] = count_y<EOL><DEDENT>ys = sum_ys / count_ys<EOL>ys[count_ys < low_counts_threshold] = np.nan<EOL>return xnews, ys, count_ys<EOL>

perform one-sided (causal) EMA (exponential moving average)
smoothing and resampling to an even grid with n points.
Does not do extrapolation, so we assume
xolds[0] <= low && high <= xolds[-1]

Arguments:

xolds: array or list  - x values of data. Needs to be sorted in ascending order
yolds: array of list  - y values of data. Has to have the same length as xolds

low: float            - min value of the new x grid. By default equals to xolds[0]
high: float           - max value of the new x grid. By default equals to xolds[-1]

n: int                - number of points in new x grid

decay_steps: float    - EMA decay factor, expressed in new x grid steps.

low_counts_threshold: float or int
                      - y values with counts less than this value will be set to NaN

Returns:
    tuple sum_ys, count_ys where
        xs        - array with new x grid
        ys        - array of EMA of y at each point of the new x grid
        count_ys  - array of EMA of y counts at each point of the new x grid


if split_fn is None: split_fn = lambda _ : '<STR_LIT>'<EOL>if group_fn is None: group_fn = lambda _ : '<STR_LIT>'<EOL>sk2r = defaultdict(list) <EOL>for result in allresults:<EOL><INDENT>splitkey = split_fn(result)<EOL>sk2r[splitkey].append(result)<EOL><DEDENT>assert len(sk2r) > <NUM_LIT:0><EOL>assert isinstance(resample, int), "<STR_LIT>"<EOL>nrows = len(sk2r)<EOL>ncols = <NUM_LIT:1><EOL>figsize = figsize or (<NUM_LIT:6>, <NUM_LIT:6> * nrows)<EOL>f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize)<EOL>groups = list(set(group_fn(result) for result in allresults))<EOL>default_samples = <NUM_LIT><EOL>if average_group:<EOL><INDENT>resample = resample or default_samples<EOL><DEDENT>for (isplit, sk) in enumerate(sorted(sk2r.keys())):<EOL><INDENT>g2l = {}<EOL>g2c = defaultdict(int)<EOL>sresults = sk2r[sk]<EOL>gresults = defaultdict(list)<EOL>ax = axarr[isplit][<NUM_LIT:0>]<EOL>for result in sresults:<EOL><INDENT>group = group_fn(result)<EOL>g2c[group] += <NUM_LIT:1><EOL>x, y = xy_fn(result)<EOL>if x is None: x = np.arange(len(y))<EOL>x, y = map(np.asarray, (x, y))<EOL>if average_group:<EOL><INDENT>gresults[group].append((x,y))<EOL><DEDENT>else:<EOL><INDENT>if resample:<EOL><INDENT>x, y, counts = symmetric_ema(x, y, x[<NUM_LIT:0>], x[-<NUM_LIT:1>], resample, decay_steps=smooth_step)<EOL><DEDENT>l, = ax.plot(x, y, color=COLORS[groups.index(group) % len(COLORS)])<EOL>g2l[group] = l<EOL><DEDENT><DEDENT>if average_group:<EOL><INDENT>for group in sorted(groups):<EOL><INDENT>xys = gresults[group]<EOL>if not any(xys):<EOL><INDENT>continue<EOL><DEDENT>color = COLORS[groups.index(group) % len(COLORS)]<EOL>origxs = [xy[<NUM_LIT:0>] for xy in xys]<EOL>minxlen = min(map(len, origxs))<EOL>def allequal(qs):<EOL><INDENT>return all((q==qs[<NUM_LIT:0>]).all() for q in qs[<NUM_LIT:1>:])<EOL><DEDENT>if resample:<EOL><INDENT>low  = max(x[<NUM_LIT:0>] for x in origxs)<EOL>high = min(x[-<NUM_LIT:1>] for x in origxs)<EOL>usex = np.linspace(low, high, resample)<EOL>ys = []<EOL>for (x, y) in xys:<EOL><INDENT>ys.append(symmetric_ema(x, y, low, high, resample, decay_steps=smooth_step)[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>assert allequal([x[:minxlen] for x in origxs]),'<STR_LIT>'<EOL>usex = origxs[<NUM_LIT:0>]<EOL>ys = [xy[<NUM_LIT:1>][:minxlen] for xy in xys]<EOL><DEDENT>ymean = np.mean(ys, axis=<NUM_LIT:0>)<EOL>ystd = np.std(ys, axis=<NUM_LIT:0>)<EOL>ystderr = ystd / np.sqrt(len(ys))<EOL>l, = axarr[isplit][<NUM_LIT:0>].plot(usex, ymean, color=color)<EOL>g2l[group] = l<EOL>if shaded_err:<EOL><INDENT>ax.fill_between(usex, ymean - ystderr, ymean + ystderr, color=color, alpha=<NUM_LIT>)<EOL><DEDENT>if shaded_std:<EOL><INDENT>ax.fill_between(usex, ymean - ystd,    ymean + ystd,    color=color, alpha=<NUM_LIT>)<EOL><DEDENT><DEDENT><DEDENT>plt.tight_layout()<EOL>if any(g2l.keys()):<EOL><INDENT>ax.legend(<EOL>g2l.values(),<EOL>['<STR_LIT>'%(g, g2c[g]) for g in g2l] if average_group else g2l.keys(),<EOL>loc=<NUM_LIT:2> if legend_outside else None,<EOL>bbox_to_anchor=(<NUM_LIT:1>,<NUM_LIT:1>) if legend_outside else None)<EOL><DEDENT>ax.set_title(sk)<EOL><DEDENT>return f, axarr<EOL>

Plot multiple Results objects

xy_fn: function Result -> x,y           - function that converts results objects into tuple of x and y values.
                                          By default, x is cumsum of episode lengths, and y is episode rewards

split_fn: function Result -> hashable   - function that converts results objects into keys to split curves into sub-panels by.
                                          That is, the results r for which split_fn(r) is different will be put on different sub-panels.
                                          By default, the portion of r.dirname between last / and -<digits> is returned. The sub-panels are
                                          stacked vertically in the figure.

group_fn: function Result -> hashable   - function that converts results objects into keys to group curves by.
                                          That is, the results r for which group_fn(r) is the same will be put into the same group.
                                          Curves in the same group have the same color (if average_group is False), or averaged over
                                          (if average_group is True). The default value is the same as default value for split_fn

average_group: bool                     - if True, will average the curves in the same group and plot the mean. Enables resampling
                                          (if resample = 0, will use 512 steps)

shaded_std: bool                        - if True (default), the shaded region corresponding to standard deviation of the group of curves will be
                                          shown (only applicable if average_group = True)

shaded_err: bool                        - if True (default), the shaded region corresponding to error in mean estimate of the group of curves
                                          (that is, standard deviation divided by square root of number of curves) will be
                                          shown (only applicable if average_group = True)

figsize: tuple or None                  - size of the resulting figure (including sub-panels). By default, width is 6 and height is 6 times number of
                                          sub-panels.

legend_outside: bool                    - if True, will place the legend outside of the sub-panels.

resample: int                           - if not zero, size of the uniform grid in x direction to resample onto. Resampling is performed via symmetric
                                          EMA smoothing (see the docstring for symmetric_ema).
                                          Default is zero (no resampling). Note that if average_group is True, resampling is necessary; in that case, default
                                          value is 512.

smooth_step: float                      - when resampling (i.e. when resample > 0 or average_group is True), use this EMA decay parameter (in units of the new grid step).
                                          See docstrings for decay_steps in symmetric_ema or one_sided_ema functions.


def cg(f_Ax, b, cg_iters=<NUM_LIT:10>, callback=None, verbose=False, residual_tol=<NUM_LIT>):

p = b.copy()<EOL>r = b.copy()<EOL>x = np.zeros_like(b)<EOL>rdotr = r.dot(r)<EOL>fmtstr =  "<STR_LIT>"<EOL>titlestr =  "<STR_LIT>"<EOL>if verbose: print(titlestr % ("<STR_LIT>", "<STR_LIT>", "<STR_LIT>"))<EOL>for i in range(cg_iters):<EOL><INDENT>if callback is not None:<EOL><INDENT>callback(x)<EOL><DEDENT>if verbose: print(fmtstr % (i, rdotr, np.linalg.norm(x)))<EOL>z = f_Ax(p)<EOL>v = rdotr / p.dot(z)<EOL>x += v*p<EOL>r -= v*z<EOL>newrdotr = r.dot(r)<EOL>mu = newrdotr/rdotr<EOL>p = r + mu*p<EOL>rdotr = newrdotr<EOL>if rdotr < residual_tol:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if callback is not None:<EOL><INDENT>callback(x)<EOL><DEDENT>if verbose: print(fmtstr % (i+<NUM_LIT:1>, rdotr, np.linalg.norm(x)))  <EOL>return x<EOL>


if '<STR_LIT>' not in os.environ:<EOL><INDENT>if sys.platform == '<STR_LIT>': <EOL><INDENT>ids = []                 <EOL><DEDENT>else:<EOL><INDENT>lrank, _lsize = get_local_rank_size(MPI.COMM_WORLD)<EOL>ids = [lrank]<EOL><DEDENT>os.environ["<STR_LIT>"] = "<STR_LIT:U+002C>".join(map(str, ids))<EOL><DEDENT>

Set CUDA_VISIBLE_DEVICES to MPI rank if not already set


def dict_gather(comm, d, op='<STR_LIT>', assert_all_have_data=True):

if comm is None: return d<EOL>alldicts = comm.allgather(d)<EOL>size = comm.size<EOL>k2li = defaultdict(list)<EOL>for d in alldicts:<EOL><INDENT>for (k,v) in d.items():<EOL><INDENT>k2li[k].append(v)<EOL><DEDENT><DEDENT>result = {}<EOL>for (k,li) in k2li.items():<EOL><INDENT>if assert_all_have_data:<EOL><INDENT>assert len(li)==size, "<STR_LIT>" % (len(li), size, k)<EOL><DEDENT>if op=='<STR_LIT>':<EOL><INDENT>result[k] = np.mean(li, axis=<NUM_LIT:0>)<EOL><DEDENT>elif op=='<STR_LIT>':<EOL><INDENT>result[k] = np.sum(li, axis=<NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>assert <NUM_LIT:0>, op<EOL><DEDENT><DEDENT>return result<EOL>

Perform a reduction operation over dicts


def sync_from_root(sess, variables, comm=None):

if comm is None: comm = MPI.COMM_WORLD<EOL>import tensorflow as tf<EOL>values = comm.bcast(sess.run(variables))<EOL>sess.run([tf.assign(var, val)<EOL>for (var, val) in zip(variables, values)])<EOL>

Send the root node's parameters to every worker.
Arguments:
  sess: the TensorFlow session.
  variables: all parameter variables including optimizer's


this_node = platform.node()<EOL>ranks_nodes = comm.allgather((comm.Get_rank(), this_node))<EOL>node2rankssofar = defaultdict(int)<EOL>local_rank = None<EOL>for (rank, node) in ranks_nodes:<EOL><INDENT>if rank == comm.Get_rank():<EOL><INDENT>local_rank = node2rankssofar[node]<EOL><DEDENT>node2rankssofar[node] += <NUM_LIT:1><EOL><DEDENT>assert local_rank is not None<EOL>return local_rank, node2rankssofar[this_node]<EOL>

Returns the rank of each process on its machine
The processes on a given machine will be assigned ranks
    0, 1, 2, ..., N-1,
where N is the number of processes on this machine.

Useful if you want to assign one gpu per machine


localrank, _ = get_local_rank_size(comm)<EOL>if comm.Get_rank() == <NUM_LIT:0>:<EOL><INDENT>with open(path, '<STR_LIT:rb>') as fh:<EOL><INDENT>data = fh.read()<EOL><DEDENT>comm.bcast(data)<EOL><DEDENT>else:<EOL><INDENT>data = comm.bcast(None)<EOL>if localrank == <NUM_LIT:0>:<EOL><INDENT>os.makedirs(os.path.dirname(path), exist_ok=True)<EOL>with open(path, '<STR_LIT:wb>') as fh:<EOL><INDENT>fh.write(data)<EOL><DEDENT><DEDENT><DEDENT>comm.Barrier()<EOL>

Copies the file from rank 0 to all other ranks
Puts it in the same place on all machines


return self._evaluate(self.vf, ob, *args, **kwargs)<EOL>

Compute value estimate(s) given the observation(s)

Parameters:
----------

observation     observation data (either single or a batch)

**extra_feed    additional data such as state or mask (names of the arguments should match the ones in constructor, see __init__)

Returns:
-------
value estimate


return self._v<EOL>


raise NotImplementedError()<EOL>


self._v = value<EOL>

Value remains constant over time.

        Parameters
        ----------
        value: float
            Constant value of the schedule


img_nhwc = np.asarray(img_nhwc)<EOL>N, h, w, c = img_nhwc.shape<EOL>H = int(np.ceil(np.sqrt(N)))<EOL>W = int(np.ceil(float(N)/H))<EOL>img_nhwc = np.array(list(img_nhwc) + [img_nhwc[<NUM_LIT:0>]*<NUM_LIT:0> for _ in range(N, H*W)])<EOL>img_HWhwc = img_nhwc.reshape(H, W, h, w, c)<EOL>img_HhWwc = img_HWhwc.transpose(<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:4>)<EOL>img_Hh_Ww_c = img_HhWwc.reshape(H*h, W*w, c)<EOL>return img_Hh_Ww_c<EOL>

Tile N images into one big PxQ image
(P,Q) are chosen to be as close as possible, and if N
is square, then P=Q.

input: img_nhwc, list or array of images, ndim=4 once turned into array
    n = batch index, h = height, w = width, c = channel
returns:
    bigim_HWc, ndarray with ndim=3


@register("<STR_LIT>")<EOL>def conv_only(convs=[(<NUM_LIT:32>, <NUM_LIT:8>, <NUM_LIT:4>), (<NUM_LIT:64>, <NUM_LIT:4>, <NUM_LIT:2>), (<NUM_LIT:64>, <NUM_LIT:3>, <NUM_LIT:1>)], **conv_kwargs):

def network_fn(X):<EOL><INDENT>out = tf.cast(X, tf.float32) / <NUM_LIT><EOL>with tf.variable_scope("<STR_LIT>"):<EOL><INDENT>for num_outputs, kernel_size, stride in convs:<EOL><INDENT>out = layers.convolution2d(out,<EOL>num_outputs=num_outputs,<EOL>kernel_size=kernel_size,<EOL>stride=stride,<EOL>activation_fn=tf.nn.relu,<EOL>**conv_kwargs)<EOL><DEDENT><DEDENT>return out<EOL><DEDENT>return network_fn<EOL>

convolutions-only net

Parameters:
----------

conv:       list of triples (filter_number, filter_size, stride) specifying parameters for each layer.

Returns:

function that takes tensorflow tensor as input and returns the output of the last convolutional layer


def network_fn(X, nenv=<NUM_LIT:1>):<EOL><INDENT>nbatch = X.shape[<NUM_LIT:0>]<EOL>nsteps = nbatch // nenv<EOL>h = tf.layers.flatten(X)<EOL>M = tf.placeholder(tf.float32, [nbatch]) <EOL>S = tf.placeholder(tf.float32, [nenv, <NUM_LIT:2>*nlstm]) <EOL>xs = batch_to_seq(h, nenv, nsteps)<EOL>ms = batch_to_seq(M, nenv, nsteps)<EOL>if layer_norm:<EOL><INDENT>h5, snew = utils.lnlstm(xs, ms, S, scope='<STR_LIT>', nh=nlstm)<EOL><DEDENT>else:<EOL><INDENT>h5, snew = utils.lstm(xs, ms, S, scope='<STR_LIT>', nh=nlstm)<EOL><DEDENT>h = seq_to_batch(h5)<EOL>initial_state = np.zeros(S.shape.as_list(), dtype=float)<EOL>return h, {'<STR_LIT:S>':S, '<STR_LIT:M>':M, '<STR_LIT:state>':snew, '<STR_LIT>':initial_state}<EOL><DEDENT>return network_fn<EOL>

Builds LSTM (Long-Short Term Memory) network to be used in a policy.
Note that the resulting function returns not only the output of the LSTM
(i.e. hidden state of lstm for each step in the sequence), but also a dictionary
with auxiliary tensors to be set as policy attributes.

Specifically,
    S is a placeholder to feed current state (LSTM state has to be managed outside policy)
    M is a placeholder for the mask (used to mask out observations after the end of the episode, but can be used for other purposes too)
    initial_state is a numpy array containing initial lstm state (usually zeros)
    state is the output LSTM state (to be fed into S at the next call)

An example of usage of lstm-based policy can be found here: common/tests/test_doc_examples.py/test_lstm_example

Parameters:
----------

nlstm: int          LSTM hidden state size

layer_norm: bool    if True, layer-normalized version of LSTM is used

Returns:
-------

function that builds LSTM with a given input tensor / placeholder


scaled_images = tf.cast(unscaled_images, tf.float32) / <NUM_LIT><EOL>activ = tf.nn.relu<EOL>h = activ(conv(scaled_images, '<STR_LIT>', nf=<NUM_LIT:32>, rf=<NUM_LIT:8>, stride=<NUM_LIT:4>, init_scale=np.sqrt(<NUM_LIT:2>),<EOL>**conv_kwargs))<EOL>h2 = activ(conv(h, '<STR_LIT>', nf=<NUM_LIT:64>, rf=<NUM_LIT:4>, stride=<NUM_LIT:2>, init_scale=np.sqrt(<NUM_LIT:2>), **conv_kwargs))<EOL>h3 = activ(conv(h2, '<STR_LIT>', nf=<NUM_LIT:64>, rf=<NUM_LIT:3>, stride=<NUM_LIT:1>, init_scale=np.sqrt(<NUM_LIT:2>), **conv_kwargs))<EOL>h3 = conv_to_fc(h3)<EOL>return activ(fc(h3, '<STR_LIT>', nh=<NUM_LIT>, init_scale=np.sqrt(<NUM_LIT:2>)))<EOL>


if callable(name):<EOL><INDENT>return name<EOL><DEDENT>elif name in mapping:<EOL><INDENT>return mapping[name]<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(name))<EOL><DEDENT>

If you want to register your own network outside models.py, you just need:

Usage Example:
-------------
from baselines.common.models import register
@register("your_network_name")
def your_network_define(**net_kwargs):
    ...
    return network_fn


def boolean_flag(parser, name, default=False, help=None):

dest = name.replace('<STR_LIT:->', '<STR_LIT:_>')<EOL>parser.add_argument("<STR_LIT>" + name, action="<STR_LIT:store_true>", default=default, dest=dest, help=help)<EOL>parser.add_argument("<STR_LIT>" + name, action="<STR_LIT>", dest=dest)<EOL>

Add a boolean flag to argparse parser.

    Parameters
    ----------
    parser: argparse.Parser
        parser to add the flag to
    name: str
        --<name> will enable the flag, while --no-<name> will disable it
    default: bool or None
        default value of the flag
    help: str
        help string for the flag


return self._value<EOL>


minutes_left = seconds_left // <NUM_LIT><EOL>seconds_left %= <NUM_LIT><EOL>hours_left = minutes_left // <NUM_LIT><EOL>minutes_left %= <NUM_LIT><EOL>days_left = hours_left // <NUM_LIT><EOL>hours_left %= <NUM_LIT><EOL>def helper(cnt, name):<EOL><INDENT>return "<STR_LIT>".format(str(cnt), name, ('<STR_LIT:s>' if cnt > <NUM_LIT:1> else '<STR_LIT>'))<EOL><DEDENT>if days_left > <NUM_LIT:0>:<EOL><INDENT>msg = helper(days_left, '<STR_LIT>')<EOL>if hours_left > <NUM_LIT:0>:<EOL><INDENT>msg += '<STR_LIT>' + helper(hours_left, '<STR_LIT>')<EOL><DEDENT>return msg<EOL><DEDENT>if hours_left > <NUM_LIT:0>:<EOL><INDENT>msg = helper(hours_left, '<STR_LIT>')<EOL>if minutes_left > <NUM_LIT:0>:<EOL><INDENT>msg += '<STR_LIT>' + helper(minutes_left, '<STR_LIT>')<EOL><DEDENT>return msg<EOL><DEDENT>if minutes_left > <NUM_LIT:0>:<EOL><INDENT>return helper(minutes_left, '<STR_LIT>')<EOL><DEDENT>return '<STR_LIT>'<EOL>

Print the number of seconds in human readable format.

    Examples:
    2 days
    2 hours and 37 minutes
    less than a minute

    Paramters
    ---------
    seconds_left: int
        Number of seconds to be converted to the ETA
    Returns
    -------
    eta: str
        String representing the pretty ETA.


def __init__(self, gamma, init_value=None):

self._value = init_value<EOL>self._gamma = gamma<EOL>

Keep a running estimate of a quantity. This is a bit like mean
        but more sensitive to recent changes.

        Parameters
        ----------
        gamma: float
            Must be between 0 and 1, where 0 is the most sensitive to recent
            changes.
        init_value: float or None
            Initial value of the estimate. If None, it will be set on the first update.


def wrap_deepmind_retro(env, scale=True, frame_stack=<NUM_LIT:4>):

env = WarpFrame(env)<EOL>env = ClipRewardEnv(env)<EOL>if frame_stack > <NUM_LIT:1>:<EOL><INDENT>env = FrameStack(env, frame_stack)<EOL><DEDENT>if scale:<EOL><INDENT>env = ScaledFloatFrame(env)<EOL><DEDENT>return env<EOL>

Configure environment for retro games, using config similar to DeepMind-style Atari in wrap_deepmind


gym.ObservationWrapper.__init__(self, env)<EOL>(oldh, oldw, oldc) = env.observation_space.shape<EOL>newshape = (oldh//ratio, oldw//ratio, oldc)<EOL>self.observation_space = gym.spaces.Box(low=<NUM_LIT:0>, high=<NUM_LIT:255>,<EOL>shape=newshape, dtype=np.uint8)<EOL>


gym.ObservationWrapper.__init__(self, env)<EOL>(oldh, oldw, _oldc) = env.observation_space.shape<EOL>self.observation_space = gym.spaces.Box(low=<NUM_LIT:0>, high=<NUM_LIT:255>,<EOL>shape=(oldh, oldw, <NUM_LIT:1>), dtype=np.uint8)<EOL>


get_current().logkv_mean(key, val)<EOL>

The same as logkv(), but if called many times, values averaged.


get_current().logkv(key, val)<EOL>

Log a value of some diagnostic
Call this once for each diagnostic quantity, each iteration
If called many times, last value will be used.


return get_current().dumpkvs()<EOL>

Write all of the diagnostics from the current iteration


return get_current().get_dir()<EOL>

Get directory that log files are being written to.
will be None if there is no output directory (i.e., if you didn't call start)


get_current().log(*args, level=level)<EOL>

Write the sequence of args, with no separators, to the console and output files (if you've configured an output file).


def decorator_with_name(func):<EOL><INDENT>def func_wrapper(*args, **kwargs):<EOL><INDENT>with profile_kv(n):<EOL><INDENT>return func(*args, **kwargs)<EOL><DEDENT><DEDENT>return func_wrapper<EOL><DEDENT>return decorator_with_name<EOL>

Usage:
@profile("my_func")
def my_func(): code


@staticmethod<EOL><INDENT>def create_datapoint(name, columns, points):<DEDENT>

return {<EOL>"<STR_LIT>": "<STR_LIT:s>",<EOL>"<STR_LIT:name>": name,<EOL>"<STR_LIT>": columns,<EOL>"<STR_LIT>": points,<EOL>}<EOL>

Create datastructure in InfluxDB 0.8 data format
:param name:
:param columns:
:param points:
:return:


l = len(iterable)<EOL>for ndx in range(<NUM_LIT:0>, l, n):<EOL><INDENT>yield iterable[ndx:min(ndx + n, l)]<EOL><DEDENT>

From http://stackoverflow.com/a/8290508/270334
:param n:
:param iterable:


<EOL>statements = [<EOL>(Keyword.SELECT, self.select_stmt),<EOL>(Keyword.FROM, self.from_stmt)<EOL>]<EOL>if self.where_stmt:<EOL><INDENT>statements.append((Keyword.WHERE, self.where_stmt))<EOL><DEDENT>if self.limit_stmt:<EOL><INDENT>statements.append((Keyword.LIMIT, self.limit_stmt))<EOL><DEDENT>if self.group_by_stmt:<EOL><INDENT>statements.append((Keyword.GROUP_BY, ['<STR_LIT>', self.group_by_stmt]))<EOL><DEDENT>return self._format_statements(statements)<EOL>

Standard string representation of select query


def handle_error(self, request, client_address):

cls, e = sys.exc_info()[:<NUM_LIT:2>]<EOL>if cls is socket.error or cls is ssl.SSLError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>return HTTPServer.handle_error(self, request, client_address)<EOL><DEDENT>

Overwrite error handling to suppress socket/ssl related errors
:param client_address: Address of client
:param request: Request causing an error


parsed_params = urlparse.parse_qs(parameters)<EOL>if '<STR_LIT:q>' not in parsed_params:<EOL><INDENT>return []<EOL><DEDENT>queries = parsed_params['<STR_LIT:q>']<EOL>if not isinstance(queries, list):<EOL><INDENT>queries = [queries]<EOL><DEDENT>return queries<EOL>

Get a list of all queries (q=... parameters) from an URL parameter string
:param parameters: The url parameter list


def send_error(self, code, message=None):

message = message.strip()<EOL>self.log_error("<STR_LIT>", code, message)<EOL>self.send_response(code)<EOL>self.send_header("<STR_LIT:Content-Type>", "<STR_LIT>")<EOL>self.send_header('<STR_LIT>', '<STR_LIT>')<EOL>self.end_headers()<EOL>if message:<EOL><INDENT>self.wfile.write(message)<EOL><DEDENT>

Send and log plain text error reply.
:param code:
:param message:


self.filter_headers(response.msg)<EOL>if "<STR_LIT>" in response.msg:<EOL><INDENT>del response.msg["<STR_LIT>"]<EOL><DEDENT>self.send_response(response.status, response.reason)<EOL>for header_key, header_value in response.msg.items():<EOL><INDENT>self.send_header(header_key, header_value)<EOL><DEDENT>body = response.read()<EOL>self.send_header('<STR_LIT>', str(len(body)))<EOL>self.end_headers()<EOL>self.wfile.write(body)<EOL>


strings = []<EOL>with open(filepath, '<STR_LIT:r>') as f:<EOL><INDENT>strings = f.readlines()<EOL>strings = [x.strip(u'<STR_LIT:\n>') for x in strings]<EOL>strings = [x for x in strings if x and not x.startswith(u'<STR_LIT:#>')]<EOL>strings.insert(<NUM_LIT:0>, u"<STR_LIT>")<EOL><DEDENT>return strings<EOL>

Get the list of naughty_strings.

    By default this will get the strings from the blns.txt file

    Code is a simple port of what is already in the /scripts directory

    :param filepath: Optional filepath the the blns.txt file
    :returns: The list of naughty strings


def flatten(d, parent_key='<STR_LIT>', sep='<STR_LIT:_>'):

items = []<EOL>for k, v in d.items():<EOL><INDENT>new_key = parent_key + sep + k if parent_key else k<EOL>if isinstance(v, collections.MutableMapping):<EOL><INDENT>items.extend(flatten(v, new_key, sep=sep).items())<EOL><DEDENT>else:<EOL><INDENT>items.append((new_key, v))<EOL><DEDENT><DEDENT>return dict(items)<EOL>

Flatten keys in a dictionary
Example:
flatten({'a': 1, 'c': {'a': 2, 'b': {'x': 5, 'y' : 10}}, 'd': [1, 2, 3]})
=> {'a': 1, 'c_a': 2, 'c_b_x': 5, 'd': [1, 2, 3], 'c_b_y': 10}
:param d:  Dictionary to flatten
:param sep: Separator between keys
:param parent_key: Key to merge with


<EOL>config = flatten(default_config.DEFAULT_CONFIG)<EOL>cli_config = flatten(parse_args())<EOL>if "<STR_LIT>" in cli_config:<EOL><INDENT>logging.info("<STR_LIT>".format(cli_config['<STR_LIT>']))<EOL>configfile = parse_configfile(cli_config['<STR_LIT>'])<EOL>config = overwrite_config(config, configfile)<EOL><DEDENT>config = overwrite_config(config, cli_config)<EOL>if '<STR_LIT>' in config:<EOL><INDENT>if config['<STR_LIT>'] == <NUM_LIT:1>:<EOL><INDENT>logging.getLogger().setLevel(logging.INFO)<EOL><DEDENT>elif config['<STR_LIT>'] > <NUM_LIT:1>:<EOL><INDENT>logging.getLogger().setLevel(logging.DEBUG)<EOL><DEDENT><DEDENT>return ObjectView(config)<EOL>

Load settings from default config and optionally
overwrite with config file and commandline parameters
(in that order).


with open(configfile) as f:<EOL><INDENT>try:<EOL><INDENT>return yaml.safe_load(f)<EOL><DEDENT>except Exception as e:<EOL><INDENT>logging.fatal("<STR_LIT>", e)<EOL>exit(-<NUM_LIT:1>)<EOL><DEDENT><DEDENT>

Read settings from file
:param configfile:


try:<EOL><INDENT>open(file, '<STR_LIT:a>')<EOL><DEDENT>except IOError:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>".format(file))<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>

Check if we can write to the given file

Otherwise since we might detach the process to run in the background
we might never find out that writing failed and get an ugly
exit message on startup. For example:
ERROR: Child exited immediately with non-zero exit code 127

So we catch this error upfront and print a nicer error message
with a hint on how to fix it.


from rules.loader import import_rules<EOL>from rules.rule_list import all_rules<EOL>rules = import_rules(all_rules)<EOL>print("<STR_LIT>")<EOL>for name, rule in rules.iteritems():<EOL><INDENT>heading = "<STR_LIT>".format(rule.description(), name)<EOL>print("<STR_LIT>".format(heading))<EOL>for line in rule.reason():<EOL><INDENT>print(line)<EOL><DEDENT>print("<STR_LIT>")<EOL><DEDENT>sys.exit(<NUM_LIT:0>)<EOL>

Show the list of available rules and quit
:return:


from version import __version__<EOL>print("<STR_LIT>".format(__package__, __version__))<EOL>sys.exit(<NUM_LIT:0>)<EOL>


def __init__(self, rules, whitelist=[], safe_mode=True):

self.parser = QueryParser()<EOL>self.guard = Guard(rules)<EOL>self.sanitizer = Sanitizer()<EOL>self.whitelist = whitelist<EOL>self.safe_mode = safe_mode<EOL>

:param rules: A list of rules to evaluate
:param safe_mode: If set to True, allow the query in case it can not be parsed
:return:


if query.get_type() in {Keyword.LIST, Keyword.DROP}:<EOL><INDENT>series = query.series_stmt<EOL><DEDENT>else:<EOL><INDENT>series = query.from_stmt<EOL><DEDENT>if len(series) >= self.min_series_name_length:<EOL><INDENT>return Ok(True)<EOL><DEDENT>return Err("<STR_LIT>")<EOL>


if query.get_type() not in {Keyword.SELECT}:<EOL><INDENT>return Ok(True)<EOL><DEDENT>earliest_date = query.get_earliest_date()<EOL>if earliest_date >= self.min_start_date:<EOL><INDENT>return Ok(True)<EOL><DEDENT>if query.limit_stmt:<EOL><INDENT>return Ok(True)<EOL><DEDENT>return Err(("<STR_LIT>"<EOL>"<STR_LIT>").format(self.min_start_date.strftime("<STR_LIT>"),<EOL>earliest_date))<EOL>


if query.get_type() not in {Keyword.SELECT, Keyword.DELETE}:<EOL><INDENT>return Ok(True)<EOL><DEDENT>datapoints = query.get_datapoints()<EOL>if datapoints <= self.max_datapoints:<EOL><INDENT>return Ok(True)<EOL><DEDENT>return Err(("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>").format(datapoints))<EOL>


pass<EOL>

When and why the rule is useful.

This should return a list of lines instead of a long string.
It's easier to format line breaks this way.

:return: The reason for the rule


pass<EOL>

Check if a given query is permitted
:param query:
:return: result.Ok() if permitted, result.Err() if not.


rule = importlib.import_module(path)<EOL>return rule<EOL>

Load the given rule
:param path: Import path to rule


if query.get_type() not in {Keyword.SELECT}:<EOL><INDENT>return Ok(True)<EOL><DEDENT>if query.get_resolution() > <NUM_LIT:0>:<EOL><INDENT>return Ok(True)<EOL><DEDENT>return Err("<STR_LIT>")<EOL>


if not tokens[Keyword.SERIES]:<EOL><INDENT>tokens[Keyword.SERIES] = '<STR_LIT>'<EOL><DEDENT>return ListQuery(self.parse_keyword(Keyword.SERIES, tokens))<EOL>

Parse tokens of list query
:param tokens: A list of InfluxDB query tokens


try:<EOL><INDENT>query_type = tokens['<STR_LIT:type>']<EOL>return getattr(self, '<STR_LIT>' % query_type)(tokens)<EOL><DEDENT>except (KeyError, TypeError):<EOL><INDENT>return self.invalid_query(tokens)<EOL><DEDENT>

Analyze query tokens and create an InfluxDBStatement from them
Return None on error
:param tokens: A list of InfluxDB query tokens


def _parse_datapoints(self, parsed_duration, parsed_resolution, limit):

return self.datapoints_parser.parse(parsed_duration, parsed_resolution, limit)<EOL>

Parse the number of datapoints of a query.
This can be calculated from the given duration and resolution of the query.
E.g. if the query has a duation of 2*60*60 = 7200 seconds and a resolution of 10 seconds
then the number of datapoints would be 7200/10 => 7200 datapoints.

:param parsed_duration:
:param parsed_resolution:
:param limit:
:return:


if not tokens[Keyword.SERIES]:<EOL><INDENT>return None<EOL><DEDENT>return DropQuery(self.parse_keyword(Keyword.SERIES, tokens))<EOL>

Parse tokens of drop query
:param tokens: A list of InfluxDB query tokens


return self.resolution_parser.parse(self.parse_keyword(Keyword.GROUP_BY, tokens))<EOL>

Parse resolution from the GROUP BY statement.
E.g. GROUP BY time(10s) would mean a 10 second resolution
:param tokens:
:return:


if not group_by_stmt:<EOL><INDENT>return Resolution.MAX_RESOLUTION<EOL><DEDENT>m = self.GROUP_BY_TIME_PATTERN.match(group_by_stmt)<EOL>if not m:<EOL><INDENT>return None<EOL><DEDENT>value = int(m.group(<NUM_LIT:1>))<EOL>unit = m.group(<NUM_LIT:2>)<EOL>resolution = self.convert_to_seconds(value, unit)<EOL>return max(resolution, Resolution.MAX_RESOLUTION)<EOL>

Extract the data resolution of a query in seconds
E.g. "group by time(99s)" => 99

:param group_by_stmt: A raw InfluxDB group by statement


@classmethod<EOL><INDENT>def get_object(cls, api_token, cert_id):<DEDENT>

certificate = cls(token=api_token, id=cert_id)<EOL>certificate.load()<EOL>return certificate<EOL>

Class method that will return a Certificate object by its ID.


return self.get_data("<STR_LIT>" % self.id, type=DELETE)<EOL>


data = self.get_data("<STR_LIT>" % self.id)<EOL>certificate = data["<STR_LIT>"]<EOL>for attr in certificate.keys():<EOL><INDENT>setattr(self, attr, certificate[attr])<EOL><DEDENT>return self<EOL>

Load the Certificate object from DigitalOcean.

Requires self.id to be set.


def assert_url_query_equal(self, url1, url2):

base1, qlist1 = self.split_url(url1)<EOL>base2, qlist2 = self.split_url(url2)<EOL>self.assertEqual(base1, base2)<EOL>self.assertEqual(qlist1, qlist2)<EOL>

Test if two URL queries are equal

        The key=value pairs after the ? in a URL can occur in any order
        (especially since dicts in python 3 are not deterministic across runs).
        The method sorts the key=value pairs and then compares the URLs.


<EOL>data = {<EOL>"<STR_LIT:name>": self.name,<EOL>"<STR_LIT>": self.ip_address,<EOL>}<EOL>domain = self.get_data("<STR_LIT>", type=POST, params=data)<EOL>return domain<EOL>


@classmethod<EOL><INDENT>def get_object(cls, api_token, domain_name):<DEDENT>

domain = cls(token=api_token, name=domain_name)<EOL>domain.load()<EOL>return domain<EOL>

Class method that will return a Domain object by ID.


return self.get_data(<EOL>"<STR_LIT>" % self.ip,<EOL>type=POST,<EOL>params={"<STR_LIT:type>": "<STR_LIT>", "<STR_LIT>": droplet_id}<EOL>)<EOL>

Assign a FloatingIP to a Droplet.

Args:
    droplet_id: int - droplet id


return self.get_data(<EOL>"<STR_LIT>" % self.ip,<EOL>type=POST,<EOL>params={"<STR_LIT:type>": "<STR_LIT>"}<EOL>)<EOL>


data = self.get_data('<STR_LIT>',<EOL>type=POST,<EOL>params={'<STR_LIT>': self.droplet_id})<EOL>if data:<EOL><INDENT>self.ip = data['<STR_LIT>']['<STR_LIT>']<EOL>self.region = data['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>return self<EOL>

Creates a FloatingIP and assigns it to a Droplet.

Note: Every argument and parameter given to this method will be
assigned to the object.

Args:
    droplet_id: int - droplet id


for attr in kwargs.keys():<EOL><INDENT>setattr(self, attr, kwargs[attr])<EOL><DEDENT>if not self.size_slug and self.size:<EOL><INDENT>self.size_slug = self.size<EOL><DEDENT>ssh_keys_id = Droplet.__get_ssh_keys_id_or_fingerprint(self.ssh_keys,<EOL>self.token,<EOL>self.name)<EOL>data = {<EOL>"<STR_LIT:name>": self.name,<EOL>"<STR_LIT:size>": self.size_slug,<EOL>"<STR_LIT:image>": self.image,<EOL>"<STR_LIT>": self.region,<EOL>"<STR_LIT>": ssh_keys_id,<EOL>"<STR_LIT>": bool(self.backups),<EOL>"<STR_LIT>": bool(self.ipv6),<EOL>"<STR_LIT>": bool(self.private_networking),<EOL>"<STR_LIT>": self.volumes,<EOL>"<STR_LIT>": self.tags,<EOL>"<STR_LIT>": bool(self.monitoring),<EOL>}<EOL>if self.user_data:<EOL><INDENT>data["<STR_LIT>"] = self.user_data<EOL><DEDENT>data = self.get_data("<STR_LIT>", type=POST, params=data)<EOL>if data:<EOL><INDENT>self.id = data['<STR_LIT>']['<STR_LIT:id>']<EOL>action_id = data['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]['<STR_LIT:id>']<EOL>self.action_ids = []<EOL>self.action_ids.append(action_id)<EOL><DEDENT>

Create the droplet with object properties.

Note: Every argument and parameter given to this method will be
assigned to the object.


def change_kernel(self, kernel, return_dict=True):

if type(kernel) != Kernel:<EOL><INDENT>raise BadKernelObject("<STR_LIT>")<EOL><DEDENT>return self._perform_action(<EOL>{'<STR_LIT:type>': '<STR_LIT>', '<STR_LIT>': kernel.id},<EOL>return_dict<EOL>)<EOL>

Change the kernel to a new one

        Args:
            kernel : instance of digitalocean.Kernel.Kernel

        Optional Args:
            return_dict (bool): Return a dict when True (default),
                otherwise return an Action.

        Returns dict or Action


snapshots = list()<EOL>for id in self.snapshot_ids:<EOL><INDENT>snapshot = Image()<EOL>snapshot.id = id<EOL>snapshot.token = self.token<EOL>snapshots.append(snapshot)<EOL><DEDENT>return snapshots<EOL>

This method will return the snapshots/images connected to that
specific droplet.


answer = self.get_data("<STR_LIT>" % self.id, type=GET)<EOL>actions = []<EOL>for action_dict in answer['<STR_LIT>']:<EOL><INDENT>action = Action(**action_dict)<EOL>action.token = self.token<EOL>action.droplet_id = self.id<EOL>action.load()<EOL>actions.append(action)<EOL><DEDENT>return actions<EOL>

Returns a list of Action objects
This actions can be used to check the droplet's status


return self._perform_action({'<STR_LIT:type>': '<STR_LIT>'}, return_dict)<EOL>

Enable automatic backups

Optional Args:
    return_dict (bool): Return a dict when True (default),
        otherwise return an Action.

Returns dict or Action


def _perform_action(self, params, return_dict=True):

action = self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=POST,<EOL>params=params<EOL>)<EOL>if return_dict:<EOL><INDENT>return action<EOL><DEDENT>else:<EOL><INDENT>action = action[u'<STR_LIT:action>']<EOL>return_action = Action(token=self.token)<EOL>for attr in action.keys():<EOL><INDENT>setattr(return_action, attr, action[attr])<EOL><DEDENT>return return_action<EOL><DEDENT>

Perform a droplet action.

Args:
    params (dict): parameters of the action

Optional Args:
    return_dict (bool): Return a dict when True (default),
        otherwise return an Action.

Returns dict or Action


def resize(self, new_size_slug, return_dict=True, disk=True):

options = {"<STR_LIT:type>": "<STR_LIT>", "<STR_LIT:size>": new_size_slug}<EOL>if disk: options["<STR_LIT>"] = "<STR_LIT:true>"<EOL>return self._perform_action(options, return_dict)<EOL>

Resize the droplet to a new size slug.
        https://developers.digitalocean.com/documentation/v2/#resize-a-droplet

        Args:
            new_size_slug (str): name of new size

        Optional Args:
            return_dict (bool): Return a dict when True (default),
                                otherwise return an Action.
            disk (bool): If a permanent resize, with disk changes included.

        Returns dict or Action


Disable automatic backups

Optional Args:
    return_dict (bool): Return a dict when True (default),
        otherwise return an Action.

Returns dict or Action


reset the root password

Optional Args:
    return_dict (bool): Return a dict when True (default),
        otherwise return an Action.

Returns dict or Action


return self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=POST,<EOL>params={"<STR_LIT>": droplet_ids}<EOL>)<EOL>


return self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=POST,<EOL>params={"<STR_LIT>": tags}<EOL>)<EOL>


identifier = None<EOL>if self.id:<EOL><INDENT>identifier = self.id<EOL><DEDENT>elif self.fingerprint is not None:<EOL><INDENT>identifier = self.fingerprint<EOL><DEDENT>data = self.get_data("<STR_LIT>" % identifier, type=GET)<EOL>ssh_key = data['<STR_LIT>']<EOL>for attr in ssh_key.keys():<EOL><INDENT>setattr(self, attr, ssh_key[attr])<EOL><DEDENT>self.id = ssh_key['<STR_LIT:id>']<EOL>

Load the SSHKey object from DigitalOcean.

Requires either self.id or self.fingerprint to be set.


@classmethod<EOL><INDENT>def get_object(cls, api_token, ssh_key_id):<DEDENT>

ssh_key = cls(token=api_token, id=ssh_key_id)<EOL>ssh_key.load()<EOL>return ssh_key<EOL>

Class method that will return a SSHKey object by ID.


return self.get_data("<STR_LIT>" % self.id, type=DELETE)<EOL>


while self.status == u'<STR_LIT>':<EOL><INDENT>sleep(update_every_seconds)<EOL>self.load()<EOL><DEDENT>return self.status == u'<STR_LIT>'<EOL>

Wait until the action is marked as completed or with an error.
It will return True in case of success, otherwise False.

Optional Args:
    update_every_seconds - int : number of seconds to wait before
        checking if the action is completed.


def get_images(self, private=False, type=None):

params = {}<EOL>if private:<EOL><INDENT>params['<STR_LIT>'] = '<STR_LIT:true>'<EOL><DEDENT>if type:<EOL><INDENT>params['<STR_LIT:type>'] = type<EOL><DEDENT>data = self.get_data("<STR_LIT>", params=params)<EOL>images = list()<EOL>for jsoned in data['<STR_LIT>']:<EOL><INDENT>image = Image(**jsoned)<EOL>image.token = self.token<EOL>images.append(image)<EOL><DEDENT>return images<EOL>

This function returns a list of Image object.


data = self.get_data("<STR_LIT>")<EOL>return [<EOL>Snapshot(token=self.token, **snapshot)<EOL>for snapshot in data['<STR_LIT>']<EOL>]<EOL>

This method returns a list of all Snapshots based on Droplets.


return LoadBalancer.get_object(api_token=self.token, id=id)<EOL>

Returns a Load Balancer object by its ID.

Args:
    id (str): Load Balancer ID


return Droplet.get_object(api_token=self.token, droplet_id=droplet_id)<EOL>


data = self.get_data("<STR_LIT>")<EOL>certificates = list()<EOL>for jsoned in data['<STR_LIT>']:<EOL><INDENT>cert = Certificate(**jsoned)<EOL>cert.token = self.token<EOL>certificates.append(cert)<EOL><DEDENT>return certificates<EOL>

This function returns a list of Certificate objects.


This method returns a list of all Snapshots based on volumes.


data = self.get_data("<STR_LIT>")<EOL>load_balancers = list()<EOL>for jsoned in data['<STR_LIT>']:<EOL><INDENT>load_balancer = LoadBalancer(**jsoned)<EOL>load_balancer.token = self.token<EOL>load_balancer.health_check = HealthCheck(**jsoned['<STR_LIT>'])<EOL>load_balancer.sticky_sessions = StickySesions(**jsoned['<STR_LIT>'])<EOL>forwarding_rules = list()<EOL>for rule in jsoned['<STR_LIT>']:<EOL><INDENT>forwarding_rules.append(ForwardingRule(**rule))<EOL><DEDENT>load_balancer.forwarding_rules = forwarding_rules<EOL>load_balancers.append(load_balancer)<EOL><DEDENT>return load_balancers<EOL>

Returns a list of Load Balancer objects.


return SSHKey.get_object(api_token=self.token, ssh_key_id=ssh_key_id)<EOL>


return Certificate.get_object(api_token=self.token, cert_id=id)<EOL>

Returns a Certificate object by its ID.

Args:
    id (str): Certificate ID


images = self.get_images(type='<STR_LIT>')<EOL>return images<EOL>

This function returns a list of Image objects representing
public base distribution images.


This function returns a list of Image objectobjects representing
public DigitalOcean 'One-Click' application images.


This method returns a list of all Snapshots.


return Account.get_object(api_token=self.token)<EOL>


data = self.get_images()<EOL>images = list()<EOL>for i in data:<EOL><INDENT>if i.public:<EOL><INDENT>i.token = self.token<EOL>images.append(i)<EOL><DEDENT><DEDENT>return images<EOL>

This function returns a list of Image objects representing
public DigitalOcean images (e.g. base distribution images
and 'One-Click' applications).


return FloatingIP.get_object(api_token=self.token, ip=ip)<EOL>

Returns a of FloatingIP object by its IP address.


data = self.get_data("<STR_LIT>")<EOL>floating_ips = list()<EOL>for jsoned in data['<STR_LIT>']:<EOL><INDENT>floating_ip = FloatingIP(**jsoned)<EOL>floating_ip.token = self.token<EOL>floating_ips.append(floating_ip)<EOL><DEDENT>return floating_ips<EOL>

This function returns a list of FloatingIP objects.


def get_data(self, url, type=GET, params=None):

if params is None:<EOL><INDENT>params = dict()<EOL><DEDENT>if type is GET:<EOL><INDENT>params.setdefault("<STR_LIT>", <NUM_LIT:200>)<EOL><DEDENT>req = self.__perform_request(url, type, params)<EOL>if req.status_code == <NUM_LIT>:<EOL><INDENT>return True<EOL><DEDENT>if req.status_code == <NUM_LIT>:<EOL><INDENT>raise NotFoundError()<EOL><DEDENT>try:<EOL><INDENT>data = req.json()<EOL><DEDENT>except ValueError as e:<EOL><INDENT>raise JSONReadError(<EOL>'<STR_LIT>' % str(e)<EOL>)<EOL><DEDENT>if not req.ok:<EOL><INDENT>msg = [data[m] for m in ("<STR_LIT:id>", "<STR_LIT:message>") if m in data][<NUM_LIT:1>]<EOL>raise DataReadError(msg)<EOL><DEDENT>self.__init_ratelimit(req.headers)<EOL>pages = data.get("<STR_LIT>", {}).get("<STR_LIT>", {})<EOL>if pages.get("<STR_LIT>") and "<STR_LIT>" not in params:<EOL><INDENT>return self.__deal_with_pagination(url, type, params, data)<EOL><DEDENT>else:<EOL><INDENT>return data<EOL><DEDENT>

This method is a basic implementation of __call_api that checks
errors too. In case of success the method will return True or the
content of the response to the request.

Pagination is automatically detected and handled accordingly


timeout_str = os.environ.get(REQUEST_TIMEOUT_ENV_VAR)<EOL>if timeout_str:<EOL><INDENT>try:<EOL><INDENT>return float(timeout_str)<EOL><DEDENT>except:<EOL><INDENT>self._log.error('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>timeout_str)<EOL><DEDENT><DEDENT>return None<EOL>

Checks if any timeout for the requests to DigitalOcean is required.
To set a timeout, use the REQUEST_TIMEOUT_ENV_VAR environment
variable.


params = {'<STR_LIT:name>': self.name,<EOL>'<STR_LIT>': self.region,<EOL>'<STR_LIT:url>': self.url,<EOL>'<STR_LIT>': self.distribution,<EOL>'<STR_LIT:description>': self.description,<EOL>'<STR_LIT>': self.tags}<EOL>data = self.get_data('<STR_LIT>', type=POST, params=params)<EOL>if data:<EOL><INDENT>for attr in data['<STR_LIT:image>'].keys():<EOL><INDENT>setattr(self, attr, data['<STR_LIT:image>'][attr])<EOL><DEDENT><DEDENT>return self<EOL>

Creates a new custom DigitalOcean Image from the Linux virtual machine
image located at the provided `url`.


if type(value) in [type(u'<STR_LIT>'), type('<STR_LIT>')]:<EOL><INDENT>return True<EOL><DEDENT>elif type(value) in [int, type(<NUM_LIT:2> ** <NUM_LIT:64>)]:<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Checks if the value provided is a string (True) or not integer
(False) or something else (None).


return self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=POST,<EOL>params={"<STR_LIT:type>": "<STR_LIT>", "<STR_LIT>": new_region_slug}<EOL>)<EOL>


return self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=PUT,<EOL>params={"<STR_LIT:name>": new_name}<EOL>)<EOL>


@classmethod<EOL><INDENT>def get_object(cls, api_token, image_id_or_slug):<DEDENT>

if cls._is_string(image_id_or_slug):<EOL><INDENT>image = cls(token=api_token, slug=image_id_or_slug)<EOL>image.load(use_slug=True)<EOL><DEDENT>else:<EOL><INDENT>image = cls(token=api_token, id=image_id_or_slug)<EOL>image.load()<EOL><DEDENT>return image<EOL>

Class method that will return an Image object by ID or slug.

This method is used to validate the type of the image. If it is a
number, it will be considered as an Image ID, instead if it is a
string, it will considered as slug.


return self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=POST,<EOL>params={"<STR_LIT:type>": "<STR_LIT>",<EOL>"<STR_LIT>": droplet_id,<EOL>"<STR_LIT>": region}<EOL>)<EOL>

Detach a Volume to a Droplet.

Args:
    droplet_id: int - droplet id
    region: string - slug identifier for the region


data = self.get_data('<STR_LIT>',<EOL>type=POST,<EOL>params={'<STR_LIT:name>': self.name,<EOL>'<STR_LIT>': self.region,<EOL>'<STR_LIT>': self.size_gigabytes,<EOL>'<STR_LIT:description>': self.description,<EOL>'<STR_LIT>': self.filesystem_type,<EOL>'<STR_LIT>': self.filesystem_label<EOL>})<EOL>if data:<EOL><INDENT>self.id = data['<STR_LIT>']['<STR_LIT:id>']<EOL>self.created_at = data['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>return self<EOL>

Creates a Block Storage volume

Note: Every argument and parameter given to this method will be
assigned to the object.

Args:
    name: string - a name for the volume
    region: string - slug identifier for the region
    size_gigabytes: int - size of the Block Storage volume in GiB
    filesystem_type: string, optional - name of the filesystem type the
        volume will be formated with ('ext4' or 'xfs')
    filesystem_label: string, optional - the label to be applied to the
        filesystem, only used in conjunction with filesystem_type

Optional Args:
    description: string - text field to describe a volume


return self.get_data(<EOL>"<STR_LIT>" % (self.domain, self.id),<EOL>type=DELETE,<EOL>)<EOL>


input_params = {<EOL>"<STR_LIT:type>": self.type,<EOL>"<STR_LIT:data>": self.data,<EOL>"<STR_LIT:name>": self.name,<EOL>"<STR_LIT>": self.priority,<EOL>"<STR_LIT:port>": self.port,<EOL>"<STR_LIT>": self.ttl,<EOL>"<STR_LIT>": self.weight,<EOL>"<STR_LIT>": self.flags,<EOL>"<STR_LIT>": self.tags<EOL>}<EOL>data = self.get_data(<EOL>"<STR_LIT>" % (self.domain),<EOL>type=POST,<EOL>params=input_params,<EOL>)<EOL>if data:<EOL><INDENT>self.id = data['<STR_LIT>']['<STR_LIT:id>']<EOL><DEDENT>

Creates a new record for a domain.

Args:
    type (str): The type of the DNS record (e.g. A, CNAME, TXT).
    name (str): The host name, alias, or service being defined by the
        record.
    data (int): Variable data depending on record type.
    priority (int): The priority for SRV and MX records.
    port (int): The port for SRV records.
    ttl (int): The time to live for the record, in seconds.
    weight (int): The weight for SRV records.
    flags (int): An unsigned integer between 0-255 used for CAA records.
    tags (string): The parameter tag for CAA records. Valid values are
        "issue", "wildissue", or "iodef"


data = {<EOL>"<STR_LIT:type>": self.type,<EOL>"<STR_LIT:data>": self.data,<EOL>"<STR_LIT:name>": self.name,<EOL>"<STR_LIT>": self.priority,<EOL>"<STR_LIT:port>": self.port,<EOL>"<STR_LIT>": self.ttl,<EOL>"<STR_LIT>": self.weight,<EOL>"<STR_LIT>": self.flags,<EOL>"<STR_LIT>": self.tags<EOL>}<EOL>return self.get_data(<EOL>"<STR_LIT>" % (self.domain, self.id),<EOL>type=PUT,<EOL>params=data<EOL>)<EOL>


tags = self.get_data("<STR_LIT>" % self.name)<EOL>tag = tags['<STR_LIT>']<EOL>for attr in tag.keys():<EOL><INDENT>setattr(self, attr, tag[attr])<EOL><DEDENT>return self<EOL>


resources = []<EOL>if not isinstance(data, list): return data<EOL>for a_droplet in data:<EOL><INDENT>res = {}<EOL>try:<EOL><INDENT>if isinstance(a_droplet, unicode):<EOL><INDENT>res = {"<STR_LIT>": a_droplet, "<STR_LIT>": "<STR_LIT>"}<EOL><DEDENT><DEDENT>except NameError:<EOL><INDENT>pass<EOL><DEDENT>if isinstance(a_droplet, str) or isinstance(a_droplet, int):<EOL><INDENT>res = {"<STR_LIT>": str(a_droplet), "<STR_LIT>": "<STR_LIT>"}<EOL><DEDENT>elif isinstance(a_droplet, Droplet):<EOL><INDENT>res = {"<STR_LIT>": str(a_droplet.id), "<STR_LIT>": "<STR_LIT>"}<EOL><DEDENT>if len(res) > <NUM_LIT:0>:<EOL><INDENT>resources.append(res)<EOL><DEDENT><DEDENT>return resources<EOL>

Private method to extract from a value, the resources.
It will check the type of object in the array provided and build
the right structure for the API.


return self.__get_resources(resources, method='<STR_LIT>')<EOL>

Remove resources from this tag.

Attributes accepted at creation time:
    resources: array - See API.


return self.__get_resources(resources, method='<STR_LIT:POST>')<EOL>

Add to the resources to this tag.

Attributes accepted at creation time:
    resources: array - See API.


droplets = droplet<EOL>if not isinstance(droplets, list):<EOL><INDENT>droplets = [droplet]<EOL><DEDENT>resources = self.__extract_resources_from_droplets(droplets)<EOL>if len(resources) > <NUM_LIT:0>:<EOL><INDENT>return self.__remove_resources(resources)<EOL><DEDENT>return False<EOL>

Remove the Tag from the Droplet.

Attributes accepted at creation time:
    droplet: array of string or array of int, or array of Droplets.


droplets = droplet<EOL>if not isinstance(droplets, list):<EOL><INDENT>droplets = [droplet]<EOL><DEDENT>resources = self.__extract_resources_from_droplets(droplets)<EOL>if len(resources) > <NUM_LIT:0>:<EOL><INDENT>return self.__add_resources(resources)<EOL><DEDENT>return False<EOL>

Add the Tag to a Droplet.

Attributes accepted at creation time:
    droplet: array of string or array of int, or array of Droplets.


for attr in kwargs.keys():<EOL><INDENT>setattr(self, attr, kwargs[attr])<EOL><DEDENT>params = {"<STR_LIT:name>": self.name}<EOL>output = self.get_data("<STR_LIT>", type="<STR_LIT:POST>", params=params)<EOL>if output:<EOL><INDENT>self.name = output['<STR_LIT>']['<STR_LIT:name>']<EOL>self.resources = output['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>


return self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=DELETE,<EOL>params={"<STR_LIT>": droplet_ids}<EOL>)<EOL>

Unassign a LoadBalancer.

Args:
    droplet_ids (obj:`list` of `int`): A list of Droplet IDs


return self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=POST,<EOL>params={"<STR_LIT>": droplet_ids}<EOL>)<EOL>

Assign a LoadBalancer to a Droplet.

Args:
    droplet_ids (obj:`list` of `int`): A list of Droplet IDs


return self.get_data('<STR_LIT>' % self.id, type=DELETE)<EOL>


rules_dict = [rule.__dict__ for rule in forwarding_rules]<EOL>return self.get_data(<EOL>"<STR_LIT>" % self.id,<EOL>type=POST,<EOL>params={"<STR_LIT>": rules_dict}<EOL>)<EOL>

Adds new forwarding rules to a LoadBalancer.

Args:
    forwarding_rules (obj:`list`): A list of `ForwrdingRules` objects


@classmethod<EOL><INDENT>def get_object(cls, api_token, id):<DEDENT>

load_balancer = cls(token=api_token, id=id)<EOL>load_balancer.load()<EOL>return load_balancer<EOL>

Class method that will return a LoadBalancer object by its ID.

Args:
    api_token (str): DigitalOcean API token
    id (str): Load Balancer ID


try:<EOL><INDENT>result = ast3.parse(type_comment, '<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>except SyntaxError:<EOL><INDENT>raise ValueError(f"<STR_LIT>") from None<EOL><DEDENT>assert isinstance(result, ast3.Expression)<EOL>return result.body<EOL>

Parse a type comment string into AST nodes.


try:<EOL><INDENT>result = ast3.parse(type_comment, '<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>except SyntaxError:<EOL><INDENT>raise ValueError(f"<STR_LIT>")<EOL><DEDENT>assert isinstance(result, ast3.FunctionType)<EOL>if len(result.argtypes) == <NUM_LIT:1>:<EOL><INDENT>argtypes = result.argtypes[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>argtypes = result.argtypes<EOL><DEDENT>return argtypes, result.returns<EOL>

Parse the fugly signature type comment into AST nodes.

    Caveats: ASTifying **kwargs is impossible with the current grammar so we
    hack it into unary subtraction (to differentiate from Starred in vararg).

    For example from:
    "(str, int, *int, **Any) -> 'SomeReturnType'"

    To:
    ([ast3.Name, ast.Name, ast3.Name, ast.Name], ast3.Str)


if isinstance(n, Leaf):<EOL><INDENT>return Leaf(n.type, n.value, prefix=n.prefix if prefix is None else prefix)<EOL><DEDENT>n.parent = None<EOL>if prefix is not None:<EOL><INDENT>n.prefix = prefix<EOL><DEDENT>return n<EOL>

lib2to3's AST requires unique objects as children.


def retype_path(<EOL>src, pyi_dir, targets, *, src_explicitly_given=False, quiet=False, hg=False<EOL>):

if src.is_dir():<EOL><INDENT>for child in src.iterdir():<EOL><INDENT>if child == pyi_dir or child == targets:<EOL><INDENT>continue<EOL><DEDENT>yield from retype_path(<EOL>child, pyi_dir / src.name, targets / src.name, quiet=quiet, hg=hg,<EOL>)<EOL><DEDENT><DEDENT>elif src.suffix == '<STR_LIT>' or src_explicitly_given:<EOL><INDENT>try:<EOL><INDENT>retype_file(src, pyi_dir, targets, quiet=quiet, hg=hg)<EOL><DEDENT>except Exception as e:<EOL><INDENT>yield (<EOL>src,<EOL>str(e),<EOL>type(e),<EOL>traceback.format_tb(e.__traceback__),<EOL>)<EOL><DEDENT><DEDENT>

Recursively retype files or directories given. Generate errors.


raise NotImplementedError(f"<STR_LIT>")<EOL>

Converts an AST object into its lib2to3 equivalent.


for arg in args.args:<EOL><INDENT>copy_type_comment_to_annotation(arg)<EOL><DEDENT>if args.vararg:<EOL><INDENT>copy_type_comment_to_annotation(args.vararg)<EOL><DEDENT>for arg in args.kwonlyargs:<EOL><INDENT>copy_type_comment_to_annotation(arg)<EOL><DEDENT>if args.kwarg:<EOL><INDENT>copy_type_comment_to_annotation(args.kwarg)<EOL><DEDENT>

Copies argument type comments from the legacy long form to annotations
    in the entire function signature.


def copy_arguments_to_annotations(args, type_comment, *, is_method=False):

if isinstance(type_comment, ast3.Ellipsis):<EOL><INDENT>return<EOL><DEDENT>expected = len(args.args)<EOL>if args.vararg:<EOL><INDENT>expected += <NUM_LIT:1><EOL><DEDENT>expected += len(args.kwonlyargs)<EOL>if args.kwarg:<EOL><INDENT>expected += <NUM_LIT:1><EOL><DEDENT>actual = len(type_comment) if isinstance(type_comment, list) else <NUM_LIT:1><EOL>if expected != actual:<EOL><INDENT>if is_method and expected - actual == <NUM_LIT:1>:<EOL><INDENT>pass  <EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>f"<STR_LIT>" +<EOL>f"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>if isinstance(type_comment, list):<EOL><INDENT>next_value = type_comment.pop<EOL><DEDENT>else:<EOL><INDENT>_tc = type_comment<EOL>def next_value(index: int = <NUM_LIT:0>) -> ast3.expr:<EOL><INDENT>return _tc<EOL><DEDENT><DEDENT>for arg in args.args[expected - actual:]:<EOL><INDENT>ensure_no_annotation(arg.annotation)<EOL>arg.annotation = next_value(<NUM_LIT:0>)<EOL><DEDENT>if args.vararg:<EOL><INDENT>ensure_no_annotation(args.vararg.annotation)<EOL>args.vararg.annotation = next_value(<NUM_LIT:0>)<EOL><DEDENT>for arg in args.kwonlyargs:<EOL><INDENT>ensure_no_annotation(arg.annotation)<EOL>arg.annotation = next_value(<NUM_LIT:0>)<EOL><DEDENT>if args.kwarg:<EOL><INDENT>ensure_no_annotation(args.kwarg.annotation)<EOL>args.kwarg.annotation = next_value(<NUM_LIT:0>)<EOL><DEDENT>

Copies AST nodes from `type_comment` into the ast3.arguments in `args`.

    Does validaation of argument count (allowing for untyped self/cls)
    and type (vararg and kwarg).


assert node.type == syms.file_input<EOL>last_n = None<EOL>for n in node.post_order():<EOL><INDENT>if last_n is not None:<EOL><INDENT>if n.type == token.NEWLINE and is_assignment(last_n):<EOL><INDENT>fix_variable_annotation_type_comment(n, last_n)<EOL><DEDENT>elif n.type == syms.funcdef and last_n.type == syms.suite:<EOL><INDENT>fix_signature_annotation_type_comment(n, last_n, offset=<NUM_LIT:1>)<EOL><DEDENT>elif n.type == syms.async_funcdef and last_n.type == syms.suite:<EOL><INDENT>fix_signature_annotation_type_comment(n, last_n, offset=<NUM_LIT:2>)<EOL><DEDENT><DEDENT>last_n = n<EOL><DEDENT>

Converts type comments in `node` to proper annotated assignments.


def retype_file(src, pyi_dir, targets, *, quiet=False, hg=False):

with tokenize.open(src) as src_buffer:<EOL><INDENT>src_encoding = src_buffer.encoding<EOL>src_node = lib2to3_parse(src_buffer.read())<EOL><DEDENT>try:<EOL><INDENT>with open((pyi_dir / src.name).with_suffix('<STR_LIT>')) as pyi_file:<EOL><INDENT>pyi_txt = pyi_file.read()<EOL><DEDENT><DEDENT>except FileNotFoundError:<EOL><INDENT>if not quiet:<EOL><INDENT>print(<EOL>f'<STR_LIT>',<EOL>file=sys.stderr,<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pyi_ast = ast3.parse(pyi_txt)<EOL>assert isinstance(pyi_ast, ast3.Module)<EOL>reapply_all(pyi_ast.body, src_node)<EOL><DEDENT>fix_remaining_type_comments(src_node)<EOL>targets.mkdir(parents=True, exist_ok=True)<EOL>with open(targets / src.name, '<STR_LIT:w>', encoding=src_encoding) as target_file:<EOL><INDENT>target_file.write(lib2to3_unparse(src_node, hg=hg))<EOL><DEDENT>return targets / src.name<EOL>

Retype `src`, finding types in `pyi_dir`. Save in `targets`.

    The file should remain formatted exactly as it was before, save for:
    - annotations
    - additional imports needed to satisfy annotations
    - additional module-level names needed to satisfy annotations

    Type comments in sources are normalized to type annotations.


def ensure_annotations_equal(name, expected, actual):

maybe_replace_any_if_equal(name, expected, actual)<EOL>

Raise ValueError if `expected` isn't equal to `actual`.

    If --replace-any is used, the Any type in `actual` is considered equal.


start, stop, step = normalize_slice(slice_obj, self.index.line_count)<EOL>if isinstance(slice_obj, slice):<EOL><INDENT>if step == <NUM_LIT:1>:<EOL><INDENT>return self._get_lines(start, stop)<EOL><DEDENT>return [self._get_lines(i)[<NUM_LIT:0>] for i in range(start, stop, step)]<EOL><DEDENT>if isinstance(slice_obj, int):<EOL><INDENT>return self._get_lines(start)[<NUM_LIT:0>]<EOL><DEDENT>return None<EOL>

Supports slice operations on the file

For example:

    with IndexedOpen(filename) as f:
        print f[6:-2]


return Path(str(self.filepath) + "<STR_LIT>")<EOL>


def create_index(file_path, index_path, index_ratio, index_width):

i = <NUM_LIT:0><EOL>with file_path.open() as f:<EOL><INDENT>with index_path.open("<STR_LIT:wb>") as idx:<EOL><INDENT>idx.write(index_ratio.to_bytes(<NUM_LIT:1>, byteorder="<STR_LIT>"))<EOL>idx.write(index_width.to_bytes(<NUM_LIT:1>, byteorder="<STR_LIT>"))<EOL>idx.write((<NUM_LIT:0>).to_bytes(<NUM_LIT:32>, byteorder="<STR_LIT>"))  <EOL>idx.write((<NUM_LIT:0>).to_bytes(index_width, byteorder="<STR_LIT>"))<EOL>while f.readline():<EOL><INDENT>i += <NUM_LIT:1><EOL>if (i % index_ratio) == <NUM_LIT:0>:<EOL><INDENT>pointer = f.tell()<EOL>b = pointer.to_bytes(index_width, byteorder="<STR_LIT>")<EOL>idx.write(b)<EOL><DEDENT><DEDENT>idx.seek(<NUM_LIT:2>)<EOL>idx.write(i.to_bytes(<NUM_LIT:32>, byteorder="<STR_LIT>"))<EOL><DEDENT>t = file_path.stat().st_mtime<EOL>os.utime(str(index_path), (t, t))<EOL><DEDENT>

Index format:
    1st byte: index_ratio
    2nd byte: index_width
    3rd byte: line_count


if isinstance(slice_obj, slice):<EOL><INDENT>start, stop, step = slice_obj.start, slice_obj.stop, slice_obj.step<EOL>if start is None:<EOL><INDENT>start = <NUM_LIT:0><EOL><DEDENT>if stop is None:<EOL><INDENT>stop = length<EOL><DEDENT>if step is None:<EOL><INDENT>step = <NUM_LIT:1><EOL><DEDENT>if start < <NUM_LIT:0>:<EOL><INDENT>start += length<EOL><DEDENT>if stop < <NUM_LIT:0>:<EOL><INDENT>stop += length<EOL><DEDENT><DEDENT>elif isinstance(slice_obj, int):<EOL><INDENT>start = slice_obj<EOL>if start < <NUM_LIT:0>:<EOL><INDENT>start += length<EOL><DEDENT>stop = start + <NUM_LIT:1><EOL>step = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>raise TypeError<EOL><DEDENT>if (<NUM_LIT:0> <= start <= length) and (<NUM_LIT:0> <= stop <= length):<EOL><INDENT>return start, stop, step<EOL><DEDENT>raise IndexError<EOL>

Given a slice object, return appropriate values for use in the range function

:param slice_obj: The slice object or integer provided in the `[]` notation
:param length: For negative indexing we need to know the max length of the object.


global engine<EOL>engine = create_engine('<STR_LIT>', echo=False)<EOL>global Session<EOL>Session = sessionmaker(bind=engine)<EOL>global session<EOL>session = Session()<EOL>session._model_changes = {}<EOL>Base.metadata.create_all(bind=engine)<EOL>session.add_all([<EOL>User(name='<STR_LIT>', lastname='<STR_LIT>', uid='<STR_LIT>', city_id=<NUM_LIT:1>),<EOL>User(name='<STR_LIT>', lastname='<STR_LIT>', uid='<STR_LIT>', city_id=<NUM_LIT:2>),<EOL>User(name='<STR_LIT>', lastname='<STR_LIT>', uid='<STR_LIT>', city_id=<NUM_LIT:1>),<EOL>City(name='<STR_LIT>'),<EOL>City(name='<STR_LIT>')<EOL>])<EOL>session.commit()<EOL>


def elastic_query(model, query, session=None, enabled_fields=None):

<EOL>instance = ElasticQuery(model, query, session, enabled_fields)<EOL>return instance.search()<EOL>

Public method for init the class ElasticQuery
        :model: SQLAlchemy model
        :query: valid string like a ElasticSearch
        :session: SQLAlchemy session *optional
        :enabled_fields: Fields allowed for make a query *optional


try:<EOL><INDENT>filters = json.loads(self.query)<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>result = self.model_query<EOL>if '<STR_LIT>'in filters.keys():<EOL><INDENT>result = self.parse_filter(filters['<STR_LIT>'])<EOL><DEDENT>if '<STR_LIT>'in filters.keys():<EOL><INDENT>result = result.order_by(*self.sort(filters['<STR_LIT>']))<EOL><DEDENT>return result<EOL>


field = attr[<NUM_LIT:0>]<EOL>operator = attr[<NUM_LIT:1>]<EOL>value = attr[<NUM_LIT:2>]<EOL>model = self.model<EOL>if '<STR_LIT:.>' in field:<EOL><INDENT>field_items = field.split('<STR_LIT:.>')<EOL>field_name = getattr(model, field_items[<NUM_LIT:0>], None)<EOL>class_name = field_name.property.mapper.class_<EOL>new_model = getattr(class_name, field_items[<NUM_LIT:1>])<EOL>return field_name.has(OPERATORS[operator](new_model, value))<EOL><DEDENT>return OPERATORS[operator](getattr(model, field, None), value)<EOL>


return partial(s, tri(string), s)<EOL>


return make_caseless_literal(s)()<EOL>


return partial(s, tri(caseless_string), s)<EOL>

returns a literal string, case independant parser.


return make_literal(s)()<EOL>


quote_char = quote()<EOL>value, _ = many_until(parser, partial(one_of, quote_char))<EOL>return build_string(value)<EOL>

Parses as much as possible until it encounters a matching closing quote.

    By default matches any_token, but can be provided with a more specific parser if required.
    Returns a string


i = peek()<EOL>if (i is EndOfFile) or (not guard(i)):<EOL><INDENT>fail(["<STR_LIT>" + _fun_to_str(guard) + "<STR_LIT:>>"])<EOL><DEDENT>next()<EOL>return i<EOL>

Returns the current token if it satisfies the guard function provided.

    Fails otherwise.
    This is the a generalisation of one_of.


results = {}<EOL>for p in sequence:<EOL><INDENT>if callable(p): <EOL><INDENT>p()<EOL>continue<EOL><DEDENT>k, v = p<EOL>results[k] = v()<EOL><DEDENT>return results<EOL>

Runs a series of parsers in sequence optionally storing results in a returned dictionary.

    For example:
    seq(whitespace, ('phone', digits), whitespace, ('name', remaining))


return choice(parser, lambda: default)<EOL>

Tries to apply the provided parser, returning default if the parser fails.


found = []<EOL>for c in string:<EOL><INDENT>found.append(one_of(c))<EOL><DEDENT>return found<EOL>

Iterates over string, matching input to the items provided.

    The most obvious usage of this is to accept an entire string of characters,
    However this is function is more general than that. It takes an iterable
    and for each item, it tries one_of for that set. For example,
       string(['aA','bB','cC'])
    will accept 'abc' in either case.

    note, If you wish to match caseless strings as in the example, use
    picoparse.text.caseless_string.


return [parser()] + many(parser)<EOL>

Like many, but must consume at least one of parser


return lambda *args, **kwargs: f(g(*args, **kwargs))<EOL>

Compose returns a two functions composed as a new function.

    The first is called with the result of the second as its argument. Any arguments
    are passed to the second.


@tri<EOL>def not_followed_by_block():<EOL><INDENT>failed = object()<EOL>result = optional(tri(parser), failed)<EOL>if result != failed:<EOL><INDENT>fail(["<STR_LIT>" + _fun_to_str(parser)])<EOL><DEDENT><DEDENT>choice(not_followed_by_block)<EOL>

Succeeds if the given parser cannot consume input


geno = bgen["<STR_LIT>"][variant_idx].compute()<EOL>if geno["<STR_LIT>"]:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>nalleles = bgen["<STR_LIT>"].loc[variant_idx, "<STR_LIT>"].compute().item()<EOL>genotypes = get_genotypes(geno["<STR_LIT>"], nalleles)<EOL>expec = []<EOL>for i in range(len(genotypes)):<EOL><INDENT>count = asarray(genotypes_to_allele_counts(genotypes[i]), float)<EOL>n = count.shape[<NUM_LIT:0>]<EOL>expec.append((count.T * geno["<STR_LIT>"][i, :n]).sum(<NUM_LIT:1>))<EOL><DEDENT>return stack(expec, axis=<NUM_LIT:0>)<EOL>

r""" Allele expectation.

    Compute the expectation of each allele from the genotype probabilities.

    Parameters
    ----------
    bgen : bgen_file
        Bgen file handler.
    variant_idx : int
        Variant index.

    Returns
    -------
    :class:`numpy.ndarray`
        Samples-by-alleles matrix of allele expectations.

    Note
    ----
    This function supports unphased genotypes only.

    Examples
    --------
    .. doctest::

        >>> from bgen_reader import allele_expectation, example_files, read_bgen
        >>>
        >>> from texttable import Texttable
        >>>
        >>> # Download an example.
        >>> example = example_files("example.32bits.bgen")
        >>> filepath = example.filepath
        >>>
        >>> # Read the example.
        >>> bgen = read_bgen(filepath, verbose=False)
        >>>
        >>> variants = bgen["variants"]
        >>> samples = bgen["samples"]
        >>> genotype = bgen["genotype"]
        >>>
        >>> genotype = bgen["genotype"]
        >>> # This `compute` call will return a pandas data frame,
        >>> variant = variants[variants["rsid"] == "RSID_6"].compute()
        >>> # from which we retrieve the variant index.
        >>> variant_idx = variant.index.item()
        >>> print(variant)
                id    rsid chrom   pos  nalleles allele_ids  vaddr
        4  SNPID_6  RSID_6    01  6000         2        A,G  19377
        >>> genotype = bgen["genotype"]
        >>> # Samples is a pandas series, and we retrieve the
        >>> # sample index from the sample name.
        >>> sample_idx = samples[samples == "sample_005"].index.item()
        >>>
        >>> genotype = bgen["genotype"]
        >>> # This `compute` call will return a dictionary from which
        >>> # we can get the probability matrix the corresponding
        >>> # variant.
        >>> p = genotype[variant_idx].compute()["probs"][sample_idx]
        >>>
        >>> genotype = bgen["genotype"]
        >>> # Allele expectation makes sense for unphased genotypes only,
        >>> # which is the case here.
        >>> e = allele_expectation(bgen, variant_idx)[sample_idx]
        >>>
        >>> genotype = bgen["genotype"]
        >>> alleles = variant["allele_ids"].item().split(",")
        >>>
        >>> genotype = bgen["genotype"]
        >>>
        >>> # Print what we have got in a nice format.
        >>> table = Texttable()
        >>> table = table.add_rows(
        ...     [
        ...         ["", "AA", "AG", "GG", "E[.]"],
        ...         ["p"] + list(p) + ["na"],
        ...         ["#" + alleles[0], 2, 1, 0, e[0]],
        ...         ["#" + alleles[1], 0, 1, 2, e[1]],
        ...     ]
        ... )
        >>> print(table.draw())
        +----+-------+-------+-------+-------+
        |    |  AA   |  AG   |  GG   | E[.]  |
        +====+=======+=======+=======+=======+
        | p  | 0.012 | 0.987 | 0.001 | na    |
        +----+-------+-------+-------+-------+
        | #A | 2     | 1     | 0     | 1.011 |
        +----+-------+-------+-------+-------+
        | #G | 0     | 1     | 2     | 0.989 |
        +----+-------+-------+-------+-------+
        >>>
        >>> # Clean-up.
        >>> example.close()


expec = asarray(expec, float)<EOL>if expec.ndim != <NUM_LIT:2>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>ploidy = expec.shape[-<NUM_LIT:1>]<EOL>return expec.sum(-<NUM_LIT:2>) / ploidy<EOL>

r""" Compute allele frequency from its expectation.

    Parameters
    ----------
    expec : array_like
        Allele expectations encoded as a samples-by-alleles matrix.

    Returns
    -------
    :class:`numpy.ndarray`
        Allele frequencies encoded as a variants-by-alleles matrix.

    Examples
    --------
    .. doctest::

        >>> from bgen_reader import read_bgen, example_files
        >>> from bgen_reader import allele_expectation, allele_frequency
        >>>
        >>> # Download an example
        >>> example = example_files("example.32bits.bgen")
        >>> filepath = example.filepath
        >>>
        >>> bgen = read_bgen(filepath, verbose=False)
        >>>
        >>> variants = bgen["variants"]
        >>> samples = bgen["samples"]
        >>> genotype = bgen["genotype"]
        >>>
        >>> variant = variants[variants["rsid"] == "RSID_6"].compute()
        >>> variant_idx = variant.index.item()
        >>>
        >>> p = genotype[variant_idx].compute()["probs"]
        >>> # For unphased genotypes only.
        >>> e = allele_expectation(bgen, variant_idx)
        >>> f = allele_frequency(e)
        >>>
        >>> alleles = variant["allele_ids"].item().split(",")
        >>> print(alleles[0] + ": {}".format(f[0]))
        A: 229.23103218810434
        >>> print(alleles[1] + ": {}".format(f[1]))
        G: 270.7689678118956
        >>> print(variant)
                id    rsid chrom   pos  nalleles allele_ids  vaddr
        4  SNPID_6  RSID_6    01  6000         2        A,G  19377
        >>>
        >>> # Clean-up the example
        >>> example.close()


def _touch(fname, mode=<NUM_LIT>, dir_fd=None, **kwargs):

flags = os.O_CREAT | os.O_APPEND<EOL>with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:<EOL><INDENT>os.utime(<EOL>f.fileno() if os.utime in os.supports_fd else fname,<EOL>dir_fd=None if os.supports_fd else dir_fd,<EOL>**kwargs,<EOL>)<EOL><DEDENT>

Touch a file.

    Credits to <https://stackoverflow.com/a/1160227>.


return ((point1[<NUM_LIT:0>] - point2[<NUM_LIT:0>]) ** <NUM_LIT:2> + (point1[<NUM_LIT:1>] - point2[<NUM_LIT:1>]) ** <NUM_LIT:2>) ** <NUM_LIT:0.5><EOL>

Computes the distance beteen two points on a plane.

Args:
    point1: Tuple or list, the x and y coordinate of the first point.

    point2: Tuple or list, the x and y coordinate of the second point.

Returns:
    The distance between the two points as a floating point number.


return width * height<EOL>

Returns the area of a rectangle with the given width and height.

    Args:
        width:
            Integer or float, width of the rectangle.

        height: Integer or float, height of the rectangle.

    Returns:
        The area of a rectangle as an integer or float.


if type == '<STR_LIT>':<EOL><INDENT>return Decimal(sum(numbers)) / len(numbers)<EOL><DEDENT>else:<EOL><INDENT>return float(sum(numbers)) / len(numbers)<EOL><DEDENT>

Calculates the average or mean of a list of numbers

Args:
    numbers: a list of integers or floating point numbers.

    numtype: string, 'decimal' or 'float'; the type of number to return.

Returns:
    The average (mean) of the numbers as a floating point number
    or a Decimal object.

Requires:
    The math module


return miles * float(<NUM_LIT>)<EOL>

Converts a number of miles to feet.

Args:
    miles: Number of miles we want to convert.

Returns:
    Floating point number as the number of
    feet in the given miles.


def get_percentage(a, b, i=False, r=False):

<EOL>if i is False and r is True:<EOL><INDENT>percentage = round(<NUM_LIT> * (float(a) / b), <NUM_LIT:2>)<EOL><DEDENT>elif (i is True and r is True) or (i is True and r is False):<EOL><INDENT>percentage = int(round(<NUM_LIT:100> * (float(a) / b)))<EOL>if r is False:<EOL><INDENT>warnings.warn(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>percentage = <NUM_LIT> * (float(a) / b)<EOL><DEDENT>return percentage<EOL>

Finds the percentage of one number over another.

Args:
    a: The number that is a percent, int or float.

    b: The base number that a is a percent of, int or float.

    i: Optional boolean integer. True if the user wants the result returned as
    a whole number. Assumes False.

    r: Optional boolean round. True if the user wants the result rounded.
    Rounds to the second decimal point on floating point numbers. Assumes False.

Returns:
    The argument a as a percentage of b. Throws a warning if integer is set to True
    and round is set to False.


def future_value(present_value, annual_rate, periods_per_year, years):

<EOL>rate_per_period = annual_rate / float(periods_per_year)<EOL>periods = periods_per_year * years<EOL>return present_value * (<NUM_LIT:1> + rate_per_period) ** periods<EOL>

Calculates the future value of money invested at an anual interest rate,
x times per year, for a given number of years.

Args:
    present_value: int or float, the current value of the money (principal).

    annual_rate: float 0 to 1 e.g., .5 = 50%), the interest rate paid out.

    periods_per_year: int, the number of times money is invested per year.

    years: int, the number of years invested.

Returns:
    Float, the future value of the money invested with compound interest.


def total_seconds(hours, minutes, seconds):

return (hours * <NUM_LIT> + minutes) * <NUM_LIT> + seconds<EOL>

Returns the number of seconds in the given number of hours,
minutes, and seconds.

Args:
    hours:
        Integer, number of hours.

    minutes:
        Integer, number of minutes.

    seconds:
        Integer, number of seconds.

Returns:
    Integer, time in seconds.


return <NUM_LIT:2> ** (height + <NUM_LIT:1>) - <NUM_LIT:1><EOL>

Calculate the number of internal nodes in a complete binary tree in which each
internal node has exactly two children. A full binary tree is complete if every
leaf in the tree has the same depth. Internal nodes include both leaves and
internal nodes. The root node is also included in this calculation.

Args:
    height: integer, the height of the tree. Height is defined by the number
    of edges from the furthest child to the root. An edge is the line segment
    that runs between and connects nodes.


if (year % <NUM_LIT>) == <NUM_LIT:0>:<EOL><INDENT>return True<EOL><DEDENT>elif (year % <NUM_LIT:100>) == <NUM_LIT:0>:<EOL><INDENT>return False<EOL><DEDENT>elif (year % <NUM_LIT:4>) == <NUM_LIT:0>:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

Checks to see if a given year is a leap year.

Args:
    Integer, the year to test.

Returns:
    Boolean


def savings_rate(take_home_pay, spending, numtype='<STR_LIT:float>'):

if numtype == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>return (<EOL>(Decimal(take_home_pay) - Decimal(spending)) / (Decimal(take_home_pay))<EOL>) * Decimal(<NUM_LIT>)<EOL><DEDENT>except (InvalidOperation, DivisionByZero):<EOL><INDENT>return Decimal(<NUM_LIT:0.0>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>return (<EOL>(float(take_home_pay) - float(spending)) / (float(take_home_pay))<EOL>) * <NUM_LIT><EOL><DEDENT>except (ZeroDivisionError):<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT><DEDENT>

Calculate net take-home pay including employer retirement savings match
using the formula laid out by Mr. Money Mustache:
http://www.mrmoneymustache.com/2015/01/26/calculating-net-worth/

Args:
    take_home_pay: float or int, monthly take-home pay

    spending: float or int, monthly spending

    numtype: string, 'decimal' or 'float'; the type of number to return.

Returns:
    your monthly savings rate expressed as a percentage.


return <NUM_LIT:2> ** height<EOL>

Calculate the number of leaves in a complete binary tree in which each internal
node has exactly two children. A full binary tree is complete if every leaf
in the tree has the same depth. A leaf is a node without children

Args:
    height: integer, the height of the tree. Height is defined by the number
    of edges from the furthest child to the root. An edge is the line segment
    that runs between and connects nodes.


return (float(point2[<NUM_LIT:1>]) - point1[<NUM_LIT:1>]) / (float(point2[<NUM_LIT:0>]) - point1[<NUM_LIT:0>])<EOL>

Calculate the slope of the line connecting two points on a grid.

Args:
    point1: Tuple or list, the x and y coordinate of the first point.

    point2: Tuple or list, the x and y coordinate of the second point

Returns:
    the slope of a line connecting two points on a grid.


return variance ** <NUM_LIT:0.5><EOL>

Calculates the standard deviation.

Args:
    variance: The variance of a group of numbers.

Returns:
    The standard deviation as a floating point number.


def take_home_pay(gross_pay, employer_match, taxes_and_fees, numtype='<STR_LIT:float>'):

if numtype == '<STR_LIT>':<EOL><INDENT>return (Decimal(gross_pay) + Decimal(employer_match)) - Decimal(<EOL>sum(taxes_and_fees)<EOL>)<EOL><DEDENT>else:<EOL><INDENT>return (float(gross_pay) + float(employer_match)) - sum(taxes_and_fees)<EOL><DEDENT>

Calculate net take-home pay including employer retirement savings match
using the formula laid out by Mr. Money Mustache:
http://www.mrmoneymustache.com/2015/01/26/calculating-net-worth/

Args:
    gross_pay: float or int, gross monthly pay.

    employer_match: float or int, the 401(k) match from your employer.

    taxes_and_fees: list, taxes and fees that are deducted from your paycheck.

    numtype: string, 'decimal' or 'float'; the type of number to return.

Returns:
    your monthly take-home pay.


def triangle_area(point1, point2, point3):

"""<STR_LIT>"""<EOL>a = point_distance(point1, point2)<EOL>b = point_distance(point1, point3)<EOL>c = point_distance(point2, point3)<EOL>"""<STR_LIT>"""<EOL>s = (a + b + c) / <NUM_LIT><EOL>"""<STR_LIT>"""<EOL>return math.sqrt(s * (s - a) * (s - b) * (s - c))<EOL>

Uses Heron's formula to find the area of a triangle
based on the coordinates of three points.

Args:
    point1: list or tuple, the x y coordinate of point one.

    point2: list or tuple, the x y coordinate of point two.

    point3: list or tuple, the x y coordinate of point three.

Returns:
    The area of a triangle as a floating point number.

Requires:
    The math module, point_distance().


def compound_interest(principal, annual_rate, years):

return principal * (<NUM_LIT:1> + <NUM_LIT> * annual_rate) ** years<EOL>

Returns the future value of money invested at an annual
interest rate, compounded annually for a given number of years.

Args:
    principal: The beginning ammount of money invested

    annual_rate: The interest rate paid out

    years: The number of years invested

Returns:
    A basic calculation of compound interest.


assert a < <NUM_LIT:0><EOL>


def get_built_in(self, language, level, data):

<EOL>pp = pprint.PrettyPrinter(indent=level)<EOL>lookup = {'<STR_LIT>' : pp.pformat(data),<EOL>'<STR_LIT>' : str(json.dumps(data, sort_keys=True, indent=level, separators=('<STR_LIT:U+002C>', '<STR_LIT>')))}<EOL>self.data_structure = lookup[language]<EOL>

Gets the return string for a language that's supported by python.
Used in cases when python provides support for the conversion.

Args:
    language: string the langage to return for.

    level: integer, the indentation level.

    data: python data structure being converted (list of tuples)

Returns:
    None, updates self.data_structure


def excel_to_html(path, sheetname='<STR_LIT>', css_classes='<STR_LIT>',caption='<STR_LIT>', details=[], row_headers=False, merge=False):

def get_data_on_merged_cells():<EOL><INDENT>"""<STR_LIT>"""<EOL>merged_cells = xls.book.sheet_by_name(sheetname).merged_cells<EOL>ds = {}<EOL>for crange in merged_cells:<EOL><INDENT>rlo, rhi, clo, chi = crange<EOL>for rowx in range(rlo, rhi):<EOL><INDENT>for colx in range(clo, chi):<EOL><INDENT>parent_cell = (rlo,clo)<EOL>child_cell = (rowx,colx)<EOL>if not parent_cell in ds:<EOL><INDENT>ds[parent_cell] = [[<NUM_LIT:1>,<NUM_LIT:1>], set([])]<EOL><DEDENT>else:<EOL><INDENT>if parent_cell != child_cell and child_cell[<NUM_LIT:0>] == parent_cell[<NUM_LIT:0>]:<EOL><INDENT>ds[parent_cell][<NUM_LIT:0>][<NUM_LIT:0>] += <NUM_LIT:1><EOL>ds[parent_cell][<NUM_LIT:1>].add('<STR_LIT:right>')<EOL><DEDENT>elif parent_cell != child_cell and child_cell[<NUM_LIT:0>] > parent_cell[<NUM_LIT:0>]:<EOL><INDENT>if child_cell[<NUM_LIT:1>] == parent_cell[<NUM_LIT:1>]:<EOL><INDENT>ds[parent_cell][<NUM_LIT:0>][<NUM_LIT:1>] += <NUM_LIT:1><EOL><DEDENT>ds[parent_cell][<NUM_LIT:1>].add('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>return ds<EOL><DEDENT>def mark_cells_going_right(cell, curr_cell, merged_cells):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>xcount = merged_cells[curr_cell][<NUM_LIT:0>][<NUM_LIT:0>]<EOL>if xcount > <NUM_LIT:1>: <EOL><INDENT>cell['<STR_LIT>'] = xcount<EOL><DEDENT>col_count = xcount - <NUM_LIT:1> <EOL>while col_count > <NUM_LIT:0>:<EOL><INDENT>cell = cell.find_next_sibling()<EOL>cell['<STR_LIT:class>'] = '<STR_LIT>'<EOL>col_count -= <NUM_LIT:1><EOL><DEDENT><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>def mark_cells_going_down(cell, curr_cell, merged_cells):<EOL><INDENT>"""<STR_LIT>"""<EOL>if curr_cell in merged_cells and merged_cells[curr_cell][<NUM_LIT:1>] == set(['<STR_LIT>']):<EOL><INDENT>ycount = merged_cells[curr_cell][<NUM_LIT:0>][<NUM_LIT:1>]<EOL>cell['<STR_LIT>'] = ycount<EOL>row_count = ycount<EOL>for child_row in cell.parent.find_next_siblings(limit=row_count - <NUM_LIT:1>):<EOL><INDENT>i = <NUM_LIT:0><EOL>for child in child_row.find_all('<STR_LIT>'):<EOL><INDENT>if i == curr_cell[<NUM_LIT:1>]:<EOL><INDENT>child['<STR_LIT:class>'] = '<STR_LIT>'<EOL><DEDENT>i += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT><DEDENT>def mark_cells_going_down_and_right(cell, curr_cell, merged_cells):<EOL><INDENT>"""<STR_LIT>"""<EOL>if curr_cell in merged_cells and('<STR_LIT>' in merged_cells[curr_cell][<NUM_LIT:1>] and'<STR_LIT:right>' in merged_cells[curr_cell][<NUM_LIT:1>]):<EOL><INDENT>xcount = merged_cells[curr_cell][<NUM_LIT:0>][<NUM_LIT:0>]<EOL>ycount = merged_cells[curr_cell][<NUM_LIT:0>][<NUM_LIT:1>]<EOL>row_count = ycount<EOL>col_count = xcount<EOL>mark_cells_going_right(cell, curr_cell, merged_cells)<EOL>flag = False<EOL>for child_row in [cell.parent] + cell.parent.find_all_next('<STR_LIT>', limit=row_count - <NUM_LIT:1>):<EOL><INDENT>i = <NUM_LIT:0><EOL>for child in child_row.find_all('<STR_LIT>'):<EOL><INDENT>if i == curr_cell[<NUM_LIT:1>]:<EOL><INDENT>mark_cells_going_right(child, curr_cell, merged_cells)<EOL>if not flag:<EOL><INDENT>child['<STR_LIT>'] = col_count<EOL>child['<STR_LIT>'] = row_count<EOL>flag = True<EOL><DEDENT>else:<EOL><INDENT>child['<STR_LIT:class>'] = '<STR_LIT>'<EOL><DEDENT><DEDENT>i += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT><DEDENT>def is_empty_th(string):<EOL><INDENT>"""<STR_LIT>"""<EOL>if string[:<NUM_LIT:8>] == '<STR_LIT>':<EOL><INDENT>data = string.split('<STR_LIT:U+0020>')<EOL>if is_numeric(data[<NUM_LIT:1>]):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL><DEDENT>def mark_header_cells(html):<EOL><INDENT>"""<STR_LIT>"""<EOL>th = html.find_all('<STR_LIT>')<EOL>for header in th:<EOL><INDENT>txt = header.string<EOL>if not is_empty_th(txt):<EOL><INDENT>header['<STR_LIT>'] = '<STR_LIT>'<EOL>count = <NUM_LIT:1><EOL>for sibling in header.find_next_siblings():<EOL><INDENT>if is_empty_th(sibling.string):<EOL><INDENT>count += <NUM_LIT:1><EOL>sibling['<STR_LIT:class>'] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if count > <NUM_LIT:1>:<EOL><INDENT>header['<STR_LIT>'] = count<EOL>header['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT><DEDENT><DEDENT><DEDENT>def create_caption(html, caption):<EOL><INDENT>"""<STR_LIT>"""<EOL>ctag = html.new_tag('<STR_LIT>')<EOL>ctag.insert(<NUM_LIT:0>, caption)<EOL>html.table.insert(<NUM_LIT:0>, ctag)<EOL><DEDENT>def create_summary_and_details(html, details):<EOL><INDENT>"""<STR_LIT>"""<EOL>if len(details) != <NUM_LIT:2>:<EOL><INDENT>msg = '<STR_LIT>'+ '<STR_LIT>'+ '<STR_LIT>'+ '<STR_LIT>'<EOL>raise RuntimeError(msg)<EOL><DEDENT>summary = details[<NUM_LIT:0>]<EOL>details = details[<NUM_LIT:1>]<EOL>if not caption:<EOL><INDENT>create_caption(html, caption)<EOL><DEDENT>dtag = html.new_tag('<STR_LIT>')<EOL>stag = html.new_tag('<STR_LIT>')<EOL>ptag = html.new_tag('<STR_LIT:p>')<EOL>stag.insert(<NUM_LIT:0>, summary)<EOL>ptag.insert(<NUM_LIT:0>, details)<EOL>dtag.insert(<NUM_LIT:0>, stag)<EOL>dtag.append(ptag) <EOL>html.table.caption.insert(<NUM_LIT:1>, dtag)   <EOL><DEDENT>def format_properly(html):<EOL><INDENT>"""<STR_LIT>"""<EOL>return html.replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>','<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>','<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>def add_row_headers(html):<EOL><INDENT>"""<STR_LIT>"""<EOL>for row in html.tbody.find_all('<STR_LIT>'):<EOL><INDENT>spans_rows = '<STR_LIT>' in row.td.attrs<EOL>spans_columns = '<STR_LIT>' in row.td.attrs<EOL>new_tag = html.new_tag('<STR_LIT>')<EOL>new_tag['<STR_LIT>'] = '<STR_LIT>'<EOL>new_tag.string = row.td.string<EOL>if spans_rows:<EOL><INDENT>new_tag['<STR_LIT>'] = row.td.attrs['<STR_LIT>']<EOL>new_tag['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if spans_columns:<EOL><INDENT>new_tag['<STR_LIT>'] = row.td.attrs['<STR_LIT>']<EOL><DEDENT>row.td.replace_with(new_tag)<EOL><DEDENT><DEDENT>def beautify(html):<EOL><INDENT>"""<STR_LIT>"""<EOL>table = html.find('<STR_LIT>')<EOL>first_tr = table.find('<STR_LIT>')<EOL>del table['<STR_LIT>']<EOL>del first_tr['<STR_LIT>']<EOL>return format_properly(html.prettify(formatter='<STR_LIT>'))<EOL><DEDENT>def parse_html(html, caption, details):<EOL><INDENT>"""<STR_LIT>"""<EOL>new_html = BeautifulSoup(html, '<STR_LIT>')<EOL>if merge:<EOL><INDENT>row_num = <NUM_LIT:1><EOL>merged_cells = get_data_on_merged_cells()<EOL>rows = new_html.find('<STR_LIT>').find('<STR_LIT>').find_all('<STR_LIT>')<EOL>for row in rows:<EOL><INDENT>cell_num = <NUM_LIT:0> <EOL>cells = row.find_all('<STR_LIT>')<EOL>for cell in cells:<EOL><INDENT>curr_cell = (row_num, cell_num)<EOL>mark_cells_going_right(cell, curr_cell, merged_cells)  <EOL>mark_cells_going_down(cell, curr_cell, merged_cells)<EOL>mark_cells_going_down_and_right(cell, curr_cell, merged_cells)<EOL>cell_num += <NUM_LIT:1><EOL><DEDENT>row_num += <NUM_LIT:1><EOL><DEDENT>mark_header_cells(new_html)<EOL>destroy = new_html.find_all(attrs={'<STR_LIT:class>' : '<STR_LIT>' })<EOL>for item in destroy:<EOL><INDENT>item.extract()<EOL><DEDENT><DEDENT>if row_headers:<EOL><INDENT>add_row_headers(new_html)<EOL><DEDENT>if caption:<EOL><INDENT>create_caption(new_html, caption)<EOL><DEDENT>if details:<EOL><INDENT>create_summary_and_details(new_html, details)<EOL><DEDENT>return beautify(new_html)<EOL><DEDENT>pd.options.display.max_colwidth = -<NUM_LIT:1><EOL>xls = pd.ExcelFile(path)<EOL>df = xls.parse(sheetname)<EOL>panda_html = df.to_html(classes=css_classes, index=False, na_rep='<STR_LIT>')<EOL>return parse_html(panda_html, caption, details)<EOL>

Convert an excel spreadsheet to an html table.
This function supports the conversion of merged
cells. It can be used in code or run from the
command-line. If passed the correct arguments
it can generate fully accessible html.

Args:
    path: string, path to the spreadsheet.

    sheetname: string, name of the sheet
    to convert.

    css_classes: string, space separated
    classnames to append to the table.

    caption: string, a short heading-like
    description of the table.

    details: list of strings, where the first
    item in the list is a string for the html
    summary element and the second item is
    a string for the details element. The
    summary should be very short, e.g. "Help",
    where as the details element should be a
    long description regarding the purpose or
    how to navigate the table.

    row_headers: boolean, defaults to False.
    Does the table have row headers? If set
    to True, the first element in each row
    will be a <th scope="row"> element
    instead of a <td> element.

    merge: boolean, whether or not to
    combine cells that were merged in the
    spreadsheet.

Returns:
    string, html table


line_count = get_line_count(f)<EOL>f = open(f, '<STR_LIT>')<EOL>output = open(output, '<STR_LIT>')<EOL>for line in range(line_count):<EOL><INDENT>string = f.readline()<EOL>string = re.sub(char, char + '<STR_LIT:\n>', string)<EOL>output.write(string)<EOL><DEDENT>

Adds line breaks after every occurance of a given character in a file.

    Args:
        f: string, path to input file.

        output: string, path to output file.

    Returns:
        None.


i = <NUM_LIT:0><EOL>with open(fname) as f:<EOL><INDENT>for i, l in enumerate(f):<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return i + <NUM_LIT:1><EOL>

Counts the number of lines in a file.

    Args:
        fname: string, name of the file.

    Returns:
        integer, the number of lines in the file.


line_count = get_line_count(f)<EOL>f = open(f, '<STR_LIT>')<EOL>output = open(output, '<STR_LIT>')<EOL>for line in range(line_count):<EOL><INDENT>string = f.readline().rstrip()<EOL>if len(string) > <NUM_LIT:0>:<EOL><INDENT>if string[-<NUM_LIT:1>] == "<STR_LIT:;>":<EOL><INDENT>output.write("<STR_LIT:U+0020>" + string + "<STR_LIT:\n>")<EOL><DEDENT>else:<EOL><INDENT>output.write(string + "<STR_LIT:\n>")<EOL><DEDENT><DEDENT><DEDENT>output.close()<EOL>f.close()<EOL>

Indentes css that has not been indented and saves it to a new file.
    A new file is created if the output destination does not already exist.

    Args:
        f: string, path to file.

        output: string, path/name of the output file (e.g. /directory/output.css).
    print type(response.read())

    Returns:
        None.


try:<EOL><INDENT>float(string)<EOL>return True<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>

Checks if a string is numeric. If the string value is an integer
or a float, return True, otherwise False. Can be used to test
soley for floats as well.

Args:
    string: a string to test.

Returns:
    boolean


retval = []<EOL>for val in iterable:<EOL><INDENT>try:<EOL><INDENT>retval.append(val.strip())<EOL><DEDENT>except(AttributeError):<EOL><INDENT>retval.append(val)<EOL><DEDENT><DEDENT>return retval<EOL>

Take a list of strings and clear whitespace
on each one. If a value in the list is not a
string pass it through untouched.

Args:
    iterable: mixed list

Returns:
    mixed list


try:<EOL><INDENT>a = float(string)<EOL>b = int(a)<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return a == b<EOL><DEDENT>

Checks if a string is an integer. If the string value is an integer
return True, otherwise return False.

Args:
    string: a string to test.

Returns:
    boolean