1

Sorry if the title is not explanatory enough, but it's the best I could come up with. This is a part of my code, a python script that translates files from Xilog3 to woodWOP format (cnc programs).

try:
    print >>woodWOPfile, 'YA="%s"' %xbo['Y']
except KeyError:
    xbo['Y']=xbo_prev['Y']
    print >>woodWOPfile, 'YA="%s"' %xbo['Y']

This prints a dictionary key item to the output file. If the key does not exist, I want to load it from the previous version of the dictionary, xbo_prev, which is copied from xbo before every new line reading cycle.

Using the print rule twice seems pretty stupid, but it's the best I can come up with. Is there a way to simplify this?

Thanks :).

Michel Storms
  • 336
  • 3
  • 10
  • 1
    Will `xbo_prev` always have the key? – Morgan Thrapp Dec 07 '15 at 21:37
  • Morgan's question is pretty important, because the simplest way if `xbo_prev` always has the key is different from the simplest way if `xbo_prev` might not have the key. – user2357112 Dec 07 '15 at 21:38
  • Yes, the dictionary is preloaded with default values at the start of the script, so if the key is not found in xbo, either a default (usually 0) one will be found in xbo_prev, which is valid. I need to be able to get the previous key if it does not exist in the current dictionary, because some drillings rely on parameters already set in the previous line. For example: – Michel Storms Dec 07 '15 at 21:40
  • XBO X=100 Y=200 Z=10 – Michel Storms Dec 07 '15 at 21:40
  • XBO X=100 Y=100 , Z is left out because it should have the same value as the previous drilling. However, the woodWOP system wants all values known for each separate drilling. – Michel Storms Dec 07 '15 at 21:41
  • This might be more appropriate at CodeReview. – Kyle Strand Dec 07 '15 at 21:46
  • Python 3 specific alternative: `from collections import ChainMap`, `xbo = ChainMap(xbo, xbo_prev)`, then use the new `xbo` normally. When the value is set on `xbo`, it will get the value from the original `xbo` (and setting a new value on the chained dictionary sets it on the underlying `xbo`), and if it isn't, it seamlessly retrieves it from `xbo_prev`. – ShadowRanger Dec 10 '15 at 02:35

3 Answers3

9

You could simply use dict.setdefault:

print woodWOPfile, 'YA="%s"' % xbo.setdefault('Y', xbo_prev['Y'])

This gets the value corresponding to key Y if it is present in the map, otherwise sets it to the second parameter and returns it.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1
if not 'Y' in xbo: 
    xbo['Y']=xbo_prev['Y']
print >>woodWOPfile, 'YA="%s"' %xbo['Y']

why not check if its there and set it first.

corn3lius
  • 4,857
  • 2
  • 31
  • 36
0

The existing two answers work too, but an alternative if you know the xbo_prev will have your key is to take advantage of .get()'s default parameter:

print >>woodWOPfile, 'YA="%s"' % xbo.get('Y', xbo_prev['Y'])

Note that this doesn't modify xbo, if you intend to modify xbo you can use .setdefault() like juanchopanza suggests, though I'd argue that putting mutating operations in a print statement is confusing and likely to cause more harm than good.

dimo414
  • 47,227
  • 18
  • 148
  • 244