Thursday, February 13, 2020

Project #8 - Stock Tracking Graph

For my 8th project, I made a program that pulls stock data from Yahoo Finance and displays it in a graph. For this example, I chose GOOG which is Googles stock sticker. The data was able to be gathered from Yahoo using the pandas_datareader and the graph was generated using bokeh.plotting.

from pandas_datareader import data
import datetime
from bokeh.plotting import figure, show, output_file

start = datetime.datetime(2015, 11, 1)
end = datetime.datetime(2016, 3, 15)

df = data.DataReader(name="GOOG", data_source="yahoo", start=start, end=end)
date_increase = df.index[df.Close > df.Open]
date_decrease = df.index[df.Close < df.Open]


def inc_dec(c, o):
    if c > o:
        value = "Increase"    elif c < o:
        value = "Decrease"    else:
        value = "Equal"    return value


df["Status"] = [inc_dec(c, o) for c, o in zip(df.Close, df.Open)]
df["Middle"] = (df.Open + df.Close0)/2df["Height"] = abs(df.Close - df.Open)

p = figure(x_axis_type='datetime', width=1000, height=1000, sizing_mode="scale_width")
p.title = "Candlestick Chart"p.grid.grid_line_alpha = 0.3
hours_12 = 12*60*60*1000
p.segment(df.index, df.High, df.index, df.low, color="black")

p.rect(df.index[df.Status == "Increase"], df.Middle(df.Status == "Increase"),       
hours_12, df.Height[df.Status == "Increase"], fill_color="#CCFFFF", line_color="black")

p.rect(df.index[df.Status == "Decease"],  df.Middle(df.Status == "Decrease"),      
 hours_12, df.Height[df.Status == "Decrease"], fill_color="#FF3333", line_color="black")

output_file("CS.html")

The graph came out looking like this:




Wednesday, February 12, 2020

Project #7 - Web Scraper

For my 7th project, I made a program that scrapes information from a webpage and shows it in the terminal.

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.pyclass.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/", 
headers={'User-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'})
c = r.content

soup = BeautifulSoup(c, "html.parser")

all = soup.find_all("div", {"class": "propertyRow"})

all[0].find("h4", {"class": "propPrice"}).text.replace("\n", "").replace(" ", "")


# Pulling data from each listing and printing it in the terminal.
for item in all:
    try:
        print(item.find_all("span", {"class", "propAddressCollapse"})[0].text)
    except:
        print("No Info")

    try:
        print(item.find_all("span", {"class", "propAddressCollapse"})[1].text)
    except:
        print("No Info")

    try:
        print(item.find("h4", {"class", "propPrice"}).text.replace("\n", "").replace(" ", ""))
    except:
        print("No Info")

    try:
        print(item.find("span", {"class", "infoBed"}).find("b").text)
    except:
        print("No Info")

    try:
        print(item.find("span", {"class", "infoValueFullBath"}).find("b").text)
    except:
        print("No Info")

    try:
        print(item.find("span", {"class", "infoValueHalfBath"}).find("b").text)
    except:
        print("No half bath")

    try:
        print(item.find("span", {"class", "infoSqFt"}).find("b").text)
    except:
        print("No Info")

    for column_group in item.find_all("div", {"class": "columnGroup"}):
        for feature_group, feature_name in zip(column_group.find_all("spans", {"class": "featureGroup"}), 
column_group.find_all("span", {"class": "featureName"})):
            print(feature_group.text, feature_name.text)

    print(" ")

Monday, February 10, 2020

Project #6 - Video Motion Tracking

For my 6th project I made a program that tracks motion through a video camera and writes to a graph of when there was motion.

Here is the code for the video motion tracking:
import cv2
import pandas
import time
from datetime import datetime


first_frame = Nonestatus_list = [None, None]
times = []
df = pandas.DataFrame(columns=["Start", "End"])

video = cv2.VideoCapture(0)

while True:

    check, frame = video.read()
    status = 0    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    if first_frame is None:
        first_frame = gray
        continue
    delta_frame = cv2.absdiff(first_frame, gray)

    thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]

    thresh_frame = cv2.dilate(thresh_frame, None, iterations=2)

    (cnts, _) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for contour in cnts:
        if cv2.contourArea(contour) < 10000:
            continue        status = 1
        (x, y, w, h) = cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 3)

    status_list.append(status)

    status_list=status_list[-2:]

    if status_list[-1] == 1 and status_list[-2] == 0:
        times.append(datetime.now())

    if status_list[-1] == 0 and status_list[-2] == 1:
        times.append(datetime.now())

    cv2.imshow("Gray Frame", gray)
    cv2.imshow("Delta Frame", delta_frame)
    cv2.imshow("Threshold Frame", thresh_frame)
    cv2.imshow("Color frame", frame)

    key = cv2.waitKey(1)

    print(gray)
    print(delta_frame)

    if key == ord('q'):
        if status == 1:
            times.append(datetime.now())
        break
print(status_list)
print(times)

for i in range(0, len(times), 2):
    df = df.append({"Start": times[i], "End": times[i+1]}, ignore_index=True)

df.to_csv("Times.csv")

video.release()
cv2.destroyAllWindows()


Here is the code for generating the graph:
from motiontracker import df
from bokeh.plotting import figure, show, output_file
from bokeh.models import HoverTool, ColumnDataSource

df["Start_string"] = df["Start"].dt.strftime("%Y-%m-%d %H:%M:%S")
df["End_string"] = df["End"].dt.strftime("%Y-%m-%d %H:%M:%S")

cds = ColumnDataSource(df)

p = figure(x_axis_type='datetime', height=100, width=500, responsive=True, title="Motion Graph")
p.yaxis.minor_tick_color = Nonep.ygrid[0].ticker.desired_num_ticks = 1
hover = HoverTool(tooltips=[("Start", "@Start_string"), ("End", "@End_string")])

q = p.quad(left="Start", right="End", bottom=0, top=1, color="blue", source=cds)

output_file("Graph.html")
show(p)


Wednesday, February 5, 2020

Project #5 - Image Face Finder

For this project I built a simple but smart program that can recognize faces in images. The program uses CV2 and an xml document to distinguish the faces in any image.

import cv2

face_cascade= cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

img = cv2.imread("People.jpg")

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=20)

for x, y, w, h in faces:
    img = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 3)

print(type(faces))
print(faces)

cv2.imshow("Gray", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here is what the outcome looks like:







Monday, February 3, 2020

Project #4 - OOP Update

I updated the back-end script to conform with the Object Oriented Programming structure.

import sqlite3


class Database:

    def __init__(self, db):
        self.conn = sqlite3.connect("books.db")
        self.cur = self.conn.cursor()
        self.cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text author text, 
year integer, isbn intege)")
        self.conn.commit()

    def insert(self, title, author, year, isbn):
        self.cur.execute("INSERT INTO book VALUES (NULL, ?, ?, ?, ?", (title, author, year, isbn))
        self.conn.commit()

    def view(self):
        self.cur.execute("SELECT * FROM book")
        rows = self.cur.fetchall()
        return rows

    def search(self, title="", author="", year="", isbn=""):
        self.cur.execute("SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?", (title, author, year, isbn))
        rows = self.cur.fetchall()
        return rows

    def delete(self, id):
        self.cur.execute("DELETE FROM book WHERE id=?", (id,))
        self.conn.commit()

    def update(self, id, title, author, year, isbn):
        self.cur.execute("UPDATE book SET title=?, author=?, year=?, isbn=?, (id, title, author, year, isbn)")
        self.conn.commit()


def __dil__(self):
    self.conn.close()

Project #4 - Record Searcher

For my 4th project I built a graphical user interface program that stores book information like title, author, year, and IBSN. The user can view all records, search an entry, add entry, update entry, delete, and close.

Here is the front end script of my code:
from tkinter import *
import backend


def get_selected_row(event):
    try:
        index = list1.curselection()[0]
        global selected_tuple
        selected_tuple = list1.get(index)
        e1.delete(0, END)
        e1.insert(END, selected_tuple[1])
        e2.delete(0, END)
        e2.insert(END, selected_tuple[2])
        e3.delete(0, END)
        e3.insert(END, selected_tuple[3])
        e4.delete(0, END)
        e4.insert(END, selected_tuple[4])
    except IndexError:
        pass

def view_command():
    for row in backend.view():
        list1.insert(END, row)


def search_command():
    list1.delete(0, END)
    for row in backend.search(title_text.get(), author_text.get(), year_text.get(), isbn_text.get()):
        list1.insert(END, row)


def add_command():
    backend.insert(title_text.get(), author_text.get(), year_text.get(), isbn_text.get())
    list1.delete(0, END)
    list1.insert(END, (title_text.get(), author_text.get(), year_text.get(), isbn_text.get()))


def delete_command():
    backend.delete(selected_tuple[0])


def update_command():
    backend.update(selected_tuple[0], title_text.get(), author_text.get(), year_text.get(), isbn_text.get())


window = Tk()


window.wm_title("Book Store")


# Labels Section
l1 = Label(window, text="Title")
l1.grid(row=0, column=0)

l2 = Label(window, text="Author")
l2.grid(row=0, column=2)

l3 = Label(window, text="Year")
l3.grid(row=1, column=0)

l4 = Label(window, text="ISBN")
l4.grid(row=1, column=2)


# Text Entry Windows Section
title_text = StringVar()
e1 = Entry(window, textvariable=title_text)
e1.grid(row=0, column=3)

author_text = StringVar()
e2 = Entry(window, textvariable=author_text)
e2.grid(row=1, column=1)

year_text = StringVar()
e3 = Entry(window, textvariable=year_text)
e3.grid(row=0, column=1)

isbn_text = StringVar()
e4 = Entry(window, textvariable=isbn_text)
e4.grid(row=1, column=3)


# Text Box Window
list1 = Listbox(window, height=6, width=35)
list1.grid(row=2, column=0, rowspan=6, columnspan=2)


# Scrollbar
sb1 = Scrollbar(window)
sb1.grid(row=2, column=2, rowspan=6)

list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

list1.bind('<<ListboxSelect>>', get_selected_row)

# Buttons Section
b1 = Button(window, text="View all", width=12, command=view_command)
b1.grid(row=2, column=3)

b2 = Button(window, text="Search entry", width=12)
b2.grid(row=3, column=3)

b3 = Button(window, text="Add entry", width=12, command=add_command)
b3.grid(row=4, column=3)

b4 = Button(window, text="Update selected", width=12, command=update_command)
b4.grid(row=5, column=3)

b5 = Button(window, text="Delete", width=12, command=delete_command)
b5.grid(row=6, column=3)

b6 = Button(window, text="Close", width=12, command=window.destroy)
b6.grid(row=7, column=3)


window.mainloop()


Here is the back-end script of my code:
import sqlite3


def connect():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text author text, 
year integer, isbn intege)")
    conn.commit()
    conn.close()


def insert(title, author, year, isbn):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("INSERT INTO book VALUES (NULL, ?, ?, ?, ?", (title, author, year, isbn))
    conn.commit()
    conn.close()


def view():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM book")
    rows = cur.fetchall()
    conn.close()
    return rows


def search(title="", author="", year="",isbn=""):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?", (title, author, year, isbn))
    rows = cur.fetchall()
    conn.close()
    return rows


def delete(id):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("DELETE FROM book WHERE id=?", (id,))
    conn.commit()
    conn.close()


def update(id, title, author, year, isbn):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("UPDATE book SET title=?, author=?, year=?, isbn=?, (id, title, author, year, isbn)")
    conn.commit()
    conn.close()


connect()





Tuesday, January 28, 2020

Project #3 - Website Blocker

For my third project I built a program that blocks selective websites during certain times of day. This is accomplished by having the program write to the hosts file. In my example below the website Facebook.com will not open during working hours, and will open on non-working hours.

import time
from datetime import datetime as dt

hosts_path = r"/filepath/hosts"redirect = "127.0.0.1"website_lists = ["www.Facebook.com", "Facebook.com"]

while True:
    if dt(dt.now().year, dt.now().month, dt.now().day, 8) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, 16):
        print("Can't access during working hours...")
        with open(hosts_path, 'r+') as file:
            content = file.read()
            for website in website_lists:
                if website in content:
                    pass                else:
                    file.write(redirect + " " + website + "\n")
    else:
        with open(hosts_path, 'r+') as file:
            content = file.readlines()
            file.seek(0)
            for line in content:
                if not any(website in line for website in website_lists):
                    file.write(line)
            file.truncate()
        print("Non-working hours, have fun...")
    time.sleep(5)

Monday, January 27, 2020

Project #2 - Mapping Volcanos and Populations

My second project is a program that maps all of the volcanoes in the United States. The data is pulled from a .txt file that has the gps coordinates of each volcano. In addition, when the pin that denotes the location is clicked it will show the name and elevation of that volcano. The pins are also colored according to the elevation of the volcano. The name of the volcano is a hyperlink that when click opens a new tab with a Google search of that volcanoes name. I added another layer to the map that overlays each country with a different color based on population size. The data for the population size is pulled from a .json file.

import folium
import pandas

data = pandas.read_csv("Volcanoes.txt")
lat = list(data["LAT"])
lon = list(data["LON"])
elev = list(data["ELEV"])
name = list(data["NAME"])

html = """Volcano name:<br><a href="https://www.google.com/search?q=%%22%s%%22" 
target="_blank">%s</a><br>Height: %s feet"""

def color_producer(elevation):
    if elevation < 1000:
        return 'green'    elif 1000 <= elevation < 3000:
        return 'orange'    else:
        return 'red'

map = folium.Map(location=[38.58, -99.09], zoom_start=5, tiles="Stamen Terrain")

fgv = folium.FeatureGroup(name="Volcanoes")

for lt, ln, el, name in zip(lat, lon, elev, name):
    iframe = folium.IFrame(html=html % (name, name, (el*3.28084)), width=200, height=100)
    fgv.add_child(folium.Marker(location=[lt, ln], popup=folium.Popup(iframe), 
icon=folium.Icon(color=color_producer(el))))

fgp = folium.FeatureGroup(name="Population")

fgp.add_child(folium.GeoJson(data = (open("world.json", "r", encoding = "utf-8-sig").read()),
style_function = lambda x: {"fillColor":"green" if x["properties"]["POP2005"] < 10000000else "orange" 
if 10000000 <= x["properties"]["POP2005"] < 20000000 else "red"}))


map.add_child(fgv)
map.add_child(fgp)
map.add_child(folium.LayerControl())

map.save("Map_html_popup_advanced.html")

Saturday, January 25, 2020

Project #1 - Dictionary Program

Here is the first project for my portfolio.

This is a program that functions as a dictionary.

When a word is typed into the prompt the program will return the definition.

The program is pulling data from a json file.

If the word is typed incorrectly the program will find a closely spelled word match and ask you if it is the word you are looking for. It will ask yes or no. If you type "Y" it will return the definition of the word. If you type "N" it will return a string that says the word does not exist and to double check it.

There are some additional features added, like the program will know to look for words with capitals even if the entered word was not capitalized.

import json
from difflib import get_close_matches

# Loading Data into a Python data type.
data = json.load(open("data.json"))


def translate(w):
    w = w.lower()
    if w in data:
        return data[w]
    elif w.title() in data:  # If user entered "texas" this will check for "Texas" as well.        return data[w.title()]
    elif w.upper() in data:  # In case user enters words like USA or NATO        return data[w.upper()]
    elif len(get_close_matches(w, data.keys())) > 0:
        yn = input("Did you mean %s? Enter Y if yes and N if no: " % get_close_matches(w, data.keys())[0])
        if yn == "Y":
            return data[get_close_matches(w, data.keys())[0]]
        elif yn == "y":
            return data[get_close_matches(w, data.keys())[0]]
        elif yn == "N":
            return "The word doesn't exist. Please double check it."        
        elif yn == "n":
            return "The word doesn't exist. Please double check it."        
        else:
            return "We didn't understand your entry."    
    else:
        return "That word doesn't exist. Please check your spelling."


# User input.
word = input("Enter word: ")
output = translate(word)
if type (output) == list:
    for item in output:
        print(item)

else:
    print(output)

Say Something Program

Here is my second program:

def sentence_maker(phrase):
    interrogatives = ("how", "what", "why")
    capitalized = phrase.capitalize()
    if phrase.startswith(interrogatives):
        return "{}?".format(capitalized)
    else:
        return"{}.".format(capitalized)

results = []
while True:
    user_input = input("Say something: ")
    if user_input == "\end":
        break    else:
        results.append(sentence_maker(user_input))

print(" ".join(results))

When the program is run the console will ask for user input in the form of "Say something: "

If the user types in any sentences that start with the keywords "how, what, or why" the program will add a question mark to the end of the sentence.

Otherwise, if the sentence does not start with any of the keywords then it will insert a period at the end of the sentence.

The the variable "capitalized' will capitalize the first letter of each sentence.

The where clause will have the console repeatedly ask for user input until the user types in "\end".

When sentences are typed into the console they will be strung together into one sting and printed after the program has ended.

Here is an example of what the program will do:

Say something: hello
Say something: how are you going today 
Say something: That's good 
Say something: what are you doing today 
Say something: sounds fun 
Say something: \end Hello. 
How are you going today? That's good. What are you doing today? Sounds fun.



Friday, January 24, 2020

Super Simple Date Time Program

This is my first simple program of the night.

First I started with:

import datetime
print(datetime.datetime.now())

Then I added some text and a variable:

import datetime
dtn = datetime.datetime.now()
print("The date and time is:", dtn)

When run the program will print:

The date and time is: 2020-01-24 13:10:11.983919

Project #8 - Stock Tracking Graph

For my 8th project, I made a program that pulls stock data from Yahoo Finance and displays it in a graph. For this example, I chose GOOG whi...