Python Forum
Code is Working As-Is, Looking for Pointers to Improve
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code is Working As-Is, Looking for Pointers to Improve
#1
Lightbulb 
Raspbian10 (Buster) (Pi-Star image 4.1.6)
Python 2.7.16
My script is working properly, other than I still need to figure out how to set the callsign variable in the bash output in the cmd.

I am looking for pointers and suggestions to make the code look better, and less redundant, possibly using arrays and loops to store the variables to send.
My hangup is the vernacular to google. Any useful input is appreciated.
I am currently going down the "2-dimensional array" rabbit hole, but it may not apply.

Also NOTE: There are limitations on what I can print out to the bash command due to the pocsag pager's screen size, and the RemoteCommand program's message buffer limit, so although I am open to input, there are constraints that I cannot get around.

## pocsagSolar.py - Pull XML data from hamsql
## Parse XML to a POCSAG msg and send via RemoteCommand or DAPNet
#
# John/KI5NYZ 2022
#
# Original script by Josh/KI6NAZ
# Adapted from youtube screenshot https://youtu.be/XnyxwmMPX_g?t=323

import urllib3.request
import xmltodict
import subprocess

callsign = "N0CALL"
rubric1 = "" # Solar Weather
rubric2 = "" # Band Conditions

http = urllib3.PoolManager()

url = 'http://www.hamqsl.com/solarxml.php'
response = http.request('GET',url)

doc = xmltodict.parse(str(response.data))
solarindex = doc['solar']['solardata']['solarflux']
aindex = doc['solar']['solardata']['aindex']
kindex = doc['solar']['solardata']['kindex']
sunspots = doc['solar']['solardata']['sunspots']
snr = doc['solar']['solardata']['signalnoise']
muf = doc['solar']['solardata']['muf']

solarindex = " SFI: {}".format(solarindex)
aindex = " / A: {}".format(aindex)
kindex = " / K: {}".format(kindex)
sunspots = " / Sunspots: {}".format(sunspots)
snr = " / SNR: {}".format(snr)
muf = " / MUF: {}".format(muf)

d0 = doc['solar']['solardata']['calculatedconditions']['band'][0]
d1 = doc['solar']['solardata']['calculatedconditions']['band'][1]
d2 = doc['solar']['solardata']['calculatedconditions']['band'][2]
d3 = doc['solar']['solardata']['calculatedconditions']['band'][3]
n0 = doc['solar']['solardata']['calculatedconditions']['band'][4]
n1 = doc['solar']['solardata']['calculatedconditions']['band'][5]
n2 = doc['solar']['solardata']['calculatedconditions']['band'][6]
n3 = doc['solar']['solardata']['calculatedconditions']['band'][7]
v1 = doc['solar']['solardata']['calculatedvhfconditions']['phenomenon'][2]

for k, v in d0.items():
	d0 = v
for k, v in d1.items():
	d1 = v
for k, v in d2.items():
	d2 = v
for k, v in d3.items():
	d3 = v
for k, v in n0.items():
	n0 = v
for k, v in n1.items():
	n1 = v
for k, v in n2.items():
	n2 = v
for k, v in n3.items():
	n3 = v
for k, v in v1.items():
	v1 = v

d0 = " 80-40:{}".format(d0)
d1 = " 30-20:{}".format(d1)
d2 = " 17-15:{}".format(d2)
d3 = " 12-10:{}".format(d3)
n0 = " 80-40:{}".format(n0)
n1 = " 30-20:{}".format(n1)
n2 = " 17-15:{}".format(n2)
n3 = " 12-10:{}".format(n3)
v1 = " / VHF: {}".format(v1)

#Send Solar Weather via RemoteCommand
cmd = "sudo /usr/local/bin/RemoteCommand 7642 page " + rubric1 + solarindex + kindex + aindex + sunspots + snr + muf + v1
process = subprocess.call(cmd,stdout=subprocess.PIPE, shell=True)

#Send Band Conditions via RemoteCommand
cmd = "sudo /usr/local/bin/RemoteCommand 7642 page " + rubric2 + d0 + d1 + d2 + d3 + n0 + n1 + n2 + n3
process = subprocess.call(cmd,stdout=subprocess.PIPE, shell=True)

#Send Solar Weather via DAPNet
#cmd = "sudo /usr/local/sbin/pistar-dapnetapi N0CALL 'Solar Index:'" + solarindex + kindex + aindex + sunspots + snr + muf + v1
#process = subprocess.call(cmd,stdout=subprocess.PIPE, shell=True)

#Send Band Conditions via DAPNet
#cmd = "sudo /usr/local/sbin/pistar-dapnetapi N0CALL 'Band Conditions:'" + d0 + d1 + d2 + d3 + n0 + n1 + n2 + n3
#process = subprocess.call(cmd,stdout=subprocess.PIPE, shell=True)
buran write May-05-2022, 08:38 AM:
Moved to Code Review section of the forum
Larz60+ write May-04-2022, 11:16 PM:
Please use proper BBCode tags
Fixed for you.
Reply


Messages In This Thread
Code is Working As-Is, Looking for Pointers to Improve - by ki5nyz - May-04-2022, 08:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to improve this Python 3 code? iamaghost 6 3,971 Jan-18-2021, 06:39 PM
Last Post: iamaghost

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020