Last active
March 29, 2016 17:38
-
-
Save riccardomarotti/52d6f699677050b7540a to your computer and use it in GitHub Desktop.
A reactive approach to monitor a file changes
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 rx | |
from rx import Observable | |
import os | |
def check_changes(previous, cur): | |
current_modify_date = -1 | |
filename = previous['filename'] | |
if os.path.isfile(filename): | |
current_modify_date = os.path.getmtime(filename) | |
return { | |
'previous_modify_date': current_modify_date, | |
'changed': previous['previous_modify_date'] != current_modify_date, | |
'filename': filename | |
} | |
def file_changed_observable(file_name): | |
return Observable.interval(100).scan(check_changes, | |
seed={'previous_modify_date': -1, | |
'changed': False, | |
'filename': file_name}).filter(lambda val: val['changed']) | |
file_changed = file_changed_observable('test.txt') | |
file_changed.subscribe(print) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment