### Setup Daffy Development Environment Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/development.md Installs project dependencies using Poetry, activates the virtual environment, and runs initial tests. ```sh poetry install poetry shell pytest ``` -------------------------------- ### Enable Pre-commit Hooks for Code Quality Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/development.md Installs pre-commit hooks to automatically check code quality with ruff and mypy on each commit. ```sh pre-commit install ``` -------------------------------- ### Daffy Quick Start: Apply Discount Example Source: https://github.com/thoughtworksinc/daffy/blob/master/README.md A practical example illustrating the application of `@df_in` and `@df_out` decorators to a function that calculates a discount on car prices. It validates input columns ('Brand', 'Price') and declares output columns ('Brand', 'Price', 'Discount'). ```Python from daffy import df_in, df_out @df_in(columns=["Brand", "Price"]) # Validate input DataFrame columns @df_out(columns=["Brand", "Price", "Discount"]) # Validate output DataFrame columns def apply_discount(cars_df): cars_df = cars_df.copy() cars_df["Discount"] = cars_df["Price"] * 0.1 return cars_df ``` -------------------------------- ### Run Tests with Coverage Report Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/development.md Executes all tests using pytest and generates a code coverage report for the daffy project. ```sh poetry run pytest --cov=daffy ``` -------------------------------- ### Run Project Tests with Pytest Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/development.md Executes all tests defined in the project using pytest. ```sh poetry run pytest ``` -------------------------------- ### Install Daffy using Pip Source: https://github.com/thoughtworksinc/daffy/blob/master/README.md Shows the command to install the Daffy library using pip, the standard Python package installer. This command fetches and installs the latest version of Daffy from the Python Package Index. ```sh pip install daffy ``` -------------------------------- ### Overriding Project-wide Strict Mode (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Demonstrates how to override the project-wide strict mode setting on individual DAFFY decorators. The example shows a @df_in decorator using the project default (true) and a @df_out decorator explicitly disabling strict mode (false). ```python # Uses strict=true from project config @df_in(columns=["Brand"]) # Explicitly disable strict mode for this decorator @df_out(columns=["Brand", "FilteredPrice"], strict=False) def filter_cars(car_df): # filter some cars return filtered_cars_df ``` -------------------------------- ### Import DAFFY Decorators (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Begin by importing the necessary decorators from the DAFFY library. These decorators are used to annotate Python functions for DataFrame validation. ```python from daffy import df_in, df_out ``` -------------------------------- ### Input Validation for Multiple Arguments (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md When a function accepts multiple arguments, you can specify which argument to validate using the 'name' parameter within the @df_in decorator. This allows precise targeting of DataFrame inputs. ```python @df_in(name="car_df", columns=["Brand", "Price"]) def process_cars(year, style, car_df): # do stuff with cars pass ``` ```python @df_in(name="car_df", columns=["Brand", "Price"]) @df_in(name="brand_df", columns=["Brand", "BrandName"]) def process_cars(car_df, brand_df): # do stuff with cars pass ``` -------------------------------- ### Project-wide Strict Mode Configuration (TOML) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Configure DAFFY's default behavior, such as enabling strict mode globally, by adding a '[tool.daffy]' section to your pyproject.toml file. Individual decorators can still override this project-level setting. ```toml [tool.daffy] strict = true ``` -------------------------------- ### Combined Input and Output Validation (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md You can apply both @df_in and @df_out decorators to a single function to validate both its input and output DataFrames. This provides comprehensive data integrity checks for function operations. ```python @df_in(columns=["Brand", "Price"]) @df_out(columns=["Brand", "Price"]) def filter_cars(car_df): # filter some cars return filtered_cars_df ``` -------------------------------- ### Log DataFrames with Data Types using @df_log(include_dtypes=True) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Enhance DataFrame logging by including data types. Use the @df_log(include_dtypes=True) annotation to capture column names along with their corresponding data types in the log output. ```python from daffy import df_log @df_log(include_dtypes=True) def process_data(input_df): # Function logic here return input_df # Example usage: # import pandas as pd # data = {'ID': [1, 2], 'Name': ['Alice', 'Bob'], 'Value': [10.5, 20.1]} # df = pd.DataFrame(data) # processed_df = process_data(df) ``` -------------------------------- ### Log DataFrames with @df_log Annotation Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Add the @df_log annotation to a Python function to log the contents of incoming and outgoing DataFrames. This helps in quickly inspecting data flow within your application. ```python from daffy import df_log @df_log def filter_cars(cars_df): # Function logic here return cars_df # Example usage: # import pandas as pd # data = {'Brand': ['Toyota', 'Honda'], 'Price': [20000, 25000]} # df = pd.DataFrame(data) # filtered_df = filter_cars(df) ``` -------------------------------- ### Regex Column Matching with @df_in (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md DAFFY supports using regular expressions within the 'columns' argument to match DataFrame column names that follow a pattern. This is useful for dynamic or similarly named columns. ```python @df_in(columns=["Brand", "r/Price_\d+/"]) def process_data(df): # This will accept DataFrames with columns like "Brand", "Price_1", "Price_2", etc. pass ``` -------------------------------- ### Output DataFrame Validation with @df_out (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md The @df_out decorator validates the DataFrame returned by a function. It checks if the returned DataFrame contains the specified columns, raising an AssertionError if any are missing. ```python @df_out(columns=["Brand", "Price"]) def get_all_cars(): # get those cars return all_cars_df ``` -------------------------------- ### Regex and Data Type Validation with @df_in (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Combine regex pattern matching with data type validation by using regex patterns as keys in the dictionary passed to the 'columns' argument. This allows validation of columns matching a pattern and their specific dtypes. ```python @df_in(columns={"Brand": "object", "r/Price_\d+/": "int64"}) def process_data(df): # This will check that "Brand" is object type and columns matching "Price_\d+" are int64 pass ``` -------------------------------- ### Data Type Validation with @df_in (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Enhance validation by checking column data types. Pass a dictionary to the 'columns' argument where keys are column names and values are expected dtypes (e.g., 'object', 'int64'). ```python @df_in(columns={"Brand": "object", "Price": "int64"}) def process_data(df): # This will check that the 'Brand' column is of object type and 'Price' is int64 pass ``` -------------------------------- ### Input DataFrame Validation with @df_in (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Use the @df_in decorator to validate DataFrame inputs to a function. Specify required columns using the 'columns' argument. This ensures the DataFrame passed to the function has the expected structure. ```python @df_in(columns=["Brand", "Price"]) def process_cars(car_df): # do stuff with cars pass ``` -------------------------------- ### Strict Mode Validation with @df_in (Python) Source: https://github.com/thoughtworksinc/daffy/blob/master/docs/usage.md Enable strict mode by setting 'strict=True' in the @df_in or @df_out decorator. In strict mode, the decorator will raise an error if the DataFrame contains any columns not explicitly defined in the annotation. ```python @df_in(columns=["Brand"], strict=True) def process_cars(car_df): # do stuff with cars pass ``` -------------------------------- ### Daffy Decorators for DataFrame Validation Source: https://github.com/thoughtworksinc/daffy/blob/master/README.md Demonstrates the use of `@df_in` and `@df_out` decorators to specify expected input and output DataFrame columns for a function. This facilitates runtime validation and documentation of DataFrame transformations. ```Python from daffy import df_in, df_out @df_in(columns=["price", "bedrooms", "location"]) @df_out(columns=["price_per_room", "price_category"]) def analyze_housing(houses_df): # Transform raw housing data into price analysis return analyzed_df ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.