简体   繁体   English

自动化现有的python程序

[英]Automating already existing python program

So I have a program that takes the .txt data (set of integer numbers), makes a sum every n numbers (divisions), and puts that result in a new list. 因此,我有一个程序可以接收.txt数据(整数集),每n个数字(除法)求和,然后将结果放入新列表中。 And then it makes a bar chart of that list. 然后制作该列表的条形图。

And so far it works just fine. 到目前为止,它工作正常。 The only problem is that I'd like it to make the program does all this without me having to manually input the number of divisions I'd like it to make :\\ 唯一的问题是,我希望它使程序执行所有这些操作,而无需手动输入我希望它进行的除法数量:\\

I'd like it to go through lets say a loop, that will put division to 10, 11, 12... to any number I like, and then put it all in a list, and I'd end up with multiple lists, that I can then transfer to excell, origin, or any other program, and do my analysis there. 我希望它经过一个循环,将除法运算除以10、11、12 ...到我喜欢的任何数字,然后将其全部放入一个列表中,最后得到多个列表,然后我可以将其转移到excell,origin或任何其他程序,并在那里进行分析。

I'll put the program, without the plotting part, and some extra things I've already asked about this: 我将把程序(不包括绘图部分)和一些我已经询问过的额外内容放进去:

# -*- coding: cp1250 -*-
from __future__ import division
from numpy import *
from matplotlib import rc
from matplotlib.pyplot import *
import numpy as np
import matplotlib.pyplot as plt


data = loadtxt("mion-090513-1.txt", int)

nuz = len(data)
nsmp = 10
duz = int(nuz/nsmp)

L = []

ukupni_broj=sum(data)

#Summed values calculation#
for i1 in range(0,nsmp):
    suma = 0
    for i2 in range(0,duz):
        suma += data[i1*duz+i2]
    L.append(suma)

print L

print 'Bin number is', len(L)

print 'Total event number is', ukupni_broj

So I'd basically like to have nsmp in a for loop, from some values to some values (for instance from 10-15, 20-50 in a 25 interval step, and so on). 因此,我基本上希望在for循环中使用nsmp,从某些值到某些值(例如,从10-15开始,以25个间隔的步长从20-50开始,等等)。

Is that thing doable? 那东西可行吗?

Also is there an easy way of exporting results in python? 还有在python中导出结果的简单方法吗? I searched all over but found nothing easy like loadtxt . loadtxt搜索,但没有发现像loadtxt这样的loadtxt

Here's the .txt file: https://dl.dropboxusercontent.com/u/55620972/mion-090513-1.txt 这是.txt文件: https://dl.dropboxusercontent.com/u/55620972/mion-090513-1.txt : https://dl.dropboxusercontent.com/u/55620972/mion-090513-1.txt

To make your program easy to automate and to be reusable, you can make a couple of things. 为了使程序易于自动化和可重用,您可以做两件事。

Separate your core functionality (eg, computations) from execution. 将核心功能(例如,计算)与执行分开。 You can put the computation into a separate module and execute it form the "main" script: 您可以将计算放到一个单独的模块中,然后通过“ main”脚本执行它:

# my_processing.py
def process_file(filename, divisions):
   ...

# process_all.py:
import my_processing
filename = "..."
divisions = [10, 20, 100]
my_processing.process_file(filename, divisions)

This way you can reuse my_processing.py . 这样,您可以重用my_processing.py Also, it is a very good habit to include main clause 另外,包含main句也是一个很好的习惯

def main():
    ....

if __name__ == "__main__"
    main()

If you are designing a tool, you should also define command line arguments - argparse is perfect for that. 如果要设计工具,还应该定义命令行参数argparse非常适合。 Then you can easily use it from shell scripts. 然后,您可以从Shell脚本轻松使用它。

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

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