def repeat(): try: global db current = DateTime.now() db.connect() while True: for i in db.execute( "SELECT * FROM words ORDER BY RANDOM() LIMIT 1"): row = i timestamp = DateTime.as_object(row[4]) interval = timedelta(days=row[3]) if current - timestamp >= interval: result = row break else: continue db.disconnect() return dumps(list(result)) except Exception as e: print(e) return "ERROR"
def calculateSunriseSunsetFromDateTime(self, datetime, depression=0.833, isSolarTime=False): """Calculate sunrise, sunset and noon for a day of year.""" solDec, eqOfTime = self._calculateSolarGeometry(datetime) # calculate sunrise and sunset hour if isSolarTime: noon = .5 else: noon = (720 - 4 * math.degrees(self._longitude) - eqOfTime + self.timezone * 60) / 1440.0 try: sunRiseHourAngle = self._calculateSunriseHourAngle( solDec, depression) except ValueError: # no sun rise and sunset at this hour noon = 24 * noon return { "sunrise": None, "noon": DateTime(datetime.month, datetime.day, *self._calculateHourAndMinute(noon)), "sunset": None } else: sunrise = noon - sunRiseHourAngle * 4 / 1440.0 sunset = noon + sunRiseHourAngle * 4 / 1440.0 # convert demical hour to solar hour # noon = self._calculateSolarTime(24 * noon, eqOfTime, isSolarTime) # sunrise = self._calculateSolarTime(24 * sunrise, eqOfTime, isSolarTime) # sunset = self._calculateSolarTime(24 * sunset, eqOfTime, isSolarTime) noon = 24 * noon sunrise = 24 * sunrise sunset = 24 * sunset return { "sunrise": DateTime(datetime.month, datetime.day, *self._calculateHourAndMinute(sunrise)), "noon": DateTime(datetime.month, datetime.day, *self._calculateHourAndMinute(noon)), "sunset": DateTime(datetime.month, datetime.day, *self._calculateHourAndMinute(sunset)) }
def calculate_sunrise_sunset_from_datetime(self, datetime, depression=0.833, is_solar_time=False): """Calculate sunrise, sunset and noon for a day of year.""" sol_dec, eq_of_time = self._calculate_solar_geometry(datetime) # calculate sunrise and sunset hour if is_solar_time: noon = .5 else: noon = (720 - 4 * math.degrees(self._longitude) - eq_of_time + self.time_zone * 60) / 1440.0 try: sunrise_hour_angle = self._calculate_sunrise_hour_angle( sol_dec, depression) except ValueError: # no sun rise and sunset at this hour noon = 24 * noon return { "sunrise": None, "noon": DateTime(datetime.month, datetime.day, *self._calculate_hour_and_minute(noon)), "sunset": None } else: sunrise = noon - sunrise_hour_angle * 4 / 1440.0 sunset = noon + sunrise_hour_angle * 4 / 1440.0 noon = 24 * noon sunrise = 24 * sunrise sunset = 24 * sunset return { "sunrise": DateTime(datetime.month, datetime.day, *self._calculate_hour_and_minute(sunrise)), "noon": DateTime(datetime.month, datetime.day, *self._calculate_hour_and_minute(noon)), "sunset": DateTime(datetime.month, datetime.day, *self._calculate_hour_and_minute(sunset)) }
def _analemma_suns(self): """Calculate times that should be used for drawing analemma_curves. Returns: A list of list of analemma suns. """ for h in xrange(0, 24): if self._analemma_position(h) < 0: continue elif self._analemma_position(h) == 0: chours = [] # this is an hour that not all the hours are day or night prevhour = self.latitude <= 0 for hoy in xrange(h, 8760, 24): thishour = self.calculate_sun_from_hoy(hoy).is_during_day if thishour != prevhour: if not thishour: hoy -= 24 dt = DateTime.from_hoy(hoy) chours.append((dt.month, dt.day, dt.hour)) prevhour = thishour tt = [] for hcount in range(int(len(chours) / 2)): st = chours[2 * hcount] en = chours[2 * hcount + 1] if self.latitude >= 0: tt = [self.calculate_sun(*st)] + \ [self.calculate_sun(st[0], d, h) for d in xrange(st[1] + 1, 29, 7)] + \ [self.calculate_sun(m, d, h) for m in xrange(st[0] + 1, en[0]) for d in xrange(3, 29, 7)] + \ [self.calculate_sun(en[0], d, h) for d in xrange(3, en[1], 7)] + \ [self.calculate_sun(*en)] else: tt = [self.calculate_sun(*en)] + \ [self.calculate_sun(en[0], d, h) for d in xrange(en[1] + 1, 29, 7)] + \ [self.calculate_sun(m, d, h) for m in xrange(en[0] + 1, 13) for d in xrange(3, 29, 7)] + \ [self.calculate_sun(m, d, h) for m in xrange(1, st[0]) for d in xrange(3, 29, 7)] + \ [self.calculate_sun(st[0], d, h) for d in xrange(3, st[1], 7)] + \ [self.calculate_sun(*st)] yield tt else: yield tuple( self.calculate_sun((m % 12) + 1, d, h) for m in xrange(0, 13) for d in (7, 14, 21))[:-2]
def calculateSunFromHOY(self, hoy, isSolarTime=False): """Get Sun data for an hour of the year. Args: datetime: Ladybug datetime isSolarTime: A boolean to indicate if the input hour is solar time (Default: False). Returns: A sun object for this particular time """ datetime = DateTime.fromHoy(hoy) return self.calculateSunFromDataTime(datetime, isSolarTime)
def calculateSunriseSunset(self, month, day, depression=0.833, isSolarTime=False): """Calculate sunrise, noon and sunset. Return: A dictionary. Keys are ("sunrise", "noon", "sunset") """ datetime = DateTime(month, day, hour=12) return self.calculateSunriseSunsetFromDateTime(datetime, depression, isSolarTime)
def calculateSun(self, month, day, hour, isSolarTime=False): """Get Sun data for an hour of the year. Args: month: An integer between 1-12 day: An integer between 1-31 hour: A positive number between 0..23 isSolarTime: A boolean to indicate if the input hour is solar time. (Default: False) Returns: A sun object for this particular time """ datetime = DateTime(month, day, *self._calculateHourAndMinute(hour)) return self.calculateSunFromDataTime(datetime, isSolarTime)
def drawSunpath(self, hoys=None, origin=None, scale=1, sunScale=1, annual=True, remNight=True): """Create sunpath geometry. This method should only be used from the + libraries. Args: hoys: An optional list of hours of the year (default: None). origin: Sunpath origin (default: (0, 0, 0)). scale: Sunpath scale (default: 1). sunScale: Scale for the sun spheres (default: 1). annual: Set to True to draw an annual sunpath. Otherwise a daily sunpath is drawn. remNight: Remove suns which are under the horizon (night!). Returns: baseCurves: A collection of curves for base plot. analemmaCurves: A collection of analemmaCurves. dailyCurves: A collection of dailyCurves. suns: A list of suns. """ # check and make sure the call is coming from inside a plus library assert ladybug.isplus, \ '"drawSunPath" method can only be used in the [+] libraries.' hoys = hoys or () origin = origin or (0, 0, 0) try: origin = tuple(origin) except TypeError as e: # dynamo try: origin = origin.X, origin.Y, origin.Z except AttributeError: raise TypeError(str(e)) scale = scale or 1 sunScale = sunScale or 1 assert annual or hoys, 'For daily sunpath you need to provide at least one hour.' radius = 200 * scale # draw base circles and lines baseCurves = plus.baseCurves(origin, radius, self.northAngle) # draw analemma # calculate date times for analemma curves if annual: asuns = self._analemmaSuns() analemmaCurves = plus.analemmaCurves(asuns, origin, radius) else: analemmaCurves = () # add sun spheres if hoys: suns = tuple(self.calculateSunFromHOY(hour) for hour in hoys) else: suns = () if remNight: suns = tuple(sun for sun in suns if sun.isDuringDay) sunGeos = plus.sunGeometry(suns, origin, radius) # draw daily sunpath if annual: dts = (DateTime(m, 21) for m in xrange(1, 13)) else: dts = (sun.datetime for sun in suns) dsuns = self._dailySuns(dts) dailyCurves = plus.dailyCurves(dsuns, origin, radius) SPGeo = namedtuple('SunpathGeo', ('compassCurves', 'analemmaCurves', 'dailyCurves', 'suns', 'sunGeos')) # return outputs return SPGeo(baseCurves, analemmaCurves, dailyCurves, suns, sunGeos)