An example of hotcode reload in Python. I used this to reload a class on an instance in development without having to recreate the instance.
Last active
December 21, 2015 03:29
-
-
Save ericmoritz/6242558 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| *.pyc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Hello(object): | |
| def __init__(self, name): | |
| self.name = name | |
| def greet(self): | |
| print "Hello, " + self.name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import greeting | |
| def rewrite(): | |
| """Simulate editing a file""" | |
| source = open("greeting.py", "rb").read() | |
| source = source.replace('"Hello', '"Hola') | |
| open("greeting.py", "wb").write(source) | |
| x = greeting.Hello("Eric") | |
| x.greet() | |
| rewrite() | |
| reload(greeting) | |
| x.__class__ = greeting.Hello | |
| x.greet() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| rm -f *.pyc | |
| python main.py | |
| git checkout greeting.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment