Main Python code Class: main.py
class Dog():
name = ""
location = ""
barks = False
NotNaughty = False
def __init__(self, name, **kwargs):
self.name = name
class Cat():
name = ""
location = ""
isQuiet = False
lovesmouse = True
def __init__(self, name, **kwargs):
self.name = name
I want to create a python file which has a collection of instances created based of user input like so:
Objects.py
from main import Dog, Cat
d = Dog("lucy")
d.location = "California"
d.barks = False
d.NotNaughty = True
c = Cat("sweetie")
c.location = "Seattle"
c.isQuiet = False
c.lovesmouse = True
My code is a python script to be used via CLI. So, I am thinking of using a python CLI package such as PyInquirer or PySimpleGUI to get user input by asking questions and using answers to create an objects.py file as shown above. (I need this object.py file for another complex functionality in my code)
Code for using PyInquirer - using this code in main.py:
from __future__ import print_function, unicode_literals
from PyInquirer import prompt, print_json
from pprint import pprint
print('Hi, welcome to Animal World')
questions = [
{
'type': 'rawlist',
'name': 'Element',
'message': 'What\'s your first Animal?',
'choices': ['Dog', 'Cat']
},
{
'type': 'input',
'name': 'name of animal',
'message': 'What\'s the name?'
},
{
'type': 'input',
'name': 'location of animal',
'message': 'What\'s the location?'
}
]
answers = prompt(questions)
pprint(answers)
Instead of just printing answers directly, I want the format to be exactly like shown above in object.py. I am looking for inputs as to how I can create objects.py dynamically using user input. Any suggestions are highly appreciated. Any alternatives to PyInquirer are welcome too! Thanks!