Beispiel #1
0
def on_save():
	global current_file
	if current_file:
		serialization.serialize(course, current_file)
		dirty = False
	else:
		on_save_as()
Beispiel #2
0
 def export_settings(self):
     serialization.serialize(self.get_settings())
     messagebox = QMessageBox()
     messagebox.setIcon(QMessageBox.Information)
     messagebox.setText("Settings saved in 'settings.json'")
     messagebox.setWindowTitle("Export settings...")
     messagebox.exec_()
Beispiel #3
0
def test_serialize_and_deserialize_other_data():
    data = [
        Bite('Sum of Numbers', 1, 'Beginner'),
        Bite('Regex Fun', 2, 'Advanced'),
    ]
    pkl_file = TMP / str(int(time.time()))
    serialize(pkl_file, data=data)
    actual = deserialize(pkl_file)
    expected = data
    assert actual == expected
Beispiel #4
0
    def render(self, data, headers =  {}, status_code  = 200, raw = False):

        if request.headers['Accept'] == 'application/xml':
            if raw:
                data = data
            else:
                data = serialize(data, 'xml')
            response = Response(data, content_type='application/xml; charset=utf-8')

        elif request.headers['Accept'] == 'text/csv':
            if raw:
                data = data
            else:
                data = serialize(data, 'csv')

            response = Response(data, content_type='text/csv; charset=utf-8')


        # JSON or invalid Content-Type
        else :
            if raw:
                data = data
            else:
                if self.to_hal is not None and status_code == 200:
                    if 'X-Total-Count' in headers:
                        args = {'total' : headers['X-Total-Count']}
                    else:
                        args = {}
                        
                    data = self.to_hal(data, args = args)

                data = serialize(data, 'json')

            response = Response(data, content_type='application/hal+json; charset=utf-8')


        access_control_headers =  "Content-Length, Content-Type, "
        access_control_headers += "Date, Server, "
        access_control_headers += "X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After, "
        access_control_headers += "X-Total-Count"

        headers['Access-Control-Expose-Headers'] = access_control_headers

        response.headers.extend(headers)
        response.status_code = status_code
        real_ip = request.headers.get('X-Real-Ip')

        if real_ip == None:
            real_ip = ''

        Log.info(request.path+' '+real_ip+' '+str(status_code) + ' '+str(response.headers.get('Content-Length')))

        return response
Beispiel #5
0
def fetch_reddit(filename=None):
    from praw.errors   import HTTPException, APIException, ClientException
    import praw
    import time

    start   = time.time()
    current = start
    i = 0

    with redirect(filename):
        while True:
            try:
                r = praw.Reddit(get_UA()) # Read the file in etc/user-agent
                submissions = []
                for submission in praw.helpers.submission_stream(r, 'all'):
                    i += 1

                    print('Opened submission %s' % submission.title)
                    comments = []
                    for comment in submission.comments:
                        comments.append(collect_attrs(comment, comment_attrs))
                        for subcomment in comment.replies:
                            comments.append(collect_attrs(comment, comment_attrs))

                    if i % group_size == 0: # Save submissions every `group_size` (ex 25th) submission
                        print('Writing submission group for reddit')
                        serialize(submissions, '../serialized/reddit%s' % (i/group_size))
                        submissions = []
                        print('Wrote submission group. Time so far: %s' % (time.time() - start))
                        concatenate('reddit')
                        i = 0

                    submissions.append(collect_attrs(submission, submission_attrs))
                    taken   = time.time() - current # "Current"
                    current = time.time()
                    print('Finished processing submission. Total elapsed: %s. For submission: %s' % (current - start, taken))

            except HTTPException or APIException as e: # Catch what isn't my fault
                print(e)
                print('Retrying...')
                time.sleep(1)

            except ClientException as e: # Catch what is
                print(e)
                break

            except KeyboardInterrupt:
                concatenate('reddit')
                sys.exit(0)
Beispiel #6
0
 def do_POST(self):
     content_length = int(self.headers['Content-Length'])
     post_data = self.rfile.read(content_length)
     callback_res = callback(serialization.deserialize(post_data))
     self._set_response()
     if callback_res is not None:
         self.wfile.write(serialization.serialize(callback_res))
Beispiel #7
0
 def template(item, seen):
     print('Crawling %s' % item)
     if len(seen) % interval == 0:
         print('Saving')
         concatenate(source)
     try:
         seen.add(item)
         content, remaining = get(item, seen)
         serialize(content, '../serialized/%s%s' % (source, len(seen)))
         return remaining 
     # If something goes wrong (on API side), throw away everything from this section of the crawl
     except exceptions as e: 
         print(repr(e))
         return defaultval
     except KeyboardInterrupt:
         concatenate(source)
         sys.exit(0)
Beispiel #8
0
def build_spend_message(outpoint: OutPoint, pk: str, sequence: int,
                        txouts: TxOut):
    """
    https://bitcoin.org/en/developer-guide#term-sighash-all
    similar to: SIGHASH_ALL
    """
    return sha256d_hexdigest(outpoint.serialize() + str(sequence) +
                             binascii.hexlify(pk).decode() +
                             serialize(txouts)).encode()
Beispiel #9
0
def join_room(player, room, sock):
    request = {
        "command": Command.JOIN_ROOM.value,
        "player": player,
        "room": room
    }
    sock.send(serialize(request))
    response = deserialize(sock.recv(4096))
    return response["board"], response["game_status"], response[
        "opponent_id"], response["symbol"]
Beispiel #10
0
def fetch_gutenberg(filename=None):
    from gutenberg.acquire import load_etext
    from gutenberg.cleanup import strip_headers
    from gutenbergsettings import popularTitles, saveInterval

    start    = time.time()
    lastsave = start

    with redirect(filename):
        try:
            for title in popularTitles:
                text = strip_headers(load_etext(title)).strip()
                serialize([(title, text)], '../serialized/guten%s' % title)
                sinceLast = time.time() - lastsave
                print('%s since last save' % sinceLast)
                if sinceLast > saveInterval:
                    concatenate('guten')
                    lastsave = time.time()
        except KeyboardInterrupt:
            concatenate('guten')
            sys.exit(0)
Beispiel #11
0
def connection_loop(connection, client_address, state):
    try:
        while True:
            data = connection.recv(4096)
            if not data: break
            command, command_input = parse_request(data)
            if command == Command.JOIN_ROOM.value:
                response = join_room(command_input, client_address, state)
            if command == Command.GET_GAME_STATUS.value:
                response = get_game_status(client_address, state)
            if command == Command.FILL_POSITION.value:
                response = fill_position(command_input, client_address, state)
            connection.send(serialize(response))
    finally:
        connection.close()
        print('client disconnected')
 def serialize_poses(self):
     serialize(self.poses)
Beispiel #13
0
    def render(self, data, headers={}, status_code=200, raw=False):

        if not self.resolve_content():
            data = {'message': 'invalid format', 'code': 55}
            status_code = 405

        if self.content_type == 'xml':
            if raw:
                data = data
            else:
                data = serialize(data, 'xml')
            response = Response(data,
                                content_type='application/xml; charset=utf-8')

        elif self.content_type == 'csv':
            if raw:
                data = data
            else:
                data = serialize(data, 'csv')

            resource_path = request.path.split("/")

            if resource_path[len(resource_path) - 1] != "":
                resource_type = resource_path[len(resource_path) - 1]

            else:
                resource_type = resource_path[len(resource_path) - 2]

            args_hash = md5hash(format_args(request.args))

            headers[
                "Content-Disposition"] = "attachment; filename=salicapi-%s-%s.csv" % (
                    resource_type, args_hash)

            response = Response(data, content_type='text/csv; charset=utf-8')

        # JSON or invalid Content-Type
        else:
            if raw:
                data = data
            else:
                if self.to_hal is not None and status_code == 200:
                    if 'X-Total-Count' in headers:
                        args = {'total': headers['X-Total-Count']}
                    else:
                        args = {}

                    data = self.to_hal(data, args=args)

                data = serialize(data, 'json')

            response = Response(
                data, content_type='application/hal+json; charset=utf-8')

        access_control_headers = "Content-Length, Content-Type, "
        access_control_headers += "Date, Server, "
        access_control_headers += "X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After, "
        access_control_headers += "X-Total-Count, "
        access_control_headers += "Content-Disposition"

        headers['Access-Control-Expose-Headers'] = access_control_headers

        response.headers.extend(headers)
        response.status_code = status_code
        real_ip = request.headers.get('X-Real-Ip')

        if real_ip == None:
            real_ip = ''

        Log.info(request.path + ' ' + real_ip + ' ' + str(status_code) + ' ' +
                 str(response.headers.get('Content-Length')))

        return response
Beispiel #14
0
def fill_position(position, sock):
    request = {"command": Command.FILL_POSITION.value, "position": position}
    sock.send(serialize(request))
    deserialize(sock.recv(4096))
Beispiel #15
0
def refresh_game_status(sock):
    request = {"command": Command.GET_GAME_STATUS.value}
    sock.send(serialize(request))
    response = deserialize(sock.recv(4096))
    return response["board"], response["game_status"], response["opponent_id"]
 def send(self, data):
     try:
         res = requests.post(self._url, serialization.serialize(data))
     except requests.exceptions.ConnectionError:
         return False, None
     return res.ok, serialization.deserialize(res.content)
Beispiel #17
0
 def serialize(self, item):
     return serialize(self.schema, item)
else:
    print('Data does not already exist..')

    # Create Data
    inputs = np.random.randint(low=0, high=10, size=[10000, 2])

    # Convert to torch tensors
    inputs = torch.from_numpy(inputs)

    # Convert to float
    inputs = inputs.float()

    # Train-test split
    inputs_train, inputs_test = train_test_split(inputs,
                                                 train_size=0.7,
                                                 test_size=0.3)
    inputs_test, inputs_val = train_test_split(inputs_test,
                                               train_size=0.5,
                                               test_size=0.5)

    # Convert to torch tensors
    inputs = [inputs_train, inputs_val, inputs_test]

    # Visualize
    print(inputs[0].shape, inputs[1].shape, inputs[2].shape)

    # Save
    serialize(inputs,
              'C:\\Users\\kaspe\\Code Projects\\Add_DNN\\inputs.pickle')
Beispiel #19
0
    def render(self, data, headers =  {}, status_code  = 200, raw = False):

        if not self.resolve_content():
            data = {'message' : 'invalid format',
                    'code' : 55}
            status_code = 405

        if self.content_type == 'xml':
            if raw:
                data = data
            else:
                data = serialize(data, 'xml')
            response = Response(data, content_type='application/xml; charset=utf-8')

        elif self.content_type == 'csv':
            if raw:
                data = data
            else:
                data = serialize(data, 'csv')

            resource_path = request.path.split("/")

            if resource_path[len(resource_path)-1] != "":
                resource_type = resource_path[len(resource_path)-1]

            else:
                resource_type = resource_path[len(resource_path)-2]

            args_hash = md5hash(format_args(request.args))

            headers["Content-Disposition"] = "attachment; filename=salicapi-%s-%s.csv"%(resource_type, args_hash)

            response = Response(data, content_type='text/csv; charset=utf-8')


        # JSON or invalid Content-Type
        else :
            if raw:
                data = data
            else:
                if self.to_hal is not None and status_code == 200:
                    if 'X-Total-Count' in headers:
                        args = {'total' : headers['X-Total-Count']}
                    else:
                        args = {}
                        
                    data = self.to_hal(data, args = args)

                data = serialize(data, 'json')

            response = Response(data, content_type='application/hal+json; charset=utf-8')


        access_control_headers =  "Content-Length, Content-Type, "
        access_control_headers += "Date, Server, "
        access_control_headers += "X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After, "
        access_control_headers += "X-Total-Count, "
        access_control_headers += "Content-Disposition"

        headers['Access-Control-Expose-Headers'] = access_control_headers

        response.headers.extend(headers)
        response.status_code = status_code
        real_ip = request.headers.get('X-Real-Ip')

        if real_ip == None:
            real_ip = ''

        Log.info(request.path+' '+real_ip+' '+str(status_code) + ' '+str(response.headers.get('Content-Length')))

        return response