<div dir="ltr"><div class="gmail_default" style="font-family:courier new,monospace">Peter, Spir - thanks for your time and effort! </div><div class="gmail_default" style="font-family:courier new,monospace"><br></div><div class="gmail_default" style="font-family:courier new,monospace">
I am posting this query to few more Python mailers.</div><div class="gmail_default" style="font-family:courier new,monospace"><br></div><div class="gmail_default" style="font-family:courier new,monospace">Thank you,</div>
<div class="gmail_default" style="font-family:courier new,monospace"><br></div><div class="gmail_default" style="font-family:courier new,monospace">Sangeeth</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Tue, Feb 25, 2014 at 5:22 AM, spir <span dir="ltr"><<a href="mailto:denis.spir@gmail.com" target="_blank">denis.spir@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="">On 02/24/2014 08:19 PM, Sangeeth Saravanaraj wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
Sorry, I should have described what I was trying!<br>
<br>
I want to create a decorator which should do the following things:<br>
<br></div>
- When an object of the decorated class is created, the objects name<div class=""><br>
(say the value of the incoming "id" argument) should be stored as a record<br>
in a table in a database.<br></div>
- When an object of the decorated class is deleted, the record with this<div><div class="h5"><br>
deleted objects name (i.e. <a href="http://object.id" target="_blank">object.id</a>) should be removed from the table.<br>
<br>
You can safely assume that all the database operations are working fine!<br>
<br>
Now, for example - consider the following snippet:<br>
<br>
@saveme<br>
class A(object):<br>
def __init__(self, id):<br>
<a href="http://self.id" target="_blank">self.id</a> = id<br>
<br>
@saveme<br>
class B(object):<br>
def __init__(self, id):<br>
<a href="http://self.id" target="_blank">self.id</a> = id<br>
<br>
"saveme" should do what I have explained earlier.<br>
<br>
a1 = A("A1")<br>
a2 = A("A2")<br>
a3 = A("A3")<br>
b1 = B("B1")<br>
b2 = B("B2")<br>
<br>
At this point if I query and print all the records in a table, I should get<br>
the following:<br>
output: ["A1", "A2", "A3", "B1", "B2"]<br>
<br>
del a1<br>
del a2<br>
del a3<br>
del b1<br>
del b2<br>
<br>
At this point, all entries in the table should be deleted; query should<br>
return an empty list!<br>
<br>
And, I want to highlight that the classes that are being decorated with<br>
"saveme" can de derived classes too!<br>
<br>
What is the best way to do this?!<br>
<br>
Thank you,<br>
<br>
Sangeeth<br>
</div></div></blockquote>
<br>
Your problem looks like a typical "crosscutting" (transversal) concern addressed by AOP (Aspect Oriented Programming). Their usual example is in fact logging. Look at the wikipedia page:<br>
<a href="https://en.wikipedia.org/wiki/Aspect-oriented_programming" target="_blank">https://en.wikipedia.org/wiki/<u></u>Aspect-oriented_programming</a><br>
<br>
Not that it would help you solve it _in python_, but this may serve at least to better understand what kind of problem you are actually facing; and why it is annoying in programming (with common languages); what may be your options.<br>
<br>
[I have no better approach than yours, using magic metamethods, and a decorator to wrap it all.]<div class="HOEnZb"><div class="h5"><br>
<br>
d<br>
______________________________<u></u>_________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="https://mail.python.org/mailman/listinfo/tutor" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div>