### Installation Source: https://github.com/samuelcolvin/dirty-equals/blob/main/README.md Command to install the dirty-equals library. ```bash pip install dirty-equals ``` -------------------------------- ### Trivial Usage Source: https://github.com/samuelcolvin/dirty-equals/blob/main/docs/index.md A simple example demonstrating the basic usage of IsPositive. ```python from dirty_equals import IsPositive assert 1 == IsPositive # (1)! assert -2 == IsPositive # this will fail! (2) ``` -------------------------------- ### Basic Usage Source: https://github.com/samuelcolvin/dirty-equals/blob/main/README.md A trivial example of what dirty-equals can do. ```python from dirty_equals import IsPositive assert 1 == IsPositive assert -2 == IsPositive # this will fail! ``` -------------------------------- ### pytest error example Source: https://github.com/samuelcolvin/dirty-equals/blob/main/docs/usage.md An example demonstrating how dirty-equals improves pytest output when assertions fail, making it easier to identify differences. ```python from datetime import datetime from dirty_equals import IsNow, IsPositiveInt def test_partial_dict(): api_response_data = { 'id': 1, # (1)! 'first_name': 'John', 'last_name': 'Doe', 'created_at': datetime.now().isoformat(), 'phone': '+44 123456789', } assert api_response_data == { 'id': IsPositiveInt(), 'first_name': 'John', 'last_name': 'Doe', 'created_at': IsNow(iso_string=True), # phone number is missing, so the test will fail } ``` -------------------------------- ### More Powerful Usage Source: https://github.com/samuelcolvin/dirty-equals/blob/main/README.md An example demonstrating more powerful usage of dirty-equals in a unit test context, checking various data types and formats. ```python from dirty_equals import IsJson, IsNow, IsPositiveInt, IsStr ... # user_data is a dict returned from a database or API which we want to test assert user_data == { # we want to check that id is a positive int 'id': IsPositiveInt, # we know avatar_file should be a string, but we need a regex as we don't know whole value 'avatar_file': IsStr(regex=r'/[a-z0-9\-]{10}/example\.png'), # settings_json is JSON, but it's more robust to compare the value it encodes, not strings 'settings_json': IsJson({'theme': 'dark', 'language': 'en'}) # created_ts is datetime, we don't know the exact value, but we know it should be close to now 'created_ts': IsNow(delta=3), } ``` -------------------------------- ### More Powerful Usage Source: https://github.com/samuelcolvin/dirty-equals/blob/main/docs/index.md An example showcasing more advanced usage within a unit test, including checking various data types and formats. ```python from dirty_equals import IsJson, IsNow, IsPositiveInt, IsStr def test_user_endpoint(client: 'HttpClient', db_conn: 'Database'): client.post('/users/create/', data=...) user_data = db_conn.fetchrow('select * from users') assert user_data == { 'id': IsPositiveInt, # (1)! 'username': 'samuelcolvin', # (2)! 'avatar_file': IsStr(regex=r'/[a-z0-9\-]{10}/example\.png'), # (3)! 'settings_json': IsJson({'theme': 'dark', 'language': 'en'}), # (4)! 'created_ts': IsNow(delta=3), # (5)! } ``` -------------------------------- ### __repr__ Source: https://github.com/samuelcolvin/dirty-equals/blob/main/docs/usage.md Illustrates the default __repr__ methods of dirty-equals types and how they change after a successful comparison. ```python from dirty_equals import IsApprox, IsInt assert repr(IsInt) == 'IsInt' assert repr(IsInt()) == 'IsInt()' assert repr(IsApprox(42)) == 'IsApprox(approx=42)' ``` ```python from dirty_equals import IsInt v = IsInt() assert 42 == v assert repr(v) == '42' ``` -------------------------------- ### Initialised vs. Uninitialised Source: https://github.com/samuelcolvin/dirty-equals/blob/main/docs/usage.md Shows that dirty-equals allows comparison with types whether they are initialised or not. ```python from dirty_equals import IsInt # these two cases are the same assert 1 == IsInt assert 1 == IsInt() ``` -------------------------------- ### IsDatetime & timezones Source: https://github.com/samuelcolvin/dirty-equals/blob/main/docs/types/datetime.md Demonstrates how IsDatetime handles timezone-aware and naive datetimes with enforce_tz=True and enforce_tz=False. ```Python from datetime import datetime from zoneinfo import ZoneInfo from dirty_equals import IsDatetime tz_london = ZoneInfo('Europe/London') new_year_london = datetime(2000, 1, 1, tzinfo=tz_london) tz_nyc = ZoneInfo('America/New_York') new_year_eve_nyc = datetime(1999, 12, 31, 19, 0, 0, tzinfo=tz_nyc) assert new_year_eve_nyc == IsDatetime(approx=new_year_london, enforce_tz=False) assert new_year_eve_nyc != IsDatetime(approx=new_year_london, enforce_tz=True) new_year_naive = datetime(2000, 1, 1) assert new_year_naive != IsDatetime(approx=new_year_london, enforce_tz=False) assert new_year_naive != IsDatetime(approx=new_year_eve_nyc, enforce_tz=False) assert new_year_london == IsDatetime(approx=new_year_naive, enforce_tz=False) assert new_year_eve_nyc != IsDatetime(approx=new_year_naive, enforce_tz=False) ``` -------------------------------- ### Boolean Combination of Types Source: https://github.com/samuelcolvin/dirty-equals/blob/main/docs/usage.md Demonstrates combining dirty-equals types using '&' (and) and '|' (or) operators, as well as inverting checks with '~'. ```python from dirty_equals import Contains, HasLen assert ['a', 'b', 'c'] == HasLen(3) & Contains('a') # (1)! assert ['a', 'b', 'c'] == HasLen(3) | Contains('z') # (2)! ``` ```python assert ['a', 'b', 'c'] != Contains('z') assert ['a', 'b', 'c'] == ~Contains('z') ``` -------------------------------- ### IsEven Source: https://github.com/samuelcolvin/dirty-equals/blob/main/docs/types/custom.md A custom type that matches any even number. ```python from decimal import Decimal from typing import Any, Union from dirty_equals import DirtyEquals, IsOneOf class IsEven(DirtyEquals[Union[int, float, Decimal]]): def equals(self, other: Any) -> bool: return other % 2 == 0 assert 2 == IsEven assert 3 != IsEven assert 'foobar' != IsEven assert 3 == IsEven | IsOneOf(3) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.