Created
March 27, 2012 22:11
-
-
Save zonoise/2220852 to your computer and use it in GitHub Desktop.
Python study about class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HpError(Exception): | |
pass | |
class Employee(object): | |
__slots__ = ['__name','__hp'] | |
def __init__(self,name,hp): | |
self.__name=name | |
self.__hp=hp | |
def work(self,time): | |
if self.__hp >= time: | |
self.__hp -= time | |
else: | |
raise HpError | |
def workable(self): | |
if self.__hp > 0: | |
return True | |
else: | |
return False | |
def __sethp(self,hp): | |
if hp >= 0: | |
self.__hp=hp | |
else: | |
raise HpError | |
def __gethp(self): | |
return self.__hp | |
hp=property(__gethp,__sethp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HpError(Exception): | |
pass | |
class Employee(object): | |
def __init__(self,name,hp): | |
self.__name=name | |
self.__hp=hp | |
def work(self,time): | |
if self.__hp >= time: | |
self.__hp -= time | |
else: | |
raise HpError | |
def workable(self): | |
if self.__hp > 0: | |
return True | |
else: | |
return False | |
def sethp(self,hp): | |
if hp >= 0: | |
self.__hp=hp | |
else: | |
raise Exception | |
def gethp(self): | |
return self.__hp | |
hp=property(gethp,sethp) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
class TestEmployee(unittest.TestCase): | |
def setUp(self): | |
self.employee = Employee("taro",100) | |
def test_gethp(self): | |
self.assertTrue(self.employee.hp == 100) | |
def test_sethp(self): | |
self.employee.hp=95 | |
self.assertTrue(self.employee.hp == 95) | |
def test_sethp_error(self): | |
try: | |
self.employee.hp = -1 | |
except Exception: | |
pass | |
except e: | |
self.fail('Unexpected exception thrown:', e) | |
else: | |
self.fail('ExpectedException not thrown') | |
def test_work(self): | |
self.employee.work(10) | |
self.assertTrue(self.employee.hp == 90) | |
def test_work_error(self): | |
self.assertRaises(HpError,self.employee.work,200) | |
def test_workable(self): | |
self.employee.hp = 1 | |
self.assertTrue(self.employee.workable()) | |
def test_workable_false(self): | |
self.employee.hp = 0 | |
self.assertFalse(self.employee.workable()) | |
if __name__ == '__main__': | |
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#using __getattr__ __setattr__ | |
class HpError(Exception): | |
pass | |
class Employee(object): | |
def __init__(self,name,hp): | |
self.name=name | |
self.hp=hp | |
def work(self,time): | |
if self.hp >= time: | |
self.hp -= time | |
else: | |
raise HpError | |
def workable(self): | |
if self.hp > 0: | |
return True | |
else: | |
return False | |
def __setattr__(self,name,value): | |
if name=='hp': | |
self.__check_hp(value) | |
self.__dict__[name]=value | |
def __getattr__(self,name): | |
return self.__dict__[name] | |
def __check_hp(self,hp): | |
if hp < 0: | |
raise HpError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment