def parse_cfg(cfgfile):
    """
    Takes a configuration file

    Returns a list of blocks. Each block describes a layer (usually) in the neural
    network to be built. Block is represented as a dictionary in the list

    """
    file = open(cfgfile, 'r')

    # store the lines in a list
    lines = file.read().split('\n')
    # get rid of the empty lines
    lines = [x for x in lines if len(x) > 0]
    # get rid of comments
    lines = [x for x in lines if x[0] != '#']
    # get rid of fringe whitespaces
    lines = [x.rstrip().lstrip() for x in lines]

    # To parse the files, we're going to create a key/value pair, and then
    # store each pair in a Python list
    block = {}
    blocks = []

    # Now that we collapsed the config file, let's go through each line
    for line in lines:
        # This marks the start of a new block based on the CFG filename, i.e. [convolutional]
        if line[0] == "[":
            # If block is not empty, this means we're still building the previous block dict.
            # Since we're in an obviously new block with the "[", the first time through we'll
            # need to send the current block to the list, and empty the current block dictionary
            # so it can receive the next values. This is how we separate dict items for our list.
            if len(block) != 0:
                # Add the current block info to the list
                blocks.append(block)
                # Reset the block
                block = {}
            # Store in a key called 'type' the text of the line minus the brackets,
            # deleting any trailing characters. Since we cleared the block earlier the first time
            # through (denoted by the "["), the first layer of the block will always be a type
            # that is a text value of layer type
            block["type"] = line[1:-1].rstrip()
        else:
            # Since we're somewhere in the block where we're not figuring out layer type,
            # split the line pairs into a tuple of key and value using "=" as the break point
            key, value = line.split("=")

            # Converting the CFG key value pairs into block keys with values
            block[key.rstrip()] = value.lstrip()

    # This is so the last time through the loop the last block gets added
    blocks.append(block)

    return blocks