def test_formatiterable(): assert textutils.format_iterable([1, 2, 3]) == '1, 2 and 3' assert textutils.format_iterable([1, 2]) == '1 and 2' assert textutils.format_iterable(('trick', 'treat'), textutils.OR) == 'trick or treat' assert textutils.format_iterable([1]) == '1' assert textutils.format_iterable([]) == '' with pytest.raises(TypeError): textutils.format_iterable(v for v in [1, 2, 3]) textutils.format_iterable('test')
def find_path(self, table, refresh=False): """ Tries to resolve the full (qualified) path for the specified table or feature class and returns it, by looking up the matching feature dataset (or workspace root when not found). In contrast to the :func:`make_path` function, the path is verified before it is returned. :param table: The (unqualified) table or feature class name to find. :param refresh: Updates the internal table lookup first. See note below. :type table: str :type refresh: bool :rtype: str :raises ValueError: If the table was not found or found multiple times (should not happen). Example: >>> wm = Workspace(r'C:/temp/db_user.sde') >>> wm.qualifier 'user' >>> wm.find_path('ele_cable') # finds the feature class in feature dataset "ELE" 'C:\\temp\\db_user.sde\\user.ele\\user.ele_cable' .. note:: The feature dataset lookup is created once on the first call to this function. This means that the first call is relatively slow and consecutive ones are fast. When the user creates new feature class paths using the :func:`make_path` method, the lookup is updated automatically, so that this function can find the new feature class. However, when the workspace is updated *from the outside*, the lookup is not updated. If the user wishes to force-update the lookup, set the *refresh* argument to ``True``. """ if refresh or not self._fds_lookup: self._fds_lookup = self._map_fds() table_name = unqualify(table) fds = self._fds_lookup.get(table_name.lower(), (_const.CHAR_EMPTY, )) if len(fds) > 1: # This case is rare, but it could happen (e.g. when qualifiers are different, but table name matches) raise ValueError('{} could belong to {}'.format( _tu.to_repr(table_name), _tu.format_iterable(fds, _const.TEXT_OR))) qualifier = self._sep.join(fds[0].split(self._sep)[:-1]) path = self._make_path(qualifier, self._sep, fds[0], table_name) if not _arcpy.Exists(path): raise ValueError('{} was not found at {}'.format( _tu.to_repr(table_name), path)) return path
def __init__(self, workspace, relation_type, reverse=False): rel_types = (_const.GNRELTYPE_AGGREGRATE, _const.GNRELTYPE_COMPOSITE, _const.GNRELTYPE_DATALINK, _const.GNRELTYPE_ENTITY, _const.GNRELTYPE_LABEL, _const.GNRELTYPE_PLAN, _const.GNRELTYPE_RELATE, _const.GNRELTYPE_SHAPEGROUP) # Check if workspace is a Workspace instance and that a valid relation_type has been set _vld.pass_if(isinstance(workspace, _paths.Workspace), ValueError, '{!r} requires a {} object'.format(DefinitionTable.__name__, _paths.Workspace.__name__)) _vld.pass_if(relation_type in rel_types, ValueError, 'relation_type must be one of {}'.format(_tu.format_iterable(rel_types, _const.TEXT_OR))) # Construct relation definition table path and where clause table_path = str(workspace.make_path(_const.GNTABLE_RELATION_DEF)) type_filter = _queries.Where(_const.GNFIELD_REL_TYPE).Equals(relation_type) # Define required field default names src_table = _const.GNFIELD_REL_TABLE_SRC dst_table = _const.GNFIELD_REL_TABLE_DST src_field = _const.GNFIELD_REL_KEYFIELD_SRC dst_field = _const.GNFIELD_REL_KEYFIELD_DST src_rel = _const.GNFIELD_REL_RELFIELD_SRC dst_rel = _const.GNFIELD_REL_RELFIELD_DST # Switch source and destination fields if 'reverse' is True if reverse: src_table = _const.GNFIELD_REL_TABLE_DST dst_table = _const.GNFIELD_REL_TABLE_SRC src_field = _const.GNFIELD_REL_KEYFIELD_DST dst_field = _const.GNFIELD_REL_KEYFIELD_SRC src_rel = _const.GNFIELD_REL_RELFIELD_DST dst_rel = _const.GNFIELD_REL_RELFIELD_SRC # Set fields to collect fields = (dst_table, _const.GNFIELD_REL_TABLE_REL, src_field, dst_field, src_rel, dst_rel, _const.GNFIELD_REL_TYPE) try: # Try and build a relationship lookup super(RelationTable, self).__init__(table_path, src_table, fields, type_filter) except RuntimeError as e: raise RuntimeError("Failed to read GEONIS relation table '{}': {}".format(table_path, e))