Package: dataclass-builder
Create instances of Python dataclasses with the builder pattern.RequirementsPython 3.6 or greaterdataclassesif using Python 3.6Installationdataclass-builderis onPyPIso the easiest way to install it is:$ pip install dataclass-builderUsageThere are two ways to usedataclass-builder.  Via a builder instance or by creating a dedicated builder.  The latter is recommended when repeated building of a given dataclass is desired or when docstrings and type checking are important.Dedicated Builder (builder factory)Using specialized builders allows for better documentation than theDataclassBuilderwrapper.fromdataclassesimportdataclassfromdataclass_builderimport(dataclass_builder,build,fields,updateREQUIRED,OPTIONAL)@dataclassclassPoint:x:floaty:floatw:float=1.0PointBuilder=dataclass_builder(Point)Now we can build a point.>>>builder=PointBuilder()>>>builder.x=5.8>>>builder.y=8.1>>>builder.w=2.0>>>build(builder)Point(x=5.8,y=8.1,w=2.0)As long as thedataclassthe builder was constructed for does not have abuildfield then abuildmethod will be generated as well.>>> builder.build()
Point(x=5.8, y=8.1, w=2.0)Field values can also be provided in the constructor.>>>builder=PointBuilder(x=5.8,w=100)>>>builder.y=8.1>>>builder.build()Point(x=5.8,y=8.1,w=100)Positional arguments are not allowed.Fields with default values in thedataclassare optional in the builder.>>>builder=PointBuilder()>>>builder.x=5.8>>>builder.y=8.1>>>builder.build()Point(x=5.8,y=8.1,w=1.0)Fields that don’t have default values in thedataclassare not optional.>>>builder=PointBuilder()>>>builder.y=8.1>>>builder.build()Traceback(mostrecentcalllast):...MissingFieldError:field'x'ofdataclass'Point'isnotoptionalFields not defined in the dataclass cannot be set in the builder.>>>builder.z=3.0Traceback(mostrecentcalllast):...UndefinedFieldError:dataclass'Point'doesnotdefinefield'z'No exception will be raised for fields beginning with an underscore as they are reserved for use by subclasses.Accessing a field of the builder before it is set gives either theREQUIREDorOPTIONALconstant>>>builder=PointBuilder()>>>builder.xREQUIRED>>>builder.wOPTIONALThefieldsmethod can be used to retrieve a dictionary of settable fields for the builder.  This is a mapping of field names todataclasses.Fieldobjects from which extra data can be retrieved such as the type of the data stored in the field.>>>list(builder.fields().keys())['x','y','w']>>>[f.type.__name__forfinbuilder.fields().values()]['float','float','float']A subset of the fields can be also be retrieved, for instance, to only get required fields:>>>list(builder.fields(optional=False).keys())['x','y']or only the optional fields.>>>list(builder.fields(required=False).keys())['w']If the underlyingdataclasshas a field namedfieldsthis method will not be generated and instead thefieldsfunction should be used instead.An already builtdataclasscan be updated with a partially completed builder using theupdatefunction.>>>point=Point(x=5.8,y=8.1,w=100)>>>update(point,PointBuilder(y=1.1))>>>pointPoint(x=5.8,y=1.1,w=100)Dataclass builders can also be updated, but frozen dataclasses cannot.Builder Instance (generic wrapper)Using a builder instance is the fastest way to get started with thedataclass-builderpackage.fromdataclassesimportdataclassfromdataclass_builderimport(DataclassBuilder,build,fields,REQUIRED,OPTIONAL)@dataclassclassPoint:x:floaty:floatw:float=1.0Now we can build a point.>>>builder=DataclassBuilder(Point)>>>builder.x=5.8>>>builder.y=8.1>>>builder.w=2.0>>>build(builder)Point(x=5.8,y=8.1,w=2.0)Field values can also be provided in the constructor.>>>builder=DataclassBuilder(Point,x=5.8,w=100)>>>builder.y=8.1>>>build(builder)Point(x=5.8,y=8.1,w=100)NotePositional arguments are not allowed, except for thedataclassitself.Fields with default values in thedataclassare optional in the builder.>>>builder=DataclassBuilder(Point)>>>builder.x=5.8>>>builder.y=8.1>>>build(builder)Point(x=5.8,y=8.1,w=1.0)Fields that don’t have default values in thedataclassare not optional.>>>builder=DataclassBuilder(Point)>>>builder.y=8.1>>>build(builder)Traceback(mostrecentcalllast):...MissingFieldError:field'x'ofdataclass'Point'isnotoptionalFields not defined in the dataclass cannot be set in the builder.>>>builder.z=3.0Traceback(mostrecentcalllast):...UndefinedFieldError:dataclass'Point'doesnotdefinefield'z'NoteNo exception will be raised for fields beginning with an underscore as they are reserved for use by subclasses.Accessing a field of the builder before it is set gives either theREQUIREDorOPTIONALconstant>>>builder=DataclassBuilder(Point)>>>builder.xREQUIRED>>>builder.wOPTIONALThefieldsfunction can be used to retrieve a dictionary of settable fields for the builder.  This is a mapping of field names todataclasses.Fieldobjects from which extra data can be retrieved such as the type of the data stored in the field.>>>list(fields(builder).keys())['x','y','w']>>>[f.type.__name__forfinfields(builder).values()]['float','float','float']A subset of the fields can be also be retrieved, for instance, to only get required fields:>>>list(fields(builder,optional=False).keys())['x','y']or only the optional fields.>>>list(fields(builder,required=False).keys())['w']