Untitled

 avatar
unknown
python
a year ago
4.1 kB
4
Indexable
import json
import random

class LocationSelector:
    def __init__(self, json_data):
        self.original_data = json.loads(json_data)
        self.reset()
        self.isLive = False
        self.afeka = {"id": 22, "lat": 32.1134, "lon": 34.8175, "name": "Afeka College"}

    def reset(self):
        self.available_coordinates = self.original_data['coordinates'].copy()

    def select_random_location(self):
        if self.isLive:
            return self.afeka

        if not self.available_coordinates:
            print("All locations have been selected. Resetting the list.")
            self.reset()

        selected = random.choice(self.available_coordinates)
        self.available_coordinates = [coord for coord in self.available_coordinates if coord['id'] != selected['id']]
        return selected

    def set_live_status(self, status):
        self.isLive = status

# JSON data
json_data = '''
{
  "coordinates": [
    {"id": 1, "lat": 31.7767, "lon": 35.2345, "name": "Jerusalem 1"},
    {"id": 2, "lat": 31.7768, "lon": 35.2346, "name": "Jerusalem 2"},
    {"id": 3, "lat": 31.7766, "lon": 35.2344, "name": "Jerusalem 3"},
    {"id": 4, "lat": 32.0853, "lon": 34.7818, "name": "Tel Aviv"},
    {"id": 5, "lat": 31.8969, "lon": 34.8186, "name": "Beit Shemesh"},
    {"id": 6, "lat": 32.1840, "lon": 34.8697, "name": "Petah Tikva"},
    {"id": 7, "lat": 31.9628, "lon": 34.8061, "name": "Ramla"},
    {"id": 8, "lat": 32.0714, "lon": 34.8242, "name": "Givatayim"},
    {"id": 9, "lat": 31.9946, "lon": 34.7589, "name": "Rishon LeZion"},
    {"id": 10, "lat": 32.0852, "lon": 34.9780, "name": "Rosh HaAyin"},
    {"id": 11, "lat": 32.0751, "lon": 34.7752, "name": "Dizengoff Center"},
    {"id": 12, "lat": 32.1001, "lon": 34.7745, "name": "Tel Aviv Port"},
    {"id": 13, "lat": 32.0724, "lon": 34.7794, "name": "Habima Square"},
    {"id": 14, "lat": 32.0684, "lon": 34.7684, "name": "Carmel Market"},
    {"id": 15, "lat": 32.0526, "lon": 34.7519, "name": "Jaffa Old City"},
    {"id": 16, "lat": 32.0998, "lon": 34.8080, "name": "Yarkon Park"},
    {"id": 17, "lat": 32.0778, "lon": 34.7868, "name": "Tel Aviv Museum of Art"},
    {"id": 18, "lat": 32.0652, "lon": 34.7765, "name": "Rothschild Boulevard"},
    {"id": 19, "lat": 32.0632, "lon": 34.7665, "name": "Neve Tzedek"},
    {"id": 20, "lat": 32.0744, "lon": 34.7923, "name": "Azrieli Center"},
    {"id": 21, "lat": 32.1118, "lon": 34.8014, "name": "Tel Aviv University"}
  ]
}
'''

# Example usage
selector = LocationSelector(json_data)

print("Random selections when isLive is False:")
for i in range(5):
    location = selector.select_random_location()
    print(f"Selection {i+1}: {location['name']} (ID: {location['id']})")

print("\nSetting isLive to True")
selector.set_live_status(True)

print("\nSelections when isLive is True:")
for i in range(5):
    location = selector.select_random_location()
    print(f"Selection {i+1}: {location['name']} (ID: {location['id']})")

print("\nSetting isLive back to False")
selector.set_live_status(False)

print("\nRandom selections after setting isLive back to False:")
for i in range(5):
    location = selector.select_random_location()
    print(f"Selection {i+1}: {location['name']} (ID: {location['id']})")


# omer ! here is the output of this code
'''
Random selections when isLive is False:
Selection 1: Beit Shemesh (ID: 5)
Selection 2: Jerusalem 3 (ID: 3)
Selection 3: Azrieli Center (ID: 20)
Selection 4: Rishon LeZion (ID: 9)
Selection 5: Jerusalem 2 (ID: 2)

Setting isLive to True

Selections when isLive is True:
Selection 1: Afeka College (ID: 22)
Selection 2: Afeka College (ID: 22)
Selection 3: Afeka College (ID: 22)
Selection 4: Afeka College (ID: 22)
Selection 5: Afeka College (ID: 22)

Setting isLive back to False

Random selections after setting isLive back to False:
Selection 1: Neve Tzedek (ID: 19)
Selection 2: Carmel Market (ID: 14)
Selection 3: Rothschild Boulevard (ID: 18)
Selection 4: Jaffa Old City (ID: 15)
Selection 5: Rosh HaAyin (ID: 10)

'''
Editor is loading...
Leave a Comment