iterstring package¶
Submodules¶
iterstring.iterstring module¶
See top level package docstring for documentation
- class iterstring.iterstring.Istr[source]¶
Bases:
strString (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
- iterstring.iterstring.distr(x, lstrip=True, rstrip=True, comments=True, coerce=True)[source]¶
Convenience function for Istr(x).to_dict()
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