diff --git a/README.md b/README.md index 59a3efc..51e61c6 100644 --- a/README.md +++ b/README.md @@ -1 +1,11 @@ -**Hello world!!!** +## Overview +This block (`block.py`) is responsible for assigning grade for the passed in probability. + +## Key Inputs & Outputs +- **Request**: Refer to `request_schema.json` for detailed input fields and validation rules. +- **Response**: Refer to `response_schema.json` for the returned structure and data types. + +## Implementation Details +- All core logic resides in `block.py` within the `__main__` function. +- Example usage and validation are demonstrated in `test_block.py`. + diff --git a/block.py b/block.py index 3b227f9..17e6ed4 100644 --- a/block.py +++ b/block.py @@ -1,21 +1,33 @@ -@flowx_block -def example_function(request: dict) -> dict: +import logging - # Processing logic here... +# Configure logging +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s [%(levelname)s] %(name)s - %(message)s", +) +logger = logging.getLogger(__name__) - return { - "meta_info": [ - { - "name": "created_date", - "type": "string", - "value": "2024-11-05" - } - ], - "fields": [ - { - "name": "", - "type": "", - "value": "" - } - ] - } +def __main__(probability: float) -> dict: + logger.info("Received input: probability=%.8f", float(probability)) + + if not isinstance(probability, (int, float)): + logger.error("Invalid input type: probability=%s", type(probability).__name__) + raise ValueError("Input probability must be a number (int or float)") + + if probability <= 0.33: + grade = "G1" + elif 0.33 < probability <= 0.41: + grade = "G2" + elif 0.41 < probability <= 0.48: + grade = "G3" + elif 0.48 < probability <= 0.61: + grade = "G4" + elif 0.61 < probability <= 0.65: + grade = "G5" + else: + grade = "G6" + + result = {"grade": grade} + + logger.info("Fraud V1 Grade: %s", result) + return result \ No newline at end of file diff --git a/request_schema.json b/request_schema.json index 0967ef4..2f6a8d0 100644 --- a/request_schema.json +++ b/request_schema.json @@ -1 +1,11 @@ -{} +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "probability": { + "type": "number", + "description": "Fraud Model predicted score." + } + }, + "required": [] +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 0967ef4..8c4acbc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -{} +jsonschema==4.23.0 \ No newline at end of file diff --git a/response_schema.json b/response_schema.json index 0967ef4..a990940 100644 --- a/response_schema.json +++ b/response_schema.json @@ -1 +1,10 @@ -{} +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "grade": { + "type": "string", + "description": "PD v2 grade." + } + } +} \ No newline at end of file diff --git a/test_block.py b/test_block.py new file mode 100644 index 0000000..12014c6 --- /dev/null +++ b/test_block.py @@ -0,0 +1,15 @@ +import unittest +from block import __main__ + +class TestBlock(unittest.TestCase): + + def test_main_success(self): + result = __main__(probability=0.3890174329280853) + self.assertEqual(result, {"grade": "G2"}) + + def test_main_invalid_input(self): + with self.assertRaises(ValueError): + __main__(probability="0.40") # Invalid input type (string) + +if __name__ == "__main__": + unittest.main()