Forum Archive

Saving and importing custom modules

resserone13

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

ccc

Get rid of both c and i and do for color in colors.

range(len()) is almost always a sign that enumerate() should be used instead but here we don’t even need that.

resserone13

@ccc I’ve update the code but messed up something and now I’m getting a name error. I’m sure it’s something small but what am I missing.


from scene import *

class MyScene (Scene):
    def setup(self):

        def layered_text(self, name, text, font, size, x, y, x_offset, y_offset, *colors, **kwargs):

            colors = [*colors]

            for color in colors:
                name = LabelNode(
                    text, 
                    font = (font, size), 
                    position = (x, y), 
                    color = color, 
                    parent=self, 
                    )

                x += x_offset
                y += y_offset
                i += 1


    self.layered_text('title', 'hello', 'DIN Condensed', 40, 200, 300, 1, -1, 'red', 'black', 'white','yellow')

run(MyScene())

resserone13

@ccc i updated the the functions with you recommendations. I was thinking I don’t need the name attribute after self ?

resserone13

@mikael you have any input on saving this as a module so I came use it I’m another script?

mikael

@resserone13:

  • 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.
resserone13

@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!