Reputation: 655
I'm rather new to Python, but have grown to like it. I am starting our first Python project and am doing some prototyping. The "Python philosophy" confuses me in terms of typing and exceptions. Can someone please shoot at this excerpt? Am I over engineering or missing some fundamental Python methodology?
class URIPartError(Exception):
pass
class WCFClient(object):
def __init__(self, host, scheme='http', port=80, path='/', user=None, password=None):
super(WCFClient, self).__init__()
#Store our variables
try:
self.__host = str(host).lower()
self.__scheme = str(scheme).lower()
self.__port = int(port)
self.__path = str(path)
self.__user = str(user) if user else None
self.__password = str(password) if password else None
except (TypeError, ValueError), e:
raise URIPartError('Invalid URI part')
#Are our inputs valid?
if not self.__scheme == 'http' and not self.__scheme == 'https':
raise URIPartError('Invalid URI scheme')
if not path.startswith('/') or not path.endswith('/'):
raise URIPartError('Invalid URI path')
#Generate valid URI for baseurl
if (self.__scheme == 'http' and self.__port == 80) or (self.__scheme == 'https' and self.__port == 443):
self.__baseurl = '{0}://{1}{2}'.format(self.__scheme, self.__host, self.__path)
else:
self.__baseurl = '{0}://{1}:{2}{3}'.format(self.__scheme, self.__host, self.__port, self.__path)
def baseurl(self):
return self.__baseurl
Thanks!
Upvotes: 0
Views: 326
Reputation: 599788
There's nothing wrong here in terms of typing. You're not insisting on types, you're doing exactly the right thing by checking values - if a user passes invalid parameters, it's perfectly valid to raise exceptions.
The only comment I would make here is that it's very unPythonic to use "private" double-underscore variables together with getters. Rather than setting self.__baseurl
and then providing a baseurl()
method, just set self.baseurl
directly.
Upvotes: 2
Reputation:
Seeing as how your WCFClient
inherits from object
(which btw only makes sense if you are using Python 2.x, since in Python 3 this is default), it does not really make sense to call the constructor of the object
class. This means that you can remove the line
super(WCFClient, self).__init__()
For the rest it is all ok :)
Upvotes: 0