GithubHelp home page GithubHelp logo

Comments (10)

Brobin avatar Brobin commented on May 28, 2024

One issue I think I will run into is having a very large file (thousands of links). Perhaps links older than 1 month are not saved

from rtv.

Brobin avatar Brobin commented on May 28, 2024

Also, I'm not too sure about log file standards. Would it be okay to put it in .config/rtv/history.log?

from rtv.

michael-lazar avatar michael-lazar commented on May 28, 2024

From what I can find it either needs to go in XDG_CACHE_HOME or XDG_DATA_HOME. It's unclear to me which one is correct, but I would lean towards CACHE because it's non-essential data. I would also name the file history.txt because it's not a standard log in the sense that it will be consumed and overwritten by the program.

Using timestamps for each link sounds like a lot of overhead, it might be better to just save the most recent N links.

from rtv.

Brobin avatar Brobin commented on May 28, 2024

To save the most recent N links, we would have to use some sort of ordered list. A set is unordered so restricting the size of it when writing would not guarantee that you would get the last N links, just N random links.

Would you be opposed to refactoring to use a list instead? That way we could just slice the end of the list for the output file.

Also, is 1000 a reasonable number of links to save?

from rtv.

michael-lazar avatar michael-lazar commented on May 28, 2024

I used a set because testing for membership is O(1) as opposed to lists which are O(N). I don't think going back to a list is a good idea. Also think about how you're going to write to this file. If you only write when the program is quit via q, you will be losing history on ctrl-c.

Here's one approach that uses file appends to avoid having to overwrite the whole file after each time. There may be other approaches as well.

  1. Read the file in as a list.
  2. If the list length is over N, overwrite the file with only the most recent N links.
  3. Convert the list to a set
  4. When a new link is clicked, add it to the set AND append the link directly to the end of the file.

As for the number of links, I think 100-200 would probably be enough.

from rtv.

Brobin avatar Brobin commented on May 28, 2024

Good point about the complexity. I like this approach. It has lot of extra file operations, but I guess that's necessary to not lose history if you quit with ctrl+c.

from rtv.

noahmorrison avatar noahmorrison commented on May 28, 2024

As for the ctrl+c problem, why not use atexit?

import atexit
import time

@atexit.register
def save():
    print('saving')

if __name__ == '__main__':
    time.sleep(2)
    print('done')

run this, no matter what (ctrl+c, or normal exit), "saving" will always be printed.

from rtv.

Brobin avatar Brobin commented on May 28, 2024

Didn't know that module existed! Sounds good to me. If we use that, I think it would be best to do the file I/O at exit instead of when you visit each link. It would keep the code cleaner.

from rtv.

Brobin avatar Brobin commented on May 28, 2024

I've got an implementation working. @michael-lazar, it is very similar to how you described it, except it uses the a@atexit decorator to do one file operation at exit instead of loading and writing to the file all of the time. See #95

from rtv.

michael-lazar avatar michael-lazar commented on May 28, 2024

Merged in #95

from rtv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.