ejmurray
ejmurray
Reputation Top 1%
Ernest Murray
10 Snippets  (96th place)
Published
1 Channel
Created
1 Channel
Following
240 points  (127th place)
Reputation
Serious Code Generator
Junior Popular Coder
Junior Autobiographer
Senior Autobiographer
Junior Famous Coder
Serious Famous Coder
Serious Wise Coder
Serious Famous Coder

Recent Snippets See all snippets by ejmurray

public by Ernest Murray  3226  4  3  0

scrape the BBC sport website.

scrape the BBC sport website.: sportScrape.py
#!/usr/bin/env python
# encoding: utf-8
# author: Ernest
# created: 03/02/2016
# http://goo.gl/pXfFe1

"""
Description
Return the scores and table for a given league using BS4. Change the url to get the given league.

premier-league
championship
spanish-la-liga
"""

from bs4 import BeautifulSoup
from urllib.request import urlopen
import csv

url = 'http://www.bbc.com/sport/football/premier-league/results'
page = urlopen(url).read()
soup = BeautifulSoup(page, "html.parser")

def has_class_but_no_id(tag):
    return tag.has_attr('score')

writer = csv.writer(open("webScraper.csv", "w"))


for match in soup.find_all('td', class_='match-details'):
    home_tag = match.find('span', class_='team-home')
    home = home_tag and ''.join(home_tag.stripped_strings)
    score_tag = match.find('span', class_='score')
    score = score_tag and ''.join(score_tag.stripped_strings)
    away_tag = match.find('span', class_='team-away')
    away = away_tag and ''.join(away_tag.stripped_strings)
    if home and score and away:
        print(home, score, away)

url2 = "http://www.bbc.com/sport/football/spanish-la-liga/table"
soup = BeautifulSoup(urlopen(url2).read(), "html.parser")
for row in soup("table", {"class": "table-stats"})[0].tbody("tr"):
    tds = row("td")
    print(tds[1].contents[2].string, tds[2].string)

;

public by ejmurray  249979  16  3  0

Photo management script. This script will copy photos from "~/Pictures/iPhone Incoming" into a tree the script creates, with folders representing month and years, and photo names timestamped. Completely based on the work of the amazing Dr. Drang; se...

Photo management script. This script will copy photos from "~/Pictures/iPhone Incoming" into a tree the script creates, with folders representing month and years, and photo names timestamped. Completely based on the work of the amazing Dr. Drang; see here: http://www.leancrew.com/all-this/2013/10/photo-management-via-the-finder/ You can see mor
#!/usr/bin/python

import sys
import os, shutil
import subprocess
import os.path
from datetime import datetime

######################## Functions #########################

def photoDate(f):
  "Return the date/time on which the given photo was taken."

  cDate = subprocess.check_output(['sips', '-g', 'creation', f])
  cDate = cDate.split('\n')[1].lstrip().split(': ')[1]
  return datetime.strptime(cDate, "%Y:%m:%d %H:%M:%S")


###################### Main program ########################

# Where the photos are and where they're going.
sourceDir = os.environ['HOME'] + '/Pictures/iPhone Incoming'
destDir = os.environ['HOME'] + '/Pictures/iPhone'
errorDir = destDir + '/Unsorted/'

# The format for the new file names.
fmt = "%Y-%m-%d %H-%M-%S"

# The problem files.
problems = []

# Get all the JPEGs in the source folder.
photos = os.listdir(sourceDir)
photos = [ x for x in photos if x[-4:] == '.jpg' or x[-4:] == '.JPG' ]

# Prepare to output as processing occurs
lastMonth = 0
lastYear = 0

# Create the destination folder if necessary
if not os.path.exists(destDir):
  os.makedirs(destDir)
if not os.path.exists(errorDir):
  os.makedirs(errorDir)

# Copy photos into year and month subfolders. Name the copies according to
# their timestamps. If more than one photo has the same timestamp, add
# suffixes 'a', 'b', etc. to the names. 
for photo in photos:
  # print "Processing %s..." % photo
  original = sourceDir + '/' + photo
  suffix = 'a'
  try:
    pDate = photoDate(original)
    yr = pDate.year
    mo = pDate.month

    if not lastYear == yr or not lastMonth == mo:
      sys.stdout.write('\nProcessing %04d-%02d...' % (yr, mo))
      lastMonth = mo
      lastYear = yr
    else:
      sys.stdout.write('.')
    
    newname = pDate.strftime(fmt)
    thisDestDir = destDir + '/%04d/%02d' % (yr, mo)
    if not os.path.exists(thisDestDir):
      os.makedirs(thisDestDir)

    duplicate = thisDestDir + '/%s.jpg' % (newname)
    while os.path.exists(duplicate):
      newname = pDate.strftime(fmt) + suffix
      duplicate = destDir + '/%04d/%02d/%s.jpg' % (yr, mo, newname)
      suffix = chr(ord(suffix) + 1)
    shutil.copy2(original, duplicate)
  except Exception:
    shutil.copy2(original, errorDir + photo)
    problems.append(photo)
  except:
    sys.exit("Execution stopped.")

# Report the problem files, if any.
if len(problems) > 0:
  print "\nProblem files:"
  print "\n".join(problems)
  print "These can be found in: %s" % errorDir


;

public by Ernest Murray  2266  1  3  0

Convert the referece data to a html output

Convert the referece data to a html output: convert2html.py
#!/usr/bin/env python
"""
__author__ = 'Ernest Murray'
__Created__: 08/09/2015

This script takes the content of a file, in this case a tab separated file and
converts it to an html table. The headers for html file are contained in the text file.
The output is given in the sys.out field.
# https://goo.gl/Hn1nzf
# https://goo.gl/ZY5JYJ
"""
# TODO(EM): direct the output to a html file
import csv
from prettytable import PrettyTable


with open("data.txt", "r") as fp:
    reader = csv.DictReader(fp, dialect='excel-tab')
    ptable = PrettyTable(reader.fieldnames)
    for rowdict in reader:
        ptable.add_row([rowdict[fn] for fn in reader.fieldnames])

    print ptable.get_html_string()

fp.close()


;

public by ejmurray  2232  1  3  0

updtaed with helper.py

updtaed with helper.py: compund search.py
#!/usr/bin/env python
__author__ = 'Ernest'

from fuzzywuzzy import fuzz
from fuzzywuzzy import process
import helper
import sys

reload(sys)
sys.setdefaultencoding('utf8')


new_search = raw_input("Enter the name of a compound > ")

# create an empty list of all the compounds - this is kept in the coupound.txt file
compounds_in_list = []

for word in helper.wordlist:
    compounds_in_list.append(word)  # add the compounds to the list

# print compounds_in_list

c = process.extract(new_search, compounds_in_list, limit=5)

print "The names of the compounds that are the same or similar to " + "\"" + new_search + "\"" + "are:"
print c




;

public by ejmurray  195401  4  3  0

The answer to the codeacademy censor question. Only tested in PyCharm.

The answer to the codeacademy censor question. Only tested in PyCharm.: codeObs.py
def censor(text, word):
    word_length = len(word)
    print(text)
    print(word_length)
    censored_word = word_length * '*'
    print(censored_word)
    separated_text = text.split()
    print(separated_text)
    update = [censored_word if x == word else x for x in separated_text]
    print(update)
    censored_text = " ".join(update)
    print(censored_text)

    return censored_text

censor('here is the text here', 'here')

;