from getpass import getpass
import os
from devicecloud import DeviceCloud


def get_authenticated_dc():
    while True:
        base_url = os.environ.get('DC_BASE_URL', 'https://login.etherios.com')

        username = os.environ.get('DC_USERNAME', None)
        if not username:
            username = input("username: ")

        password = os.environ.get('DC_PASSWORD', None)
        if not password:
            password = getpass("password: ")

        dc = DeviceCloud(username, password, base_url=base_url)
        if dc.has_valid_credentials():
            print("Credentials accepted!")
            return dc
        else:
            print("Invalid username or password provided, try again")

from getpass import getpass
import os
from six.moves import input
from devicecloud import DeviceCloud


def get_authenticated_dc():
    while True:
        base_url = os.environ.get('DC_BASE_URL', 'https://login.etherios.com')

        username = os.environ.get('DC_USERNAME', None)
        if not username:
            username = input("username: ")

        password = os.environ.get('DC_PASSWORD', None)
        if not password:
            password = getpass("password: ")

        dc = DeviceCloud(username, password, base_url=base_url)
        if dc.has_valid_credentials():
            print("Credentials accepted!")
            return dc
        else:
            print("Invalid username or password provided, try again")

Handle using the input function in python 2 for getting username for examples
Handle using the input function in python 2 for getting username for examples

Previously this used the builtin input function to get the username.  In
python 3 this is fine, but if python 2 this is equivalent to

eval(raw_input(prompt))

and thus tried to evaluate the username as a variable and typically failed.

  from getpass import getpass
  import os
+ from six.moves import input
  from devicecloud import DeviceCloud
  
  
  def get_authenticated_dc():
      while True:
          base_url = os.environ.get('DC_BASE_URL', 'https://login.etherios.com')
  
          username = os.environ.get('DC_USERNAME', None)
          if not username:
              username = input("username: ")
  
          password = os.environ.get('DC_PASSWORD', None)
          if not password:
              password = getpass("password: ")
  
          dc = DeviceCloud(username, password, base_url=base_url)
          if dc.has_valid_credentials():
              print("Credentials accepted!")
              return dc
          else:
              print("Invalid username or password provided, try again")
  
Handle using the input function in python 2 for getting username for examples
## Code Before:
from getpass import getpass
import os
from devicecloud import DeviceCloud


def get_authenticated_dc():
    while True:
        base_url = os.environ.get('DC_BASE_URL', 'https://login.etherios.com')

        username = os.environ.get('DC_USERNAME', None)
        if not username:
            username = input("username: ")

        password = os.environ.get('DC_PASSWORD', None)
        if not password:
            password = getpass("password: ")

        dc = DeviceCloud(username, password, base_url=base_url)
        if dc.has_valid_credentials():
            print("Credentials accepted!")
            return dc
        else:
            print("Invalid username or password provided, try again")

## Instruction:
Handle using the input function in python 2 for getting username for examples
## Code After:
from getpass import getpass
import os
from six.moves import input
from devicecloud import DeviceCloud


def get_authenticated_dc():
    while True:
        base_url = os.environ.get('DC_BASE_URL', 'https://login.etherios.com')

        username = os.environ.get('DC_USERNAME', None)
        if not username:
            username = input("username: ")

        password = os.environ.get('DC_PASSWORD', None)
        if not password:
            password = getpass("password: ")

        dc = DeviceCloud(username, password, base_url=base_url)
        if dc.has_valid_credentials():
            print("Credentials accepted!")
            return dc
        else:
            print("Invalid username or password provided, try again")
