-1

I have a statement which prints a string relating to a chemical and its desired key values, how do I print the key values corresponding key name?

def chemByString(chemName,chemicals,priority="echo"):
    for chemical in chemicals:
        chemDict = chemical.toDict(priority)
        if chemDict["chemicalName"] == chemName
            return chemical
    return None

print str(chemByString('O2', allChemicals, priority="echo").chemicalName) + str("{chemicalName:<5s} {charge:<20s}{comment:<20s}".format(**chemByString('O2', allChemicals, priority="echo").toDict()))

Output:

{'tv': 'O2', 'echo': 'O2'} O2     0     O2   

Desired Output:

{'tv': 'O2', 'echo': 'O2'} O2 Charge 0 Comment O2

or

                         Species  Charge   Comment

{'tv': 'O2', 'echo': 'O2'}  O2        0       O2 
Amy Rose
  • 95
  • 1
  • 9
  • why you tagged `dictionary` to this question as you have not any dict here ? also you have some undefined name here ? – Mazdak Jan 04 '15 at 18:28
  • 4
    This is completely impossible to answer. What is `chemByString`? – Daniel Roseman Jan 04 '15 at 18:29
  • 1
    Uh, change `"{speciesName:<5s} {charge:<20s}{comment:<20s}"` to `"{speciesName:<5s} Charge {charge:<20s} Comment {comment:<20s}"`? – Aran-Fey Jan 04 '15 at 18:35
  • @Rawing, is there another more automated way? – Amy Rose Jan 04 '15 at 18:39
  • @AmyRose: I don't think so. You'd probably just end up making your code harder to read. – Aran-Fey Jan 04 '15 at 18:44
  • 1
    @DanielRoseman if the code works and the output is real, it seems that `chemByString` is a function that returns an object with properties `chemicalName` (dict or custom object), `speciesName`, `charge` and `comment` (str or custom objects), method `toDict()` that returns dict – Aprillion Jan 04 '15 at 18:47
  • 1
    @Rawing always gloom and doom? one moment please and I'll try to write an answer... – Aprillion Jan 04 '15 at 18:51
  • 3
    @DanielRoseman not impossible, just a good question in disguise of some missing information. – Mike McKerns Jan 04 '15 at 18:57

2 Answers2

0

You can provide them in the string formatting…

>>> print str(chemByString('O2', allChemicals, priority="echo").chemicalName) + str("speciesName: {speciesName:<5s} charge: {charge:<20s} comment: {comment:<20s}".format(**chemByString('O2', allChemicals, priority="echo").toDict()))
{'tv': 'O2', 'echo': 'O2'} speciesName: O2    charge: 0    comment: O2

Should get you where you want to go.

EDIT: If you have something totally generic, that you want to apply to all members, you might be able to do something odd, like this:

>>> d = {'foo': 4, 'bar': 6}
>>> '{}: %s, {}: %s'.format(*d.keys()) % tuple(d.values())
'foo: 4, bar: 6'

That's not really intuitive, as much as special-case, though.

You might also make a __repr__ method on the class, so you have it print like you want it to print. See: Purpose of Python's __repr__

Community
  • 1
  • 1
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
  • 1
    Is there another more intuitive way? Thanks – Amy Rose Jan 04 '15 at 18:45
  • I don't think so… not unless you are going to use all the dict keys, or do the same thing to each key/value pair. If you are, you might be able to do some meta-formatting on the string. – Mike McKerns Jan 04 '15 at 18:48
0

you can use getattr() in a list comprehension for printing object properties using a list of properties:

properties = ('chemicalName', 'speciesName', 'charge', 'comments')
template = '{0:<20s} {1:<5s} {2:<20s} {3:<20s}'

chem = chemByString('O2', allChemicals, priority="echo")

print template.format(*properties)
print template.format(*[str(getattr(chem, p)) for p in properties])
Aprillion
  • 21,510
  • 5
  • 55
  • 89