Skip to content

Commit 6d154fd

Browse files
Ken KundertKen Kundert
Ken Kundert
authored and
Ken Kundert
committed
Restructure the documentation, removing redundancies.
1 parent c092018 commit 6d154fd

File tree

7 files changed

+94
-123
lines changed

7 files changed

+94
-123
lines changed

doc/api.rst renamed to doc/exceptions.rst

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
1-
Programmer's Interface
2-
======================
3-
4-
5-
Reader
6-
------
7-
8-
.. autofunction:: nestedtext.loads
9-
10-
11-
Writer
12-
------
13-
14-
.. autofunction:: nestedtext.dumps
15-
16-
17-
Exception
18-
---------
1+
Exceptions
2+
----------
193

204
*NestedText* imports the *Error* exception from `inform
215
<https://inform.readthedocs.io/en/stable/api.html#exceptions>`_ and renames it
@@ -30,7 +14,9 @@ exception there.
3014
3115
3216
As with most exceptions, you can simply cast it to a string to get a reasonable
33-
error message:
17+
error message.
18+
19+
**Example:**
3420

3521
.. code-block:: python
3622
@@ -52,6 +38,8 @@ You can also use the *report* method to print the message directly. This is
5238
appropriate if you are using *inform* for your messaging as it follows
5339
*inform*'s conventions.
5440

41+
**Example:**
42+
5543
.. code-block:: python
5644
5745
>> try:
@@ -61,6 +49,8 @@ appropriate if you are using *inform* for your messaging as it follows
6149
6250
The *terminate* method prints the message directly and exits.
6351

52+
**Example:**
53+
6454
.. code-block:: python
6555
6656
>> try:
@@ -73,7 +63,9 @@ contains the basic text of the message. You can change this message by
7363
overriding the attribute when using *report*, *terminate*, or *render*.
7464
*render* is like casting the exception to a string except that allows for the
7565
passing of arguments. For example, to convert a particular message to Spanish,
76-
you could use:
66+
you could use something like the following.
67+
68+
**Example:**
7769

7870
.. code-block:: python
7971
@@ -85,4 +77,3 @@ you could use:
8577
... template = 'llave duplicada: {}.'
8678
... print(e.render(template=template))
8779
3: llave duplicada: name.
88-

doc/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Documentation
9696
format
9797
reader
9898
writer
99-
api
99+
exceptions
100100
releases
101101

102102
* :ref:`genindex`

doc/reader.rst

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
Reader
22
------
33

4-
You can read *NestedTest* data using :func:`nestedtext.loads()`, which takes
5-
a string as an argument:
4+
*loads* converts *NestedText* to Python data objects.
5+
6+
.. autofunction:: nestedtext.loads
7+
8+
**Example:**
69

710
.. code-block:: python
811
@@ -27,10 +30,24 @@ a string as an argument:
2730
'age': '74',
2831
}
2932
33+
*NestedText* is specified to *loads* in the form of a string:
34+
3035
*loads()* takes an optional second argument, *culprit*. If specified, it will be
3136
prepended to any error messages. It is often used to designate the source of
3237
*contents*. For example,if *contents* were read from a file, *culprit* would be
33-
the file name.
38+
the file name. Here is a typical example of reading *NestedText* from a file:
39+
40+
**Example:**
41+
42+
.. code-block:: python
43+
44+
>>> filename = 'examples/duplicate-keys.nt'
45+
>>> try:
46+
... with open(filename) as f:
47+
... addresses = nestedtext.loads(f.read(), filename)
48+
... except nestedtext.NestedTextError as e:
49+
... print(str(e))
50+
examples/duplicate-keys.nt, 5: duplicate key: name.
3451
3552
3653
*NestedText* as a Structure Parser

doc/writer.rst

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
Writer
22
------
33

4-
You can use :func:`nestedtext.dumps()` to convert a data structure consisting of
5-
dictionaries, lists, and strings to *NestedText*:
4+
*dumps* converts Python data objects to *NextedText*.
5+
6+
.. autofunction:: nestedtext.dumps
7+
8+
**Example:**
69

710
.. code-block:: python
811
@@ -37,7 +40,9 @@ list-like types such as *tuple* and *set* by converting them to the types
3740
supported by the format. This implies that a round trip through *dumps* and
3841
*loads* could result in the types of values being transformed. You can prevent
3942
this by passing `default='strict'` to *dumps*. Doing so means that values that
40-
are not dictionaries, lists, or strings generate exceptions:
43+
are not dictionaries, lists, or strings generate exceptions.
44+
45+
**Example:**
4146

4247
.. code-block:: python
4348
@@ -59,7 +64,9 @@ are not dictionaries, lists, or strings generate exceptions:
5964
6065
Alternatively, you can specify a function to *default*, which is used to convert
6166
values to strings. It is used if no other converter is available. Typical
62-
values are *str* and *repr*:
67+
values are *str* and *repr*.
68+
69+
**Example:**
6370

6471
.. code-block:: python
6572
@@ -85,7 +92,9 @@ values are *str* and *repr*:
8592
house: red
8693
8794
You can also specify a dictionary of renderers. The dictionary maps the object
88-
type to a render function:
95+
type to a render function.
96+
97+
**Example:**
8998

9099
.. code-block:: python
91100
@@ -106,7 +115,9 @@ type to a render function:
106115
house: red
107116
108117
If the dictionary maps a type to *None*, then the default behavior is used for
109-
that type. If it maps to *False*, then an exception is raised:
118+
that type. If it maps to *False*, then an exception is raised.
119+
120+
**Example:**
110121

111122
.. code-block:: python
112123

examples/address.nt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Contact information for our officers
2+
3+
president:
4+
name: Katheryn McDaniel
5+
address:
6+
> 138 Almond Street
7+
> Topika, Kansas 20697
8+
phone:
9+
cell: 1-210-555-5297
10+
home: 1-210-555-8470
11+
12+
kids:
13+
- Joanie
14+
- Terrance
15+
16+
vice president:
17+
name: Margaret Hodge
18+
address:
19+
> 2586 Marigold Lane
20+
> Topika, Kansas 20682
21+
phone: 1-470-555-0398
22+
23+
kids:
24+
- Arnie
25+
- Zach
26+
- Maggie
27+
28+
treasurer:
29+
name: Fumiko Purvis
30+
address:
31+
> 3636 Buffalo Ave
32+
> Topika, Kansas 20692
33+
phone: 1-268-555-0280
34+
35+
kids:
36+
- Lue
37+
comments:
38+
> Fumiko's term is ending at the end of the year.
39+
> She will be replaced by Mariah Mobley.

examples/duplicate-keys.nt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this is an invalid NestedText file because it contains a dictionary with
2+
# duplicate keys
3+
4+
name:
5+
name:

nestedtext.py

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -294,22 +294,6 @@ def loads(contents, culprit=None):
294294
Returns:
295295
A dictionary or list containing the data. If contents is empty, an
296296
empty dictionary is returned.
297-
298-
**Example**::
299-
300-
>>> import nestedtext
301-
>>> contents = '''
302-
... name: Deryl McKinnon
303-
... phone: 212-590-3107
304-
... '''
305-
306-
try:
307-
data = nestedtext.loads(contents)
308-
print(data)
309-
except nestedtext.NestedTextError as e:
310-
e.report()
311-
{'name': 'Deryl McKinnon', 'phone': 'phone: 212-590-3107'}
312-
313297
"""
314298
with set_culprit(culprit):
315299
lines = Lines(contents)
@@ -370,82 +354,6 @@ def dumps(obj, *, sort_keys=False, indent=4, renderers=None, default=None, level
370354
this is used to increment the level and so the indent. Generally
371355
not specified by the user, but can be useful in unusual situations
372356
to specify an initial indent.
373-
374-
**Example**::
375-
376-
>>> import nestedtext
377-
378-
>>> try:
379-
... print(nestedtext.dumps({'a': [0, 1], 'b': [2, 3, 4]}))
380-
... except nestedtext.NestedTextError as e:
381-
... e.report()
382-
a:
383-
- 0
384-
- 1
385-
b:
386-
- 2
387-
- 3
388-
- 4
389-
390-
*dumps* has built in support for the base Python types of *None*, *bool*,
391-
*str*, *float*, *list*, *tuple*, *set*, and *dict*. If *default* = 'strict'
392-
is specified, that list shrinks to *str*, *list*, and *dict*.
393-
394-
You must make special arrangements to handle objects of other types. There
395-
are two approaches that can be used separately or together. You can specify
396-
a default renderer that converts any unknown object type to a string.
397-
398-
**Example**::
399-
400-
>>> class Color:
401-
... def __init__(self, color):
402-
... self.color = color
403-
... def __repr__(self):
404-
... return f'Color({self.color!r})'
405-
... def __str__(self):
406-
... return self.color
407-
408-
>>> data = {'key': 42, 'value': 3.1415926, 'valid': True, 'color': Color('red')}
409-
>>> try:
410-
... print(nestedtext.dumps(data))
411-
... except nestedtext.NestedTextError as e:
412-
... print(str(e))
413-
Color('red'): unsupported type.
414-
415-
>>> print(nestedtext.dumps(data, default=repr))
416-
key: 42
417-
value: 3.1415926
418-
valid: True
419-
color: "Color('red')"
420-
421-
>>> print(nestedtext.dumps(data, default=str))
422-
key: 42
423-
value: 3.1415926
424-
valid: True
425-
color: red
426-
427-
You may also specify a dictionary of renderers.
428-
429-
**Example**::
430-
431-
>>> renderers = {
432-
... bool: lambda b: 'yes' if b else 'no',
433-
... int: hex,
434-
... float: lambda f: f'{f:0.3}',
435-
... Color: lambda c: c.color,
436-
... }
437-
438-
>>> try:
439-
... print(nestedtext.dumps(data, renderers=renderers))
440-
... except nestedtext.NestedTextError as e:
441-
... e.report()
442-
key: 0x2a
443-
value: 3.14
444-
valid: yes
445-
color: red
446-
447-
Mapping a type to *False* in *renderers* results in an exception being
448-
raised if that type is found.
449357
"""
450358

451359
# define sort function

0 commit comments

Comments
 (0)