python - 在主程序中收集数据,然后运行外部程序来处理收集的数据并使用结果在主程序中绘图
问题描述
我正在开发一个程序作为收集数据的用户界面,然后使用外部程序对其进行处理,最后,将结果取回以将它们绘制在 main 中。我的问题是我无法在不陷入循环的情况下使用正确的导入功能。
为简单起见,我将创建一个虚拟代码来代表我在做什么和我的问题:
主文件
import sys,os, math, statistics, re
from multiprocessing import Process,Queue,Pipe
import subprocess
'''---Data input block---'''
print('-----Welcome to mainX-----')
num1=int(input('type number 1:'))
num2=int(input('type number 2:'))
print('choose a tool between adding and multiplying')
tool=input('multiply/add: ')
'''---Data storage block---'''
class Input:
def __init__(self, point_a, point_b):
self.data_a=num1
self.data_b=num2
def data_a(self):
return(self.data_a)
def data_b(self):
return(self.data_b)
'''---external program block---'''
if tool == 'add':
Add= subprocess.run(['python',"add.py"], capture_output=True)
if tool == 'multiply':
Multiply= subprocess.run(['python',"multiply.py"], capture_output=True)
'''---Plotting block---'''
fig = plt.figure(figsize = (7,5))
ax= fig.add_subplot(111)
TK = np.linspace(1,10)
if tool == add:
ax.plot(TK, addresult, label='This is te result of addition', c='g', linewidth=3)
if tool == multiply:
ax.plot(TK, multiplyresult, label='This is te result of multiplying', c='g', linewidth=3)
ax.set_xlim(270, 400)
plt.legend(fontsize=14)
plt.tight_layout()
plt.show()
添加.py
from main import data_a
addresult:[data_a +1, data_a +2, data_a +3]
乘法.py
from main import data_b
multiplyresult: [data_b *1, data_b *2, data_b *3]
免责声明:这个虚拟代码显然存在一些错误,但我只是想说明一般流程。
我得到的错误是:
ImportError: cannot import name 'data_a' from 'main'
或者只是进入一个无限循环询问输入数据
谢谢您的帮助!
解决方案
正如@furas所建议的那样,完成这项工作的最佳方法是将输入保存在 main 中,在外部脚本中定义方法,然后将它们导入 main以将它们与输入信息一起使用。您也可以将结果保存在 main 中,然后将其用于您想要的任何内容。像谋划。
为了补充@furas编写的代码并使其功能齐全,我做了一些小的调整:
添加.py
def add(data):
return [data+1, data+2, data+3]
乘法.py
def multiply(data):
return [data*1, data*2, data*3]
主文件
import sys # PEP8: imports in separated lines
import os
import math
import statistics
import re
import numpy as np
import matplotlib.pyplot as plt
from Add import add
from Multiply import multiply
# --- classes ---
class Input:
def __init__(self, a, b):
self.a = a
self.b = b
def get_a(self):
return self.a
def get_b(self):
return self.b
# --- functions ---
def main():
# ---Data input block---
print('-----Welcome to mainX-----')
num1 = int(input('type number 1:')) # PEP8: spaces aroung `=`
num2 = int(input('type number 2:'))
print('choose a tool between adding and multiplying')
tool = input('multiply/add: ')
# ---Data storage block---
data = Input(num1, num2)
# ---external program block---
if tool == 'add':
result = add(data.get_a())
if tool == 'multiply':
result = multiply(data.get_b())
# ---Plotting block---
fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(111)
if tool == 'add':
ax.plot(result, label='This is the result of addition', c='r', linewidth=3)
if tool == 'multiply':
ax.plot(result, label='This is the result of multiplying', c='g', linewidth=3)
plt.legend(fontsize=14)
plt.tight_layout()
plt.show()
if __name__ == '__main__':
main()
推荐阅读
- c# - Xamarin.Forms:样式绑定不起作用
- module - Odoo 10:在 odoo 启动时自动安装自定义模块
- javascript - React - 从 API 调用创建对象并存储在一个大对象中
- apache - 使用 mod_authnz_ldap 公开组信息
- html - Angular Material Table - 将动态背景颜色应用于一行(Angular 2+)
- join - 如何使用实体框架结合嵌套交叉连接和左连接
- java - dropzone.js - 将目录和文件上传到 Spring MVC
- c++ - 使用带有 boost-parameter 的初始化列表
- python - 如何为每个拆分执行命令?
- pandas - 为什么我得到一个 SettingWithCopyWarning 一个值正试图在一个切片的副本上设置?