0

So I am running into the weirdest problem right now... My code only works once. After that, I need to change a print statement within the code or it does not work anymore.

I'm calling perform_city_actions_in_order from an outside class and from there, as you can see I am calling get_city_action_queue that should return a list of database objects. When it works, it then performs an action on the object and changes the status so it will not match the queries in get_city_action_queue anymore. But when a row in the database table is changed so that it should match the queries, get_city_action_queue does not return it anymore if I don't change a print statement within the code.

Also, if I change a print statement within perform_city_actions_in_order it also works but only once.

class CityHelper:

def get_city_action_queue(city, actions_until_time=datetime.now()):
    queue = []
    fields = city.resource_fields.all().filter(status="Under Construction", action_done_time__lte=actions_until_time) | city.resource_fields.all().filter(status="Upgrading")
    buildings = city.buildings.all().filter(status="Under Construction", action_done_time__lte=actions_until_time) | city.buildings.all().filter(status="Upgrading")
    queue.extend(fields)
    queue.extend(buildings)
    print("this needs to change between executions for the above code to work")
    return queue

def perform_city_actions_in_order(city, actions_until_time=datetime.now()):
    print("city", city)

    queue = CityHelper.get_city_action_queue(city, actions_until_time)
    print("before", queue)
    queue.sort(key = lambda x:x.action_done_time)
    print("after", queue)
    for action in queue:
        print("perfofming ", action)
        if(action.__class__.__name__ == "ResourceFieldModel"):
            ResourceHelper.upgradeResourceField(action)
        elif(action.__class__.__name__ == "BuildingModel"):
            BuildingHelper.upgradeBuilding(action)

And here are my models but these should not be the problem as the logic in the code seems to work.

class BuildingModel(models.Model):
    position = models.IntegerField(default=0)
    building_type = models.CharField(max_length=32)
    status = models.CharField(max_length=32)
    action_done_time = models.DateTimeField(default=None, null=True)
    level = models.IntegerField(default=0)
    city = models.ForeignKey(CityModel, on_delete=models.CASCADE, null=True, related_name='buildings')
    objects = BuildingManager()

    def __str__(self):
        return self.building_type + " in " + self.city.city_name + " at position: " + str(self.position)



class ResourceFieldModel(models.Model):
    position = models.IntegerField(default=0)
    field_type = models.CharField(max_length=10)
    status = models.CharField(max_length=32)
    action_done_time = models.DateTimeField(default=None, null=True, blank=True)
    level = models.IntegerField(default=0, verbose_name='level')
    city = models.ForeignKey(CityModel, on_delete=models.CASCADE, null=True, related_name='resource_fields')
    objects = ResourceFieldManager()

    def __str__(self):
        return self.field_type + " at: " + str(self.position) + " in " + self.city.city_name
Lauri
  • 61
  • 1
  • 8
  • 1
    Try printing the value of `actions_until_time` - the default value initialisation for this is only done once (this is a err feature of Python). To fix this, instead of giving a value, use default of None and within the function if action_until_time is None then initialise it to datetime.now() – DisappointedByUnaccountableMod May 22 '21 at 09:46
  • @barny Yeah thanks, that seems to have fixed it. Interesting feature to say the least... – Lauri May 22 '21 at 09:55
  • 2
    Does this answer your question? ["Least Astonishment" and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) There are also this one [Why does using `arg=None` fix Python's mutable default argument issue?](https://stackoverflow.com/questions/10676729/why-does-using-arg-none-fix-pythons-mutable-default-argument-issue), and this [article](https://web.archive.org/web/20200221224620/http://effbot.org/zone/default-values.htm) – Rodrigo Rodrigues May 22 '21 at 10:35

0 Answers0