Skip to content

Instantly share code, notes, and snippets.

@riccardomarotti
Last active March 29, 2016 17:38
Show Gist options
  • Save riccardomarotti/52d6f699677050b7540a to your computer and use it in GitHub Desktop.
Save riccardomarotti/52d6f699677050b7540a to your computer and use it in GitHub Desktop.
A reactive approach to monitor a file changes
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