1

I have found a really great Befunge interpreter, and am trying to add simple importing of fingerprints and their corresponding functions from a file. My current code uses classes for each of the functions, and then assigns the functions under a fingerprint, and then under a letter. This is how the original code, and my current code works. I was hoping to import a file named fingerprint.py as a module, and then run fingerprint.initiate() or something of the sort to add the fingerprints to the fingerprint dictionary. It is in the interpreter file, and I am trying to use the code:

global fingerDict   
fingerDict += 0x4567890: {'A': FUNC.A}  

to add the fingerprint data to the dictionary, and it doesn't allow me to do it, saying it has a syntax error. I have no idea how I should instantiate the classses. The full code is here Any answers are heavily appreciated, and I would love to mention you in my upcoming publishing og the code. Thanks, and have a good day.

  • Are you trying to add an entry to the dict? `fingerDict[0x4567890] = {'A': FUNC.A}`? – Claudiu Apr 29 '16 at 15:03
  • Yes, thanks. That helps. How would I do the classes, should I make them global? So they can be accessed under FUNC.A from the main code? – Finian Blackett Apr 29 '16 at 15:04
  • Would that make it be able to edit the variable inside interpreter.py? – Finian Blackett Apr 29 '16 at 15:05
  • More specifically, would that allow the imported code to edit the variable inside the main code? And allow it to be called by FUNC.A instead of fingerprint.FUNC.A()? Or would that be okay too? – Finian Blackett Apr 29 '16 at 15:10
  • Hmm I can't be sure without looking deeply at your code. Generally, the function `FUNC.A` will only be able to access what it can access where it was defined. If it has access to the other module's variables then it can access them. But that doesn't sound like the best design. You probably want a `state` variable (can be a dict or a class instance), which is passed as the first parameter to any function in `fingerDict`, then all the functions defined expect it and mutate it when they want to change something. That way you don't have to deal with modifying variables in another module. – Claudiu Apr 29 '16 at 15:17
  • So, what would that be in code? I am still relatively new to python, so I do not have any clear ideas for what that would be like. All I want to do is have the module imported, if the example fingerprint is FUNC, define variable FUNC = fingerprint.FUNC() from the fingerprint.py module, and then add the functions to the fingerprint dictionary from the fingerprint.py module. I don't want you to have to edit the interpreter's code to add a function. I want it all inside fingerprint.py. – Finian Blackett Apr 29 '16 at 21:47

1 Answers1

0

You can use fingerDict.__setitem__(0x4567890, {'A': FUNC.A}).

Alternatively, fingerDict[0x4567890] = {'A': FUNC.A} will also work, as said Claudiu.