def new_log(username): with open('log.txt', 'r+') as reader: # file object for reading and writing print("Enter New Journal") content = reader.read() # getting file content into a single object journal = input() # journal string is being entered by the user L = Log(journal) # making an object of Log class from data.py file j = L.get_message() # getting the journal with timestamp reader.seek(0, 0) reader.write(username + "$" + j + "\n" + content) # writing the journal to file print("New Log Successfully Written")
def run(self): session = Session() last_id = Log.get_last_id(session, simulate_time=self.simulate_time) if last_id is None: last_id = 0 last_id -= BUFFER_LEN time_delta = None if self.simulate_time: time_delta = monotonic_time() - self.simulate_time while not self.closing: query = session.query(Log).filter(Log.id > last_id).filter(Log.interesting == True).order_by(Log.id) if time_delta is not None: log2 = session.query(Log).filter(Log.interesting == True).filter(Log.timestamp <= monotonic_time() - time_delta).order_by(Log.timestamp.desc()).first() query = query.filter(Log.id <= log2.id) new_data = query.all() if len(new_data) > 0: last_id = new_data[-1].id #print >> sys.stderr, "Read %d records" % (len(new_data)) self.buffer += new_data self.buffer = self.buffer[-BUFFER_LEN:] session.expunge_all() session.rollback() time.sleep(SLEEP_TIME) session.rollback() session.close()
if __name__ == '__main__': session = Session() retime = 'retime' in sys.argv[1:] last_commit = time.time() last_interesting = 0.0 try: while True: line = sys.stdin.readline() if line == '': break data = [my_float(x) for x in line.strip().split(",")] if retime: data[0] = time.time() timestamp = float(data[0]) log = Log.from_tuple(data) if timestamp - last_interesting >= 1.0 / INTERESTING_FPS: log.interesting = True last_interesting = timestamp session.add(log) session.flush() now = time.time() if now - last_commit >= COMMIT_FREQ: last_commit = now # After the implicit flush, all the objects in the # session are weakly references by the session, so # they're immediately pruned; i.e., this shouldn't # have memory leaks session.commit() except KeyboardInterrupt: