def populateYears(): pitchingFile = open('Pitching.csv', 'r') ageFile = open('Master.csv', 'r') #This gets the birth years for all playerIDS for line in ageFile: line = line.split(',') id = line[0] birthYear = line[1] if len(birthYear) > 1: birthYears[id] = birthYear #this creates a PitcherYear for each row in Pitching.csv and adds it to the appropriate Year in the years list for line in pitchingFile: line = line.split(",") if line[0] == "playerID": for i in range(len(line)): headers[line[i]] = i else: playerID = line[headers["playerID"]] IP = int(line[headers["IPouts"]])/3 if IP >=162 and playerID in birthYears: yearID = int(line[headers["yearID"]]) age = yearID - int(birthYears[playerID]) BB = int(line[headers["BB"]]) SO = int(line[headers["SO"]]) H = int(line[headers["H"]]) ERA = float(line[headers["ERA"]]) player = PitcherYear(playerID,SO,BB,ERA,IP,H,yearID,age) if yearID not in years: year = Year(yearID) year.enterPitcher(player) years[yearID] = year else: years[yearID].enterPitcher(player)
def test_uses_taken_week_when_scheduling(self): y = Year(2012) taken = Week() taken.set_day(0, TimeRange(Time(13, 30), Time(17))) y.add_taken(40, taken) w = y.get_schedule_for_week(40, Hours(57, 50)) self.assertEquals(w.get_day(0).get_start(), Time(17))
def test_doesnt_schedule_hours_scheduled_by_taken_week(self): y = Year(2012) taken = Week() taken.set_day(0, TimeRange(Time(13, 30), Time(17))) y.add_taken(40, taken) w = y.get_schedule_for_week(40, Hours(47, 50)) self.assertEquals(w.get_total_time(), Hours(44, 20))
def test_uses_blocked_week_when_scheduling(self): y = Year(2012) blocked = Week() blocked.set_day(0, TimeRange(Time(8, 30), Time(13, 30))) y.add_blocked(40, blocked) w = y.get_schedule_for_week(40, Hours(57, 50)) self.assertEquals(w.get_day(0).get_start(), Time(13, 30))
def __init__(self, firstYear, secondYear, fileName): self.year1 = Year(firstYear) self.year2 = Year(secondYear) self.year1.setNext(self.year2) self.year2.setPrev(self.year1) self.fileName = fileName self.currentDay = self.year1.months[0].days[0] self.currentWeek = self.year1.months[0].weeks[0]
def display_search(self, date): """Colour search result backgrounds""" year = date.year() month = date.month() day = date.day() year = Year(year) col = year.get_column(month, day) item = self.item(month - 1, col) item.child(0, 1).data = True # Flag for colouring background item.setBackground(self.colourSettings.searchday_col)
def add_brew(self, date): """Colour in brew dates and construct QStandardItem for is_brew flag""" year = date.year() month = date.month() day = date.day() colour = self.colourSettings.brewday_col year = Year(year) col = year.get_column(month, day) item = self.item(month - 1, col) item.setBackground(colour) is_brew = item.child(0, 0) is_brew.data = True
def test_can_generate_monthly_report_for_december(self): y2012 = Year(2012) y2013 = Year(2013) y2012.set_next_year(y2013) blocked = self._get_daycare_week() taken = self._get_assistance_week() y2013.add_blocked(1, blocked) y2013.add_taken(1, taken) m = y2012.generate_montly_report(12, Hours(48, 0)) self.assertEquals(m.get_day(31), TimeRange(Time(17), Time(20)))
def set_year(self, year): """Fills in date numbers and colours background of brews and searched for dates""" year_instance = Year(year) months = year_instance.get_months() today = False colour = Qt.QColor('black') if year_instance.get_today(): today = year_instance.get_today() for month_num in range(12): curr_month = month_num month = months[month_num] for date in range(len(month)): curr_date = date date = month[date] item = self.item(curr_month, curr_date) item_text = str(date) if item is not None: if item.child(0, 0) == True: # Is a brew item.setBackground(self.colourSettings.brewday_col) if item.child(0, 1).data == True: # Identified in search item.setBackground(self.colourSettings.searchday_col) # Set text if today: if today[0] == curr_date and today[1] == curr_month: # Highlight today colour = Qt.QColor('red') item.setFont(QFont('Sans Serif', 15, QFont.Bold)) else: colour = Qt.QColor('black') item.setForeground(colour) item.setText(item_text) item.setTextAlignment(Qt.Qt.AlignCenter) # Make the QDate a child of the day if date is not '': date = int(date) now = QDate(year, curr_month + 1, date) date = QStandardItem() item.setChild(0, 1, date) date.setData(now)
class Interface: def __init__(self): self.__year = Year() def projectInterface(self): print('########################################################################') print('############################## Year Validator ##########################') print('########################################################################') print(self.__year.validateYear(self.__year.getYear())) print('Weiteres Jahr ueberpruefen? \n') answer = input('J/N') if answer == "J" or answer == "j" or answer == "yes": self.projectInterface() elif answer == 'N' or answer == 'n' or answer == 'no': exit()
def test_can_generate_realistic_monthly_report(self): y = Year(2012) blocked = self._get_daycare_week() taken = self._get_assistance_week() for i in [40, 41, 42, 43, 44]: y.add_blocked(i, blocked) y.add_taken(i, taken) m = y.generate_montly_report(10, Hours(57, 50)) self.assertEquals(m.get_day(1), TimeRange(Time(17), Time(20))) self.assertEquals(m.get_day(2), TimeRange(Time(13, 30), Time(20)))
def test_respects_both_blocked_and_taken_weeks(self): y = Year(2012) blocked = Week() blocked.set_day(0, TimeRange(Time(8, 30), Time(13, 30))) y.add_blocked(40, blocked) taken = Week() taken.set_day(1, TimeRange(Time(7), Time(17))) y.add_taken(40, taken) w = y.get_schedule_for_week(40, Hours(57, 50)) self.assertEquals(w.get_day(0).get_start(), Time(13, 30)) self.assertEquals(w.get_day(1).get_start(), Time(17))
from Year import Year testYear = Year(2016) #5 testMonth = testYear.months[8] testDay = testMonth.days[9] print( str(testDay.month) + " " + str(testDay.date) + ", " + str(testYear.name) + ' (' + testDay.weekday + ')\n') for day in testCalendar.getCurrentWeek(): print(day.date) ## Month test print 'Month details: ' print testCalendar.getCurrentMonth().name, ':' print 'Number of days: ', len(testCalendar.getCurrentMonth().days) for i in testCalendar.getCurrentMonth().weeks[0]: print i.date, i.weekday for i in testCalendar.getCurrentMonth().weeks[1]: print i.date, i.weekday for i in testCalendar.getCurrentMonth().weeks[2]: print i.date, i.weekday for i in testCalendar.getCurrentMonth().weeks[3]: print i.date, i.weekday for i in testCalendar.getCurrentMonth().weeks[4]: print i.date, i.weekday for i in testCalendar.getCurrentMonth().weeks[5]: print i.date, i.weekday newDay = testCalendar.getMonth('august', 2016).getDay(30)
from TimeGenerator import generate_week from Hours import Hours from ReportGenerator import ReportGenerator from Year import Year from Week import Week from TimeRange import TimeRange from Time import Time #m = Month(2012, 11) h1 = Hours(48, 20) weeks = {} year = {} year[2012] = Year(2012) year[2013] = Year(2013) year[2012].set_next_year(year[2013]) # blocked2012 = Week() # blocked2012.set_day(0, TimeRange(Time(8, 30), Time(13, 30))) # blocked2012.set_day(1, TimeRange(Time(8, 30), Time(13, 30))) # blocked2012.set_day(3, TimeRange(Time(8, 30), Time(13, 30))) # # taken2012 = Week() # taken2012.set_day(0, TimeRange(Time(13, 30), Time(17))) # taken2012.set_day(1, TimeRange(Time(6, 30), Time(8, 30))) # taken2012.set_day(2, TimeRange(Time(7), Time(17))) # taken2012.set_day(3, TimeRange(Time(6, 30), Time(8, 30))) #
def __init__(self): self.__year = Year()
def test_can_retrieve_taken_week(self): y = Year(2012) taken = Week() y.add_taken(40, taken) self.assertEquals(y.get_taken(40), taken)
def test_can_hold_taken_week(self): y = Year(2012) taken = Week() y.add_taken(40, taken)
def test_can_give_schedule_for_week(self): y = Year(2012) w = y.get_schedule_for_week(40, Hours(57, 50)) self.assertEquals(w.get_day(0), TimeRange(Time(7), Time(17)))
def test_can_be_linked_to_next_year(self): y2012 = Year(2012) y2013 = Year(2013) y2012.set_next_year(y2013) self.assertEquals(y2012.get_next_year(), y2013)
def test_gives_none_if_no_taken_week_specified(self): y = Year(2012) self.assertEquals(y.get_taken(40), None)
def test_can_hold_blocked_ranges(self): y = Year(2012) blocked = Week() y.add_blocked(40, blocked) self.assertEquals(y.get_blocked(40), blocked)
def test_can_generate_monthly_report(self): y = Year(2012) m = y.generate_montly_report(10, Hours(57, 50)) self.assertEquals(m.get_day(1), TimeRange(Time(7), Time(17)))
class Calendar(): ## @fn __init__ # @brief Initializes a Calendar object # @param [in] firstYear An integer value of the first year to be created # @param [in] secondYear An integer value of the second year to be created # @param [in] fileName The file name which will be used to save and load information # in order to rebuild the Calendar object in the future # @param [out] return The initialized Calendar object # # @details # # Initializes a Calendar object. Initializes two Year objects based on the firstYear # and secondYear arguments and stores the fileName which will be used to save and # load information from a local file. # def __init__(self, firstYear, secondYear, fileName): self.year1 = Year(firstYear) self.year2 = Year(secondYear) self.year1.setNext(self.year2) self.year2.setPrev(self.year1) self.fileName = fileName self.currentDay = self.year1.months[0].days[0] self.currentWeek = self.year1.months[0].weeks[0] ## @fn getCurrentDay # @brief Gets the currentDay # @param [out] return The currentDay member variable # def getCurrentDay(self): return self.currentDay ## @fn getCurrentWeek # @brief Gets the week associated with the current day # @param [out] return A list of days ordered as a consecutive week (sunday-saturday), # with currentDay being one of the days in the list. # def getCurrentWeek(self): for week in self.currentDay.month.weeks: for day in week: if day == self.currentDay: return week ## @fn getCurrentMonth # @brief Gets the month which the currentDay belongs to # @param [out] return the month object which owns currentDay # def getCurrentMonth(self): return self.currentDay.month ## @fn getCurrentYear # @brief Gets the year which the currentDay belongs to # @param [out] return the year object which owns currentDay # def getCurrentYear(self): if self.currentDay.month.year.name == self.year1.name: return self.year1 return self.year2 ## @fn getYear # @brief Gets a year based on the integer value # @param [in] yearName An integer value of the year you are looking for # @param [out] return The year object associated with the integer value if one exists # def getYear(self, yearName): if self.year1.name == yearName: return self.year1 elif self.year2.name == yearName: return self.year2 return None ## @fn getYear # @brief Gets a month based on the integer value for the year and string of the month name # @param [in] monthName A string value of the month name for the month you are looking for # @param [in] yearName An integer value of the year you are looking for # @param [out] return The month object associated with monthName and yearName # def getMonth(self, monthName, yearName): if self.year1.name == yearName: y = self.year1 elif self.year2.name == yearName: y = self.year2 if (y != None): return y.getMonth(monthName) else: return None ## @fn load # @brief reads through a file, interpets the data, and edits it own contents to match # # @details # # Trys to open the file. If it succeeds, it proceeds to read through the textfile, # look for day data and details for that day, and as it recognises this data, # add the details to the days it currently possesses. # def load(self): try: log = open(self.fileName) except IOError: return lines = log.readlines() newDay = True day = None for line in lines: if line == ';\n': newDay = True elif newDay: words = line.split() day = self.getYear(int(words[0])).months[int( words[1])].days[int(words[2])] newDay = False else: day.addDetail(line.rstrip('\n')) ## @fn save # @brief creates/overwrites a file and saves the details of its days # # @details # # Creates a new file or overwrites an existing file. It then scans through its owns # days, and for each day with at least one detail, logs information about the day # and its details in order to replicate the data in the future. # def save(self): log = open(self.fileName, 'w+') m = 0 for month in self.year1.months: d = 0 for day in month.days: if (len(day.details) != 0): log.write( str(self.year1.name) + ' ' + str(m) + ' ' + str(d)) for detail in day.details: log.write('\n' + detail) log.write('\n;\n') d += 1 m += 1 m = 0 for month in self.year2.months: d = 0 for day in month.days: if (len(day.details) != 0): log.write( str(self.year2.name) + ' ' + str(m) + ' ' + str(d)) for detail in day.details: log.write('\n' + detail) log.write('\n;\n') d += 1 m += 1 log.close()
def doAnalysis(): client = MongoClient(mongostring, retryWrites=False) db = client.moneymanager months_to_update = [] number_of_trans = db.transactions.count() cat_counter = 0 # reset current analysis db.months.update_many({}, {'$unset': {'categories': 1}}) for trans in db.transactions.find(modifiers={'$snapshot': True}): # categorization for category in keywords.keys(): if any(substring in trans['descr'] for substring in keywords[category]): cat_counter += 1 months_to_update.append({ 'year': trans['date'].year, 'month': trans['date'].month, 'category': 'categories.' + category, 'amount': 0 - trans['amount'] }) print('found one') #db.months.update_one({'year': trans['date'].year, 'month': trans['date'].month}, {'$inc': {'categories['+ category +']': trans['amount']}}) print("now updating") for entry in months_to_update: #query = {'$inc': {'categories.food': 1}} query = {'$inc': {entry['category']: entry['amount']}} print(query) db.months.update_one({ 'year': entry['year'], 'month': entry['month'] }, query) print("could categorize", cat_counter, "out of", number_of_trans, "transactions (", cat_counter / number_of_trans * 100, "%)") print("now summing up the years") years_to_save = {} num_of_months = db.months.count() for month in db.months.find(): if month['year'] in years_to_save: years_to_save[month['year']]['savings'] += month['savings'] if 'categories' in month: for category in month['categories'].keys(): if category in years_to_save[month['year']]['categories']: years_to_save[month['year']]['categories'][ category] += month['categories'][category] else: years_to_save[month['year']]['categories'][ category] = month['categories'][category] else: years_to_save[month['year']] = {} years_to_save[month['year']]['categories'] = {} years_to_save[month['year']]['savings'] = month['savings'] if 'categories' in month: years_to_save[month['year']]['categories'] = {} for category in month['categories'].keys(): years_to_save[month['year']]['categories'][ category] = month['categories'][category] print("saving years") for year in years_to_save.keys(): avg_spendings = {} if 'categories' in years_to_save[year]: avg_spendings = years_to_save[year]['categories'].copy() for category in avg_spendings.keys(): avg_spendings[category] /= num_of_months current_year = Year(year, years_to_save[year]['savings'], avg_spendings, years_to_save[year]['categories']) db.years.insert_one(current_year.__dict__)
def addYear(self, y, ns, ne, ni, na, nd, nsuc): newyear = Year(y, self.screen, self.evaluate, self.incubate, self.accelerate, self.develop) newyear.setActiveScreen(self.activeScreen) newyear.setActiveEvaluate(self.activeEvaluate) newyear.setActiveIncubate(self.activeIncubate) newyear.setActiveAccelerate(self.activeAccelerate) newyear.setActiveDevelop(self.activeDevelop) newyear.setNewScreen(ns) newyear.setNewEvaluate(ne) newyear.setNewIncubate(ni) newyear.setNewAccelerate(na) newyear.setNewDevelop(nd) newyear.setNewSuccess(nsuc) self.years.append(newyear)