我对 Python 比较陌生,但已经喜欢它了。我正在开始我们的第一个 Python 项目,并且正在做一些原型设计。“Python 哲学”在类型和异常方面让我感到困惑。有人可以拍摄这段摘录吗?我是过度工程还是缺少一些基本的 Python 方法?
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
谢谢!