marsh.path

Path tools that work with any separator character.

iterative_split(path, delimiter='.')[source]

Iterate through the fields in a path.

Escaped or quoted (unescaped quotes) delimiters will become part of a field instead of splitting it into more fields. Quotes are removed from the final fields.

Parameters:
  • path (str) – The path to split into fields and iterate through.

  • delimiter (str) – The delimiter between fields in the path.

Return type:

Iterator[str]

Returns:

Field iterator.

split(path, delimiter='.')[source]

Split the fields of a path.

Escaped or quoted (unescaped quotes) delimiters will become part of a field instead of splitting it into more fields. Quotes are removed from the final fields.

Parameters:
  • path (str) – The path to split into fields.

  • delimiter (str) – The delimiter between fields in the path.

Return type:

Sequence[str]

Returns:

The fields.

escape_field(field, delimiter='.')[source]

Escape all delimiters and quotes found in a field.

This may be done to maintain the original field when it becomes part of a path that is later split back into fields.

Parameters:
  • field (str) – The field to escape characters in.

  • delimiter (str) – The delimiter used.

Return type:

str

Returns:

The escaped field.

strip_delimiter(field_or_path, delimiter='.')[source]

Remove any delimiters from the start and end of a field or path.

Escaped delimiters are not removed.

Parameters:
  • field_or_path (str) – The field or path to strip delimiters from.

  • delimiter (str) – The delimiter to strip.

Return type:

str

Returns:

The field or path with stripped delimiters.

prepend(path, field, delimiter='.')[source]

Prepend a field to a path.

Parameters:
  • field (str) – The field to prepend to a path.

  • path (str) – The path to prepend the field to.

  • delimiter (str) – The delimiter used between fields in the path.

Return type:

str

Returns:

The prepended path.

append(path, field, delimiter='.')[source]

Append a field to a path.

Parameters:
  • field (str) – The field to append to a path.

  • path (str) – The path to append the field to.

  • delimiter (str) – The delimiter used between fields in the path.

Return type:

str

Returns:

The appended path.

join_fields(fields, delimiter='.')[source]

Create a path from fields.

Parameters:
  • fields (Iterable[str]) – The fields to create a path from.

  • delimiter (str) – The delimiter to use in the path between fields.

Return type:

str

Returns:

The path.

head(path, delimiter='.')[source]

Split the first field in a path from the path.

Parameters:
  • path (str) – The path to split.

  • delimiter (str) – The delimiter used between fields in the path.

Return type:

_head_return_value

Returns:

A tuple of the first field in the path and the remaining path.

tail(path, delimiter='.')[source]

Split the last field in a path from the path.

Parameters:
  • path (str) – The path to split.

  • delimiter (str) – The delimiter used between fields in the path.

Return type:

_tail_return_value

Returns:

A tuple of the last field in the path and the remaining path.