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 import struct
from objects import glob from objects import glob
from helpers import dataHelper
def handle(client, data): def handle(client, data):
ping_ip = ".".join((str(x) for x in struct.unpack(b"<BBBB", data[4:8]))) 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 packet_data.append(len(rules)) # len_rules
for name, value in rules.items(): for name, value in rules.items():
packet_data.append(len(name)) # len_name packet_data.append([
packet_data.append(name) len(name), name,
packet_data.append(len(value)) # len_value len(value), value
packet_data.append(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 % [y for x in rules.items() for y in x], # array of only rules entries
*flat_packet_data) *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: for client in clients:
client.write_socket() client.write_socket()
for c in self.clients.values(): """for c in self.clients.values():
print(c.__writebuffer) print(c.__writebuffer)"""