1

i'm making a code where in one part I need to access to data inside a DataFrame. The main problem is that the columns in dataframe may change depending on file accessed. So i've thinked that I could define aux var for the keys to access it. My main problem now is that the code seems to work but the solusion looks pretty ugly.


if isAirData:
        LOGlat  = 'latitude'
        LOGlon  = 'longitude'
        LOGalt  = 'height_above_takeoff(feet)'
        LOGtime = 'datetime(utc)'
        LOGhead = 'compass_heading(degrees)'
        LOGpitch= 'gimbal_pitch(degrees)'#pitch(degrees)'
        LOGroll = 'roll(degrees)'
        LOGvid  = 'isVideo'
else:
        LOGlat  = 'OSD.latitude'
        LOGlon  = 'OSD.longitude'
        LOGalt  = 'OSD.height[ft]'
        LOGtime = 'CUSTOM.updateTime[local]'
        LOGhead = 'OSD.yaw'
        LOGpitch= 'OSD.pitch'
        LOGroll = 'OSD.roll'
        LOGvid  = 'CAMERA.isVideo'
        

these are my keys of interest. In the different files column number and names changes. So I was wondering which is the best way to work with this?

abcd
  • 10,215
  • 15
  • 51
  • 85
Ulises Bussi
  • 1,635
  • 1
  • 2
  • 14
  • IMO your code isn't that ugly. If you wanted to use something else, then you could always use the [ternary operator](https://www.tutorialspoint.com/ternary-operator-in-python) – Michael M. Aug 10 '22 at 18:36
  • I don't see anything ugly with your code but, depending on how are those variables used, you might improve the maintenance effort by using a dictionary or a list of tuples but, as I said, it depends on how likable is that list of variables to change or whether you can optimize the code by traversing over them. – HuLu ViCa Aug 10 '22 at 19:17

2 Answers2

1

First I would create a json file for each file type that you may access, something like this:

{
   'LOGlat': 'latitude',
   'LOGlon': 'longitude',
   ...
}

Then I would access the relevant file, saving the keys for the DataFrame in a dictionary. My code would look something like this:

import json

def read_file(path):
    file = open(path)
    data = file.read()
    file.close()
    return data

isAirData = True
keys = {}

if isAirData:
    data = read_file('isAir.json')
    keys = json.loads(data)
else:
    data = read_file('isNotAir.json')
    keys = json.loads(data)

Now all you need to do to access a column name is:

DataFrame[keys['key_name']]

I know that this solution will slow down your code, but if all you want is pretty-looking code, this works fine

Hope you find this useful

0

I would create a class container. I'd suggest inheriting from StrEnum, though it can be superfluous:

from strenum import StrEnum

class AirData(StrEnum):
        LOGlat  = 'latitude'
        LOGlon  = 'longitude'
        LOGalt  = 'height_above_takeoff(feet)'
        LOGtime = 'datetime(utc)'
        LOGhead = 'compass_heading(degrees)'
        LOGpitch= 'gimbal_pitch(degrees)'#pitch(degrees)'
        LOGroll = 'roll(degrees)'
        LOGvid  = 'isVideo'

class NotAirData(StrEnum):
        LOGlat  = 'OSD.latitude'
        LOGlon  = 'OSD.longitude'
        LOGalt  = 'OSD.height[ft]'
        LOGtime = 'CUSTOM.updateTime[local]'
        LOGhead = 'OSD.yaw'
        LOGpitch= 'OSD.pitch'
        LOGroll = 'OSD.roll'
        LOGvid  = 'CAMERA.isVideo'

So you can use a single definition as

mylabels = AirData if isAirData else NotAirData
do_something(mylabels.LOGlat)

Pros:

  • The item is treated as a string type whenever needed
  • You don't need external resource files: you can just import the module containing the two defined classes.
Buzz
  • 1,102
  • 1
  • 9
  • 24
  • It's a good solution I think, here a question why to make the class inherit from `StrEnum` ? wich are the pros of this? – Ulises Bussi Aug 10 '22 at 19:44
  • Just the advantages coming from an enumerator. But I agree with you, it can be superfluous – Buzz Aug 10 '22 at 20:30
  • sorry my ignorance, but wich are this adventages? If don't mind you to explain if not i will go to read. – Ulises Bussi Aug 10 '22 at 20:45
  • 1
    Easy iteration through the elements, just to mention one for this case. Check this answer for more advs https://stackoverflow.com/a/37601645/5321862 – Buzz Aug 10 '22 at 20:56
  • For example: `headers=[header for header in AirData]` gives you a headers list for your Qt table straight away – Buzz Aug 10 '22 at 20:58