python - Manipulating attributes from a namedtuple() derived class -


i have class called easyurl() derived urlparse.parseresult(). parseresult() instantiated when calling urlparse.urlparse(url), have static method inside easyurl() changes class type of instantiated parseresult() object easyurl() object. wrap urlparse.urlparse() function , class type conversion function parse_url().

the reason behind such function, attempt hack around separate problem don't require answer one, typeerror when __new__ called during instantiating process, lets me know have invalid number of arguments.

error received when instantiating easyurl() directly

# snippet  url = 'stackoverflow.com' url = easyurl(url) # snippet end  output: typeerror: __new__() takes 7 arguments (2 given) 

the parseresult() class inherits namedtuple().

excerpt urlparse library

class parseresult(namedtuple('parseresult', 'scheme netloc path params query fragment'), resultmixin):      __slots__ = ()      def geturl(self):         return urlunparse(self) 

now have described little functionality of code, here problem. can't access named tuple's (parseresult) attributes. i'm trying implement default scheme parseresult() if missing.

but can't access attributes in class definition.

import urlparse   def parse_url(url):     """ return parsed easyurl() object"""     parse_result = urlparse.urlparse(url)     return easyurl.evolveparseresult(parse_result)   class easyurl(urlparse.parseresult):      @staticmethod     def evolveparseresult(parse_result):         """ change type of class easyurl() object."""         parse_result.__class__ = easyurl         easy_url = parse_result # readabilty         easy_url.init()         return easy_url      def __init__(self, url):         self = parse_url(url) # doesn't work      def init(self):         self.url = self.geturl()         #self.set_scheme_if_non() # uncomment when no error raised      def set_scheme_if_non(self, scheme='http'):         if not self.scheme:             self.scheme = scheme             self.url = self.geturl() # rebuild our url new scheme    # passes set_scheme_if_non trigger #url = 'https://stackoverflow.com' # fails if statment, attempts set variable, # error raised: attributeerror: can't set attribute url = 'stackoverflow.com'  # give error: typeerror: __new__() takes 7 arguments (2 given) #url = easyurl(url)  # works fine, don't know why. except can't access # tuples attributes in class definition url = parse_url(url)   print url.scheme # works fine  url.set_scheme_if_non() # raises error 

output

file "/home/crispycret/easyurl.py", line 50, in <module>   url.set_scheme_if_non() # raises error file "/home/crispycret/easyurl.py", line 29, in set_scheme_if_non   self.scheme = scheme  attributeerror: can't set attribute 

why not create new class scratch , transfer attributes parseresult over?

import urlparse  class easyurl(object):     def __init__(self, parse_result):         self.scheme = parse_result.scheme         self.netloc = parse_result.netloc         self.path = parse_result.path         self.params = parse_result.params         self.query = parse_result.query         self.fragment = parse_result.fragment      @classmethod     def from_url(cls, url):         return cls(urlparse.urlparse(url))  if __name__ == '__main__':     url = 'http://foo.bar.com:8888/path/to/script.php?a=1&b=2'      # call from_url class method, convenient     easy_url = easyurl.from_url(url)      # or,     easy_url = easyurl(urlparse.urlparse(url)) 

you can add additional method class desired.

update

  • namedtuple function creates new type on fly. way, our easyurl object not act tuple unless add code __getitem__()
  • parseresult not allow change attributes such scheme, there no reason inherit it.

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -