def get_dates(dt): output = [] """ Params date_arg: a date in format datetime Returns: master_list_sorted: list of dates within a week that includes the given date, plus matching day from the previous week. If a Monday date is provided,'current' dates will be the previous week, not the week that includes the Monday date """ if dt.isoweekday() == 1: #change dt to previous Sunday dt = dt - timedelta(days=1) else: dt = dt + timedelta(days=(7 - dt.isoweekday())) #change target to following Sunday for x in reversed(range(7)): result = { "current": dt - timedelta(days=x), "previous": dt - timedelta(days=x, weeks=1), } output.append(result) return output
def is_weekend(self, datetime): """ Returns True if datetime lands on a weekend. """ for weekend in self._weekends: if datetime.isoweekday() == weekend: return True return False
def is_weekend(self, datetime): """ Returns True if datetime lands on a weekend. """ for weekend in self.weekends: if datetime.isoweekday() == weekend: return True return False
def isRecordingTime(dt, DAY_START = time(8, 30),DAY_END = time(15, 18),NIGHT_START = time(20, 30),NIGHT_END = time(2, 33)): currentTime = dt.time() recording = False # 判断当前处于的时间段 if ((currentTime >= DAY_START and currentTime <= DAY_END) or (currentTime >= NIGHT_START) or (currentTime <= NIGHT_END)): recording = True # 过滤周末时间段 weekday = dt.isoweekday() if ((weekday == 6 and currentTime > NIGHT_END) or weekday == 7): recording = False return recording
def generate_data_frame(self, df, debug=False): def convert_to_target_group(value): return 1 if str(value) == str(self.target_group) else 0 def convert_to_light(value): light_aware_date_time = LightAwareDatetime(value, self.location) if light_aware_date_time.is_light(): return 1 elif light_aware_date_time.is_dark(): return 0 else: return -1 def convert_to_dusk(value): light_aware_date_time = LightAwareDatetime(value, self.location) return self.location.dusk(date=light_aware_date_time.date) import time start = time.time() def timecheck(note=None): if debug: print(note, time.time() - start) if self.datetime_column != "datetime": if "datetime" in df.columns: df.drop("datetime", axis=1, inplace=True) df.rename(columns={self.datetime_column: "datetime"}, inplace=True) timecheck("Before dropping na rows") df.dropna(how="any", subset=["datetime", self.target_column], inplace=True) timecheck("After dropping na rows") # set index to datetime timecheck("Before setting index") df.index = df.datetime timecheck("After setting index") timecheck("Before stripping non-evening hours") df = self._strip_non_evening_hours(df) timecheck("After stripping non-evening hours") if self.dst_restrict: timecheck("Before stripping non-seasonal days") df = self._strip_non_seasonal_days(df) df = self._strip_based_on_min_max_twilight(df) timecheck("After stripping non-seasonal days") timecheck("Before converting to target group") if "original_target" in df.columns: df.drop("original_target", axis=1, inplace=True) df.rename(columns={self.target_column: "original_target"}, inplace=True) if "target" in df.columns: df.drop("target", axis=1, inplace=True) df["target"] = df["original_target"].apply(convert_to_target_group) timecheck("After converting to target group") timecheck("Before converting to light") df["light"] = df["datetime"].apply(convert_to_light) if debug: df["dusk"] = df["datetime"].apply(convert_to_dusk) timecheck("After converting to light") timecheck("Before stripping twilight hours") df = self._strip_grey_hours(df) timecheck("After stripping twilight hours") timecheck("Before creating year column") df["year"] = df["datetime"].apply(lambda dt: dt.year) timecheck("After creating year column") df["month"] = df["datetime"].apply(lambda dt: dt.month) df["day_of_week"] = df["datetime"].apply(lambda dt: dt.isoweekday()) def time_in_seconds(dt): start_of_day = dt.replace(hour=0, minute=0, second=0, microsecond=0) return int(dt.timestamp() - start_of_day.timestamp()) df["time_in_seconds"] = df["datetime"].apply(time_in_seconds) if self.officer_id_column: df.rename(columns={self.officer_id_column: "officerid"}, inplace=True) df.officerid = df.officerid.apply(str) return df
contador += 1 fechaactual = fechadesde.strftime(formato) print(contador, fechaactual, 'es martes') fechadesde = fechadesde + timedelta(days=1) except: print('Fecha incorrecta') print(input(" continuar?")) limpiar() print("#########################################################") print("Obtener día de la semana por su número") print( "La función isoweekday() devuelve el número de día de la semana a que corresponda la fecha indicada, según los siguientes valores por día: 1-Lunes, 2-Martes, 3-Miércoles, 4-Jueves, 5-Viernes, 6-Sábado y 7-Domingo." ) dia_semana = datetime.isoweekday(fecha1) print(fecha1, "->", dia_semana, "->", tupla_diassem[dia_semana - 1]) print(input(" continuar?")) limpiar() print("#########################################################") print("Dado el ordinal se obtiene la fecha correspondiente") print("Si la fecha 01-01-0001 tiene el ordinal 1 entonces...") for num in range(1, 7): print("El día", 10**num, "se corresponde con", date.fromordinal(10**num)) print(input(" continuar?")) limpiar() print("#########################################################") print("Dada una fecha se obtiene un ordinal (01-01-0001 -> 1)") fecha3 = datetime(1, 1, 1, 0, 0, 0) print("La fecha", fecha3, "tiene el ordinal", date.toordinal(fecha3))
def get_weekday(i): return weekday[str(datetime.isoweekday(i))]
def OpenAllShutter(): print( 'Ouverture') # if(datetime.isoweekday(datetime.now())<=5): # Only Monday to Friday if(datetime.isoweekday(datetime.now())>10): # Never # if(datetime.isoweekday(datetime.now())>0): # Always url='http://localhost/domocan/www/php/CmdVR.php?carte=0x06&entree=0x0E&data0=0x26' # Volet Salon url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file time.sleep( 10 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x0A&entree=0x0B&data0=0x26' # VR Sdb 1 url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file time.sleep( 3 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x06&entree=0x09&data0=0x52' # VR haut cuisine url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file # if(datetime.isoweekday(datetime.now())<=5): # Only Monday to Friday if(datetime.isoweekday(datetime.now())>10): # Never # if(datetime.isoweekday(datetime.now())>0): # Always time.sleep( 0.5 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x02&entree=0x02&data0=0x26' # Chambre 3 url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file time.sleep( 0.5 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x02&entree=0x07&data0=0x26' # Chambre 4 url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file time.sleep( 0.5 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x04&entree=0x06&data0=0x26' # VR Fixe Chambre Nord url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file time.sleep( 0.5 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x04&entree=0x01&data0=0x26' # VR FChambre Nord url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file time.sleep( 0.5 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x0A&entree=0x04&data0=0x26' # VR Fixe Chambre Terrasse url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file time.sleep( 0.5 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x0A&entree=0x06&data0=0x26' # VR Chambre Terrasse url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file time.sleep( 0.5 ) url='http://localhost/domocan/www/php/CmdVR.php?carte=0x02&entree=0x09&data0=0x26' # VR Sdb 2 + Garage url_response = urllib2.urlopen(url) html = url_response.read() url_response.close() # best practice to close the file
import datetime from datetime import datetime, date, time import time import calendar test_day = datetime.strptime('19901204', '%Y%m%d').date() today = date.today() month_ago = date(2018, 4, 15) plus_one_year = date(2019, 4, 15) print(test_day) #for question #3 print(today - month_ago) #for question #4 print(datetime.isoweekday(plus_one_year)) #4 what day plus one year
def generate_data_frame(self, df, debug=False): def convert_to_target_group(value): return 1 if str(value) == str(self.target_group) else 0 def convert_to_light(value): light_aware_date_time = LightAwareDatetime(value, self.location) if light_aware_date_time.is_light(): return 1 elif light_aware_date_time.is_dark(): return 0 else: return -1 @lru_cache def cached_location_lookup(location, time_of_day, date, local): if time_of_day == "sunset": return location.sunset(date=date, local=local) elif time_of_day == "sunrise": return location.sunrise(date=date, local=local) elif time_of_day == "dusk": return location.dusk(date=date, local=local) elif time_of_day == "dawn": return location.dawn(date=date, local=local) def convert_to_light_vector( df, datetime_column: str, location: astral.Location ): timezone = pytz.timezone(location.timezone) df["_local_datetime"] = df[datetime_column].dt.tz_localize(timezone) df["_date"] = df["_local_datetime"].dt.date dates = df["_date"].tolist() df["_sunrise"] = [ cached_location_lookup(location, "sunrise", date=date, local=True) for date in dates ] df["_sunset"] = [ cached_location_lookup(location, "sunset", date=date, local=True) for date in dates ] df["_dawn"] = [ cached_location_lookup(location, "dawn", date=date, local=True) for date in dates ] df["_dusk"] = [ cached_location_lookup(location, "dusk", date=date, local=True) for date in dates ] df["_light"] = (df["_sunrise"] <= df["_local_datetime"]) & ( df["_local_datetime"] <= df["_sunset"] ) df["_dark"] = (df["_local_datetime"] < df["_dawn"]) | ( df["_local_datetime"] >= df["_dusk"] ) df["_gray"] = ~(df["_light"] | df["_dark"]) df["light"] = (df["_light"]).astype(int) return df def convert_to_dusk(value): light_aware_date_time = LightAwareDatetime(value, self.location) return self.location.dusk(date=light_aware_date_time.date) import time start = time.time() def timecheck(note=None): if debug: print(note, time.time() - start) if self.datetime_column != "datetime": if "datetime" in df.columns: df.drop("datetime", axis=1, inplace=True) df.rename(columns={self.datetime_column: "datetime"}, inplace=True) timecheck("Before dropping na rows") df.dropna(how="any", subset=["datetime", self.target_column], inplace=True) timecheck("After dropping na rows") # set index to datetime timecheck("Before setting index") df.index = df.datetime timecheck("After setting index") timecheck("Before stripping non-evening hours") df = self._strip_non_evening_hours(df) timecheck("After stripping non-evening hours") if self.dst_restrict: timecheck("Before stripping non-seasonal days") df = self._strip_non_seasonal_days(df) df = self._strip_based_on_min_max_twilight(df) timecheck("After stripping non-seasonal days") timecheck("Before converting to target group") if "original_target" in df.columns: df.drop("original_target", axis=1, inplace=True) df.rename(columns={self.target_column: "original_target"}, inplace=True) if "target" in df.columns: df.drop("target", axis=1, inplace=True) df["target"] = df["original_target"].apply(convert_to_target_group) timecheck("After converting to target group") timecheck("Before converting to light") df = convert_to_light_vector(df, "datetime", self.location.location) # df["light"] = df["datetime"].apply(convert_to_light) if debug: df["dusk"] = df["datetime"].apply(convert_to_dusk) timecheck("After converting to light") timecheck("Before stripping twilight hours") df = self._strip_grey_hours(df) timecheck("After stripping twilight hours") timecheck("Before creating year column") df["year"] = df["datetime"].apply(lambda dt: dt.year) timecheck("After creating year column") df["month"] = df["datetime"].apply(lambda dt: dt.month) df["day_of_week"] = df["datetime"].apply(lambda dt: dt.isoweekday()) def time_in_seconds(dt): start_of_day = dt.replace(hour=0, minute=0, second=0, microsecond=0) return int(dt.timestamp() - start_of_day.timestamp()) df["time_in_seconds"] = df["datetime"].apply(time_in_seconds) if self.officer_id_column: df.rename(columns={self.officer_id_column: "officerid"}, inplace=True) df.officerid = df.officerid.apply(str) return df