<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def deleteInactiveDevicesByQuota(self, per_jid_max = 15, global_max = 0):
 """ Delete inactive devices by setting a quota. With per_jid_max you can define the amount of inactive devices that are kept for each jid, with global_max you can define a global maximum for inactive devices. If any of the quotas is reached, inactive devices are deleted on an LRU basis. This also deletes the corresponding sessions, so if a device comes active again and tries to send you an encrypted message you will not be able to decrypt it. The value "0" means no limitations/keep all inactive devices. It is recommended to always restrict the amount of per-jid inactive devices. If storage space limitations don't play a role, it is recommended to not restrict the global amount of inactive devices. Otherwise, the global_max can be used to control the amount of storage that can be used up by inactive sessions. The default of 15 per-jid devices is very permissive, but it is not recommended to decrease that number without a good reason. This is the recommended way to handle inactive device deletion. For a time-based alternative, look at the deleteInactiveDevicesByAge method. """


        if per_jid_max < 1 and global_max < 1:
            return

        if per_jid_max < 1:
            per_jid_max = None

        if global_max < 1:
            global_max = None

        bare_jids = yield self._storage.listJIDs()

        if not per_jid_max == None:
            for bare_jid in bare_jids:
                devices = yield self.__loadInactiveDevices(bare_jid)

                if len(devices) > per_jid_max:
                    # This sorts the devices from smaller to bigger timestamp, which means
                    # from old to young.
                    devices = sorted(devices.items(), key = lambda device: device[1])

                    # This gets the first (=oldest) n entries, so that only the
                    # per_jid_max youngest entries are left.
                    devices = devices[:-per_jid_max]

                    # Get the device ids and discard the timestamps.
                    devices = list(map(lambda device: device[0], devices))

                    yield self.__deleteInactiveDevices(bare_jid, devices)
        
        if not global_max == None:
            all_inactive_devices = []

            for bare_jid in bare_jids:
                devices = yield self.__loadInactiveDevices(bare_jid)

                all_inactive_devices.extend(map(
                    lambda device: (bare_jid, device[0], device[1]),
                    devices.items()
                ))

            if len(all_inactive_devices) > global_max:
                # This sorts the devices from smaller to bigger timestamp, which means
                # from old to young.
                devices = sorted(all_inactive_devices, key = lambda device: device[2])

                # This gets the first (=oldest) n entries, so that only the global_max
                # youngest entries are left.
                devices = devices[:-global_max]

                # Get the list of devices to delete for each jid
                delete_devices = {}

                for device in devices:
                    bare_jid  = device[0]
                    device_id = device[1]

                    delete_devices[bare_jid] = delete_devices.get(bare_jid, [])
                    delete_devices[bare_jid].append(device_id)

                # Now, delete the devices
                for bare_jid, devices in delete_devices.items():
                    yield self.__deleteInactiveDevices(bare_jid, devices)