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?

4
  • 1
    What does it mean to "not be set"? That it has a value of None? I don't know what library this is. Commented Jun 11, 2018 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 minimal reproducible example ? Commented Jun 11, 2018 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. Commented Jun 18, 2018 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. Commented Jun 18, 2018 at 15:01

2 Answers 2

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)

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

3 Comments

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?
I could drop a couple lines on that, but I could not be as extensive as the answers here: stackoverflow.com/questions/36901/…
Thank you so much, @jsbueno! You have been great help! :)
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.

1 Comment

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.

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.