Beispiel #1
0
    def setUp(self):
        self._MULTIPLIER = 1000
        self._NUM_LOC = 10
        self._NUM_OBS = 7
        if self._NUM_OBS >= self._MULTIPLIER:
            raise Exception(
                'Error: _NUM_OBJ cannot be more thank _MULTIPLIER.\nKey creation will fail!'
            )

        self._inputs = []
        self._checkLoc = []
        self._checkDate = {}  # Key is Loc:index
        self._checkValues = {}  # Key is Loc:index

        dt = datetime.strptime('1/1/2020', '%m/%d/%Y')
        for i in range(0, self._NUM_LOC):
            loc = Location(str(i))
            self._checkLoc.append(loc)

            for j in range(0, self._NUM_OBS):
                value = i * self._MULTIPLIER + j
                keyIndex = value
                key = TestObsCollection._makeKey(loc, keyIndex)
                dtNow = dt + timedelta(days=keyIndex)
                self._inputs.append((loc, dtNow.strftime('%m/%d/%Y'), value))
                self._checkDate[key] = dtNow
                self._checkValues[key] = value

        self._numInputs = len(self._inputs)
Beispiel #2
0
    def test_num_observations(self):
        coll = ObsCollection()
        self.assertEqual(coll.numObservations(), 0)

        obsDate = lambda dt: ObservationDate.fromMDY(dt.strftime('%m/%d/%Y'))
        coll.addObservation(Location(city_="New City"),
                            obsDate(datetime.now()), 1)
        self.assertEqual(coll.numObservations(), 1)
Beispiel #3
0
 def test1():
     self.assertIsNone(CmdLineToLocation.makeLocation(''))
     self.assertIsNone(CmdLineToLocation.makeLocation('abc'))
     self.assertIsNone(CmdLineToLocation.makeLocation('/'))
     self.assertIsNone(CmdLineToLocation.makeLocation('abc/def'))
     loc = Location('abc', 'def', 'ghi')
     self.assertEqual(loc,
                      CmdLineToLocation.makeLocation('abc/def/ghi'))
     self.assertEqual(loc,
                      CmdLineToLocation.makeLocation('abc/def/ghi/jkl'))
Beispiel #4
0
 def test1():
     # Test for None
     l = Location()
     self.assertNotEqual(None, l)
     self.assertEqual(Location(), l)
     self.assertEqual(l, l)
     self.assertNotEqual('', l)
     l1 = Location()
     self.assertEqual(l1, l)
     l1._country = None
     self.assertEqual(l1, l)
     l1._country = ''
     self.assertNotEqual(l1, l)
     l1 = l
     self.assertEqual(l1, l)
     l1 = Location(None)
     self.assertEqual(l1, l)
     l1 = Location('', '', '')
     self.assertNotEqual(10, l1)
Beispiel #5
0
 def test3():
     loc = Location('abc xyz', 'def', 'ghi')
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc xyz/def/ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation(' abc xyz/def/ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc xyz /def/ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation(' abc xyz /def/ghi/jhk'))
     loc = Location('abc xyz', 'def mno', 'ghi')
     # print(str(loc), loc.state, Location('abc xyz', 'def mno', 'ghi').state)
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc xyz/def mno/ghi/jhk'))
     self.assertEqual(
         loc,
         CmdLineToLocation.makeLocation('abc xyz/ def mno/ghi/jhk'))
     self.assertEqual(
         loc,
         CmdLineToLocation.makeLocation('abc xyz/def mno /ghi/jhk'))
     self.assertEqual(
         loc,
         CmdLineToLocation.makeLocation('abc xyz/ def mno /ghi/jhk'))
Beispiel #6
0
    def addCSVToObsCollection(csvObj_, obsColl_):
        """
        Convert a csvObj_ to an Observation, and add to collection of observations.
        :param csvObj_: Covid19CSV object
        :param obsColl_: Collection of observations as ObsCollection
        :return: List of Observation
        """
        assert isinstance(csvObj_, Covid19CSV)
        assert isinstance(obsColl_, ObsCollection)

        loc = Location(csvObj_.country, csvObj_.state, csvObj_.county)
        for dtStr in csvObj_.dateList:
            obsDate = ObservationDate.fromMDY(dtStr)
            if obsDate is None:
                # Try using the 2-digit year format, since the file can have either.
                obsDate = ObservationDate.fromMDY2(dtStr)
            # print(dtStr)
            assert isinstance(obsDate, ObservationDate)
            obsColl_.addObservation(loc, obsDate, csvObj_.date2Value[dtStr])
Beispiel #7
0
 def test2():
     loc = Location('abc', 'def', 'ghi')
     self.assertEqual(loc,
                      CmdLineToLocation.makeLocation('abc/def/ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation(' abc/def/ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc /def/ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc/ def/ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc/def /ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc/def/ ghi/jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc/def/ghi /jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc/def/ghi/ jhk'))
     self.assertEqual(
         loc, CmdLineToLocation.makeLocation('abc / def / ghi / jhk'))
Beispiel #8
0
            def verifyObs(obsColl_):
                def compareLists(dateTup_, valueTup_, obsL_):
                    j = 0
                    self.assertEqual(len(dateTup_), len(valueTup_))
                    for dtStr in dateTup_:
                        # print(dtStr, valueTup_[j])
                        dt = datetime.strptime(dtStr, '%m/%d/%Y')
                        value = valueTup_[j]
                        obs = obsL_[j]
                        # print(str(obs))
                        self.assertEqual(dt, obs._obsDate._value)
                        self.assertEqual(value, obs._value)
                        j = j + 1

                i = 0
                for locTup in locList:
                    loc = Location(country_=locTup[0],
                                   state_=locTup[1],
                                   county_=locTup[2])
                    obsList = obsColl_.getObservations(loc)
                    self.assertEqual(len(valueList[i]), len(obsList))
                    compareLists(dateList[i], valueList[i], obsList)
                    i = i + 1
Beispiel #9
0
 def test_city(self):
     loc = Location()
     self.assertIsNone(loc._city)
     loc = Location(None, None, None, 'New City')
     self.assertEqual('New City', loc._city)
     self.assertNotEqual('Nanuet', loc._city)
     loc._city = 'Ramapo'
     self.assertEqual('Ramapo', loc._city)
     loc._country = 'US'
     loc._state = 'NY'
     loc._county = 'Rockland'
     self.assertEqual('Ramapo', loc._city)
     l1 = Location('US', 'NY', 'Rockland', 'Ramapo')
     self.assertEqual(loc, l1)
     l1.city = 'Monroe'
     self.assertNotEqual('Monroe', l1.city)
     self.assertEqual('Ramapo', l1.city)
Beispiel #10
0
 def test_country(self):
     loc = Location()
     self.assertIsNone(loc.country)
     self.assertEqual('//', loc.locationAsStr())
     loc = Location('')
     self.assertIsNotNone(loc.country)
     self.assertEqual('//', loc.locationAsStr())
     loc = Location('abc')
     self.assertIsNotNone(loc)
     self.assertEqual('abc', loc.country)
     self.assertNotEqual('abcd', loc.country)
     self.assertEqual('abc//', loc.locationAsStr())
     loc.country = 'xyz'
     self.assertEqual('abc', loc.country)
     self.assertNotEqual('xyz', loc.country)
     self.assertEqual('abc//', loc.locationAsStr())
Beispiel #11
0
 def test_county(self):
     loc = Location()
     self.assertIsNone(loc._county)
     loc = Location(None, None, 'Rockland')
     self.assertEqual('Rockland', loc._county)
     self.assertNotEqual('Middlesex', loc._county)
     self.assertEqual('//Rockland', loc.locationAsStr())
     loc._county = 'Hudson'
     self.assertEqual('Hudson', loc._county)
     self.assertEqual('//Hudson', loc.locationAsStr())
     loc._country = 'US'
     loc._state = 'NY'
     self.assertEqual('Hudson', loc._county)
     self.assertEqual('US/NY/Hudson', loc.locationAsStr())
     l1 = Location('US', 'NY', 'Hudson')
     self.assertEqual(loc, l1)
     l1.county = 'Orange'
     self.assertNotEqual('Orange', l1.county)
     self.assertEqual('Hudson', l1.county)
     self.assertEqual('US/NY/Hudson', loc.locationAsStr())
Beispiel #12
0
 def test_state(self):
     loc = Location()
     self.assertIsNone(loc._state)
     loc = Location(None, 'NY')
     self.assertEqual('NY', loc._state)
     self.assertNotEqual('NJ', loc._state)
     self.assertEqual('/NY/', loc.locationAsStr())
     loc._state = 'CA'
     self.assertEqual('CA', loc._state)
     self.assertEqual('/CA/', loc.locationAsStr())
     loc._country = 'US'
     self.assertEqual('CA', loc._state)
     self.assertEqual('US/CA/', loc.locationAsStr())
     l1 = Location('US', 'CA')
     self.assertEqual(loc, l1)
     l1 = Location('US', 'New Jersey')
     self.assertEqual('New Jersey', l1.state)
     self.assertEqual(Location('US', 'New Jersey'), l1)
     self.assertEqual('US/New Jersey/', l1.locationAsStr())
     l1.state = 'NJ'
     self.assertNotEqual('NJ', l1.state)
     self.assertEqual('New Jersey', l1.state)
     self.assertEqual('US/New Jersey/', l1.locationAsStr())
Beispiel #13
0
 def test1():
     h = hash(Location())
     self.assertEqual(h, h)
     l = Location()
     self.assertEqual(Location(), l)
     self.assertEqual(h, hash(l))
     l._country = None
     self.assertEqual(h, hash(l))
     l1 = Location()
     h1 = hash(l1)
     self.assertEqual(h1, h)
     l1._country = None
     self.assertEqual(h1, hash(l1))
     l1._country = ''
     self.assertNotEqual(h1, hash(l1))
     l1._country = 'abc'
     self.assertNotEqual(h1, hash(l1))
     h1 = hash(l1)
     self.assertEqual(h1, hash(l1))
     l2 = Location(country_='abc')
     self.assertEqual(h1, hash(l2))
     l2._state = 'NY'
     l2._county = 'Rockland'
     l2._city = 'New City'
     self.assertNotEqual(l1, l2)
     self.assertNotEqual(h1, hash(l2))
     self.assertEqual(
         hash(
             Location(country_='abc',
                      state_='NY',
                      county_='Rockland',
                      city_='New City')), hash(l2))
Beispiel #14
0
 def test2():
     # Test for value
     l = Location('')
     self.assertEqual(l, l)
     self.assertNotEqual('', l)
     self.assertNotEqual('[ ,country =  ,]', l)
     self.assertNotEqual(Location, l)
     self.assertEqual(Location(''), l)
     l1 = Location('')
     self.assertEqual(l1, l)
     l1._country = 'abc'
     self.assertNotEqual(l1, l)
     l1._country = l._country
     self.assertEqual(l1, l)
     l1 = Location(state_='')
     self.assertNotEqual(l1, l)
     l2 = Location()
     l2._state = ''
     self.assertEqual(l1, l2)
     self.assertNotEqual(l2, l)
     l2._country = 'abc'
     self.assertNotEqual(l2, l1)
     l1._country = 'abc'
     self.assertEqual(l2, l1)
     l1._state = 'NY'
     self.assertNotEqual(l2, l1)
     self.assertEqual(l1, l1)
     l2._state = l1._state
     self.assertEqual(l2, l1)
     self.assertEqual(Location(country_='abc', state_='NY'), l1)
     l1._county = 'Rockland'
     l1._city = 'New City'
     self.assertNotEqual(l2, l1)
     self.assertEqual(l1, l1)
     l2._city = l1._city
     self.assertNotEqual(l2, l1)
     l2._state = l1._state
     self.assertNotEqual(l2, l1)
     l2._county = l1._county
     self.assertEqual(l2, l1)
     l1._country = 'US'
     self.assertNotEqual(l2, l1)
     self.assertNotEqual(Location(country_='US', state_='NY'), l1)
     self.assertNotEqual(
         Location(country_='US', state_='NY', county_='Rockland'), l1)
     self.assertEqual(
         Location(country_='US',
                  state_='NY',
                  county_='Rockland',
                  city_='New City'), l1)
Beispiel #15
0
def build_game():
    """
    Edit this to customize the story of the game
    """
    # Locations
    cottage = Location("Cottage",
                       "You are standing in a small cottage.")
    garden_path = Location("Garden Path",
                           "You are standing on a lush garden path. There is a cottage here.")
    cliff = Location("Cliff",
                     "There is a steep cliff here. You fall off the cliff and lose the game. THE END.",
                     end_game=True)
    fishing_pond = Location("Fishing Pond",
                            "You are at the edge of a small fishing pond.")

    # Connections
    cottage.add_connection("out", garden_path)
    garden_path.add_connection("west", cliff)
    garden_path.add_connection("south", fishing_pond)

    # Items that can be pick up
    fishing_pole = Item("pole", "a fishing pole", "It's just a simple fishing pole.",
                        start_at=cottage)
    potion = Item("potion", "a bottle of potion", "It's bright green and steaming.",
                  start_at=cottage,
                  take_text="As you get near the potion, the fumes cause you to faint and lose the game. THE END.",
                  end_game=True)
    rosebush = Item("rosebush", "a rosebush", "The rosebush contains a single red rose. It is beautiful.",
                    start_at=garden_path)
    rose = Item("rose", "a red rose", "It smells good.")
    fish = Item("fish", "a fish", "The fish is dead. It smells terrible.")

    # Items that cannot be pick up
    pond = Item("pond", "a small fishing pond", "There are fish swimming in the pond.",
                start_at=fishing_pond,
                gettable=False)

    """
    Add special functions to items
    """
    # Generating dictionary of alternative commands
    commands = ["pick rose",
                "smell rose",
                "catch fish",
                "catch fish with pole",
                "eat fish"]
    # RUN THIS TO CREATE LOCAL ANNOTATIONS
    # senses, hypernyms, hyponyms = generate_annotations(commands)
    # save_to_file(senses, hypernyms, hyponyms)
    cmd_dict = generate_command_dict(commands)
    # Making alternatives work
    for cmd in cmd_dict["pick rose"]:
        rosebush.add_action(cmd, add_item_to_inventory,
                        (rose, "You pick the lone rose from the rosebush.", "You already picked the rose."))
    for cmd in cmd_dict["smell rose"]:
        rose.add_action(cmd, describe_something,
                    ("It smells sweet."))
    for cmd in cmd_dict["catch fish"]:
        pond.add_action(cmd, describe_something,
                        ("You reach into the pond and try to catch a fish with your hands, but they are too fast."))
    for cmd in cmd_dict["catch fish with pole"]:
        pond.add_action(cmd,
                    add_item_to_inventory,
                    (fish, "You dip your hook into the pond and catch a fish.", "You weren't able to catch any other fish."),
                    preconditions={"inventory_contains": fishing_pole})
    for cmd in cmd_dict["eat fish"]:
        fish.add_action(cmd, end_game,
                        ("That's disgusting! It's raw! And definitely not sashima-grade! But you've won this version of the game. THE END."))

    return Game(cottage)
Beispiel #16
0
 def makeLocation(locStr_):
     if locStr_.count('/') >= 2:
         locPartList = locStr_.split('/')
         return Location(str(locPartList[0]).strip(),
                         str(locPartList[1]).strip(),
                         str(locPartList[2]).strip())
Beispiel #17
0
 def createLoc(s1_, s2_, s3_):
     return Location(country_=s1_, state_=s2_, county_=s3_)