-1

example.py has some code that creates a MIDI file based from whatever note blocks appear above it (however many there are), like this:

#Note block
track = 0 
channel = 0 
pitch = 60 
time = 0 
duration = 1 
volume = 100 

If I have a bunch of these on example2.py, what code can I write on example.py that brings them in verbatim? i.e., preserves them as variables in the structure in which they are written?

Thanks in advance.

2 Answers2

1

You can just import example2 in example. That's the preferred way:

from example2 import track, channel, pitch, ...

If you have many of those, you can just do:

from example2 import *

But the above is ugly, it's not clear what is imported and what is not. A better way would be to keep the notes in a constant dict/list, name it and just import a single variable that contains all notes.

samu
  • 2,870
  • 16
  • 28
  • there will be dozens or hundreds of "Note Blocks" one after the other in example2.py, each with the same variables. They all need to be seen in example.py. in that same structure. – Tabor18 May 08 '18 at 13:41
  • In that case, you can just do `from example2 import *` - not the best way to do it, but will work. I will update the answer to include another usecase. – samu May 08 '18 at 13:53
-1

I figured out the answer. I used:

exec(open(".example2.py").read())

and it worked like a charm!