introspection.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. def get_info_about_object(obj):
  2. print(dir(obj))
  3. print(f'Всего у объекта {len(dir(obj))} атрибутов и методов')
  4. def check_exist_attrs(obj, lst):
  5. 'Возвращает словарь со статусом атрибутов из списка лист у obj'
  6. return {x:hasattr(obj, x) for x in lst}
  7. def create_attrs(obj, lst):
  8. for data in lst:
  9. setattr(obj, data[0], data[1])
  10. def my_test_function():
  11. pass
  12. def print_goods(lst):
  13. pass
  14. def count_strings(*args):
  15. str_count = 0
  16. for value in args:
  17. if isinstance(value, str):
  18. str_count += 1
  19. return str_count
  20. def find_keys(**kwargs):
  21. lst = []
  22. for name, value in kwargs.items():
  23. if isinstance(value, (list, tuple)):
  24. lst.append(name)
  25. return sorted(lst, key=str.lower)
  26. def get_extensions(file_list):
  27. def _get_extansions(file_name):
  28. if "." in file_name:
  29. ext = file_name.split(".")[-1]
  30. else:
  31. ext = ""
  32. return ext
  33. return [_get_extansions(i) for i in file_list]
  34. def double_odd_numbers(numbers):
  35. def double(value):
  36. return 2*value
  37. def is_odd(value):
  38. return value%2 != 0
  39. return [double(num) for num in numbers if is_odd(num)]
  40. def calculate(x, y, operation='a'):
  41. def addition(x, y):
  42. print(x + y)
  43. def subtraction(x, y):
  44. print(x - y)
  45. def division(x, y):
  46. if y == 0:
  47. print("На ноль делить нельзя!")
  48. else:
  49. print(x/y)
  50. def multiplication(x, y):
  51. print(x*y)
  52. if operation == 'a':
  53. addition(x, y)
  54. elif operation == 's':
  55. subtraction(x, y)
  56. elif operation == 'd':
  57. division(x, y)
  58. elif operation == 'm':
  59. multiplication(x, y)
  60. else:
  61. print('Ошибка. Данной операции не существует')
  62. def main():
  63. # get_info_about_object(my_test_function)
  64. # print(hasattr(my_test_function, '__code__'))
  65. # print_goods.is_working = False
  66. # print_goods.status = 'Not ready'
  67. # print(check_exist_attrs(print_goods, ['is_working', 'status', 'time', 'speed']))
  68. # create_attrs(print_goods, [('is_working', False), ('days', 10), ('status', 'Not ready')])
  69. # print(check_exist_attrs(print_goods, ['sort', 'is_working', 'days', 'status', 'upper']))
  70. # print(count_strings(1, 2, 'hello', True, 't'))
  71. # print(find_keys(At=[4], awaited=(3,), aDobe=[5]))
  72. # print(get_extensions(["foo.txt", "bar.mp4", "python3"]))
  73. # print(double_odd_numbers([-43, 91, 932, 9201, 32, 93]))
  74. calculate(2, 5) # Печатает 7.0
  75. calculate(2.2, 15, 'a') # Печатает 17.2
  76. calculate(22, 15, 's') # Печатает 7.0
  77. calculate(2, 3.2, 'm') # Печатает 6.4
  78. calculate(10, 0.4, 'd') # Печатает 25.0
  79. if __name__ == '__main__':
  80. main()