While I was working on a Python script, I wanted to create a function which should print additional (verbose) output for debugging purposes.
To do this, I created a function "verboseoutput" which is then called later in the script (while parsing JSON data):
[...]
# Verbose output for debugging help
def verboseoutput(message) :
print(time.strftime("%Y%m%d %H:%M:%S"), message)
# handle json input
with open('tests/input.json', 'r') as json_file:
data = json.load(json_file)
for item in data:
verboseoutput("Found elements:", item['Status'], item['Name'], item['Id'])
[...]
However when the Python script was executed, the following error message showed up:
ck@linux ~ $ python3 script.py
Traceback (most recent call last):
File "/home/ck/script.py", line 93, in <module>
verboseoutput("Found elements:", item['Status'], item['Name'], item['Id'])
TypeError: verboseoutput() takes 1 positional argument but 4 were given
The error hints that my verboseoutput() function only supports one (1) argument, but 4 were submitted to the function:
One solution would be to adjust the verboseoutput function and enter the exact amount of arguments:
# Verbose output for debugging help
def verboseoutput(arg1, arg2, arg3, arg4) :
print(time.strftime("%Y%m%d %H:%M:%S"), message)
However this only works if the number of arguments always remain the same number.
As you can imagine, a verbose message could just be a single string - or could contain a list of variables. This function should therefore be flexible to accept anything it's been thrown at.
This can be done by setting the function to accept a variable number of arguments:
[...]
# Verbose output for debugging help
def verboseoutput(*message) :
print(time.strftime("%Y%m%d %H:%M:%S"), message)
# handle json input
with open('tests/input.json', 'r') as json_file:
data = json.load(json_file)
for item in data:
verboseoutput("Found elements:", item['Status'], item['Name'], item['Id'])
[...]
Noticed the asterisk in front of the input argument (*message)? This tells the function to accept a variable number of arguments as input.
After this change, the Python script is executed again and the verbose output is shown as expected:
ck@linux ~ $ python3 script.py
20240215 15:24:03 ('Found elements:', {'HealthRollup': 'OK', 'State': 'Enabled'}, 'HPE MR416i-o Gen11', 'DE00E000')
20240215 15:24:03 ('Found elements:', {'HealthRollup': 'OK', 'State': 'Enabled'}, 'HPE NS204i-u Gen11 Boot Controller', 'DE00B000')
20240215 15:24:03 ('Found elements:', {'HealthRollup': 'OK', 'State': 'Enabled'}, 'HPE NS204i-u Gen11 Boot Controller', 'DE00B000')
No comments yet.
AWS Android Ansible Apache Apple Atlassian BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Influx Internet Java KVM Kibana Kodi Kubernetes LVM LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Office PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder