Package: linked-properties
This is a python library that allows you to link properties together, so
if one is changed the others change accordingly.You can use this library everywhere, I don’t care. Giving credit would
be nice, but is optional.Installing$pipinstalllinked_propertiesUsingThe library consists of two important parts:WatchablePropertyandLinkableProperty.A class namedComplexLinkedPropertyor something like that may be
added in the future, it would combine both concepts; but it would need
additional work. I added the documentation anyways.WatchablePropertyAs the docstring states:WatchableProperty is a class that is used to create a property that can be submitted to a LinkableProperty.

It works exactly the same as "property", except for the name.

If a linked property is reading from another one, than this object may be blocked for writing for the instance in question.
To get around this, you may define an "at_blocked_set" function, that unlocks the linked property by setting it to None. It may return true or false, if it is true, the __set__ function is called another time.

Note: the at_blocked_set function can invoke the __set__ function again only once per tried write access!Youcannotuse normal properties to make LinkableProperties work.Here’s how you can use them (Spoiler Alert: It’s (almost) exactly the same as
with the builtin properties):
* 1st option: Replace the builtin property (kind of). Thisshould generally not be used, although it can be used if you are updating an old project to fit the requirements automatically.importlinked_propertiesproperty=linked_properties.WatchableProperty# ... (Your own code)2nd option: Using them with function decorators. Thisshouldbe the preferred way, but some (or just this one, i don’t know) editors like PyCharm think you are working with the builtin properties. This may lead to warnings that shouldn’t exist.importlinked_properties# ... (Your own code)classSomeName(object):# ... (Your own code)@linked_properties.WatchablePropertydefx(self)->int:returnself._x@x.setterdefx(self,value:int)->None:self._x=int(value)# ... (Your own code)# ... (Your own code)3rd option: Using them the ugly way.
This is still better than the first one,
and it’s the only one to make PyCharm stop spawning warnings because of types all over the place.importlinked_properties# ... (Your own code)classSomeName(object):# ... (Your own code)# ... (Your own code)def_get_x(self)->int:returnself._xdef_set_x(self,value:int)->None:self._x=valuex=linked_properties.WatchableProperty(_get_x,_set_x)# ... (Your own code)# ... (Your own code)But be aware of this: Two things is different.While@property_name.setterreturns a new instancefor a builtin property because they areimmutable, a WatchablePropertyreturns itself and changes accordinglybecause it ismutable.Just try to avoid situations where you need mutable or immutable descriptors, because this behaviour can change in future versions. (I can’t even think of a situation where this would help)And youcannotdefine a deleter, although that may change in the future as well.LinkablePropertyAs the docstring states:The main functionality[.] [...]You cannot define any getter, setter or deleter. The init function takes aWatchableProperty.The definition is simple, and the usage is as well:If you have two LinkedProperties, you can set one to be the other (obj1.l_x = obj2.l_x). This results in following behaviour:You can’t set the associatedWatchablePropertyof the receivingLinkablePropertyanymore (at least not byobj1.x = ...), and if the associatedWatchablePropertyof the sendingLinkablePropertygets changed, the setter of the otherWatchablePropertygets called.If you want to unlink them, set the receivingLinkablePropertytoNone. (obj1.l_x = None)Be aware of this:You shouldonlyuse LinkedProperties if your propertymakes sense to be able to get linked. This excludes properties that count how often something was written to or read, and everything thatchanges behaviour after writing the same value or reading twice.LinkableTransmissionThis is the object the getter of aLinkablePropertyreturns, to be able to connect with other LinkedProperties.You don’t have to care about this one, if you don’t want to change how the library works.
I put this in anyway, because this will be expanded in the next version; allowing you to interact with it like you would interact with the underlying value.
(->obj1.l_x = obj2.l_x * 2)If theComplexLinkablePropertyis implemented, usingtransmission.valuemay be important to let it be used by other libraries.
Using this technique is also important if you want to set the new value only once, and don’t want those two properties to be linked.ComplexLinkableProperty*This is not implemented yet.*This LinkableProperty will be aLinkableProperty, with a getter and setter that you define,
and you can assign values to this properties, and not just transmissions.
If you want to be able to set the property to a transmission, you have to use the current way of dealing with LinkedProperties.This returns a LinkableTransmission if you are receiving, with atransmission.valuegathered by the getter. If you are trying to set the attribute, one of two things will happen:If the value is aLinkableTransmissionor a subclass: The property gets linked by the transmission.If it isn’t: Run the setter with the new value.The setter will also run when the value of the linked property changes, or if they are linked.ExampleI personally can learn better from examples. So here is one:importlinked_propertiesclassWidget(object):def__init__(self,width:int,height:int)->None:self._width=0# type: intself._height=0# type: intself.width=widthself.height=height@linked_property.WatchablePropertydefwidth(self)->int:returnself._width@width.setterdefwidth(self,new_value:int)->None:assertisinstance(new_value,int)self._width=new_valuel_width=linked_properties.LinkableProperty(width)# ...  (height; see width)Or after theComplexLinkablePropertyarrived:importlinked_propertiesclassWidget(object):def__init__(self,width:int,height:int)-None:self._width=0# type: intself._height=0# type: intself.width=widthself.height=height@linked_property.ComplexLinkablePropertydefwidth(self)->int:returnself._width@width.setterdefwidth(self,new_value:int)->None:assertisinstance(new_value,int)self._width=new_valueContributingIf you want to contribute, just email me:dodo2998@gmail.com