Python

From Dikapedia
Jump to: navigation, search

To quickly test python scripts, you can use AWS Cloud9 IDE, jupiter notebook, or any windows/Linux machine to run python scritps.

Basic Practice:

http://codingbat.com/python

More Mathematical (and Harder) Practice:

https://projecteuler.net/archives

List of Practice Problems:

http://www.codeabbey.com/index/task_list

A SubReddit Devoted to Daily Practice Problems:

https://www.reddit.com/r/dailyprogrammer

A very tricky website with very few hints and touch problems (Not for beginners but still interesting)

http://www.pythonchallenge.com/

CloudEndure Python API example

Complete Python 3 Bootcamp GitHub

Simple Examples


run:

$ python
Python 3.7.10 (default, Jun  3 2021, 00:02:01) 
[GCC 7.3.1 20180712 (Red Hat 7.3.1-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> some_string = "Hello world!"
>>> print(some_string)
Hello World!
>>>


  • A variable is a value that can change. Variable names are defined when you declare them. Variables in python are declared in the format name = value. In python, we can store different types of data without having to be explicit about the data type when we declare it.


Virtual Environments


To create a virtual environment:

Admin:~/environment $ python -m venv my_venv
(my_venv) Admin:~/environment $
  • You will know you are inside the venv when you see the prefix (my_venv).

Once the virtual environment has been created, you need to activate it. Once activated, the code runs inside the env, including any packages you install. To activate on Linux, run:

$ source my_venv/bin/activate
  • To exit from the venv, type deactivate.
(my_venv) Admin:~/environment $ deactivate
Admin:~/environment $


Data Types


The computer needs to know if a variable is a string, number or another type of data. This is to rpevent mistakes like attempting to add a string "word" to a number.

Strings

Shortened to str and refers to anything inside quotes. The quotes can be double or single.

"word"
'word'
  • If you want to use a direct quote in your sentence/string, then use single quotes for the string and double quotes for the actual quote, i.e.:
'The error messages was "Incorrect Data Type"'
  • How to Set str as variable:
>>>first_name = "ardika"
>>>last_name = "Sulistija"
  • How to add strings together using variables:
>>>print(first_name+last_name)
ArdikaSulistija

>>>print(first_name + " " + last_name)
Ardika Sulistija
  • Using Variables in strings: You can use the {} in your string to indicate whether the variable should go. Then use .format(variable_name) after the quotation marks. If you have multiple variables, for each variable you use a {}. In the .format() separate each variable with a comma, i.e: .formate(variable_1, variable_2).
>>>first_name = "Ardika"
>>>last_name = "sulistija"
>>>print("My first name is {}. My family name is {}".format(first_name, last_name))
My first name is Ardika. My last name is sulistija
  • From python version 3.6+, you can do the same as above by using a format called f-strings to include variables in your strings. Choose whichever method you like best.
>>>first_name = "Ardika"
>>>last_name = "sulistija"
>>>print(f"My first name is {first_name}. My family name is {last_name}")
My first name is Ardika. My last name is sulistija
  • New lines and indentation - if you want your text to go over several lines, you can use \n and \t. The \ character is called an escape character.
    • n = put text on next line
    • t = add a tab spacing
>>>string = "This is a string over\nthree lines\n\twith the third line indented"
>>>print(string)
This is a string over
three lines
        with the third line indented
Numbers
  • Integers - an integer is a whole number such as 50 or 1. The data type integer is abbreviated to int.
  • Floating point numbers - a floating point number is a number followed by a decimal point such as 50.5.

Using numbers variables in strings -

  • The below will not work because you will get the error TypeError: can only concatenate str (not "int") to str, because you are trying to use a data type that will not work. In this case you cannot add a string and a number together:
>>>my_int = 10
>>>sentence = "The total is: "
>>>print(sentence + my_int)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
  • Solution: use str() method to convert the variable from an int to a string.
>>>my_int = 10
>>>sentence = "The total is: "
>>>print(sentence + str(my_int))
The total is: 10

Other methods include:

  • str() returns a string object
  • int() returns an integer object
  • float() returns a floating point object
  • bool() a boolean value of True or False


Dictionaries


A dictionary is a way to store related info in key-value pairs. It uses a key as an identifier and a value to store the info. A dictionary when written in python would be like {"first_name":"Ardika"}. A dictionary in python is abbreviated to dict and has the following syntax: {"key":"value"}.


  • To read the value associated with a key, you can do so like:
>>> user = {"first_name":"ardika"}
>>> print(user["first_name"])
ardika
  • If you are going to be adding the contents of a dictionary later, you can declare an empty dictionary in two ways:
customer_details = {}

or

customer_details = dict()
  • Dictionaries are mutable, meaning they can be changed after you create them. You can add, update, or delete the key-value pairs in a dictionary. To add an additional key-value to a dictionary, provide the dictionary name, the new key in [] and a value after an = sign.
>>> user["family_name"] = "Sulistija"
>>> print(user)
{'first_name': 'ardika', 'family_name': 'Sulistija'}
  • To modify a value in a similar way to adding it, provide the new value after the = sign:
>>> user["family_name"] = "Whoopty"
>>> print(user)
{'first_name': 'ardika', 'family_name': 'Whoopty'}
  • To remove a key-value pair you use the del statement with the name of the dictionary and the key you want to delete:
>>> del user["family_name"]
>>> print(user)
{'first_name': 'ardika'}


Dictionaries are NOT immutable. Meaning, the element inside of a dictionary CAN be reassigned.


Lists


A list is an ordered sequence of values separated by spaces like [0,1,2,3] or ["pizza","icecream","candy"]. It can contain other objects such as dictionaries: [{"fruit_type":"oranges"},{"number":10}].

  • Lists can be created by assigning the values you want to store in a list to a variable:
>>> food = ["pizza","icecream","candy"]
  • If you are going to be adding the contents of the list later, you can declare an empty list in two ways:
food = []

or use the list() constructor:

food = list()
  • Objects stored in a list are given an index number starting at 0. To read an element from a list you use the index number of the stored value.
>>> food = ["pizza","icecream","candy"]
>>> print(food[2])
candy
  • If you want to know the length of a list, use len():
>>> food = ["pizza","icecream","candy"]
>>> len(food)
3
  • To return the last value in a list or work backwards from the last item, use a negative index value:
>>> food = ["pizza","icecream","candy"]
>>> print(food[-3])
pizza
>>> print(food[-1])
candy
  • Lists are mutable, meaning they can be changed after you create them. You can add, update, delete and reorder elements in a list. You can use append() to add an alement to the end of the list:
>>>food.append("chips")
>>>print(food)
['pizza', 'icecream', 'candy', 'chips']
  • If you want to add an element at a specific point in the list you can use the index value with the insert() method.
>>>food.insert(2, "cake")
>>>print(food)
['pizza', 'icecream', 'cake', 'candy', 'chips']
  • You can remove elements using the del statement.
>>> print(food)
['pizza', 'icecream', 'chips', 'candy', 'cake']
>>> del food[1]
>>> print(food)
['pizza', 'chips', 'candy', 'cake']
  • If you use del the element is deleted, so you can no longer use it. If you want to use the value after removing it from a list use the pop() method. To use this method, you need to store the value you have removed from the list inside another variable.
>>> favorite_food = food.pop()
>>> print(favorite_food)
cake
  • In the above example, notice it has returned the last element in the list, which is default behavior of pop(). You can return any element with the pop() by usng the index value.
>>> fresh_food = food.pop(1)
>>> print(fresh_food)
chips
  • The elements in a list are not sorted automatically. If you want to return information in a sorted format, you can use the sorted() function. However, keep in mind this will retian the original order of the list and will not alter it permanently:
>>>print(sorted(food))
['cake', 'candy', 'chips', 'icecream', 'pizza']
>>>print(food)
['pizza', 'icecream', 'cake', 'candy', 'chips']
  • If you don't know th eindex position, or you dont want to remove the last item, you can use the remove() method to specify the specific value you want to remove:
>>> food.remove("candy")
>>> print(food)
['pizza']
  • If you want to permanently sort the list, you can use sort() method:
>>> food.sort()
>>> print(food)
['cake', 'candy', 'chips', 'icecream', 'pizza']
  • To reverse the order, use the reverse() method. This will permanently reverse the order:
>>> print(food)
['cake', 'candy', 'chips', 'icecream', 'pizza']
>>> food.reverse()
>>> print(food)
['pizza', 'icecream', 'chips', 'candy', 'cake']




Determining Type


You may occasionally run into a TypeError. This can be annoying. The first step to fixing it is to find out what type of data python thinks the object is. The way to findo ut is to use the type() method like so:

>>> my_variable = "A string"
>>> print(type(my_variable))
<class 'str'>

Once you know the data type, you can fix it by explicitly state how ptyhon should treat the data. Here is an example of a TypeError:

>>> mynumber = 10
>>> some_string = "The number is "
>>> print(some_string + mynumber)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

Solution:

>>> print(some_string + str(mynumber))
The number is 10


Python and AWS


To work with AWS you will need to install the Boto3 package, which is AWS SDK for python.

$ pip install boto3

A python package is a code compromising of individual components called modules that we can use in our code. Packages save us from having to write every aspect of our program from scratch by allowing us to use other people's code. By importing packages into our program we can use the features of that package. Python has common packages that are included, others have to be installed into our virtual environment using the python package manager pip.

  • You can see what packages have installed by running:
$ pip freeze
boto3==1.20.27
botocore==1.23.27
jmespath==0.10.0
python-dateutil==2.8.2
s3transfer==0.5.0
six==1.16.0
urllib3==1.26.7
  • When you use pip freeze you see all the packages installed into your virtual env. We need to be able to recreate the set of packages when the code is reused elsewhere. For example, giving the code to someone else or when running it in a different env. To accomplish this pip uses a python_requirements.txt file. This will contain a list of all the packages and versions you need to install the new env. You can create a python_requirements.txt file by running:
(my_venv) Admin:~/environment $ pip freeze > python_requirements.txt
(my_venv) Admin:~/environment $ cat python_requirements.txt 
boto3==1.20.27
botocore==1.23.27
jmespath==0.10.0
python-dateutil==2.8.2
s3transfer==0.5.0
six==1.16.0
urllib3==1.26.7
  • To install a python_requirements.txt file in a new virtual env, type the following command in the new venv to install all the same packages and dependencies from that file:
(my_venv) Admin:~/environment $ pip install -r python_requirements.txt
Requirement already satisfied: boto3==1.20.27 in ./my_venv/lib/python3.7/site-packages (from -r  python_requirements.txt (line 1)) (1.20.27)
Requirement already satisfied: botocore==1.23.27 in ./my_venv/lib/python3.7/site-packages (from -r python_requirements.txt (line 2)) (1.23.27)
Requirement already satisfied: jmespath==0.10.0 in ./my_venv/lib/python3.7/site-packages (from -r python_requirements.txt (line 3)) (0.10.0)
Requirement already satisfied: python-dateutil==2.8.2 in ./my_venv/lib/python3.7/site-packages (from -r python_requirements.txt (line 4)) (2.8.2)
Requirement already satisfied: s3transfer==0.5.0 in ./my_venv/lib/python3.7/site-packages (from -r python_requirements.txt (line 5)) (0.5.0)
Requirement already satisfied: six==1.16.0 in ./my_venv/lib/python3.7/site-packages (from -r python_requirements.txt (line 6)) (1.16.0)
Requirement already satisfied: urllib3==1.26.7 in ./my_venv/lib/python3.7/site-packages (from -r python_requirements.txt (line 7)) (1.26.7)


Functions


A function is a section of a program which performs a specific task, and has a name associated with it. Functions add flexibility to code like variables because they are reusable, which reduces the amount of code you have to write repeatedly.

You can declare a function using the syntax def function_name().

A function begins with def and has lower case characters for the name, with words separated by underscores, a set of {} which contain parameters and ends in :

# This is a function which will print "hello world"
def hello_world():
    print('hello world')

# This line calls (runs) the function:
hello_world()

To create the function, create a new file with the name ending in .py. In this case, we created a file called "lab_1_hello_world.py" with the contents above. The py on the end denotes that it is a python file. To run the program, enter the following command in the terminal:

(my_venv) Admin:~/environment $ python lab_1_hello_world.py
hello world


By default the info remains contained within the boundary of the function. If you want to pass the info to other parts of your code, you need to use return. The value the function returns is called the return value and it is passed back to the line which called the function.

# This is a function which will print "hello world"
def hello_world():
    return 'hello world'
    
# This line calls (runs) the function:
greeting = hello_world()
print(greeting)


Example Lab 1: How to use Amazon Translate to translate text from English to French using Python and Boto3

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/translate.html

import boto3

client = boto3.client('translate')

# declare the function using def, name, braces for parameters and a colon:
def translate_text(): 
    response = client.translate_text(
    
        # assigning the value of the string to the variable 'Text':
        Text='I am learning to code using Python in AWS Cloud9 IDE', 
        
        # we are using a two letter language code from the documentation (en = english)
        SourceLanguageCode='en',
        
        # we use a second two letter language code from the doc (fr = french)
        TargetLanguageCode='fr'
    )
    
# this line will print the contents of the variable 'response'
    print(response)
    
# this line will call our function. Without it, python will not do anything
translate_text()
  • Output:
$ python lab2_boto3.py 
{'TranslatedText': "J'apprends à coder en Python dans l'IDE AWS Cloud9", 'SourceLanguageCode': 'en', 'TargetLanguageCode': 'fr', 'ResponseMetadata': {'RequestId': '8703bf0d-8663-4c2f-8ab6-db7fd3d3dd69', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '8703bf0d-8663-4c2f-8ab6-db7fd3d3dd69', 'cache-control': 'no-cache', 'content-type': 'application/x-amz-json-1.1', 'content-length': '124', 'date': 'Tue, 04 Jan 2022 00:51:03 GMT'}, 'RetryAttempts': 0}}


Example Lab 2: How to use the main() function
  • We will use the code from lab 1 to explain this section.

The main() function sets the entry point for a python program. A python program will run line by line, but it won't run the functions until it comes to a line that calls it.

In other words, it sets the start point of your code to control the order in which your code executes. It is conventional to include all the calls to your functions within the main() function. This will help others to read your code and understand the logic and flow.

import boto3 

client = boto3.client('translate')

# declare the function using def, name, braces for parameters and a colon:
def translate_text(): 
    response = client.translate_text(
    
        # assigning the value of the string to the variable 'Text':
        Text='I am learning to code using Python in AWS Cloud9 IDE', 
        
        # we are using a two letter language code from the documentation (en = english)
        SourceLanguageCode='en',
        
        # we use a second two letter language code from the doc (fr = french)
        TargetLanguageCode='fr'
    )
    
# this line will print the contents of the variable 'response'
    print(response)
    
def main():
 translate_text()

if __name__=="__main__":
    main()
  • Output:
my_venv) Admin:~/environment $ python lab3_main_function.py 
{'TranslatedText': "J'apprends à coder en Python dans l'IDE AWS Cloud9", 'SourceLanguageCode': 'en', 'TargetLanguageCode': 'fr', 'ResponseMetadata': {'RequestId': '33b896a3-5f39-4312-9fc7-12dfc883b91b', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '33b896a3-5f39-4312-9fc7-12dfc883b91b', 'cache-control': 'no-cache', 'content-type': 'application/x-amz-json-1.1', 'content-length': '124', 'date': 'Tue, 04 Jan 2022 00:59:41 GMT'}, 'RetryAttempts': 0}}
  • So what did python actually do?

1) the python interpreter created a special variable called __name__ and assigned the code in this file a name of "__main__".

2) It then checked the if statement and if the __name__=="__main__" it called a function called main()

3) When it ran the main() function it found that it contained one call to another function called translate_text()


Arguments and Parameters


Arguments are used to pass values between programs, subroutines, or functions. When an arugment is used to customize a program, it is called a parameter.

*args and **kwargs: https://www.geeksforgeeks.org/args-kwargs-python/

  • *args - allows us to treat it as a tuple of parameters that are coming in.
def myfunct(*args):
    print(args)
    return sum(args) * 0.05
myfunc(40,60)

Result = 
(40, 60)
5.0
  • **kwargs - allows us to treat it as a dictionary for all the parameters coming.
def myfunc(**kwargs):
    print(kwargs)
myfunc(fruit='apple',veggie='lettuce')

Result: {'fruit': 'apple', 'veggie': 'lettuce'}


def myfunc(**kwargs):
    if 'fruit' in kwargs:
        print('My fruit of choice is {}'.format(kwargs['fruit']))
    else:
        print('I did not find any fruite here')
myfunc(fruit='apple')

Result: My fruit of choice is apple
myfunc(fruit='apple',veggie='lettuce')

Result: My fruit of choice is apple


Example Prompt

Define a function called myfunc that takes in an arbitrary number of arguments, and returns a list containing only those arguments that are even.

Remember, don't run the function, simply provide the definition.

To give an idea what the function would look like when tested:

myfunc(5,6,7,8)

  1. Output: [6, 8]
  • Answer:
def myfunc(*args):
    even_numbers=[]
    
    for number in args:
        if number % 2 == 0:
            even_numbers.append(number)
        else:
            pass
        
    return even_numbers
   
myfunc(2,3,4,5)
  • OUTPUT:
[2, 4]
Using *args and **kwargs in a Combo
def myfunc(*args,**kwargs):
    print(args)
    print(kwargs)
    print('I would like {} {}'.format(args[0],kwargs['food']))
myfunc(10,20,30,fruit='orange',food='eggs',animal='dog')

Result:
(10, 20, 30)
{'fruit': 'orange', 'food': 'eggs', 'animal': 'dog'}
I would like 10 eggs


Example lab 3:
import boto3

# We are now defining the positional arguments in the ()
def translate_text(text, source_language_code, target_language_code): 
    client = boto3.client('translate')
    response = client.translate_text(
    
        # Remove the hard coded value
        Text=text,
        
        # we are now using the positional argument instead of a hardcoded value
        SourceLanguageCode=source_language_code,
        TargetLanguageCode=target_language_code
    )
    
# this line will print the contents of the variable 'response'
    print(response)
    
def main():
 translate_text('I am learning to code in AWS','en','fr')
 
if __name__=="__main__":
    main()


Example Lab 4: Modifying the function to use keyword arguments
import boto3

# We are now defining the positional arguments in the ()
def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    
# this line will print the contents of the variable 'response'
    print(response)
    
def main():
    translate_text(Text='I am learning to code in AWS',SourceLanguageCode='en',TargetLanguageCode='fr')
 
if __name__=="__main__":
    main()
  • So what did python do?

1) We replaced the positional arguements with keyword arguments by using the **kwargs.

2) We removed all the parameters such as Text=text which shortens our code and replaced it with response = client.translate_text(**kwargs).

3) We defined the keyword arguments when we called the function using the syntax Text='I am learning to code in AWS'


Example lab 5: AWS will return a lot of info in the dictionary "key":"value" format. We can use these like so:
import boto3

# We are now defining the positional arguments in the ()
def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    
# this line will print the contents of the variable 'response'
    print(response)
    
kwargs={
    "Text":"I am learning to code in AWS",
    "SourceLanguageCode":"en",
    "TargetLanguageCode":"fr"
    }

def main():
    translate_text(**kwargs)
 
if __name__=="__main__":
    main()
  • Python used the key:value pairs defined in the dictionary in place of the keyword arguments. Being able to pass a dictionary of key:value pairs rather than keyword pairs is useful because in AWS Inputs to programs often use dictionaries.


Map and Filter functions, and Lambda Expressions


  • Map() is a built-in function that works as an iterator to return a result after applying a function to every item of an iterable (tuple, lists, etc.). It is used when you want to apply a single transformation function to all the iterable elements. The iterable and function are passed as arguments to the map in Python.
  • Filter() is a built-in function that allows you to process an iterable and extract those items that satisfy a given condition. This process is commonly known as a filtering operation. In other words, the filter() function selects elements from an iterable (list, tuple etc.) based on the output of a function.
  • Lambda expressions is also known as an anonymous function, and the reason for that is because it's some functionality that you intend to only use one time. Because of that, we don't actually give the lambda expression/function a name, nor do we use the 'def' keyword. So instead of a name or the 'def' keyword, we replace that with the keyword 'lambda'.
Map()

Let's say we have a list of numbers and we want to apply the square() function to every single number in my list (below). One thing we can do to achieve that is to use a 'for-loop'... But that has a lot of code. Instead we can use the MAP function.

def square(num):
    return num**2 

my_nums = [1,2,3,4,5]

list(map(square,my_nums))
 
# OUTPUT:
[1, 4, 9, 16, 25]
  • another example of map using strings/names:
def splicer(mystring):
    if len(mystring)%2 == 0:
        return 'even'
    else:
        return mystring[0] 

names = ['andy','ardika','eve','sally'] 

list(map(splicer,names)) 

# OUTPUT:
['even', 'even', 'e', 's']
  • Notice how we are passing 'square' or 'splicer'... we're actually not "calling" them to execute inside this map. Map will execute them by itself. So you don't use/add-in the () parenthesis, instead you just pass in the function itself as the argument.
Filter()

This is an example of the filter function:

def check_even(num):
    return num%2 == 0

mynums = [1,2,3,4,5,6] 

list(filter(check_even,mynums)) 

# OUTPUT:
[2, 4, 6]
Lambda Expressions

You should really only use Lambda expressions when you can still easily read it.

If you were to come back to your code later and as you get more and more expreience with Python, you will be able to quickly realize what a Lambda expression is doing. But in the beginning it's going to be a bit tricky at first. So if your ever struggle of trying to convert a normal function to a Lambda expression, just try using a normal function instead.

So let's turn the 'square()' function from above into a Lambda expression.

Python will allow you to write functions like this, even though it is not the conventional way:

def square(num): return num ** 2 

square(2) 

# OUTPUT:
4

Remember, a Lambda expression is also known as an anonymous function which you only intend on using once. So the above can be changed to the following Lambda expression:

lambda num: num ** 2

Example:

square = lambda num: num ** 2

square(2)

# OUTPUT:
4

A lot of times you're not really going to be using <name> = to some lambda expression like above. Instead you're going to be using it in conjunction with other functions such as 'map' and 'filter'.

Earlier when we were using 'map' we were passing in the 'square()' function and applying it to every item in the list (mynums). However, this required us to write/define the square() function fully. This is the lambda way of writing it:

list(map(lambda num: num ** 2, mynums))

# OUTPUT:
[1, 4, 9, 16, 25, 36]

This is an example of using the 'filter' function with a lambda expression.

list(filter(lambda num: num % 2 == 0, mynums))

# OUTPUT:
[2, 4, 6]
  • Another example:
names = ['andy','ardika','eve','sally']

list(map(lambda name:name[0],names))

# OUTPUT:
['a', 'a', 'e', 's']
list(map(lambda name:name[::-1],names))

# OUTPUT:
['ydna', 'akidra', 'eve', 'yllas']



Inputs


This section shows how to pass info to your program as inputs from external sources.

The input() function: this will prompt the user with a message and then wait for them to provide input.

Input always accepts things as a string.

Code:

import boto3

# We are now defining the positional arguments in the ()
def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    
# this line will print the contents of the variable 'response'
    print(response)
    
text = input("Provide the text you want to translate: ")
source_language_code = input("Provide the two letter source language code: ")
target_language_code = input("Provide the two letter target language code: ") 

def main():
    translate_text(
    Text=text,
    SourceLanguageCode=source_language_code,
    TargetLanguageCode=target_language_code
    )
 
if __name__=="__main__":
    main()
  • Output:
(my_venv) Admin:~/environment $ python lab_5_console_input.py 
Provide the text you want to translate: hello
Provide the two letter source language code: en
Provide the two letter target language code: fr
{'TranslatedText': 'bonjour', 'SourceLanguageCode': 'en', 'TargetLanguageCode': 'fr', 'ResponseMetadata': {'RequestId': '321d8d7d-453b-41ad-8a2f-ae76f194f3c7', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '321d8d7d-453b-41ad-8a2f-ae76f194f3c7', 'cache-control': 'no-cache', 'content-type': 'application/x-amz-json-1.1', 'content-length': '80', 'date': 'Tue, 04 Jan 2022 01:30:02 GMT'}, 'RetryAttempts': 0}}


CLI Example

This is how you pass AWS CLI arguments to a python script:

import argparse
import boto3


parser = argparse.ArgumentParser(description="Provides translation between one source language and another of the same set of languages.")

parser.add_argument(
    '--text',
    dest="Text",
    type=str,
    help="The text to translate. The text string can be a maximum of 5,000 bytes long. Depending on your character set, this may be fewer than 5,000 characters",
    required=True
    )
    
parser.add_argument(
    '--source-language-code',
    dest="SourceLanguageCode",
    type=str,
    help="The language code for the language of the source text. The language must be a language supported by Amazon Translate.",
    required=True
    )

parser.add_argument(
    '--target-language-code',
    dest="TargetLanguageSource",
    type=str,
    help="The language code for the language of the target text. The language must be a language supported by Amazon Translate.",
    required=True
    )
    
args = parser.parse_args()
     

# We are now defining the positional arguments in the ()
def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    print(response)
     

def main():
    translate_text(**vars(args))
 
if __name__=="__main__":
    main()
# Error if you pass no arguments:
(my_venv) Admin:~/environment $ python lab5_step2_cli_arguments.py 
usage: lab5_step2_cli_arguments.py [-h] --text TEXT --source-language-code
                                   SOURCELANGUAGECODE --target-language-code
                                   TARGETLANGUAGESOURCE
lab5_step2_cli_arguments.py: error: the following arguments are required: --text, --source-language-code, --target-language-code

# How to pass arguments:
(my_venv) Admin:~/environment $ python lab5_step2_cli_arguments.py --text "Hello my name is Ardika" --source-language-code "en" --target-language-code "fr"
{'TranslatedText': "Bonjour je m'appelle Ardika", 'SourceLanguageCode': 'en', 'TargetLanguageCode': 'fr', 'ResponseMetadata': {'RequestId': '50a6f99f-9f9e-405b-b690-89fc7a4c63d6', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '50a6f99f-9f9e-405b-b690-89fc7a4c63d6', 'cache-control': 'no-cache', 'content-type': 'application/x-amz-json-1.1', 'content-length': '100', 'date': 'Tue, 04 Jan 2022 17:05:30 GMT'}, 'RetryAttempts': 0}}


Input from an external text file example

How to format the code to get input from an external file:

with open(filename, 'r') as varibale_name:
     <Do sp,etjomg with the variable here>

Example:

def open_input(file):
    with open(file, 'r') as f:
        text = f.read() # we use read() to read the actual contents of the file
        print(text)
        
def main():
    open_input("text.txt")
    
if __name__=="__main__":
    main()


Json Inputs

JSON = Javascript object Notation. It is a common format for computer programs to exhcange information in using APIs. When we convert Python to JSON there is a conversion. Thus in order to do the conversion we use the json package, which is a standard python package so you don't have to install it.

json.load() & json.dump() - Use to input and output json from files and into files.

json.loads() & json.dumps() - Use to input and output json from strings and into strings.

import json 

json_string = """
{
    "Input":[
        {
        "Text":"Learning how to code python is super fun",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr",
        "Required": true
        }
    ]
}
"""

def main():
    json_input = json.loads(json_string)
    print(json_input)
    
if __name__=="__main__":
    main()

Output:

(my_venv) Admin:~/environment $ python lab5_step4_json_input.py 
{'Input': [{'Text': 'Learning how to code python is super fun', 'SourceLanguageCode': 'en', 'TargetLanguageCode': 'fr', 'Required': True}]}


Loops


We can use break, continue, and pass statements in our loops to add additional functionality for various cases. For example, if you don't necessarily want to run something you can use 'pass', as a placeholder, so you don't get error.

The three statements are defined by:

  • break - Breaks out of the current closest enclosing loop
  • continue - Goes to the top of the closest enclosing loop
  • pass - Does nothing at all.


Here is a good example of the difference between break, continue, and pass: https://www.knowledgehut.com/blog/programming/break-and-continue-statement

For Loops

SYNTAX OF A FOR LOOP

my_iterable = [1,2,3]

# item_name is a variable name is a placeholder for every single item in your variable object (numbers)
for item_name in my_iterable:
    print(item_name)

Output:
1
2
3

For Loops + Tuples

mylist = [(1,2),(3,4),(5,6)] # You have a list, that has tuples as items in the list

for item in mylist:
    print(item)

Output:
(1, 2)
(3, 4)
(5, 6)
# You can unpack tuples like this:
mylist = [(1,2),(3,4),(5,6)] # You have a list, that has tuples as items in the list

for (a,b) in mylist:
    print(a)
    print(b)

output:
1
2
3
4
5
6
mylist = [(1,2,7),(3,4,9),(5,6,13)]

for a,b,c in mylist:
    print(c)

Output:
7
9 
13

For Loops + Dictionaries

# FOR LOOPS ON DICTIONARIES
d = {'K1':1, 'K2':2, 'K3': 3}

for item in d:
    print(item)
    
# Notice how it only iterates through the keys
Output:
K1
K2
K3
  • If you want to iterate through the items themselves, call '.items'
d = {'K1':1, 'K2':2, 'K3': 3} 

for item in d.items():
    print(item)
 
Output:
('K1', 1)
('K2', 2)
('K3', 3)
  • Now combine it with unpacking tuples!
d = {'K1':1, 'K2':2, 'K3': 3} 

for key,value in d.items():
    print(value)

Output:
1
2
3

  • NOTE when iterating through dictionaries, there's no guarantee that you will get a response/results in the order that you put them in.

How to create a simple loop:

>>> food = ["pizza","icecream","candy"]
>>> for dinner in food:
...      print(f'I am going to eat {dinner} for dinner')
... 
I am going to eat pizza for dinner
I am going to eat icecream for dinner
I am going to eat candy for dinner

Simple loop with numbers:

>>> numbers = [0,2,4,6,8,10]
>>> for even_number in numbers:
...     print(f'{even_number} is an even number')
... 
0 is an even number
2 is an even number
4 is an even number
6 is an even number
8 is an even number
10 is an even number

But what if you wanted to count to 10,000? An easier way is to use the range() function:

>>> for number in range(10000):
...     print(f'Counted {number}')
... 
Counted 0
Counted 1
Counted 2
.
[output truncated]
. 
Counted 9997
Counted 9998
Counted 9999

How to count starting at 1:

>>> for number in range(1,10):
...     print(f'Counted {number}')
... 
Counted 1
Counted 2
Counted 3
Counted 4
Counted 5
Counted 6
Counted 7
Counted 8
Counted 9

>>> for number in range(5,10):
...     print(f'Counted {number}')
... 
Counted 5
Counted 6
Counted 7
Counted 8
Counted 9

How to increment the counter by more than the default value of 1. For example, what if you only wanted odd numbers or even numbers. To do this you use a third parameter in the range() function like so:

  • If you want to increment by a value other than 1, the range() function will need THREE parameters otherwise it won't work. I.e. range(0,10,2) works but range(10,2) will not.
>>> for number in range(1,10,3):
...     print(f'Counted {number}')
... 
Counted 1
Counted 4
Counted 7

>>> for number in range(1,10,2):
...     print(f'Counted {number}')
... 
Counted 1
Counted 3
Counted 5
Counted 7
Counted 9


While Loops

Syntax:

while some_boolean_condition:
   #do something
else:
   # do something different

Example:

x = 0 

while x <5:
    print(f'The current value of x is {x}')
    x = x + 1

Output:
The current value of x is 0
The current value of x is 1
The current value of x is 2
The current value of x is 3
The current value of x is 4





Tuples


Tuples are very similar to lists. However, they have one key difference: immutability.

Once an element is inside a tuple, it cannot be reassigned.

Tuples use parenthesis: (1,2,3)


If Statements


  • Equivalence - To check if two values are equivalent, you use the == sign. If it is equal, then it will return true, if not, then it will return false.


How to Read a Python Traceback


https://realpython.com/python-traceback/#what-is-a-python-traceback


ModuleNotFoundError: No module named...


Sometimes you may get an error like this when running a python script:

Traceback (most recent call last):
  File "/Users/ardikas/Documents/ce_syseng/updateSMEWiki.py", line 2, in <module>
    import requests_negotiate
ModuleNotFoundError: No module named 'requests_negotiate'

A good way to check is to run pip list to see what modules are there:

pip3 list
pip list

If it is not there, you can try installing the module like so:

  • Option A:
pip3 install requests_negotiate
  • Option B if the above still doesn't work (this one usually works for me):
python3 -m pip install requests_negotiate