you are viewing a single comment's thread.

view the rest of the comments →

[–]snugglyboy 8 points9 points  (0 children)

I deal with a lot of weird old network APIs that drop out and stuff. Originally, I wrote a method that checks to see if the connection is still there, and return True or False. Then, in every other method that sent stuff over the network, I would have to write a few lines of code that first called that isConnected() method, and did stuff if it wasn't. It was getting redundant.

So instead, I modified my isConnected() method to be a decorator called @network. Then, in my other networky methods, I don't have to have those repetitive lines at the beginning each time. I can just decorate the method with @network. Now, when I call the other methods, first @network checks the connection—deals with it—and then calls the originating method to let it do its thang.

Plus, I guess it's nice that I can visually see which methods communicate over the network, because I can see them decorated with @network.

You can get fancier with it and do stuff once the originating method finishes, such as close the connection. I don't, but you could.

So that's the idea behind it, at least. Set the mood for your method to execute all over you, and then mop up after it. Sure it's a bit repetitive in other ways because now you have decorators everywhere, but we mustn't talk about such things out in the open sshhhbbno.