<commit_before>require 'tempfile'

module SimpleStatsStore
  class FileDump
    def initialize(dir)
      @dir = dir
    end

    def files_contents
      contents = []
      Dir["#{@dir}/*"].each do |f|
        data = File.open(f, 'r').read
        if /\n---\n$/.match(data)
          contents << data
          File.delete(f)
        end
      end
      contents
    end

    def each(&block)
      files_contents.each &block
    end

    def write(model, data)
      i = 0
      while File.exists?(File.expand_path("sss-#{$$}-#{Time.new.to_i}-#{i}.stats", @dir))
        i += 1
      end
      File.open(File.expand_path("sss-#{$$}-#{Time.new.to_i}-#{i}.stats", @dir), 'w') do |f|
        f.puts "---"
        f.puts model<commit_msg>Catch failure to open file<commit_after>require 'tempfile'

module SimpleStatsStore
  class FileDump
    def initialize(dir)
      @dir = dir
    end

    def files_contents
      contents = []
      Dir["#{@dir}/*"].each do |f|
        begin
          data = File.open(f, 'r').read
          if /\n---\n$/.match(data)
            contents << data
            File.delete(f)
          end
        catch Error::ENOENT
          puts "Failed to open file #{f}"
        end
      end
      contents
    end

    def each(&block)
      files_contents.each &block
    end

    def write(model, data)
      i = 0
      while File.exists?(File.expand_path("sss-#{$$}-#{Time.new.to_i}-#{i}.stats", @dir))
        i += 1
      end
      File.open(File.expand_path("sss-#{$$}-#{Time.new.to_i}-#{i}.stats", @dir), 'w') do |f|
        f.puts "---"
        f.puts model
{'fraction_non_alphanumeric': 0.1638418079096045, 'fraction_numerical': 0.0025109855618330196, 'mean_word_length': 1.7818499127399652, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2214103', 'n_tokens_mistral': 552, 'n_tokens_neox': 556, 'n_words': 119}