def hash_block_header(block): key = serializer.Serializer() key.write_4_bytes(block.version) key.write_hash(block.previous_block_hash) key.write_hash(block.merkle) key.write_4_bytes(block.timestamp) key.write_4_bytes(block.bits) key.write_4_bytes(block.nonce) return generate_sha256_hash(key.data)
def main(): serial_port = os.getenv('LIBTHING_PORT', '/dev/ttyAMA0') baud_rate = 9600 id_service = tags.DeploydTags('http://dev-laurip2.red-gate.com:2403/users') lib_service = books.DeploydBooks('http://10.120.200.158:2403/books') #lib_service = books.DeploydBooks('http://dev-laurip2.red-gate.com:2404/books') disp = dispatcher.Dispatcher(id_service, lib_service) ser = serializer.Serializer() serial = zigbee.ZigBee(serial_port, baud_rate, disp, ser) serial.run()
def convert(): """Main function""" input_file = None # must be set by command line argument lang = "" # else, use that from catalog! base_uri = "http://www.example.com" # may be overwritten by command line parameter output_folder = "output" # may be overwritten by command line parameter image_uri = "ignore" # uri path to images as specified in bmecat catalog - ignore ignores any images model_only = False # print model data only, i.e. hide/skip offering details pattern = "" # product uri pattern, any string containing %s is allowed, e.g. http://www.example.com/products/id_%s/ catalog = classes.Catalog() # global settings are stored in catalog object if entries[0].get(): input_file = entries[0].get() if entries[1].get(): base_uri = entries[1].get() if entries[2].get(): image_uri = entries[2].get() if entries[3].get(): output_folder = entries[3].get() if entries[4].get(): lang = entries[4].get() if entries[5].get() == "actual": catalog.typeOfProducts = entries[5].get() elif entries[5].get() == "placeholder": catalog.typeOfProducts = entries[5].get() elif entries[5].get() == "model": model_only = True elif entries[5].get(): print "WARNING: Could not interpret supplied product type -> %s" % entries[ 5].get() if entries[6].get() != None: pattern = entries[6].get() if not input_file: sys.stderr.write("No XML input file was provided") return -1 print "Conversion started... please wait" # parse and serialize on-the-fly serializerobject = serializer.Serializer(output_folder, base_uri, catalog, lang, image_uri, model_only, pattern) parserobject = parser.Parser(serializerobject) parserobject.parse( input_file, search="cataloggroup") # mappings between articles and catalog groups parserobject.parse(input_file, search="be") parserobject.parse(input_file, search="offer") specgen.create_html(output_folder) print "Conversion successfully finished"
def __init__(self, protocol, accept_params=None, daemon_port=None): """ Open a socket of the given type. A socket object represents one endpoint of a network connection. :param protocol: SOCK_RE or SOCK_RAW. :param accept_params: Accept params are relevant for cases where a socket is "born" out of an accept command :param daemon_port: The daemon localhost TCP Port. Use only if you use multiple YORE Daemons on the same computer (if you want to have multiple YO addresses on the same computer). """ # A Resocket must be initialized with a selected protocol (RE/Raw/etc.), # and can be initialized with "accept params". Accept params are relevant for # cases where a socket is "born" out of an accept command (just like real sockets). # See the server script for an example about accepted socket. self.main_lock = threading.RLock() self.daemon_port = daemon_port if daemon_port is not None else RESOCKET_DAEMON_DEFAULT_PORT self.__protocol = protocol # Create an IPC Serializer helper class. self.__serializer = serializer.Serializer() # Create our IPC socket and connect to the YORE ProtocolDaemon. self.__ipc = socket.socket(socket.AF_INET) self.__connect_to_daemon() # If the socket has accept parameters, initialize it with them. if accept_params: self.__yo_dest = accept_params[0] self.re_port = accept_params[1] self.__sid = accept_params[2] self.last_packet = accept_params[3] self.__blocking = accept_params[4] self.__timeout = accept_params[5] self.__state = STATE.CONNECTION_ESTABLISHED # Notify the ProtocolDaemon of the new socket existence. # This is necessary since the original server socket still exists, and # when a new connection is created, a new socket is allocated for it. # The ProtocolDaemon has to know it has a new socket which performs the actual session. self.__notify_accepted_socket() else: self.__yo_dest = None self.re_port = None self.__sid = None self.__state = STATE.NONE self.__set_protocol() self.__blocking = BLOCKING self.__timeout = TIMEOUT_DEFAULT
def play(location, option): model: tf.keras.Model = tf.keras.models.load_model(location) minimax = Minimax(model) logo.show() print('\n\n') side = None while side not in ['W', 'B']: side = input('Play as:\nWhite (W) | Black (B) | Random (R) > ') if side == 'R': side = 'W' if np.random.uniform() < 0.5 else 'B' side = side == 'W' szr = serializer.Serializer(None) if option == 'P': pgn = chess.pgn.read_game(io.StringIO(input('Paste PGN:\n> '))) for move in pgn.mainline_moves(): szr.board.push(move) while not szr.board.is_game_over(): logo.show() print('\n\n') display_board(szr.board, side) score = model.predict(np.expand_dims(szr.serialize(), 0)) print(f'ch0ss evaluation: {score}') if szr.board.turn == side: uci = None while not szr.board.is_legal(uci): if uci is not None: print('Illegal move') try: uci = chess.Move.from_uci(input('Type your move:\n > ')) except ValueError: print('Invalid move') uci = None if szr.board.is_legal(uci): szr.board.push(uci) else: print('Illegal move.') else: print('ch0ss is thinking...') move = minimax.search(szr.board, 3, side) szr.board.push(move[1])
def load_structure(): serializer_instance = serializer.Serializer() file_name = '' while True: file_name = input('Insert the pickle file name or (q)uit: ') if file_name == 'q': break try: loaded_structures.append( serializer_instance.deserialize(file_name) ) except OSError: print('Error: could not open file.')
def test(location): """ Evaluates a given FEN using the model at `location`. """ model = tf.keras.models.load_model(location) while True: logo.show() print('\n\n') fen = input('Paste FEN\n').split()[0] szr = serializer.Serializer(None) bitboard = szr.serialize_fen(fen) value = model.predict(np.expand_dims(bitboard, 0)) print(szr.board.unicode()) print(f'ch0ss evaluation: {value}\n\n\n') input('Press anything to continue...')
def process(data, split=0.2): """ Shuffles, splits and ensures the shape of X and y is correct. """ print('Serializing moves...') fens = list(data.keys()) results = list(data.values()) n = len(fens) random = np.arange(n) np.random.shuffle(random) szr = serializer.Serializer(None) X = np.array(list(map(szr.serialize_fen, fens)), dtype=np.int8).reshape((n, 6, 8, 8))[ random] y = np.array(list(map(np.mean, results)), dtype=np.float)[random] assert(len(X) == len(y)) # split into train, test cutoff = int(n * split) return X[cutoff:], X[:cutoff], y[cutoff:], y[:cutoff]
def launchSas(*args): """ Create and run the Single Algorithm Scheme synchronously by the Workflow Designer """ tempSerializer = serializer.Serializer() argumentCount = len(args) if not 2 <= argumentCount <= 3: raise RuntimeError('expected 2 or 3 arguments') algorithmName = args[0] outputString = unicode('') if 2 == argumentCount: outputString = tempSerializer.createTmpFileAndGetItsPath() elif 3 == argumentCount: outputString = serializer.getUnixAbsPath(args[2]) inputString = tempSerializer.bioListToString(args[1], True) resultList = u2py_internals.Scheme.launchSas(unicode(algorithmName), inputString, outputString) if 2 == argumentCount: resultList = tempSerializer.stringListToBioList(resultList) tempSerializer.cleanUp() return resultList
def parse_dataset(location, max_n=None): """ Parses each move from every game in the dataset as an observation / example. Each observation is a `6⨯8⨯8` `np.array` as dictated in `serializer.py`. Args: `location`: Dataset file location. `max_n`: Maximum number of observations. Returns: `X_train`: `6⨯8⨯8` training design matrix `y_train`: `n⨯1` training response `X_test`: `6⨯8⨯8` test design matrix `y_test`: `n⨯1` test response `g`: number of games in dataset """ data = defaultdict(list) # We only want to include wins. # https://arxiv.org/pdf/1711.09667.pdf shows there's to advantage # to including draws in the training set. result_map = {'1-0': 1, '0-1': 0} g = 0 # Game number n = 0 # Observation number # PGN's are dispersed over many lines; and seperated by a newline, and so we must # concatenate the lines until a newline is observered. current_pgn = '' with open(location, 'r') as raw_data: for pgn_line in raw_data: pgn_line = pgn_line.strip() if not pgn_line and current_pgn: game = chess.pgn.read_game(io.StringIO(current_pgn)) # Apparently there's a special token "*" which indicates # an unknown or otherwise unavailable result. I found this # out the hard way :) if game.headers['Result'] not in result_map: current_pgn = '' continue result = result_map[game.headers['Result']] g += 1 # Erase previous 2 command-line entries print('\x1b[1A\x1b[2K'*2) print(f'Processing game {g}. Total observations: {n}') # Serialize each move in the game szr = serializer.Serializer(None) for move in game.mainline_moves(): if max_n is not None and n >= max_n: X_train, X_test, y_train, y_test = process(data) return X_train, X_test, y_train, y_test, g n += 1 szr.board.push(move) data[szr.board.fen().split()[0]].append(result) current_pgn = '' else: current_pgn += f' {pgn_line}' X_train, X_test, y_train, y_test = process(data) return X_train, X_test, y_train, y_test, g
class Scheme(u2py_internals.Scheme): def __init__(self, *args): argCount = len(args) if 1 == argCount: schemeFilePath = serializer.getUnixAbsPath(args[0]) super(Scheme, self).__init__(unicode(schemeFilePath)) elif 1 < argCount: algorithmName = args[0] inputData = self._serializer.bioListToString(args[1], True) outputData = self._serializer.createTmpFileAndGetItsPath() if 3 == argCount: outputData = serializer.getUnixAbsPath(args[2]) elif 3 < argCount: raise TypeError('expected 3 or less arguments') super(Scheme, self).__init__(unicode(algorithmName), unicode(inputData), unicode(outputData)) else: super(Scheme, self).__init__() def addReader(self, readerName, inputData): """ Add to the computational scheme a new read element of the supplied type, set its input data and get the element's name """ inputString = self._serializer.bioListToString(inputData, True) return super(Scheme, self).addReader(unicode(readerName), inputString) def addWriter(self, writerName, outputPath): """ Add to the computational scheme a new write element of the supplied type, set its output file path and get the element's name """ absPath = serializer.getUnixAbsPath(outputPath) return super(Scheme, self).addWriter(unicode(writerName), absPath) def setElementAttribute(self, elementName, attributeName, attributeValue): """" Specify the given attribute of the supplied element """ treatStringsAsPaths = True if not ('url-in' == attributeName or 'url-out' == attributeName): treatStringsAsPaths = False stringValue = self._serializer.bioListToString(attributeValue, treatStringsAsPaths) super(Scheme, self).setElementAttribute(unicode(elementName), unicode(attributeName), stringValue) def getElementAttribute(self, elementName, attributeName): """" Get the value of the given element's attribute """ multivalueAttributeDelimeter = ';' attributeValue = super(Scheme, self).getElementAttribute( unicode(elementName), unicode(attributeName)) if multivalueAttributeDelimeter in attributeValue: valuesList = attributeValue.split(multivalueAttributeDelimeter) for index, value in enumerate(valuesList): if value in self._serializer.createdFiles: valuesList[index] = self._serializer.createdFiles[value] return valuesList elif attributeValue in self._serializer.createdFiles: return self._serializer.createdFiles[attributeValue] else: return attributeValue def launch(self): """ Run the scheme synchronously by the Workflow Designer """ resultList = super(Scheme, self).launch() return self._serializer.stringListToBioList(resultList) @staticmethod def launchSas(*args): """ Create and run the Single Algorithm Scheme synchronously by the Workflow Designer """ tempSerializer = serializer.Serializer() argumentCount = len(args) if not 2 <= argumentCount <= 3: raise RuntimeError('expected 2 or 3 arguments') algorithmName = args[0] outputString = unicode('') if 2 == argumentCount: outputString = tempSerializer.createTmpFileAndGetItsPath() elif 3 == argumentCount: outputString = serializer.getUnixAbsPath(args[2]) inputString = tempSerializer.bioListToString(args[1], True) resultList = u2py_internals.Scheme.launchSas(unicode(algorithmName), inputString, outputString) if 2 == argumentCount: resultList = tempSerializer.stringListToBioList(resultList) tempSerializer.cleanUp() return resultList def cleanUp(self): """ Removes all temporary files created during the scheme execution """ self._serializer.cleanUp() _serializer = serializer.Serializer()
import time import os import math if os.name == "posix": portName = "/dev/ttyUSB0" # Change this to your main Serializer port! #portName = "/dev/rfcomm0" # For bluetooth on Linux # Note: On Linux, after connecting to the Bluetooth adapter, run the command # sudo rfcomm bind /dev/rfcomm0 else: portName = "COM12" # Change this to your main Serializer port! baudRate = 19200 # Change this to your Serializer baud rate! mySerializer = Serializer.Serializer(port=portName, baudrate=baudRate, timeout=0.5) mySerializer.connect() print "Firmware Version", mySerializer.fw() print "Units", mySerializer.get_units() """ * * * * * Caution! Uncomment the following functions ONLY when you know your robot is ready to move safely! You should also set the vpid, dpid, wheel diameter, wheel track and gear_reduction to match your robot. (Default units are meters and radians.) """ #mySerializer.set_units(0) # Set units to metric. #mySerializer.set_vpid(2, 0, 5, 45) #mySerializer.set_dpid(1, 0, 0, 5, 10) #mySerializer.set_wheel_diameter(0.132) # Wheel diameter in meters
def search(self, board: chess.Board, depth, side, alpha=-2, beta=2): """ Performs a minimax search with alpha-beta pruning. Alpha, beta are set to -2, 2 respectively; because the suppport of the CNN is [0,1]. Args: `board`: Chess board state at current node. `depth`: Search depth. `side`: White (True), Black (False). White is the maximising player. `alpha`: Current minimum score White is assured of. `beta`: Current maximum score Black is assured of. Returns: (evaluation, uci-encoded move that `side` should play) """ if depth == 0: input = szr.Serializer(board).serialize() return (self.model.predict(np.expand_dims(input, 0)), board.pop()) if board.is_game_over(): res = board.result() if res == '0-1': return 0 elif res == '1-0': return 1 else: return 0.5 if side: max_score = -2 best_move = '' for move in board.legal_moves: new_board = board.copy() new_board.push(move) score = self.search(new_board, depth - 1, alpha, beta, False)[0] if score > max_score: max_score = score best_move = move alpha = max(alpha, score) if alpha >= beta: # black has a better option available earlier on in the tree. break return (max_score, best_move) else: min_score = 2 best_move = '' for move in board.legal_moves: new_board = board.copy() new_board.push(move) score = self.search(new_board, depth - 1, alpha, beta, True)[0] min_score = min(score, min_score) if score < min_score: min_score = score best_move = move beta = min(beta, score) if beta <= alpha: # white has a better option avilable earlier on in the tree. break return (min_score, best_move)
def main(): """Main function""" # init lang = "" # else, use that from catalog! input_file = None # must be set by command line argument base_uri = "http://www.example.com" # may be overwritten by command line parameter output_folder = "output" # may be overwritten by command line parameter image_uri = "ignore" # uri path to images as specified in bmecat catalog - ignore ignores any images model_only = False # print model data only, i.e. hide/skip offering details pattern = "" # product uri pattern, any string containing %s is allowed, e.g. http://www.example.com/products/id_%s/ catalog = classes.Catalog() # global settings are stored in catalog object # parse command line arguments previous = "" for arg in sys.argv[1:]: warn = False if previous == "-b": base_uri = arg elif previous == "-o": output_folder = arg elif previous == "-l": lang = arg elif previous == "-t": if arg == "actual": catalog.typeOfProducts = arg elif arg == "placeholder": catalog.typeOfProducts = arg elif arg == "model": model_only = True else: warn = True elif previous == "-i": image_uri = arg elif previous == "-p": pattern = arg elif previous == "" and len(arg) > 0 and arg[0] != '-': input_file = arg elif previous: warn = True if warn: print "WARNING: Could not interpret command series -> %s %s" % ( previous, arg) previous = "" if arg == "--help": print "USAGE" print "\tpython main.py [options] FILE" print print "OPTIONS" print "\t-o <dir>\t\tcustomize location of output folder" print "\t\t\t\t(default = output)" print "\t-b <uri>\t\tprovide base uri for deployment" print "\t\t\t\t(default = http://www.example.com)" print "\t-l <language>\t\t2-letter language code according to ISO 639-1" print "\t\t\t\t(default = try to determine from catalog)" print "\t-t <typeOfProduct>\tconfigure the type of product (if \"model\", then will print product model catalog only)" print "\t\t\t\t(default = actual)" print "\t\t\t\tPossible values are [1]:" print "\t\t\t\t* actual: products are likely all gr:ActualProductOrServiceInstance, and" print "\t\t\t\t* placeholder: products are likely all gr:ProductOrServicesSomeInstancesPlaceholder" print "\t\t\t\t* model: only product model data (gr:ProductOrServiceModel) gets printed out, i.e. offer data is skipped" print "\t-i <uri>\t\tfull uri path to image folder that contains images as specified in bmecat file" print "\t\t\t\t(default = ignore)" print "\t-p <uri_pattern>\turi pattern for product page urls" print "\t\t\t\t(default = \"\")" print "\t\t\t\tproduct uri pattern, any string containing %s is allowed, e.g. http://www.example.com/products/id_%s/" print "\t--help\t\t\tprint usage summary" print print print "[1] http://www.heppnetz.de/ontologies/goodrelations/v1#ProductOrService" print print "..." print "LGPL licensed command-line script for the conversion of BMECat XML to GoodRelations for Web publishing" print "E-Business and Web Science Research Group, http://www.unibw.de/ebusiness/" print "Developer: Alex Stolz <*****@*****.**>" print "..." return elif len(arg) > 0 and arg[0] == "-": previous = arg if not input_file: sys.stderr.write("No XML input file was provided\n") print "Usage summary: \"python main.py --help\"" return # parse and serialize on-the-fly serializerobject = serializer.Serializer(output_folder, base_uri, catalog, lang, image_uri, model_only, pattern) parserobject = parser.Parser(serializerobject) parserobject.parse( input_file, search="cataloggroup") # mappings between articles and catalog groups parserobject.parse(input_file, search="be") parserobject.parse(input_file, search="offer") specgen.create_html(output_folder) print "Conversion successfully finished"
def deserialize(data, status_code): """Deserializes a JSON string into a dictionary.""" if status_code == 204: return data return serializer.Serializer().deserialize(data)['body']