Exemple #1
0
def getMoreData(pageNum):
    temp = page2
    temp += str(pageNum)
    try:
        html_text = requests.get(temp).text
        soup = BeautifulSoup(html_text, 'html.parser')
        recipes = soup.findAll("a", {"class": "tout__titleLink"})
        for recipe in recipes:
            link = root + recipe.attrs['href']
            results = HTMLParser.fetchAndParseHTML(link)
            ingredients = HTMLParser.get_ingredients(results['ingredients'])
            steps = InstructionParser.parseToolsAndCookingMethod(
                results, replaceEmptyMainMethod=False)
            for ing in ingredients:
                if ing['name'].lower() in meats or ing['name'].lower(
                ) in seafood:
                    Italian['protein'][ing['name'].lower(
                    )] = Italian['protein'].get(ing['name'].lower(), 0) + 1
                elif ing['name'].lower() in vegetables:
                    Italian['vegetables'][ing['name'].lower(
                    )] = Italian['vegetables'].get(ing['name'].lower(), 0) + 1
                elif ing['name'].lower() in herbs_spices:
                    Italian['spices'][ing['name'].lower(
                    )] = Italian['spices'].get(ing['name'].lower(), 0) + 1
            Italian['cooking methods'][steps['main_cooking_method'].lower(
            )] = Italian['cooking methods'].get(
                steps['main_cooking_method'].lower(), 0) + 1
    except AttributeError:
        print("Error Fetching Page Information")
Exemple #2
0
def runRoutesOverDays(s, dates, routesFile):
    messageLimit = 10
    warningLimit = 10

    rawInstructions = Serialization.loadInstructionsFromFile(routesFile)
    #    if Array.length parseWarnings > 0 then
    #        printfn "%d instruction parse warnings" (Array.length parseWarnings)
    routes = InstructionParser.ConvertRawInstructions(s.projection,
                                                      rawInstructions)
    flightCountsAndCosts = {}

    for date in dates:
        #        weather = s.weather[date]
        flights = s.flights[date]
        airports = s.airports  #.[date]
        airspace = s.airspace[date]

        print "Simulating " + str(date)
        #        airports = None
        #        print routes
        #        raw_input('routes')
        weather = None
        results = Simulator.simulateFlights(s.simulationParameters, airports,
                                            airspace, weather, flights, routes)
        # return a map of FlightId to
        #   print results
        #   raw_input('next')
        #        countArrived = [ x.ReachedDestination for x in results]
        #        cost = [x.Cost for x in results]
        flightCosts = {}
        cost = []
        countArrived = 0
        for k, v in results.iteritems():
            flightCosts[k] = v['CostDetail']
            countArrived += 1 if v['ReachedDestination'] else 0
            cost.append(v['Cost'])
        print 'countArrived'
        print countArrived
        print 'cost'
        print cost
        flightCountsAndCosts[date] = {
            'date': date,
            'flightCount': countArrived,
            'meanCost': sum(cost) / len(cost),
            #flightLogs = logs
            'flightCosts': flightCosts
        }
    return flightCountsAndCosts
Exemple #3
0
def RecipeParser(url, toVegetarian, toHealthy, toItalian):
    results = HTMLParser.fetchAndParseHTML(url)
    if len(results) == 0:
        print("Please Provide a valid Recipe URL")
        sys.exit()
    steps = {}
    if toItalian:
        steps = Transformation.toItalian(results)
    else:
        steps = InstructionParser.parseToolsAndCookingMethod(results)
    if toVegetarian:
        steps = Transformation.toVeggie(steps)
    else:
        steps = Transformation.fromVeggie(steps)
    if toHealthy:
        steps['ingredients'] = HTMLParser.to_healthy(steps['ingredients'])
    else:
        steps['ingredients'] = HTMLParser.to_unhealthy(steps['ingredients'])
    print("This Recipe Parse will transform", "\"" + results['name'] + "\"",
          "to:", "Italian" if toItalian else "",
          "Vegetarian" if toVegetarian else "Non-Vegetarian", "and",
          "Healthy" if toHealthy else "Unhealthy")
    res = HTMLParser.format_ings(steps['ingredients'])
    print("The Ingredients are: ")
    for i in res:
        print(i)
    print()
    print("The Tools are:")
    for t in steps['tools']:
        print(t)
    print()
    print("The Primary Cooking Method is:")
    print(steps['main_cooking_method'])
    print()
    print("The Secondary Cooking Methods Are:")
    print(steps['secondary_cooking_methods'])
    print()
    print("The Steps Are:")
    counter = 1

    for s in steps['steps']:
        print(str(counter) + ".", s['instruction'])
        counter += 1
Exemple #4
0
def parserUI(results):
    ingredients = HTMLParser.format_ings(
        HTMLParser.get_ingredients(results["ingredients"]))
    tools_instructions = InstructionParser.parseToolsAndCookingMethod(results)
    active = True
    first = True
    while active is True:
        if first:
            user = input(
                "I've found the recipe for \"" + results["name"] +
                "\".\nMAIN MENU:\n\n[1] look at the ingredients\n[2] look at the steps\n[3] enter another recipe\n[4] quit the conversation?\n\n"
            )
        else:
            user = input(
                "I'm sorry, I couldn't process that input. Would you like to: [1] look at the ingredients, [2] look at the steps, [3] enter another recipe, or [4] quit the conversation? \n"
            )
        if user == '1':
            print_ing = "Here is the list of ingredients:\n"
            for ing in ingredients:
                print_ing += ing + "\n"
            print(print_ing)
            conversation(tools_instructions, ingredients)
            active = False
        elif user == '2':
            print("The first step is: " +
                  tools_instructions["steps"][0]["instruction"] + "\n")
            conversation(tools_instructions, ingredients)
            active = False
        elif user == '3':
            urlinput(False)
            active = False
        elif user == '4':
            print("Goodbye!")
            time.sleep(2)
            sys.exit(0)
        else:
            first = False
Exemple #5
0
    except AttributeError:
        print("Error Fetching Page Information")


try:
    for i in range(2, 6):
        getMoreData(2)
    html_text = requests.get(url).text
    soup = BeautifulSoup(html_text, 'html.parser')

    recipes = soup.findAll("div", {"class": "card__detailsContainer"})
    for recipe in recipes:
        link = recipe.findChildren("a", recursive=True)[0].attrs['href']
        results = HTMLParser.fetchAndParseHTML(link)
        ingredients = HTMLParser.get_ingredients(results['ingredients'])
        steps = InstructionParser.parseToolsAndCookingMethod(
            results, replaceEmptyMainMethod=False)
        for ing in ingredients:
            if ing['name'].lower() in meats or ing['name'].lower() in seafood:
                Italian['protein'][ing['name'].lower(
                )] = Italian['protein'].get(ing['name'].lower(), 0) + 1
            elif ing['name'].lower() in vegetables:
                Italian['vegetables'][ing['name'].lower(
                )] = Italian['vegetables'].get(ing['name'].lower(), 0) + 1
            elif ing['name'].lower() in herbs_spices:
                Italian['spices'][ing['name'].lower()] = Italian['spices'].get(
                    ing['name'].lower(), 0) + 1
        Italian['cooking methods'][steps['main_cooking_method'].lower(
        )] = Italian['cooking methods'].get(
            steps['main_cooking_method'].lower(), 0) + 1
    print(Italian)
    with open('Italian_Ingredients.json', 'w') as f:
Exemple #6
0
def toItalian(url):
    if not os.path.exists('Italian_Ingredients.json'):
        print(
            "Run StyleOfCuisine.py to generate italian cuisine ingredients first"
        )
        sys.exit()
    with open('Italian_Ingredients.json') as f:
        italian = json.load(f)
    steps = InstructionParser.parseToolsAndCookingMethod(url)
    new_ingredients = []
    protein_subs = list(italian['protein'].keys())
    vegetable_subs = list(italian['vegetables'].keys())
    spice_subs = list(italian['spices'].keys())

    rep_map = {}
    for ing in steps['ingredients']:
        for token in ing['name'].lower().split():
            if token in meats or token in seafood and ing[
                    'name'] not in protein_subs:
                rep = random.choice(protein_subs)
                rep_map[ing['name']] = rep
                if rep in [i['name'] for i in new_ingredients]:
                    for i in new_ingredients:
                        if i['name'] == rep:
                            i['quantity'] += ing['quantity']
                        break
                else:
                    ing['name'] = rep
                protein_subs.remove(rep)
                break
            elif token in vegetables and ing['name'] not in vegetable_subs:
                rep = random.choice(vegetable_subs)
                rep_map[ing['name']] = rep
                if rep in [i['name'] for i in new_ingredients]:
                    for i in new_ingredients:
                        if i['name'] == rep:
                            i['quantity'] += ing['quantity']
                        break
                else:
                    ing['name'] = rep
                vegetable_subs.remove(rep)
                break
            elif token in herbs_spices and ing['name'] not in spice_subs:
                rep = random.choice(spice_subs)
                rep_map[ing['name']] = rep
                if rep in [i['name'] for i in new_ingredients]:
                    for i in new_ingredients:
                        if i['name'] == rep:
                            i['quantity'] += ing['quantity']
                        break
                else:
                    ing['name'] = rep
                spice_subs.remove(rep)
                break
        new_ingredients.append(ing)

    method_subs = list(italian['cooking methods'].keys())
    method_subs = method_subs[1:]

    for s in steps['steps']:
        for i in s['ingredients'].keys():
            if s['ingredients'][i] in rep_map.keys():
                s['instruction'] = s['instruction'].replace(
                    i, rep_map[s['ingredients'][i]])
        new_actions = []
        for a in s['action']:
            if a not in method_subs:
                rep = rep_map.get(a, random.choice(method_subs))
                rep_map[a] = rep
                new_actions.append(rep)
                ins = nlp(s['instruction'])
                for token in ins:
                    if token.lemma_ == a:
                        suffix = ''
                        if token.text.endswith('ing'):
                            suffix = 'ing'
                        elif token.text.endswith('ed'):
                            suffix = 'ed'
                        if suffix != '':
                            if rep[-2] in ['a', 'e', 'i', 'o', 'u']:
                                rep += rep[-1]
                            elif rep[-1] == 'e':
                                rep = rep[:-1]
                            elif rep == 'fry' and suffix == 'ed':
                                rep = 'fri'
                        s['instruction'] = s['instruction'].replace(
                            token.text, rep + suffix)
            else:
                new_actions.append(a)
        s['action'] = new_actions

    steps['main_cooking_method'] = rep_map.get(steps['main_cooking_method'],
                                               steps['main_cooking_method'])
    steps['ingredients'] = new_ingredients

    return steps
Exemple #7
0
airports = Serialization.loadAirportsFromFile(airportsFile) #, landingFile, taxiFile)

#    timeStampPrint sw "Loading date-specific info "
(flightsDates, airspaceDates) =  Serialization.loadDateMaps(
     flightPattern, restrictedAirspace, turbulentZonesPattern, dates)

#    // config contains references to pre-loaded objects/maps
configL = config.Config(projection, airports,
                        flightsDates, airspaceDates, simulationParameters)

submission = config.settings['submissions']
print submission
routesFile = basePath + '/' + submission
rawInstructions = Serialization.loadInstructionsFromFile(routesFile)
# rawInstructions is a dict
routes = InstructionParser.ConvertRawInstructions(configL.projection, rawInstructions)
weather = None
airports = configL.airports 
airportEnvironment = None
fullCoreFunctions = SimulationTypes.SimulationCoreFunctions(
    FuelModel.fuelBurn,
    WeightModel.grossWeight,
    AirSpeedLimiter.limitAirSpeed,
    AirSpeedLimiter.limitAltitude,
    Arrival.arrivalModel)

output_file = basePath + "USTC9600_final_submission.csv'
with open(output_file, "w") as f:
    f.write("FlightId,Ordinal,Latitude,Longitude,Altitude,AirSpeed\n")

count_miss =0
Exemple #8
0
def main():

    # Hard-coded table holding clock frequency information for HW
    frequencyTable = {
        5: 193.723,
        6: 257.931,
        7: 266.241,
        8: 311.818,
        9: 405.844
    }

    # Create option parser
    parser = OptionParser(
        usage="usage: run-simulator.py [-hvcqmpfdews] filename", version="1.0")

    # Add options (self-documenting)
    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="log cycle by cycle debug information")
    parser.add_option("-c",
                      "--core",
                      action="store_true",
                      dest="core",
                      default=False,
                      help="show only core cycles")
    parser.add_option("-q",
                      "--quiet",
                      action="store_true",
                      dest="quiet",
                      default=False,
                      help="less terminal output")
    parser.add_option("-m",
                      "--mute",
                      action="store_true",
                      dest="mute",
                      default=False,
                      help="no summary output")
    parser.add_option(
        "-p",  # "--pipeline",
        type="int",
        dest="pipeline",
        default=5,
        help="set number of pipeline stages [default 5]")
    parser.add_option(
        "-f",  # "--IFStages",
        type="int",
        dest="ifcycles",
        default=-1,
        help="set number of Fetch (IF) Stage cycles")
    parser.add_option(
        "-d",  # "--IDStages",
        type="int",
        dest="idcycles",
        default=-1,
        help="set number of Decode (ID) Stage cycles")
    parser.add_option(
        "-e",  # "--EXStages",
        type="int",
        dest="excycles",
        default=-1,
        help="set number of Execution (EX) Stage cycles")
    parser.add_option(
        "-w",  # "--WBStages",
        type="int",
        dest="wbcycles",
        default=-1,
        help="set number of Writeback (WB) Stage cycles")
    parser.add_option(
        "-s",  # "--WBStages",
        type="int",
        dest="startAddr",
        default=0x0,
        help="set execution start address")

    (options, args) = parser.parse_args()

    # For automated coloured testing output
    B = bcolors()

    # Integer conversion
    options.pipeline = int(options.pipeline)
    options.ifcycles = int(options.ifcycles)
    options.idcycles = int(options.idcycles)
    options.excycles = int(options.excycles)
    options.wbcycles = int(options.wbcycles)

    pipelineConfigError = False

    # Pipeline checking
    if (options.pipeline < 4):
        pipelineConfigError = True

    # Use 1 ID and WB Cycle by default
    if (options.idcycles == -1):
        options.idcycles = 1
    if (options.wbcycles == -1):
        options.wbcycles = 1
    # Use maximum number of EX/MEM Cycles by default (ugly code...)
    if (options.excycles == -1 and options.pipeline >= 5):
        if (options.pipeline == 5):
            options.excycles = 2
        elif (options.pipeline == 6):
            options.excycles = 3
        else:
            options.excycles = 4
    # The rest of the instructions will be WB
    if (options.ifcycles == -1):
        remCycles = options.pipeline - options.idcycles - options.excycles - options.wbcycles
        if (remCycles < 1):
            pipelineConfigError = True
        options.ifcycles = remCycles

    # Double check for correct pipeline configuration
    pipelineSum = options.ifcycles + options.idcycles + options.excycles + options.wbcycles
    if (pipelineSum != options.pipeline):
        pipelineConfigError = True

    # Give error if something is wrong
    if (pipelineConfigError):
        B.printError("Error: Incorrect pipeline configuration")
        sys.exit()

    inputFile = None

    # Open the input file
    try:
        inputFile = open(args[0], "r")
    except IOError:
        B.printError("Error: Could not open input file\t" + args[0])
        sys.exit()

    # Default values
    defaultSimASMFile = "simasm.sim"
    defaultDataMemFile = "datamem.sim"
    defaultPreProcLogFile = "preprocLog.sim"
    defaultSimRunFile = "simrun.sim"

    oldstdout = sys.stdout

    # Initialize parsers
    iparser = InstructionParser.InstructionParser()
    eparser = elf32parser.elf32parser()

    # If custom simulation assembly file output is set
    SimAsmFileName = args[2] if len(args) >= 3 else defaultSimASMFile
    SimAsmFile = open(SimAsmFileName, 'w')
    sys.stdout = SimAsmFile

    # If custom data memory file output is set
    DataMemFileName = args[4] if len(args) >= 5 else defaultDataMemFile

    if (not options.quiet):
        oldstdout.write("> Starting Parser...\n")

    # Convert elf32-bigmips to simulator friendly format
    eparser.convertToSimASM(args[0], SimAsmFileName, DataMemFileName)

    # Extract statistics
    lines = eparser.getLines()
    datamem = eparser.getDataMem()
    mainAddr = eparser.getMainAddr()
    coreInstr = eparser.getCCoreInstr()

    # IF custom preprocessing log file name is set
    PPLogFileName = args[3] if len(args) >= 4 else defaultPreProcLogFile
    PPLogFile = open(PPLogFileName, 'w')
    sys.stdout = PPLogFile

    if (not options.quiet):
        oldstdout.write("> Starting Assembler...\n")

    # Parse in lines, do preprocessing and check for dependencies
    lines = iparser.parseLines(lines, (options.pipeline - options.ifcycles),
                               options.ifcycles, coreInstr)

    pipelineInfo = [
        options.pipeline, options.ifcycles, options.idcycles, options.excycles,
        options.wbcycles
    ]

    # Initialize simulator
    pipelinesim = PipelineSimulator.PipelineSimulator(
        lines, datamem, options.startAddr, oldstdout, options.verbose,
        options.quiet, pipelineInfo)

    if (not options.quiet):
        print "> Starting Simulation..."

    startTime = time.clock()

    # Set logfile
    simulationFileName = args[1] if len(args) >= 2 else defaultSimRunFile
    simulationFile = open(simulationFileName, 'w')
    sys.stdout = simulationFile

    # Run simulation
    pipelinesim.run()

    elapsedTime = (time.clock() - startTime)

    if (not options.quiet):
        oldstdout.write("\n> Simulation Completed in ")
        oldstdout.write(str(elapsedTime))
        oldstdout.write(" s")

    # Close output files
    simulationFile.close()
    PPLogFile.close()

    sys.stdout = oldstdout
    checker = Checker.Checker(simulationFileName, options.quiet)
    success = False

    # Give output according to the settings

    # Normal terminal-based single run output
    if (not options.quiet):
        checker.runCheck()

    # Only summary and statistics output (for use in automated scripts)
    elif (not options.mute):
        success = checker.runCheck()
        if (success):
            if (options.core):
                B.printCoreOnly(checker.getCoreCycles())
            else:
                # Compile statistics
                pNOPs = str(
                    round(
                        float(
                            str(
                                float(checker.getCoreNops()) /
                                (float(checker.getCoreCycles())))) * 100, 1))
                c = checker.getCycles()
                n = checker.getNOPs()
                cpi = checker.getCPI()
                cc = checker.getCoreCycles()
                cn = checker.getCoreNops()
                ex = str(
                    round(
                        float(
                            int(c) *
                            float(1 / frequencyTable.get(options.pipeline))),
                        8)) + "us"
                cex = str(
                    round(
                        float(
                            int(cc) *
                            float(1 / frequencyTable.get(options.pipeline))),
                        8)) + "us"
                # Print successful test
                B.printPass(args[0], [c, n, cpi, cc, cn, pNOPs, ex, cex])
        else:
            # Indicate failure
            B.printFail(args[0], "")
 def setServerSpecs(self):        
     self.serverRam, self.maxData = InstructionParser.getServerDetails(RESOURCE_DIR + "\\" + CONFIG_FILE)
 def getInstructionSet(self):
     return InstructionParser.getInstructionSet(self)