(Sorry, the name is pretty obscure. I could not find a good one.)
Say I have a url like this (it is the root):
"/forums/support/windows/help_i_deleted_sys32/6/"
and I'm trying to break it down into a class structure like this:
class Forum_Spot: def __init__(self, url): parts = url.strip("/").split("/") #parts is now ["forums", "support", "windows", "help...", "6"] self.root = "forums" self.section = "support" self.subsection = "windows" self.thread = "help..." self.post = "6"
but I will say that I do not know how long the url will be displayed (it can be "/ forums / support /", "/ forums / support / windows /", etc.) (but I know that it wonโt be deeper than 5 levels). Can anyone think of an elegant way of assigning these values โโwithout letting any part assign None ? (Ie for "/ forums / support / windows /", thread attributes and posts will be None)
I know I can do this:
class Forum_Spot: def __init__(self, url): parts = url.strip("/").split("/") #parts is now ["forums", "support", "windows", "help...", "6"] if len(parts) > 0: self.root = parts[0] else: self.root = None if len(parts) > 1: self.section = parts[1] else: #etc
but it is obviously supernatural and unpleasantly labor intensive. Can anyone think of a more elegant solution while keeping the class signature the same? (I could convert the __init__ function to take the keyword parameters, the default is None , but I would like to be able to just pass the URL and evaluate the class myself)
Thanks!
source share