예제 #1
0
 def test_sched_calc_5(self):
     """11 months since last run"""
     sched = Subscription(send_to=self.dummy_user, report=self.report, frequency='Yearly',
                     email_subject='nosend', time=nowish, start_date=d.today()-relativedelta(months=11),
                     last_scheduled_run=dt_today-relativedelta(months=11))
     sched.save()
     self.assertFalse(sched.should_send())
예제 #2
0
def monthly_sales(request, month=None):
    """A report of sales totals a single month"""

    def get_total_sales(orders_list):
        return orders_list.aggregate(Sum('order_total'))

    # first determine today, all dates below based on today
    today = datetime.now()

    # then filter Order objects based on parameter

    if month == 'previous':
        # use relativedelta to get last month
        last_month = today + relativedelta(months=-1)
        orders_list = Order.objects.filter(order_date__year=today.year, 
            order_date__month=last_month.month)
        total_sales = get_total_sales(orders_list)
    
    elif month == 'three':
        start_date = today + relativedelta(months=-3)
        orders_list = Order.objects.filter(order_date__range=(start_date, today))
        total_sales = get_total_sales(orders_list)  
    else:
        orders_list = Order.objects.filter(order_date__year=today.year,
            order_date__month=today.month)
        total_sales = get_total_sales(orders_list)

    return render(request, 'products/monthly_sales.html', {
        'orders_list': orders_list,
        'total_sales': total_sales,
        })
예제 #3
0
 def test_sched_calc_4(self):
     """25 days after last run"""
     sched = Subscription(send_to=self.dummy_user, report=self.report, frequency='Monthly',
                     email_subject='nosend', time=nowish, start_date=d.today()-relativedelta(days=25),
                     last_scheduled_run=dt_today-relativedelta(days=25))
     sched.save()
     self.assertFalse(sched.should_send())
예제 #4
0
파일: utils.py 프로젝트: anjos/django-audit
def daily_popularity(q, days, legend, style):
  """Calculates a popularity barchart per day."""

  if not q: return

  maximum = q.aggregate(Max('date'))['date__max']
  maximum = datetime.datetime(maximum.year, maximum.month, maximum.day, 0, 0, 0)
  minimum = maximum - relativedelta(days=days)

  intervals = [minimum + relativedelta(days=k) for k in range(days)]
  intervals += [maximum, maximum + relativedelta(days=1)]

  log = q.exclude(AnonymousQ)
  unlog = q.filter(AnonymousQ).exclude(RobotQ)
  bots = q.filter(AnonymousQ).filter(RobotQ)

  bars = []
  for k in range(len(intervals)-1):
    bars.append({
      'label0': days-k, 
      'logged': log.filter(date__gte=intervals[k]).filter(date__lt=intervals[k+1]).count(),
      'anon': unlog.filter(date__gte=intervals[k]).filter(date__lt=intervals[k+1]).count(),
      'bots': bots.filter(date__gte=intervals[k]).filter(date__lt=intervals[k+1]).count(),
      })

  return bar_line_chart(bars, legend, style)
예제 #5
0
파일: risk_tests.py 프로젝트: nulvinge/risk
    def test_failedLoginCountLastWeek(self):
        user = '******'
        now = datetime.now()
        time1 = self.timeToStr(now + relativedelta(days=-1))
        time2 = self.timeToStr(now + relativedelta(days=-2))
        time3 = self.timeToStr(now + relativedelta(days=-3))
        time5 = self.timeToStr(now + relativedelta(days=-5))
        time8 = self.timeToStr(now + relativedelta(days=-8))

        rv = self.failedLoginCountLastWeek()
        assert "0" in rv.data
        rv = self.failed(user, time=time1)
        rv = self.failedLoginCountLastWeek()
        assert "1" in rv.data

        rv = self.failed(user, time=time8)
        rv = self.failedLoginCountLastWeek()
        assert "1" in rv.data

        rv = self.failed(user, time=time2)
        rv = self.failedLoginCountLastWeek()
        assert "2" in rv.data

        rv = self.successful(user, time=time3)
        rv = self.failedLoginCountLastWeek()
        assert "2" in rv.data

        rv = self.failed(user, time=time5)
        rv = self.failedLoginCountLastWeek()
        assert "3" in rv.data
예제 #6
0
def update_csv(month, year):
    import csv

    if (year == "default") or (month == "default"):
        now = datetime.datetime.now()
        ff = now + relativedelta(months=+1)
        month = ff.month
        year = ff.year

    cyclestart = patch_tuesday(month, year)
    cycleend = cyclestart + relativedelta(months=+1)
    cycleend = patch_tuesday(cycleend.month, cycleend.year)

    ifile = open("bigfix-scheduled-jobs.csv", "rb")
    reader = csv.reader(ifile)
    ofile = open("new_schedule.csv", "wb")
    writer = csv.writer(ofile)
    rownum = 0
    for row in reader:
        print row[0]
        if (rownum == 0) or (row[0] == "comment"):
            writer.writerow(row)
        else:
            row[5] = window_start_day(cyclestart, int(row[2]), row[3])
            if row[4] == "unrestricted":
                row[7] = cycleend
            else:
                jobopen = window_start_datetime(row[5], row[6])
                jobclose = jobopen + relativedelta(hours=+int(row[8]))
                row[7] = jobclose
            writer.writerow(row)
        rownum += 1

    ifile.close()
    ofile.close()
예제 #7
0
    def get_report_data_custom(self, ids, context={}):
        company_id = get_active_company()
        comp = get_model("company").browse(company_id)
        if ids:
            params = self.read(ids, load_m2o=False)[0]
        else:
            params = self.default_get(load_m2o=False, context=context)
        settings = get_model("settings").browse(1)
        date_from = params.get("date_from")
        date_to = params.get("date_to")
        d0 = datetime.strptime(date_from, "%Y-%m-%d")
        year_date_from = d0.strftime("%Y-01-01")  # XXX: get company fiscal year
        prev_date_from = (d0 - timedelta(days=1) - relativedelta(day=1)).strftime("%Y-%m-%d")
        prev_date_to = (d0 - timedelta(days=1) + relativedelta(day=31)).strftime("%Y-%m-%d")

        year_date_from_prev_year = (
            datetime.strptime(year_date_from, "%Y-%m-%d") - relativedelta(years=1)).strftime("%Y-%m-%d")
        date_from_prev_year = (datetime.strptime(date_from, "%Y-%m-%d") - relativedelta(years=1)).strftime("%Y-%m-%d")
        date_to_prev_year = (datetime.strptime(date_to, "%Y-%m-%d") - relativedelta(years=1)).strftime("%Y-%m-%d")

        data = {
            "date_from": date_from,
            "date_to": date_to,
            "year_date_from": year_date_from,
            "prev_date_from": prev_date_from,
            "prev_date_to": prev_date_to,
            "company_name": comp.name,
            "year_date_from_prev_year": year_date_from_prev_year,
            "date_from_prev_year": date_from_prev_year,
            "date_to_prev_year": date_to_prev_year,
        }
        print("data", data)
        return data
예제 #8
0
    def test_multiday_streak_longest_and_current(self):
        today = datetime.date.today()
        d2 = today - relativedelta(days=1)
        d3 = today - relativedelta(days=2)
        runs = [
            models.Run(date=d3, distance=4),
            models.Run(date=d2, distance=4),
            models.Run(date=today, distance=4),
        ]

        streaks = models.User._calculate_streaks(runs)

        expected_streaks = {
            'longest': {
                'length': 3,
                'start': d3.strftime(
                    "%m/%d/%Y"),
                'end': today.strftime(
                    "%m/%d/%Y")
            },
            'current': {
                'length': 3,
                'start': d3.strftime(
                    "%m/%d/%Y"),
                'end': today.strftime(
                    "%m/%d/%Y")
            },
        }

        self.assertEqual(streaks, expected_streaks)
예제 #9
0
파일: eurex.py 프로젝트: cmdawson/mrmarket
	def opt_expiry(self, month, year):
		"""Last Friday prior to the first calendar day of the option expiration month, followed
		by at least two exchange days prior to the first calendar day of the option expiration
		month. Exception: If this Friday is not an exchange day, or if this Friday is not an
		exchange day and followed by only one exchange day prior to the first calendar day of
		the option expiration month then the exchange day immediately preceding that Friday is
		the Last Trading Day. An exchange day within the meaning of this exception is a day,
		which is both an exchange day at Eurex and a Federal workday in the US.
	
		I've ignored the bit about "Federal workdays" since the only federal holiday that
		could be relevant is xmas and it's Eurex holiday anyway.""" 
		bom = datetime(year,month,1, self.hh)
		bom3p = business_day(bom,-3,self.holidays)

		fri1 = bom + relativedelta(weekday=FR(-1))
		fri2 = bom3p + relativedelta(weekday=FR(-1))

		if is_holiday(fri1, self.holidays) and business_day(fri1, 1, self.holidays) < bom:
			return business_day(fri1, -1, self.holidays)

		elif is_holiday(fri2, self.holidays):
			return business_day(fri2, -1, self.holidays)

		else:
			return fri2
예제 #10
0
    def testRelativeDeltaFractionalRepr(self):
        rd = relativedelta(years=3, months=-2, days=1.25)

        self.assertEqual(repr(rd), "relativedelta(years=+3, months=-2, days=+1.25)")

        rd = relativedelta(hours=0.5, seconds=9.22)
        self.assertEqual(repr(rd), "relativedelta(hours=+0.5, seconds=+9.22)")
예제 #11
0
    def calculate_points(self):
        day = datetime.datetime(*strptime("2007-09-01", "%Y-%m-%d")[0:5])

        while True:

            if day.date().month >= datetime.date.today().month and day.date().year >= datetime.date.today().year:
                break

            check = AccountsTrophies.objects.filter(date=day.date())

            if len(check) > 0:
                last_day = day + relativedelta(day=31)
                day = last_day + datetime.timedelta(days=1)
                continue

            # 'day' is always first day of month
            for trophy in Trophies.objects.all().order_by('priority', 'position'):
                at = UsersTable.objects.filter(table_type='m', date=day.date(), season_id=settings.CURRENT_SEASON_ID,
                                               league_id=trophy.league_id).order_by('position')[
                     int(trophy.position) - 1:int(trophy.position)]

                if len(at) == 1:
                    at = at[0]
                    AccountsActivity(user_id=at.user.id, username=str(at.username), dst_id=0, action_type='trophies',
                                     points=trophy.points, is_calculated=0, is_confirmed=1,
                                     created_at=day.date()).save()
                    AccountsTrophies(user_id=at.user.id, username=str(at.username), trophy_id=trophy.id,
                                     league_id=trophy.league_id, season_id=settings.CURRENT_SEASON_ID,
                                     priority=trophy.priority, date=day.date()).save()

            last_day = day + relativedelta(day=31)
            day = last_day + datetime.timedelta(days=1)
예제 #12
0
파일: metainfo.py 프로젝트: great/pyclassic
	def nextkey(self):
		if (self.catalog):
			tmp_date = parse(self.catalog[-1].split('.')[-1] + '01')
			next = tmp_date + relativedelta(months=+1)
			return next.strftime("%Y%m")
		else:
			return (date.today() + relativedelta(months=+1)).strftime("%Y%m")
예제 #13
0
def computeRecurrings(firstRecurringDatetime, repeat):
    """Compute recurring datetimes according to repeat value"""
    #get tzinfo
    tzinfo = firstRecurringDatetime.tzinfo
    #create new datetime without tzinfo (mandatory to use dateutil.rrule lib:S)
    dt = datetime(firstRecurringDatetime.year, firstRecurringDatetime.month, firstRecurringDatetime.day, firstRecurringDatetime.hour, firstRecurringDatetime.minute)
    #get current date infos
    now = datetime.now()
    today = datetime(now.year, now.month, now.day)
    until = today + relativedelta(months=+2)
    #compute reccurings
    recurrings = []
    if repeat==0:
        #not a recurring schedule
        recurrings = [dt]
    elif repeat==1:
        #repeat every day
        recurrings = list(rrule(DAILY, until=until, dtstart=dt))
    elif repeat==7:
        #repeat every week
        recurrings = list(rrule(WEEKLY, until=until, dtstart=dt))
    elif repeat==31:
        #repeat every month
        recurrings = list(rrule(MONTHLY, until=until, dtstart=dt))
    elif repeat==365:
        #repeat every year
        #need to save at least 2 schedules, otherwise it will be lost by purge
        until = today + relativedelta(years=+1, months=+1)
        recurrings = list(rrule(YEARLY, until=until, dtstart=dt))
    #read tzinfo to computed recurrings
    fixedRecurrings = []
    for recurring in recurrings:
        fixedRecurrings.append(datetime(recurring.year, recurring.month, recurring.day, recurring.hour, recurring.minute, 0, 0, tzinfo))
    return fixedRecurrings
예제 #14
0
def parse_date(s):
    today = datetime.datetime.utcnow()
    offset = re.match(r'^(\d+)([ymwdhs]|min)$', s)

    if offset:
        units = {'y': 'years', 'm': 'months', 'w': 'weeks', 'd': 'days',
            'h': 'hours', 'min': 'minutes', 's': 'seconds'}
        unit = units[offset.group(2)]
        value = -int(offset.group(1))
        kw = {unit: value}
        date = today + relativedelta(**kw)
    elif re.match(r'^\d\d\d\d$', s):
        date = parsetime(s) + relativedelta(yearday=1)
    elif re.match(r'^\d\d\d\d[-/]\d\d$', s):
        date = parsetime(s) + relativedelta(day=1)
    elif re.match(r'^(\d\d)?\d\d[-/]\d\d[-/]\d\d$', s):
        date = parsetime(s)
    elif re.match(r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\+\d\d:\d\d)?$', s):
        try:
            # try converting timezone if one is specified
            date = parsetime(s).astimezone(utc)
        except ValueError:
            # otherwise default to UTC if none is specified
            date = parsetime(s).replace(tzinfo=utc)
    else:
        msg = '"{}" is not a valid date argument'.format(s)
        raise argparse.ArgumentTypeError(msg)
    return (s, date.isoformat())
예제 #15
0
def print_orders(char, api):
    """List active orders"""

    active_orders = [order for oid, order in char.orders().result.iteritems() if order['status'] == 'active']
    if not active_orders: return

    # sort by timestamp, first ones to expire on top
    active_orders = sorted(active_orders, key=lambda item: item['timestamp'], reverse=False)

    print("Orders (%d):" % len(active_orders))

    total_isk = 0

    for order in active_orders:
        total_isk += order['price'] * order['amount_left']

        td = relativedelta((datetime.fromtimestamp(order['timestamp']) + relativedelta(days=order['duration'])), datetime.now())
        tdstr = "%dd %dh %dm" % (td.days, td.hours, td.minutes)

        msg = (u"  %-50s  %17s %4d units end: %s" % (typeid_to_string(order['type_id']),
                                                     format_currency(order['price']),
                                                     order['amount_left'],
                                                     tdstr))
        print(msg.encode('utf-8'))

    print("Total: %s" % format_currency(total_isk))
예제 #16
0
파일: sla.py 프로젝트: pnuckowski/selena
def calculatesla():
    # SlaCache object for bulk create
    sla_cache_create = []

    services = Service.objects.filter(is_active=True).only('id').order_by('id')
    for service in services:
        last_day = _find_start_date(service)
        diff = datetime.date.today() - last_day
        for day in range(diff.days):
            if day > 0:
                _calculate_SLA(day, service)

        sla7d = '%.2f' % _calculate_cache(service, timezone.now() - datetime.timedelta(days=WEEK))
        sla1m = '%.2f' % _calculate_cache(service, timezone.now() + relativedelta(months=-ONE_MONTH))
        sla3m = '%.2f' % _calculate_cache(service, timezone.now() + relativedelta(months=-THREE_MONTHS))
        # create SlaCache and append it to list
        sla_cache_create.append(SlaCache(service_id=service.id,
                                         sla7days=sla7d,
                                         sla1month=sla1m,
                                         sla3months=sla3m))

    # delete all cache objects.
    SlaCache.objects.filter(pk__in=[sc.service_id for sc in sla_cache_create]).delete()
    # create all SlaCache objects in bulk.
    SlaCache.objects.bulk_create(sla_cache_create)
    def handle(self, *args, **options):

        try:
            sdate = options['sdate']
        except KeyError:
            raise OptionValueError("option %s: invalid start date value: %r. Should have a format like 'YYYY/MM/DD'" % ('-s', options['sdate']))
        try:
            edate = options['edate']
        except KeyError:
            raise OptionValueError("option %s: invalid end date value: %r. Should have a format like 'YYYY/MM/DD'" % ('-s', options['edate']))

        try:
            wstate = options['wstate']
            state = WFSTATE[wstate]
        except KeyError:
            raise OptionValueError("option %s: invalid Workflow state. Valid state valies are new, ack, open, resolved, closed, reopen'" % ('-s', options['edate']))

        from datetime import date
        from time import mktime, strptime

        sdate = datetime.fromtimestamp(mktime(strptime(sdate, "%Y/%m/%d")))
        edate = datetime.fromtimestamp(mktime(strptime(edate, "%Y/%m/%d")))

        month_ranges = [(dt, dt + relativedelta(months= +1) + relativedelta(days=-1)) for dt in rrule.rrule(rrule.MONTHLY, sdate, until=edate)]
        for month in month_ranges:
            deps = Complaint.objects.filter(created__range = month).filter(curstate = state).values('department').annotate(n=Count('pk'))
            for dep in deps:
                d = ComplaintDepartment.objects.get(pk = dep['department'])
                from django.utils.encoding import smart_str
                print month[0].strftime("%Y-%m-%d"), ",", smart_str(d.name), ",", dep['n']
예제 #18
0
def bucket_command(args):
    if len(args) < 3:
        print fail("bucket (days|weeks) (num|time|var) filter_re")
        return

    time_len = args[0]

    sum_var = args[1]

    filter_re = " ".join(args[2:])

    events = get_events(filter_re)

    if len(events) == 0:
        print fail("No events")
        return

    bucketed = events.bucket(time_len)

    # output
    if time_len == 'months':
        gap = relativedelta(months=1)
    elif time_len == 'weeks':
        gap = relativedelta(weeks=1)
    else:
        gap = relativedelta(days=1)

    last = sorted(bucketed)[-1]
    i = sorted(bucketed)[0]
    while True:
        val = bucketed[i].get_sum_var(sum_var)
        print "%s\t%s" % (okblue(str(i)), val)
        i += gap
        if i > last:
            break
    def getDuration(self, starttime, endtime):
        
        if endtime > starttime:
            rdelta = relativedelta(endtime, starttime)
        else:
            endtime = endtime + timedelta(days=+1)
            rdelta = relativedelta(endtime, starttime)

        output = u""
        
        if rdelta.days > 0:
            if rdelta.days == 1:
                suffix = " day "
            else:
                suffix = " days "

            output = output + str(rdelta.days) + suffix
                    
        if rdelta.hours > 0:
            if rdelta.hours == 1:
                suffix = " hr "
            else:
                suffix = " hrs "

            output = output + str(rdelta.hours) + suffix
            
        if rdelta.minutes > 0:
            if rdelta.minutes == 1:
                suffix = " min "
            else:
                suffix = " mins "

            output = output + str(rdelta.minutes) + suffix
        
        return output
예제 #20
0
def get_embargo_values(embargoed=None, embargoed_until=None, embargo_days_from_now=None):
    if isinstance(embargoed, basestring):
        embargoed = embargoed.strip()
    if isinstance(embargoed_until, basestring):
        embargoed_until = embargoed_until.strip()
    if isinstance(embargo_days_from_now, basestring):
        embargo_days_from_now = embargo_days_from_now.strip()
    e_status=None
    e_date=None
    if embargoed == None:
        #No embargo details are supplied by user
        e_status = True
        e_date = (datetime.now() + relativedelta(years=+70)).isoformat()
    elif embargoed==True or embargoed.lower() in ['true', '1'] :
        #embargo status is True
        e_status = True
        e_date = None
        if embargoed_until:
            try:
                e_date = parse(embargoed_until, dayfirst=True, yearfirst=False).isoformat()
            except:
                e_date = (datetime.now() + relativedelta(years=+70)).isoformat()
        elif embargo_days_from_now:
            if embargo_days_from_now.isdigit():
                e_date = (datetime.now() + timedelta(days=int(embargo_days_from_now))).isoformat()
            else:
                e_date = (datetime.now() + relativedelta(years=+70)).isoformat()
    elif embargoed==False or embargoed.lower() in ['false', '0'] :
        e_status = False
    else:
        #Default case: Treat it as though embargo=None
        e_status = True
        e_date = (datetime.now() + relativedelta(years=+70)).isoformat()
    return (e_status, e_date)
예제 #21
0
    def test_send_reminders(self,
                        reminder_name,
                        monthday,
                        byhour,
                        byminute,                    
                        additional_reminders_to_send,
                        default_query=ServiceDeliveryPoint.objects.all(),
                        bysetpos=-1,
                        send_initial=True,
                        message_kwargs={}):
        
        now = callbacks._get_current_time()
        additional_reminders_to_send_count = len(additional_reminders_to_send)
        sdp_status_type = ServiceDeliveryPointStatusType.objects.filter(short_name=reminder_name)[0:1].get()
        
        #create a date rule (for business day logic) - last weekday prior to monthday
        if monthday:
            start_time = get_last_business_day_on_or_before(now + relativedelta(months=-1, hour = byhour, minute = byminute, microsecond=0))
            end_time = get_last_business_day_on_or_before(now + relativedelta(hour = byhour, minute = byminute, microsecond=0))
        else:
            start_time = now + relativedelta(months=-1, 
                                             day=get_last_business_day_of_month((now + relativedelta(months=-1)).year, 
                                                                                (now + relativedelta(months=-1)).month))
        
            end_time = now + relativedelta(day=get_last_business_day_of_month(now.year, 
                                                                              now.month))

        return [start_time, end_time]
예제 #22
0
def get_dates(use_defaults=True):
    start_date = request.params.get('start_date')
    if start_date is not None and not isinstance(start_date, date):
        try:
            start_date = parse_date(start_date)
        except:
            start_date = None

    if start_date is None and use_defaults:
        start_date = date.today() + relativedelta(day=1)

    end_date = request.params.get('end_date')
    if end_date is not None and not isinstance(end_date, date):
        try:
            end_date =  parse_date(end_date)
        except:
            end_date = None

    if end_date is None and use_defaults:
        end_date = start_date + relativedelta(days=-1, months=+1)

    if end_date is not None and start_date is not None:
        if end_date < start_date:
            (start_date, end_date) = (end_date, start_date)

    return (start_date, end_date)
예제 #23
0
 def test_fax_is_billed(self):
     site = self.site
     current_month = date.today().replace(day=1)
     last_day_of_previous_month = current_month - timedelta(days=1)
     site.signup_date = last_day_of_previous_month - timedelta(days=40)
     for i in range(5):
         Fax.objects.create(
             site=site,
             page_count=2,
             transaction='000',
             timestamp=date.today()+relativedelta(months=-2),
             logged=True
         )
     for i in range(5):
         Fax.objects.create(
             site=site,
             page_count=2,
             transaction='000',
             timestamp=date.today()+relativedelta(months=-1),
             logged=False
         )
     invoice = site.create_invoice()
     fax_cost = site.plan.fax_page_cost * 10
     self.assertEqual(fax_cost, invoice.fax_charges)
     self.assertEqual(fax_cost + site.plan.monthly_cost, invoice.total)
 def testMonthsOfDiffNumOfDays(self):
     self.assertEqual(date(2003, 1, 27)+relativedelta(months=+1),
                      date(2003, 2, 27))
     self.assertEqual(date(2003, 1, 31)+relativedelta(months=+1),
                      date(2003, 2, 28))
     self.assertEqual(date(2003, 1, 31)+relativedelta(months=+2),
                      date(2003, 3, 31))
예제 #25
0
    def testComparison(self):
        d1 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1, minutes=1, seconds=1, microseconds=1)
        d2 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1, minutes=1, seconds=1, microseconds=1)
        d3 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1, minutes=1, seconds=1, microseconds=2)

        self.assertEqual(d1, d2)
        self.assertNotEqual(d1, d3)
    def testRelativeDeltaFractionalNegativeOverflow(self):
        # Equivalent to (days=-1)
        rd1 = relativedelta(days=-0.5, hours=-12)
        self.assertEqual(rd1.normalized(),
            relativedelta(days=-1))

        # Equivalent to (days=-1)
        rd2 = relativedelta(days=-1.5, hours=12)
        self.assertEqual(rd2.normalized(),
            relativedelta(days=-1))

        # Equivalent to (days=-1, hours=-14, minutes=-45)
        rd3 = relativedelta(days=-1.5, hours=-2.5, minutes=-15)
        self.assertEqual(rd3.normalized(),
            relativedelta(days=-1, hours=-14, minutes=-45))

        # Equivalent to (days=-1, hours=-14, minutes=+15)
        rd4 = relativedelta(days=-1.5, hours=-2.5, minutes=45)
        self.assertEqual(rd4.normalized(),
            relativedelta(days=-1, hours=-14, minutes=+15))

        # Carry back up - equivalent to:
        # (days=-2, hours=-2, minutes=0, seconds=-2, microseconds=-3)
        rd3 = relativedelta(days=-1.5, hours=-13, minutes=-59.50045,
                            seconds=-31.473, microseconds=-500003)
        self.assertEqual(rd3.normalized(),
            relativedelta(days=-2, hours=-2, minutes=0,
                          seconds=-2, microseconds=-3))
예제 #27
0
파일: utils.py 프로젝트: anjos/django-audit
def weekly_popularity(q, legend, style):
  """Calculates a popularity barchart per week."""

  if not q: return

  minimum = q.aggregate(Min('date'))['date__min'] 
  minimum = datetime.datetime(minimum.year, minimum.month, minimum.day, 0, 0, 0) + relativedelta(weekday=MO(-1))
  maximum = q.aggregate(Max('date'))['date__max']
  maximum = datetime.datetime(maximum.year, maximum.month, maximum.day, 0, 0, 0) + relativedelta(weekday=MO(-1))
  weeks = (maximum - minimum).days/7

  intervals = [minimum + relativedelta(weeks=k) for k in range(weeks)]
  intervals += [maximum, maximum + relativedelta(weeks=1)]

  log = q.exclude(AnonymousQ)
  unlog = q.filter(AnonymousQ).exclude(RobotQ)
  bots = q.filter(AnonymousQ).filter(RobotQ)
  bars = []
  for k in range(len(intervals)-1):
    bars.append({
      'label0': intervals[k].strftime('%U'), 
      'label1': intervals[k].strftime('%Y'),
      'logged': log.filter(date__gte=intervals[k]).filter(date__lt=intervals[k+1]).count(),
      'anon': unlog.filter(date__gte=intervals[k]).filter(date__lt=intervals[k+1]).count(),
      'bots': bots.filter(date__gte=intervals[k]).filter(date__lt=intervals[k+1]).count(),
      })

  return bar_line_chart(bars, legend, style)
예제 #28
0
파일: views.py 프로젝트: mfalcon/edujango
def f_cashflow_semanal(request):
    today = date.today()
    weekday = date.today().isoweekday() # Monday 0, Sunday 6
    first_day_of_week = today+relativedelta(days=-(weekday+1)) #me posiciono en el lunes de la semana actual
    dates_list = []
    for d in range(7):
        dates_list.append(first_day_of_week+relativedelta(days=+d)) # FIX MEesta guardando sabado y domingo de la sem pasada

    pago_cuotas = [(p, 'ingreso') for p in Pago.objects.filter(fecha__range=(dates_list[0],dates_list[6]))] # abona
    pagos_unicos = [(u, 'ingreso_unico') for u in UnicoPago.objects.filter(fecha__range=(dates_list[0],dates_list[6]))]
    gastos = [(g, 'gasto') for g in Gasto.objects.filter(fecha__range=(dates_list[0],dates_list[6]))] # importe
    sueldos =[(s, 'sueldo') for s in PagoSueldo.objects.filter(fecha__range=(dates_list[0],dates_list[6]))] #abona
    lista_mov = pago_cuotas + pagos_unicos + gastos + sueldos

    ingresos = sum([p[0].abona for p in pago_cuotas]) + sum([pu[0].importe for pu in pagos_unicos])
    gastos = sum([g[0].importe for g in gastos]) + sum([s[0].abona for s in sueldos])

    balance = ingresos - gastos

    context = {
        'movimientos' : lista_mov, 'ingresos': ingresos, 'gastos': gastos,
        'balance': balance,
    }
    return render_to_response(
        'jardin/cashflow_semanal.html',
         context,
         context_instance = RequestContext(request),
    )
예제 #29
0
def command_maze(bot, user, channel, args):
    try:
        show = pytvmaze.get_show(show_name=args, embed="episodes")
    except pytvmaze.exceptions.ShowNotFound:
        bot.say(channel, "Show '%s' not found" % args)
        return

    next_episode = None
    next_delta = None

    now = datetime.now(pytz.timezone("Europe/Helsinki"))

    # go through the episodes in reverse order
    for episode in reversed(show.episodes):
        # episode has id and name, but no airstamp yet (not announced)
        if not episode.airstamp:
            continue

        delta = relativedelta(parse(episode.airstamp), now)

        # episode is in the past, stop searching
        if delta.months <= 0 and delta.days <= 0 and delta.minutes <= 0:
            break
        else:
            # episode is (still) in the future
            next_episode = episode
            next_delta = delta

    # No new episodes found
    if not next_episode:
        # ended show, find latest episode
        if show.status == "Ended":
            next_episode = show.episodes.pop()
            latest_delta = relativedelta(now, parse(next_episode.airstamp))
        else:
            # Still running, ep airdate not yet known
            bot.say(channel, "No new episodes found for %s" % show.name)
            return

    show_id = "%s %s '%s'" % (
        show.name,
        "%dx%02d" % (next_episode.season_number, next_episode.episode_number),
        next_episode.title,
    )

    if show.status == "Ended":
        msg = "Latest episode of %s aired on %s (%s ago) on %s [Ended]" % (
            show_id,
            next_episode.airdate,
            _ago(latest_delta),
            show.network["name"],
        )
    else:
        msg = "Next episode of {0} airs {1} ({2})".format(show_id, next_episode.airdate, _ago(next_delta))
        # Not all shows have network info for some reason
        if show.network:
            msg = "{0} on {1}".format(msg, show.network["name"])

    bot.say(channel, msg.encode("UTF-8"))
    def testMonthEndMonthBeginningLeapYear(self):
        self.assertEqual(relativedelta(datetime(2012, 1, 31, 23, 59, 59),
                                       datetime(2012, 3, 1, 0, 0, 0)),
                         relativedelta(months=-1, seconds=-1))

        self.assertEqual(relativedelta(datetime(2003, 3, 1, 0, 0, 0),
                                       datetime(2003, 1, 31, 23, 59, 59)),
                         relativedelta(months=1, seconds=1))
예제 #31
0
           'Friday':4, 'Saturday':5, 'Sunday':6}
           
remotePath = local.remote
           
DEBUGGING = False
   

if __name__ == '__main__':
    
    tab = '    '
    #pp = pprint.PrettyPrinter(indent=4)
    
    #Now = DT.datetime.now() + relativedelta(microsecond=0)

    print '===================================================================='
    print 'UPLOAD_ARCHIVE.py ', str(DT.datetime.now() + relativedelta(microsecond=0))
    print '===================================================================='    
    
    #startDelta = local.startDelta
    #endDelta = local.endDelta
    
	'''
    rootFolder = ''.join((local.remoteStub,'Audio4/'))
    
    startTuple = (2016, 7,15,11,54,30)
    endTuple = (2016,7,15,12,2,00)
    targetFolder = rootFolder
    targetFile = 'NNN-Fri-TEST.mp3'
    sftp, success = uploadArchive(startTuple, endTuple, targetFolder, targetFile)
    print 'sftp -> ', str(sftp), ' ', str(type(sftp))
    #ftp.close()  
예제 #32
0
async def linear_predict(rent: Rent):
    """
    Forecast apartment rent for 3, 6, 12 months 
    This is just Linear model. It won't show great forecast accuracy 
    Created to determine base line and for test purposes.
    """
    city = rent.city.lower().title()
    state = rent.state.lower().title()

    now = datetime.today()
    # get date 3 months, 6 months, 12 months in the future for our forecasting
    month_3 = now + relativedelta(months=+3)
    month_6 = now + relativedelta(months=+6)
    month_12 = now + relativedelta(months=+12)

    # create 4 separate dfs based on data provided for linear regression model
    df0 = pd.DataFrame(data={
        "Month": now.month,
        "Year": now.year,
        "City": city
    },
                       index=[0])

    df1 = pd.DataFrame(data={
        "Month": month_3.month,
        "Year": month_3.year,
        "City": city
    },
                       index=[0])

    df2 = pd.DataFrame(data={
        "Month": month_6.month,
        "Year": month_6.year,
        "City": city
    },
                       index=[0])

    df3 = pd.DataFrame(data={
        "Month": month_12.month,
        "Year": month_12.year,
        "City": city
    },
                       index=[0])

    # create dummy variables for cities
    dum_data0 = pd.get_dummies(df0,
                               prefix=[""],
                               prefix_sep="",
                               columns=["City"],
                               drop_first=False)
    dum_data1 = pd.get_dummies(df1,
                               prefix=[""],
                               prefix_sep="",
                               columns=["City"],
                               drop_first=False)
    dum_data2 = pd.get_dummies(df2,
                               prefix=[""],
                               prefix_sep="",
                               columns=["City"],
                               drop_first=False)
    dum_data3 = pd.get_dummies(df3,
                               prefix=[""],
                               prefix_sep="",
                               columns=["City"],
                               drop_first=False)

    # encode state variable
    encoded_state = encoder.transform([[state]])
    dum_data0["Encoded_States"] = encoded_state
    dum_data1["Encoded_States"] = encoded_state
    dum_data2["Encoded_States"] = encoded_state
    dum_data3["Encoded_States"] = encoded_state

    # get the data excluding target (just to get columns names later)
    X_data = data.drop(columns=['Price'])
    # get missing columns
    missing_cols = set(X_data.columns) - set(dum_data1.columns)

    # fill up dataframe with missing colunmns
    for c in missing_cols:
        dum_data0[c] = 0
        dum_data1[c] = 0
        dum_data2[c] = 0
        dum_data3[c] = 0

    # Ensure the order of columns in the dataframes is the same as in data
    dum_data0 = dum_data0[X_data.columns]
    dum_data1 = dum_data1[X_data.columns]
    dum_data2 = dum_data2[X_data.columns]
    dum_data3 = dum_data3[X_data.columns]

    # predict prices
    pred0 = float(linear_model.predict(dum_data0)[0])
    pred1 = float(linear_model.predict(dum_data1)[0])
    pred2 = float(linear_model.predict(dum_data2)[0])
    pred3 = float(linear_model.predict(dum_data3)[0])

    # get historical data
    sub = data[(data[city] == 1) & (data["Year"] == 2020)]
    date_col = pd.to_datetime(sub[['Year', 'Month']].assign(DAY=1))
    sub["date"] = date_col.apply(lambda x: x.date().strftime("%Y-%m-%d"))
    sub.index = pd.RangeIndex(start=1, stop=12, step=1)
    last_year_price = sub[["date", "Price"]].to_dict(orient="index")

    a = {
        "city": city,
        "price": {
            "historical": last_year_price,
            "today": {
                "price": pred0,
                "date": str(now.date())
            },
            "forecast_3": {
                "price": pred1,
                "date": str(month_3.date())
            },
            "forecast_6": {
                "price": pred2,
                "date": str(month_6.date())
            },
            "forecast_12": {
                "price": pred3,
                "date": str(month_12.date())
            }
        }
    }

    return a
예제 #33
0
def prescription_view(request):
	response = HttpResponse(['content'],content_type='application/pdf')
	response['Content-Disposition'] = 'inline; filename="prescription.pdf"'

	# ~ PAGE_HEIGHT = defaultPageSize[1]; PAGE_WIDTH = defaultPageSize[0]

	# ~ BottomMargin = 0.4 * inch
	# ~ TopMargin = 10 * mm
	# ~ LeftMargin = .1 * inch
	# ~ RightMargin = .1 * inch
	# ~ ContentBottomMargin = TopMargin + 0.25 * inch
	# ~ ContentTopMargin = BottomMargin + 0.35 * inch
	# ~ ContentLeftMargin = LeftMargin
	# ~ ContentRightMargin = RightMargin

	fs19 = 19 - 4 #1
	fs17 = 17 #2
	fs14 = 14 #3
	fs12 = 12 #4
	fs11 = 11 #5
	fs10 = 10

	buffer = BytesIO()

	help = 'Generate & print prescription forms'
	## Doctor data
	dr = Doctor.objects.get(id = 1)
	l1 = dr.first_name + " " + dr.last_name + " " + dr.suffix
	l2 = dr.diplomate
	l3 = dr.hosp_main
	l4 = dr.hours_am
	l5 = dr.hours_pm
	l6 = dr.telephone
	l7 = dr.lic_no
	l8 = dr.ptr_no
	l9 = dr.s2_no


	lw1 = stringWidth(l1,'Helvetica-Bold', fs19)
	lw2 = stringWidth(l2, 'Helvetica', fs12)

	## Visit data
	vi = Visit.objects.latest('id')
	vi_id = vi.id
	vi_pat = str(vi.patient)
	pa_id = vi.patient.id
	vi_twn = str(vi.patient.town)
	vi_gen = str(vi.patient.gender)
	dob = vi.patient.date_of_birth
	age = str(relativedelta(date.today(), dob).years)
	today = str(date.today())

	def coord(x,y,unit=mm):
		x, y = x * unit, y * unit
		return x, y

	#pr1 = canvas.Canvas("prescription.pdf",pagesize = HALF_LETTER)
	#pr1 = canvas.Canvas(buffer)
	pr1 = canvas.Canvas(response)

	pr1.pagesize = HALF_LETTER
	#pr1.pagesize = A5

	y = 100 * mm

	''' This draws the header info on the prescription form.
	y = up from bottom
	x = distance from left side
	'''

	pr1.setLineWidth(.5)

	## This generates the doctor's information
	### Line 1 = Doctor Name

	x = 10
	#y -= 5
	pr1.setFont('Helvetica-Bold', fs19)
	pr1.drawString(*coord(x, y, mm), str(l1))

	### Line 2 Diplomate
	pr1.setFont('Helvetica', fs12)
	x = 15
	y -= 5
	pr1.drawString(*coord(x, y, mm), str(l2))

	### Line 3 Main Hospital
	pr1.setFont('Helvetica-Bold', fs12)
	x = 10
	y -= 8
	pr1.drawString(*coord(x, y, mm), str(l3)) ## Main Hosp

	### Line 4 Mon-Fri AM line
	pr1.setFont('Helvetica', fs11)
	y -= 5
	pr1.drawString(*coord(x, y, mm), 'Mon. - Fri. :')
	pr1.drawString(*coord(x + 20, y, mm), str(l4)) ## AM Hours

	### Line 5 Mon - Fri PM line
	y -= 5
	pr1.drawString(*coord(x + 20, y, mm), str(l5)) ## PM hours
	## Affils
	pr1.setFont('Helvetica-Bold', fs12)
	pr1.drawString(*coord(x + 70, y, mm), 'HOSPITAL AFFILIATIONS')

	### Line 6 Sat AM line
	pr1.setFont('Helvetica', fs11)
	y -= 5
	pr1.drawString(*coord(x, y, mm), 'Saturday :')
	pr1.drawString(*coord(x + 20, y, mm), str(l4)) ## AM Hours

	### Line 6a NOPH
	pr1.setFont('Helvetica', fs11)
	pr1.drawString(*coord(x + 70, y, mm), 'NOPH')

	### Line 7 Telephone
	y -= 5
	pr1.setFont('Helvetica', fs11)
	pr1.drawString(*coord(x, y, mm), 'Tel. No. :')
	pr1.drawString(*coord(x + 20, y, mm), str(l6))

	### Line 7a Holy Child Hosp
	pr1.setFont('Helvetica', fs11)
	pr1.drawString(*coord(x + 70, y, mm), 'Holy Child Hospital')

	### Line between heading & patient
	pr1.setFont('Helvetica', fs12)
	# x = 12
	y -= 4
	pr1.line(x * mm, y * mm, x + 130 * mm, y * mm)
	y -= 1
	pr1.line(x * mm, y * mm, x + 130 * mm, y * mm)

	## This generates the patient's information
	pr1.setFont('Helvetica', fs11)
	y -= 5
	pr1.drawString(*coord(x, y, mm),'Patient')
	pr1.line(x + 27*mm, y*mm, x + 80*mm, y*mm)
	pr1.drawString(*coord(x + 20,y,mm), vi_pat)

	pr1.drawString(*coord(x + 75, y, mm), 'Age')
	pr1.line(x + 90*mm, y*mm, x + 100*mm, y*mm)
	pr1.drawString(*coord(x + 85, y, mm), age)

	pr1.drawString(*coord(x + 100, y, mm), 'Sex')
	pr1.line(x + 115*mm, y*mm, x + 125*mm, y*mm)
	pr1.drawString(*coord(x + 110, y, mm), vi_gen)

	y -= 6

	pr1.drawString(*coord(x, y, mm), 'Address')
	pr1.line(x + 27 *mm, y*mm, x + 80*mm, y*mm)
	pr1.drawString(*coord(x + 20, y, mm), vi_twn)

	pr1.drawString(*coord(x + 75, y, mm), 'Date')
	pr1.line(x + 90*mm, y*mm, 120*mm, y*mm)
	pr1.drawString(*coord(x + 85, y, mm), today)

	x += 10
	y -= 5

	pr2 = Prescription.objects.filter(visit_id = vi.id)
	for pr in pr2:
		y -= 5
		mr = str(pr.medicine_reminder) + " for " + str(pr.medicine_duration_days) + " days."

		pr1.setFont('Helvetica-Bold', fs12)
		pr1.setFillColor(colors.green)
		pr1.drawString(*coord(x, y, mm), str(pr.medicine_generic))

		pr1.setFont('Helvetica', fs11)
		pr1.setFillColor(colors.black)
		pr1.drawString(*coord(x + 40, y, mm), str(pr.medicine_dose))

		pr1.drawString(*coord(x + 80, y ,mm), str(pr.medicine_quantity))

		pr1.setFillColor(colors.blue)
		pr1.drawString(*coord(x + 10, y - 5, mm), str(pr.medicine_brand))
		pr1.setFillColor(colors.black)

		pr1.setFont('Helvetica', fs10)
		pr1.drawString(*coord(x, y - 10, mm), str(mr))
		pr1.line(x*mm, (y - 15)*mm, 120*mm, (y - 15)*mm )

		y -= 15

	### Prescription footer data
	x = 80
	y = 120
	pr1.setFont('Helvetica', fs12)
	pr1.line(x*mm, y*mm, (x + 35)*mm, y*mm)
	pr1.drawString(*coord(x + 36, y, mm), 'M.D.')

	pr1.drawString(*coord(x, y - 4, mm), "Lic No.:")
	pr1.drawString(*coord(x + 18, y - 4, mm), str(l7))
	pr1.line((x + 18)*mm, (y - 4)*mm, (x + 40)*mm, (y - 4)*mm)

	pr1.drawString(*coord(x, y - 8, mm), "PTR No.")
	pr1.drawString(*coord(x + 18, y - 8, mm), str(l8))
	pr1.line((x + 18)*mm, (y - 8)*mm, (x + 40)*mm, (y - 8)*mm )

	pr1.drawString(*coord(x, y - 12, mm), "S2 No:")
	pr1.drawString(*coord(x + 18, y - 12, mm), str(l9))
	pr1.line((x + 18)*mm, (y - 12)*mm, (x + 40)*mm, (y - 12)*mm )

	# ~ p1 = os.popen('lpr', 'w')
	# ~ p1.write(str(pr1))
	# ~ p1.close()
	pr1.showPage()
	pr1.save()
	pdf = buffer.getvalue()
	buffer.close()
	response.write(pdf)
	return response
예제 #34
0
 def year_gen(iter_start_dt, iter_end_dt):
     iter_dt = iter_start_dt
     while iter_dt < iter_end_dt:
         yield iter_dt.strftime('%Y-%m-%d')
         iter_dt += relativedelta(years=+1)
예제 #35
0
def from_scene_et_fraction(scene_coll, start_date, end_date, variables,
                           interp_args, model_args, t_interval,
                           _interp_vars=['et_fraction', 'ndvi'],
                           use_joins=False):
    """Interpolate from a precomputed collection of Landast ET fraction scenes

    Parameters
    ----------
    scene_coll : ee.ImageCollection
        Non-daily 'et_fraction' images that will be interpolated.
    start_date : str
        ISO format start date.
    end_date : str
        ISO format end date (exclusive, passed directly to .filterDate()).
    variables : list
        List of variables that will be returned in the Image Collection.
    interp_args : dict
        Parameters from the INTERPOLATE section of the INI file.
        # TODO: Look into a better format for showing the options
        interp_method : {'linear}, optional
            Interpolation method.  The default is 'linear'.
        interp_days : int, str, optional
            Number of extra days before the start date and after the end date
            to include in the interpolation calculation. The default is 32.
    model_args : dict
        Parameters from the MODEL section of the INI file.  The reference
        source and parameters will need to be set here if computing
        reference ET or actual ET.
    t_interval : {'daily', 'monthly', 'annual', 'custom'}
        Time interval over which to interpolate and aggregate values
        The 'custom' interval will aggregate all days within the start and end
        dates into an image collection with a single image.
    use_joins : bool, optional
        If True, use joins to link the target and source collections.
        If False, the source collection will be filtered for each target image.
        This parameter is passed through to interpolate.daily().
    _interp_vars : list, optional
        The variables that can be interpolated to daily timesteps.
        The default is to interpolate the 'et_fraction' and 'ndvi' bands.

    Returns
    -------
    ee.ImageCollection

    Raises
    ------
    ValueError

    Notes
    -----
    This function currently assumes that "mask" and "time" bands exist in the
    scene collection.

    """
    # Get interp_method
    if 'interp_method' in interp_args.keys():
        interp_method = interp_args['interp_method']
    else:
        interp_method = 'linear'
        logging.debug('interp_method was not set, default to "linear"')

    # Get interp_days
    if 'interp_days' in interp_args.keys():
        interp_days = interp_args['interp_days']
    else:
        interp_days = 32
        logging.debug('interp_days was not set, default to 32')

    # Check that the input parameters are valid
    if t_interval.lower() not in ['daily', 'monthly', 'annual', 'custom']:
        raise ValueError(f'unsupported t_interval: {t_interval}')
    elif interp_method.lower() not in ['linear']:
        raise ValueError(f'unsupported interp_method: {interp_method}')

    if type(interp_days) is str and utils.is_number(interp_days):
        interp_days = int(interp_days)
    elif not type(interp_days) is int:
        raise TypeError('interp_days must be an integer')
    elif interp_days <= 0:
        raise ValueError('interp_days must be a positive integer')

    if not variables:
        raise ValueError('variables parameter must be set')

    # Adjust start/end dates based on t_interval
    # Increase the date range to fully include the time interval
    start_dt = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_dt = datetime.datetime.strptime(end_date, '%Y-%m-%d')
    if t_interval.lower() == 'annual':
        start_dt = datetime.datetime(start_dt.year, 1, 1)
        # Covert end date to inclusive, flatten to beginning of year,
        # then add a year which will make it exclusive
        end_dt -= relativedelta(days=+1)
        end_dt = datetime.datetime(end_dt.year, 1, 1)
        end_dt += relativedelta(years=+1)
    elif t_interval.lower() == 'monthly':
        start_dt = datetime.datetime(start_dt.year, start_dt.month, 1)
        end_dt -= relativedelta(days=+1)
        end_dt = datetime.datetime(end_dt.year, end_dt.month, 1)
        end_dt += relativedelta(months=+1)
    start_date = start_dt.strftime('%Y-%m-%d')
    end_date = end_dt.strftime('%Y-%m-%d')

    # The start/end date for the interpolation include more days
    # (+/- interp_days) than are included in the ETr collection
    interp_start_dt = start_dt - datetime.timedelta(days=interp_days)
    interp_end_dt = end_dt + datetime.timedelta(days=interp_days)
    interp_start_date = interp_start_dt.date().isoformat()
    interp_end_date = interp_end_dt.date().isoformat()

    # Get reference ET source
    if 'et_reference_source' in model_args.keys():
        et_reference_source = model_args['et_reference_source']
    else:
        raise ValueError('et_reference_source was not set')

    # Get reference ET band name
    if 'et_reference_band' in model_args.keys():
        et_reference_band = model_args['et_reference_band']
    else:
        raise ValueError('et_reference_band was not set')

    # Get reference ET factor
    if 'et_reference_factor' in model_args.keys():
        et_reference_factor = model_args['et_reference_factor']
    else:
        et_reference_factor = 1.0
        logging.debug('et_reference_factor was not set, default to 1.0')
        # raise ValueError('et_reference_factor was not set')

    # CGM - Resampling is not working correctly so commenting out for now
    # # Get reference ET resample
    # if 'et_reference_resample' in model_args.keys():
    #     et_reference_resample = model_args['et_reference_resample']
    # else:
    #     et_reference_resample = 'nearest'
    #     logging.debug('et_reference_resample was not set, default to nearest')
    #     # raise ValueError('et_reference_resample was not set')

    if 'et_reference_date_type' in model_args.keys():
        et_reference_date_type = model_args['et_reference_date_type']
    else:
        et_reference_date_type = None
        # logging.debug('et_reference_date_type was not set, default to "daily"')
        # et_reference_date_type = 'daily'

    if type(et_reference_source) is str:
        # Assume a string source is an single image collection ID
        #   not an list of collection IDs or ee.ImageCollection
        if (et_reference_date_type is None or
                et_reference_date_type.lower() == 'daily'):
            daily_et_ref_coll = ee.ImageCollection(et_reference_source) \
                .filterDate(start_date, end_date) \
                .select([et_reference_band], ['et_reference'])
        elif et_reference_date_type.lower() == 'doy':
            # Assume the image collection is a climatology with a "DOY" property
            def doy_image(input_img):
                """Return the doy-based reference et with daily time properties from GRIDMET"""
                image_date = ee.Algorithms.Date(input_img.get('system:time_start'))
                image_doy = ee.Number(image_date.getRelative('day', 'year')).add(1).int()
                doy_coll = ee.ImageCollection(et_reference_source)\
                    .filterMetadata('DOY', 'equals', image_doy)\
                    .select([et_reference_band], ['et_reference'])
                # CGM - Was there a reason to use rangeContains if limiting to one DOY?
                #     .filter(ee.Filter.rangeContains('DOY', doy, doy))\
                return ee.Image(doy_coll.first())\
                    .set({'system:index': input_img.get('system:index'),
                          'system:time_start': input_img.get('system:time_start')})
            # Note, the collection and band that are used are important as
            #   long as they are daily and available for the time period
            daily_et_ref_coll = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET')\
                .filterDate(start_date, end_date).select(['eto'])\
                .map(doy_image)
    # elif isinstance(et_reference_source, computedobject.ComputedObject):
    #     # Interpret computed objects as image collections
    #     daily_et_reference_coll = ee.ImageCollection(et_reference_source)\
    #         .filterDate(start_date, end_date) \
    #         .select([et_reference_band])
    else:
        raise ValueError(f'unsupported et_reference_source: {et_reference_source}')

    # Scale reference ET images (if necessary)
    # CGM - Resampling is not working correctly so not including for now
    if et_reference_factor and et_reference_factor != 1:
        def et_reference_adjust(input_img):
            return input_img.multiply(et_reference_factor) \
                .copyProperties(input_img) \
                .set({'system:time_start': input_img.get('system:time_start')})
        daily_et_ref_coll = daily_et_ref_coll.map(et_reference_adjust)

    # Initialize variable list to only variables that can be interpolated
    interp_vars = list(set(_interp_vars) & set(variables))

    # To return ET, the ETf must be interpolated
    if 'et' in variables and 'et_fraction' not in interp_vars:
        interp_vars.append('et_fraction')

    # With the current interpolate.daily() function,
    #   something has to be interpolated in order to return et_reference
    if 'et_reference' in variables and 'et_fraction' not in interp_vars:
        interp_vars.append('et_fraction')

    # The time band is always needed for interpolation
    interp_vars.append('time')

    # Filter scene collection to the interpolation range
    # This probably isn't needed since scene_coll was built to this range
    scene_coll = scene_coll.filterDate(interp_start_date, interp_end_date)

    # For count, compute the composite/mosaic image for the mask band only
    if 'count' in variables:
        aggregate_coll = openet.core.interpolate.aggregate_to_daily(
            image_coll = scene_coll.select(['mask']),
            start_date=start_date, end_date=end_date)

        # The following is needed because the aggregate collection can be
        #   empty if there are no scenes in the target date range but there
        #   are scenes in the interpolation date range.
        # Without this the count image will not be built but the other
        #   bands will be which causes a non-homogeneous image collection.
        aggregate_coll = aggregate_coll.merge(
            ee.Image.constant(0).rename(['mask'])
                .set({'system:time_start': ee.Date(start_date).millis()}))

    # Interpolate to a daily time step
    # NOTE: the daily function is not computing ET (ETf x ETr)
    #   but is returning the target (ETr) band
    daily_coll = openet.core.interpolate.daily(
        target_coll=daily_et_ref_coll,
        source_coll=scene_coll.select(interp_vars),
        interp_method=interp_method, interp_days=interp_days,
        use_joins=use_joins,
    )

    # Compute ET from ETf and ETr (if necessary)
    # The check for et_fraction is needed since it is back computed from ET and ETr
    # if 'et' in variables or 'et_fraction' in variables:
    def compute_et(img):
        """This function assumes ETr and ETf are present"""
        et_img = img.select(['et_fraction']) \
            .multiply(img.select(['et_reference']))
        return img.addBands(et_img.double().rename('et'))

    daily_coll = daily_coll.map(compute_et)

    def aggregate_image(agg_start_date, agg_end_date, date_format):
        """Aggregate the daily images within the target date range

        Parameters
        ----------
        agg_start_date: str
            Start date (inclusive).
        agg_end_date : str
            End date (exclusive).
        date_format : str
            Date format for system:index (uses EE JODA format).

        Returns
        -------
        ee.Image

        Notes
        -----
        Since this function takes multiple inputs it is being called
        for each time interval by separate mappable functions

        """
        if 'et' in variables or 'et_fraction' in variables:
            et_img = daily_coll.filterDate(agg_start_date, agg_end_date) \
                .select(['et']).sum()
        if 'et_reference' in variables or 'et_fraction' in variables:
            # et_reference_img = daily_coll \
            et_reference_img = daily_et_ref_coll \
                .filterDate(agg_start_date, agg_end_date) \
                .select(['et_reference']).sum()

        image_list = []
        if 'et' in variables:
            image_list.append(et_img.float())
        if 'et_reference' in variables:
            image_list.append(et_reference_img.float())
        if 'et_fraction' in variables:
            # Compute average et fraction over the aggregation period
            image_list.append(
                et_img.divide(et_reference_img).rename(
                    ['et_fraction']).float())
        if 'ndvi' in variables:
            # Compute average ndvi over the aggregation period
            ndvi_img = daily_coll \
                .filterDate(agg_start_date, agg_end_date) \
                .mean().select(['ndvi']).float()
            image_list.append(ndvi_img)
        if 'count' in variables:
            count_img = aggregate_coll \
                .filterDate(agg_start_date, agg_end_date) \
                .select(['mask']).sum().rename('count').uint8()
            image_list.append(count_img)

        return ee.Image(image_list) \
            .set({
                'system:index': ee.Date(agg_start_date).format(date_format),
                'system:time_start': ee.Date(agg_start_date).millis()})
        #     .set(interp_properties) \

    # Combine input, interpolated, and derived values
    if t_interval.lower() == 'daily':
        def agg_daily(daily_img):
            # CGM - Double check that this time_start is a 0 UTC time.
            # It should be since it is coming from the interpolate source
            #   collection, but what if source is GRIDMET (+6 UTC)?
            agg_start_date = ee.Date(daily_img.get('system:time_start'))
            # CGM - This calls .sum() on collections with only one image
            return aggregate_image(
                agg_start_date=agg_start_date,
                agg_end_date=ee.Date(agg_start_date).advance(1, 'day'),
                date_format='YYYYMMdd')

        return ee.ImageCollection(daily_coll.map(agg_daily))

    elif t_interval.lower() == 'monthly':
        def month_gen(iter_start_dt, iter_end_dt):
            iter_dt = iter_start_dt
            # Conditional is "less than" because end date is exclusive
            while iter_dt < iter_end_dt:
                yield iter_dt.strftime('%Y-%m-%d')
                iter_dt += relativedelta(months=+1)

        month_list = ee.List(list(month_gen(start_dt, end_dt)))

        def agg_monthly(agg_start_date):
            return aggregate_image(
                agg_start_date=agg_start_date,
                agg_end_date=ee.Date(agg_start_date).advance(1, 'month'),
                date_format='YYYYMM')

        return ee.ImageCollection(month_list.map(agg_monthly))

    elif t_interval.lower() == 'annual':
        def year_gen(iter_start_dt, iter_end_dt):
            iter_dt = iter_start_dt
            while iter_dt < iter_end_dt:
                yield iter_dt.strftime('%Y-%m-%d')
                iter_dt += relativedelta(years=+1)

        year_list = ee.List(list(year_gen(start_dt, end_dt)))

        def agg_annual(agg_start_date):
            return aggregate_image(
                agg_start_date=agg_start_date,
                agg_end_date=ee.Date(agg_start_date).advance(1, 'year'),
                date_format='YYYY')

        return ee.ImageCollection(year_list.map(agg_annual))

    elif t_interval.lower() == 'custom':
        # Returning an ImageCollection to be consistent
        return ee.ImageCollection(aggregate_image(
            agg_start_date=start_date, agg_end_date=end_date,
            date_format='YYYYMMdd'))
예제 #36
0
sliding_windows = [
                    ("2020-08-01", "2020-10-31"),
                    ("2021-02-01", "2021-03-31"),
                    ("2019-12-01", "2020-02-29"),
                    ("2020-01-01", "2020-03-31"),
                    ("2020-02-01", "2020-04-30"),
                    ("2020-03-01", "2020-05-31"),
                    ("2020-04-01", "2020-06-30"),
                    ("2020-05-01", "2020-07-31"),
                    ("2020-06-01", "2020-08-31"),
                    ("2020-07-01", "2020-09-30")
                  ]

# Supplement with 3 month sliding windows since beginning of pandemic
TODAY = datetime.date.today()
LASTMONTH = TODAY-relativedelta(months=+1, day=31)
THREEMONTHSAGO = TODAY-relativedelta(months=+3, day=1)

starts = [dt.strftime('%Y-%m-%d') for dt in rrule(MONTHLY, interval=1,bymonthday=(1),dtstart=parse("20191201T000000"), until=THREEMONTHSAGO)]
ends = [dt.strftime('%Y-%m-%d') for dt in rrule(MONTHLY, interval=1,bymonthday=(-1),dtstart=parse("20200228T000000"), until=LASTMONTH)]
sliding_windows = set(list(zip(starts,ends)) + sliding_windows)


get_sliding_window_counts_task = PythonOperator(
        task_id=f'get_sliding_window_counts',
        python_callable=write_sliding_window_counts,
        op_kwargs={ "output_fn" : sliding_window_count_output_fn, "sliding_windows": sliding_windows},
        dag=dag,
    )

dag.doc_md = __doc__
예제 #37
0
    def _build_date_sources(self, identifier,
                            shown_event_count) -> BrowseMediaSource:
        sources = []

        now = dt.datetime.now(DEFAULT_TIME_ZONE)
        today = now.replace(hour=0, minute=0, second=0, microsecond=0)

        start_of_today = int(today.timestamp())
        start_of_yesterday = start_of_today - SECONDS_IN_DAY
        start_of_month = int(today.replace(day=1).timestamp())
        start_of_last_month = int(
            (today.replace(day=1) + relativedelta(months=-1)).timestamp())
        start_of_year = int(today.replace(month=1, day=1).timestamp())

        count_today = self._count_by(after=start_of_today,
                                     camera=identifier['camera'],
                                     label=identifier['label'],
                                     zone=identifier['zone'])
        count_yesterday = self._count_by(after=start_of_yesterday,
                                         before=start_of_today,
                                         camera=identifier['camera'],
                                         label=identifier['label'],
                                         zone=identifier['zone'])
        count_this_month = self._count_by(after=start_of_month,
                                          camera=identifier['camera'],
                                          label=identifier['label'],
                                          zone=identifier['zone'])
        count_last_month = self._count_by(after=start_of_last_month,
                                          before=start_of_month,
                                          camera=identifier['camera'],
                                          label=identifier['label'],
                                          zone=identifier['zone'])
        count_this_year = self._count_by(after=start_of_year,
                                         camera=identifier['camera'],
                                         label=identifier['label'],
                                         zone=identifier['zone'])

        # if a date range has already been selected
        if identifier['before'] != '' or identifier['after'] != '':
            before = int(
                now.timestamp()) if identifier['before'] == '' else int(
                    identifier['before'])
            after = int(now.timestamp()) if identifier['after'] == '' else int(
                identifier['after'])

            # if we are looking at years, split into months
            if before - after > SECONDS_IN_MONTH:
                current = after
                while (current < before):
                    current_date = DEFAULT_TIME_ZONE.localize(
                        dt.datetime.fromtimestamp(current)).replace(
                            hour=0, minute=0, second=0, microsecond=0)
                    start_of_current_month = int(current_date.timestamp())
                    start_of_next_month = int(
                        (current_date + relativedelta(months=+1)).timestamp())
                    count_current = self._count_by(
                        after=start_of_current_month,
                        before=start_of_next_month,
                        camera=identifier['camera'],
                        label=identifier['label'],
                        zone=identifier['zone'])
                    sources.append(
                        BrowseMediaSource(
                            domain=DOMAIN,
                            identifier=
                            f"clips/{identifier['name']}.{current_date.strftime('%Y-%m')}/{start_of_current_month}/{start_of_next_month}/{identifier['camera']}/{identifier['label']}/{identifier['zone']}",
                            media_class=MEDIA_CLASS_DIRECTORY,
                            children_media_class=MEDIA_CLASS_VIDEO,
                            media_content_type=MEDIA_CLASS_VIDEO,
                            title=
                            f"{current_date.strftime('%B')} ({count_current})",
                            can_play=False,
                            can_expand=True,
                            thumbnail=None))
                    current = current + SECONDS_IN_MONTH
                return sources

            # if we are looking at a month, split into days
            if before - after > SECONDS_IN_DAY:
                current = after
                while (current < before):
                    current_date = DEFAULT_TIME_ZONE.localize(
                        dt.datetime.fromtimestamp(current)).replace(
                            hour=0, minute=0, second=0, microsecond=0)
                    start_of_current_day = int(current_date.timestamp())
                    start_of_next_day = start_of_current_day + SECONDS_IN_DAY
                    count_current = self._count_by(after=start_of_current_day,
                                                   before=start_of_next_day,
                                                   camera=identifier['camera'],
                                                   label=identifier['label'],
                                                   zone=identifier['zone'])
                    if count_current > 0:
                        sources.append(
                            BrowseMediaSource(
                                domain=DOMAIN,
                                identifier=
                                f"clips/{identifier['name']}.{current_date.strftime('%Y-%m-%d')}/{start_of_current_day}/{start_of_next_day}/{identifier['camera']}/{identifier['label']}/{identifier['zone']}",
                                media_class=MEDIA_CLASS_DIRECTORY,
                                children_media_class=MEDIA_CLASS_VIDEO,
                                media_content_type=MEDIA_CLASS_VIDEO,
                                title=
                                f"{current_date.strftime('%B %d')} ({count_current})",
                                can_play=False,
                                can_expand=True,
                                thumbnail=None))
                    current = current + SECONDS_IN_DAY
                return sources

            return sources

        if count_today > shown_event_count:
            sources.append(
                BrowseMediaSource(
                    domain=DOMAIN,
                    identifier=
                    f"clips/{identifier['name']}.today/{start_of_today}//{identifier['camera']}/{identifier['label']}/{identifier['zone']}",
                    media_class=MEDIA_CLASS_DIRECTORY,
                    children_media_class=MEDIA_CLASS_VIDEO,
                    media_content_type=MEDIA_CLASS_VIDEO,
                    title=f"Today ({count_today})",
                    can_play=False,
                    can_expand=True,
                    thumbnail=None))

        if count_yesterday > shown_event_count:
            sources.append(
                BrowseMediaSource(
                    domain=DOMAIN,
                    identifier=
                    f"clips/{identifier['name']}.yesterday/{start_of_yesterday}/{start_of_today}/{identifier['camera']}/{identifier['label']}/{identifier['zone']}",
                    media_class=MEDIA_CLASS_DIRECTORY,
                    children_media_class=MEDIA_CLASS_VIDEO,
                    media_content_type=MEDIA_CLASS_VIDEO,
                    title=f"Yesterday ({count_yesterday})",
                    can_play=False,
                    can_expand=True,
                    thumbnail=None))

        if count_this_month > count_today + count_yesterday and count_this_month > shown_event_count:
            sources.append(
                BrowseMediaSource(
                    domain=DOMAIN,
                    identifier=
                    f"clips/{identifier['name']}.this_month/{start_of_month}//{identifier['camera']}/{identifier['label']}/{identifier['zone']}",
                    media_class=MEDIA_CLASS_DIRECTORY,
                    children_media_class=MEDIA_CLASS_VIDEO,
                    media_content_type=MEDIA_CLASS_VIDEO,
                    title=f"This Month ({count_this_month})",
                    can_play=False,
                    can_expand=True,
                    thumbnail=None))

        if count_last_month > shown_event_count:
            sources.append(
                BrowseMediaSource(
                    domain=DOMAIN,
                    identifier=
                    f"clips/{identifier['name']}.last_month/{start_of_last_month}/{start_of_month}/{identifier['camera']}/{identifier['label']}/{identifier['zone']}",
                    media_class=MEDIA_CLASS_DIRECTORY,
                    children_media_class=MEDIA_CLASS_VIDEO,
                    media_content_type=MEDIA_CLASS_VIDEO,
                    title=f"Last Month ({count_last_month})",
                    can_play=False,
                    can_expand=True,
                    thumbnail=None))

        if count_this_year > count_this_month + count_last_month and count_this_year > shown_event_count:
            sources.append(
                BrowseMediaSource(
                    domain=DOMAIN,
                    identifier=
                    f"clips/{identifier['name']}.this_year/{start_of_year}//{identifier['camera']}/{identifier['label']}/{identifier['zone']}",
                    media_class=MEDIA_CLASS_DIRECTORY,
                    children_media_class=MEDIA_CLASS_VIDEO,
                    media_content_type=MEDIA_CLASS_VIDEO,
                    title="This Year",
                    can_play=False,
                    can_expand=True,
                    thumbnail=None))

        return sources
예제 #38
0
 def calcAge(self):
     age = relativedelta(date.today(), self.birthdate)
     return age
예제 #39
0
 def calcAge(self):
     age = relativedelta(date.today(), self.firstRegistration)
     return age
예제 #40
0
 def timeUntilWeeknight(self, t):
     weeknightStart = t + relativedelta(
         hour=18, minute=0, second=0, microseconds=0)
     return weeknightStart - t
예제 #41
0
# imports
from time import time
from datetime import datetime
from dateutil.relativedelta import *

# Measure runtime
start = time()

# Number of Sundays
total = 0

# Starting date
dt = datetime.strptime("01/01/1901", "%m/%d/%Y")

# Check only the first of every month 01/01/1901 - 12/31/2000
for month in range(12 * (2001 - 1901)):

    # Check if Sunday
    if 6 == dt.weekday():
        total += 1

    # Iterate to next month
    dt += relativedelta(months=+1)

# Answer
print(total)

# Print runtime
print("--- %s seconds ---" % (round(time() - start, 2)))
예제 #42
0
def get_birth_year_from_age(age):
    today = date.today()
    year_of_birth = datetime.now() - relativedelta(years=int(age))

    return year_of_birth.strftime("%Y")
예제 #43
0
def lambda_handler(context, event):
    ce = boto3.client('ce')
    budgetClient = boto3.client('budgets')

    responseStatus = 'SUCCESS'
    responseData = {}

    # Here we are listing existing budgets.
    print "first list the existing budgets"
    response = budgetClient.describe_budgets(AccountId=AccountId,
                                             MaxResults=99)
    print response

    # Here we will delete previous budget with same name.
    print "deleting the budget"
    try:
        response = budgetClient.delete_budget(AccountId=AccountId,
                                              BudgetName='AWS_Budget_DEV')
        print response
    except:
        pass

    # To get average cost of last 3 months, we are getting first date of last third month and
    # we are taking end date as todays date.
    Start_Date = str(datetime.date.today() - relativedelta(months=3))
    End_Date = str(datetime.date.today())

    startdt = get_first_day(datetime.date.today())
    enddt = get_last_day(datetime.date.today() + relativedelta(days=1))
    start = int(time.mktime(startdt.timetuple()))
    end = int(time.mktime(enddt.timetuple()))

    #using get_cost_and_usage api to calculate AWS resource costs
    response = ce.get_cost_and_usage(TimePeriod={
        'Start': Start_Date,
        'End': End_Date
    },
                                     Granularity='MONTHLY',
                                     Metrics=['BlendedCost'])

    #Here we are collecting last 3 months cost data for API response came from previous API call.
    amt1 = (response["ResultsByTime"][0]["Total"]["BlendedCost"]["Amount"])
    amt2 = (response["ResultsByTime"][1]["Total"]["BlendedCost"]["Amount"])
    amt3 = (response["ResultsByTime"][2]["Total"]["BlendedCost"]["Amount"])

    # Calculate average costs of last 3 moths, that average amount will go as next budget amount.
    avg = (int(locale.atof(amt1)) + int(locale.atof(amt2)) +
           int(locale.atof(amt3))) / 3
    """
        In this call we are going to create new budget with average of last 3 months
        and we are going to set notification if budget reaches 110% of actual budget.
        For notification we are using SNS topic, which is already created.
    """
    response = budgetClient.create_budget(
        AccountId=AccountId,
        Budget={
            'BudgetName': 'AWS_Budget_DEV',
            'BudgetLimit': {
                'Amount': str(avg),
                'Unit': 'USD'
            },
            'CostFilters': {
                "AZ": ["us-west-2"]
            },
            'CostTypes': {
                'IncludeTax': True,
                'IncludeSubscription': True,
                'UseBlended': False,
                'IncludeRefund': True,
                'IncludeCredit': True,
                'IncludeUpfront': True,
                'IncludeRecurring': True,
                'IncludeOtherSubscription': True,
                'IncludeSupport': True,
                'IncludeDiscount': True,
                'UseAmortized': True
            },
            'TimeUnit': 'MONTHLY',
            'TimePeriod': {
                'Start': start,
                'End': end
            },
            'BudgetType': 'COST'
        },
        NotificationsWithSubscribers=[{
            'Notification': {
                'NotificationType': 'ACTUAL',
                'ComparisonOperator': 'GREATER_THAN',
                'Threshold': 110,
                'ThresholdType': 'PERCENTAGE'
            },
            'Subscribers': [
                {
                    'SubscriptionType': 'SNS',
                    'Address': 'arn:aws:sns:us-west-2:xxxxxxx:AWS_Budget_DEV'
                },
            ]
        }])
    return 'BudgetLambda Executed'
예제 #44
0
def check_if_within_time_interval(updated_at_date):
    lower_bound = datetime.now(timezone.utc) + relativedelta(seconds=-7)
    if updated_at_date > lower_bound:
        return True
    return False
예제 #45
0
def next_mont_2_first_date():
    today = date.today()
    d = today + relativedelta(months=2)
    firstDate = date(d.year, d.month, 1)
    return firstDate
예제 #46
0
파일: utils.py 프로젝트: leoslf/206CDE
def datemath(value, format="%d-%b-%y", **kwargs):
    value = strptime(value, format)
    if len(kwargs) > 0:
        value += relativedelta(**kwargs)
    return dateformat(value, format)
        if (value[i][0] == 'DATE'):
            birt = value[i][1]
            birt = datetime.datetime.strptime(birt, '%d %b %Y').date()
        if (value[i][0] == 'DEAT'):
            deat = value[i][1]
            deat = datetime.datetime.strptime(deat, '%d %b %Y').date()
            deat_count = deat_count + 1
        if (value[i][0] == 'FAMC'):
            famc = value[i][1]
        if (value[i][0] == 'FAMS'):
            fams = value[i][1]
        if (deat_count < 1):
            deat = 'NA'
    if (any('DEAT' in i for i in value)):
        alive = False
        age = relativedelta(deat, birt).years
    else:
        age = relativedelta(datetime.datetime.now(), birt).years
        alive = True

    df_indi = df_indi.append(
        {
            'ID': key,
            'Name': name,
            'Gender': gender,
            'Birthday': birt,
            'Alive': alive,
            'Death': deat,
            'Child': famc,
            'Spouce': fams,
            'Age': age
예제 #48
0
 def get_report_data(self, ids, context={}):
     company_id = get_active_company()
     comp = get_model("company").browse(company_id)
     if ids:
         params = self.read(ids, load_m2o=False)[0]
     else:
         params = self.default_get(load_m2o=False, context=context)
     settings = get_model("settings").browse(1)
     if not params.get("account_id"):
         return
     account_id = int(params.get("account_id"))
     account = get_model("account.account").browse(account_id)
     date_from = params.get("date_from")
     if not date_from:
         db = database.get_connection()
         res = db.get(
             "SELECT min(m.date) AS min_date FROM account_move_line l,account_move m WHERE m.id=l.move_id AND l.account_id=%s AND m.state='posted'",
             account_id)
         if not res:
             return
         date_from = res["min_date"][:7] + "-01"
     date_to = params.get("date_to")
     if not date_to:
         date_to = (date.today() +
                    relativedelta(day=31)).strftime("%Y-%m-%d")
     contact_id = params.get("contact_id")
     if contact_id:
         contact_id = int(contact_id)
     track_id = params.get("track_id")
     if track_id:
         track_id = int(track_id)
     track2_id = params.get("track_id")
     if track2_id:
         track2_id = int(track2_id)
     data = {
         "company_name": comp.name,
         "date_from": date_from,
         "date_to": date_to,
         "contact_id": contact_id,
         "track_id": track_id,
         "track2_id": track2_id,
         "account_name": account.name,
         "company_currency_code": settings.currency_id.code,
         "account_currency_code": account.currency_id.code,
         "lines": [],
         "total": 0,
         "total_cur": 0,
     }
     d0 = datetime.strptime(date_from, "%Y-%m-%d")
     d2 = datetime.strptime(date_to, "%Y-%m-%d")
     while d0 <= d2:
         d1 = d0 + relativedelta(day=31)
         if d1 > d2:
             d1 = d2
         ctx = {
             "date_from": d0.strftime("%Y-%m-%d"),
             "date_to": d1.strftime("%Y-%m-%d"),
             "contact_id": contact_id,
             "track_id": track_id,
             "track2_id": track2_id,
         }
         acc = get_model("account.account").read([account_id],
                                                 ["balance", "balance_cur"],
                                                 context=ctx)[0]
         balance = acc["balance"]
         balance_cur = acc["balance_cur"]
         line = {
             "account_id": account_id,
             "month": d0.strftime("%B %Y"),
             "balance": balance,
             "balance_cur": balance_cur,
             "date_from": d0.strftime("%Y-%m-%d"),
             "date_to": d1.strftime("%Y-%m-%d"),
         }
         data["lines"].append(line)
         data["total"] += line["balance"]
         data["total_cur"] += line["balance_cur"]
         d0 = d1 + timedelta(days=1)
     return data
예제 #49
0
 def month_gen(iter_start_dt, iter_end_dt):
     iter_dt = iter_start_dt
     # Conditional is "less than" because end date is exclusive
     while iter_dt < iter_end_dt:
         yield iter_dt.strftime('%Y-%m-%d')
         iter_dt += relativedelta(months=+1)
예제 #50
0
 def testNextWednesdayIsToday(self):
     self.assertEqual(self.today+relativedelta(weekday=WE),
                      date(2003, 9, 17))
예제 #51
0
import poplib, sys, datetime, email, os
from pymongo import Connection
from pymongo.objectid import ObjectId
from datetime import date, datetime, time
from dateutil.relativedelta import *
import sendMailUtil
import csv

todayDate = date.today()
file_date = todayDate + relativedelta(days=-1)
file_date = file_date.strftime("%d-%b-%Y")
todayDate = todayDate.strftime("%d-%b-%Y")
todayDate = datetime.strptime(todayDate, "%d-%b-%Y")

previousDate = todayDate + relativedelta(days=-1)
previousDate = previousDate.strftime("%d-%b-%Y")
previousDate = datetime.strptime(previousDate, "%d-%b-%Y")

Month = previousDate.strftime("%b-%Y")
Year = previousDate.strftime("%Y")
day1 = datetime.combine(previousDate, time(0, 0))
day2 = datetime.combine(todayDate, time(0, 0))


def getMongoConnection(MONGO_HOST,
                       MONGO_PORT,
                       DB,
                       isSlave=False,
                       isAuth=False,
                       username='******',
                       password='******'):
예제 #52
0
 def testNextWenesdayNotToday(self):
     self.assertEqual(self.today+relativedelta(days=+1, weekday=WE),
                      date(2003, 9, 24))
예제 #53
0
파일: aris_web_api.py 프로젝트: mkbrk/bmon
def get_energy_use(building_id,
                   energy_type_id,
                   last_update_ts=0,
                   energy_parameter='EnergyQuantity',
                   energy_multiplier=1,
                   expected_period_months=1):
    """
    Returns building energy usage information via the ARIS Web API

    """

    aris_url = getattr(settings, 'BMSAPP_ARIS_URL')
    aris_username = getattr(settings, 'BMSAPP_ARIS_USERNAME')
    aris_password = getattr(settings, 'BMSAPP_ARIS_PASSWORD')

    last_update_dt = datetime.utcfromtimestamp(last_update_ts + 1)

    timestamp_list = []
    values_list = []

    url = aris_url + '/api/buildingmonitoring/buildingenergyusage'
    values = {
        'userName': aris_username,
        'password': aris_password,
        'buildingId': building_id,
        'energyTypeId': energy_type_id,
        'LatestUpdateDate': last_update_dt
    }

    try:
        r = requests.post(url,
                          data=values,
                          headers={'Accept': 'application/json'})
        r.raise_for_status()
        response_data = r.json()
    except requests.exceptions.RequestException as e:
        print e
        raise

    if len(response_data) > 0:
        for response_row in response_data:
            # Assign the sensor date/time
            if response_row['MeterReadDate']:
                read_dt = parser.parse(response_row['MeterReadDate'])
            else:
                read_dt = datetime.strptime(response_row['UsageMonthYear'],
                                            '%m-%Y') + relativedelta(months=+1,
                                                                     days=-1)

            if response_row['PreviousReadDate']:
                last_dt = parser.parse(response_row['PreviousReadDate'])
                if abs(
                    (read_dt - last_dt).days) > (expected_period_months * 52):
                    last_dt = read_dt + relativedelta(
                        months=(-1 * expected_period_months))
            else:
                last_dt = read_dt + relativedelta(
                    months=(-1 * expected_period_months))
                # a better algorithm to deal with the last day of the month would be:
                # last_dt = read_dt + relativedelta(days=+1)
                #                   + relativedelta(months=(-1 * expected_period_months))
                #                   + relativedelta(days=-1)
                # but, changing this might mess up data from sensors that have already been processed

            sensor_dt = last_dt + (read_dt - last_dt) / 2
            read_period_hours = (read_dt - last_dt).total_seconds() / 60 / 60

            # Get the value for the requested energy parameter
            if energy_parameter in response_row:
                if not response_row[energy_parameter]:
                    continue
                try:
                    energy_parameter_value = float(
                        response_row[energy_parameter])
                except:
                    print "ARIS Value Conversion Error: ", response_row[
                        energy_parameter]
                    continue
            else:
                print "Parameter Name Not Present: ", energy_parameter
                continue

            # Convert the energy parameter value into appropriate units for the sensor
            if energy_parameter == 'EnergyQuantity':
                # return hourly energy use
                sensor_value = energy_parameter_value * energy_multiplier / read_period_hours
            elif energy_parameter in ['DollarCost', 'DemandCost']:
                # return monthly cost. There are 8766 hours in a year, so 730.5 hours on average per month
                sensor_value = energy_parameter_value * energy_multiplier / (
                    read_period_hours / 730.5)
            else:
                # return values for demand or anything else
                sensor_value = energy_parameter_value * energy_multiplier

            # Update the last update date
            update_dt = parser.parse(response_row['UpdateDate'])
            if update_dt > last_update_dt:
                last_update_dt = update_dt
            last_update_ts = (last_update_dt -
                              datetime.utcfromtimestamp(0)).total_seconds()

            # Add the results to the output lists
            timestamp_list.append(
                (sensor_dt - datetime.fromtimestamp(0)).total_seconds())
            values_list.append(sensor_value)

    return last_update_ts, timestamp_list, values_list
예제 #54
0
 def test15thISOYearWeek(self):
     self.assertEqual(date(2003, 1, 1) +
                      relativedelta(day=4, weeks=+14, weekday=MO(-1)),
                      date(2003, 4, 7))
def increment_date(date, day=False):
    date = dt.datetime.strptime(date, '%Y%m%d') if day else dt.datetime.strptime(date, '%Y%m')
    delta = dt.timedelta(days=1) if day else relativedelta(months=1)
    return (date + delta).strftime('%Y%m%d') if day else (date + delta).strftime('%Y%m')
예제 #56
0
    def date_compatibility_fucn(exact_date_and_month=None,
                                before_month_count=None,
                                before_days_count=None,
                                increment_type=None):
        '''
        ..codeauthor:: Muthukumar Subramanian
        :param exact_date_and_month: pass YES|Yes|yes or NO|No|no if you need exact date and month difference
        :param before_month_count: default month count is <type int> '1'
        :param before_days_count: default day count is <type int> '30'
        :param increment_type: pre increment for previous data or post increment for future data
        :return: return_dict
                Example:  {'current_date': '2019-11-01', 'datetime_format_current':
                datetime.datetime(2019, 11, 1, 0, 0), 'string_of_months': '2019-10-01', 'string_of_days': None,
                'datetime_format_before': datetime.datetime(2019, 10, 1, 0, 0)}
        '''

        return_dict = {}
        final_before_days = None
        final_before_months = None
        string_of_months = None
        string_of_days = None

        # condition check for increment_type
        if increment_type is None:
            inc_type = 'pre'
        else:
            if increment_type == 'pre':
                inc_type = 'pre'
                print("Date difference for 'pre' increment for previous data")
            elif increment_type == 'post':
                inc_type = 'post'
                print("Date difference for 'post' increment for future data")
            else:
                raise Exception(
                    "Only the pre or post value is supported. Please provide the valid data on "
                    "'increment_type' variable")

        # condition check for exact_date_and_month
        if exact_date_and_month is None:
            exact_date = True
        else:
            regx = re.match(r'([Yy](?:ES|es))|([Nn](?:O|o))',
                            exact_date_and_month)
            if regx is not None:
                g1 = regx.group(1)
                g2 = regx.group(2)
                if g1 is not None:
                    exact_date = True
                elif g2 is not None:
                    exact_date = False
            else:
                # regex will not break here
                regx = re.match('([YyEeSs]+)|([NnOo]+)', exact_date_and_month)
                if regx is not None:
                    g1 = regx.group(1)
                    g2 = regx.group(2)
                    if g1 is not None:
                        exact_date = True
                    elif g2 is not None:
                        exact_date = False
        # condition check for before_month_count and before_days_count
        month_count = None
        days_count = None
        if before_month_count is not None and before_days_count is not None:
            raise Exception(
                "User can give either before_month_count or before_days_count")
        elif before_month_count is None and before_days_count is None:
            print(
                "Both Days and Months is None, so default 'months count' as '1'"
            )
            month_count = 1
        elif before_month_count is not None:
            if before_month_count is None:
                month_count = 1
            else:
                month_count = int(before_month_count)
        elif before_days_count is not None:
            if before_days_count is None:
                print(
                    "Both Days and Months is None, so default 'days count' as '30'"
                )
                days_count = 30
            else:
                days_count = before_days_count

        # Today date and time
        current_date = datetime.today()
        # current_date = '2019-11-01 12:49:00.710802'  # hard code
        print("Initial Date and Time: {}".format(current_date))
        regex_date = re.match(r'(\d{4}-\d{2}-\d{2})\s+(\d{2}:\d{2}:\d{2}).*',
                              str(current_date))
        if regex_date is not None:
            date_val = regex_date.group(1)
            time_val = regex_date.group(2)  # As of now unused
            date_val_r = regex_date.group(1).split('-')
            time_val_r = regex_date.group(2).split(':')
        current_date_and_time = datetime(
            int(date_val_r[0]), int(date_val_r[1]), int(date_val_r[2]),
            int(time_val_r[0]), int(time_val_r[1]), int(time_val_r[2]))

        print("current_date is: {}".format(date_val))
        current_date_and_time_without_msec = datetime.strptime(
            str(current_date_and_time), '%Y-%m-%d %H:%M:%S')
        current_date_and_time_without_msec = str(
            current_date_and_time_without_msec)
        current_date_list = current_date_and_time_without_msec.split(' ')
        date_str_format = current_date_list[0]
        time_str_format = current_date_list[1]  # As of now unused

        # Getting the current date here
        current_date_now = None
        regex_date_c = re.match(r'(\d{4}-\d{2}-\d{2})', date_str_format)
        if regex_date_c is not None:
            current_date_conv = regex_date_c.group().split('-')
            if exact_date is False and before_days_count is not None:
                current_date_now = '%s-%s-%s' % (current_date_conv[0],
                                                 current_date_conv[1],
                                                 current_date_conv[2])
            elif exact_date is True:
                current_date_now = '%s-%s-%s' % (current_date_conv[0],
                                                 current_date_conv[1],
                                                 current_date_conv[2])
            elif exact_date is False and before_days_count is None:
                current_date_now = '%s-%s-%s' % (current_date_conv[0],
                                                 current_date_conv[1], '01')
        final_date = datetime.strptime(str(current_date_now), '%Y-%m-%d')

        # Getting a days difference here
        if days_count is not None:
            print("days_count enabled!!!")
            if inc_type == 'pre':
                days_before = (final_date - timedelta(days=int('%d' %
                                                               (days_count))))
            else:
                days_before = (final_date + timedelta(days=int('%d' %
                                                               (days_count))))
            days_before = days_before.isoformat().split(' ')
            regex_date = re.match(r'(\d{4}-\d{2}-\d{2})', days_before[0])
            if regex_date is not None:
                before_date_conv = regex_date.group().split('-')
                if exact_date is False and before_days_count is not None:
                    string_of_days = '%s-%s-%s' % (before_date_conv[0],
                                                   before_date_conv[1],
                                                   before_date_conv[2])
                    print(
                        "We cant set satrt date as '01',So we are disabling this variable exact_date_and_month'"
                    )
                elif exact_date is True:
                    string_of_days = '%s-%s-%s' % (before_date_conv[0],
                                                   before_date_conv[1],
                                                   before_date_conv[2])
                elif exact_date is False and before_days_count is None:
                    string_of_days = '%s-%s-%s' % (before_date_conv[0],
                                                   before_date_conv[1], '01')
            final_before_days = datetime.strptime(str(string_of_days),
                                                  '%Y-%m-%d')
        else:
            print("months_count enabled!!!")
            if inc_type == 'pre':
                update_prev_mon = (current_date_and_time -
                                   relativedelta(months=int('+%d' %
                                                            (month_count))))
            else:
                update_prev_mon = (current_date_and_time +
                                   relativedelta(months=int('+%d' %
                                                            (month_count))))
            update_prev_mon = str(update_prev_mon)
            months_before = update_prev_mon.split(' ')
            regex_date = re.match(r'(\d{4}-\d{2}-\d{2})', months_before[0])
            if regex_date is not None:
                before_months_conv = regex_date.group().split('-')
                if exact_date is False and before_days_count is not None:
                    string_of_months = '%s-%s-%s' % (before_months_conv[0],
                                                     before_months_conv[1],
                                                     before_months_conv[2])
                elif exact_date is True:
                    string_of_months = '%s-%s-%s' % (before_months_conv[0],
                                                     before_months_conv[1],
                                                     before_months_conv[2])
                elif exact_date is False and before_days_count is None:
                    string_of_months = '%s-%s-%s' % (
                        before_months_conv[0], before_months_conv[1], '01')
            final_before_months = datetime.strptime(str(string_of_months),
                                                    '%Y-%m-%d')
        if final_before_months:
            datetime_format_before = final_before_months
            before_data = string_of_months
        else:
            datetime_format_before = final_before_days
            before_data = string_of_days
        # 'string_of_months': string_of_months, 'string_of_days': string_of_days,
        return_dict = {
            'current_date': current_date_now,
            'datetime_format_current': final_date,
            'before_data': before_data,
            'datetime_format_before': datetime_format_before
        }
        return True, return_dict
예제 #57
0
 def timeUntilWeekend(self, t):
     weekendStart = t + relativedelta(
         hour=18, minute=0, second=0, microseconds=0, weekday=FR)
     return weekendStart - t
예제 #58
0
def buildChunkList (DTstart, DTend):
    '''
    accepts:
        DTstart, DTend: datetime object
            start and end time of a broadcast that we wish to archive
    returns:
        chunkList (a list of hour long archives that will be used to build
            mp3 archive for a particular show)
        Each element of the ChunkList is a dict containing the following:
            'StartTime' : type = datetime.datetime.timetuple()
                (could start anytime during a clock hour)
            'Delta': type = datetime.timedelta
                (time duration of chunk, start time + delta can't spill over
                 into next clock hour)
         success: boolean
    '''
    chunkList = []
    
    duration = DTend - DTstart
    
    fourHours = DT.timedelta(seconds=60*60*4)
    if DTstart >= DTend or duration > fourHours: #start should be *before* the end!
        success = False
        return chunkList, success
    else:
        duraSeconds = duration.seconds
        
        showHours, partialEnd = numArchives(DTstart, DTend)
        partialOffset = 0
        if partialEnd:
            partialOffset = 1
        
        chunk= {}
        count = 0
        #if the show is an hour or less, does not stradle an hour, and doesn't end
            # at the end of an hour, this is an edge case ...
        if showHours == 1 and partialEnd == True:
            chunk['StartTime'] = DTstart
            chunk['TimeDelta'] = DTend - DTstart
            chunkList.append(chunk)
        
        else: #not an edge case
            # offset = time from beginning of show to end of first hour
                # ex: show starts at 2:15, offset is 45 minutes
            offset = (DTstart + relativedelta(hours=+1, 
                                minute =0, second=0)) -DTstart
              
            if count < showHours:
                chunk['StartTime'] = DTstart
                chunk['TimeDelta'] = offset
                chunkList.append(chunk)
                count += 1
            
            while count + partialOffset < showHours: # working with a complete hour
                chunk = {}   
                chunk['StartTime'] = chunkList[-1]['StartTime'] + \
                                chunkList[-1]['TimeDelta']
                chunk['TimeDelta'] = DT.timedelta(seconds=3600)
                chunkList.append(chunk)
                count += 1
            
            if partialEnd:
                chunk = {}
                chunk['StartTime'] = chunkList[-1]['StartTime'] + \
                                chunkList[-1]['TimeDelta']
                chunk['TimeDelta'] = DTend - chunk['StartTime']
                chunkList.append(chunk)
        
        success = True                                    
        return chunkList,success   
예제 #59
0
    def calcular(self):

        plazo_amortizacion = self.plazo - self.carencia
        amortizacion = round(self.capital / float(plazo_amortizacion), 2)

        for i in range(self.plazo + 1):

            if i == 0:
                fechaC = self.fecha
                mensual = 0
                interes = 0
                capAm = 0
                capPdte = self.capital

            elif i <= self.carencia:
                # Tratamiento de un periodo de carencia
                # Fecha del periodo
                fechaC = self.cuotas[i - 1].fecha + relativedelta(
                    months=+self.intervalo) + relativedelta(day=self.fecha.day)
                # Cálculo de los intereses del periodo
                if (self.clavePr == clave[1]):
                    # Clave [365/360]
                    dif = (fechaC - self.cuotas[i - 1].fecha).days
                    interes = round(
                        self.cuotas[i - 1].cap_pdte * self.tipo_int *
                        (dif / (self.intervalo * 30)), 2)
                else:
                    # Clave [360/360] o [365/365]
                    interes = round(
                        self.cuotas[i - 1].cap_pdte * self.tipo_int, 2)
                # La mensualidad son los intereses porque sólo pagamos los intereses
                mensual = interes
                # No se amortiza ningún capital
                capAm = 0
                # Entonces, seguimos con el mismo capital pendiente
                capPdte = round(self.cuotas[i - 1].cap_pdte - capAm, 2)

            else:
                fechaC = self.cuotas[i - 1].fecha + relativedelta(
                    months=+self.intervalo) + relativedelta(day=self.fecha.day)
                if self.clavePr == clave[1]:
                    dif = (fechaC - self.cuotas[i - 1].fecha).days
                    interes = round(
                        self.cuotas[i - 1].cap_pdte * self.tipo_int *
                        (dif / (self.intervalo * 30)), 2)
                else:
                    interes = round(
                        self.cuotas[i - 1].cap_pdte * self.tipo_int, 2)

                if i < self.plazo:
                    capAm = amortizacion
                    mensual = round(capAm + interes, 2)

                else:
                    capAm = self.cuotas[i - 1].cap_pdte
                    mensual = round(capAm + interes, 2)

                capPdte = self.cuotas[i - 1].cap_pdte - capAm

            cuota = Cuota(fechaC, mensual, interes, capAm, capPdte,
                          self.etiquetas)

            self.cuotas.append(cuota)
예제 #60
0
 def weekendTimeRemaining(self, t):
     weekendEnd = t + relativedelta(
         hour=6, minute=0, second=0, microseconds=0, weekday=MO)
     return weekendEnd - t