question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Curiosity: how would I go about implementing a refresh() method on Model?

See original GitHub issue

Here’s the scoop:

The project I’m working on creates the scenario where I have local variables holding instances of a variety of Models. Changes to some have side-effects on the others (intentional ones, via signals). Right now I’m using the following to “refresh” a model instance when I want to ensure the data is up-to-date:

instance = Model.get(Model.something = something)
# ...
# .. do work
# ...
# .. need to "refresh"
instance = Model.get(Model.id == instance.id)
# .. continue

I don’t mind the additional get, but I thought it might be fun to implement a refresh() method on my base Model subclass. At my first crack, I caught myself trying this boneheaded implementation:

def refresh(self):
    self = self.__class__.get(self.__class__.id == self.id)

Obviously that’s stupid. I don’t want to change self. Anybody holding reference to that object won’t see the updates, as far as I can tell. There might be a nuance I’m missing in the Python runtime, but I’m pretty sure it wouldn’t work.

Have any ideas as to what I could try? I’d be glad to write it in such a way that it could make a PR to the project, if you think it’ll be useful for the general audience.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maaakscommented, Oct 19, 2016

I think something like this should be nice, but I in fact just wrote it now and haven’t yet tested enough. This works «in place» by loading another instance of the same class and copying all its fields. Seems to work as planned, but I am pretty sure there are some unwanted side effects somewhere. Maybe somebody has a better implementation?

def reload(self):
    newer_self = self.get(self._meta.primary_key == self._get_pk_value())
    for field_name in self._meta.fields.keys():
        val = getattr(newer_self, field_name)
        setattr(self, field_name, val)
    self._dirty.clear()
0reactions
josefdlangecommented, Feb 7, 2016

If you want to get into a really hairy territory, you could go so far as to write some kind of code that takes in the remote updated version and merges it with the local object, and if it’s trying to clobber a locally-dirty field, raise some kind of exception, etc, but then again this is becoming more not-very-Pythonic the more we explore the possibilities.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Checkpoint Deep Learning Models in Keras
In this post, you will discover how to checkpoint your deep learning models during training in Python using the Keras library.
Read more >
Algorithms — Ray 2.2.0 - the Ray documentation
RLlib's MAML implementation is a meta-learning method for learning and quick adaptation across different tasks for continuous control. Code here is adapted from ......
Read more >
How to reload/refresh model from database in Laravel?
refresh() is a mutable operation: It will reload the current model instance from the database.
Read more >
Class Agent | ML Agents | 1.0.8 - Unity - Manual
When an episode ends and a new one begins, the Agent object's OnEpisodeBegin() function is called. You can implement OnEpisodeBegin() to reset ...
Read more >
Keras: Starting, stopping, and resuming training
Using this method of training you'll often be able to improve your model accuracy. Let's go ahead and get started! Why do we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found