Skip to content

Commit 4588859

Browse files
JuanCJuanC
authored andcommitted
Changing event method identifiers
1 parent d34d58b commit 4588859

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ For objective 2, the module was architected thinking in how another EOP language
7272

7373
2. delegates are not commonly exposed publicly, for security reasons. Just as fields/attributes in a class have to be encapsulated, so do delegates. The way to encapsulate them is with events. Fields/attributes are to properties as delegates are to events.
7474

75-
3. Properties encapsulate fields/attributes using two methods: "get" and "set", which define the logic of how data should be GET and SET out of the object, in C# events encapsulate delegates with 2 methods as well called "add" and "remove", which define the logic of how functions/subscribers should be added or removed out of the delegate.
75+
3. Properties encapsulate fields/attributes using two methods: "getter" and "setter", which define the logic of how data should be GET and SET out of the object, in C# events encapsulate delegates with 2 methods as well called "adder" and "remover", which define the logic of how functions/subscribers should be added or removed out of the delegate.
7676

7777

7878
## Installation
@@ -376,7 +376,7 @@ Below this text, the use cases and explanation about the events are shown, pleas
376376
def name_changed(self,value)->None: # This example doesn't contain the parameter annotations for simplicity because it is the first example, however (as the document will explain in the summary of simple events), it is really important to place the event annotations. (There is a link at the end of this code block to go to the explanation)
377377
self._name_changed += value
378378

379-
@name_changed.remove
379+
@name_changed.remover
380380
def name_changed(self,value)->None: # Annotations are not included for simplicity because it is the first example
381381
self._name_changed -= value
382382

@@ -408,21 +408,21 @@ To implement a *simple event* the first thing you have to do is create a variabl
408408
self._name_changed = Delegate() # it can be viewed as a "To do list"
409409
```
410410

411-
As you might notice the variable that is going to store the subscribers is a Delegate and the name starts with '_' to "protect" the attribute. Expose the attribute "publicly" is not a good practice, because other parts of the code can manipulate the attribute wrongly or get/set information in a way that was not mean to. To fix this, we can define 2 methods to encapsulate the delegate (add/remove methods), Through these 2 methods the other objects in the code can subscribe/unsubscribe (add/remove) callables to our delegate.
411+
As you might notice the variable that is going to store the subscribers is a Delegate and the name starts with '_' to "protect" the attribute. Expose the attribute "publicly" is not a good practice, because other parts of the code can manipulate the attribute wrongly or get/set information in a way that was not mean to. To fix this, we can define 2 methods to encapsulate the delegate (adder/remover methods), Through these 2 methods the other objects in the code can subscribe/unsubscribe (add/remove) callables to our delegate.
412412

413413
```Python
414414
@event
415415
def name_changed(self,value)->None:
416416
self._name_changed += value # add the new callable to the attribute with a delegate
417417

418-
@name_changed.remove
418+
@name_changed.remover
419419
def name_changed(self,value)->None:
420420
self._name_changed -= value # remove the callable to the attribute with a delegate
421421
```
422422

423-
Code above implements add/remove logic to the delegate. Function below *@event* decorator defines the logic for the *add* or how a callable should be added to our "To do list". Function below *@name_changed.remove* defines the logic for the *remove* or how a callable should be removed from the delegate
423+
Code above implements add/remove logic to the delegate. Function below *@event* decorator defines the logic for the *add* or how a callable should be added to our "To do list". Function below *@name_changed.remover* defines the logic for the *remover* or how a callable should be removed from the delegate
424424

425-
Notice the functions HAVE to be named exactly with the same name, and if an *@event* is defined you **must** implement *@IDENTIFIER.remove* or the code will throw a traceback, this is to protect the integrity of the code and provide instructions about how to add AND remove a callable.
425+
Notice the functions HAVE to be named exactly with the same name, and if an *@event* is defined you **must** implement *@IDENTIFIER.remover* or the code will throw a traceback, this is to protect the integrity of the code and provide instructions about how to add AND remove a callable.
426426

427427
The callable to be added/removed will be passed through the "value" parameter. Notice in this example "value" parameter doesn't have any type annotation, this is only to keep this first example "simple/readable" at first sight, however it is **HIGHLY RECOMMENDED** annotate the type as the following examples on this document (Events with arguments or Events with modifiable arguments examples contain these annnotations), due this is the way to indicate clearly what is the signature expected from the event to their subscribers (callables). [Link to event annotation convention explanation](#event-annotation-convention)
428428

@@ -499,7 +499,7 @@ The next snipped code shows and example of how the *simple events* should be imp
499499
def name_changed(self,value:Callable[[object, EventArgs], None])->None:
500500
self._name_changed += value
501501

502-
@name_changed.remove
502+
@name_changed.remover
503503
def name_changed(self,value:Callable[[object, EventArgs], None])->None:
504504
self._name_changed -= value
505505
```
@@ -516,9 +516,9 @@ def person_name_changed(sender:object,e:EventArgs)->None: #function to be execut
516516
print("person change its name to %s" % sender.name)
517517

518518
person = Person("Juan") #creates a person
519-
person.name_changed += person_name_changed # we add 'person_name_changed' (subscriber) to event name_changed of 'person', this line will execute function under @event decorator (add function)
519+
person.name_changed += person_name_changed # we add 'person_name_changed' (subscriber) to event name_changed of 'person', this line will execute function under @event decorator (adder function)
520520
person.name = "Carlos" # change the name to trigger the event (this will execute 'person_name_changed')
521-
person.name_changed -= person_name_changed #unsubcribe the function, this line will execute function under @name_changed.remove decorator (remove function)
521+
person.name_changed -= person_name_changed #unsubcribe the function, this line will execute function under @name_changed.remover decorator (remover function)
522522
person.name = "Something" # change the name again to prove 'person_name_changed' is not executed anymore
523523
```
524524

@@ -568,7 +568,7 @@ person.name = "Something" # change the name again to prove 'person_name_changed'
568568
def moved(self,value:Callable[[object, MovedEventArgs], None])->None:
569569
self._moved += value
570570

571-
@moved.remove
571+
@moved.remover
572572
def moved(self,value:Callable[[object, MovedEventArgs], None])->None:
573573
self._moved -= value
574574

@@ -616,7 +616,7 @@ In the next code block we can see how the event is being defined:
616616
def moved(self,value:Callable[[object, MovedEventArgs], None])->None:
617617
self._moved += value
618618

619-
@moved.remove
619+
@moved.remover
620620
def moved(self,value:Callable[[object, MovedEventArgs], None])->None:
621621
self._moved -= value
622622
```
@@ -764,7 +764,7 @@ Second difference is when *location* settter is calling *\_on\_moved* method, no
764764
def location_changing(self,value:Callable[[object, LocationChangingEventArgs], None])->None:
765765
self._location_changing += value
766766

767-
@location_changing.remove
767+
@location_changing.remover
768768
def location_changing(self,value:Callable[[object, LocationChangingEventArgs], None])->None:
769769
self._location_changing -= value
770770

@@ -903,7 +903,7 @@ class Person:
903903
def person_created(value:Callable[[object, EventArgs], None])->None:
904904
Person._person_created += value
905905

906-
@person_created.remove
906+
@person_created.remover
907907
def person_created(value:Callable[[object, EventArgs], None])->None:
908908
Person._person_created -= value
909909

python_sharp.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ def __init__(
151151
self._fadd = None
152152
self._fremove = None
153153

154-
self.add(fadd)
155-
self.remove(fremove)
154+
self.adder(fadd)
155+
self.remover(fremove)
156156

157157
self._proxy = None
158158

159159

160-
def add(self,fadd:Callable[[Callable[[object,EventArgs], None]], None] | None)->"BaseEvent":
160+
def adder(self,fadd:Callable[[Callable[[object,EventArgs], None]], None] | None)->"BaseEvent":
161161
"""
162162
Allows provide the function will be use to add a callable.
163163
@@ -167,12 +167,12 @@ def add(self,fadd:Callable[[Callable[[object,EventArgs], None]], None] | None)->
167167
BaseEvent: Current instance with the new fadd function
168168
"""
169169
if fadd is not None and self._fremove is not None and self._fremove.__name__ != fadd.__name__:
170-
raise AttributeError("add function '%s'and remove function '%s' name missmatch, both members must have same name" % (fadd.__name__,self._fremove.__name__))
170+
raise AttributeError("adder function '%s'and remover function '%s' name missmatch, both members must have same name" % (fadd.__name__,self._fremove.__name__))
171171

172172
self._fadd = fadd
173173
return self
174174

175-
def remove(self,fremove:Callable[[Callable[[object,EventArgs], None]], None] | None)->"BaseEvent":
175+
def remover(self,fremove:Callable[[Callable[[object,EventArgs], None]], None] | None)->"BaseEvent":
176176
"""
177177
Allows provide the function will be use to remove a callable.
178178
@@ -182,7 +182,7 @@ def remove(self,fremove:Callable[[Callable[[object,EventArgs], None]], None] | N
182182
BaseEvent: Current instance with the new fremove function
183183
"""
184184
if fremove is not None and self._fadd is not None and self._fadd.__name__ != fremove.__name__:
185-
raise AttributeError("remove function '%s'and add function '%s' name missmatch, both members must have same name" % (fremove.__name__,self._fadd.__name__))
185+
raise AttributeError("remover function '%s'and adder function '%s' name missmatch, both members must have same name" % (fremove.__name__,self._fadd.__name__))
186186

187187
self._fremove = fremove
188188
return self
@@ -219,9 +219,9 @@ def __get__(self, instance:Any, owner:type)->Any:
219219
error_message = ""
220220

221221
if self._fadd is None and self._fremove is None:
222-
error_message = "event in %s is not defining any function (add/remove)" % owner
222+
error_message = "event in %s is not defining any function (adder/remover)" % owner
223223
else:
224-
function_info= (self._fremove,"add") if self._fadd is None else (self._fadd,"remove")
224+
function_info= (self._fremove,"adder") if self._fadd is None else (self._fadd,"remover")
225225
error_message = "event %s does not have '%s' function assigned in %s" % (function_info[0].__name__,function_info[1],owner)
226226

227227
raise NotImplementedError(error_message)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="python_sharp", # Unique name of your package on PyPI
5-
version="1.1.3", # Version of the package, following semantic versioning
5+
version="1.2.0", # Version of the package, following semantic versioning
66
author="Juan Carlos Lopez Garcia", # Author's name
77
author_email="[email protected]", # Contact email
88
description="python# (python sharp) is a module created to add EOP (event oriented programing) into python in the most native feeling, easy sintax way possible. Based on C# event implementation structure", # Short description

test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def kill(self)->None:
130130
def name_changed(self,value: Callable[[object, EventArgs], None])->None:
131131
self._name_changed += value
132132

133-
@name_changed.remove
133+
@name_changed.remover
134134
def name_changed(self,value: Callable[[object, EventArgs], None])->None:
135135
self._name_changed -= value
136136

@@ -139,7 +139,7 @@ def name_changed(self,value: Callable[[object, EventArgs], None])->None:
139139
def moved(self,value:Callable[[object, MovedEventArgs], None])->None:
140140
self._moved += value
141141

142-
@moved.remove
142+
@moved.remover
143143
def moved(self,value:Callable[[object, MovedEventArgs], None])->None:
144144
self._moved -= value
145145

@@ -148,7 +148,7 @@ def moved(self,value:Callable[[object, MovedEventArgs], None])->None:
148148
def location_changing(self,value:Callable[[object, LocationChangingEventArgs], None])->None:
149149
self._location_changing += value
150150

151-
@location_changing.remove
151+
@location_changing.remover
152152
def location_changing(self,value:Callable[[object, LocationChangingEventArgs], None])->None:
153153
self._location_changing -= value
154154

@@ -157,7 +157,7 @@ def location_changing(self,value:Callable[[object, LocationChangingEventArgs], N
157157
def died(self,value:Callable[[object, EventArgs], None])->None:
158158
self._died += value
159159

160-
@died.remove
160+
@died.remover
161161
def died(self,value:Callable[[object, EventArgs], None])->None:
162162
self._died -= value
163163

@@ -166,7 +166,7 @@ def died(self,value:Callable[[object, EventArgs], None])->None:
166166
def person_created(value:Callable[[object, EventArgs], None])->None:
167167
Person._person_created += value
168168

169-
@person_created.remove
169+
@person_created.remover
170170
def person_created(value:Callable[[object, EventArgs], None])->None:
171171
Person._person_created -= value
172172

0 commit comments

Comments
 (0)