def place_queen(N, queens_positions, current_row): if current_row == N: solution = str(queens_positions).strip('[]') solution = solution.replace(' ', '') if not solution_exists(N): global first_time first_time = True queen_case = QueenCase(n=N, number_of_solutions=0) session.add(queen_case) session.commit() if first_time: query = session.query(QueenCase).filter_by(n=N) for x in query: queen_case_id = x.id sol_obj = Solution(queen_case_id=queen_case_id, solution=solution) session.add(sol_obj) session.commit() global sol sol += 1 else: for column in range(0, N): if verify(queens_positions, current_row, column): queens_positions[current_row] = column place_queen(N, queens_positions, current_row + 1)
def calculate(N): global sol, first_time sol = 0 first_time = False queens_positions = [-1] * N place_queen(N, queens_positions, 0) if first_time: queen_case = session.query(QueenCase).filter_by(n=N).first() queen_case.number_of_solutions = sol session.commit() elif not solution_exists(N): queen_case = QueenCase(n=N, number_of_solutions=sol) session.add(queen_case) session.commit() return sol
def getcountry(threeletter="PAK"): print threeletter baseurl = "http://api.worldbank.org/datafiles/%s_Country_MetaData_en_EXCEL.xls" value = {'dsID': 'World Bank', 'region': threeletter, 'source': baseurl % threeletter, 'is_number': True} fh = dl.grab(baseurl % threeletter, [404]) if not fh: return messy = messytables.excel.XLSTableSet(fh) table = xypath.Table.from_messy(list(messy.tables)[0]) indicators = table.filter(is_in(indicator_list)) indname = indicators.shift(x=-1) if not len(indname) == len(indicator_list): print "missing indicators", [x.value for x in indname] code = table.filter(equal_to('Indicator Code')) years = code.fill(xypath.RIGHT) junction = indname.junction(years) for ind_cell, year_cell, value_cell in junction: vdict = dict(value) vdict['indID'] = ind_cell.value vdict['period'] = year_cell.value vdict['value'] = value_cell.value indicator = {'indID': vdict['indID']} nameunits = re.search('(.*)\((.*)\)', vdict['indID']) if nameunits: (indicator['name'], indicator['units']) = nameunits.groups() else: indicator['name'] = vdict['indID'] indicator['units'] = 'uno' Indicator(**indicator).save() v = Value(**vdict) if not v.is_blank(): v.save() print len(session.query(Value).filter(Value.dsID == 'World Bank').all()) session.commit()
for x in Entity.query: c = x.children["owner"] while c: c.pop() c = x.children["user"] while c: c.pop() if not x.name in data: session.delete(x) for name, entity in owners.items(): nodes = names.get(name, []) if nodes: c = entity.children["owner"] for node in nodes: c.append(node) else: print "Node for '%s' not found" % name for name, entities in users.items(): nodes = names.get(name, []) if nodes: for entity in entities: c = entity.children["user"] for node in nodes: c.append(node) else: print "Node for '%s' not found" % name session.commit()
def create_product(cls): name = input("Ingrese el nombre del producto: ") price = int(input("Ingrese el precio: ")) product = Products(name, price) session.add(product) session.commit()
def update_user(cls): ob = session.query(Products).get(1) ob.nombre = "Cambio!!" session.commit()
def delete_user(cls): ob = session.query(Products).get(2) session.delete(ob) session.commit()
code = table.filter(equal_to('Indicator Code')) years = code.fill(xypath.RIGHT) junction = indname.junction(years) for ind_cell, year_cell, value_cell in junction: vdict = dict(value) vdict['indID'] = ind_cell.value vdict['period'] = year_cell.value vdict['value'] = value_cell.value indicator = {'indID': vdict['indID']} nameunits = re.search('(.*)\((.*)\)', vdict['indID']) if nameunits: (indicator['name'], indicator['units']) = nameunits.groups() else: indicator['name'] = vdict['indID'] indicator['units'] = 'uno' Indicator(**indicator).save() v = Value(**vdict) if not v.is_blank(): v.save() print len(session.query(Value).filter(Value.dsID == 'World Bank').all()) session.commit() for country in getcountrylist(): try: getcountry(country) except Exception, e: print country, e raise
def getindicator(ind="100106", overridefunction=None): if not overridefunction: baseurl = 'http://hdrstats.undp.org/en/indicators/display_cf_xls_indicator.cfm?indicator_id=%s&lang=en' % ind html = requests.get(baseurl).content else: html, baseurl = overridefunction() value = {'dsID': 'HDRStats', 'indID': "HDR:"+ind, 'source': baseurl, 'is_number': True} dataset = {'dsID': 'HDRStats', 'last_scraped': orm.now(), 'name': 'Human Development Indicators, UNDP'} indicator = {'indID': "HDR:"+ind} hdi_indicator = {'indID': 'HDR:HDI Rank', 'name': 'Human Development Index rank', 'units': ''} Indicator(**hdi_indicator).save() DataSet(**dataset).save() print html exit(3) htmlio = StringIO.StringIO(html) messy = messytables.html.HTMLTableSet(htmlio) table = xypath.Table.from_messy(list(messy.tables)[0]) root = lxml.html.fromstring(html) "get odd indicator / update time" indicator_text = root.xpath("//h2/text()")[-1] print indicator_text try: indicator_split, = re.findall("(.*)\(([^\(\)]+)\)", indicator_text) except ValueError: indicator_split = [indicator_text, ""] indicator['name'], indicator['units'] = indicator_split indicator['name'] = indicator['name'].strip() access_text, = [x.tail.strip() for x in root.xpath("//br") if str(x.tail) != "None" and x.tail.strip()] access_date_raw, = re.findall('Accessed:(.*)from', access_text) dataset['last_updated'] = dateutil.parser.parse(access_date_raw).isoformat() print dataset['last_updated'], indicator['name'], "*", indicator['units'] Indicator(**indicator).save() country_cell = table.filter("Country").assert_one() years = country_cell.fill(xypath.RIGHT).filter(lambda b: b.value != '') countries = country_cell.fill(xypath.DOWN) hdi_rank = table.filter("HDI Rank").assert_one() max_year = max(year.value for year in years) for i in countries.junction(hdi_rank): newvalue = dict(value) newvalue['indID'] = "HDR:HDI Rank" newvalue['region'] = get_region(i[0]) newvalue['value'] = i[2].value.strip() newvalue['period'] = 2012 # TODO Hard coded for now because year it pertains to is not clear if newvalue['value'].strip() != '..': Value(**newvalue).save() for i in countries.junction(years): newvalue = dict(value) newvalue['region'] = get_region(i[0]) newvalue['value'] = i[2].value.strip() newvalue['period'] =i[1].value.strip() if newvalue['value'].strip() != '..': Value(**newvalue).save() print newvalue session.commit()
def getindicator(ind="100106", overridefunction=None): if not overridefunction: baseurl = 'http://hdrstats.undp.org/en/indicators/display_cf_xls_indicator.cfm?indicator_id=%s&lang=en' % ind html = requests.get(baseurl).content else: html, baseurl = overridefunction() value = { 'dsID': 'HDRStats', 'indID': "HDR:" + ind, 'source': baseurl, 'is_number': True } dataset = { 'dsID': 'HDRStats', 'last_scraped': orm.now(), 'name': 'Human Development Indicators, UNDP' } indicator = {'indID': "HDR:" + ind} hdi_indicator = { 'indID': 'HDR:HDI Rank', 'name': 'Human Development Index rank', 'units': '' } Indicator(**hdi_indicator).save() DataSet(**dataset).save() print html exit(3) htmlio = StringIO.StringIO(html) messy = messytables.html.HTMLTableSet(htmlio) table = xypath.Table.from_messy(list(messy.tables)[0]) root = lxml.html.fromstring(html) "get odd indicator / update time" indicator_text = root.xpath("//h2/text()")[-1] print indicator_text try: indicator_split, = re.findall("(.*)\(([^\(\)]+)\)", indicator_text) except ValueError: indicator_split = [indicator_text, ""] indicator['name'], indicator['units'] = indicator_split indicator['name'] = indicator['name'].strip() access_text, = [ x.tail.strip() for x in root.xpath("//br") if str(x.tail) != "None" and x.tail.strip() ] access_date_raw, = re.findall('Accessed:(.*)from', access_text) dataset['last_updated'] = dateutil.parser.parse( access_date_raw).isoformat() print dataset['last_updated'], indicator['name'], "*", indicator['units'] Indicator(**indicator).save() country_cell = table.filter("Country").assert_one() years = country_cell.fill(xypath.RIGHT).filter(lambda b: b.value != '') countries = country_cell.fill(xypath.DOWN) hdi_rank = table.filter("HDI Rank").assert_one() max_year = max(year.value for year in years) for i in countries.junction(hdi_rank): newvalue = dict(value) newvalue['indID'] = "HDR:HDI Rank" newvalue['region'] = get_region(i[0]) newvalue['value'] = i[2].value.strip() newvalue[ 'period'] = 2012 # TODO Hard coded for now because year it pertains to is not clear if newvalue['value'].strip() != '..': Value(**newvalue).save() for i in countries.junction(years): newvalue = dict(value) newvalue['region'] = get_region(i[0]) newvalue['value'] = i[2].value.strip() newvalue['period'] = i[1].value.strip() if newvalue['value'].strip() != '..': Value(**newvalue).save() print newvalue session.commit()