0

Basically, I need to write an entire class to a new .py file. The only problem is that two important variables inside the class rely on user input. When I write the class to the file I need to have the actual number/string where the user input is instead. Here is an example of what I currently have:

class CreateClient:
    def writer(self):
        file = open('client_test.py', 'w+')
        file.write(Client)
        print("File created")

class ClientConfig:

    def hostvar(self):
        inhost = str(input('Enter the listener hostname: '))
        return inhost

    def portvar(self):
        inport = int(input('Enter the port: '))
        return inport

class Client:
    def examp(self):
        host = ClientConfig.hostvar(ClientConfig)
        port = ClientCOnfig.portvar(ClientConfig)

As you can see I need Client to be writed to a new file, except it will write

host = ClientConfig.hostvar(ClientConfig)
port = ClientCOnfig.portvar(ClientConfig)

instead of what I need (i.e host='127.0.0.1', port = 9999). Is there any way I can do this, possibly use the operation 'replace'? If I attempt to access the host or port variable by doing Client.examp.host it won't work, so I don't see how I could use replace.

9
  • pass your variable entered by user to hostvar function as args Commented Jun 11, 2017 at 20:00
  • Could you be a little clearer please? Is it just Client that needs to go somewhere else? Also, why are you passing ClientConfig as a parameter to those functions? Commented Jun 11, 2017 at 20:00
  • 1
    You should write a minimal reproducible example, what you've written now isn't really writing python source as you're suggesting. Plus, why would you need to do that? What are you actually trying to accomplish? Commented Jun 11, 2017 at 20:01
  • @Coldspeed The entire class 'Client' needs to be written to a new .py file. If I don't pass ClientConfig in the brackets I get a 'parameter unfilled: self' error. Commented Jun 11, 2017 at 20:03
  • Okay, @sS5H you're doing that wrong, but no problems. That can be fixed. What is your other issue? Who needs to have host='127.0.0.1', port = 9999? Where is this entered? Is it taken from a file? Is it taken from user input? Do you provide it? Commented Jun 11, 2017 at 20:05

2 Answers 2

1
class ClientConfig:
    @staticmethod
    def hostvar():
        inhost = raw_input('Enter the listener hostname: ')
        return inhost
    @staticmethod
    def portvar():
        inport = int(input('Enter the port: '))
        return inport

class Client:
    def examp(self):
        self.host = ClientConfig.hostvar()
        self.port = ClientConfig.portvar()

Example:

>>> client = Client()
>>> client.examp()
Enter the listener hostname: 127.0.0.1
Enter the port: 8000
>>> client.host
'127.0.0.1'
>>> client.port
8000

Note: If you're not going to use self object inside hostvar or hostvar, I strongly recommend using staticmethod.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the piece of code. When I try to write the class Client to a file I get a TypeError that it has to be a string, not a type. Is there any way I can do this?
have you updated your ClientConfig class with same as this one?
0

CreateClient

Here' you're calling file.write(Client) where you pass the class as a parameter. Assuming you want to write the code to another file, this is definitely something you don't want to do, because it just doesn't work*. I suggest you just copy-paste the code yourself into a new file and save it.

* This is because, if you want to save python objects, you'll need to convert them into a bytes stream. The best way to do this is using the pickle module, but this is not relevant for your problem

ClientConfig

Here, you have a choice. As @pramod suggested, you could convert these methods to static. Or, you could leave it be, and I'll show you how to call these accordingly in Client.

Client

Here's a solution for if you don't want to use static methods:

class Client:
    def examp(self):
        c = ClientConfig()
        self.host = c.hostvar()
        self.port = c.portvar()

And the alternative definition for this class, if you want to use static methods has been given by pramod.


Creating a new python file [as per requirement]

import random
class CreateClient:
    __client_str = '''class Client:\n\t\n\t\tdef examp(self): self.host = %s; self.port = %d\n'''

    def writer(self):
        c = ClientConfig()
        host = c.hostvar()
        port = c.portvar()
        file = open('client_test%d.py' %random.randint(1, 999999), 'w')
        file.write(CreateClient.__client_str %(host, port))
        print("Client created")
        file.close()

Invocation from within some python script:

import os
os.system("python cilent_test<some_number>.py")

15 Comments

Thanks for the answer. The entirety of class 'Client' would need to somehow to written to a new file in a function, otherwise there wouldn't be any purpose to what I'm trying to achieve. I don't know if this is remotely possible, however.
@sS5H Do you want the source code into a new file? Or do you want some object of Client in a new file?
@sS5H What are you trying to achieve? Why are you trying to write a new source file? That makes no sense.
@Coldspeed I need Client's entire block of code put in a new file for this to work. My goal is to essentially make a function that can create a new working Python script in a separate file
@sS5H You should consider creating a new instance of Client and saving that in a new file. You can then use it as you wish. The alternative method, that you wish to do, is to save the definition of Client in a huge string, then write it to a new file, and then invoke that file using subprocess or system. Very painful.
|