Reputation: 2545
I’m working on a Python script that accesses number of websites. I don’t think writing a whole class for each website is a feasible option, so I am looking for a way to reduce the code and re-use code as much as I can and write it as efficiently as I can. Although the website share same or similar engine (e.g. Wordpress), they often differ with slight details like cookie names, changed URL paths (e.g. example.com/signin.php and example2.com/login.php), data names and values for POST requests, and so on.
In most cases I could use variables, but sometimes I need to find extra data from retrieved url in the middle of method to continue, so that means adding few lines of code that needs to be processed before proceeding. I found that possible solution for me could theoretically be to use superclasses. But I couldn’t find any information how to actually ‘step into’ a method to make changes. To better illustrate my point, take a look at this example
class Wordpress(object):
def login(self):
# Do some stuff here
def newentry(self):
# Code to check if I'm logged in code
# and do bunch of other stuff
#
# Retrieve a page that *** sometimes contain important information
# in which case must be processed to continue! ***
data = {'name' : title,
'message' : message,
'icon' : iconid}
# Post new entry here
As I already commented in the example above in some cases I need to make adjustement just inside a method itself, add snippet of code, or change value of the variable or value in the dictionary, etc.. How can I achieve that, if it's even possible? Maybe my perception of superclasses is way off and they aren't really for what I think they are.
Maybe there's other way I haven't thought of. Feel free to post your own ideas how you would solve this problem. I am looking forward to your replies. Thank you.
Upvotes: 0
Views: 62
Reputation: 88777
Why can't you just call some process
function from the main method, process can do whatever you want it to do, and you can even override it in derived classes e.g.
class WordPress(object):
def newentry(self):
data = get_data()
data = process_data(data)
# do moe generi cthings
def process_data(self):
return data
Upvotes: 1