Flatten array

This commit is contained in:
Emily 2018-09-26 08:41:50 +02:00
parent 99d00cf6a9
commit 2887b7acf0
3 changed files with 11 additions and 8 deletions

View File

@ -1,5 +1,6 @@
import struct
from objects import glob
from helpers import dataHelper
def handle(client, data):
ping_ip = ".".join((str(x) for x in struct.unpack(b"<BBBB", data[4:8])))
@ -37,14 +38,14 @@ def query_r():
packet_data.append(len(rules)) # len_rules
for name, value in rules.items():
packet_data.append(len(name)) # len_name
packet_data.append(name)
packet_data.append(len(value)) # len_value
packet_data.append(value)
packet_data.append([
len(name), name,
len(value), value
])
flat_packet_data = [y for x in packet_data for y in x] # flattens the packet data arrays to one stream
flat_packet_data = dataHelper.flatten(packet_data)
packet = struct.pack(b"<H" + b"c%dsc%ds" * packet_data[0] # using len_rules
packet = struct.pack(b"<H" + b"c%dsc%ds" * flat_packet_data[0] # using len_rules
% [y for x in rules.items() for y in x], # array of only rules entries
*flat_packet_data)

2
helpers/dataHelper.py Normal file
View File

@ -0,0 +1,2 @@
def flatten(l):
return flatten(l[0]) + (flatten(l[1:]) if len(l) > 1 else []) if type(l) is list else [l]

View File

@ -52,5 +52,5 @@ class Server(Thread):
for client in clients:
client.write_socket()
for c in self.clients.values():
print(c.__writebuffer)
"""for c in self.clients.values():
print(c.__writebuffer)"""