0

I want to have a folder with some .py files so that everytime that I add a new file there, it would dynamically add the the new functions that I defined there to a dictionary so that I can use them by user input. Something like django does with views maybe? I can do this if it's on the same file. Like this.

#dicionario q guarda as referencias pras minhas funções
dictionary = dict()

#funcao anexadora.
def anexa_func(func):

    dictionary[func.__qualname__] = func


    return

@anexa_func
def nova_funcao():

    print('joined in the function')




    return

#mostrando q realmente salvou no dictionary
print(dictionary)



for key,value in dictionary.items():
    dictionary[key]()


dictionary['nova_funcao']()

So i want to create a folder like "commands" and add new .py files with python functions inside that follow a syntax of what a "command" is, so i can invoke them with some args like first order functions.

if u guys run the program above, the output will be like this.

$ python main.py {'new_function': function new_function at 0x7f8f678d6510} joined in the function joined in the function

Laurent H.
  • 6,316
  • 1
  • 18
  • 40
WagnerAlbJr
  • 190
  • 1
  • 12
  • I might be misreading it, but it sounds a lot like you're just trying to make a module/package? – asongtoruin Feb 27 '18 at 16:40
  • not a module at all, the big thing is that i just want to put files in a folder, without importing them in the file that has the dictionary, and they automatically add themself to the dictionary. Like , i want to everytime that i want to add a new command , to this dictionary i just write a command_1.py file and save this in the "command" folder, without modyfing or manual importing anything. – WagnerAlbJr Feb 27 '18 at 16:43
  • Why not just import them? Something like `from commands import *` Perhaps you're missing a possible obvious solution. – sytech Feb 27 '18 at 16:44
  • node can do this with require, you use fs.readdiirr() to read a directory and after you take the name of the file, you use require to load the functions that you'v wanted to import, and u manually set function to a dictionary as a value, and use the key as the file name. And all this is done automatically. – WagnerAlbJr Feb 27 '18 at 16:47
  • Of course i could import them, the result would be the same, but i want to understand if i can do this in python, i think that django, do this with views, after u put a view and use the decorator in them, he automatically index them as views. i want to improve my python understanding. – WagnerAlbJr Feb 27 '18 at 16:50
  • Well. Node isn't Python. Python has different ideas and workflows compared to node. One major idea in Python, *flat is better than nested*. The idea of adding a bunch of files like this is counter to the way things should be done in Python If you just want to add another function, you should just add another function to, say, your `commands` module or package. Is there a particular problem you're trying to solve here? The way django handles views is by importing them, and each view is mapped in a urls.py, so I'm not sure what you mean by that. – sytech Feb 27 '18 at 16:51
  • For example, if you have `commands.py` you could simply write another function in that module, alongside others. Then when you import `commands`, your new function is available, just like the others. Is there something there that is incomplete for your goals? – sytech Feb 27 '18 at 16:56
  • i think that in this way, i can abstract commands since i can turn in and off commands simple by taking them from the folder or not. I think that i can do this using 'exec' and parsing all the files in the folder, but i don't know if this is the best way to do. – WagnerAlbJr Feb 27 '18 at 16:59
  • something like that as you'v mentioned. – WagnerAlbJr Feb 27 '18 at 17:00
  • You can define a `__all__` for your package which determines which names are exported when you do `from package import *` -- So you could control it in this way, if that suits your case. See [this answer](https://stackoverflow.com/a/35710527/5747944) which explains its use. If you want to 'disable' a function, you can simply remove it from the list of names in `__all__`. Perhaps that will work for you. – sytech Feb 27 '18 at 17:02
  • Don't use `exec`. Do some research into Python packages and start working in the way they're set up rather than trying to mangle Python to be like Node – asongtoruin Feb 27 '18 at 17:02
  • so, i know that if u use exec, that will work, but i don't know if this is the best way. the question isn't about the best way to do, but if i can do , more to research process. – WagnerAlbJr Feb 27 '18 at 17:06

0 Answers0