Changeset 1171:ac0820702c0c


Ignore:
Timestamp:
10/20/24 12:56:57 (2 months ago)
Author:
István Váradi <ivaradi@…>
Branch:
python3
Phase:
public
Message:

The fleet can be read from a JSON file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/rpccommon.py

    r1156 r1171  
    1010class Plane(object):
    1111    """Information about an airplane in the fleet."""
     12    _jsonAttributes = ["tailNumber", "aircraftType",
     13                       "dow", "dowNumCabinCrew", "maxPassengers",
     14                       "fuselageLength", "wingSpan"]
     15
    1216    @staticmethod
    1317    def str2status(letter):
     
    2529               "P" if status==const.PLANE_PARKING else ""
    2630
     31    @staticmethod
     32    def fromJSON(data):
     33        """Create a Plane object from the given JSON data."""
     34        plane = Plane()
     35
     36        for attributeName in Plane._jsonAttributes:
     37            setattr(plane, attributeName, data[attributeName])
     38        plane.status = const.PLANE_AWAY
     39        plane.gateNumber = "-"
     40
     41        return plane
     42
    2743    @property
    2844    def hasStairs(self):
     
    3248                                     const.AIRCRAFT_F70,
    3349                                     const.AIRCRAFT_DH8D]
     50
     51    def toJSON(self):
     52        """Create a JSON representation of the Plane object from the given JSON data."""
     53        data = {}
     54        for attributeName in Plane._jsonAttributes:
     55            data[attributeName] = getattr(self, attributeName)
     56        return data
    3457
    3558    def _setStatus(self, letter):
     
    5376class Fleet(object):
    5477    """Information about the whole fleet."""
     78    @staticmethod
     79    def fromJSON(data):
     80        """Load the fleet data from the given JSON file."""
     81        fleet = Fleet()
     82        for planeData in data:
     83            fleet._addPlane(Plane.fromJSON(planeData))
     84        return fleet
     85
    5586    def __init__(self):
    5687        """Construct the fleet information by reading the given file object."""
     
    96127            plane.gateNumber = gateNumber
    97128
     129    def toJSON(self):
     130        """Convert the fleet into a JSON data."""
     131        return [plane.toJSON() for plane in self._planes.values()]
     132
    98133    def _addPlane(self, plane):
    99134        """Add the given plane to the fleet."""
Note: See TracChangeset for help on using the changeset viewer.