Package: valueparser
ParserPython package for quick creation/ combination of value parser object for muly-puposesInstall>pipinstallvalueparserUsagefromvalueparser.parsersimportBoundedvalue_parser=Bounded(min=0,max=1)value_parser.parse(4.0)# ParseError: 4.0 is higher than 1.0Several parsers can be combined to one single parser object, they share however the same name space for configuration
parametersfromvalueparserimportparser,Clipped,Roundedratio_parser=parser((float,Clipped,Rounded),min=0,max=1.0,ndigits=2)assertratio_parser.parse(0.231234)==0.23assertratio_parser.parse(4.5)==1.0assertratio_parser.parse("0.12345")==0.12Equivalent can be done by creating a new classfromvalueparserimportparser_class,Clipped,RoundedMyParser=parser_class((float,Clipped,Rounded),"MyParser")ratio_parser=MyParser(min=0,max=1,ndigits=2)conparserworks the same way thanparserexcept it construct a typing object to be use inside pydantic BaseModel in
a compact way.fromvalueparserimportconparser,BoundedfrompydanticimportBaseModelPixel=conparser((int,Bounded),min=0,max=1023)classData(BaseModel):x:Pixel=512y:Pixel=512Data(x=-200)##pydantic.error_wrappers.ValidationError: 1 validation error for Data#x#    -200.0 is lower than 0.0 (type=value_error.parse; error_code=Errors.OUT_OF_BOUND)to make any function aparser(e.g. an object withparsemethod) one can use theparserfunction as well :fromvalueparserimportparserfloat_parser=parser(float)assertfloat_parser.parse("1.234")==1.234force_int_parser=parser((float,int))# parse to float then intassertforce_int_parser.parse("1.234")==1Actually theparserfunction accepts :A Parser Class iddentified as a class with theparsemethodA callableAn instance of a Parser Classan mix inside an iterablePlus any kwargs accepted by the combination of parsersBuiltin Parsersclass namekwargscommentBoundedmin=-inf, max=+infraise an error if value outside interval else return valueClippedmin=-inf, max=+infclip silently the value to inferior and superior boundsRoundedndigits=0round the numerical value to ndigitsFormatedformat="%s"convert to string with the given formatListeditems=[], default_item(optional)raise error if value not in items list else return value adefault_item can be set to be returned instead of error raisedEnumeratedenumeratorreturn enumerator(value) or raise errorCreate a custom parserTo create a parser one need to create a new class from BaseParser, declare any configurable argument
inside the child classConfigand define the static or classmethod__parse__For instance a parser adding some noise to a value ( can be usefull for e.i. a simulator)fromvalueparserimportBaseParserimportrandomclassNoisier(BaseParser):classConfig:noise_scale=1.0@staticmethoddef__parse__(value,config):returnvalue+(random.random()-0.5)*config.noise_scaleUsage :noisier=Noisier(noise_scale=100)x=noisier.parse(0.0)x36.700125482238036Or use conparser in pydantic Model:fromvalueparserimportconparserfrompydanticimportBaseModelclassMyData(BaseModel):x:conparser(Noisier,noise_scale=100)y:conparser(Noisier,noise_scale=100)my_data=MyData(x=0,y=0)my_dataMyData(x=32.819723479459284,y=-25.95893228872207)Parser and SystemyParsers are beased on :class:systemy.Systemclass. One can include a parser factory in
a systemy class and expose the parser configuration to user.fromvalueparserimportParserFactory,BoundedfromsystemyimportBaseSystemfrompydanticimportAnyUrldummy=lambdax:xclassNode(BaseSystem):classConfig:url:AnyUrl="http://localhost:4840"parser:ParserFactory=ParserFactory(type=dummy)defset(self,value):value=self.parser.parse(value)# set value on serverreturnvaluenode=Node(parser={'type':(float,Bounded),'min':0,'max':10})