def generate_intervals(overlap:int=35, inc:int=250, init_start:str="2004-01-01", init_end:str="TODAY") -> list: """ start defaults to "2004-01-01", which represents the entire series. Format : "YYYY-MM-DD" """ to_str = lambda dt_date : datetime.strftime(dt_date, "%Y-%m-%d") to_dt = lambda str_date : datetime.strptime(str_date, "%Y-%m-%d") intervals = [] if init_end == "TODAY": init_end = to_dt(to_str(date.today())) else: init_end = to_dt(init_end) init_start = to_dt(init_start) duration = init_end - init_start n_iter = int(duration.days / (inc - overlap)) if n_iter == 0: return [to_str(init_start) + " " + to_str(init_end)] for i in range(n_iter): # Start(i) < End(i-1) # End(i) > Start(i+1) if i == 0: end = to_str(init_end) start = to_str(to_dt(end) - delta(days=+inc)) else: last = intervals[i-1] last_start, last_end = last[:10], last[11:] end = to_str(to_dt(last_start) + delta(days=+overlap))[:10] start = to_str(to_dt(end) - delta(days=+inc)) intervals.append(start + " " + end) intervals.reverse() return intervals
def cronBuilder(serverAddr, clientSeq, total): # assume clientSeq 0 based def cronHelper(time): # CHANGE iperf-client config here, if needed benchmarkCmd = "iperf -c %s --dualtest --window 416k --time %s " % ( serverAddr, CYCLE_DURATION) # CHANGE crontab lines here, if needed crontabCmd = "%s %s %s * * ubuntu python3 ~/SCRIPT/scripts/remote/iperf_slave.py -c %s -i %s -x '%s' -s %s -v %s\n" % ( str(time.minute), str(time.hour), str(time.day), CYCLES_PER_SET, EXPERIMENT_ID, benchmarkCmd, setId, vmId) # TODO pass setId, vmId to slave return crontabCmd utc_time = datetime.utcnow() launch_time = utc_time + delta(minutes=int(LAUNCH_DELAY)) cmdList = [] for i in range(total): setId, vmId = i + 1, clientSeq + 1 if REVERSE_FLAG is False and clientSeq <= i: cmdList.append( cronHelper(launch_time + delta(minutes=int(SETS_INTERVAL) * i))) if REVERSE_FLAG is True and clientSeq >= i: cmdList.append( cronHelper(launch_time + delta(minutes=int(SETS_INTERVAL) * i))) return cmdList
def this_quarter(): """ Return start and end date of this quarter. """ since = TODAY + delta(day=1) while since.month % 3 != 0: since -= delta(months=1) until = since + delta(months=3) return Date(since), Date(until)
def cronBuilder(starttime, clientSeq, total): # assume clientSeq 0 based def cronHelper(time, benchmark): # CHANGE crontab lines here, if needed #c5d_run.py [benchmark] [setId] [vmId] [runs] crontabCmd = "%s %s %s * * ubuntu python3 ~/SCRIPT/scripts/remote/c5d_run.py %s %s %s %s\n" % ( str(time.minute), str(time.hour), str( time.day), benchmark, setId, vmId, CYCLES_PER_SET) return crontabCmd launch_time = starttime + delta(minutes=int(LAUNCH_DELAY)) cmdList = [] setsOffset = 0 with open("/home/ubuntu/SCRIPT/scripts/remote/config.json", 'r') as fin: config = json.loads(fin.read()) for i in range(total): # each sets setId, vmId = i + 1, clientSeq + 1 benchmarkOffset = 0 for benchmark in config["types"]: # each benchmark if REVERSE_FLAG is False and clientSeq <= i: cmdList.append( cronHelper( launch_time + delta(minutes=(setsOffset + benchmarkOffset)), benchmark)) if REVERSE_FLAG is True and clientSeq >= i: cmdList.append( cronHelper( launch_time + delta(minutes=(setsOffset + benchmarkOffset)), benchmark)) benchmarkOffset += (int(config["timecost"][benchmark]) * CYCLES_PER_SET / 60) + 1 setsOffset += benchmarkOffset TIME.append([launch_time, datetime.now() + delta(minutes=setsOffset)]) return cmdList
def this_year(): """ Return start and end date of this fiscal year """ since = TODAY while since.month != 3 or since.day != 1: since -= delta(days=1) until = since + delta(years=1) return Date(since), Date(until)
def notify_user(auth, user_obj, req): rb_user = rb.extensions.get_user_data(auth, user_obj['href']) if not rb_user: return pt_user = _slack_email_dict.get(rb_user['email'], None) if not pt_user: return last_updated = dateutil.parser.parse(req['last_updated']) now = datetime.datetime.now(tzlocal()) idle_days = delta(now, last_updated).days idle_hours = delta(now, last_updated).hours + 24 * idle_days if idle_hours < 20: print ("review request %s from %s has been idle for %s hours => not nagging" % (req['id'], pt_user['profile']['real_name'], idle_hours)) return msg = ("%s, you have a lonely review request (repo %s) waiting on your action at: " + "https://review.salsitasoft.com/r/%s .") % ( pt_user['profile']['real_name'], req['links']['repository']['title'], req['id']) if idle_days > 2: msg += ("*This is getting serious*. " + "It's been lying there for *%s days* now!" % (idle_days,)) print " >>> %s" % (msg,) print " >>> idle for: %s days" % (idle_days,) _slack.chat.post_message('@' + pt_user['name'], msg)
def parse(self): """ Parse the options. """ # Run the parser opt, arg = self.parser.parse_known_args(self.arguments) self.opt = opt self.arg = arg self.check() # Enable --all if no particular stat or group selected opt.all = not any([ getattr(opt, stat.dest) or getattr(opt, group.dest) for group in self.sample_stats.stats for stat in group.stats ]) # Time period handling if opt.since is None and opt.until is None: opt.since, opt.until, period = did.base.Date.period(arg) else: opt.since = did.base.Date(opt.since or "1993-01-01") opt.until = did.base.Date(opt.until or "today") # Make the 'until' limit inclusive opt.until.date += delta(days=1) period = "given date range" # Validate the date range if not opt.since.date < opt.until.date: raise RuntimeError("Invalid date range ({0} to {1})".format( opt.since, opt.until.date - delta(days=1))) header = "Status report for {0} ({1} to {2}).".format( period, opt.since, opt.until.date - delta(days=1)) # Finito log.debug("Gathered options:") log.debug('options = {0}'.format(opt)) return opt, header
def get_context_data(self, **kwargs): context = super(DayView, self).get_context_data(**kwargs) context['form'] = TaskEntryForm prev_day = (parse(self.view_date) + delta(days=-1)).strftime('%Y/%b/%d') next_day = (parse(self.view_date) + delta(days=+1)).strftime('%Y/%b/%d') context['prev_day'] = prev_day.split('/') context['prev_day_s'] = prev_day context['next_day'] = next_day.split('/') context['next_day_s'] = next_day return context
def parse(self, arguments=None): """ Parse the options. """ # Split arguments if given as string and run the parser if arguments is not None: self.arguments = arguments if (self.arguments is not None and isinstance(self.arguments, basestring)): self.arguments = self.arguments.split() # Otherwise properly decode command line arguments if self.arguments is None: self.arguments = [arg.decode("utf-8") for arg in sys.argv[1:]] (opt, arg) = self.parser.parse_args(self.arguments) self.opt = opt self.arg = arg self.check() # Enable --all if no particular stat or group selected opt.all = not any([ getattr(opt, stat.dest) or getattr(opt, group.dest) for group in self.sample_stats.stats for stat in group.stats]) # Detect email addresses and split them on comma if not opt.emails: opt.emails = did.base.Config().email opt.emails = utils.split(opt.emails, separator=re.compile(r"\s*,\s*")) if not opt.emails: raise ConfigError("No email given. Use --email or create config.") # Time period handling if opt.since is None and opt.until is None: opt.since, opt.until, period = did.base.Date.period(arg) else: opt.since = did.base.Date(opt.since or "1993-01-01") opt.until = did.base.Date(opt.until or "today") # Make the 'until' limit inclusive opt.until.date += delta(days=1) period = "given date range" # Validate the date range if not opt.since.date < opt.until.date: raise RuntimeError( "Invalid date range ({0} to {1})".format( opt.since, opt.until.date - delta(days=1))) print(u"Status report for {0} ({1} to {2}).".format( period, opt.since, opt.until.date - delta(days=1))) # Finito log.debug("Gathered options:") log.debug('options = {0}'.format(opt)) return opt
def objects(refs="PMO BNK"): Journal = rt.modules.ledger.Journal USERS = Cycler(settings.SITE.user_model.objects.all()) OFFSETS = Cycler(12, 20, 28) START_YEAR = dd.plugins.ledger.start_year end_date = settings.SITE.demo_date(-30) for ref in refs.split(): offset = OFFSETS.pop() date = datetime.date(START_YEAR, 1, 1) jnl = Journal.objects.get(ref=ref) sug_table = jnl.voucher_type.table_class.suggestions_table while date < end_date: voucher = jnl.create_voucher(user=USERS.pop(), voucher_date=date + delta(days=offset)) yield voucher # start action request for do_fill: ba = sug_table.get_action_by_name('do_fill') ar = ba.request(master_instance=voucher) # select all rows: suggestions = sug_table.request(voucher) ar.selected_rows = list(suggestions) # run the action: ar.run() # some items differ from what was suggested: if ref == 'BNK': for item in voucher.items.all(): pd = PAYMENT_DIFFS.pop() if pd: pd = Decimal(pd) item.amount += item.amount * pd if item.amount: item.save() else: item.delete() # if no items have been created (or if they have been # deleted by PAYMENT_DIFFS), remove the empty voucher: if voucher.items.count() == 0: voucher.delete() else: voucher.register(REQUEST) voucher.save() date += delta(months=1)
def objects(refs="PMO BNK"): Journal = rt.modules.ledger.Journal USERS = Cycler(settings.SITE.user_model.objects.all()) OFFSETS = Cycler(12, 20, 28) START_YEAR = dd.plugins.ledger.start_year end_date = settings.SITE.demo_date(-30) for ref in refs.split(): offset = OFFSETS.pop() date = datetime.date(START_YEAR, 1, 1) jnl = Journal.objects.get(ref=ref) sug_table = jnl.voucher_type.table_class.suggestions_table while date < end_date: voucher = jnl.create_voucher( user=USERS.pop(), voucher_date=date + delta(days=offset)) yield voucher # start action request for do_fill: ba = sug_table.get_action_by_name('do_fill') ar = ba.request(master_instance=voucher) # select all rows: suggestions = sug_table.request(voucher) ar.selected_rows = list(suggestions) # run the action: ar.run() # some items differ from what was suggested: if ref == 'BNK': for item in voucher.items.all(): pd = PAYMENT_DIFFS.pop() if pd: pd = Decimal(pd) item.amount += item.amount * pd if item.amount: item.save() else: item.delete() # if no items have been created (or if they have been # deleted by PAYMENT_DIFFS), remove the empty voucher: if voucher.items.count() == 0: voucher.delete() else: voucher.register(REQUEST) voucher.save() date += delta(months=1)
def objects(): Journal = rt.models.ledger.Journal Person = rt.models.contacts.Person Product = rt.models.products.Product USERS = Cycler(settings.SITE.user_model.objects.all()) PRODUCTS = Cycler(Product.objects.order_by('id')) JOURNAL_S = Journal.objects.get(ref="SLS") CUSTOMERS = Cycler(Person.objects.filter( gender=dd.Genders.male).order_by('id')) assert Person.objects.count() > 0 ITEMCOUNT = Cycler(1, 2, 3) QUANTITIES = Cycler(15, 10, 8, 4) # SALES_PER_MONTH = Cycler(2, 1, 3, 2, 0) SALES_PER_MONTH = Cycler(5, 4, 1, 8, 6) date = datetime.date(dd.plugins.ledger.start_year, 1, 1) end_date = settings.SITE.demo_date(-10) # + delta(years=-2) while date < end_date: partner = None for i in range(SALES_PER_MONTH.pop()): # Every fifth time there are two successive invoices # to the same partner. if partner is None or i % 5: partner = CUSTOMERS.pop() invoice = sales.VatProductInvoice( journal=JOURNAL_S, partner=partner, user=USERS.pop(), voucher_date=date + delta(days=5 + i), entry_date=date + delta(days=5 + i + 1), # payment_term=PAYMENT_TERMS.pop(), ) yield invoice for j in range(ITEMCOUNT.pop()): item = sales.InvoiceItem( voucher=invoice, product=PRODUCTS.pop(), qty=QUANTITIES.pop()) item.product_changed(REQUEST) item.before_ui_save(REQUEST) yield item invoice.register(REQUEST) invoice.save() date += delta(months=1)
def plot_chart(request, code, term, terms): ohlc_dict = { 'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last' } freq, window, period, width = terms[term] end = Price.objects.filter(code=code).last().date if term == '3month' or term == '6month': start = end - delta(months=period) else: start = end - delta(years=period) query = Price.objects.filter(code=code).order_by('date') array = np.array([[r.date, r.open, r.high, r.low, r.close] for r in query.filter(date__range=(start, end))]) df = pd.DataFrame(array, columns=['date', 'open', 'high', 'low', 'close']).set_index('date') #株式調整 for row in Adjust.objects.filter(code=code): if row.date <= end: df.loc[df.index <= row.date] *= row.constant df.index = pd.to_datetime(df.index) ohlc_df = df.resample(freq).aggregate(ohlc_dict) ohlc_df.index = mdates.date2num(ohlc_df.index) ohlc_array = ohlc_df.reset_index().values #移動平均線 series = pd.Series({r.date: r.adjust for r in query}).rolling(window).mean() series = series[series.index >= start] fig, ax = plt.subplots() mpl_finance.candlestick_ohlc(ax, ohlc_array, width=width) series.plot(ax=ax) ax.grid() x_label = pd.date_range(start, end) steps = int(len(x_label) / 4) ax.set_xticks(x_label[0::steps]) canvas = FigureCanvasAgg(fig) output = io.BytesIO() canvas.print_png(output) plt.close(fig) return HttpResponse(output.getvalue(), content_type='image/png')
def period(argument): """ Detect desired time period for the argument """ since, until, period = None, None, None if "today" in argument: since = Date("today") until = Date("today") until.date += delta(days=1) period = "today" elif "yesterday" in argument: since = Date("yesterday") until = Date("yesterday") until.date += delta(days=1) period = "yesterday" elif "friday" in argument: since = Date("today") until = Date("today") since.date += delta(weekday=FRIDAY(-1)) until.date += delta(weekday=FRIDAY(-1)) until.date += delta(days=1) period = "the last friday" elif "year" in argument: if "last" in argument: since, until = Date.last_year() period = "the last fiscal year" else: since, until = Date.this_year() period = "this fiscal year" elif "quarter" in argument: if "last" in argument: since, until = Date.last_quarter() period = "the last quarter" else: since, until = Date.this_quarter() period = "this quarter" elif "month" in argument: if "last" in argument: since, until = Date.last_month() else: since, until = Date.this_month() period = since.datetime.strftime("%B") else: if "last" in argument: since, until = Date.last_week() else: since, until = Date.this_week() period = "the week {0}".format(since.datetime.strftime("%V")) return since, until, period
def generate_intervals(overlap: int = 40, inc: int = 265, start: str = "2004-01-01") -> list: """ start defaults to "2004-01-01", which represents the entire series. Format : "YYYY-MM-DD" """ to_str = lambda dt_date: datetime.strftime(dt_date, "%Y-%m-%d") to_dt = lambda str_date: datetime.strptime(str_date, "%Y-%m-%d") n_iter = 25 intervals = [] for i in range(n_iter): start = "2004-01-01" if i == 0 else to_str( to_dt(end) + delta(days=-overlap)) end = to_str(to_dt(start) + delta(days=+inc)) intervals.append(start + " " + end) return intervals
def get_date_expexted(self): if self.tanggal_mulai and self.line_ids: wp = max(sum([line.komponen_id.waktu_pengerjaan for line in self.line_ids if line.komponen_id]) - 1, 0) print 'wp', wp te = fields.Date.from_string(self.tanggal_mulai) + delta(days=wp) self.tanggal_expektasi = te else: self.tanggal_expektasi = False
def get_reviews(status, auth): time_added = datetime.date.today() - delta(days=5) return rb.extensions.get_review_requests2({ 'max-results': 200, 'ship-it-count-gt': 0, 'last-updated-from': time_added, 'status': status }, auth)
def fake_call(start, index): return CallRecord(started_at=start, ended_at=start + delta(minutes=random.randint(0, 1000)), call_id=uuid.uuid4(), source=NUMBER1 if index < 10 else NUMBER2, destination=fake_phonenumber(), duration=random_duration(), price=random_price())
def main(argv): C2S_MAP = createIperfPair() launchIperfServer() setsTotal = configurIperfClient(C2S_MAP) endTimeCounter = LAUNCH_DELAY + setsTotal * SETS_INTERVAL # in minutes endTime = datetime.now() + delta(minutes=int(endTimeCounter)) print("\nExperiment will end at :" + datetime.ctime(endTime)) print("\nuse collect_data.py to collect data at that time")
def test_should_filter_last_but_one_month(self, client): date = now() - delta(months=2) response = client.get('/bills/%s/%s-%s/' % (NUMBER1, date.month, date.year)) assert len(response.data) == 10 response = client.get('/bills/%s/%s-%s/' % (NUMBER2, date.month, date.year)) assert len(response.data) == 5
def get_context_data(self, **kwargs): context = super(MonthView, self).get_context_data(**kwargs) context['today'] = datetime.date.today().strftime('%Y-%b-%d').split('-') context['month'] = context.get('month', context['today'][1]) context['months'] = _months()[1:] context['daynames'] = calendar.weekheader(2).split(" ") context['days'] = calendar.monthcalendar(int(context['year']), _months(context['month'])) context['prev_year'] = int(context['year']) - 1 context['next_year'] = int(context['year']) + 1 view_date = '-'.join([context['year'], context['month']]) prev_month = (parse(view_date) + delta(months=-1)).strftime('%Y/%b') next_month = (parse(view_date) + delta(months=+1)).strftime('%Y/%b') context['prev_month'] = prev_month.split('/') context['prev_month_s'] = prev_month context['next_month'] = next_month.split('/') context['next_month_s'] = next_month return context
def period(argument): """ Detect desired time period for the argument """ since, until, period = None, None, None if "today" in argument: since = Date("today") until = Date("today") until.date += delta(days=1) period = "today" elif "yesterday" in argument: since = Date("yesterday") until = Date("yesterday") until.date += delta(days=1) period = "yesterday" elif "year" in argument: if "last" in argument: since, until = Date.last_year() period = "the last fiscal year" else: since, until = Date.this_year() period = "this fiscal year" elif "quarter" in argument: if "last" in argument: since, until = Date.last_quarter() period = "the last quarter" else: since, until = Date.this_quarter() period = "this quarter" elif "month" in argument: if "last" in argument: since, until = Date.last_month() period = "the last month" else: since, until = Date.this_month() period = "this month" else: if "last" in argument: since, until = Date.last_week() period = "the last week" else: since, until = Date.this_week() period = "this week" return since, until, period
def check_disability(self): notify_env = self.env['hr.notifications'] user_rec = self.env['res.users'].search([('id', '=', self._uid)], limit=1) emp_ids = self.search([('disability_limited_until', '!=', False)]) exp = user_rec.company_id.savirity or 1 date_after_month = datetime.now() + delta(weeks=exp) for rec in emp_ids: if date_after_month.strftime(DF) == rec.disability_limited_until: data_dict = {"emp": rec, 'date': rec.disability_limited_until} notify_env.create_notification('disability', data_dict)
def test_next_month(self): option = NextMonth() for loc in self._get_locales(): self._set_locale(loc) for month_day in [datetime.date(2007, 1, 1), datetime.date(2007, 1, 15), datetime.date(2007, 1, 31)]: option.get_today_date = lambda: month_day next_month_day = month_day + delta(months=+1) self.assertEqual(option.get_interval(), self._get_month_interval(next_month_day))
def __init__(self, date=None): """ Parse the date string """ if isinstance(date, datetime.date): self.date = date elif date is None or date.lower() == "today": self.date = TODAY elif date.lower() == "yesterday": self.date = TODAY - delta(days=1) else: self.date = datetime.date(*[int(i) for i in date.split("-")]) self.datetime = datetime.datetime( self.date.year, self.date.month, self.date.day, 0, 0, 0)
def send_notification(self): notify_env = self.env['hr.notifications'] user_rec = self.env['res.users'].search([('id', '=', self._uid)], limit=1) contract_ids = self.search([]) for rec in contract_ids: today = datetime.now() #.strftime(DF) #training_check if rec.trial_date_end: exp = user_rec.company_id.training_end_day or 1 date_after_month = today + delta(weeks=exp) if date_after_month.strftime(DF) == rec.trial_date_end: data_dict = { "emp": rec.employee_id, 'date': rec.trial_date_end } notify_env.create_notification('training_end', data_dict) #work Permit check if rec.visa_expire: exp = user_rec.company_id.work_permit or 1 date_after_month = today + delta(weeks=exp) if date_after_month.strftime(DF) == rec.visa_expire: data_dict = { "emp": rec.employee_id, 'date': rec.visa_expire } notify_env.create_notification('permit_end', data_dict) #end of term if rec.date_end: exp = user_rec.company_id.term_end or 1 date_after_month = today + delta(weeks=exp) new_contract = self.search([('employee_id', '=', rec.employee_id.id), ('date_start', '>', rec.date_end)]) if date_after_month.strftime( DF) == rec.date_end and not new_contract: data_dict = {"emp": rec.employee_id, 'date': rec.date_end} notify_env.create_notification('term_end', data_dict)
def test_next_week(self): option = NextWeek() for loc in self._get_locales(): self._set_locale(loc) # starting in 2008/01/01, wednesday for i in range(1, 8): get_today_date = lambda: datetime.date(2008, 1, i) option.get_today_date = get_today_date next_week_day = get_today_date() + delta(weeks=+1) self.assertEqual(option.get_interval(), self._get_week_interval(next_week_day)) self._testWeekday(loc, option.get_interval())
def setup_method(self): calls = [] # last but one month calls for i in range(15): calls.append( fake_call( fake.date_time_this_month(after_now=True, tzinfo=pytz.utc) - delta(months=2), i)) # last month calls for i in range(20): calls.append( fake_call( fake.date_time_this_month(after_now=True, tzinfo=pytz.utc) - delta(months=1), i)) # this month calls for i in range(10): calls.append( fake_call(fake.date_time_this_month(tzinfo=pytz.utc), i)) CallRecord.objects.bulk_create(calls)
def main(argv): print("please use iperf_master_privateip.py.") print( "This version routes through the public IP channel on AWS, and will burn your credits! " ) sys.exit() C2S_MAP = createIperfPair() launchIperfServer() setsTotal = configurIperfClient(C2S_MAP) endTimeCounter = LAUNCH_DELAY + setsTotal * SETS_INTERVAL # in minutes endTime = datetime.now() + delta(minutes=int(endTimeCounter)) print("\nExperiment will end at :" + datetime.ctime(endTime)) print("\nuse collect_data.py to collect data at that time")
def __init__(self, date=None): """ Parse the date string """ if isinstance(date, datetime.date): self.date = date elif date is None or date.lower() == "today": self.date = TODAY elif date.lower() == "yesterday": self.date = TODAY - delta(days=1) else: try: self.date = datetime.date(*[int(i) for i in date.split("-")]) except StandardError as error: log.debug(error) raise OptionError("Invalid date format: '{0}', use YYYY-MM-DD.".format(date)) self.datetime = datetime.datetime(self.date.year, self.date.month, self.date.day, 0, 0, 0)
def _compute_service_time(self): ## İki tarih arası yıl, ay, gün hesaplayan class for record in self: if record.ensure_one(): time_worked = delta(fields.date.today(), record.start_date) if (time_worked.days < 0) or (time_worked.months < 0) or (time_worked.years < 0): raise ValidationError( 'Start date is selected beyond today !!!') else: ## Burda da güzel bir hale dönüştürüyoruz. record.service_time = str( time_worked.years) + ' year(s) ' + str( time_worked.months) + ' month(s) ' + str( time_worked.days) + ' day(s)'
def __init__(self, date=None): """ Parse the date string """ if isinstance(date, datetime.date): self.date = date elif date is None or date.lower() == "today": self.date = TODAY elif date.lower() == "yesterday": self.date = TODAY - delta(days=1) else: try: self.date = datetime.date(*[int(i) for i in date.split("-")]) except StandardError as error: log.debug(error) raise OptionError( "Invalid date format: '{0}', use YYYY-MM-DD.".format(date)) self.datetime = datetime.datetime(self.date.year, self.date.month, self.date.day, 0, 0, 0)
def kwds_coordinates(self, num=10): keywords10 = self.get_keywords(num=num) end = datetime.now().strftime('%Y-%m-%d') start = (datetime.now() - delta(years=1)).strftime('%Y-%m-%d') kwds = keywords10.relkeyword.to_list() kwd01 = [] for x in range(0, len(kwds), 5): kwd01.append(kwds[x:x + 5]) ## naver data lab results = [] for i in range(len(kwd01)): client_id = keydict['nav_client_id'] client_secret = keydict['nav_client_secret'] url = "https://openapi.naver.com/v1/datalab/search" body = { 'startDate': start, 'endDate': end, 'timeUnit': 'month', 'keywordGroups': [] # 'keywordGroups': [{'groupName': '한글', 'keywords': ['한글', 'korean']}, # {'groupName': '영어', 'keywords': ['영어', 'english']}], # 'device': 'pc', # 'ages': ['1', '2'], # 'gender': 'f' } for kw in kwd01[i]: group = {'groupName': kw, 'keywords': [kw]} body['keywordGroups'].append(group) header = {} header["X-Naver-Client-Id"] = client_id header["X-Naver-Client-Secret"] = client_secret header["Content-Type"] = "application/json" rq = requests.post(url, json=body, headers=header) results.extend(rq.json()['results']) self.kwds_coordis = results return results
def parse(self, arguments=None): """ Parse the options. """ # Split arguments if given as string and run the parser if arguments is not None: self.arguments = arguments if self.arguments is not None and isinstance(self.arguments, basestring): self.arguments = self.arguments.split() # Otherwise properly decode command line arguments if self.arguments is None: self.arguments = [arg.decode("utf-8") for arg in sys.argv[1:]] opt, arg = self.parser.parse_known_args(self.arguments) self.opt = opt self.arg = arg self.check() # Enable --all if no particular stat or group selected opt.all = not any( [ getattr(opt, stat.dest) or getattr(opt, group.dest) for group in self.sample_stats.stats for stat in group.stats ] ) # Time period handling if opt.since is None and opt.until is None: opt.since, opt.until, period = did.base.Date.period(arg) else: opt.since = did.base.Date(opt.since or "1993-01-01") opt.until = did.base.Date(opt.until or "today") # Make the 'until' limit inclusive opt.until.date += delta(days=1) period = "given date range" # Validate the date range if not opt.since.date < opt.until.date: raise RuntimeError("Invalid date range ({0} to {1})".format(opt.since, opt.until.date - delta(days=1))) header = "Status report for {0} ({1} to {2}).".format(period, opt.since, opt.until.date - delta(days=1)) # Finito log.debug("Gathered options:") log.debug("options = {0}".format(opt)) return opt, header
def findAllMeetings(month='All', endDate=None, startDate=firstDate): ''' Finds all meetings between two dates that occur in a given month, or all months. ''' if endDate == None: endDate = datetime.now() dates = [] if not month.isdigit(): month = months.index(month[:3].lower()) + 1 while startDate < endDate: if startDate.month != month and month != 'All': startDate += delta(months=1) continue if startDate.month == 5: #May # Find the last day of the month startDate += delta(months=1) startDate = datetime(startDate.year, startDate.month, 1) - delta(days=1) # Subtract out days to get to monday. # Monday == 0, so the following line will get to the last # Monday of the month. startDate -= delta(days=startDate.weekday()) #Subtract another 7 days to get the second-to-last Monday. startDate -= delta(days=7) dates.append(startDate) elif startDate.month == 11: #November # No meeting in November pass # Obviously the hardest part of this program elif startDate.month == 12: #December # Find the first day of the month startDate = datetime(startDate.year, startDate.month, 1) # Find the next Monday # Uses modulo arithmatic instead of a simple 7-X because # 7-0 = 7, where as (-0)%7 = 0. If the given day happens # to be Monday by chance, this version will use it instead # of the Monday after that. startDate += delta(days=-startDate.weekday()%7) # Add a week if the day is the 1st or 2nd if startDate.day in [1,2]: startDate += delta(days=7) dates.append(startDate) else: # All other months # Find the last Monday of the month # Identical to the condition for May above startDate += delta(months=1) startDate = datetime(startDate.year, startDate.month, 1) - delta(days=1) startDate -= delta(days=startDate.weekday()) dates.append(startDate) startDate += delta(months=1) return dates
def last_month(): """ Return start and end date of this month. """ since = TODAY + delta(day=1, months=-1) until = since + delta(months=1) return Date(since), Date(until)
def keywords_graph(kwd): if not os.path.exists('./data'): os.mkdir('data') ## naver ads keywords BASE_URL = 'https://api.naver.com' uri = '/keywordstool' method = 'GET' payload = {'hintKeywords': kwd, 'showDetail': 1} r = requests.get(BASE_URL + uri, params=payload, headers=get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID)) keywords = r.json()['keywordList'][:100] del r # preprocessing keywords = pd.DataFrame(keywords) keywords.columns = [ 'relkeyword', 'sch', 'mob_sch', 'click', 'mob_click', 'clk_r', 'mob_clk_r', 'num_ads', 'comp' ] comp_dic = {'높음': 2, '중간': 1, '낮음': 0} keywords.comp = keywords.comp.apply(lambda x: comp_dic[x]) keywords.sch = keywords.sch.astype('str').str.replace('< ', '').astype('int') keywords.mob_sch = keywords.mob_sch.astype('str').str.replace( '< ', '').astype('int') # preprocessing before entering DataLab API keywords10 = keywords[:10] from datetime import datetime from dateutil.relativedelta import relativedelta as delta end = datetime.now().strftime('%Y-%m-%d') start = (datetime.now() - delta(years=1)).strftime('%Y-%m-%d') kwds = keywords10.relkeyword.to_list() kwd01 = [] kwd01.append(kwds[:5]) kwd01.append(kwds[5:]) ## naver data lab for i in range(2): client_id = keydict['nav_client_id'] client_secret = keydict['nav_client_secret'] url = "https://openapi.naver.com/v1/datalab/search" body = { 'startDate': start, 'endDate': end, 'timeUnit': 'month', 'keywordGroups': [] # 'keywordGroups': [{'groupName': '한글', 'keywords': ['한글', 'korean']}, # {'groupName': '영어', 'keywords': ['영어', 'english']}], # 'device': 'pc', # 'ages': ['1', '2'], # 'gender': 'f' } for kw in kwd01[i]: group = {'groupName': kw, 'keywords': [kw]} body['keywordGroups'].append(group) header = {} header["X-Naver-Client-Id"] = client_id header["X-Naver-Client-Secret"] = client_secret header["Content-Type"] = "application/json" rq = requests.post(url, json=body, headers=header) results = rq.json()['results'] fig, axs = plt.subplots(1, 1, constrained_layout=True, sharey=True, figsize=(15, 8)) for item in results: df = pd.DataFrame(item['data']) axs.plot(df.period, df.ratio, '--o', label=item['title']) axs.grid(True) plt.xticks(rotation=45) plt.legend(loc='best') plt.xlabel('month') plt.ylabel('Trend') fig.savefig('data/graph_%s%d.png' % (kwd, i))
def objects(): TradeTypes = rt.models.ledger.TradeTypes #VatRule = rt.models.vat.VatRule Journal = rt.models.ledger.Journal # Person = rt.models.contacts.Person Partner = rt.models.contacts.Partner Product = rt.models.products.Product VatProductInvoice = rt.models.sales.VatProductInvoice InvoiceItem = rt.models.sales.InvoiceItem def get_trade_countries(tt): Country = rt.models.countries.Country areas = set() for va in VatAreas.get_list_items(): if VatRules.get_vat_rule(va, tt, default=False): areas.add(va) for obj in Country.objects.all(): if VatAreas.get_for_country(obj) in areas: yield obj USERS = Cycler(settings.SITE.user_model.objects.all()) PRODUCTS = Cycler(Product.objects.order_by('id')) JOURNAL_S = Journal.objects.get(ref="SLS") # tt = TradeTypes.sales # regimes = set() # for reg in VatRegimes.get_list_items(): # if VatRule.get_vat_rule(tt, reg, default=False): # regimes.add(reg) # qs = Partner.objects.filter(vat_regime__in=regimes).order_by('id') # assert qs.count() > 0 # CUSTOMERS = Cycler(qs) CUSTOMERS = Cycler( Partner.objects.filter( country__in=get_trade_countries( TradeTypes.sales)).order_by('id')) if len(CUSTOMERS) == 0: raise Exception("20171006 no customers ({})".format( len(list(get_trade_countries(TradeTypes.sales))))) # CUSTOMERS = Cycler(Person.objects.filter( # gender=dd.Genders.male).order_by('id')) # assert Person.objects.count() > 0 ITEMCOUNT = Cycler(1, 2, 3) QUANTITIES = Cycler(15, 10, 8, 4) # SALES_PER_MONTH = Cycler(2, 1, 3, 2, 0) SALES_PER_MONTH = Cycler(5, 4, 1, 8, 6) date = datetime.date(dd.plugins.ledger.start_year, 1, 1) end_date = settings.SITE.demo_date(-10) # + delta(years=-2) while date < end_date: partner = None for i in range(SALES_PER_MONTH.pop()): # Every fifth time there are two successive invoices # to the same partner. if partner is None or i % 5: partner = CUSTOMERS.pop() invoice = VatProductInvoice( journal=JOURNAL_S, partner=partner, user=USERS.pop(), voucher_date=date + delta(days=5 + i), entry_date=date + delta(days=5 + i + 1), # payment_term=PAYMENT_TERMS.pop(), ) yield invoice for j in range(ITEMCOUNT.pop()): item = InvoiceItem( voucher=invoice, seqno=j+1, product=PRODUCTS.pop(), qty=QUANTITIES.pop()) try: item.product_changed(REQUEST) except Exception as e: msg = "20171006 {} in ({} {!r})".format( e, invoice.partner, invoice.vat_regime) # raise Exception(msg) dd.logger.warning(msg) else: item.before_ui_save(REQUEST) yield item invoice.register(REQUEST) invoice.save() date += delta(months=1)
def last_year(): """ Return start and end date of the last fiscal year """ since, until = Date.this_year() since = since.date - delta(years=1) until = until.date - delta(years=1) return Date(since), Date(until)
def objects(refs="PMO BNK"): Journal = rt.models.ledger.Journal Company = rt.models.contacts.Company Movement = rt.models.ledger.Movement USERS = Cycler(settings.SITE.user_model.objects.all()) OFFSETS = Cycler(12, 20, 28) START_YEAR = dd.plugins.ledger.start_year end_date = settings.SITE.demo_date(-30) site_company = settings.SITE.site_config.site_company ses = rt.login('robin') qs = Company.objects.filter(country__isnull=False) # if qs.count() < 10: # raise Exception("20171009") for p in qs: if Movement.objects.filter(partner=p, cleared=False).count(): add_demo_account(p) for ref in refs.split(): # if ref == 'BNK': # continue # temp 20171007 jnl = Journal.objects.get(ref=ref) sug_table = jnl.voucher_type.table_class.suggestions_table do_fill = sug_table.get_action_by_name('do_fill') if ref == 'PMO': assert site_company is not None if site_company.country is None: raise Exception( "Oops, site company {} has no country".format( site_company)) acct = add_demo_account(site_company) jnl.sepa_account = acct yield jnl offset = OFFSETS.pop() date = datetime.date(START_YEAR, 1, 1) while date < end_date: voucher = jnl.create_voucher( user=USERS.pop(), entry_date=date + delta(days=offset)) yield voucher # start action request for do_fill: ar = do_fill.request(master_instance=voucher) # select all rows: suggestions = sug_table.request(voucher) ar.selected_rows = list(suggestions) # run the action: ar.run() # some items differ from what was suggested: if ref == 'BNK': for item in voucher.items.all(): pd = PAYMENT_DIFFS.pop() if pd: pd = Decimal(pd) item.amount += item.amount * pd if item.amount: item.save() else: item.delete() # if no items have been created (or if they have been # deleted by PAYMENT_DIFFS), remove the empty voucher: if voucher.items.count() == 0: voucher.delete() else: # if ref == 'PMO': # voucher.execution_date = voucher.entry_date # assert voucher.execution_date is not None voucher.register(REQUEST) voucher.save() # for i in voucher.items.all(): # if i.partner: # yield add_demo_account(i.partner) # For payment orders we also write the XML file if ref == 'PMO': rv = voucher.write_xml.run_from_session(ses) if not rv['success']: raise Exception("20170630") if False: # not needed here because write_xml validates fn = Path(settings.SITE.cache_dir + rv['open_url']) if not fn.exists(): raise Exception("20170630") validate_pain001(fn) date += delta(months=1)
def last_quarter(): """ Return start and end date of this quarter. """ since, until = Date.this_quarter() since = since.date - delta(months=3) until = until.date - delta(months=3) return Date(since), Date(until)
def last_week(): """ Return start and end date of the last week. """ since = TODAY + delta(weekday=MONDAY(-2)) until = since + delta(weeks=1) return Date(since), Date(until)
def this_week(): """ Return start and end date of the current week. """ since = TODAY + delta(weekday=MONDAY(-1)) until = since + delta(weeks=1) return Date(since), Date(until)
def _get_week_interval(self, today): weekday = get_weekday_start() start = today + delta(weekday=weekday(-1)) end = start + delta(days=+6) return start, end
Defines classes related to date ranges. This is a tested document. To test it, run:: $ python setup.py test -s tests.UtilsTests.test_dates """ from __future__ import unicode_literals import collections from dateutil.rrule import DAILY, rrule, MO, TU, WE, TH, FR from dateutil.relativedelta import relativedelta as delta AMONTH = delta(months=1) ADAY = delta(days=1) from lino.utils.format_date import fds DateRangeValue = collections.namedtuple( 'DateRangeValue', ('start_date', 'end_date')) """ A named tuple with the following fields: .. attribute:: start_date The start date .. attribute:: end_date
def _get_month_interval(self, today): start = today + delta(day=1) end = start + delta(day=31) return start, end
def objects(): Company = dd.resolve_model('contacts.Company') Person = dd.resolve_model('contacts.Person') Product = dd.resolve_model('products.Product') if False: # old system MODEL = ledger.AccountInvoice vt = ledger.VoucherTypes.get_for_model(MODEL) JOURNALS = Cycler(vt.get_journals()) Partner = dd.resolve_model(partner_model) #~ logger.info("20130105 mini Partners %s",Partner.objects.all().count()) #~ PARTNERS = Cycler(Partner.objects.order_by('name')) PARTNERS = Cycler(Company.objects.order_by('id')) USERS = Cycler(settings.SITE.user_model.objects.all()) AMOUNTS = Cycler([Decimal(x) for x in "2.50 6.80 9.95 14.50 20 29.90 39.90 39.90 99.95 199.95 599.95 1599.99".split()]) ITEMCOUNT = Cycler(1, 2, 3) for i in range(10): u = USERS.pop() jnl = JOURNALS.pop() invoice = MODEL(journal=jnl, partner=PARTNERS.pop(), user=u, date=settings.SITE.demo_date(-30 + i)) yield invoice ar = MODEL.request(user=u) for j in range(ITEMCOUNT.pop()): item = ledger.InvoiceItem(voucher=invoice, account=jnl.get_allowed_accounts()[ 0], #~ product=PRODUCTS.pop(), total_incl=AMOUNTS.pop() ) item.total_incl_changed(ar) item.before_ui_save(ar) #~ if item.total_incl: #~ print "20121208 ok", item #~ else: #~ if item.product.price: #~ raise Exception("20121208") yield item invoice.register(ar) invoice.save() USERS = Cycler(settings.SITE.user_model.objects.all()) if sales: yield Product(name="Foo", sales_price='399.90') yield Product(name="Bar", sales_price='599.90') yield Product(name="Baz", sales_price='990.00') PRODUCTS = Cycler(Product.objects.order_by('id')) JOURNAL_S = ledger.Journal.objects.get(ref="S") #~ assert JOURNAL_S.dc == accounts.DEBIT CUSTOMERS = Cycler(Person.objects.order_by('id')) ITEMCOUNT = Cycler(1, 2, 3) QUANTITIES = Cycler(5, 1, 2, 3) SALES_PER_MONTH = Cycler(2, 1, 3, 2, 0) PROVIDERS = Cycler(Company.objects.order_by('id')) JOURNAL_P = ledger.Journal.objects.get(ref="P") #~ assert JOURNAL_P.dc == accounts.CREDIT ACCOUNTS = Cycler(JOURNAL_P.get_allowed_accounts()) AMOUNTS = Cycler([Decimal(x) for x in "20 29.90 39.90 99.95 199.95 599.95 1599.99".split()]) AMOUNT_DELTAS = Cycler([Decimal(x) for x in "0 0.60 1.10 1.30 2.50".split()]) DATE_DELTAS = Cycler((1, 2, 3, 4, 5, 6, 7)) INFLATION_RATE = Decimal("0.02") """ 5 "purchase stories" : each story represents a provider who sends monthly invoices. """ PURCHASE_STORIES = [] for i in range(5): # provider, (account,amount) story = (PROVIDERS.pop(), []) story[1].append((ACCOUNTS.pop(), AMOUNTS.pop())) if i % 3: story[1].append((ACCOUNTS.pop(), AMOUNTS.pop())) PURCHASE_STORIES.append(story) #~ date = settings.SITE.demo_date() + delta(years=-2) START_YEAR = settings.SITE.start_year # 2011 date = datetime.date(START_YEAR, 1, 1) end_date = datetime.date(2013, 5, 1) while date < end_date: if sales: for i in range(SALES_PER_MONTH.pop()): #~ print __file__, date invoice = sales.Invoice(journal=JOURNAL_S, partner=CUSTOMERS.pop(), user=USERS.pop(), date=date + delta(days=10 + DATE_DELTAS.pop())) yield invoice for j in range(ITEMCOUNT.pop()): item = sales.InvoiceItem(voucher=invoice, product=PRODUCTS.pop(), qty=QUANTITIES.pop() ) item.product_changed(REQUEST) item.before_ui_save(REQUEST) #~ if item.total_incl: #~ print "20121208 ok", item #~ else: #~ if item.product.price: #~ raise Exception("20121208") yield item #~ invoice.set_workflow_state('registered') # ~ invoice.state = 'registered' # automatically call invoice.register(REQUEST) invoice.save() for story in PURCHASE_STORIES: invoice = ledger.AccountInvoice(journal=JOURNAL_P, partner=story[0], user=USERS.pop(), date=date + delta(days=DATE_DELTAS.pop())) yield invoice for account, amount in story[1]: amount += amount + \ (amount * INFLATION_RATE * (date.year - START_YEAR)) item = ledger.InvoiceItem(voucher=invoice, account=account, total_incl=amount + AMOUNT_DELTAS.pop() ) item.total_incl_changed(REQUEST) item.before_ui_save(REQUEST) #~ if item.total_incl: #~ print "20121208 ok", item #~ else: #~ if item.product.price: #~ raise Exception("20121208") yield item invoice.register(REQUEST) invoice.save() # last month not yet done if finan and (end_date - date) > MORE_THAN_A_MONTH: #~ po = finan.PaymentOrder(journal=JOURNAL_PO, JOURNAL_PO = ledger.Journal.objects.get(ref="PO") po = JOURNAL_PO.create_voucher( user=USERS.pop(), date=date + delta(days=20)) yield po suggestions = finan.SuggestionsByPaymentOrder.request(po) ba = finan.SuggestionsByPaymentOrder.get_action_by_name('do_fill') ar = ba.request(master_instance=po) ar.selected_rows = [x for x in suggestions] ar.run() po.register(REQUEST) po.save() #~ bs = finan.BankStatement(journal=JOURNAL_BANK, JOURNAL_BANK = ledger.Journal.objects.get(ref="B") bs = JOURNAL_BANK.create_voucher( user=USERS.pop(), date=date + delta(days=28)) yield bs suggestions = finan.SuggestionsByBankStatement.request(bs) ba = suggestions.actor.get_action_by_name('do_fill') ar = ba.request(master_instance=bs) ar.selected_rows = [x for x in suggestions] ar.run() bs.register(REQUEST) bs.save() date += delta(months=1)
def objects(): Journal = rt.models.ledger.Journal Company = rt.models.contacts.Company USERS = Cycler(settings.SITE.user_model.objects.all()) PROVIDERS = Cycler(Company.objects.filter(sepa_accounts__iban__isnull=False).order_by("id")) JOURNAL_P = Journal.objects.get(ref="PRC") ACCOUNTS = Cycler(JOURNAL_P.get_allowed_accounts()) AMOUNTS = Cycler([Decimal(x) for x in "20 29.90 39.90 99.95 199.95 599.95 1599.99".split()]) AMOUNT_DELTAS = Cycler([Decimal(x) for x in "0 0.60 1.10 1.30 2.50".split()]) DATE_DELTAS = Cycler((1, 2, 3, 4, 5, 6, 7)) INFLATION_RATE = Decimal("0.02") """"purchase stories" : each story represents a provider who sends monthly invoices. """ PURCHASE_STORIES = [] for i in range(5): # provider, (account,amount) story = (PROVIDERS.pop(), []) story[1].append((ACCOUNTS.pop(), AMOUNTS.pop())) if i % 3: story[1].append((ACCOUNTS.pop(), AMOUNTS.pop())) PURCHASE_STORIES.append(story) START_YEAR = dd.plugins.ledger.start_year date = datetime.date(START_YEAR, 1, 1) end_date = settings.SITE.demo_date(-10) # + delta(years=-2) # end_date = datetime.date(START_YEAR+1, 5, 1) # print(20151216, START_YEAR, settings.SITE.demo_date(), end_date - date) while date < end_date: for story in PURCHASE_STORIES: vd = date + delta(days=DATE_DELTAS.pop()) invoice = vat.VatAccountInvoice( journal=JOURNAL_P, partner=story[0], user=USERS.pop(), voucher_date=vd, # payment_term=PAYMENT_TERMS.pop(), entry_date=vd + delta(days=1), ) yield invoice for account, amount in story[1]: amount += amount + (amount * INFLATION_RATE * (date.year - START_YEAR)) item = vat.InvoiceItem( voucher=invoice, account=account, total_incl=myround(amount) + AMOUNT_DELTAS.pop() ) item.total_incl_changed(REQUEST) item.before_ui_save(REQUEST) # ~ if item.total_incl: # ~ print "20121208 ok", item # ~ else: # ~ if item.product.price: # ~ raise Exception("20121208") yield item invoice.register(REQUEST) invoice.save() date += delta(months=1)