def days2exp(exyear, exmonth, exday): """ Calculate the days to expiry from today """ tnow = date.today() expiry = date(exyear, exmonth, exday) days2expiry = abs(expiry - tnow) return int(days2expiry.days)
def print_selection(): msg = "你所選擇要進入的是{:}系統".format(var.get()) BotSpeak.speak(msg) Ans = tk.messagebox.askyesno(title='小提醒', message='你是否選擇要進入' + var.get() + "的頁面?") if Ans: if var.get() == "備忘錄": Login() elif var.get() == "行事曆": date() # pass elif var.get() == "生活小助手": self.openFrame3() self.win_ch.destroy() #關閉畫面
def __calculate_rtt_year_prorata(self, date_now): # calculate the distance between the actual date in the year and it's # beginning, used to calculate the prorata rtt = 52 - int( 52*(date_now - date(date_now.year)) // (365 + (date_now.year % 4 == 0) - (date_now.year % 100 == 0) + (date_now.year % 400 == 0) ) )/2
def CONNECTED_VPN(): logging.info("") logging.info("STATE ---> CONNECTED_VPN") global STATE, vpn_problem_counter #-------------------------------------------- check vpn ------------------------------------ time.sleep(1) counter = 0 while True: if vpn_check() == "OK": LED_E3(True) time.sleep(1) LED_E3(True) time.sleep(1) LED_E3(True) vpn_problem_counter = 0 update_db("adsl_table", "vpn_ip", vpn_ip() ) #DB update_db("adsl_table", "vpn_ip_method", ip_method('Vpn') ) #DB update_db("ports_table", "vpn_123",check_123()) #DB update_db("ports_table", "vpn_80",check_80()) #DB mydate = date() #logging.info("mydate: %s" %mydate) if mydate != "KO": update_db("adsl_table", "last_check", mydate ) #DB update_db("ports_table", "last_check", mydate ) #DB # dump_general_table() # dump_adsl_table() # dump_ports_table() time.sleep(60) # 10 min STATE = "CONNECTED_VPN" return elif (counter<2): counter += 1 logging.debug("[CONNECTED_VPN] %s try to vpn_check failed ! Retry in 3s" %counter) time.sleep(1) continue else: logging.debug("[CONNECTED_VPN] %s try to vpn_check failed !!!" %counter) vpn_problem_counter += 1 try: eth_down() LEDS_OFF() time.sleep(1) LEDS_OFF() except: logging.error("[CONNECTED_VPN] impossible to eth_down(point:3)") if (vpn_problem_counter<10): logging.info("Total refreshing in 30s ... vpn_problem_counter: %s" %vpn_problem_counter) update_db("general_table", "connection_type","NONE") #DB time.sleep(30) STATE = "START" return else: logging.error("[CONNECTED_VPN] VPN PROBLEM ----> NEED_GPRS \n\n\n") update_db("general_table", "connection_type","NONE") #DB STATE = "NEED_GPRS" return
def NEED_GPRS(): logging.info("") logging.info("STATE ---> NEED_GPRS") global STATE #--------------------------------- GPRS UP ---------------------------------------- counter = 0 while True: if gprs_up() == "OK": time.sleep(7) update_db("general_table", "connection_type","GPRS") #DB update_db("modem_table", "gprs_ip", gprs_ip() ) #DB update_db("modem_table", "gprs_ip_method", ip_method('Modem') ) #DB update_db("ports_table", "gprs_123",check_123()) #DB update_db("ports_table", "gprs_80",check_80()) #DB mydate = date() #logging.info("mydate: %s" %mydate) if mydate != "KO": update_db("general_table", "last_check", mydate ) #DB update_db("modem_table", "last_check", mydate ) #DB update_db("ports_table", "last_check", mydate ) #DB # dump_modem_table() # dump_ports_table() break elif (counter<2): counter += 1 logging.debug("[NEED_GPRS] %s try to gprs_up failed ! Retry in 3s" %counter) time.sleep(3) continue else: logging.debug("[NEED_GPRS] %s try to gprs_up failed !!!" %counter) logging.debug("Total refreshing in 30s ...") LEDS_OFF() time.sleep(1) LEDS_OFF() time.sleep(30) STATE = "START" return #----------------------- miab GET via GPRS ------------------------------------ counter = 0 while True: if miab() == "OK": #miab OK LED_E3(True) time.sleep(0.1) LED_GbE(False) time.sleep(0.1) LED_GPRS(True) time.sleep(0.1) LED_E3(True) time.sleep(0.1) LED_GbE(False) time.sleep(0.1) LED_GPRS(True) time.sleep(0.1) LED_E3(True) time.sleep(30) #wait then go to first check of gprs STATE = "CONNECTED_GPRS" return elif (counter<2): #miab NO time.sleep(0.1) gprs_up() time.sleep(6) counter += 1 logging.debug("[NEED_GPRS] %s try to miab_gprs failed ! Retry in 3s" %counter) continue else: #miab NO for 3 time LEDS_OFF() time.sleep(0.5) LEDS_OFF() logging.debug("[NEED_GPRS] %s try to miab_gprs failed !!!" %counter) logging.debug("Total refreshing in 30s ...") time.sleep(30) STATE = "START" return
update_db("general_table", "message","CONNECTED_VPN") #DB LED_GPRS(False) time.sleep(0.2) LED_GbE(True) time.sleep(0.2) LED_GPRS(False) time.sleep(0.2) LED_GbE(True) time.sleep(0.3) LED_GbE(True) time.sleep(0.1) CONNECTED_VPN() except Exception, e: update_db("general_table", "message","ERROR") #DB update_db("general_table", "last_check",date()) #DB logging.error("[FSM ERROR] CONNECTED_VPN ERROR ---> %s !!!" %str(e) ) LEDS_OFF() STATE = "ERROR" time.sleep(60) continue elif STATE == "CONNECTED_GPRS": try: update_db("general_table", "message","CONNECTED_GPRS") #DB LED_GbE(False)
import date print(date(2020,2,3) - date(2020,3,31))
def get_from-date(self): return self.from-date
def FSM_RUN(): # -------------------------------- Finite State Machine (FSM) ------------------------------------------ global STATE, TIMER while True: if STATE == "START": try: update_db("general_table", "message","START") #DB LEDS_OFF() #cleaning ... time.sleep(0.1) eth_down() time.sleep(0.1) gprs_down() time.sleep(0.1) LEDS_OFF() time.sleep(0.1) #Start FSM ... START() except Exception, e: update_db("general_table", "message","ERROR") #DB update_db("general_table", "last_check",date()) #DB logging.error("[FSM ERROR] START ERROR ----> %s !!!" %str(e) ) LEDS_OFF() STATE = "ERROR" time.sleep(60) continue elif STATE == "NEED_GPRS": try: update_db("general_table", "message","NEED_GPRS") #DB LEDS_OFF() time.sleep(0.2) LEDS_OFF() time.sleep(0.2) NEED_GPRS() except Exception, e: update_db("general_table", "message","ERROR") #DB update_db("general_table", "last_check",date()) #DB logging.error("[FSM ERROR] NEED_GPRS ERROR ---> %s !!!" %str(e) ) LEDS_OFF() STATE = "ERROR" time.sleep(60) continue
import datetime x = datetime.date.() print(x) import datetime d = datetime.date(2019, 4, 13) print(d) We can only import date class from the datetime module. Here's how: from datetime import date a = date(2019, 4, 13) print(a) Example 4: Get current date You can create a date object containing the current date by using a classmethod named today(). Here's how: from datetime import date today = date.today() print("Current date =", today)
def nope(): print(date()) pass
class date(builtins.object) | date(year, month, day) --> date object |
| __rsub__(self, value, /) | Return value-self. | | __str__(self, /) | Return str(self). | | __sub__(self, value, /) | Return self-value. | | astimezone(...) | tz -> convert to local time in new timezone tz | | ctime(...) | Return ctime() style string. | | date(...) | Return date object with same year, month and day. | | dst(...) | Return self.tzinfo.dst(self). | | isoformat(...) | [sep] -> string in ISO 8601 format, YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM]. | sep is used to separate the year from the time, and defaults to 'T'. | timespec specifies what components of the time to include (allowed values are 'auto', 'hours', 'minutes', 'seconds', 'milliseconds', and 'microseconds'). | | replace(...) | Return datetime with new specified fields. | | time(...) | Return time object with same time but with tzinfo=None.
if __name__ == '__main__': input_filename = sys.argv[1] f = open(input_filename, 'r') # Read number of deposits no_of_deposits = int(f.readline()) print("\nDeposit Date Amount ") depositDates = [] depositAmounts = [] total_purchased = 0 total_sold = 0 for i in range(no_of_deposits): line = f.readline() line = ' '.join(line.split()) line = line.split(' ')[:4] deposit_date = date(int(line[0]), int(line[1]), int(line[2])) amount = float(line[3]) if amount > 0: total_purchased += amount else: total_sold += (-amount) depositDates.append(deposit_date) depositAmounts.append(amount) print(deposit_date, " ", amount) line = f.readline() line = ' '.join(line.split()) line = line.split(' ')[:4] currentDate = date(int(line[0]), int(line[1]), int(line[2])) currentAmount = float(line[3]) print("\nTotal Value Purchased:", total_purchased) print("Total Value Sold:", total_sold)
def CONNECTED_GPRS(): logging.info("") logging.info("STATE ---> CONNECTED_GPRS") global STATE, TIMER TIMER += 1 #Refreshing timer #-------------------------------------------- check gprs ------------------------------------------ counter = 0 while True: if gprs_check() == "OK": if(TIMER>14): #time to refresh the system to check if eth0 is up again time.sleep(3) logging.info("STATE ----> START") TIMER = 1 try: eth_down() time.sleep(1) gprs_down() time.sleep(1) except: logging.error("[CONNECTED_GPRS] ERROR (point:4)") STATE = "START" return else: #check miab to verify if gprs is really up time.sleep(1) counter_miab = 0 while True: if miab() == "OK": #miab:OK => OK LED_E3(True) time.sleep(1) LED_E3(True) time.sleep(1) LED_E3(True) logging.debug("[CONNECTED_GPRS] gprs_check: OK ... TIMER:%s",TIMER) update_db("modem_table", "gprs_ip", gprs_ip() ) #DB update_db("modem_table", "gprs_ip_method", ip_method('Modem') ) #DB update_db("ports_table", "gprs_123",check_123()) #DB update_db("ports_table", "gprs_80",check_80()) #DB mydate = date() #logging.info("mydate: %s" %mydate) if mydate != "KO": update_db("modem_table", "last_check", mydate ) #DB update_db("ports_table", "last_check", mydate ) #DB # dump_general_table() # dump_modem_table() # dump_ports_table() time.sleep(60) #10 min sleep STATE = "CONNECTED_GPRS" return elif (counter_miab<2): LED_E3(False) time.sleep(0.5) LED_E3(False) time.sleep(0.5) LED_E3(False) #miab:KO => Try again gprs_up() counter_miab += 1 logging.debug("[CONNECTED_GPRS] %s try to miab_gprs failed ! Retry in 3s" %counter_miab) time.sleep(5) continue else: #miab:KO => Shutting down gprs go to total refreshing LEDS_OFF() time.sleep(1) LEDS_OFF() logging.debug("[CONNECTED_GPRS] %s try to gprs_check failed !!!" %counter) gprs_down() logging.debug("Total refreshing in 30s ...") update_db("general_table", "connection_type","NONE") #DB time.sleep(30) STATE = "START" return elif (counter<3): #gprs_check:KO => Try again counter += 1 logging.debug("[CONNECTED_GPRS] %s try to gprs_check failed ! Retry in 3s" %counter) time.sleep(3) continue else: #gprs_check:KO => Shutting down gprs go to total refreshing logging.debug("[CONNECTED_GPRS] %s try to gprs_check failed !!!" %counter) gprs_down() LEDS_OFF() time.sleep(0.5) LEDS_OFF() logging.debug("Total refreshing in 30s ...") update_db("general_table", "connection_type","NONE") #DB time.sleep(30) STATE = "START" return
def START(): logging.info("") logging.info("STATE ----> START") global STATE update_db("general_table", "host", host() ) #DB update_db("general_table", "serial_code", serial_code() ) #DB update_db("general_table", "mac", mac() ) #DB #--- eth up --- counter = 0 while True: if eth_up() == "OK": time.sleep(7) update_db("general_table", "connection_type","ETH") #DB update_db("adsl_table", "eth_ip",eth_ip()) #DB update_db("adsl_table", "eth_ip_method",ip_method('Eth')) #DB update_db("adsl_table", "vpn_ip","---") #DB update_db("ports_table", "eth_1194",check_1194()) #DB update_db("ports_table", "eth_123",check_123()) #DB update_db("ports_table", "eth_80",check_80()) #DB update_db("adsl_table", "dns1_ip",dns1_ip()) #DB update_db("adsl_table", "dns2_ip",dns2_ip()) #DB update_db("adsl_table", "gateway",Gateway_ip()) #DB mydate = date() #logging.info("mydate: %s" %mydate) if mydate != "KO": update_db("general_table", "last_check", mydate ) #DB update_db("adsl_table", "last_check", mydate ) #DB update_db("ports_table", "last_check", mydate ) #DB # dump_general_table() # dump_adsl_table() # dump_ports_table() break elif (counter<2): counter += 1 logging.debug("[START] %s try to eth_up failed ! Retry in 2s" %counter) time.sleep(1) continue else: logging.debug("[START] %s try to eth_up failed !!!" %counter) STATE = "NEED_GPRS" return #--- GET via eth --- counter = 0 while True: if miab() == "OK": LED_E3(True) time.sleep(0.2) LED_E3(True) time.sleep(0.2) LED_E3(True) time.sleep(0.2) break elif (counter<2): try: eth_down()#eth down time.sleep(1) eth_up()#eth up time.sleep(15) except: logging.error("[START] Impossible to reset eth !!! (point:1)") counter += 1 logging.debug("[START] %s try to miab_eth failed ! Retry in 6s" %counter) continue else: LED_E3(False) time.sleep(0.3) LED_E3(False) logging.debug("[START] %s try to miab_eth failed !!!" %counter) eth_down() time.sleep(0.3) STATE = "NEED_GPRS" return #--- VPN UP --- counter = 0 while True: if vpn_up() == "OK": update_db("general_table", "connection_type","VPN") #DB update_db("adsl_table", "vpn_ip",vpn_ip() ) #DB update_db("adsl_table", "vpn_ip_method", ip_method('Vpn')) #DB update_db("ports_table", "vpn_123",check_123()) #DB update_db("ports_table", "vpn_80",check_80()) #DB mydate = date() #logging.info("mydate: %s" %mydate) if mydate != "KO": update_db("general_table", "last_check", mydate ) #DB update_db("adsl_table", "last_check", mydate ) #DB update_db("ports_table", "last_check", mydate ) #DB # dump_adsl_table() # dump_ports_table() STATE = "CONNECTED_VPN" return elif (counter<2): counter += 1 logging.debug("[START] %s try to vpn_up failed ! Retry in 3s" %counter) try: date() #logging.info("---> |%s|" %date() ) except: logging.error("[START] Impossible to set date from miab !!! (point:2)") time.sleep(3) continue else: logging.debug("[START] %s try to vpn_up failed !!!" %counter) eth_down() STATE = "NEED_GPRS" return
You are given the following information, but you may prefer to do some research for yourself. - 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. - A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? ''' rom datetime import date def is_sunday(day): return day.weekday() == 6 def main(initial_date, final_date): sundays = 0 for y in range(initial_date.year, final_date.year+1): sundays += sum([1 for m in range(1, 13) if is_sunday(date(y, m, 1))]) print(sundays) if __name__ == '__main__': initial_date = date(1901, 1, 1) final_date = date(2000, 12, 31) main(initial_date, final_date)
import date date1 = date(2019, 12, 1) date2 = date(2019, 12, 10) diff = date2 - date1 print("diff of dates", diff.days)
def main(initial_date, final_date): sundays = 0 for y in range(initial_date.year, final_date.year+1): sundays += sum([1 for m in range(1, 13) if is_sunday(date(y, m, 1))]) print(sundays)
invest = map(invItem,investors) inv = map(invItem, investors) return [sum(nbrOfDays)/float(len(nbrOfDays)), sum(inv)/float(len(inv))] def getDistance(investor,avg): datediff = date.today() - investor[1] investValue = pow(pow(datediff.days,2)/float(avg[0]) + pow(investor[2],2),0.5)/float(avg[1]) investor.append(investValue) def getRanking(investors): investors.sort(key=operator.itemgetter(3),reverse=True) Invest = [["Daniel", date(2016, 10, 1), 500 ],["Svennis", date(2016, 9, 21), 600 ],["Kasto", date(2016, 10, 9), 500], ["Johan", date(2016, 9, 23), 501],["Rolf", date(2016, 9, 22), 500 ]] avg = getMean(Invest) for i in range(0,len(Invest)): getDistance(Invest[i], avg) getRanking(Invest) print "Nuvarande turordning:" for i in Invest: print i[0]
password = config.get("DB_Config", "DATABASE_PASSWORD") charset = config.get("DB_Config", "DATABASE_CHARSET") #print(host, port, dbname, username, password) conn = pymysql.connect(host=host, port=int(port), user=username, charset=charset, passwd=password, db=dbname) cursor = conn.cursor() cursor.execute("SELECT VERSION()") row = cursor.fetchone() try: with conn.cursor() as cursor: # 执行sql语句,插入记录 sql = 'INSERT INTO employees (first_name, last_name, hire_date, gender, birth_date) VALUES (%s, %s, %s, %s, %s)' cursor.execute(sql, ('Robin', 'Zhyea', 'tomorrow', 'M', date(1989, 6, 14))) # 没有设置默认自动提交,需要主动提交,以保存所执行的语句 conn.commit() finally: conn.close() print("MySQL server version:", row[0]) cursor.close() conn.close()