Best Python code snippet using hypothesis
2019-8-13-Property.py
Source:2019-8-13-Property.py
...14 def __get__(self,instance,owner):15 print('getting...',self,instance,owner)16 def __set__(self,instance,value):17 print('setting...',self,instance,value)18 def __delete__(self,instance):19 print('deleting...',self,instance)2021 22>>> class Test:23 x = MyDecriptor()2425 26>>> ##æ述符,MyDecriptorå°±æ¯æ述符类27>>> 28>>> test = Test()29>>> test.x30getting... <__main__.MyDecriptor object at 0x000002130ACF4548> <__main__.Test object at 0x000002130ACE2B08> <class '__main__.Test'>31>>> test32<__main__.Test object at 0x000002130ACE2B08>33>>> Test34<class '__main__.Test'>35>>> test.x = 'X_man'36setting... <__main__.MyDecriptor object at 0x000002130ACF4548> <__main__.Test object at 0x000002130ACE2B08> X_man37>>> del test.x38deleting... <__main__.MyDecriptor object at 0x000002130ACF4548> <__main__.Test object at 0x000002130ACE2B08>39>>> 40=============================== RESTART: Shell ===============================41>>> class NyProperty:42 def __init__(self,fget = None,fset = None,fdel = None):43 self.fget = fget44 self.fset = fset45 self.fdel = fdel46 def __get__(self,instance,owner):47 return self.fget(instance)48 def __set__(self,instance,value):49 return self.fset(instance,value)50 def __del__(self,instance):51 self.fdel(instance)5253 54>>> 55>>> class C:56 def __init__(self):57 self._x = None58 def getX(self):59 return self._x60 def setX(self,value):61 self._x = value62 def delX(self):63 del self.x64 x = MyProperty(getX,setX,delX)6566 67Traceback (most recent call last):68 File "<pyshell#44>", line 1, in <module>69 class C:70 File "<pyshell#44>", line 10, in C71 x = MyProperty(getX,setX,delX)72NameError: name 'MyProperty' is not defined73>>> class MyProperty:74 def __init__(self,fget = None,fset = None,fdel = None):75 self.fget = fget76 self.fset = fset77 self.fdel = fdel78 def __get__(self,instance,owner):79 return self.fget(instance)80 def __set__(self,instance,value):81 return self.fset(instance,value)82 def __del__(self,instance):83 self.fdel(instance)8485 86>>> class C:87 def __init__(self):88 self._x = None89 def getX(self):90 return self._x91 def setX(self,value):92 self._x = value93 def delX(self):94 del self.x95 x = MyProperty(getX,setX,delX)9697 98>>> c = C()99>>> c.x = 'x-man'100>>> c.x101'x-man'102>>> c._x103'x-man'104>>> del c.x105Traceback (most recent call last):106 File "<pyshell#53>", line 1, in <module>107 del c.x108AttributeError: __delete__109>>> class MyProperty:110 def __init__(self,fget = None,fset = None,fdel = None):111 self.fget = fget112 self.fset = fset113 self.fdel = fdel114 def __get__(self,instance,owner):115 return self.fget(instance)116 def __set__(self,instance,value):117 return self.fset(instance,value)118 def __delete__(self,instance):119 self.fdel(instance)120121 122>>> class C:123 def __init__(self):124 self._x = None125 def getX(self):126 return self._x127 def setX(self,value):128 self._x = value129 def delX(self):130 del self.x131 x = MyProperty(getX,setX,delX)132
...
descriptor.py
Source:descriptor.py
...21 print u"РаÑÑибÑÑ", self, u"Ñ
оÑÑÑ Ð·Ð°Ð¿Ð¸ÑаÑÑ Ð·Ð½Ð°Ñение", value,22 print u"ÑеÑез ÑкземплÑÑ", instance23 print u"ÐÑÐ¾Ñ Ð¼ÐµÑод Ð¼Ð¾Ð¶ÐµÑ ÑоÑ
ÑаниÑÑ Ð½Ð¾Ð²Ð¾Ðµ знаÑение"24 # ÐÑзÑваеÑÑÑ Ð¿Ñи Ñдалении "del instance.аÑÑибÑÑ"25 def __delete__(self, instance):26 print u"ÐÑÑибÑÑ", self, u"Ñ
оÑÑÑ ÑдалиÑÑ ÑеÑез ÑкземплÑÑ", instance27 print u"ÐÑÐ¾Ñ Ð¼ÐµÑод Ð¼Ð¾Ð¶ÐµÑ ÐºÐ°Ðº-нибÑÐ´Ñ ÑдалиÑÑ Ð°ÑÑибÑÑ. ",28 print u"ÐÑи ÑÑом Ñам деÑкÑипÑÐ¾Ñ Ð¾ÑÑанеÑÑÑ."29 30class A(object):31 32 x = MyDescriptor() # ÐÑо бÑдÑÑ Ð°ÑÑибÑÑÑ ÐºÐ»Ð°ÑÑа, доÑÑÑп к коÑоÑÑм33 y = MyDescriptor() # конÑÑолиÑÑеÑÑÑ Ð¼ÐµÑодами __get__/__set__/__delete__34a = A()35print a.x # 123, вÑÐ·Ð¾Ð²ÐµÑ MyDescriptor.__get__(x, a, A)36print A.x # 123, вÑÐ·Ð¾Ð²ÐµÑ MyDescriptor.__get__(x, None, A)37a.x = 5 # ÐÑÐ·Ð¾Ð²ÐµÑ MyDescriptor.__set__(x, a, 5)38#A.x = 5 # ÐÑÑибÑÑ A.x ÑÑÐ°Ð½ÐµÑ ÑиÑлом 5, __set__ не бÑÐ´ÐµÑ Ð²Ñзван39del a.x # ÐÑÐ·Ð¾Ð²ÐµÑ MyDescriptor.__delete__(x, a), A.x не бÑÐ´ÐµÑ Ñдален40print a.x # 12341# ÐамеÑание: ÐеÑкÑипÑоÑ, Ñ ÐºÐ¾ÑоÑого еÑÑÑ Ð¼ÐµÑод __set__ или __del__, назÑваеÑÑÑ 42# деÑкÑипÑоÑом даннÑÑ
(data descriptor). Такие деÑкÑипÑоÑÑ Ð¸Ð¼ÐµÑÑ Ð¿ÑиоÑиÑÐµÑ 43# пеÑед лиÑнÑми аÑÑибÑÑами обÑекÑа пÑи ÑÑении и запиÑи. ÐапÑимеÑ, еÑли Ñ 44# клаÑÑа еÑÑÑ Ð´ÐµÑкÑипÑÐ¾Ñ Ð´Ð°Ð½Ð½ÑÑ
"x", а Ñ ÑкземплÑÑа ÑÑого клаÑÑа еÑÑÑ 45# аÑÑибÑÑ "x", ÑÑение/запиÑÑ "ÑкземплÑÑ.x" бÑÐ´ÐµÑ ÑабоÑаÑÑ Ñ Ð´ÐµÑкÑипÑоÑом. 46# ÐÑо ÑÑение/запиÑÑ Ð°ÑÑибÑÑов Ñм. в замеÑке "classes".47#48# ÐамеÑание: ÐÑÐ±Ð°Ñ ÑÑнкÑÐ¸Ñ Ð¸Ð¼ÐµÐµÑ Ð¼ÐµÑод __get__, коÑоÑÑй возвÑаÑÐ°ÐµÑ ÑпеÑиалÑнÑй 49# обÑекÑ-меÑод Ð´Ð»Ñ ÑÑой ÑÑнкÑии. ÐоÑÑÐ¾Ð¼Ñ ÑÑнкÑÐ¸Ñ - ÑÑо деÑкÑипÑоÑ, и когда она 50# ÑвлÑеÑÑÑ Ð°ÑÑибÑÑом клаÑÑа, пÑи обÑаÑении к ней ÑоздаеÑÑÑ Ð¼ÐµÑод.51#52# ÐамеÑание: ÐбÑÐµÐºÑ Ð²ÐµÐ´ÐµÑ ÑÐµÐ±Ñ ÐºÐ°Ðº деÑкÑипÑÐ¾Ñ ÑолÑко пÑи обÑаÑении к Ð½ÐµÐ¼Ñ ÐºÐ°Ðº 53# к аÑÑибÑÑÑ ÐºÐ»Ð°ÑÑа. РоÑÑалÑнÑÑ
ÑлÑÑаÑÑ
меÑÐ¾Ð´Ñ __get__/__set__/__delete__ ...
09 __get__,__set__,__delete__方法.py
Source:09 __get__,__set__,__delete__方法.py
2# __title__ = '09 __get__,__set__,__delete__æ¹æ³.py'3# __author__ = 'yangyang'4# __mtime__ = '2018.03.20'5'''6 æ述符æ¯ä»ä¹:æ述符æ¬è´¨å°±æ¯ä¸ä¸ªæ°å¼ç±»,å¨è¿ä¸ªæ°å¼ç±»ä¸,è³å°å®ç°äº__get__(),__set__(),__delete__()ä¸çä¸ä¸ª,è¿ä¹è¢«ç§°ä¸ºæ述符åè®®7__get__():è°ç¨ä¸ä¸ªå±æ§æ¶,触å8__set__():为ä¸ä¸ªå±æ§èµå¼æ¶,触å9__delete__():éç¨delå é¤å±æ§æ¶,触å10æ述符æ¯å¹²ä»ä¹ç:æ述符çä½ç¨æ¯ç¨æ¥ä»£çå¦å¤ä¸ä¸ªç±»çå±æ§ç(å¿
é¡»ææ述符å®ä¹æè¿ä¸ªç±»çç±»å±æ§ï¼ä¸è½å®ä¹å°æé å½æ°ä¸)11'''12#æ述符Str13class Str:14 def __get__(self, instance, owner):15 print('Strè°ç¨')16 def __set__(self, instance, value):17 print('Str设置...')18 def __delete__(self, instance):19 print('Strå é¤...')20#æ述符Int21class Int:22 def __get__(self, instance, owner):23 print('Intè°ç¨')24 def __set__(self, instance, value):25 print('Int设置...')26 def __delete__(self, instance):27 print('Intå é¤...')28class People:29 name=Str()30 age=Int()31 def __init__(self,name,age): #name被Str类代ç,age被Int类代ç,32 self.name=name33 self.age=age34#ä½å°ï¼ï¼å®ä¹æå¦å¤ä¸ä¸ªç±»çç±»å±æ§35#ä½æ¶ï¼ï¼ä¸çä¸åæ¼ç¤º36p1=People('ryan',18) # è°ç¨æ述符ç __set__37#æ述符Strç使ç¨38# p1.name # è°ç¨æ述符ç __get__39# p1.name='cherry' # è°ç¨æ述符ç __set__40# del p1.name # è°ç¨Str æ述符ç __delete__...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!