Advertisement
Guest User

hero

a guest
Dec 11th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. class ChoiceField(object): #
  2. choices = ['offline','hidden','online','mage','assasin','knight','warrior']
  3. val = ''
  4. def __init__(self, val):
  5. self.choices = __class__.choices
  6. self.val = val
  7.  
  8. def __get__(self, instance, owner):
  9. return self.val
  10. '''__get__(self, instance, instance_class)
  11. Определяет поведение при возвращении значения из дескриптора. instance это объект,
  12. для чьего атрибута-дескриптора вызывается метод. owner это тип (класс) объекта.
  13. '''
  14. def __set__(self, instance, val):
  15. '''__set__(self, instance, value)
  16. Определяет поведение при изменении значения из дескриптора.
  17. instance это объект, для чьего атрибута-дескриптора вызывается метод.
  18. value это значение для установки в дескриптор. '''
  19. if val in self.choices:
  20. self.val = val
  21. else:
  22. raise ValueError('Invalid value(profa or status)')
  23.  
  24.  
  25. class Hero(object):
  26. profession = ['mage','assasin','knight','warrior']
  27. # status = ['offline','hidden','online']
  28. name = ''
  29. level = ''
  30. location = "23.44344, 0.344234"
  31. profa = ChoiceField('profa') #descriptor
  32. character_status = ChoiceField('character_status') #проверяем такущий статус героя #descriptor
  33.  
  34. def __init__(self, character_status,profa,name,level, location):
  35. self.character_status = character_status #descriptor
  36. self.profa = profa #descriptor
  37. self.name = self.validate_name(name)
  38. self.level = self.validate_level(level)
  39. self.location = self.validate_location(location)
  40.  
  41. def validate_name(self, name):
  42. if isinstance(name, str) and name[0].isdigit() == False:
  43. return name
  44. else:
  45. raise ValueError('Invalid Name')
  46.  
  47. def validate_level(self,level):
  48. if isinstance(level,(int, str)) == False and level.isalnum() == True and level.isdigit() == False :
  49. raise ValueError('level must be only int')
  50. elif int(level) >= 100:
  51. raise ValueError('out of range')
  52. else:
  53. return level
  54.  
  55. def validate_location(self, location):
  56. x = location.split(',')
  57. if float(x[0]) and float(x[1]):
  58. return location
  59. else:
  60. raise ValueError('Erorr . invalid location')
  61.  
  62. def save_to_file(self,data_file):
  63.  
  64. with open(data_file, 'w') as write_file:
  65. s_data = json.dump((unit.__dict__,{"status": Hero.status,"profession": Hero.profession}), write_file, indent=4, separators=(',', ':'))
  66. return s_data
  67.  
  68. def load_from_file(self,data_file):
  69.  
  70. with open(data_file) as load_file:
  71. data = json.load(load_file)
  72. self.name = self.validate_name(data['name'])
  73. self.level = self.validate_level(data['level'])
  74. self.profa = self.validate_profession(data['profa'])
  75. self.location = self.validate_location(data['location'])
  76. self.character_status = self.validate_status(data['character_status'])
  77.  
  78. data_file = 'hero.json'
  79.  
  80. unit = Hero('online','mage','Sam','33','23.44344, 0.344234',)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement