<commit_before># Copyright (C) 2008 <NAME> <<EMAIL>>
# Licensed under the Academic Free License version 3.0

module Amazing

  # Parse a /proc file
  #
  #   cpuinfo = ProcFile.parse_file("cpuinfo")
  #   cpuinfo[1]["model name"]
  #   #=> "AMD Turion(tm) 64 X2 Mobile Technology TL-50"
  class ProcFile
    include Enumerable

    def self.parse_file(file)
      file = "/proc/#{file}" if file[0] != ?/
      new(File.new(file))
    end

    def initialize(string_or_io)
      case string_or_io
      when String
        content = string_or_io
      when IO
        content = string_or_io.read
      end
      @list = [{}]
      content.each_line do |line|
        if sep = line.index(":")
          @list[-1][line[0..sep-1].strip] = line[sep+1..-1].strip
        else
          @list << {}
        end
      end
      @list.pop if @list[-1].empty?
    end

    def each
      @list.each do |section|
        yield section
      end
    end

    def [](section)
      @list[section]
    end
  end
end<commit_msg>Close IO after reading in ProcFile<commit_after># Copyright (C) 2008 <NAME> <<EMAIL>>
# Licensed under the Academic Free License version 3.0

module Amazing

  # Parse a /proc file
  #
  #   cpuinfo = ProcFile.parse_file("cpuinfo")
  #   cpuinfo[1]["model name"]
  #   #=> "AMD Turion(tm) 64 X2 Mobile Technology TL-50"
  class ProcFile
    include Enumerable

    def self.parse_file(file)
      file = "/proc/#{file}" if file[0] != ?/
      new(File.new(file))
    end

    def initialize(string_or_io)
      case string_or_io
      when String
        content = string_or_io
      when IO
        content = string_or_io.read
        string_or_io.close
      end
      @list = [{}]
      content.each_line do |line|
        if sep = line.index(":")
          @list[-1][line[0..sep-1].strip] = line[sep+1..-1].strip
        else
          @list << {}
        end
      end
      @list.pop if @list[-1].empty?
    end

    def each
      @list.each do |section|
        yield section
      end
    end

    def [](section)
      @list[section]
    end
  end
end
{'fraction_non_alphanumeric': 0.12295081967213115, 'fraction_numerical': 0.01832208293153327, 'mean_word_length': 1.9899135446685878, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, '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': '3677263', 'n_tokens_mistral': 731, 'n_tokens_neox': 689, 'n_words': 181}