iterstring package

Submodules

iterstring.iterstring module

See top level package docstring for documentation

class iterstring.iterstring.Istr[source]

Bases: str

String (str) subclass that adds to_list and to_dict convenience methods

  • By default, strip whitespace from left and right of each item

  • By default, coerce items to numbers where possible (see coerce)

  • Iterating over the object treats it like a list

  • Indexing the object treats it like a dictionary

  • For dictionaries, When keys clash, the last one wins

  • dict(Istr) does NOT work (dict makes assumptions about the iterable)
    • list(Istr) and list comprehensions work fine

  • to_list() and to_dict() reprocess the string every time
    • So listr and sistr may be more efficient and predictable

>>> from iterstring import listr # or distr

A simple use case:

>>> some_list = listr('''
item one # with a comment
  2
three
''')
>>> some_list
['item one', 2, 'three']
>>> type(some_list)
<class 'list'>

Using the class directly:

>>> from iterstring import Istr
>>> asdf = Istr('''
item one # with a comment
  2
three
''')
>>> asdf.to_list()
['item one', 2, 'three']
>>> type(asdf)
<class 'iterstring.Istr'>
>>> [x for x in asdf]
['item one', 2, 'three']
>>> fdsa = Istr('''
item one # with a comment
  2 some other value
key3 3.14159
''')
>>> asdf.to_dict()
{'item': 'one', 2: 'some other value', 'key3': 3.14159}
>>> asdf.to_dict(coerce=False)
{'item': 'one', '2': 'some other value', 'key3': '3.14159'}
to_list(lstrip=True, rstrip=True, comments=True, coerce=True)

Create line-based list representation of string

to_dict(lstrip=True, rstrip=True, comments=True, coerce=True)

Create line-based dictionary representation of string

to_dict(lstrip=True, rstrip=True, comments=True, coerce=True)[source]

Create a dictionary using the first token of each line as key

to_list(lstrip=True, rstrip=True, comments=True, coerce=True)[source]

Create a list using each line as an item

class iterstring.iterstring.TokenList[source]

Bases: str

to_list(delimiter='\\s+', comments=True, coerce=True)[source]
iterstring.iterstring.distr(x, lstrip=True, rstrip=True, comments=True, coerce=True)[source]

Convenience function for Istr(x).to_dict()

iterstring.iterstring.listr(x, lstrip=True, rstrip=True, comments=True, coerce=True)[source]

Convenience function for Istr(x).to_list()

iterstring.iterstring.numerify(x)[source]

Coerce string into float or int if possible

iterstring.iterstring.tlist(x, delimiter='\\s+', comments=True, coerce=True)[source]

Convenience function for TokenStr(x).to_list()

Module contents

Simple class that allows writing lists and dicts as heredoc strings

  • Write lists as strings with one line per element

  • Same for dictionaries, but use first token on each line as key

Features

  • Handles comments (using # characters)

  • Strips away extraneous whitespace with reasonable defaults (configurable)

  • Coerce items to numbers where possible (see coerce)

  • Iterating over the object treats it like a list

  • Indexing the object treats it like a dictionary

  • listr and distr helper functions provide simple interfaces

Examples

A simple use case:

>>> from iterstring import listr # or distr
>>> some_list = listr('''
item one # with a comment
  2
three
''')
>>> some_list
['item one', 2, 'three']
>>> type(some_list)
<class 'list'>

Using the class directly:

>>> from iterstring import Istr
>>> asdf = Istr('''
item one # with a comment
  2
three
''')
>>> asdf.to_list()
['item one', 2, 'three']
>>> type(asdf)
<class 'iterstring.Istr'>

>>> [x for x in asdf]
['item one', 2, 'three']

>>> fdsa = Istr('''
item one # with a comment
  2 some other value
key3 3.14159
''')
>>> asdf.to_dict()
{'item': 'one', 2: 'some other value', 'key3': 3.14159}
>>> asdf.to_dict(coerce=False)
{'item': 'one', '2': 'some other value', 'key3': '3.14159'}

License

  • Free software: MIT license

Documentation