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

    method overloading in micropython

    Scheduled Pinned Locked Moved MicroPython
    micropythonrgbledoopmethodoverload
    3 Posts 2 Posters 673 Views 2 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.
    • T Offline
      tttadam
      last edited by

      Hello,

      As far as I know there is know method overloading in python or micropython
      Whats the best practice workaround for that?
      I created class for the rgbled. I want to use that for notify me about statuses, and progresses.
      It would be nice to run these notification on second thread, because it's operate with sleeps, and don't want to sleep the whole system for blinking an led.
      BTW, any feedback for my code is welcome ! :)

      My code:

      import pycom
      import time
      
      
          ##################################
          ######## avaible colors ##########
          ##################################
              # 0xFF3300 - narancs - orange 
              # 0xFF0000 - piros - red
              # 0x0000FF - kék - blue
              # 0xEE00FF - lila - purple
              # 0xFFFFFF - fehér - white
              # 0xFF0088 - rózsaszín - pink
              # 0x00FF00 - zöld - green
              # 0x00FFFF - cián - cyan
              # 0xFFBB00 - sárga - yellow
      
          ###################################
          ######## avaible methods ##########
          ###################################
      
      		# longsignal(self)
      		# longsignal(self, color)
      		# criticalError(self)
      		# message(self, color)
      		# message(self, color, time)
      		# message(self, color, time, iteration)
      		# okay(self)
      		# startup(self)
      		# shutdown(self)
      		# SOS(self, color)
      
      
      class RGBSignal:
      
          colors = {
              'orange' : 0xFF3300,
              'red' : 0xFF0000,
              'blue' : 0x0000FF,
              'purple' : 0xEE00FF,
              'white' : 0xFFFFFF,
              'pink' : 0xFF0088,
              'green' : 0x00FF00,
              'cyan' : 0x00FFFF,
              'yellow' : 0xFFBB00,
              'off': 0x000000
          }
      
      
          def  __init__(self): #this is how a ctor look like
             pycom.heartbeat(False) 
      
          def startup(self):
      		for k in self.colors:
      			pycom.rgbled(self.colors[k])
      			time.sleep(0.1)
      			pycom.rgbled(self.colors['off'])
      			time.sleep(0.1)
      
          def longsignal(self):
              pycom.rgbled(self.colors['white'])
              time.sleep(1)
              pycom.rgbled(self.colors['off'])
          
          def longsignal(self, color):
              pycom.rgbled(self.colors[color])
              time.sleep(1)
              pycom.rgbled(self.colors['off'])
          
          def criticalError(self):
              for x in range(50):
                  pycom.rgbled(self.colors['red'])
                  time.sleep(0.05)
                  pycom.rgbled(self.colors['off'])
                  time.sleep(0.05)
      
          def message(self, color, time, iteration):
              for x in range(iteration):
                  pycom.rgbled(self.colors[color])
                  time.sleep(time)
                  pycom.rgbled(self.colors['off'])
                  time.sleep(time)
          
          
          def message(self, color):
                  pycom.rgbled(self.colors[color])
                  time.sleep(0.5)
                  pycom.rgbled(self.colors['off'])
      
          def message(self, color, time):
                  pycom.rgbled(self.colors[color])
                  time.sleep(time)
                  pycom.rgbled(self.colors['off'])
      
          def okay(self):
                  pycom.rgbled(self.colors['green'])
                  time.sleep(0.6)
                  pycom.rgbled(self.colors['off'])
      
          def SOS(self, color):
              pycom.rgbled(self.colors[color])
              time.sleep(0.1)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.1)
              pycom.rgbled(self.colors[color])
              time.sleep(0.1)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.1)
              pycom.rgbled(self.colors[color])
              time.sleep(0.1)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.1)
              pycom.rgbled(self.colors[color])
              time.sleep(0.2)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.2)
              pycom.rgbled(self.colors[color])
              time.sleep(0.2)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.2)
              pycom.rgbled(self.colors[color])
              time.sleep(0.2)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.2)
              pycom.rgbled(self.colors[color])
              time.sleep(0.1)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.1)
              pycom.rgbled(self.colors[color])
              time.sleep(0.1)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.1)
              pycom.rgbled(self.colors[color])
              time.sleep(0.1)
              pycom.rgbled(self.colors['off'])
              time.sleep(0.1)
      
          def shutdown(self):
              for x in range(10):
                  pycom.rgbled(self.colors['red'])
                  time.sleep(0.1)
                  pycom.rgbled(self.colors['off'])
                  time.sleep(0.1)
                  pycom.rgbled(self.colors['green'])
                  time.sleep(0.1)
                  pycom.rgbled(self.colors['off'])
      
      1 Reply Last reply Reply Quote 1
      • C Offline
        campbellsinclair
        last edited by campbellsinclair

        Use default values for arguments.

        def message(self, color, time=0.5, iteration=1):
            for x in range(iteration):
                pycom.rgbled(self.colors[color])
                time.sleep(time)
                pycom.rgbled(self.colors['off'])
                time.sleep(time)
        

        This method can be called as:

        message('orange')
        
        message('orange', time=1.4)
        message('orange', 1.4)
        
        message('orange', time=1.4, iteration=6)
        message('orange', 1.4, 6)
        
        message('orange', iteration=6)  # which will use the default value 0.5 for time
        

        Note that this is not identical to your message(self, color) and message(self, color, time) methods because they do not have a time.sleep(time) after the LED is turned off. If that is necessary, use:

        def message(self, color, time=0.5, iteration=1):
            for x in range(iteration):
                pycom.rgbled(self.colors[color])
                time.sleep(time)
                pycom.rgbled(self.colors['off'])
                
                if iteration > 1:
                    time.sleep(time)
        

        This Stack Overflow question has some other solutions
        https://stackoverflow.com/questions/6434482/python-function-overloading

        1 Reply Last reply Reply Quote 2
        • T Offline
          tttadam
          last edited by

          Thank you for the detailed answer!
          This is the best solution what I could find.

          1 Reply Last reply Reply Quote 1

          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