Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Python environment after program ends

    Scheduled Pinned Locked Moved Pymakr
    8 Posts 5 Posters 97 Views 5 Watching
    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.
    • P Offline
      poesel
      last edited by

      I'm using the pymakr plugin with VSC on Windows. After a program ends or is interrupted I'm back on the REPL. So far so good but all programm objects are destroyed. I can of course manually type in the commands to recreate them but this is tedious.

      Is there a way to keep the current environment (objects, variables, etc...) after a program ends or is interrupted?

      Thanks!

      1 Reply Last reply Reply Quote 0
      • C Offline
        crumble
        last edited by

        You have to store your desired objects in global variables or write log files.

        Have a look on the lifetime of objects and why it is good practice to keep it either short or keep them as long as possible alive.

        Have a look on unit tests as well. The concept may be only useful for a small set of your methods, but it is a great concept for testing the methods which have not to deal with side effects. There may be no fancy tool for micropython. But you can use the idea to write yourself some simple test packages and upload/call them when needed.

        1 Reply Last reply Reply Quote 0
        • P Offline
          poesel
          last edited by

          I don't think you understood what I'm asking for.

          Simple example: I have a device on the i2c bus I want to talk to. So in my program I have an object 'i2c' from machine.I2C. The program ends or is aborted and 'i2c' is gone. If I want to test I have to recreate that object in the REPL.
          Is there a way to keep that object alive?

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @poesel
            last edited by robert-hh

            @poesel If you start a script, let's call it my_script.py, manually with import or from main with import, and that script finishes regularly, you have an object call my_script in repl, and dir(my_script) shows you all objects created within. So you would have that i2c object under the name myscript.i2c.
            If you aborted my_script e.g. with Ctrl-c, you can re-import it and have then all objects visible. The re-import will not run the script again.

            1 Reply Last reply Reply Quote 0
            • P Offline
              poesel
              last edited by

              Thanks to both responders - the confusion was on my side. I had a wrong understanding about how this works.
              That what I wanted is not possible but you can get pretty close with a construct like that:

              ...
              i2c = None
              ds = None
              
              def main():
              
                  global i2c
                  global ds
              ...
              

              I guess that is what @crumble meant. Variables in a global scope survive program termination, those inside a function (like main() ) get destroyed when the function ends.

              1 Reply Last reply Reply Quote 1
              • A Offline
                aen
                last edited by

                @crumble do you have a good pointer to unit testing these devices?

                A C 2 Replies Last reply Reply Quote 0
                • A Offline
                  andreas @aen
                  last edited by andreas

                  Dear @aen,

                  do you mean real DUT testing or just some environment which will help you by emulating the hardware through mocking the MicroPython API to some extent?

                  Within the Terkin Datalogger, we've built upon esp32-machine-emulator and expanded it by a large amount to conduct some pretty tests you will find at [1]. This MicroPython test harness is essentially based on CPython and is therefore probably different from other players in this field. However, it gives us much confidence on many levels and helped tremendously while refactoring the growing code base even when not being invoked on the device itself.

                  Maybe this helps?

                  With kind regards,
                  Andreas.

                  [1] https://github.com/hiveeyes/terkin-datalogger/tree/master/test

                  1 Reply Last reply Reply Quote 0
                  • C Offline
                    crumble @aen
                    last edited by

                    @aen

                    By now I have written my own tests without using a library. I had some trouble to fit my stuff into a LoPy 1. I still have to write me some scripts for precompiling and splitting the full source into testable projects. I am using Atom which seems not support such fancy things out of the box.

                    I found this library the other day, which seems to run with the pycom implementation:

                    micropython-lib on github

                    1 Reply Last reply Reply Quote 0

                    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                    With your input, this post could be even better 💗

                    Register Login
                    • 1 / 1
                    • First post
                      Last post

                    Pycom on Twitter

                    Pycom