7770个NumPy分级练习:用Python一举搞定机器学习矩阵运算

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

hessian的python客户端hessian的python客户端博客分类:python今天简单看了一下hessian的客户端,全部代码就480行,非常简单啊,完全可以模仿此代码写各种http客户端。最核心的代码:Python代码request=HessianWriter().write_call(method,params)importhttplibh=httplib.HTTP(self._host)h.putrequest(POST,self._uri)#requiredbyHTTP/1.1h.putheader(Host,self._host)h.putheader(User-Agent,hessianlib.py/%s%__version__)h.putheader(Content-Length,str(len(request)))h.endheaders()h.send(request)errcode,errmsg,headers=h.getreply()iferrcode!=200:raiseProtocolError(self._url,errcode,errmsg,headers)returnself.parse_response(h.getfile())全部:Python代码##AHessianclientinterfaceforPython.Thedateandlongtypesrequire#Python2.2orlater.##TheHessianproxyisusedasfollows:##proxy=Hessian()##printproxy.hello()##--------------------------------------------------------------------##TheApacheSoftwareLicense,Version1.1##Copyright(c)2001-2002CauchoTechnology,Inc.Allrightsreserved.##Redistributionanduseinsourceandbinaryforms,withorwithout#modification,arepermittedprovidedthatthefollowingconditions#aremet:##1.Redistributionsofsourcecodemustretaintheabovecopyright#notice,thislistofconditionsandthefollowingdisclaimer.##2.Redistributionsinbinaryformmustreproducetheabovecopyright#notice,thislistofconditionsandthefollowingdisclaimerin#thedocumentationand/orothermaterialsprovidedwiththe#distribution.##3.Theend-userdocumentationincludedwiththeredistribution,if#any,mustincludethefollowingacknowlegement:#Thisproductincludessoftwaredevelopedbythe#CauchoTechnology().#Alternately,thisacknowlegementmayappearinthesoftwareitself,#ifandwhereversuchthird-partyacknowlegementsnormallyappear.##4.ThenamesHessian,Resin,andCauchomustnotbeusedto#endorseorpromoteproductsderivedfromthissoftwarewithoutprior#writtenpermission.Forwrittenpermission,pleasecontact#info@caucho.com.##5.ProductsderivedfromthissoftwaremaynotbecalledResin#normayResinappearintheirnameswithoutpriorwritten#permissionofCauchoTechnology.##THISSOFTWAREISPROVIDED``ASIS''ANDANYEXPRESSEDORIMPLIED#WARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIES#OFMERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEARE#DISCLAIMED.INNOEVENTSHALLCAUCHOTECHNOLOGYORITSCONTRIBUTORS#BELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,SPECIAL,EXEMPLARY,#ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENT#OFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;OR#BUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,#WHETHERINCONTRACT,STRICTLIABILITY,ORTORT(INCLUDINGNEGLIGENCE#OROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHISSOFTWARE,EVEN#IFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.#--------------------------------------------------------------------##Credits:hessianlib.pywasinspiredandpartiallybasedon#xmlrpclib.pycreatedbyFredrikLundhat*fromstructimportunpackfromstructimportpack__version__=0.1#--------------------------------------------------------------------#ExceptionsclassError:#baseclassforclienterrorspassclassProtocolError(Error):#RepresentsanHTTPprotocolerrordef__init__(self,url,code,message,headers):self.url=urlself.code=codeself.message=messageself.headers=headersdef__repr__(self):return(<ProtocolErrorfor%s:%s%s>%(self.url,self.code,self.message))classFault(Error):#RepresentsafaultfromHessiandef__init__(self,code,message,**detail):self.code=codeself.message=messagedef__repr__(self):return<HessianFault%s:%s>%(self.code,self.message)#--------------------------------------------------------------------#WrappersforHessiandatatypesnon-standardinPython###Boolean--usetheTrueorFalseconstants#classBoolean:def__init__(self,value=0):self.value=(value!=0)def_hessian_write(self,out):ifself.value:out.write('T')else:out.write('F')def__repr__(self):ifself.value:return<Trueat%x>%id(self)else:return<Falseat%x>%id(self)def__int__(self):returnself.valuedef__nonzero__(self):returnself.valueTrue,False=Boolean(1),Boolean(0)##Date-wrapsatimevalueinseconds#classDate:def__init__(self,value=0):self.value=valuedef__repr__(self):return(<Date%sat%x>%(time.asctime(time.localtime(self.value)),id(self)))def_hessian_write(self,out):out.write(d)out.write(pack(>q,self.value*1000.0))##Binary-binarydata#classBinary:def__init__(self,data=None):self.data=datadef_hessian_write(self,out):out.write('B')out.write(pack('>H',len(self.data)))out.write(self.data)#--------------------------------------------------------------------#Marshallingandunmarshallingcode##HessianWriter-writesHessiandatafromPythonobjects#classHessianWriter:dispatch={}defwrite_call(self,method,params):self.refs={}self.ref=0self.__out=[]self.write=write=self.__out.appendwrite(c\x01\x00m);write(pack(>H,len(method)));write(method);forvinparams:self.write_object(v)write(z);result=string.join(self.__out,)delself.__out,self.write,self.refsreturnresultdefwrite_object(self,value):try:f=self.dispatch[type(value)]exceptKeyError:raiseTypeError,cannotwrite%sobjects%type(value)else:f(self,value)defwrite_int(self,value):self.write('I')self.wri

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功