How do I let the user decide which variable he wants to change in python?

Asked

Viewed 159 times

0

I am developing a framework, in the directory where it is located there is a folder called "Plugins" inside it is the modules that the user can import inside the framework using the command "use", very similar to what Metasploit does, for this I use the library "imp". But each module is a module and in each one of them has different variables and within them specific values, so I wanted that when the user imports some module he can change the contents of some variable of the module using for example the set, very similar to the Metasploit.

from __future__ import print_function
import imp
import sys
def main():
    while 1:
        request = raw_input("O que você deseja: ")

        if "use" in request:
            srequest = request.split()
            if (len(srequest) < 2 or len(srequest) > 3):
                print("Requisição errada!")
            else:
                modulo = str(srequest[1])
                modulo = modulo.replace("/", ".")


                arquivo, caminho, descricao = imp.find_module(modulo, ['C:/Users/User/Desktop/TesteCriptografia/Plugins/'])


                moduloImportado = imp.load_module(modulo, arquivo, caminho, descricao)
                listadosmodulos = arquivo, caminho, descricao
                ##Era aqui que eu gostaria que o usuario escolhe a variavel que ele deseja trocar e qual valor decidir

                
if __name__ == '__main__':
    main()

1 answer

2

Variables, whether variables in a module, class attributes, or instance attributes, can be changed in Python, having the variable name as a string, with the function setattr.

So, let’s assume that I have the module "plugin_a", and inside it the variable "paramtro_1":

import plugin_a

var = "parametro_1"
novo_valor = 5

setattr(plugin_a, var, novo_valor)

The form is setattr(objeto, atributo_como_string, novo_valor). If it’s a class inside the module it’s: setattr(plugin_a.ClasseB, "atributo_c", valor)

This is one of the great semantic differences between Python and Javascript - while in Javascript it does as much as it did if the name of a variable is made to be used as "given", in Python, although access is possible, you have to use explicit mechanisms for this. (There are more "low level" forms than setattr - but setattr is the most recommended, because, when used with a class or instance, for example, will respect all the rules of access to the attribute that exist in the language - and things like property and __slots__ will always work well).

In static languages, such as Java and C#, as a rule, it is not possible to access a variable having its name as "given", that is - the name being the content in another variable (although both languages have introspection mechanisms that allow this, but are not made for this).

It is also worth mentioning that the setattr does not work for local variables. First, because a local variable only exists while a function is running, then at least some level of parallelism or complexity is required in the code in order to "see" the variables of a function while it is running. But yes, this can happen in many ways, and it is possible to "see" the value of a local variable. But not "change" - on account of a Cpython optimization for access to local variables, which eventually entered the language specification.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.