简体   繁体   English

python 3.x joblib 简单的保存功能

[英]python 3.x joblib simple save function

I'm trying to create a simple joblib function, which will evaluate the expression and pickle the result, while checking for the existence of the pickle file.我正在尝试创建一个简单的 joblib 函数,该函数将评估表达式并pickle 结果,同时检查pickle 文件是否存在。 But when I put this function in some other file and import the function after adding the path of the file to sys.path.但是当我将此函数放在其他文件中并在将文件路径添加到 sys.path 后导入该函数时。 I get errors.我收到错误。

from pathlib import Path
import joblib as jl    
def saveobj(filename, expression_obj,ignore_file = False):
    fname = Path(filename)
    if fname.exists() and not ignore_file:
        obj = jl.load(filename)
    else:
        obj = eval(expression_obj)
        jl.dump(obj,fname,compress = True)        
    return obj

Sample call:示例调用:

rf_clf = saveobj(file, "rnd_cv.fit(X_train, np.ravel(y_train))", ignore_file=True)

Error:错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-02c2cae43c5d> in <module>
      1 file = Path("rf.pickle")
----> 2 rf_clf = saveobj(file, "rnd_cv.fit(X_train, np.ravel(y_train))", ignore_file=True)

~/Dropbox/myfnlib/util_funs.py in saveobj(filename, expression_obj, ignore_file)
     37         obj = jl.load(filename)
     38     else:
---> 39         obj = eval(expression_obj)
     40         jl.dump(obj,fname,compress = True)
     41     return obj

~/Dropbox/myfnlib/util_funs.py in <module>

NameError: name 'rnd_cv' is not defined

I guess, python needs to evaluate the function locally, but since the objects don't exist in that scope, it is throwing this error.我想,python 需要在本地评估该函数,但是由于该范围内不存在对象,所以它抛出了这个错误。 Is there a better way of doing this.有没有更好的方法来做到这一点。 I need to do this repeatedly, that's why a function.我需要重复执行此操作,这就是函数的原因。 Thanks a lot for your help.非常感谢你的帮助。

You can check the documentation of eval :您可以查看eval的文档:

Help on built-in function eval in module builtins:模块 builtins 中内置函数 eval 的帮助:

eval(source, globals=None, locals=None, /) eval(source, globals=None, locals=None, /)

 Evaluate the given source in the context of globals and locals. The source may be a string representing a Python expression or a code object as returned by compile(). The globals must be a dictionary and locals can be any mapping, defaulting to the current globals and locals. If only globals is given, locals defaults to it.

It has arguments for global and local variables.它具有全局和局部变量的参数。 So, in your case, you can:因此,在您的情况下,您可以:

from pathlib import Path
import joblib as jl    
def saveobj(filename, expression_obj,global,local,ignore_file = False):
    fname = Path(filename)
    if fname.exists() and not ignore_file:
        obj = jl.load(filename)
    else:
        obj = eval(expression_obj, global, local)
        jl.dump(obj,fname,compress = True)        
    return obj

The code can be changed to:代码可以改为:

rf_clf = saveobj(file, "rnd_cv.fit(X_train, np.ravel(y_train))", globals(), locals(), ignore_file=True)

I was about to post answer my own question, when I saw @youkaichao answer.当我看到@youkaichao 的回答时,我正要发布回答我自己的问题。 Thanks a lot.非常感谢。 One more way to skin the cat: (although limited to keyword arguments)另一种给猫剥皮的方法:(虽然仅限于关键字参数)

def saveobj(filename,func, ignore_file = False, **kwargs):
    fname = Path(filename)
    if fname.exists() and not ignore_file:
        obj = jl.load(filename)
    else:
        obj = func(**kwargs)
        jl.dump(obj,fname,compress = True)        
    return obj

Changed Call:更改电话:

file = Path("rf.pickle")
rf_clf = saveobj(file, rnd_cv.fit, ignore_file=False, X=X_train, y= np.ravel(y_train))

Although, I would still love to know, which one is better.虽然,我仍然很想知道,哪个更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM