I719 Fundamentals of Python/lecture5v2

From ICO wiki

Lecture 5

Working with real data.
Datetime, CSV files

Datetime

import datetime as dt

my_datetime = dt.datetime.utcnow()  # get the current date and time (without timezone) as a datetime object

one_day = dt.timedelta(days=1)  # create a duration of 1 day

tomorrow = my_datetime + one_day

yesterday = my_datetime - one_day

tomorrow > yesterday  # True

current_year = my_datetime.year

first_day_of_year = dt.datetime(year=current_year, month=1, day=1)

Task 1

Display the current date like
“2017-05-04”

Calculate How many days since Jan 1.

Optional, requires using timezone:
How many hours are left until midnight today?

import datetime as dt

print(dt.datetime.utcnow().strftime("%Y-%m-%d"))

today = dt.date.today()

first_of_the_year = dt.date(year=2017, month=1, day=1)

print(today - first_of_the_year)


now = dt.datetime.now()
midnight = dt.datetime(year=now.year, month=now.month, day=(now.day + 1))
print(midnight - now)

CSV files

CSV files are a portable and common way of using tabular data. They are easily used in python.

Example: Printing every row of a csv file

Using a hardcoded path to ~/sheet1.csv

"""
Uses a hardcoded path to the file. used when you cannot use the command line to run python. For example if you run the script in an IDE
"""
import csv
from os.path import expanduser, join

home = expanduser("~")
path = join(home, 'sheet1.csv')
csv_file = open(path)

reader = csv.reader(csv_file)
for row in reader:
    print(line)

csv_file.close()

Using the argparse library

"""
Open a file using argparse.
Work when ran from command line
"""
import argparse
import csv

parser = argparse.ArgumentParser()
parser.add_argument('file')
args = parser.parse_args()

csv_file = open(args.file)

reader = csv.reader(csv_file)
for row in reader:
    print(row)

csv_file.close()

Task 2

What was the average price of bitcoin in April?

CSV of historical price data here:
https://blockchain.info/charts/market-price?timespan=1year

Hints

convert timestamps to datetime object

datetime.datetime.strptime("2016-05-11 00:00:00", "%Y-%m-%d 00:00:00")

Example solution

"""
Open a file using argparse.
Work when ran from command line
"""
import datetime as dt
import argparse
import csv


MIN_DATE = dt.datetime(year=2017, month=4, day=1)
MAX_DATE = dt.datetime(year=2017, month=5, day=1)

parser = argparse.ArgumentParser()
parser.add_argument('file')
args = parser.parse_args()
csv_file = open(args.file)
reader = csv.reader(csv_file)

prices_in_april = []

for row in reader:
    raw_price = row[1]
    price = float(raw_price)
    raw_date = row[0]
    date = dt.datetime.strptime(raw_date, "%Y-%m-%d 00:00:00")
    if date >= MIN_DATE and date < MAX_DATE:
        prices_in_april.append(price)

csv_file.close()

print(sum(prices_in_april) / len(prices_in_april))