2016-04-17 14 views
0

最近、私はTorchのDataframeパッケージの開発を支援しました。コードベースがすばやく2倍になったため、組織とフォローアップ(issue #8)を改善するために、クラスを複数のセクションに分割する必要があります。TorchクラスをLua岩のいくつかのファイルに分割する方法

簡単なテストクラスはテストパッケージのルートフォルダにtest.luaファイルのようになります。

test = torch.class('test') 
function test:__init() 
    self.data = {} 
end 

function test:a() 
    print("a") 
end 

function test:b() 
    print("b") 
end 

今、このためrockspecは、単純に次のようになります。

package = "torch-test" 
version = "0.1-1" 
source = { 
    url = "..." 
} 
description = { 
    summary = "A test class", 
    detailed = [[ 
     Just an example 
    ]], 
    license = "MIT/X11", 
    maintainer = "Jon Doe" 
} 
dependencies = { 
    "lua ~> 5.1", 
    "torch >= 7.0", 
} 
build = { 
    type = 'builtin', 
    modules = { 
     ["test"] = 'test.lua', 
    } 
} 

答えて

0

複数のファイルを1つのクラスで動作させるには、最初に作成したクラスオブジェクトを返し、それをサブセクションに渡す必要があります。上記の例では、ファイル構造に入れることができます。

\init.lua 
\main.lua 
\test-0.1-1.rockspec 
\Extensions\a.lua 
\Extensions\b.lua 

ファイルは、各.がディレクトリを意味し、.luaが取り残されている構文を「必要」によると、つまり我々はrockspecを変更する必要がluarocks install/makeコピー:

package = "torch-test" 
version = "0.1-1" 
source = { 
    url = "..." 
} 
description = { 
    summary = "A test class", 
    detailed = [[ 
     Just an example 
    ]], 
    license = "MIT/X11", 
    maintainer = "Jon Doe" 
} 
dependencies = { 
    "lua ~> 5.1", 
    "torch >= 7.0", 
} 
build = { 
    type = 'builtin', 
    modules = { 
     ["test.init"] = 'init.lua', 
     ["test.main"] = 'main.lua', 
     ["test.Extensions.a"] = 'a.lua', 
     ["test.Extensions.b"] = 'b.lua' 
    } 
    } 

上記のように、パッケージは、ファイルやサブディレクトリと一緒に常駐テスト -folderを作成します。クラスの初期化は現在、クラスオブジェクトを返しますinit.luaに常駐:

test = torch.class('test') 
function test:__init() 
    self.data = {} 
end 
return test 

サブクラス-ファイルは今(以下init.luaファイルを参照してください)ピックアップにloadfile()を使用して渡されたクラスのオブジェクトが必要です。我々はinit.luaファイルを持って一緒にすべてを接着するために

local params = {...} 
local test = params[1] 
function test:b() 
    print("b") 
end 

local params = {...} 
local test = params[1] 
function test:a() 
    print("a") 
end 

b.luaについても同様加え:a.luaは次のようになります。次は、おそらく少し過剰に複雑であるが、それはの世話をする:

利用でき、それらをロードするすべての拡張機能( 注検索
  • :はあなたがrockspecに追加する必要がありlua filesystemを必要とし、あなたはまだ、各ファイルを追加する必要がありますがまたはrockspecにそれは
  • はパスが
  • ロードパッケージなしの純粋なテスト環境で
  • ワークス
をインストールし main.luaをフォルダ識別)機能拡張フォルダにありません。 init.luaため

コード:

require 'lfs' 

local file_exists = function(name) 
    local f=io.open(name,"r") 
    if f~=nil then io.close(f) return true else return false end 
end 

-- If we're in development mode the default path should be the current 
local test_path = "./?.lua" 
local search_4_file = "Extensions/load_batch" 
if (not file_exists(string.gsub(test_path, "?", search_4_file))) then 
    -- split all paths according to ; 
    for path in string.gmatch(package.path, "[^;]+;") do 
    -- remove trailing ; 
    path = string.sub(path, 1, string.len(path) - 1) 
    if (file_exists(string.gsub(path, "?", "test/" .. search_4_file))) then 
     test_path = string.gsub(path, "?", "test/?") 
     break; 
    end 
    end 
    if (test_path == nil) then 
    error("Can't find package files in search path: " .. tostring(package.path)) 
    end 
end 

local main_file = string.gsub(test_path,"?", "main") 
local test = assert(loadfile(main_file))() 

-- Load all extensions, i.e. .lua files in Extensions directory 
ext_path = string.gsub(test_path, "[^/]+$", "") .. "Extensions/" 
for extension_file,_ in lfs.dir (ext_path) do 
    if (string.match(extension_file, "[.]lua$")) then 
    local file = ext_path .. extension_file 
    assert(loadfile(file))(test) 
    end 
end 

return test 

は、私はあなたが同じ問題に遭遇し、少しもまばらなドキュメントを見つけた場合、これは役立ちます願っています。あなたがより良い解決策を知った場合、共有してください。

関連する問題