Parser for function arguments
Project description
Function Argument Pre-Processor
This is a abstract library that need to be extended before put actual use.
The primary use case is to extend it as a Flask Extenstion
but is open to use with any other environment/framework that needs to deal with http endpoints eg: AWS Lambda
Implementations
Installation
$ pip install funcargpreprocessor
or download the code and run
$ python3 setup.py install
What it does?
- Extraction and transformation of function argument and raise appropriate exceptions
Example
- Please refer testimplementation.py and fieldtest.py for examples
Explanation
The following explanantion uses the example from test
folder
def get_current_time():
return datetime.now().replace(microsecond=0)
def get_current_date():
return date.today()
def get_future_date(date_factor=1):
def inner_fu():
return date.today() + timedelta(date_factor)
return inner_fu
definition = {
"pageNo": { #Key name expected from the HTTP endpoint
"data_type": int # Data type expected
, "min_val": 0 # Min validation for the key
, "max_val": 20 # Max validation for the key
, 'alias': 'page_no' # Key for the function argument, to the function the argument will be 'page_no'
# Need? most of the time the http request are expected json and the keys will be in camelCase
, "default": 1 # Default value for the field if no value has been passed
}
, "start_date": {
"data_type": DateArg('%Y-%m-%d') # Expects a date argument in <str>'YYYY-MM-DD' format or datetime.date object accepts '2020-01-10', datetime.date(2020, 1, 10) converts,in case of a string argument, to datetime.date(2020, 1, 10) and passes it to the function
, "min_val": get_current_date # Function can be passed for min value, this function should not take any argument and should return a single value of the same type
, "max_val": get_future_date(10) # Function can be passed for max value, this function should not take any argument and should return a single value of the same type
, "required": True # This key is required to be there in the input
}
, "id_list": {
"data_type": list # Expects list of value
, "nested": int # The values in the list should be int same rule as `date_type`
, "value_list": [0, 1, 2, 3] # Accepted values, valid argument ex: [1,2], [1], [2,3,0]
# Need? Multiselect options/ENUMS
}
, 'reg_time': {"data_type": DateTimeArg('%Y-%m-%d %H:%M:%S'),
"default": get_current_time # Function can be passed for default value, this function should not take any argument and should return a single value of the same type
}
, "location": {"data_type": list
, "nested": { # Custom definition for objects in the list
"address_line_1": {"data_type": str, "required": True}
, "address_line_2": {"data_type": str
, "min_len": 5 #Mininum length expected for the argument
, "max_len": 10 # Maximum length accepted for the argument
}
, "latitude": {"data_type": DecimalArg(), "min_val": Decimal("-90"), "max_val": Decimal("90")}
, "longitude": {"data_type": DecimalArg(), "min_val": Decimal("-180"), "max_val": Decimal("180")}
, "pincode": {"data_type": int, "required": True}
, "contact_person": {
"data_type": dict
, "nested": {
"first_name": {"data_type": str, "required": True}
, "last_name": {"data_type": str}
, "phone_number": {
"data_type": str
, "required": True
, "regex": r"[0-9]{10,12}" # Regular expression validation
, "regex_error_message": "<some message>" # Message when the RegEx validation fails
}
}
}
}
}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for funcargpreprocessor-0.10.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 573ec145dc93447757bf75fdc8101866ea05eaaa1a08f531f4d7bc9f23001740 |
|
MD5 | 83e070345a2923343d0df76391dd51b8 |
|
BLAKE2b-256 | e40cc543e7b3233180db0068ec3c921945128474e0059f3938b680b64dfa45bc |
Close
Hashes for funcargpreprocessor-0.10.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94125beb454979e823aa300f31199f99dd8cd05a46d51b6fd09193be0f3085f6 |
|
MD5 | 58ca9b8877f94513f2fbbe293b45485c |
|
BLAKE2b-256 | e96f2e61f284fc7dcfd0716b70320b24b91e131d482430a5a04990cd26eb1745 |