[2013-Mar-21] Comic - Program

Blame Quintushalls for this.

Moderators: NeatNit, Kimra

HappyFerret
Posts: 1
Joined: Thu Mar 21, 2013 12:24 pm

[2013-Mar-21] Comic - Program

Post by HappyFerret »

Under here is a python script doing what was asked in today's comic, using wikipedia.

It works for Python 2.* and 3.*, and uses wget (which you should separately install somewhere it can be launched by the system, so within the Path), and is called giving one argument, the birthdate considered, in the Swiss (héhé) format: dd.mm.yyyy

>C:\Python27\python.exe dateSMBC.py 01.10.1989
me@hostnam ~ $ python dateSMBC.py 01.10.1989


Here you go, have fun !!

##################
# PROGRAM START #
##################

import os, sys, re, urllib2


months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
lenMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


def BDayminus9mon(date):
day = date[0]
month = date[1]
year = date[2]

result = date[:]

if month < 10:
result[2] -= 1

result[1] = ((result[1] + 2) % 12) + 1

if day > lenMonths[result[1]-1]:
result[0] = day - lenMonths[result[1]-1]
result[1] = (result[1] % 12) + 1;

return result

def datePlus(date, nDays):
day = date[0]
month = date[1]
year = date[2]

result = date[:]

if day + nDays > lenMonths[month-1]:
if month == 12:
result[2] += 1
result[1] = 1
else:
result[1] += 1

result[0] = (day + nDays) - lenMonths[month-1]
else:
result[0] += nDays

return result

def dateMinus(date, nDays):
day = date[0]
month = date[1]
year = date[2]

result = date[:]

if day - nDays < 0:
if month == 1:
result[2] -= 1
result[1] = 12
else:
result[1] -= 1

result[0] = lenMonths[month-2] + day - nDays
else:
result[0] -= nDays

return result

def findStr(data, eDate):
if data.find(">" + months[eDate[1]-1] + " " + str(eDate[0]) + "<") != -1:
data = data[data.find(">" + months[eDate[1]-1] + " " + str(eDate[0]) + "<")+len(">" + months[eDate[1]-1] + " " + str(eDate[0]))+9:]
data = data[:data.find("</li>")]

result = re.sub(r'<[^>]*>', '', data)

return result
else:
return -1


date = sys.argv[1].split('.')
for i in range(len(date)):
date = int(date)


print(date)
eDate = BDayminus9mon(date)
print(eDate)


os.system("wget http://en.wikipedia.org/wiki/"+str(date[2])+" -O index.html")
ifp = open("index.html", "r")
tmp = ifp.readlines()
ifp.close()


data = ""
for t in tmp:
data += t
data = data[data.rfind(">Events<"):data.rfind(">Births<")]


result = findStr(data, eDate)
if result != -1:
print(months[eDate[1] - 1] + " " + str(eDate[0]) + ", " + str(eDate[2]))
print(result)
exit(0)
else:
for i in range(1,20):
newDate = dateMinus(eDate, i)
if newDate[2] == eDate[2]:
result = findStr(data, newDate)
if result != -1:
print("Closest Match: " + months[newDate[1] - 1] + " " + str(newDate[0]) + ", " + str(newDate[2]))
print(result)
exit(0)

newDate = datePlus(eDate, i)
if newDate[2] == eDate[2]:
result = findStr(data, newDate)
if result != -1:
print("Closest Match: " + months[newDate[1] - 1] + " " + str(newDate[0]) + ", " + str(newDate[2]))
print(result)
exit(0)

exit(0)

sja5164
Posts: 1
Joined: Thu Mar 21, 2013 2:28 pm

[2013-03-21] - We broke the internet

Post by sja5164 »

Image

It appears to be a forum for those looking to conceive a child...

User avatar
GUTCHUCKER
Gotchucker's less handsome twin
Posts: 2126
Joined: Wed Dec 01, 2010 2:26 am
Location: Paradise City?

Re: [2013-03-21] - We broke the internet

Post by GUTCHUCKER »

Wait, so what's the point of this thread?
Datanazush wrote:I ship Mohammed and Jehova.

Phrot

Re: [2013-03-21] - We broke the internet

Post by Phrot »

Comic worked for me.

On Topic:
Funny enough, I looked into this way back in high school and was happily surprised (yet still very skeeved) to find out what was going on 38 weeks before I was born.

First Man on the Moon.

That's one small step for [a] man, one giant leap for me!

In keeping with familial traditions I have unprotected sex every time we put a man on the moon. :(

I'll let the incomparable Neil deGrasse Tyson narrate my disappointment:
http://www.youtube.com/watch?v=CbIZU8cQWXc

N031

Re: 2013-03-21 Comic - Program

Post by N031 »

I just added the identation, and changed the wget part for the urllib2, so don't need to install anything

Kampai my friend, 50 internet points to you

Code: Select all

import sys, re, urllib2

months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
lenMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def BDayminus9mon(date):
    day = date[0]
    month = date[1]
    year = date[2]

    result = date[:]

    if month < 10:
        result[2] -= 1

        result[1] = ((result[1] + 2) % 12) + 1

    if day > lenMonths[result[1]-1]:
        result[0] = day - lenMonths[result[1]-1]
        result[1] = (result[1] % 12) + 1;

    return result

def datePlus(date, nDays):
    day = date[0]
    month = date[1]
    year = date[2]

    result = date[:]

    if day + nDays > lenMonths[month-1]:
        if month == 12:
            result[2] += 1
            result[1] = 1
        else:
            result[1] += 1

        result[0] = (day + nDays) - lenMonths[month-1]
    else:
        result[0] += nDays

    return result

def dateMinus(date, nDays):
    day = date[0]
    month = date[1]
    year = date[2]

    result = date[:]

    if day - nDays < 0:
        if month == 1:
            result[2] -= 1
            result[1] = 12
        else:
            result[1] -= 1

        result[0] = lenMonths[month-2] + day - nDays
    else:
        result[0] -= nDays

    return result

def findStr(data, eDate):
    if data.find(">" + months[eDate[1]-1] + " " + str(eDate[0]) + "<") != -1:
        data = data[data.find(">" + months[eDate[1]-1] + " " + str(eDate[0]) + "<")+len(">" + months[eDate[1]-1] + " " + str(eDate[0]))+9:]
        data = data[:data.find("</li>")]

        result = re.sub(r'<[^>]*>', '', data)

        return result
    else:
        return -1


date = sys.argv[1].split('.')
for i in range(len(date)):
    date[i] = int(date[i])


print(date)
eDate = BDayminus9mon(date)
print(eDate)

url = "http://en.wikipedia.org/wiki/"+str(date[2])
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

req = urllib2.Request(url, headers=hdr)
usock = urllib2.urlopen(req)
data = usock.read()
usock.close()

data = data[data.rfind(">Events<"):data.rfind(">Births<")]


result = findStr(data, eDate)
if result != -1:
    print(months[eDate[1] - 1] + " " + str(eDate[0]) + ", " + str(eDate[2]))
    print(result)
    exit(0)
else:
    for i in range(1,20):
        newDate = dateMinus(eDate, i)
        if newDate[2] == eDate[2]:
            result = findStr(data, newDate)
            if result != -1:
                print("Closest Match: " + months[newDate[1] - 1] + " " + str(newDate[0]) + ", " + str(newDate[2]))
                print(result)
                exit(0)

newDate = datePlus(eDate, i)
if newDate[2] == eDate[2]:
    result = findStr(data, newDate)
if result != -1:
    print("Closest Match: " + months[newDate[1] - 1] + " " + str(newDate[0]) + ", " + str(newDate[2]))
    print(result)
    exit(0)

exit(0)

N031

Re: 2013-03-21 Comic - Program

Post by N031 »

I don't know how to edit past comments, identation was wrong at the end, and in python that is bad news

This is the right one, now I will go and ashamed accept the internet rage upon me

Code: Select all

import sys, re, urllib2


months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
lenMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


def BDayminus9mon(date):
    day = date[0]
    month = date[1]
    year = date[2]

    result = date[:]

    if month < 10:
        result[2] -= 1

        result[1] = ((result[1] + 2) % 12) + 1

    if day > lenMonths[result[1]-1]:
        result[0] = day - lenMonths[result[1]-1]
        result[1] = (result[1] % 12) + 1;

    return result

def datePlus(date, nDays):
    day = date[0]
    month = date[1]
    year = date[2]

    result = date[:]

    if day + nDays > lenMonths[month-1]:
        if month == 12:
            result[2] += 1
            result[1] = 1
        else:
            result[1] += 1

        result[0] = (day + nDays) - lenMonths[month-1]
    else:
        result[0] += nDays

    return result

def dateMinus(date, nDays):
    day = date[0]
    month = date[1]
    year = date[2]

    result = date[:]

    if day - nDays < 0:
        if month == 1:
            result[2] -= 1
            result[1] = 12
        else:
            result[1] -= 1

        result[0] = lenMonths[month-2] + day - nDays
    else:
        result[0] -= nDays

    return result

def findStr(data, eDate):
    if data.find(">" + months[eDate[1]-1] + " " + str(eDate[0]) + "<") != -1:
        data = data[data.find(">" + months[eDate[1]-1] + " " + str(eDate[0]) + "<")+len(">" + months[eDate[1]-1] + " " + str(eDate[0]))+9:]
        data = data[:data.find("</li>")]

        result = re.sub(r'<[^>]*>', '', data)

        return result
    else:
        return -1


date = sys.argv[1].split('.')
for i in range(len(date)):
    date[i] = int(date[i])


print(date)
eDate = BDayminus9mon(date)
print(eDate)

url = "http://en.wikipedia.org/wiki/"+str(date[2])
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

req = urllib2.Request(url, headers=hdr)
usock = urllib2.urlopen(req)
data = usock.read()
usock.close()

data = data[data.rfind(">Events<"):data.rfind(">Births<")]


result = findStr(data, eDate)
if result != -1:
    print(months[eDate[1] - 1] + " " + str(eDate[0]) + ", " + str(eDate[2]))
    print(result)
    exit(0)
else:
    for i in range(1,20):
        newDate = dateMinus(eDate, i)
        if newDate[2] == eDate[2]:
            result = findStr(data, newDate)
            if result != -1:
                print("Closest Match: " + months[newDate[1] - 1] + " " + str(newDate[0]) + ", " + str(newDate[2]))
                print(result)
                exit(0)

        newDate = datePlus(eDate, i)
        if newDate[2] == eDate[2]:
            result = findStr(data, newDate)
            if result != -1:
                print("Closest Match: " + months[newDate[1] - 1] + " " + str(newDate[0]) + ", " + str(newDate[2]))
                print(result)
                exit(0)

exit(0)

User avatar
Arandur
Posts: 15
Joined: Mon Sep 17, 2012 12:50 pm
Location: Stupid California

Re: [2013-03-21] - We broke the internet

Post by Arandur »

1989 July 31st: Nintendo releases the Gameboy portable console in America.

1990 May 9th: I am born.

Draw your own conclusions.

Oz

Re: 2013-03-21 Comic - Program

Post by Oz »

I got this error trying to compile:
Traceback (most recent call last):
File "C:/Users/NewOz/Desktop/test.py", line 78, in <module>
date = sys.argv[1].split('.')
IndexError: list index out of range

Oz

Re: 2013-03-21 Comic - Program

Post by Oz »

Never mind, I'm an idiot.

Rrhain

Re: 2013-03-21 Comic - Program

Post by Rrhain »

Well, in my case, we're pretty sure when I was conceived. I was due on Christmas which makes my conception date March 20...

...which happens to be my father's birthday.

Betcha he never thought he'd have to have his Christmas and birthday present being the same thing!

Decoy256

Re: 2013-03-21 Comic - Program

Post by Decoy256 »

Yes, but a woman's ovulation cycle can vary, so there should be a two week buffer (+/- 1 week on either side of estimated date) built into the system, then pick the most significant event during that 2 week period.

treelobsters
Posts: 4
Joined: Wed Mar 20, 2013 1:05 pm

Re: 2013-03-21 Comic - Program

Post by treelobsters »

If you want to make the calculation easier, the convention is to use 40 weeks (280 days) instead of 9 months.

Classic!

Re: 2013-03-21 Comic - Program

Post by Classic! »

- 4 LA police are charged with beating Rodney King

arakell

Re: 2013-03-21 Comic - Program

Post by arakell »

Being from Poland, I find this comic doubly funny :D

centax

Re: 2013-03-21 Comic - Program

Post by centax »

N031 wrote:I don't know how to edit past comments, identation was wrong at the end, and in python that is bad news

This is the right one, now I will go and ashamed accept the internet rage upon me
I modified this because it was having problems with people born in October thru December

Code: Select all

import sys, re, urllib2
## NEW CODE
from datetime import datetime, timedelta
## NEW CODE


months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
lenMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


def BDayminus9mon(date):
##    day = date[0]
##    month = date[1]
##    year = date[2]

    result = date[:]

##    if month < 10:
##        result[2] -= 1

##        result[1] = ((result[1] + 2) % 12) + 1

##    if day > lenMonths[result[1]-1]:
##        result[0] = day - lenMonths[result[1]-1]
##        result[1] = (result[1] % 12) + 1;

## NEW CODE
    bday = datetime(date[2], date[1], date[0])
    preggers = timedelta(days=240)
    cday = bday - preggers
    result = date[:]
    result[0] = cday.day
    result[1] = cday.month
    result[2] = cday.year
## NEW CODE

    return result

def datePlus(date, nDays):
    day = date[0]
    month = date[1]
    year = date[2]

    result = date[:]

    if day + nDays > lenMonths[month-1]:
        if month == 12:
            result[2] += 1
            result[1] = 1
        else:
            result[1] += 1

        result[0] = (day + nDays) - lenMonths[month-1]
    else:
        result[0] += nDays

    return result

def dateMinus(date, nDays):
    day = date[0]
    month = date[1]
    year = date[2]

    result = date[:]

    if day - nDays < 0:
        if month == 1:
            result[2] -= 1
            result[1] = 12
        else:
            result[1] -= 1

        result[0] = lenMonths[month-2] + day - nDays
    else:
        result[0] -= nDays

    return result

def findStr(data, eDate):
    if data.find(">" + months[eDate[1]-1] + " " + str(eDate[0]) + "<") != -1:
        data = data[data.find(">" + months[eDate[1]-1] + " " + str(eDate[0]) + "<")+len(">" + months[eDate[1]-1] + " " + str(eDate[0]))+9:]
        data = data[:data.find("</li>")]

        result = re.sub(r'<[^>]*>', '', data)

        return result
    else:
        return -1


date = sys.argv[1].split('.')
for i in range(len(date)):
    date[i] = int(date[i])


print(date)
eDate = BDayminus9mon(date)
print(eDate)

url = "http://en.wikipedia.org/wiki/"+str(date[2])
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

req = urllib2.Request(url, headers=hdr)
usock = urllib2.urlopen(req)
data = usock.read()
usock.close()

data = data[data.rfind(">Events<"):data.rfind(">Births<")]


result = findStr(data, eDate)
if result != -1:
    print(months[eDate[1] - 1] + " " + str(eDate[0]) + ", " + str(eDate[2]))
    print(result)
    exit(0)
else:
    for i in range(1,20):
        newDate = dateMinus(eDate, i)
        if newDate[2] == eDate[2]:
            result = findStr(data, newDate)
            if result != -1:
                print("Closest Match: " + months[newDate[1] - 1] + " " + str(newDate[0]) + ", " + str(newDate[2]))
                print(result)
                exit(0)

        newDate = datePlus(eDate, i)
        if newDate[2] == eDate[2]:
            result = findStr(data, newDate)
            if result != -1:
                print("Closest Match: " + months[newDate[1] - 1] + " " + str(newDate[0]) + ", " + str(newDate[2]))
                print(result)
                exit(0)

exit(0)

Post Reply