Skip to content

Commit

Permalink
Merge pull request #1 from IncapableFury/master
Browse files Browse the repository at this point in the history
Origin Update
  • Loading branch information
Eric-Fernandes-529 authored Oct 2, 2020
2 parents bbec9d5 + e37243a commit 3dff8c1
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 60 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: python
python:
- "3.7"
#install:
script:
- python -m unittest Testing.py
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[![build status of master](https://travis-ci.org/IncapableFury/Final_Project_SW555.svg?branch=master)](https://travis-ci.org/IncapableFury/Final_Project_SW555)
# Final_Project_SW555
Project for SW555 2020 Fall
53 changes: 52 additions & 1 deletion Testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,48 @@
"""

import unittest
from datetime import date

from models.Individual import Individual
from models.Family import Family
from models import Gedcom
from models.Gedcom import Gedcom

class TestTriangles(unittest.TestCase):

def testMarriageBeforeDivorce1(self):
self.assertTrue(marriage_before_divorce(fam1))

def testMarriageBeforeDivorce2(self):
self.assertEqual(marriage_before_divorce(fam1), True)

def testMarriageBeforeDivorce3(self):
self.assertNotEqual(marriage_before_divorce(fam1), False)

def testMarriageBeforeDivorce4(self):
self.assertIsNot(marriage_before_divorce(fam1), " ")

def testMarriageBeforeDivorce5(self):
self.assertIsNotNone(marriage_before_divorce(fam1))

def testMarriageBeforeDeath1(self):
self.assertTrue(fam2.marriage_before_death())

def testMarriageBeforeDeath2(self):
self.assertEqual(fam2.marriage_before_death(), True)

def testMarriageBeforeDeath3(self):
self.assertNotEqual(fam2.marriage_before_death(), False)


def marriage_before_divorce(Family):
from datetime import date
marriage= Family.get_marriedDate()
divorce= Family.get_divorcedDate()
timediff = date(*marriage)-date(*divorce)
if timediff.days <0:
return True
print("Error marriage before divorce: Marriage date of "+Family.get_id+" happened after the divorce date.")
return False

class TestDivorseBeforeDeath(unittest.TestCase):
def test1(self):
Expand Down Expand Up @@ -104,5 +141,19 @@ def test2(self):
self.assertTrue(family1.birth_before_death_of_parents())

if __name__ == '__main__':
# -----------------------------------------------
fam1=Family("F01")
fam1.set_divorcedDate(['8', 'SEP', '2009'])
fam1.set_marriedDate(['8', 'SEP', '2000'])
fam2=Family("F02")
male1=Individual("P01")
per1=Individual("P01")
per2=Individual("P02")
per1.set_deathDate(['8', 'SEP', '2010'])
per2.set_deathDate(['8', 'SEP', '2011'])
fam2.set_husband(per1)
fam2.set_wife(per2)
fam2.set_marriedDate(['8', 'SEP', '2001'])
#-------------------------------------------------
print('Running unit tests')
unittest.main()
49 changes: 33 additions & 16 deletions models/Family.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def get_id(self) -> str:
return self.id

def get_husband(self):
return self._husband.get_id()
return self._husband

def get_wife(self):
return self._wife.get_id()
return self._wife

def get_marriedDate(self) -> tuple:
return self._marriedDate

def get_divorced(self) -> tuple:
def get_divorcedDate(self) -> tuple:
return self._divorced

def get_children(self) -> list:
Expand All @@ -42,12 +42,12 @@ def set_wife(self, wife) -> None:
##if not isinstance(wife, Individual): raise TypeError("input has to be a Individual type")
self._wife = wife

def set_marriedDate(self, married_date: str) -> None:
def set_marriedDate(self, married_date: tuple) -> None:

##if not isinstance(married_date, str): raise TypeError("input has to be a str type")
self._marriedDate = self.change_date_formate(married_date)

def set_divorced(self, divorced_date: str) -> None:
def set_divorcedDate(self, divorced_date: tuple) -> None:

##if not isinstance(divorced_date, str): raise TypeError("input has to be a str type")
self._divorced = self.change_date_formate(divorced_date)
Expand All @@ -63,26 +63,31 @@ def add_child(self, child) -> None:
##if not isinstance(child, Individual): raise TypeError("input has to be a Individual type")
self._children.append(child)

def change_date_formate(self, str_input_date: str) -> tuple:
def change_date_formate(self, date: list) -> tuple:
'''
Would take the string input and convert it into a int tuple:(year, month, day)
'''
monthList = {"JAN": 1, "FEB": 2, "MAR": 3, "APR": 4, "MAY": 5, "JUN": 6, "JUL": 7, "AUG": 8, "SEP": 9, "OCT": 10, "NOV": 11, "DEC": 12}
date_list = str_input_date.split(" ")
date_list[1] = monthList[date_list[1]]
temp = int(date_list[0])
date_list[0] = int(date_list[2])
date_list[2] = temp
tuple_out = tuple(date_list)
return tuple_out
'''
Would take the string input and convert it into a int tuple:(year, month, day)
'''
monthList = {"JAN": 1, "FEB": 2, "MAR": 3, "APR": 4, "MAY": 5, "JUN": 6, "JUL": 7, "AUG": 8, "SEP": 9,
"OCT": 10, "NOV": 11, "DEC": 12}
return int(date[2]), monthList[date[1]], int(date[0])



def multiple_births_lessOrEqual_than_5(self):
pass

def marriage_before_divorce(self):
pass
from datetime import date
marriage= self.get_marriedDate()
divorce= self.get_divorced()
timedelta = date(*marriage)-date(*divorce)
if timedelta.days <0:
return True
print("Error marriage before divorce: Marriage date of "+Family.get_id+" happened after the divorce date.")
return False

def dates_before_current_date(self):
pass
Expand All @@ -91,7 +96,19 @@ def marriage_after_14(self):
pass

def marriage_before_death(self):
pass
from datetime import date
from models.Individual import Individual
marriage=self.get_marriedDate()
if self._husband.get_deathDate() > self._wife.get_deathDate():
death= self._wife.get_deathDate()
else:
death= self._husband.get_deathDate()
timedelta=date(*marriage)-date(*death)
if timedelta.days<0:
return True
print("Error marriage before death: Marriage date of "+Family.get_id+" happened after they died.")
return False


def divorce_before_death(self) -> bool:
if not self._husband or not self._wife: raise ValueError("No husband || wife")
Expand Down
130 changes: 118 additions & 12 deletions models/Gedcom.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,129 @@
class Gedcom:
def __init__(self, path,supportTags):
self._data = self.readfile(path)
def __init__(self, path, supportTags):
self._supportTags = supportTags
self._individuals = {}
self._families = {}
self._data = self.readfile(path)

def readfile(self, path):
res = []
res = [[], [], []] # [[level, tag, arguments], [start indices of indi], [start indices of fam]]
f = open(path, "r")
for l in f:
res.append(l)
index = 0
for line in f:
level, tag, arguments = self.parseline(line)
if tag not in self._supportTags: # skip unsupported tags
continue
res[0].append([level, tag, arguments])
if tag == "INDI":
res[1].append(index)
elif tag == "FAM":
if not res[2]:
res[1].append(index) # the first start index for family is the last end index for indis
res[2].append(index)
index += 1
res[2].append(index) # end index for fam
f.close()
return res

def parseline(self, line):
line = line.split()
level, tag, arguments = line[0], line[1], line[2:]
if arguments and arguments[0] in ["INDI", "FAM"]:
tag, arguments = arguments[0], tag
return level, tag, arguments

def peek(self):
def parseline(line):
for line in self._data[0]:
level, tag, arguments = line[0], line[1], line[2:]
if arguments and arguments[0] in ["INDI", "FAM"]:
tag, arguments = arguments[0], tag
if tag in self._supportTags:
print(" " * int(level), level, tag, arguments)
print(" " * int(level), level, tag, arguments)

def get_data(self):
return self._data

def get_individuals(self):
return self._individuals

def get_families(self):
return self._families

def parse(self):
from models.Individual import Individual
from models.Family import Family
for i in range(len(self._data[1]) - 1): # enumerate individuals
start_index = self._data[1][i]
end_index = self._data[1][i + 1]
# print(self._data[0][start_index:end_index])
id = self._data[0][start_index][2]
new_indi = Individual(id)
self._individuals[id] = new_indi
# print(start_index, end_index)
for j in range(start_index + 1, end_index):
level, tag, arguments = self._data[0][j]
if tag == "NAME":
new_indi.set_name("".join(arguments))
elif tag == "SEX":
new_indi.set_gender(arguments[0])
elif tag == "BIRT":
j += 1
level, tag, arguments = self._data[0][j]
new_indi.set_birthDate(arguments)
elif tag == "DEAT":
j += 1
level, tag, arguments = self._data[0][j]
new_indi.set_deathDate(arguments)
elif tag == "FAMS":
if arguments[0] not in self._families:
new_fam = Family(arguments[0])
self._families[arguments[0]] = new_fam
new_indi.add_to_family(self._families[arguments[0]])
elif tag == "FAMC":
if arguments[0] not in self._families:
new_fam = Family(arguments[0])
self._families[arguments[0]] = new_fam
new_indi.set_parentFamily(self._families[arguments[0]])
for i in range(len(self._data[2]) - 1):
start_index = self._data[2][i]
end_index = self._data[2][i + 1]
# print(self._data[0][start_index:end_index])
id = self._data[0][start_index][2]
if id not in self._families:
new_fam = Individual(id)
self._families[id] = new_fam
fam = self._families[id]
for j in range(start_index + 1, end_index):
level, tag, arguments = self._data[0][j]
# print(level, tag, arguments)
if tag == "HUSB":
fam.set_husband(self._individuals[arguments[0]])
elif tag == "WIFE":
fam.set_wife(self._individuals[arguments[0]])
elif tag == "CHIL":
fam.add_child(self._individuals[arguments[0]])
elif tag == "MARR":
j += 1
level, tag, arguments = self._data[0][j]
# print(level, tag, arguments)
try:
fam.set_marriedDate(arguments)
except:
continue
elif tag == "DIV":
j += 1
level, tag, arguments = self._data[0][j]
# print(level, tag, arguments)
try:
fam.set_divorcedDate(arguments)
except:
continue

for line in self._data:
parseline(line.split())
# if __name__ == "__main__":
# SUPPORT_TAGS = {"INDI", "NAME", "SEX", "BIRT", "DEAT", "FAMC", "FAMS", "FAM", "MARR", "HUSB", "WIFE", "CHIL",
# "DIV", "DATE", "HEAD", "TRLR", "NOTE"}
# g1 = Gedcom("../testing_files/Jiashu_Wang.ged", SUPPORT_TAGS)
# for i in range(len(g1.get_data()[0])):
# print(i,g1.get_data()[0][i])
# for i in range(len(g1.get_data()[1])):
# print(g1.get_data()[1][i])
# print(g1.get_data(),sep='/n')
# g1.parse()
# print(len(g1.get_individuals()),g1.get_individuals()["@I2@"].get_birthDate())
Loading

0 comments on commit 3dff8c1

Please sign in to comment.