0

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!

user3710436
  • 243
  • 1
  • 2
  • 8
  • Instead of generating one of the code files (if I understand correctly what you mean), shouldn't you rather store the data (as text, or pickled, or whatever) and recreate the objects you need from that? That would be much cleaner and would avoid some potential problems... – Thierry Lathuille Mar 27 '20 at 17:52
  • Hi Thierry, I do need to store the data, I am using a python file for it instead of text as you mentioned. – user3710436 Mar 27 '20 at 17:57
  • How to create a nice looking text file containing instances of my objects? But taking values of attributes from user input. – user3710436 Mar 27 '20 at 18:55

0 Answers0