def find_hash(h): chars = string.lowercase + string.digits strings = itertools.product(chars, repeat=18) for a in strings: result = reduce(lambda x, y: x+y, list(a)) my = myhash.myhash(result) if my == h: return result
def getid(element): """ returns a hash code for the XML element and children to create a repeatable id for the element """ if element is None: print("getid error: null element") return -1 id = element.attrib.get('ID') if id and id.isnumeric() and len(list(element)) == 1: return int(id) else: text = ET.tostring(element) hashcode = myhash(text) if debug: print('key:', text, 'hash', hashcode) return hashcode
if opts.merge: files = [file(name) for name in args] count = {} else: # In this mode we don't assume anything about # the input. The output is sorted, just like # sort | uniq -c. count = {} files = [] for words in input: line = " ".join(words) if opts.key: key = " ".join(words[keystart:keystop]) else: key = line if opts.parallel and myhash.myhash(key, modulus) != residue: continue count[line] = count.get(line, 0) + 1 if len(count) >= opts.max_types: if opts.verbose: sys.stderr.write( "writing counts to temporary file (memory=%s)\n" % monitor.memory()) keys = count.keys() keys.sort() f = tempfile.TemporaryFile() for key in keys: f.write("%s\t%s\n" % (count[key], key)) f.seek(0) files.append(f) count = {}
def readreactions(tree, conn): """ read an xml file into the designated database """ start = time.time() insertcache = set() root = tree.getroot() sql = 'insert into reaxys.reaction (%s) values %s;' cur = conn.cursor() for elem in root.findall('REACTIONS/REACTION'): data = {} data['reaction_id'] = getid(elem) data['reaxys_reaction_id'] = elem.attrib.get('ID') for item in [ 'RXNSTRUCTURE', 'RANK', 'MW_LARGEST_PRODUCT', 'SORT_CREATION_DATE', 'SORT_REACTION_SCALE', 'SORT_TOTAL_VOLUME' ]: subelem = elem.find(item) if subelem is not None: tag = subelem.tag value = subelem.text data[tag] = value for item in ['REACTANT_ID', 'METABOLITE_ID', 'PRODUCT_ID']: subelem = elem.findall(item) if subelem: ilist = list() for sselem in subelem: # some of the data are MD5 hash strings, to be annoying # we will just make this a hash to be compatible as integer key # for this database text = sselem.text if text.startswith("MD5"): text = myhash(text.encode('utf-8')) text = int(text) ilist.append(text) data[item] = ilist for item in ['VARIATIONS']: subelem = elem.findall(item) if subelem: ilist = list() for sselem in subelem: tag = getid(sselem) ilist.append(tag) data[item] = ilist columns = data.keys() values = [data[column] for column in columns] cmd = cur.mogrify( sql, (AsIs(','.join(columns)), tuple(values))).decode('utf-8') + "\n" h = hash(cmd) if not h in lines: lines.add(h) insertcache.add(cmd) if len(insertcache) > CHUNKSIZE: cur.execute('\n'.join(insertcache)) insertcache.clear() cur.execute('\n'.join(insertcache)) conn.commit() print("\treadreactions load took %5.2f %6i records" % ((time.time() - start), len(lines))) return
def handle_net_command(who, command, line): "Returns False if the command had no impact on gamestate" global downloading global fast_forward global host_mode global screen_dirty args = line.split(' ') # Some commands don't require you to be seated if command == "say": out(who + ": " + line) return False if command == "raw": out(line) return False if command == "seats": seats[:] = [Seat(args[i], i) for i in range(len(args))] out("> %s set the /seats to: %s" % (who, line)) screen_dirty = True redraw() return if command == "callhash": out("> %s issued /callhash" % who) send("raw #%X for %s\n" % (myhash((board, tasks._pending)), localname)) #send("raw #%X for %s\n" % (myhash(tasks._pending), localname)) return False if command == "mk": #Obsolete, use /e pos = (int(args[1]), int(args[2])) team = int(args[3]) if len(args) > 3 else None return make_thing(pos, args[0], team) if command == "rm": #Obsolete, use /e pos = (int(args[0]), int(args[1])) obliterate_tile(pos) return if command == "e": pos = (int(args[0]), int(args[1])) cmd = args[2] if cmd == 'rm': obliterate_tile(pos) return team = int(args[3]) if len(args) > 3 else None return make_thing(pos, cmd, team) if command == "bias": # Sets a team's directional / handedness biases. # Useful during mapmaking to balance things, but it doesn't apply to already-placed units. team = int(args[0]) angle = int(args[1]) flip = int(args[2]) if angle < 0 or angle >= 6 or abs( flip) != 1 or team < 0 or team >= len(team_bias_angles): out("Bad \"bias\" command from " + who) else: team_bias_angles[team] = angle team_bias_flips[team] = flip return if command == "join": out("> %s has joined" % who) if host_mode: send("sync %s\n" % who) return False if command == "host": out("> %s is now the /host" % who) host_mode = (who == localname) return False if command == "dehost": out("> %s has /dehost'ed" % who) if who == localname: host_mode = False return False if command == "sync": out("> %s started /sync-ing to: %s" % (who, line)) if who == localname: global uploading uploading = True pause_net_input() global logfile logfile.close() logfile = gzip.open(log_filename) elif localname in args: downloading = True fast_forward = True pause_net_input() return False if command == "syncdone": if downloading: # Which you always should be in this case out("---------- SYNC COMPLETE ----------") downloading = False fast_forward = False resume_net_input() return False if command == "alliance": affectedTeam = int(args[0]) newCode = int(args[1]) oldCode = team_alliance[affectedTeam] team_alliance[affectedTeam] = newCode affected = [((team_alliance[t] & oldCode) == 0) != ((team_alliance[t] & newCode) == 0) for t in range(len(team_alliance))] affected[affectedTeam] = True #Handle idle people standing next to new enemies. for brow in board: for tile in brow: for content in tile.contents: #we are an ai'd actor who is on a team affected by this change if isinstance(content, Unit) and content.ai != None and affected[ content.team]: content.ai.queue_immediately() out("> %s set team %d to alliance code %d" % (who, int(args[0]), int(args[1]))) return for seat in seats: if seat.name == who: break else: out("Couldn't find seat for input %s:%s %s" % (who, command, line)) return False if command == "T": seat.time = int(line) try: requested_time = min(s.time for s in seats if actor_counts[s.team] != 0) except ValueError: # All seated teams are dead, nothing to do. requested_time = -1 if (requested_time < 0): redraw() return select(None) do_step(requested_time) for s in seats: s.time = -1 redraw() return if command == "do": args = [int(a) for a in args] src = (args[0], args[1]) dest = (args[2], args[3]) subject = get_selectable(src, seat.team) if subject == None: out("Couldn't find a selectable thing at %s" % str(src)) else: send_tile_command(subject, dest, len(args) == 5) if subject.pos == selected: select(subject) return if command == "team": seat.team = int(args[0]) seat.time = -1 redraw() return out("> %s issued unknown command %s" % (who, command)) return False
residue = int(residue) modulus = int(modulus) sys.stderr.write("I am %d of %d\n" % (residue, modulus)) # rotate args to reduce load on filesystem args = [args[(i + residue) % len(args)] for i in xrange(len(args))] input = fileinput.input(args) if opts.key is not None: cols = [int(x) for x in opts.key.split("-")] if len(cols) == 1: startfield, stopfield = cols[0] - 1, cols[0] elif len(cols) == 2: startfield, stopfield = cols[0] - 1, cols[1] else: parser.print_help() sys.exit(1) progress = 0 for line in input: if opts.key: key = " ".join(line.split()[startfield:stopfield]) else: key = line if myhash.myhash(key, modulus) == residue: outfile.write(line) progress += 1 if opts.verbose and progress % 1000000 == 0: sys.stderr.write("%d lines\n" % progress)
if opts.merge: files = [file(name) for name in args] count = {} else: # In this mode we don't assume anything about # the input. The output is sorted, just like # sort | uniq -c. count = {} files = [] for words in input: line = " ".join(words) if opts.key: key = " ".join(words[keystart:keystop]) else: key = line if opts.parallel and myhash.myhash(key,modulus) != residue: continue count[line] = count.get(line, 0)+1 if len(count) >= opts.max_types: if opts.verbose: sys.stderr.write("writing counts to temporary file (memory=%s)\n" % monitor.memory()) keys = count.keys() keys.sort() f = tempfile.TemporaryFile() for key in keys: f.write("%s\t%s\n" % (count[key], key)) f.seek(0) files.append(f) count = {} del keys
sys.stderr.write("I am %d of %d\n" % (residue, modulus)) # rotate args to reduce load on filesystem args = [args[(i+residue)%len(args)] for i in xrange(len(args))] input = fileinput.input(args) if opts.key is not None: cols = [int(x) for x in opts.key.split("-")] if len(cols) == 1: startfield, stopfield = cols[0]-1, cols[0] elif len(cols) == 2: startfield, stopfield = cols[0]-1, cols[1] else: parser.print_help() sys.exit(1) progress = 0 for line in input: if opts.key: key = " ".join(line.split()[startfield:stopfield]) else: key = line if myhash.myhash(key,modulus) == residue: outfile.write(line) progress += 1 if opts.verbose and progress % 1000000 == 0: sys.stderr.write("%d lines\n" % progress)