简体   繁体   English

下标问题(脚本实例化并从另一个脚本调用)只运行被调用的 function,而不是整个脚本

[英]Issue with subscript (script instantiated and called from another script) only running the called function, but not the entire script

Goal: From the superscript, per each iteration of the for loop, send a list to a function in the subscript, update that list, exit the function in the subscript, and perform several actions, then onto the next iteration.目标:从上标开始,for 循环的每次迭代,将列表发送到下标中的 function,更新该列表,退出下标中的 function,并执行几个操作,然后进入下一次迭代。

Issue: Only the function I call from the superscript is run and not the entire subscript.问题:只有我从上标调用的 function 运行,而不是整个下标。

CrossValidationSmall.py (superscript) CrossValidationSmall.py(上标)

import subfileTest

data = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K']
n = 2

for start in range(0, len(data), n):
    stop = start + n
    test = data[start: stop]
    train = data[:start] + data[stop:]
    subfileTest.set_train_data(train)

subfileTest.py (subscript) subfileTest.py(下标)

train_data = []

def set_train_data(train):
    global train_data
    train_data = train

print(train_data) #should output the lists each time subfileTest is called.

Expected Output预计 Output

MacBook-Pro:SmallerEnviromentTest Me$ python CrossValidationSmall.py
['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
['A', 'B', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
['A', 'B', 'C', 'D', 'G', 'H', 'I', 'J', 'K']
['A', 'B', 'C', 'D', 'E', 'F', 'I', 'J', 'K']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']

Current Output当前 Output

MacBook-Pro:SmallerEnviromentTest Me$ python CrossValidationSmall.py
[]

From what I understood, you need your subscript to be called in entirety on every iteration.据我了解,您需要在每次迭代时完整调用您的下标。

Here's the thing:事情是这样的:

  • A new module once found is executed and module from the initial execution is cached .一个新的模块一旦被发现就会被执行,并且来自初始执行的模块被缓存
  • When the same module is imported in a different file, the cached version is returned.当在不同的文件中导入相同的模块时,将返回缓存的版本。
  • This cached version lives throughout the lifetime of the process.此缓存版本在进程的整个生命周期内都存在。

Since you need you subscript to be run fresh on each iteration, a Class can be used.由于您需要在每次迭代时重新运行下标,因此可以使用 Class。 an object of the class to: class 的 object 至:

  • Store data from each iteration存储每次迭代的数据
  • Write any number of functions that work with this data编写任意数量的函数来处理这些数据
  • Retain values from each object as long as you need根据需要保留每个 object 的值

Superscript :上标

from subscript import ModelCrossValidator

trial = 0

data = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
n = 2

for start in range(0, len(data), n):
    trial += 1

    stop = start + n
    test = data[start: stop]

    # train_subset to be used for cross validation
    train_subset = data[:start] + data[stop:]

    cross_validator = ModelCrossValidator(train_subset)
    cross_val_score = cross_validator.calc_score()

print("CrossValSet: {}, Data: {}, Score: {:.2}".format(trial, cross_validator.data, cross_val_score))

Subscript :下标

import random

class ModelCrossValidator:

    def __init__(self, train_subset):
        self.data = train_subset     # your folded cross-val data
        self.model = None            # can pass in a model too here
        self.accuracy = 0            # other fields that you might need

    def calc_score(self):
        # you can have any number of functions like this.
        # all will deal with only the data from a single object.

        self.accuracy = 0.8 + (random.random()) % 0.2
        return self.accuracy

Output: Output:

Macbook-Pro: chimichanga$ python superscript.py 
CrossValSet: 1, Data: ['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'], Score: 0.89
CrossValSet: 2, Data: ['A', 'B', 'E', 'F', 'G', 'H', 'I', 'J', 'K'], Score: 0.94
CrossValSet: 3, Data: ['A', 'B', 'C', 'D', 'G', 'H', 'I', 'J', 'K'], Score: 0.9
CrossValSet: 4, Data: ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'J', 'K'], Score: 0.84
CrossValSet: 5, Data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K'], Score: 0.88
CrossValSet: 6, Data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], Score: 0.94

暂无
暂无

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

相关问题 如何检查是从终端还是从另一个脚本调用脚本 - How to check if a script is being called from terminal or from another script 如何打印另一个Tkinter脚本中调用的gui脚本的输出? - How to print output from a script in gui called in another Tkinter script? 从cron调用的scrapy脚本仅调用了构造函数 - scrapy script called from cron only constructor called 中断在 tkinter 中另一个脚本中调用的脚本 - Interrupting a script called within another script in tkinter Python:登录模块脚本以在另一个脚本中调用 - Python : Logging in module script to be called in another script 更新从其他脚本调用的 python 脚本 - Update python script called from other script 如何停止导入的python脚本运行,但仅在代码中调用时运行 - How to stop imported python script from running , but only runs when called in code 为什么从PHP调用的长时间运行的Python脚本失败 - Why does a long running Python script called from PHP fail 当使用 eval() 从 php 脚本调用脚本以及从 bash 脚本调用相同的函数时,python 如何使用路径? - How does python use paths when a script is called using eval() from a php script and when the same function is called from a bash script? 如何将变量从主脚本导入到辅助脚本的功能中,该功能由主脚本调用 - How to import variable from a primary script into a function of a secondary script, which is called by the primary script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM