Ejemplo n.º 1
0
def make_response(data, marshal_table, cursors=None):
	if util.is_iterable(data):
		response = {
			'status': 'success',
			'count': len(data),
			'now': datetime.utcnow().isoformat(),
			'result': map(lambda l: flask_restful.marshal(l, marshal_table), data),
		}
		if cursors:
			if isinstance(cursors, dict):
				if cursors.get('next'):
					response['next_cursor'] = cursors['next']
					response['next_url'] = util.generate_next_url(cursors['next'])
				if cursors.get('prev'):
					response['prev_cursor'] = cursors['prev']
					response['prev_url'] = util.generate_next_url(cursors['prev'])
			else:
				response['next_cursor'] = cursors
				response['next_url'] = util.generate_next_url(cursors)
		return util.jsonpify(response)
	return util.jsonpify({
		'status': 'success',
		'now': datetime.utcnow().isoformat(),
		'result': flask_restful.marshal(data, marshal_table),
	})
Ejemplo n.º 2
0
def make_response(data, marshal_table, cursors=None):
    if util.is_iterable(data):
        response = {
            'status': 'success',
            'count': len(data),
            'now': datetime.utcnow().isoformat(),
            'result': [flask_restful.marshal(d, marshal_table) for d in data],
        }
        if cursors:
            if isinstance(cursors, dict):
                if cursors.get('next'):
                    response['next_cursor'] = cursors['next']
                    response['next_url'] = util.generate_next_url(
                        cursors['next'])
                if cursors.get('prev'):
                    response['prev_cursor'] = cursors['prev']
                    response['prev_url'] = util.generate_next_url(
                        cursors['prev'])
            else:
                response['next_cursor'] = cursors
                response['next_url'] = util.generate_next_url(cursors)
        return util.jsonpify(response)
    return util.jsonpify({
        'status': 'success',
        'now': datetime.utcnow().isoformat(),
        'result': flask_restful.marshal(data, marshal_table),
    })
Ejemplo n.º 3
0
 def testIsIterable(self):
     self.assertTrue(util.is_iterable([]))
     self.assertTrue(util.is_iterable({}))
     self.assertTrue(util.is_iterable(set()))
     self.assertTrue(util.is_iterable(()))
     self.assertFalse(util.is_iterable(42))
     self.assertFalse(util.is_iterable('list?'))
     self.assertFalse(util.is_iterable(object))
Ejemplo n.º 4
0
 def testIsIterable(self):
   self.assertTrue(util.is_iterable([]))
   self.assertTrue(util.is_iterable({}))
   self.assertTrue(util.is_iterable(set()))
   self.assertTrue(util.is_iterable(()))
   self.assertFalse(util.is_iterable(42))
   self.assertFalse(util.is_iterable('list?'))
   self.assertFalse(util.is_iterable(object))
Ejemplo n.º 5
0
 def csvwrite(path, data, writetype='a'):
     '''Writes a CSV from a series of data
     '''
     with open(path, writetype) as outfile:
         csv_writer = csv.writer(outfile, delimiter=',')
         for row in data:
             if util.is_iterable(row):
                 csv_writer.writerow(row)
             else:
                 csv_writer.writerow([row])
Ejemplo n.º 6
0
 def csvwrite(path, data, writetype='a'):
     '''Writes a CSV from a series of data
     '''
     with open(path, writetype) as outfile:
         csv_writer = csv.writer(outfile, delimiter=',')
         for row in data:
             if util.is_iterable(row):
                 csv_writer.writerow(row)
             else:
                 csv_writer.writerow([row])
Ejemplo n.º 7
0
def make_response(data, marshal_table, next_cursor=None):
    if util.is_iterable(data):
        response = {
            'status': 'success',
            'count': len(data),
            'now': datetime.utcnow().isoformat(),
            'result': map(lambda l: restful.marshal(l, marshal_table), data),
        }
        if next_cursor:
            response['next_cursor'] = next_cursor
            response['next_url'] = util.generate_next_url(next_cursor)
        return util.jsonpify(response)
    return util.jsonpify({
        'status': 'success',
        'now': datetime.utcnow().isoformat(),
        'result': restful.marshal(data, marshal_table),
    })
Ejemplo n.º 8
0
def make_response(data, marshal_table, next_cursor=None):
  if util.is_iterable(data):
    response = {
        'status': 'success',
        'count': len(data),
        'now': datetime.utcnow().isoformat(),
        'result': map(lambda l: restful.marshal(l, marshal_table), data),
      }
    if next_cursor:
      response['next_cursor'] = next_cursor
      response['next_url'] = util.generate_next_url(next_cursor)
    return util.jsonpify(response)
  return util.jsonpify({
      'status': 'success',
      'now': datetime.utcnow().isoformat(),
      'result': restful.marshal(data, marshal_table),
    })
Ejemplo n.º 9
0
def get_forward_walk_ops(seed_ops,
                         inclusive=True,
                         within_ops=None,
                         within_ops_fn=None,
                         stop_at_ts=(),
                         control_outputs=None):
    """Do a forward graph walk and return all the visited ops.

  Args:
    seed_ops: an iterable of operations from which the forward graph
      walk starts. If a list of tensors is given instead, the seed_ops are set
      to be the consumers of those tensors.
    inclusive: if True the given seed_ops are also part of the resulting set.
    within_ops: an iterable of `tf.Operation` within which the search is
      restricted. If `within_ops` is `None`, the search is performed within
      the whole graph.
    within_ops_fn: if provided, a function on ops that should return True iff
      the op is within the graph traversal. This can be used along within_ops,
      in which case an op is within if it is also in within_ops.
    stop_at_ts: an iterable of tensors at which the graph walk stops.
    control_outputs: a `util.ControlOutputs` instance or None.
      If not `None`, it will be used while walking the graph forward.
  Returns:
    A Python set of all the `tf.Operation` ahead of `seed_ops`.
  Raises:
    TypeError: if `seed_ops` or `within_ops` cannot be converted to a list of
      `tf.Operation`.
  """
    _, control_outputs = check_cios(False, control_outputs)
    if not util.is_iterable(seed_ops):
        seed_ops = [seed_ops]
    if not seed_ops:
        return []
    if isinstance(seed_ops[0], tf_ops.Tensor):
        ts = util.make_list_of_t(seed_ops, allow_graph=False)
        seed_ops = util.get_consuming_ops(ts)
    else:
        seed_ops = util.make_list_of_op(seed_ops, allow_graph=False)

    seed_ops = frozenset(seed_ops)
    stop_at_ts = frozenset(util.make_list_of_t(stop_at_ts))
    if within_ops:
        within_ops = util.make_list_of_op(within_ops, allow_graph=False)
        within_ops = frozenset(within_ops)
        seed_ops &= within_ops

    def is_within(op):
        return (within_ops is None or op
                in within_ops) and (within_ops_fn is None or within_ops_fn(op))

    result = list(seed_ops)
    wave = set(seed_ops)
    while wave:
        new_wave = set()
        for op in wave:
            for new_t in op.outputs:
                if new_t in stop_at_ts:
                    continue
                for new_op in new_t.consumers():
                    if new_op not in result and is_within(new_op):
                        new_wave.add(new_op)
            if control_outputs is not None:
                for new_op in control_outputs.get(op):
                    if new_op not in result and is_within(new_op):
                        new_wave.add(new_op)
        util.concatenate_unique(result, new_wave)
        wave = new_wave
    if not inclusive:
        result = [op for op in result if op not in seed_ops]
    return result