paramfunc 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
__init__.py ADDED
File without changes
@@ -0,0 +1,79 @@
1
+ Metadata-Version: 2.4
2
+ Name: paramfunc
3
+ Version: 0.0.1
4
+ Summary: Decorator and class for easy argument conversion and validation.
5
+ Author-email: "light.seeker" <light.the.seeker@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://pypi.org/project/paramfunc/
8
+ Project-URL: Repository, https://github.com/Lightseeker7/paramfunc
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE.txt
14
+ Dynamic: license-file
15
+
16
+ # paramfunc
17
+ Decorator and class for easy argument conversion and validation..
18
+
19
+ Github: [link](https://github.com/Lightseeker7/paramfunc)
20
+
21
+ Project: [link](https://pypi.org/project/paramfunc/)
22
+
23
+ # @parameterize
24
+ The function decorator takes up to two arguments, `params` and `kwparams`, and converts the function into a callable `ParamFunc` object. This `ParamFunc` object can be called as a normal function can, only it will first convert the arguments and check them against any related parameters.
25
+
26
+ Formatted as follows:
27
+ ```
28
+ @parameterize(params=<params>,kwparams=<kwparams>)
29
+ def my_func(arg1,arg2):
30
+ return arg1 + arg2
31
+ ```
32
+ In the event a converted argument does not satisfy a parameter, it will throw an error. Any errors involved with converting the argument will also occur, where applicable.
33
+ ## params
34
+ Set to `None` if none are given.
35
+
36
+ `params` is used for all positional-only arguments, what would be called with `*args`
37
+
38
+ The format for `params` is a `dict` object formatted like the following:
39
+ ```
40
+ {"convert":convert,"params":[boolean functions]}
41
+ ```
42
+ An example of a `params` argument that converts all input items in `*args` to `int` objects and checks if they are even and not negative:
43
+ ```
44
+ {"convert":int,"params":[lambda x:x>=0,lambda x:x%2==0]}
45
+ ```
46
+ ## kwparams
47
+ Set to `None` if none are given.
48
+
49
+ `kwparams` is used for all other arguments, whether they are keyword-only or positional with a keyword.
50
+
51
+ The format for `kwparams` is a `dict` object formatted like the following:
52
+ ```
53
+ {"keyword1":{"convert":convert,"params":[boolean functions]},"keyword2":{"convert":convert,"params":[boolean functions]}}
54
+ ```
55
+ An example of a `kwparams` argument that checks if the `"file"` argument is a .txt file and the `"contents"` is a list of strings:
56
+ ```
57
+ {"file":{"convert":str,"params":[lambda x:x[-4:]==".txt",lambda x:len(x)>=5]},"contents":{"convert":list,"params":[lambda x:all(isinstance(item,str) for item in x)]}}
58
+ ```
59
+ If a keyword argument has no need for conversion or for checking against a variable, it can be omitted from `kwparams`.
60
+ ## "convert"
61
+ `"convert"` can be a `type` object such as `int` or `str`, or it can be any function that takes the related argument and returns a value. This can be a `lambda` or a function defined elsewhere in the program. If no change should happen to the related argument(s), but there still needs to be `"params"` for it, it should be set to the following:
62
+ ```
63
+ "convert":lambda x:x
64
+ ```
65
+ ## "params"
66
+ `"params"` is a list of functions that all must return boolean values. These can be `lambda` functions or functions defined elsewhere in the program. If no parameters are needed for the argument, but it needs to be converted with `"convert"`, it should just be set to an empty list:
67
+ ```
68
+ "params":[]
69
+ ```
70
+ # ParamFunc
71
+ `@paramterize` converts the function it is decorating into a `ParamFunc` object. This object has three instance variables:
72
+
73
+ `func`: the function that was decorated
74
+
75
+ `params`: the input `params` argument in the decoration, `None` if none was given.
76
+
77
+ `kwparams`: the input `kwparams` argument in the decoration, `None` if none was given.
78
+
79
+ The `ParamFunc` object can be called as a function. It will take the arguments given it, convert them using the related `"convert"` functions, and check them against any related `"params"` functions with `assert param(converted_argument)`.
@@ -0,0 +1,7 @@
1
+ __init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ paramfunc.py,sha256=sd71ar-4_icVRsZQsgO4IzxwoUnAeYmqHnyk0vy3DO0,4428
3
+ paramfunc-0.0.1.dist-info/licenses/LICENSE.txt,sha256=Tbu8cV6DoIA8FPd5AwbP8chHBJ47EnBmuEE6pV0uIiw,1069
4
+ paramfunc-0.0.1.dist-info/METADATA,sha256=L8OZIWt0iivOSohOgqlSf4iq7f3umRrcHuJjMPMrgmo,3958
5
+ paramfunc-0.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
6
+ paramfunc-0.0.1.dist-info/top_level.txt,sha256=DRyjZrhaycE68e5B1zZjffXXj27RtSIC-quQ1LGs-jA,19
7
+ paramfunc-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 light.seeker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ __init__
2
+ paramfunc
paramfunc.py ADDED
@@ -0,0 +1,99 @@
1
+ """Decorator and class for easy argument conversion and validation."""
2
+
3
+ def parameterize(params:list|None=None,kwparams:dict|None=None):
4
+ """
5
+ Decorator function that converts the function into a callable ParamFunc object
6
+
7
+ Format of params argument:
8
+ {convert,[boolean functions]}
9
+ e.g.
10
+ {str,[lambda x:x[-4:]==".txt",lambda x:len(x)>=5]}
11
+ Format of kwparams argument:
12
+ {"key word":{"convert":convert,"params":[boolean functions]}}
13
+ e.g.
14
+ {"num1":{"convert":int,"params":[lambda x:x%3==0, lambda x:x>=0]}}
15
+
16
+ convert can be any function that takes an argument and returns a converted argument (use lambda x:x if it needs to stay the same)
17
+ boolean functions can be lambdas or any function that takes the converted argument and returns a boolean
18
+
19
+ key word arguments with no conversion functions or parameters can be left empty
20
+ """
21
+ def create_param_func(func:function):
22
+ return ParamFunc(func,params=params,kwparams=kwparams)
23
+ return create_param_func
24
+
25
+ class ParamFunc:
26
+
27
+ def __init__(self,func:function,params:list|None=None,kwparams:dict|None=None):
28
+ """
29
+ Format of params argument:
30
+ {"convert":convert,"params":[boolean functions]}
31
+ e.g.
32
+ {"convert":str,"params":[lambda x:x[-4:]==".txt",lambda x:len(x)>=5]}
33
+ Format of kwparams argument:
34
+ {"key word":{"convert":convert,"params":[boolean functions]}}
35
+ e.g.
36
+ {"num1":{"convert":int,"params":[lambda x:x%3==0, lambda x:x>=0]}}
37
+
38
+ convert can be any function that takes an argument and returns a converted argument (use lambda x:x if it needs to stay the same)
39
+ boolean functions can be lambdas or any function that takes the converted argument and returns a boolean
40
+ """
41
+ self.func = func
42
+ self.params = params
43
+ self.kwparams = kwparams
44
+
45
+ def __call__(self, *args, **kwargs):
46
+ varnames = self.func.__code__.co_varnames[:self.func.__code__.co_argcount] # will be () if none
47
+ def_var = self.func.__defaults__ # will be None if none
48
+ kw_def_var = self.func.__kwdefaults__ # will be None if none
49
+
50
+ converted_kwargs = {}
51
+
52
+ if kw_def_var != None:
53
+ for kw_var in kw_def_var.items():
54
+ converted_kwargs[kw_var[0]] = kw_var[1]
55
+
56
+ if def_var != None:
57
+ for var in range(-1,-1*len(def_var)-1,-1):
58
+ converted_kwargs[varnames[var]] = def_var[var]
59
+
60
+ if len(varnames) > 0:
61
+ if self.kwparams != None:
62
+ for var in range(len(args)):
63
+ if varnames[var] in self.kwparams.keys():
64
+ converted_arg = self.kwparams[varnames[var]]["convert"](args[var])
65
+ for param in self.kwparams[varnames[var]]["params"]:
66
+ assert param(converted_arg), f"arg {args[var]} doesn't satisfy parameter(s)"
67
+ converted_kwargs[varnames[var]] = converted_arg
68
+ else:
69
+ converted_kwargs[varnames[var]] = args[var]
70
+ else:
71
+ for var in range(len(args)):
72
+ converted_kwargs[varnames[var]] = args[var]
73
+ args = args[len(varnames):]
74
+
75
+ converted_args = []
76
+
77
+ if self.params != None:
78
+ for arg in args:
79
+ converted_arg = self.params["convert"](arg)
80
+ for param in self.params["params"]:
81
+ assert param(converted_arg), f"arg {arg} doesn't satisfy parameter(s)"
82
+ converted_args.append(converted_arg)
83
+ else:
84
+ converted_args = args
85
+
86
+ if self.kwparams != None:
87
+ for kwarg in kwargs.items():
88
+ if kwarg[0] in self.kwparams.keys():
89
+ converted_kwarg = self.kwparams[kwarg[0]]["convert"](kwarg[1])
90
+ for param in self.kwparams[kwarg[0]]["params"]:
91
+ assert param(converted_kwarg), f"arg {kwarg[1]} doesn't satisfy parameter(s)"
92
+ converted_kwargs[kwarg[0]] = converted_kwarg
93
+ else:
94
+ converted_kwargs[kwarg[0]] = kwarg[1]
95
+ else:
96
+ for kwarg in kwargs.items():
97
+ converted_kwargs[kwarg[0]] = kwarg[1]
98
+
99
+ return self.func(*converted_args,**converted_kwargs)