I rarely use Python before. Here are new things I've discovered on Python while learning this machine learning course.
- Modules Classes and Functions
- Output
- Working with Pathname
- Working with Files
- Working with List
- Working with Dictionary
Suppose our Python project is stored in /foo/bar
directory. And this application has one file that servers as the single entry point. We can name this file __main__.py
so we can run this project simply be referencing it's directory path:
$ python /foo/bar
# It's equivalent to this
$ python /foo/bar/__main__.py
We can dynamically import a Python module using the __import__
function:
module_name = "numpy"
__import__(module_name)
In Python we can return multiple values seperated by commas.
def test():
return 100, "foo"
someNumber, someString = test()
In order to import modules from outside of the directory, we need to add that module's directory with sys.path.append
. Suppose we have the following directory structure:
|--foo
| |-- bar.py
|
|-- tools
| |-- speak_yoda.py
If we want to use the speak_yoda.py
module within the bar.py
, we can do the following:
# /foo/bar.py
import os
sys.path.append("../tools")
import speak_yoda
However this won't work if you run the baz.py
file from outside of its foo
directory:
# Works inside the /foo directory.
$ cd /foo
$ python bar.py
# Won't work if it's on its project root directory.
$ python foo/bar.py
To solve this we can append the tools
directory using it's absolute path.
# /foo/bar.py
import os
import sys
# Get the directory name for this file.
current_dirname = os.path.dirname(os.path.realpath(__file__))
# Use the absolute path to the tools directory
tools_path = os.path.abspath(os.path.join(dirname, "../tools"))
sys.path.append(tools_path)
import speak_yoda
To print the emojis or any other unicode characters in Python, we have to declare the encoding type at the top like this:
# coding: utf8
print("😅")
Print Python data structure nicely with configurable indentation:
import pprint
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(people)
Read the os.path
documentation.
We can extract a filename from the given URL:
import os
from urlparse import urlparse
url = "https://example.com/foo.txt"
url_components = urlparse(url)
filename = os.path.basename(url_components.path) # foo.txt
To check if the given file path is exists or not:
import os
is_exists = os.path.isfile("foo.txt")
To create a directory if it's not exists:
import os
import errno
try:
os.makedirs(directory_path)
except OSError, e:
if e.errno != errno.EEXIST:
raise
We can use the urllib
module to download a file in Python. The first argument is the file URL, and the second argument is an optional filename that will be used to save the file.
import urllib
urllib.urlretrieve("https://example.com/foo.txt", "foo.txt")
There's tarfile
module that we can use to deal with Tar file in Python. To extract the tar.gz
file we can use the following code:
import tarfile
# Open the file.
tfile = tarfile.open("foo.tar.gz")
# Extract the file to the given path.
tfile.extractall(path)
We can pass the mode
parameter to the open
method. By default the mode
would be r
—reading mode with transparent compression. There are also other mode options:
r:gz
: Reading mode with gzip compression.r:
: Reading mode without compression.a
: Appending mode without compression.w
: Writting mode without compression.- Checkout other available options in tarfile documentation
Use the for..in
syntax to generate a list of random numbers in one line.
import random
# Initialize internal state of random generator.
random.seed(42)
# Generate random points.
randomNumbers = [random.random() for i in range(0, 10)]
# [0.6394267984578837, 0.025010755222666936, 0.27502931836911926, ...]
The built-in zip
function can pair values from two lists, but it will return a list of tuples. To get a list of value pairs, we can combine it with for..in
:
coordinates = [[x, y] for x,y in zip([5,10,15], [0,1,0])]
# [[5, 0], [10, 1], [15, 0]]
We can easily split a list in Python by specifying the starting index and it's ending index. The ending index is excluded. You can specify a negative index. And both of these indices are optional.
a = [0,1,2,3,4,5]
a[0:3] # 0,1,2
a[1:3] # 1,2
a[2:] # 2,3,4,5
a[:3] # 0,1,2
a[0:-2] # 0,1,2,3
a[-2:] # 4,5
a[:] # 0,1,2,3,4,5
We can easily filter a list by combinning the for..in
and if
syntaxs together.
numbers = range(1,11)
# Filter even numbers only.
[numbers[i] for i in range(0, len(numbers)) if numbers[i] % 2 == 0]
# [2, 4, 6, 8, 10]
We can sort a list in ascending order by calling the sort
method.
people = ["John", "Alice", "Poe"]
people.sort()
print(people) # ["Alice", "John", "Poe"]
Just like its name, we can use filter
to filter our list:
numbers = range(1, 11)
even_numbers = filter(lambda number: number % 2 == 0, numbers)
# [2, 4, 6, 8, 10]
We can break the above statement into two parts:
lambda number: statement
:number
is the variable name we'd like to use to refrence a single item from thenumbers
list. The following statement must evaluate to truthy/falsy value—falsy means the current item will be removed from the final result.numbers
: The second parameter is the list we'd like to filter.
We can use the reduce
function to find the total of particular key in a list of dictionary:
items = [{value:10}, {value:20}, {value:50}]
# Find the total of value key.
totalValues = reduce(lambda total, item: total + item["value"], items, 0) # 80
It can be broken down into 4 parts:
lambda total
: It's the carried/accumulative value that will finally be returned.item: statement
:item
is the name of variable we'd like to use to reference the single item from theitems
list. The statement to execute in order to define the accumulative value for the next iteration.items
: It's the list of item that we would like to "reduce"0
: The last parameter is optional, it's the initial accumulative value for the first iteration.
We can also use reduce
function to find a single item from the list. Here's an example to find the person with the biggest total_payments
within the given list of people dictionary.
people = [
{"name": "John", "total_payments": 100},
{"name": "Alice", "total_payments": 1000},
{"name": "Poe", "total_payments": 800}
]
person_biggest_total_payments = reduce(lambda paid_most, person: person if person["total_payments"] > paid_most["total_payments"] else paid_most, people, { "total_payments": 0 })
# {'name': 'Alice', 'total_payments': 1000}
We can use the itervalues
method to loop through a dictionary:
for person in people.itervalues():
print(person["email_address"])
You can also use the iteritems
if you want to access the key too:
for person in people.iteritems():
print(person[0] + ": " + person[1]["email_address"])
Suppose we want to calculate the total of sallary
key on a people
dictionary:
total_salary = sum([person["salary"] for person in people.itervalues()])