def logPurge(): if Config.doLogPurge: logTC("Starting log file purge...") filesToPurge = [] filePaths = [] purgePeriodAgo = Date(getNow()).dateobj - datetime.timedelta(days=Config.logPurgePeriod) purgePeriodAgo = Date( str(purgePeriodAgo.month) + "-" + str(purgePeriodAgo.day) + "-" + str(purgePeriodAgo.year) ) for dirpath, _, filenames in os.walk(Config.loggingDir): for f in filenames: if f.endswith(".txt"): lfDate = Date(findSS(f, "log-", ".txt")) if lfDate.isPast(date=purgePeriodAgo): filesToPurge.append(f) filePaths.append(os.path.abspath(os.path.join(dirpath, f))) if len(filesToPurge) > 0: logTC("Found " + str(len(filesToPurge)) + " files to purge.") logTC("Purging now...") for f in range(0, len(filesToPurge)): logTC("Purging " + filesToPurge[f]) os.remove(filePaths[f]) logTC("Log file purge complete!") else: logTC("No files to purge found.") else: pass
def date_header(self, key, date=None): if date: date = Date.time2str(date) self[key] = date else: value = self.get(key) if value is None: return None return Date.str2time(value)
def main(): print('Welcome! This program will tell you the current date and conatins various methods to use on the date') today = Date() print(today) today.setMonth(12) print(today) today.setDay(5, 4, 2014) print(today)
def _set_date_header(self, key, date): # XXX for now we only document that the helpers can accept a # datetime object, but you can also pass a string and a int. # Let's see in the futur if this is usefull and document all # the behavior if isinstance(date, datetime): date = date elif isinstance(date, int): date = Date.epoch2time(date) elif isinstance(date, str): date = Date.str2time(date) else: raise ValueError("date is of type <{type}> but can only be an " "instance of string, int or a datetime object" .format(type=type(date))) self.set(key, date)
def _get_date_header(self, key): value = self.__getitem__(key) if value is None: return None if isinstance(value, str): return Date.str2time(value) elif isinstance(value, int): return Date.epoch2time(value) elif isinstance(value, datetime): return value else: print value raise ValueError("date is of type <{type}> but can only be an " "instance of string, int or a datetime object" .format(type=type(value)))
class Employee: """Employee Class Employee with Date attributes. """ def __init__(self, fn, ln, bth_mo, bth_day, bth_yr, h_mo, h_day, h_yr): """Constructor Constructor for class Employee. """ # fn = first name # ln = last name # bth = birth # mo = month # yr = year # h = hire # Objects of class Date self.birth_date = Date(bth_mo, bth_day, bth_yr) self.hire_date = Date(h_mo, h_day, h_yr) self.lname = ln self.fname = fn print "Employee constructor: %s, %s" % (self.lname, self.fname) def __del__(self): """Deconstructor Called before object of class Employee is destroyed. """ print "Employee object about to be destroyed: %s, %s" % (self.lname, self.fname) def display(self): """Displays employee information""" print "%s, %s" % (self.fname, self.lname) print "Hired:", self.hire_date.display() print "Birth date:", self.birth_date.display()
def __init__(self, fn, ln, bth_mo, bth_day, bth_yr, h_mo, h_day, h_yr): """Constructor Constructor for class Employee. """ # fn = first name # ln = last name # bth = birth # mo = month # yr = year # h = hire # Objects of class Date self.birth_date = Date(bth_mo, bth_day, bth_yr) self.hire_date = Date(h_mo, h_day, h_yr) self.lname = ln self.fname = fn print "Employee constructor: %s, %s" % (self.lname, self.fname)
def get(self, key): """ Returns the value for a given header :param key: name of the header :type key: string :rtype: string """ value = self.__getitem__(key) if isinstance(value, datetime): value = Date.time2str(value) return value
class Story: def __init__(self, news_organization, headline, storyURL, imageURL, fromDate, toDate, _id, loadCount, clickCount, position): self.news_organization = news_organization self.headline = headline self.storyURL = storyURL self.imageURL = imageURL self.fromDate = fromDate self.toDate = toDate self._id = _id self.date_handler = Date() if fromDate is not None: self.formatedFromDate = self.date_handler.format_date(fromDate) if toDate is not None: self.formatedToDate = self.date_handler.format_date(toDate) self.loadCount = loadCount self.clickCount = clickCount if loadCount == 0 or loadCount == None or clickCount == 0 or clickCount == None: self.clickthrough = 0 else: self.clickthrough = round((float(clickCount) / float(loadCount)) * 100, 2) self.position = position def get_story_object(self): story = {} story['news_organization'] = self.news_organization story['headline'] = self.headline story['url'] = self.storyURL story['image'] = self.imageURL story['date'] = self.fromDate story['to_date'] = self.toDate story['load_count'] = self.loadCount story['click_count'] = self.clickCount story['clickthrough'] = self.clickthrough story['position'] = self.position if not self._id: return story else: story['_id'] = self._id return story
def __init__(self, news_organization, headline, storyURL, imageURL, fromDate, toDate, _id, loadCount, clickCount, position): self.news_organization = news_organization self.headline = headline self.storyURL = storyURL self.imageURL = imageURL self.fromDate = fromDate self.toDate = toDate self._id = _id self.date_handler = Date() if fromDate is not None: self.formatedFromDate = self.date_handler.format_date(fromDate) if toDate is not None: self.formatedToDate = self.date_handler.format_date(toDate) self.loadCount = loadCount self.clickCount = clickCount if loadCount == 0 or loadCount == None or clickCount == 0 or clickCount == None: self.clickthrough = 0 else: self.clickthrough = round((float(clickCount) / float(loadCount)) * 100, 2) self.position = position
def test_isValidGregorian(self): date = Date(12, 1, 2000) self.assertEqual(date._isValidGregorian(333, 30, 2000), False) self.assertEqual(date._isValidGregorian(12, 1, 2000), True) self.assertEqual(date._isValidGregorian(2, 29, 2000 ), True) self.assertEqual(date._isValidGregorian(2, 30, 2000 ), False)
def test_set_year_zero(self): self.date = Date(1, 1, 1970) with self.assertRaises(ValueError): self.date.year = 0
def test_julianDay(self) : date = Date(1, 1, 2000) # print(date.julianDay()) self.assertEqual(date.julianDay(), 2451545)
def test_isLeapYear(self): # shiji runnian date = Date(1, 1, 2000) # print(2000 % 100) # print(2000 % 400) self.assertEqual(date.isLeapYear(), True) # putong runnian date = Date(1, 1, 2004) self.assertEqual(date.isLeapYear(), True) # bushi nunnian date = Date(1, 1, 2003) self.assertEqual(date.isLeapYear(), False) # teshu de bushi runnian date = Date(1, 1, 600) self.assertEqual(date.isLeapYear(), False) self.assertEqual(date._isLeapYear(2000), True)
def test_asGregorian(self): date = Date(12, 12, 2019) # print(date.asGregorian()) self.assertEqual(date.asGregorian(), "12/12/2019") self.assertEqual(date.asGregorian(","), "12,12,2019")
def test_monthName(self): date = Date(1, 1, 1990) self.assertEqual(date.monthName(), "January") date = Date(2, 1, 1990) self.assertEqual(date.monthName(), "February") date = Date(3, 1, 1990) self.assertEqual(date.monthName(), "March") date = Date(4, 1, 1990) self.assertEqual(date.monthName(), "April") date = Date(5, 1, 1990) self.assertEqual(date.monthName(), "May") date = Date(6, 1, 1990) self.assertEqual(date.monthName(), "June") date = Date(7, 1, 1990) self.assertEqual(date.monthName(), "July") date = Date(8, 1, 1990) self.assertEqual(date.monthName(), "August") date = Date(9, 1, 1990) self.assertEqual(date.monthName(), "September") date = Date(10, 1, 1990) self.assertEqual(date.monthName(), "October") date = Date(11, 1, 1990) self.assertEqual(date.monthName(), "November") date = Date(12, 1, 1990) self.assertEqual(date.monthName(), "December")
def test_month(self): date = Date(1, 1, 2000) # print(date.month()) self.assertEqual(date.month(), 1)
def setUp(self): self.flight = Flight(Date(29, 11, 2016, '12:20'), Date(29, 11, 2016, '15:30'), 100, 120, "Sofia", "London", Terminal(2, 30), False)
from parsing import GetDict import mysql.connector from bank import pin, accountNumber from date import Date from statement import Statement Date = Date() options = int( input( "\t\t\t WELCOME TO MYBANK\n\t\t\t 1.Create Account\n\t\t\t 2.Existing Customer\n" )) # Database connections Modify According to Developer Data cred = GetDict() conn = mysql.connector.connect(**cred) cur = conn.cursor() auto_pin = pin() accountNumber = accountNumber() statement = Statement() try: #Creating Account def createAccount(): if options == 1: accounttype = int( input("\t\t\t 1.Current Account \n\t\t\t 2.Savings Account\n")) #Current Account if accounttype == 1: print(Date)
from date import Day from date import Month from date import Date start_date = Date(Day.WED, Month.APR, 1, 2006) #Consider moving to calendar_window? def fill_rows_cols(start_date, builder): start_row = int(start_date.numday / 7) + 1 start_col = start_date.day.num() cur_date = start_date for row in range(1, 7): for col in range(1, 8): if (row <= start_row and col < start_col) or start_date.month != cur_date.month: builder.get_object('Date' + str(row) + str(col)).set_text('\n\n') else: builder.get_object('Date' + str(row) + str(col)).set_text( str(cur_date.numday) + '\n\n') cur_date = cur_date.next_date() def render_calendar(start_date, builder): builder.get_object('month').set_text( str(start_date.month.name()) + ' ' + str(start_date.year)) fill_rows_cols(start_date, builder)
def test_calc_difference5(self): self.date1 = Date(1, 1, 2000) self.date2 = Date(1, 1, 2000) self.assertEqual(self.date1.calc_difference(self.date2), 0)
def test_calc_difference1(self): self.date1 = Date(1, 1, 2000) self.date2 = Date(25, 9, 2019) self.assertEqual(self.date1.calc_difference(self.date2), 7207)
def test_not_leap_year(self): self.date = Date(1, 1, 2019) self.assertFalse(self.date.is_leap_year())
def test_leap_year(self): self.date = Date(1, 1, 2000) self.assertTrue(self.date.is_leap_year())
def test_dayOfWeek(self): date = Date(1, 7, 2019) self.assertEqual(date.dayOfWeek(), 0) date = Date(1, 8, 2019) self.assertEqual(date.dayOfWeek(), 1) date = Date(1, 9, 2019) self.assertEqual(date.dayOfWeek(), 2) date = Date(1, 10, 2019) self.assertEqual(date.dayOfWeek(), 3) date = Date(1, 11, 2019) self.assertEqual(date.dayOfWeek(), 4) date = Date(1, 12, 2019) self.assertEqual(date.dayOfWeek(), 5) date = Date(1, 13, 2019) self.assertEqual(date.dayOfWeek(), 6) date = Date(12, 12, 2019) self.assertEqual(date.dayOfWeek(), 3)
def test_dayOfWeekName(self): date = Date(1, 3, 2000) # print(date.dayOfWeekName()) self.assertEqual(date.dayOfWeekName(), "Monday") date = Date(1, 4, 2000) self.assertEqual(date.dayOfWeekName(), "Tuesday") date = Date(1, 5, 2000) self.assertEqual(date.dayOfWeekName(), "Wednesday") date = Date(1, 6, 2000) self.assertEqual(date.dayOfWeekName(), "Thursday") date = Date(1, 7, 2000) self.assertEqual(date.dayOfWeekName(), "Friday") date = Date(1, 8, 2000) self.assertEqual(date.dayOfWeekName(), "Saturday") date = Date(1, 9, 2000) self.assertEqual(date.dayOfWeekName(), "Sunday")
if k >= 10**6 and flag: quilty = five_sigmas(holder) return 1, kapital, t, quilty, n_list, m_list it += 1 quilty = five_sigmas(holder) if flag: return 0, kapital, t, quilty, n_list, m_list else: return 1, kapital, t, quilty, n_list, m_list, count_oc_accidents if __name__ == '__main__': date = Date(17, 1, 2019) n = 500 x = [] flag = 3 s = 0 if flag == 1: ar = [] kapitaly = [] repeat = 250 for i in range(repeat): holder = paris_ruin(k=500000, n=1, oc=1200000, m_oc=2400000, m=200, ac=120,
def test_dayOfYear(self): date = Date(1, 1, 2000) # print(date.dayOfYear()) self.assertEqual(date.dayOfYear(), 1) date = Date(1, 31, 2000) self.assertEqual(date.dayOfYear(), 31) date = Date(2, 29, 2000) self.assertEqual(date.dayOfYear(), 31 + 29) date = Date(3, 1, 2000) self.assertEqual(date.dayOfYear(), 31 + 29 + 1) date = Date(12, 31, 2000) self.assertEqual(date.dayOfYear(), 366)
def paris_ruin(k, n, oc, m_oc, m, ac, m_ac, mi, s_oc, start_year, end_year, flag=False, text=False, strategy=False): ''' k_0 - kapitał początkowy, n - liczba początkowych sprzedanych polis oc, oc- cena jednej polisy oc, m - liczba początkowo sprzedanyh polis ac, ac - cena jednej policy ac, m_oc, m_ac - są to odpowienio popyt na polisy oc i ac, mi - nakłady przeznaczane na marketing, s_oc - stała związana z popytem na polisy oc ''' lambda_days = [1.016, 1.0, 1.003, 1.027, 1.179, 0.961, 0.806] lambda_months = [ 0.735, 0.635, 0.726, 0.776, 1.04, 1.13, 1.221, 1.22, 1.202, 1.22, 1.024, 1.072 ] lambd_month = lambda_months[0] date = Date(1, 1, start_year) k_0 = k W_i = sum(np.random.exponential(0.05, n)) count_oc_accidents = 0 ac_accidents = ac_pay_moments(check_days(date.month, date.year), m) days = cycles(start_year, end_year) it = 1 n_list = [n] m_list = [m] credit = 0 is_credit = False bankruptcy_time = 0 credit_time = 0 kapital = [] t = [] holder = [] paid_per_month = [] paid_ac_per_month = [] while days >= it: kapital.append(k) t.append(it) '''Warunki bankructwa''' if k < -2 * k_0 - n * ac: quilty = five_sigmas(holder) return 0, kapital, t, quilty, n_list, m_list, count_oc_accidents if k < 0: bankruptcy_time += 1 else: bankruptcy_time = 0 if bankruptcy_time == 730: quilty = five_sigmas(holder) return 0, kapital, t, quilty, n_list, m_list, count_oc_accidents '''Podstawowe intensywności dla różnych rodzajów dni''' if date.holiday: lambd = lambda_months[7] * lambda_days[4] else: lambd_day = lambda_days[date.weekday] lambd = lambd_day * lambd_month '''Codzienne wypłaty.''' if W_i == 0: accidents = [] else: accidents = poisson_process(lambd * W_i, 1) count_oc_accidents += len(accidents) / n if len(accidents) != 0: to_pay_oc = sum(pareto(8.26, 200, len(accidents))) else: to_pay_oc = 0 ac_accidents_now = ac_accidents.count(date.day) if ac_accidents_now != 0: li_ac = [] for i in range(ac_accidents_now): li_ac.append( min(abs(np.random.standard_cauchy(1)[0]) / 2, 500 * ac)) to_pay_ac = sum(li_ac) else: to_pay_ac = 0 k = k - to_pay_oc - to_pay_ac paid_ac_per_month.append(to_pay_ac * ac_accidents_now) paid_per_month.append(to_pay_oc) holder.append(to_pay_oc) '''Kredyt.''' if is_credit: credit_time += 1 if k < 0 and not is_credit: credit = 120 * n credit_0 = credit k += credit is_credit = True if credit_time > 182 and (credit_time - 182) % 30 == 0: credit = credit - 1 / 12 * credit_0 if credit == -8 / 12 * credit_0: is_credit = False credit_time = 0 date.next_day() '''Sprawdzamy czy dzień jest ostatnim w miesiącu. ''' if 1 == date.day: if strategy: hold_this = oc_strategy(oc, m_oc, n, paid_per_month) oc = hold_this[0] ac = ac_strategy(ac, m_ac, m, paid_ac_per_month) mi = hold_this[1] k -= mi helper = end_of_month(n, oc, m_oc, m, ac, m_ac, mi, s_oc) n = helper[0] m = helper[1] ac_accidents = ac_pay_moments(check_days(date.month, date.year), m) W_i = sum(np.random.exponential(0.05, n)) n_list.append(n) m_list.append(m) if text: print(n, m, oc, ac) lambd_month = lambda_months[date.month - 1] k = k + n * oc + m * ac #print(sum(paid_per_month), n*oc, n) paid_ac_per_month = [] paid_per_month = [] if k >= 10**6 and flag: quilty = five_sigmas(holder) return 1, kapital, t, quilty, n_list, m_list it += 1 quilty = five_sigmas(holder) if flag: return 0, kapital, t, quilty, n_list, m_list else: return 1, kapital, t, quilty, n_list, m_list, count_oc_accidents
def test_isSolstice(self): date = Date(6, 21, 2019) self.assertEqual(date.isSummerSolstice(), True) date = Date(5, 21, 2019) self.assertEqual(date.isSummerSolstice(), False) date = Date(6, 22, 2019) self.assertEqual(date.isSummerSolstice(), False) date = Date(12, 22, 2019) self.assertEqual(date.isWinterSolstice(), True) date = Date(1, 22, 2019) self.assertEqual(date.isWinterSolstice(), False) date = Date(12, 23, 2019) self.assertEqual(date.isWinterSolstice(), False)
def test_month_zero(self): self.date = Date(1, 1, 1970) with self.assertRaises(ValueError): self.date.month = 0
def test_printCalendar(TestCase): date = Date(12,12,2019) printCalendar(date) date = Date(11,1,2019) printCalendar(date)
def main(): today = Date() print(today) print('\nTesting setMonth()') today.setMonth(-4) print(today) today.setMonth(13) print(today) today.setMonth(4.5) print(today) today.setMonth(4) print(today) print('\nTesting setYear()') today.setYear(2014.5) print(today) today.setYear(1581) print(today) today.setYear(-2) print(today) today.setYear(2014) print(today) print('\nTesting setDay()') today.setDay(31) print(today) today.setDay(-1) print(today) today.setDay(15.3) print(today) today.setDay(14) print(today)
def test__init__(self): "docstring" date = Date(0,0,0) print(date._toGregorian()) date = Date(0, 1, 2000) print(date._toGregorian()) date = Date(1, 0, 2000) print(date._toGregorian()) date = Date(1, 1, 0) print(date._toGregorian()) print("init test done") print()
def test_set_date(self): self.date = Date(1, 1, 1970) self.date.set_date(25, 9, 2019) self.assertListEqual([self.date.day, self.date.month, self.date.year], [25, 9, 2019])
def main(): date = Date(22, 12, 2011) print(date) date.add_days(20) print(date)
def test_year_negative(self): self.date = Date(1, 1, 1970) with self.assertRaises(ValueError): self.date.year = -1
def __init__(self): self.name = "Untitled" self.start_date = Date() self.due_date = Date() self.end_date = Date()
class Assignment: ### Initialize the Date object def __init__(self): self.name = "Untitled" self.start_date = Date() self.due_date = Date() self.end_date = Date() ### Prompt for the assignment from the user def prompt(self): self.name = input("Name: ") print() print("Start Date:") self.start_date.prompt() print() print("Due Date:") self.due_date.prompt() print() print("End Date:") self.end_date.prompt() ### Display the assignment def display(self): print("Assignment: {}".format(self.name)) print("Start Date:") self.start_date.display() self.start_date.display_long() print("Due Date:") self.due_date.display() self.due_date.display_long() print("End Date:") self.end_date.display() self.end_date.display_long()
return len(self._activities.keys()) def getActivity(self, date): return self._activities.get(date, None) def addActivity(self, date, activity): assert self._dateFrom <= date <= self._dateTo, "The date must be within the valid date range for the calendar" self._activities[date] = activity def displayMonth(self, month): activities = [(k, v) for k, v in self._activities.items() if k.month() == month] activities.sort(key=lambda kv: kv[0].day()) if len(activities) <= 0: print('No activity in month', month) else: print("%04d-%02d" % (self._dateTo.year(), month)) for a in activities: print(a[1]) if __name__ == '__main__': df = Date(2017, 1, 1) dt = Date(2017, 1, 30) ac = ActivitiesCalendar(df, dt) ac.displayMonth(1) ac.addActivity(Date(2017, 1, 9), "read book1") ac.addActivity(Date(2017, 1, 10), "read book2") ac.displayMonth(1)
def test_month_negative(self): self.date = Date(1, 1, 1970) with self.assertRaises(ValueError): self.date.month = -1