-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
latentvector
committed
Apr 17, 2024
1 parent
7f5f17c
commit a346ead
Showing
26 changed files
with
1,168 additions
and
739 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import commune as c | ||
import os | ||
|
||
class History(c.Module): | ||
def __init__(self, folder_path='history'): | ||
self.folder_path = self.resolve_path(folder_path) | ||
|
||
|
||
|
||
def set_folder_path(self, path): | ||
self.folder_path = self.resolve_path(path) | ||
assert os.path.isdir(self.folder_path), f"History path {self.folder_path} does not exist" | ||
c.print(f"History path: {self.folder_path}", color='green') | ||
|
||
def add(self, item:dict, path=None): | ||
if 'timestamp' not in item: | ||
item['timestamp'] = c.timestamp() | ||
path = path or (self.folder_path + '/' + str(item['timestamp'])) | ||
return self.put(path, item) | ||
|
||
def paths(self, key=None, max_age=None): | ||
files = [] | ||
current_timestamp = c.timestamp() | ||
for file in c.ls(self.folder_path): | ||
timestamp = self.get_file_timestamp(file) | ||
if max_age and current_timestamp - timestamp > max_age: | ||
continue | ||
files.append(file) | ||
return files | ||
|
||
def get_file_timestamp(self, file): | ||
return int(file.split('/')[-1].split('.')[0]) | ||
|
||
def history_paths(self, search=None, n=1000, reverse=False): | ||
paths = self.ls(self.folder_path) | ||
sorted_paths = sorted(paths, reverse=reverse) | ||
if search: | ||
sorted_paths = [p for p in sorted_paths if search in p] | ||
return sorted_paths[:n] | ||
|
||
def history(self, search=None, n=100, reverse=True, idx=None): | ||
history_paths = self.history_paths(n=n, reverse=reverse, search=search) | ||
history = [c.get(s) for s in history_paths] | ||
if idx: | ||
return history[idx] | ||
return history | ||
|
||
def last_n(self, n=1): | ||
return self.history(n=n) | ||
|
||
|
||
|
||
|
This file contains 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
Oops, something went wrong.