Sha256: ce2d1a51f5f6aa37401435bfe03d47b01933934be4e6cf27d0d153458e26b5dd

Contents?: true

Size: 1.47 KB

Versions: 11

Compression:

Stored size: 1.47 KB

Contents

module Torch
  module Utils
    module Data
      module DataPipes
        module Iter
          class FileOpener < IterDataPipe
            def initialize(datapipe, mode: "r", encoding: nil, length: -1)
              super()
              @datapipe = datapipe
              @mode = mode
              @encoding = encoding

              if !["b", "t", "rb", "rt", "r"].include?(@mode)
                raise ArgumentError, "Invalid mode #{mode}"
              end

              if mode.include?("b") && !encoding.nil?
                raise ArgumentError, "binary mode doesn't take an encoding argument"
              end

              @length = length
            end

            def each(&block)
              get_file_binaries_from_pathnames(@datapipe, @mode, encoding: @encoding, &block)
            end

            private

            def get_file_binaries_from_pathnames(pathnames, mode, encoding: nil)
              if !pathnames.is_a?(Enumerable)
                pathnames = [pathnames]
              end

              if ["b", "t"].include?(mode)
                mode = "r#{mode}"
              end

              pathnames.each do |pathname|
                if !pathname.is_a?(String)
                  raise TypeError, "Expected string type for pathname, but got #{pathname.class.name}"
                end
                yield pathname, StreamWrapper.new(File.open(pathname, mode, encoding: encoding))
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
torch-rb-0.18.0 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.17.1 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.17.0 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.16.0 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.15.0 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.14.1 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.14.0 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.13.2 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.13.1 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.13.0 lib/torch/utils/data/data_pipes/iter/file_opener.rb
torch-rb-0.12.2 lib/torch/utils/data/data_pipes/iter/file_opener.rb