Ulises Bussi
Ulises Bussi

Reputation: 1725

defining multiple variables with a condition in python

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?

Upvotes: 1

Views: 136

Answers (2)

Buzz
Buzz

Reputation: 1412

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.

Upvotes: 0

Filippo Ferrario
Filippo Ferrario

Reputation: 94

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

Upvotes: 1

Related Questions