Ejemplo n.º 1
0
    def fileload(path):
        """
        Given a path to a formatted csv, open csv, parse header, and return body as a panda.
        :param path: path to csv file
        :return:
        name -- name of investment account
        subname -- further categorization of investment account
        path -- path to file, stored for future use
        funds -- a list of strings of fund names
        alloc -- custom structure: a list of 2-tuples, first item is target percentage of total in this category,
        second is a set of fund names
        """
        with open(path, mode='r') as file:

            # read first line and extract name
            name = file.readline().strip()
            # read second line and extract subname
            subname = file.readline().strip()
            # read third line and extract list of funds
            funds = file.readline().strip().split(',')
            # read allocations
            percentage = 0.
            alloc = allocation.Allocation()
            while percentage < 1.0:
                line = file.readline().strip()  # remove \n at end!
                # parse line to get (itype, percent, fundlist)
                construction_parameters = allocation.AllocationEntry.parse_csv_str(
                    line)
                alloc.append(
                    allocation.AllocationEntry(*construction_parameters))
                percentage += construction_parameters[1]
            alloc.check_percentages(
            )  # raise Exception if percents do not total 1.0
            # read rest of file as a DataFrame
            temp = pd.read_csv(file, parse_dates=['date'])
            # column 'date' imported as datetime object
            # convert to date object
            temp['date'] = temp['date'].dt.date

        return (name, subname, path, funds, alloc, temp)
Ejemplo n.º 2
0
    def Allocate(self, depot, size, **kwargs):
        # Generate destination Allocation and Capabilities using the form below
        # IBPv031[0] IBP_ALLOCATE[1] reliability cap_type duration size timeout
        reliability = flags.IBP_HARD
        cap_type = flags.IBP_BYTEARRAY
        timeout = settings.DEFAULT_TIMEOUT
        duration = settings.DEFAULT_DURATION
        tmpCommand = ""

        if "reliability" in kwargs:
            reliability = kwargs["reliability"]
        if "type" in kwargs:
            cap_type = kwargs["type"]
        if "duration" in kwargs:
            duration = kwargs["duration"]
        if "timeout" in kwargs:
            timeout = kwargs["timeout"]

        try:
            tmpCommand = "{0} {1} {2} {3} {4} {5} {6} \n".format(
                flags.IBPv031, flags.IBP_ALLOCATE, reliability, cap_type,
                duration, size, timeout)
            result = self._dispatch_command(depot["host"], depot["port"],
                                            tmpCommand)
            result = result.split(" ")[1:]

        except Exception as exp:
            return None

        if result[0].startswith("-"):
            return None

        alloc = allocation.Allocation()
        alloc.SetReadCapability(result[0])
        alloc.SetWriteCapability(result[1])
        alloc.SetManageCapability(result[2])
        alloc.depotSize = size
        return alloc
Ejemplo n.º 3
0
 def test_wrong_unit(self):
     with self.assertRaises(ValueError):
         allocation.Allocation(1, 128, 'ab', 'mb')
     with self.assertRaises(ValueError):
         allocation.Allocation(1, 128, 'kb', 'ab')
Ejemplo n.º 4
0
 def initialize_standard(self):
     return allocation.Allocation(1, 128, 'mb', 'kb', 'first')
Ejemplo n.º 5
0
 def test_init(self):
     memory = allocation.Allocation(1, 128, 'mb', 'kb')
     self.assertEqual(memory.list_files(), {})
     self.assertEqual(memory.availability(),
                      '0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7')
Ejemplo n.º 6
0
 def test_init_one_block(self):
     memory = allocation.Allocation(1, 1, 'kb', 'kb')
     self.assertEqual(memory.list_files(), {})
     self.assertEqual(memory.availability(), '0')
Ejemplo n.º 7
0
import investment_types
import investment
import allocation

v = investment.Account(file='../vanguard.csv')
td_i = investment.Account(file='../ameritrade_i.csv')
td_r = investment.Account(file='../ameritrade_r.csv')
td_b = investment.Account(file='../ameritrade_b.csv')
mit401 = investment.Account(file='../fidelity_401.csv')
juni = investment.Account(file='../fidelity_529.csv')
# hsa = investment.account(file='../healthhub.csv')
tiaa = investment.Account(file='../tiaa.csv')

my_alloc = allocation.Allocation([
    allocation.AllocationEntry(investment_types.Stock(), 0.25, []),
    allocation.AllocationEntry(investment_types.Bond(), 0.25, []),
    allocation.AllocationEntry(investment_types.StockInt(), 0.25, []),
    allocation.AllocationEntry(investment_types.RealEstate(), 0.25, []),
])

td = investment.Portfolio(accounts=[td_i, td_r, td_b], alloc=my_alloc)

kitchensink = investment.Portfolio(
    accounts=[td_i, td_r, td_b, v, mit401, tiaa], alloc=my_alloc)
Ejemplo n.º 8
0
    def run_cli(self):
        print()
        print('Welcome to the memory-allocation CLI!')
        print('Created by Charles Yu')
        print()

        size, unit = self.obtain_size_and_unit('drive capacity')
        block_size, block_unit = self.obtain_size_and_unit('block size')
        print('Please select whether first fit or best fit allocation is to be used. Enter one of "best" or "first": ')
        algorithm = input()
        print()

        memory = allocation.Allocation(size, block_size, unit, block_unit, algorithm)
        exit = False

        print()
        while not exit:
            print('Please select an option using the corresponding number: ')
            print('1) Save file')
            print('2) Read file')
            print('3) Delete file')
            print('4) List files and locations')
            print('5) View unallocated blocks')
            print('6) Exit')

            choice = int(input())

            print()
            if choice == 1:
                file_id = self.obtain_file_id()
                size, unit = self.obtain_size_and_unit('file size')
                print()
                try:
                    location = memory.save(file_id, size, unit)
                    print()
                    print('File located at ' + str(location))
                except ValueError as e:
                    print(e)
                print()
            elif choice == 2:
                file_id = self.obtain_file_id()
                print()
                try:
                    location = memory.read(file_id)
                    print()
                    print('File located at ' + str(location))
                except ValueError as e:
                    print(e)
                print()
            elif choice == 3:
                file_id = self.obtain_file_id()
                print()
                try:
                    memory.delete(file_id)
                    print()
                    print('Available Blocks: ' + memory.availability())
                    print()
                except ValueError as e:
                    print(e)
            elif choice == 4:
                print('File List: ')
                files = memory.list_files()
                if len(files) == 0:
                    print('No files saved')
                else:
                    for file_id, blocks in files.items():
                        print('{}: {}'.format(file_id, blocks))
                print()
            elif choice == 5:
                print('Unallocated Blocks: ' + memory.availability())
                print()
            elif choice == 6:
                exit = True
            else:
                print('Your choice was not recognized. Please try again.')

        print('Exiting CLI. Thank you!')
        print()