Class: Treyja::Reader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/treyja/reader.rb

Constant Summary collapse

MAGIC_BYTES =
"XCIX"

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Reader

Returns a new instance of Reader.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/treyja/reader.rb', line 12

def initialize file = nil
  io = file ? open(file) : STDIN
  io.binmode

  @enumerator = Enumerator.new do |y|
    while magic = io.read(4)
      raise "Incorrect magic bytes" unless magic == MAGIC_BYTES
      length = io.read(8).reverse.unpack("Q").first.to_i
      y << Tensors::TensorsProto.decode(io.read(length))
    end
  end
end