What does it mean to put the 'as' command after the 'import' command in the code below?

6

The 'as' is in the send_to_twitter (msg) function

import urllib.request
import time

def send_to_twitter(msg):
    import twitter as t:
    CONSUMER_KEY = '1wrnsF5lB8fEWVzRdvlIqdTle'
    CONSUMER_SECRET = 'eTiylDUHLJgGnTCcxzzCtzHXG4OlHrbY6wLvuZUnAEhrokyNAF'
    ACCESS_KEY = '2325915097-q2JYaZ3UGeL9Pr95BJC7643NMyETY6x7Bb8T1q1'
    ACCESS_SECRET = '8GRq4e9ukVKcC8XjroM3iLKuZYOM2QtFEdCHXG3TXx0zo'
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
    api = tweepy.API(auth)
    api.update_status(msg)
    print(msg)

def get_price():
    page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices-loyalty.html")
    text = page.read().decode("utf8")
    where = text.find('>$')
    start_of_price = where + 2
    end_of_price = start_of_price + 4
    return(text[start_of_price : end_of_price])

escolha = input("Precisa do valor imediatamente? ")

if escolha == 'y':
    send_to_twitter(get_price())
else:
    if escolha == 'n':
        price = 99.99
        while price > 4.74:
            price = float(get_price())
            time.sleep(3)
        send_to_twitter('BUY!')
    else:
        print("Voce nao escolheu nada")
    
asked by anonymous 06.01.2017 / 22:15

3 answers

10

According to in the documentation (en) o as in import twitter as t: in Python means that you are creating an alias for the imported module.

import without the practice of alias:

>>>import math
>>>math.pi
>>>3.141592653589793
etc..

import with practice of alias:

>>>import math as m
>>>m.pi
>>>3.141592653589793
etc..

You can read more at Importing Modules .

Matheus Saraiva's Note:

  

The most practical use of this is that you can give more names   for an imported class or function and use the new name   in its code in place of the original name. I also use it in cases where the   The name of an imported function is a bit big so I create the alias for   not have to type that huge named function every time   need to use it.

    
06.01.2017 / 22:38
6

As @AckLay explained, you can use the as as a nickname for the module imported into Python.

This syntax was proposed by PEP 221 and as explained in that document, it was not intended to be a Python keyword, but it is listed in the keyword list of that language.

Prints the list of keywords in Python 2.7. *:

>>> import keyword
>>> print(keyword.kwlist)
  

as is used to create an alias while importing a module. It means   giving a different name (user-defined) to a module while importing it.

Source: Program - List Of Keywords In Python Programming

    
06.01.2017 / 22:49
0

It means "how"

import os as so

In this case you are speaking "Import module os and change its name to this code as so"

so you can call its methods by the name you gave the module:

os.chdir()

It is useful to give the names you want and also to shorten the name of some modules that may be too large to be typed all the time, for example we can have a module named myModuloPython, that very big name, so we can call it from mp (python module) used the as.

import meuModuloPython as mp
mp.fazalgo()
    
09.01.2017 / 03:17