3 def __init__(self
, mth
, sort
, args
):
8 def __repr__(self
): return str(self
.mth
)
16 def attach(self
, obs_meth
, sort
=10, rename
='', args
=None):
18 onm
= rename
or obs_meth
.__name
__
19 if onm
not in self
.observers
:
20 self
.observers
[onm
] = []
21 self
.observers
[onm
] += [ObsInfo(obs_meth
, sort
, args
)]
22 sorted_obs
= sorted(self
.observers
[onm
], key
=lambda obs
: obs
.sort
)
23 self
.observers
[onm
] = sorted_obs
25 def detach(self
, obs_meth
, lambda_call
=None):
26 if isinstance(obs_meth
, str):
28 observers
= [obs
for obs
in self
.observers
[onm
]
29 if obs
.mth
== lambda_call
]
31 onm
= obs_meth
.__name
__
32 observers
= [obs
for obs
in self
.observers
[onm
]
33 if obs
.mth
== obs_meth
]
36 list(map(self
.observers
[onm
].remove
, observers
))
38 def notify(self
, meth
, *args
, **kwargs
):
39 if meth
not in self
.observers
:
40 return # no obs for this notification
41 for obs
in self
.observers
[meth
][:]:
42 if obs
in self
.observers
[meth
]: # if an obs removes another one
44 act_args
= obs
.args
+ list(args
)
45 obs
.mth(*act_args
, **kwargs
)
51 def observing(self
, obs_meth
):
52 if callable(obs_meth
):
53 obs_meth
= obs_meth
.__name
__
54 return obs_meth
in self
.observers
and self
.observers
[obs_meth
]
56 def destroy(self
): self
.observers
= None