Cara menggunakan DEEPDIFF pada Python
Show
Get a Meaningful Assertion Error When Comparing Two Python ObjectsMotivationWhen comparing two Python objects, you might not want the test to focus on some trivial differences such as the order of the values in a list. For example, you might want the test to consider However, you get an error when the order is different. Image by AuthorIs there a way that you can ignore the order when comparing two Python objects? Image by Author Image by AuthorThat is when DeepDiff comes in handy. In this article, you will learn how to use DeepDiff to prevent comparing certain parts of Python objects. What is DeepDiff?DeepDiff is a Python library that recursively looks for all the changes in dictionaries, iterables, strings, and other objects. To install DeepDiff, type: pip install deepdiff Descriptive ErrorWhen comparing between two different Python objects using AssertionError: assert {'apple': 2, 'banana': [3, 2, 2], 'orange': 3} == {'apple': 2, 'banana': [3, 2], 'orange': 3} This assertion error is not very informative since we don’t know the exact elements that make these two dictionaries different. With DeepDiff, we can see a more descriptive error showing what the differences are and where the differences occur. Image by Author — Copy the code above hereFrom the error above, we know three things:
Tree ViewThe default view of Ignore OrderAs shown at the beginning of the
article, you can ignore the order using The output You can also use Ignore Small Difference Between Two NumbersIgnore up to a Certain DigitIt can be annoying to see the assertion error when two numbers are very close to each other. If you want to ignore the difference between two numbers after a particular digit, use In the code below, we only compare the two numbers up to the second digit. Image by Author — Copy the code above hereIgnore the Difference Below An EpsilonSometimes,
two numbers are very close, but they don’t have similar digits. To ignore small differences between two numbers, use In the code below, I use Ignore String CaseIf you want to ignore string
case (.i.e, “Red” and “red”), use Ignore NaN InequalityIf you have worked with some NaNs, you might know that not every NaNs in Python are equal: Image by AuthorThus,
it can be confusing to compare objects that contain different types of NaNs. (Isn’t To ignore different types of NaNs, use Exclude TypesSometimes, you might not care if certain types change or not. To include certain data types, use Ignore Numeric Type
Truncate DatetimeWhen comparing between two datetime objects, you might just want to make sure they are similar to a certain extent (have the same hour, not the same hour and minute). Image by AuthorYou can specify to how precise Ignore PathIf you want to exclude certain
paths from the report, you Exclude Regrex PathIf you want to ignore multiple paths with a certain pattern, use For example, to avoid comparing Check this cheatsheet if you are not familiar with regular expression. Use DeepDiff with pytestTo use DeepDiff with pytest, write ConclusionCongratulations! You have just learned how to ignore certain elements when comparing between two Python objects using DeepDiff. I hope this tool will make it easier for you to write tests and debug your code. Feel free to play and fork the source code of this article here: I like to write about basic data science concepts and play with different data science tools. You could connect with me on LinkedIn and Twitter. Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: |