0

I was wondering if there's anyway to 'stop' a parameter going through if a certain condition is made.

My current code looks something like this

for FMUV in FMUVList:
    ET.SubElement(fmiMV, "ScalarVariables",
                  name=FMUV.getName(),
                  valueReference=FMUV.getRNumber(),
                  description=FMUV.getDescription(),
                  start=FMUV.getStart())

Name and ValueReference is always set, but description and start might not be. I will also be adding more values later, so doing if statement for every single case might not be funniest way of doing it.

Someone got any ideas?

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Kvixen
  • 47
  • 7
  • 1
    What does it mean to "not be set"? That it has a value of `None`? I don't know what library this is. – roganjosh Jun 11 '18 at 15:10
  • 1
    That is also a very funny way of passing parameters... what are you trying to do, can you provide us a [mcve] ? – MooingRawr Jun 11 '18 at 15:11
  • @roganjosh , if they hade no value they were not even definied and prompted an NameError. Probably bad practice, but it was a temporary script lucky enough. I'm really new to Python and I'm usually very bad at explaining things. Not the best perks to have if you are trying to post in so, heh. – Kvixen Jun 18 '18 at 14:58
  • @MooingRawr , Sorry, I wish I could provide you with that example, but the script I wrote is now not being used anymore. It was used in order to read and convert certain files into XML. ET is the ElementTree package and an FMUV is just FMUVariable, an object which main purpose is to just hold some variables needed for the specific (branch?) of the tree. Next time posting, I will think about the Minimal, Complete and Verifiable example! Sorry for the headache. – Kvixen Jun 18 '18 at 15:01

2 Answers2

1

You can use star-mapping - the Python syntax that allows you to send a dictionary directly as the parameter_names/arguments part in a function call.

All you have to do them is to "curate" your dictionary so that you supress the keys for which there are no meaningful values. For this case, let's suppose you get None for the unwanted arguments:

for FMUV in FMUVList: 
  params = dict(
     name=FMUV.getName(),
     valueReference=FMUV.getRNumber(),
     description=FMUV.getDescription(),
     start=FMUV.getStart())
  )
  params = {key:value for key, value in params.items() if value is not None}
  ET.SubElement(fmiMV,"ScalarVariables", **params)

(The dict-comprehension used to filter the unused values could be placed directly in the function call, at the cost of readability)

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Sorry for the late response, but this really helped. I have tried understanding everything, but I still have a problem with **params. Would you mind explaining it? – Kvixen Jun 18 '18 at 15:03
  • I could drop a couple lines on that, but I could not be as extensive as the answers here: https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – jsbueno Jun 18 '18 at 16:11
  • Thank you so much, @jsbueno! You have been great help! :) – Kvixen Jun 19 '18 at 11:51
0

One solution involves ternary operators.

First, make sure your function treats variables with value None correctly. Then,

for FMUV in FMUVList:
  ET.SubElement(fmiMV,"ScalarVariables",
    name = FMUV.getName(),
    valueReference = FMUV.getRNumber(),
    description = (FMUV.getDescription() if <conditionA> else None),
    start = (FMUV.getStart() if <conditionB> else None)
  )

You'll still have to add one of these for every "optional" parameter, but at least it's all in only one place.

Alternatively, whatever your FMUV object is, you could make sure that FMUV.getDescription() and FMUV.getStart() return None in situations where they're not set, and then again make sure that ET.SubElement() correctly handles such values.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • This won't prevent the parameter name from being used - and if there is a difference in the called function from getting a parameter with "None" as value and not getting it at all, this would not work. – jsbueno Jun 11 '18 at 15:17