-1

I have a question I have a dictionary called (plane) where I can call variables such as 'name' or 'speed'.

These values ​​give me this kind of output for example:

print(plane.get('name'))
print(plane.get('altitude'))

Output:

'name'= FLRDD8EFC
'speed'= 136.054323

My question is, how can I approximate the values ​​as follows?

'name'= DD8EFC (Always deleting the first three lines)
'speed'= 136.  (Approximate whole)

Thank you so much for your help! G

3 Answers3

3

My question is, how can I approximate the values ​​as follows?

You have to write the code explicitly.


'name'= DD8EFC (Always deleting the first three lines)

Fetch the string, then slice it:

name = plane.get('name')[3:]
print(f"'name' = {name}'")

However, the fact that you're using get rather than [] implies that you're expecting to handle the possibility that name doesn't exist in plane.

If that isn't a possibility, you should just use []:

name = plane['name'][3:]

If it is, you'll need to provide a default that can be sliced:

name = plane.get('name', '')[3:]

'speed'= 136. (Approximate whole)

It looks like you want to round to 0 fractional digits, but keep it a float? Call round with 0 digits on it. And again, either you don't need get, or you need a different default:

speed = round(plane['speed'], 0)

… or:

speed = round(plane.get('speed', 0.0), 0)

As for printing it: Python doesn't like to print a . after a float without also printing any fractional values. You can monkey with format fields, but it's probably simpler to just put the . in manually:

print(f"'speed': {speed}.")
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Could UserDict be used to provide already modified results directly @abamert? – Reblochon Masque Jul 17 '18 at 06:13
  • @ReblochonMasque I suppose. Or just inherit directly from `dict`; there's no real need for the indirection here. But how would `__getitem__` decide what to do? Switch on the name? The type? If the OP specified some rules, we could build those rules, but I don't want to guess too wildly. – abarnert Jul 17 '18 at 06:15
  • Thank you - I wrote a quick prototype; the 'switch' is fugly and brittle. – Reblochon Masque Jul 17 '18 at 06:26
1
>>> plane.get('name')[3:]
'DD8EFC'
>>> round(plane.get('speed'))
136
Sunitha
  • 11,777
  • 2
  • 20
  • 23
0

You could, possibly, subclass collections.UserDict (or maybe dict as @abamert suggested), and code some sort of switch/filter/formatter in the special method__getitem__:

something like this, maybe:

from collections import UserDict


class MyDict(UserDict):

    def __getitem__(self, key):
        if key == 'name':
            return self.data[key][3:]
        if key == 'speed':
            return round(self.data[key], 3)
        return self.data[key]


if __name__ == '__main__':

    mydata = MyDict({'name': 'ABCDEF', 'speed': 12.123456})
    print(mydata['name'], mydata['speed'])

output:

DEF 12.123

or subclassing dict:

class MyDict(dict):

    def __getitem__(self, key):
        if key == 'name':
            return self.get(key)[3:]
        if key == 'speed':
            return round(self.get(key), 3)
        return self[key]

Disclaimer: This is more a proof of concept than anything; I do not recommend this approach; the 'switch' is ugly and could get out of hand as soon as the list of constraints grows a bit.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80