Exemplo n.º 1
0
def apply_after_restoration(restoration_base_date = 'no_restoration',\
 start_date = 'very_beginning',\
 end_date = 'today',\
 remote_root = 'Default',\
 local_cache_root = '',\
 dtsk_data = ''):

    restoration_factor_file = directory.open_prioritized_file(\
     file_relative_path = 'StockInfo/after_restoration_factor_list.txt',
     remote_root = remote_root, local_cache_root = local_cache_root)


    trading_days_file = directory.open_prioritized_file(\
     file_relative_path = 'StockInfo/tradingdays.txt',\
     remote_root = remote_root, local_cache_root = local_cache_root)

    date_list = trading_days_file.read().splitlines()
    start_date = date.parse(start_date)
    end_date = date.parse(end_date)

    stock_name = ''
    re_start_date_list = []
    re_end_date_list = []
    price_list = []
    for line in reversed(restoration_factor_file.read().splitlines()):
        if not line:
            continue

        line_words = line.split()
        symbol = line_words[0]
        re_start_date = line_words[1]  # include
        re_start_date = date.parse(re_start_date)
        re_end_date = line_words[2]  # include
        re_end_date = date.parse(re_end_date)
        price = float(line_words[3])

        if not stock_name or stock_name.lower() == symbol.lower():
            stock_name = symbol
            re_end_date_list.append(re_end_date)
            re_start_date_list.append(re_start_date)
            price_list.append(price)
        else:
            dtsk_date = calculate_restoration(\
             restoration_base_date = restoration_base_date, dtsk_data = dtsk_data,\
             stock_name = stock_name, price_list = price_list, end_date = end_date,\
             date_list = date_list, re_start_date_list = re_start_date_list,\
             re_end_date_list = re_end_date_list, start_date = start_date)

            stock_name = ''
            re_end_date_list = []
            re_start_date_list = []
            price_list = []

    dtsk_date = calculate_restoration(\
     restoration_base_date = restoration_base_date, dtsk_data = dtsk_data,\
     stock_name = stock_name, price_list = price_list, end_date = end_date,\
     date_list = date_list, re_start_date_list = re_start_date_list,\
     re_end_date_list = re_end_date_list, start_date = start_date)

    return dtsk_data
Exemplo n.º 2
0
def sort_by_account_within_date(entries,
                                account_reverse=False,
                                date_reverse=True,
                                insert_none=True,
                                amount_sort=False):

    months = range(1, 13) if not date_reverse else range(12, 0, -1)

    bucket = {month: [] for month in months}
    for ety in entries:
        (month, _, _) = date.parse(ety.date())
        bucket[month].append(ety)

    for month in months:
        bucket[month].sort(key=entry.date_key)
        bucket[month].sort(key=entry.account_key, reverse=account_reverse)
        if amount_sort:
            bucket[month].sort(key=entry.amount_key, reverse=True)

    new_entries = []
    first_month = True
    for month in months:
        if not bucket[month]:
            continue
        if not first_month and insert_none:
            new_entries.extend([None])
        first_month = False
        new_entries.extend(bucket[month])
    return new_entries
Exemplo n.º 3
0
def calculate_restoration(restoration_base_date = 'no_restoration', end_date = 'today',\
 stock_name = '', price_list = [], date_list = [], re_start_date_list = [],\
 re_end_date_list = [], start_date = 'very_beginning',  dtsk_data = ''):

    multiple_keys = get_multiple_keys()
    divided_keys = get_divided_keys()

    if restoration_base_date == 'no_restoration':
        return dtsk_data
    restoration_base_date = date.parse(restoration_base_date)
    start_date = date.parse(start_date)
    end_date = date.parse(end_date)

    base_restoration_price = 1
    for index, price in enumerate(price_list):
        if restoration_base_date >= re_start_date_index[index] and \
         restoration_base_date <= re_end_date_list[index]:
            base_restoration_price = price
            break

    if stock_name in dtsk_data['SYMBOL'].values:
        start = date_list[bisect.bisect_left(date_list, start_date)]
        end = date_list[bisect.bisect_right(date_list, end_date) - 1]

        dtsk_data.loc[start:end, :, stock_name,
                      multiple_keys].values /= base_restoration_price
        dtsk_data.loc[start:end, :, stock_name,
                      divided_keys].values *= base_restoration_price

        for index, price in enumerate(price_list):
            re_start_date = re_start_date_list[index]
            if start_date > re_start_date:
                re_start_date = start_date

            re_end_date = re_end_date_list[index]
            if end_date < re_end_date:
                re_end_date = end_date

            start = date_list[bisect.bisect_left(date_list, re_start_date)]
            end = date_list[bisect.bisect_right(date_list, re_end_date) - 1]

            dtsk_data.loc[start:end, :, stock_name,
                          multiple_keys].values *= price
            dtsk_data.loc[start:end, :, stock_name,
                          divided_keys].values /= price

    return dtsk_data
Exemplo n.º 4
0
def apply(restoration_base_date = 'no_restoration',\
  start_date = 'very_beginning',\
  end_date = 'today',\
  dtsk_data = ''):

    # First, Check parameters.
    # Using the same utility with dtsk.load()

    if restoration_base_date.lower() == 'no_restoration'.lower():
        print 'no restoration.'
        return dtsk_data
    restoration_base_date = date.parse(restoration_base_date)
    start_date = date.parse(start_date)
    end_date = date.parse(end_date)
    new_dtsk = dtsk_data

    # Second, Seperate 3 cases and apply:
    # Situation 1: restoration_base_date < start_date,
    # DTSK will apply after_restoration based on restoration_base_date
    if restoration_base_date < start_date:
        print 'try to apply after restoration.'
        new_dtsk = apply_after_restoration(\
         restoration_base_date = restoration_base_date,\
         start_date = start_date,\
         end_date = end_date,\
         dtsk_data = dtsk_data)

    # Situation 2: restoration_base_date > end_date.
    # DTSK will apply forward_restoration based on restoration_base_date.
    elif restoration_base_date > end_date:
        print 'try to apply forward restoration.'
        new_dtsk = apply_forward_restoration(\
         restoration_base_date = restoration_base_date,\
         start_date = start_date,\
         end_date = end_date,\
         dtsk_data = dtsk_data)

    # Situation 3: start_date <= restorateion_base_date <= end_date:
    # DTSK will not apply any restoration factor.
    else:
        print 'no restoration.'
        return dtsk_data

    # [To Implement]. Third, Automation of test.

    return new_dtsk
def parse():
    parser = command_line_parser()
    args = parser.parse_args()

    if args.month is None and args.date_start is not None:
        (args.month, _, _) = date.parse(args.date_start)
    if args.month is None and args.date_end is not None:
        (args.month, _, _) = date.parse(args.date_end)
    if args.month is None:
        args.month = date.this_month()

    if args.year is None and args.date_start is not None:
        (_, _, args.year) = date.parse(args.date_start)
    if args.year is None and args.date_end is not None:
        (_, _, args.year) = date.parse(args.date_end)
    if args.year is None:
        args.year = date.this_year()

    if not (0 <= args.month and args.month <= 12):
        raise ValueError("Invalid month: " + args.month)
    if not (0 <= args.year and args.year <= 3000):
        raise ValueError("Invalid year: " + args.year)

    args.month_name = date.month_name(args.month)

    if args.date_start is None:
        args.date_start = date.month_start(month=args.month, year=args.year)
    if args.date_end is None:
        args.date_end = date.today()
    if args.posted_start is None:
        args.posted_start = args.date_start

    args.date_start = date.fmt(args.date_start)
    args.date_end = date.fmt(args.date_end)
    args.posted_start = date.fmt(args.posted_start)

    if args.all_reports:
        args.material_report = True
        args.ministry_report = True
        args.unassigned_report = True
        args.vendor_report = True
        args.subfund_report = True

    return args
    def __init__(self, application, se=True, ge=True, ts=None):
        # self.entities = entities

        if ts is not None:
            rel = ['WILL USE', 'MAY USE']
            self.ts = date.parse(ts)
        else:
            rel = []

        # print('Experiment timestamp: %s  -- %s' % (self.ts, ts))

        DependencyVisitor.__init__(self, rel, se, ge)

        self.application = application
	def __init__(self, application, se = True, ge = True, ts = None):
		# self.entities = entities
		
		if ts is not None:
			rel = ['WILL USE', 'MAY USE']
			self.ts = date.parse(ts)
		else:
			rel = []
			
		# print('Experiment timestamp: %s  -- %s' % (self.ts, ts))
		
		DependencyVisitor.__init__(self, rel, se, ge)
		
		self.application = application
Exemplo n.º 8
0
def collectfixmes(dw, ns, exceptions = []):
	pages = dw.searchpages("FIXME")
	# print(pages)

	rx_namespace = re.compile(ns, re.IGNORECASE)
	rx_exceptions = [re.compile(ex, re.IGNORECASE) for ex in exceptions]
	
	pageids = [(dw.resolve(p['id']), p['score']) for p in pages]
	fixmes = []
	
	for pid, hits in pageids:
		if not rx_namespace.match(pid):
			continue
		
		skip = False
		
		for ex in rx_exceptions:
			if ex.match(pid) is not None:
				skip = True
				break
		
		if skip:
			continue
		
		doc = dw.getpage(pid)
		text = '\n'.join(doc)

		# print("Page: %s (%d)" % (pid, hits))
		matches = FixMe.rx_syntax.finditer(text)

		numfixmes = 0
		for f in matches:
			# print(f.group())
			numfixmes += 1
			partner = f.group(1)
			todo = f.group(4)
			deadline = f.group(3)
			
			# print("%s - %s\n%s" % (partner, deadline, todo))
			# print("%s  --  %s" % (deadline, date.parse(deadline)))
			
			fixmes.append(FixMe(partner, todo, deadline, pid))
			
		if numfixmes != hits:
			logging.warning("Invalid FIXME syntax on page %s" % pid)

	fixmes.sort(key = lambda f: (date.parse(f.deadline), f.partner, f.page))
	return fixmes
Exemplo n.º 9
0
def collectfixmes(dw, ns, exceptions=[]):
    pages = dw.searchpages("FIXME")
    # print(pages)

    rx_namespace = re.compile(ns, re.IGNORECASE)
    rx_exceptions = [re.compile(ex, re.IGNORECASE) for ex in exceptions]

    pageids = [(dw.resolve(p['id']), p['score']) for p in pages]
    fixmes = []

    for pid, hits in pageids:
        if not rx_namespace.match(pid):
            continue

        skip = False

        for ex in rx_exceptions:
            if ex.match(pid) is not None:
                skip = True
                break

        if skip:
            continue

        doc = dw.getpage(pid)
        text = '\n'.join(doc)

        # print("Page: %s (%d)" % (pid, hits))
        matches = FixMe.rx_syntax.finditer(text)

        numfixmes = 0
        for f in matches:
            # print(f.group())
            numfixmes += 1
            partner = f.group(1)
            todo = f.group(4)
            deadline = f.group(3)

            # print("%s - %s\n%s" % (partner, deadline, todo))
            # print("%s  --  %s" % (deadline, date.parse(deadline)))

            fixmes.append(FixMe(partner, todo, deadline, pid))

        if numfixmes != hits:
            logging.warning("Invalid FIXME syntax on page %s" % pid)

    fixmes.sort(key=lambda f: (date.parse(f.deadline), f.partner, f.page))
    return fixmes
Exemplo n.º 10
0
 def test_split(self):
     self.assertEqual(parse("1/2/3"), "2001-02-03")
     self.assertEqual(parse("3/20/1"), "2001-03-20")
     self.assertEqual(parse("2010/5/2"), "2010-02-05")
     self.assertEqual(parse("10/99/2"), "2099-02-10")
     self.assertEqual(parse("2010/99/2"), "is illegal")
     self.assertEqual(parse("2010/5/2000"), "is illegal")
     self.assertEqual(parse("2019/2/29"), "is illegal")
     self.assertEqual(parse("2020/2/29"), "2020-02-29")
     self.assertEqual(parse("19/2/29"), "2029-02-19")
     self.assertEqual(parse("20/2/29"), "2020-02-29")
     self.assertEqual(parse("0/1/2"), "2000-01-02")
     self.assertEqual(parse("0/0/2"), "is illegal")
Exemplo n.º 11
0
 def present(self, meta):
     self.v.visit(meta)
     self.experiments = [(exp.get_date(), exp.get_site(),
                          exp.get_scenario().get_name())
                         for exp in self.v.get_result()]
     self.experiments.sort(key=lambda tup: (date.parse(tup[0]), tup[2]))
	def present(self, meta):
		self.v.visit(meta)
		self.experiments = [(exp.get_date(), exp.get_site(), exp.get_scenario().get_name()) for exp in self.v.get_result()]
		self.experiments.sort(key = lambda tup: (date.parse(tup[0]), tup[2]))