omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Separate Files

    Pythonista
    3
    7
    4551
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Cubbarooney104
      Cubbarooney104 last edited by

      (Yikes, I ask a lot of questions!)
      Let's pretend I was writing a code that used the scene module. This code had a list of pictures that were only used 1/10 of the time. Instead of having all of that code "cluttering up" my main code I would want it in a seperate file that could be called upon when needed. I've done stuff like this before ("execfile()"), but that was without the scene module. Would it be that the second file should be an entirely new class? Or would it be that it would be another MyScene class? Or am I completely missing the ball here and it is something very different? or is it simply not possible (if so, why)?

      Cubbarooney

      1 Reply Last reply Reply Quote 0
      • eliskan175
        eliskan175 last edited by

        This is very easy to implement and I will walk you through the steps.

        Step 1) Make a new file with some functions you want to use in your script, but that you want out of your way. For my example, I will make a very simple example, so my file will only have 1 function and 1 value:
        <pre>#A variable we can call from our other script.
        test_var = 100

        #Print whatever the input is
        def print_input(input):
        print "Input is: "+str(input)
        </pre>

        Now that this file is created, save it as "print_functions" or another appropriate name that is easy for you to remember. You will need to know this name for importing.

        Next, create a new Python file. This file HAS to be in the same directory as the other one, or you won't be able to import the other file. This new Python file can be your active MyScene script, or anything you want really. At the top of the new file, put this line:

        <pre>from print_functions import *
        </pre>

        What this says is to import everything in print_functions. So you can now call the print_input function or find the value of test_var from your other script, after you have imported it:

        <pre>print_input(10)
        print test_var
        </pre>

        If you wanted to keep your print_functions OBVIOUS they're in the print_functions section of your code, you could import your module differently:

        <pre>import print_functions
        print_functions.print_input(10)
        print print_functions.test_var
        </pre>

        or...

        <pre>import print_functions as pf
        pf.print_input(10)
        print pf.test_var
        </pre>

        If you only need a specific function for your current script and you don't need all the functions available, it's a good idea to implicitly state which functions/values you need. This way, your program doesn't have to compile a lot of useless 'garbage' before it runs:

        <pre>from print_functions import print_input, test_var
        print_input(10)
        print test_var
        </pre>

        Personally I suggest the first method. It imports everything. You only need to import a script once, then the functions will be useable throughout the rest of the script - regardless of what your script is doing, it will work.

        1 Reply Last reply Reply Quote 0
        • Cubbarooney104
          Cubbarooney104 last edited by

          Didn't know that import worked with scripts too! This has many uses...

          Thanks Eliskan! Helped a lot. Now have to twiddle with another idea, possibly fail, and then (if the previous step proves true) post question #11. :D

          Cubbarooney

          P.S. you did a good job explaining the method!

          1 Reply Last reply Reply Quote 0
          • eliskan175
            eliskan175 last edited by

            Nothing wrong with asking questions. The only stupid question is the one not asked.

            One more simple import method that I never really used, but might be useful:

            <pre>from print_functions import print_input as p
            p(10)</pre>

            Good way to shorten the code if you use a function a thousand times. You can shorten the functions name or give it a name you better understand. "p" could be anything you want it to be.

            1 Reply Last reply Reply Quote 0
            • Cubbarooney104
              Cubbarooney104 last edited by

              Oh goodie, I don't like asking stupid questions :P

              And that is interesting. Can you do the following then?
              <pre>from scene import *
              from scene import image as i
              from time import time, localtime as ti, loc
              from other_module import functiona, functionb as ,b</pre>

              The first two are simple, it basically redefines image. The third line does it in bulk while line imports two commands, but only shortens one of them. Which of these (besides line one...) work? I just realized I can check myself... So I will. In a moment. I'll post what I find when I have Internet again (couple hours, out of house).

              Thanks,
              Cubbarooney

              1 Reply Last reply Reply Quote 0
              • PyLot213
                PyLot213 last edited by

                Also FYI: If you want to structure your stuff even more, then you can also have your file in a different folders (since the import follows the python import rules). The only thing you have to do is create an (even empty) init file in the directory and then you can import it like
                import mydir.mycode
                (you can read more about it here: http://docs.python.org/2/tutorial/modules.html#packages )

                1 Reply Last reply Reply Quote 0
                • Cubbarooney104
                  Cubbarooney104 last edited by

                  @PyLot
                  Wow, thanks! that really helps keep things a bit more organized :)

                  I owe ya' one.

                  Cubbarooney

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors