How to use the datalab.context function in datalab

To help you get started, we’ve selected a few datalab examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github googledatalab / pydatalab / legacy_tests / bigquery / query_tests.py View on Github external
def _create_context():
    project_id = 'test'
    creds = mock.Mock(spec=google.auth.credentials.Credentials)
    return datalab.context.Context(project_id, creds)
github googledatalab / pydatalab / datalab / bigquery / _table.py View on Github external
def __init__(self, name, context=None):
    """Initializes an instance of a Table object. The Table need not exist yet.

    Args:
      name: the name of the table either as a string or a 3-part tuple (projectid, datasetid, name).
        If a string, it must have the form ':.' or '.<table></table><table></table>'.
      context: an optional Context object providing project_id and credentials. If a specific
        project id or credentials are unspecified, the default ones configured at the global
        level are used.
    Raises:
      Exception if the name is invalid.
    """
    if context is None:
      context = datalab.context.Context.default()
    self._context = context
    self._api = _api.Api(context)
    self._name_parts = _utils.parse_table_name(name, self._api.project_id)
    self._full_name = '%s:%s.%s%s' % self._name_parts
    self._info = None
    self._cached_page = None
    self._cached_page_index = 0
    self._schema = None
github googledatalab / pydatalab / datalab / bigquery / _query.py View on Github external
context: an optional Context object providing project_id and credentials. If a specific
          project id or credentials are unspecified, the default ones configured at the global
          level are used.
      values: a dictionary used to expand variables if passed a SqlStatement or a string with
          variable references.
      udfs: array of UDFs referenced in the SQL.
      data_sources: dictionary of federated (external) tables referenced in the SQL.
      kwargs: arguments to use when expanding the variables if passed a SqlStatement
          or a string with variable references.

    Raises:
      Exception if expansion of any variables failed.
      """
    if context is None:
      context = datalab.context.Context.default()
    self._context = context
    self._api = _api.Api(context)
    self._data_sources = data_sources
    self._udfs = udfs

    if data_sources is None:
      data_sources = {}

    self._results = None
    self._code = None
    self._imports = []
    if values is None:
      values = kwargs

    self._sql = datalab.data.SqlModule.expand(sql, values)
github googledatalab / pydatalab / google / datalab / ml / _util.py View on Github external
def wait_for_long_running_operation(operation_full_name):
  print('Waiting for operation "%s"' % operation_full_name)
  api = discovery.build('ml', 'v1', credentials=datalab.context.Context.default().credentials)
  while True:
    response = api.projects().operations().get(name=operation_full_name).execute()
    if 'done' not in response or response['done'] is not True:
      time.sleep(3)
    else:
      if 'error' in response:
        print(response['error'])
      else:
        print('Done.')
      break
github googledatalab / pydatalab / datalab / context / commands / _projects.py View on Github external
def _set_line(args, _):
  id_ = args['id'] if args['id'] else ''
  context = datalab.context.Context.default()
  context.set_project_id(id_)
  datalab.context.Projects.save_default_id(id_)
github googledatalab / pydatalab / datalab / context / commands / _projects.py View on Github external
def _list_line(args, _):
  # TODO(gram): should we use a paginated table?
  filter_ = args['filter'] if args['filter'] else '*'
  data = [{'id': project.id, 'name': project.name}
          for project in datalab.context.Projects() if fnmatch.fnmatch(project.id, filter_)]
  return IPython.core.display.HTML(datalab.utils.commands.HtmlBuilder.render_table(data, ['id',
                                                                                          'name']))
github googledatalab / pydatalab / datalab / storage / _item.py View on Github external
def __init__(self, bucket, key, info=None, context=None):
    """Initializes an instance of an Item.

    Args:
      bucket: the name of the bucket containing the item.
      key: the key of the item.
      info: the information about the item if available.
      context: an optional Context object providing project_id and credentials. If a specific
          project id or credentials are unspecified, the default ones configured at the global
          level are used.
    """
    if context is None:
      context = datalab.context.Context.default()
    self._context = context
    self._api = _api.Api(context)
    self._bucket = bucket
    self._key = key
    self._info = info