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?

2
  • IMO your code isn't that ugly. If you wanted to use something else, then you could always use the ternary operator Commented Aug 10, 2022 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. Commented Aug 10, 2022 at 19:17

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

It's a good solution I think, here a question why to make the class inherit from StrEnum ? wich are the pros of this?
Just the advantages coming from an enumerator. But I agree with you, it can be superfluous
sorry my ignorance, but wich are this adventages? If don't mind you to explain if not i will go to read.
Easy iteration through the elements, just to mention one for this case. Check this answer for more advs stackoverflow.com/a/37601645/5321862
For example: headers=[header for header in AirData] gives you a headers list for your Qt table straight away

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.