Issue
Data extracted from one method is not getting parsed correctly when called in other.
class AppTool:
def __init__(self, local_ip=None, host_ip=None, interval=None, transfer=None, bandwidth=None, app_results=None):
self.local_ip = local_ip
self.host_ip = host_ip
self.interval = interval
self.transfer = transfer
self.bandwidth = bandwidth
self.app_results = app_results if app_results is not None else {}
@staticmethod
def extract_information(output):
local_ip_match = re.search(r'local (\d+\.\d+\.\d+\.\d+)', output)
host_ip_match = re.search(r'Connecting to host (\d+\.\d+\.\d+\.\d+)', output)
receiver_line_match = re.search(
r'([\d.]+-[\d.]+)\s+sec\s+([\d.]+\s+\w?Bytes)\s+([\d.]+\s+\w?bits/sec)\s+receiver', output)
if local_ip_match and host_ip_match and receiver_line_match:
local_ip = local_ip_match.group(1)
host_ip = host_ip_match.group(1)
interval, transfer, bandwidth = receiver_line_match.groups()
print("Extracted Information - local_ip", local_ip)
print("Extracted Information - host_ip", host_ip)
print("Extracted Information - interval", interval)
print("Extracted Information - transfer", transfer)
print("Extracted Information - bandwidth", bandwidth)
return AppTool(local_ip, host_ip, interval, transfer, bandwidth)
else:
return None
The key's are parsing the correct value in the above method. But when this is called in the below method, it is taking incorrect values
@staticmethod
def create_config_map_manifest(extracted_info, app_results=None):
data = {
"local_IP": extracted_info.local_ip,
"host_IP": extracted_info.host_ip,
"interval": extracted_info.interval,
"transfer": extracted_info.transfer,
"bandwidth": extracted_info.bandwidth,
}
if app_results:
data["app_results"] = app_results
config_map_manifest = yaml.dump(data, default_flow_style=False)
print("ConfigMap Data:", config_map_manifest)
return data
On executing the code, the output is as shown below. The values extracted from extracted_information is not getting set correctly in create_config_map_manifest(). There is a mismatch
Extracted Information - local_ip 10.122.1.159
Extracted Information - host_ip 10.122.1.150
Extracted Information - interval 0.00-5.02
Extracted Information - transfer 10.8 GBytes
Extracted Information - bandwidth 18.4 Gbits/sec
-------------------------------
ConfigMap Data: bandwidth: null
host_IP: 0.00-5.02
interval: 10.8 GBytes
local_IP: 10.122.1.150
transfer: 18.4 Gbits/sec
How to preserve the yaml key-value and it's order when called into other method?
Solution
I'm assuming the extracted_info
object is an instance of your class AppTool
. The first thing I notice is you are passing in positional arguments to a function where all your parameters are keyword arguments, and that the resulting mismatch is shifted 1 value down. So host_ip
becomes local_ip
, interval
becomes host_ip
, etc. I would try returning your AppTool
object with the following instantiation instead:
return AppTool(local_ip=local_ip, host_ip=host_ip, interval=interval, transfer=transfer, bandwidth=bandwidth)
Answered By - cashes11
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.