Saving and importing custom modules
-
I have created a function. I relies on the scene module to make label nodes. My question is what all can I get rid of in the script if anything. Where should it be save( I pretty sure the folder for the project. Or in a directory if I would like to use it anywhere... I’m not sure how or which directory though. And any other advice you have on creating, saving, importing module and writing a better function is welcomed.
from scene import * class MyScene (Scene): def layered_text(self, name, text, font, size, x, y, x_offset, y_offset, *colors, **kwargs): i = 0 colors = [*colors] for c in range(len(colors)): name = LabelNode( text, font = (font, size), position = (x, y), color = colors[i], parent=self, ) x += x_offset y += y_offset i += 1
-
Get rid of both
c
andi
and dofor color in colors
.range(len())
is almost always a sign thatenumerate()
should be used instead but here we don’t even need that.
-
This post is deleted!last edited by
-
@ccc i updated the the functions with you recommendations. I was thinking I don’t need the name attribute after self ?
-
@mikael you have any input on saving this as a module so I came use it I’m another script?
-
- If you want to import a function to reuse it in a
scene
project, simplest is not to have it as a member of MyScene, just a plain function that you give a scene as a parameter. - If you want to import it in one project, save it in a file like label_creator.py or scene_utils.py in the same directory as your other project files. Then, in the other files,
from label_creator import layered_text
and use it. - If you want to use it in any project, put it in the existing
site-packages
directory, and use it as above. Note, though, that if you then make changes to it, you need to restart Pythonista to re-import it.
- If you want to import a function to reuse it in a
-
@mikael I’ll put it in site packages for later use. Thanks for the break down. I would like to say thank you to everyone who always helps me. Mainly you, @ccc and @cvp. I appreciate you all taking the time to explain things. I hope one day I will know as much as all three of you. I promise to help others and spread the knowledge. Thanks for everything!