-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass-Writing Program.py
49 lines (41 loc) · 1.36 KB
/
Class-Writing Program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""Python 102 - Assignment 6
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1VjxPt-MpDXlOrO477R2brvxzTybk6s0A
"""
def get_init_code(property_list):
init_code = '\ndef __init__(self, '
for each_property in property_list:
init_code += each_property + ', '
# replace the last , with ):
init_code = init_code[0:-2]
init_code = init_code + "):"
for each_property in property_list:
init_code += f'\n self.{each_property} = {each_property}'
init_code += '\n'
return init_code
def get_repr_code(class_name, property_list): # Assignment 6 (1/3 of the class)
init_code = 'def __repr__(self)\n return f"'
init_code += class_name
init_code += ': ('
for elem in property_list:
init_code += elem
init_code += ': {self.'
init_code += elem
init_code += '} '
init_code += ')"\n'
return init_code
if __name__ == "__main__":
input_str = input('Enter your class details (ex: Student [name, email, age]): ')
input_str_tokens = input_str.split()
elem_list = []
for elem in input_str_tokens:
elem = elem.replace('[', '')
elem = elem.replace(']', '')
elem = elem.replace(',', '')
elem_list.append(elem)
class_name = elem_list[0]
property_list = elem_list[1:]
print(get_init_code(property_list))
print(get_repr_code(class_name, property_list))