Skip to content

Commit 0098ad0

Browse files
Improve documentation (#6)
1 parent 4d861ac commit 0098ad0

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

README.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ If one wants to compare two directories with the following structures:
4747
```
4848

4949
```bash
50-
└── compared_dir
51-
├── sub_dir_1
52-
| ├── sub_file_1.a
53-
| └── sub_file_2.b
54-
| └── sub_file_3.b
55-
└── file_1.c
50+
└── compared_dir
51+
├── sub_dir_1
52+
| ├── sub_file_1.a
53+
| └── sub_file_2.b
54+
| └── sub_file_3.b
55+
└── file_1.c
5656
```
5757

5858
These two directories can be compared with the following code:
@@ -124,6 +124,34 @@ Changed the value of '[a]' from 1 to 2.
124124
Changed the value of '[b][0]' from 1 to 10.
125125
```
126126

127+
Finally, the comparators have parameters that can be passed either to be used for all files of a
128+
given extension or only for a specific file:
129+
130+
```python
131+
import dir_content_diff
132+
133+
# Get the default comparators
134+
comparators = dir_content_diff.get_comparators()
135+
136+
# Replace the comparators for JSON files to perform the comparison with a given tolerance
137+
comparators[".json"] = dir_content_diff.JsonComparator(default_diff_kwargs={"tolerance": 0.1})
138+
139+
# Use a specific tolerance for the file ``sub_dir_1/sub_file_1.a``
140+
# In this case, the kwargs are used to compute the difference by default, except the following
141+
# specific kwargs: ``return_raw_diffs``, ``load_kwargs``, ``format_data_kwargs``, ``filter_kwargs``,
142+
# ``format_diff_kwargs``, ``sort_kwargs``, ``concat_kwargs`` and ``report_kwargs``.
143+
specific_args = {"sub_dir_1/sub_file_1.a": {"tolerance": 0.5}}
144+
145+
dir_content_diff.assert_equal_trees(
146+
"reference_dir",
147+
"compared_dir",
148+
comparators=comparators,
149+
specific_args=specific_args,
150+
)
151+
```
152+
153+
Each comparator has different arguments that are detailed in the documentation.
154+
127155

128156
### Export formatted data
129157

dir_content_diff/base_comparators.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,16 @@ def _format_change_value(value):
321321
def diff(self, ref, comp, *args, **kwargs):
322322
"""Compare 2 dictionnaries.
323323
324-
This function calls :func:`dictdiffer.diff`, read the doc of this function for details on
325-
args and kwargs.
324+
This function calls :func:`dictdiffer.diff` to compare the dictionaries, read the doc of
325+
this function for details on args and kwargs.
326+
327+
Keyword Args:
328+
tolerance: Relative threshold to consider when comparing two float numbers.
329+
absolute_tolerance: Absolute threshold to consider when comparing
330+
two float numbers.
331+
ignore: Set of keys that should not be checked.
332+
path_limit: List of path limit tuples or :class:`dictdiffer.utils.Pathlimit`
333+
object to limit the diff recursion depth.
326334
"""
327335
if len(args) > 5:
328336
dot_notation = args[5]
@@ -342,7 +350,10 @@ def format_diff(self, difference):
342350

343351

344352
class JsonComparator(DictComparator):
345-
"""Comparator for JSON files."""
353+
"""Comparator for JSON files.
354+
355+
This comparator is based on the :class:`DictComparator` and uses the same parameters.
356+
"""
346357

347358
def load(self, path):
348359
"""Open a JSON file."""
@@ -352,7 +363,10 @@ def load(self, path):
352363

353364

354365
class YamlComparator(DictComparator):
355-
"""Comparator for YAML files."""
366+
"""Comparator for YAML files.
367+
368+
This comparator is based on the :class:`DictComparator` and uses the same parameters.
369+
"""
356370

357371
def load(self, path):
358372
"""Open a YAML file."""
@@ -364,6 +378,8 @@ def load(self, path):
364378
class XmlComparator(DictComparator):
365379
"""Comparator for XML files.
366380
381+
This comparator is based on the :class:`DictComparator` and uses the same parameters.
382+
367383
.. warning:: The XML files must have only one root.
368384
369385
.. note::
@@ -447,13 +463,26 @@ def xmltodict(obj):
447463

448464

449465
class PdfComparator(BaseComparator):
450-
"""Compartor for PDF files."""
466+
"""Comparator for PDF files."""
451467

452468
def diff(self, ref, comp, *args, **kwargs):
453469
"""Compare data from two PDF files.
454470
455471
This function calls the `diff_pdf_visually.pdfdiff() <https://github.com/bgeron/diff-pdf-
456472
visually/blob/21e85f1db1bdaee5c0e8e0b730771d6c4e8c3e44/diff_pdf_visually/diff.py#L83>`_
457473
function, read the doc of this function for details on args and kwargs.
474+
It compares the visual aspects of the PDF files, ignoring the invisible content (e.g. file
475+
header or invisible things like white font on white background). The PDF files are converted
476+
into images using ``ImageMagick`` and then these images are compared.
477+
478+
Keyword Args:
479+
threshold (int): The threshold used to compare the images.
480+
tempdir (pathlib.Path): Empty directory where the temporary images will be exported.
481+
dpi (int): The resolution used to convert the PDF files into images.
482+
verbosity (int): The log verbosity.
483+
max_report_pagenos (int): Only this number of the different pages will be logged (only
484+
used if the verbosity is greater than 1).
485+
num_threads (int): If set to 2 (the default), the image conversion are processed in
486+
parallel. If set to 1 it is processed sequentially.
458487
"""
459488
return not pdfdiff(ref, comp, *args, **kwargs)

0 commit comments

Comments
 (0)