如何在python中使用google / gin-config?

时间:2018-09-20 23:42:27

标签: python

首先,我已经按照项目站点上的说明进行操作,但仍然不知道如何使用它。 我的文件夹结构如下:

Folder
|________use_gin.py
     |___config.gin
     |___test.py

use_gin.py内部:

import gin   

@gin.configurable
def my_other_func(a, b, c):
  return a, b, c

config.gin内部:

my_other_func.a = -2.9
my_other_func.b = 9.3

# Comments!
my_other_func.c = 'Oh, Dear.'

并且我使用test.py来调用my_other_func函数,但它给出了错误:

C:/Anaconda3/envs/py3/python.exe d:/Folder/test.py
Traceback (most recent call last):
  File "d:/Folder/test.py", line 3, in <module>
    my_other_func()
  File "C:\Anaconda3\Anaconda3\envs\py3tf\lib\site-packages\gin\config.py", line 1032, in wrapper
    utils.augment_exception_message_and_reraise(e, err_str)
  File "C:\Anaconda3\Anaconda3\envs\py3tf\lib\site-packages\gin\utils.py", line 48, in augment_exception_message_and_reraise
    six.raise_from(proxy.with_traceback(exception.__traceback__), None)
  File "<string>", line 3, in raise_from
  File "C:\Anaconda3\Anaconda3\envs\py3tf\lib\site-packages\gin\config.py", line 1009, in wrapper
    return fn(*new_args, **new_kwargs)
TypeError: my_other_func() missing 3 required positional arguments: 'a', 'b', and 'c'
  No values supplied by Gin or caller for arguments: ['a', 'b', 'c']
  Gin had values bound for: []
  Caller supplied values for: []
  In call to configurable 'my_other_func' (<function my_other_func at 0x00000000029C8268>)

我的设置有问题吗?

2 个答案:

答案 0 :(得分:1)

您只应在使用my_other_func()

之前在test.py中进行操作
import gin
gin.parse_config_file('config.gin')    

阅读here以获得更多详细信息。

答案 1 :(得分:0)

congig.gin

import use_gin
my_other_func.a = -2.9
my_other_func.b = 9.3

# Comments!
my_other_func.c = 'Oh, Dear.'

test.py

import gin
gin.parse_config_file('config.gin')
import use_gin
print(use_gin.my_other_func())

use_gin.py

import gin
@gin.configurable
def my_other_func(a, b, c):
  return a, b, c