Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Last active March 14, 2017 06:24
Show Gist options
  • Save prodeveloper/6d991eb671ecc13e969dcbaea687fa17 to your computer and use it in GitHub Desktop.
Save prodeveloper/6d991eb671ecc13e969dcbaea687fa17 to your computer and use it in GitHub Desktop.
Working with libraries

So far we have learned a lot about using the python api. While it is possible to build anything you want, most of the time it makes much more sense to resuse something that already exists.

Python comes prepackaged with a lot of libraries that offer common functionality https://docs.python.org/3/library/

Python's extensive library includes everything from mathematical functions to compression functionality.

For this class we shall be looking at the two most commonly used libraries, OS and Math. We shall also take a look at an important library for working with remote resources urllib.

##Step 1

Suppose you wish to implement the area of a circle. The formula is

    PI * Radius * Radius

In previous cases, we simply assumed that PI was 3.14. We can be more accurate by using python's internal pi.

Lets start by creating the folder for the lesson.

mkdir 8lesson
cd 8lesson
touch area_cirle.py

Inside the python file type in the code for area of circle.

import math

def area_circle(radius):
    radius = int(radius)
    area = math.pi * radius * radius
    return area

user_radius = input("What is the radius of the circle?:")
calculated_area =  area_circle(user_radius)
print("The area of a circle with radius {} is {}".format(user_radius,calculated_area))

You note that now we have used math.pi this gives the constant for pi. You further note that before using an external library you must first import it.

###Class assignment

Implement the following requirement

    As a mathematician, I want to give the script a number and have it give to me it's square root

Hint: Use the math library provided by python

##Step 2

Python can also interact with the operating system in which it is currently running on. You can use this functionality from the OS library.

Suppose that you have this requirement.

    As a developer, I want to know the name of my current folder and the files that are in the current folder

To implement this requirement, let us first create a few dummy files to make this interesting.

touch sample1.txt sample2.txt sample3.txt

When you run that command. Three empty text files will be created.

Now create the python file.

touch file_lister.py

Inside the python file type in the code.

import os
os.system("pwd")
os.system("ls")

What do you see? You note that system calls are run directly so that you don't have to print them out.

###Class assignment

Implement the following requirement

    As a developer, I want the script to generate for me 3 text files named dummy1.txt, dummy2.txt, dummy3.txt

Hint: Use os.system

##Step 3

Libraries can get quite complex. An example is urllib which can fetch content from any publicly available website.

Suppose you have this requirement.

    As an author, I want the system to fetch content from my api https://jsonplaceholder.typicode.com/posts and display it on the screen

We would implement this requirement by first creating the python file

touch blog_fetcher.py

Inside the python file type in

import urllib.request
blog_content = urllib.request.urlopen("https://jsonplaceholder.typicode.com/posts").read()
print(blog_content)

Run the file.

You will note that using libraries generally make your code much easier to read.

###Class assignment

Implement the following requirement

    As the bellringer, I want to get the latest time from my api at http://date.jsontest.com/

Hint: Use urllib

##References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment