1

In microbit muPython: sleep(ms), the units is milliseconds.

However, if import time module and use sleep() then muPython uses time module’s sleep(s) which is units of full seconds. Coder must substitute time module’s sleep_ms(ms) to get units of milliseconds.

If using time module, how can I force use of the ‘normal’ sleep(ms)?

Or more generally, how can I specify using any command from the ‘normal’ muPython as opposed to the same-spelled command from an imported module?

# Task: Show SAD, sleep 1 sec, show HAPPY
# Problem: HAPPY takes 17 minutes to appear
from microbit import *
from time import *
display.show(Image.SAD)
sleep(1000) # uses time.sleep(units=sec) so 1,000 sec
display.show(Image.HAPPY)
Playing with GAS
  • 565
  • 3
  • 10
  • 18

1 Answers1

3

Use from ... import ... as notation.

from microbit import sleep as microbit_sleep
from time import sleep as normal_sleep

microbit_sleep(1000) # sleeps for one second
normal_sleep(1000) # sleeps for much longer

Or, if you need everything in those two modules, just do a normal import.

import microbit
import time

microbit.sleep(1000)
time.sleep(1)

from ... import * is generally considered bad Python style precisely for the reasons you've discovered here. It's okay for really quick scripts, but best avoided as projects get larger and depend on more modules.

Rob Hansen
  • 317
  • 1
  • 4
  • Thanks, Rob, for help. I think I get the theory but left betixt and between. If I do from microbit import sleep as microbit_sleep then I have to repeat for each microbit function, like display if I do import microbit – Playing with GAS Jul 14 '20 at 17:37
  • Thanks, Rob, for help. I think I get the theory but on syntax I'm betwixt and between. If I do from microbit import sleep as microbit_sleep then would have to repeat for each microbit function, including display? I tried import microbit But line with display.show() throws error "Name Not Recognized" Am I being greedy to want to import all functions from a module wo/ individual lines to import & name each function? I was hoping that I could resolve ambiguity by just preface a command with its module name. Again, thanks for your time. – Playing with GAS Jul 15 '20 at 02:09
  • The `from X import Y as Z` makes _only_ the function Y available, except it renames it Z. If you don't need to rename it you can just `from X import Y`. The normal way Python programmers import many functions at once is `from X import Y, Z, A, B, C`. `import X` imports all (handwaving a little bit) functions from the module X, and makes them available as `X.Y()` ("call function Y located in module X"). Hope this helps. If you need more help, just ask! – Rob Hansen Jul 15 '20 at 22:15
  • Thanks, Rob, for both theory and syntax – Playing with GAS Jul 16 '20 at 21:40