问题是5这段python程序打包成exe哪里有问题

Shell执行python程序的缓冲区问题
环境:Linux
一段执行时间很长的Python程序在linux后台执行,把结果输出到某文件:
cmd='python
'$1' '$2' '$3' '$5' '$4
RESULT=eval $cmd
echo $RESULT
注:1,2,3,4,5为系统参数,指向Python文件及向Python传入的参数。
遇到的问题,程序没报错,echo却没有任何输出。
按照前一篇博客的方法可以解决这个问题,但是需要修改原有Python程序。现在提供一种新的解决方式:
修改shell如下:
cmd='python -u '$1' '$2' '$3' '$5' '$4
RESULT=eval $cmd
echo $RESULT
两者的区别是在python命令之后添加-u参数。
很多人对于python命令的参数不太清楚,其实可以使用man python命令查看:
python - an interpreted, interactive, object-oriented programming language
python [ -B ] [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ]
[ -O ] [ -O0 ] [ -R ] [ -Q argument ] [ -s ] [ -S ] [ -t ] [ -u ]
[ -v ] [ -V ] [ -W argument ] [ -x ] [ -3 ] [ -?
[ -c command | script | - ] [ arguments ]
DESCRIPTION
interpreted,
interactive,
object-oriented programming language that combines remarkable power with very clear syntax.
For an introduction to programming in Python you are referred to the Python Tutorial.
Python Library Reference documents built-in and standard types, constants, functions and modules.
Finally, the Python Reference Manual describes the syntax and semantics of the core
(These documents may be located via the INTERNET RESOURCES they may be installed on your system as well.)
be extended with your own modules written in C or C++.
On most systems such modules may be dynamically loaded.
Python is also adaptable as an extension language for existing applications.
internal documentation for hints.
Documentation for installed Python modules and packages can be viewed by running the pydoc program.
COMMAND LINE OPTIONS
Don鈚 write .py[co] files on import. See also PYTHONDONTWRITEBYTECODE.
-c command
Specify the command to execute (see next section).
This terminates the option list (following options are passed as arguments to the command).
Turn on parser debugging output (for wizards only, depending on compilation options).
Ignore environment variables like PYTHONPATH and PYTHONHOME that modify the behavior of the interpreter.
Prints the usage for the interpreter executable and exits.
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command.
It does not read the $PYTHONSTARTUP file.
This can be useful to inspect global vari-
ables or a stack trace when a script raises an exception.
-m module-name
Searches sys.path for the named module and runs the corresponding .py file as a script.
Turn on basic optimizations.
This changes the filename extension for compiled (bytecode) files from .pyc to .pyo.
Given twice, causes docstrings to be discarded.
Discard docstrings in addition to the -O optimizations.
on "hash randomization", so that the hash() values of str, unicode, buffer and datetime objects are "salted" with an unpredictable pseudo-random value. Although they remain constant within an individual Python pro-
cess, they are not predictable between repeated invocations of Python.
This is intended to provide protection against a denial of service caused by carefully-chosen inputs that exploit the worst case performance of a dict
construction,
complexity.
http://www.ocert.org/advi-
sories/ocert-2011-003.html for details.
-Q argument
D see PEP 238.
The argument must be one of "old" (the default, int/int and long/long return an int or long), "new" (new division semantics, i.e. int/int and long/long returns a float), "warn" (old divi-
sion semantics with a warning for int/int and long/long), or "warnall" (old division semantics with a warning for all use of the division operator).
For a use of "warnall", see the Tools/scripts/fixdiv.py script.
Don鈚 add user site directory to sys.path.
Disable the import of the module site and the site-dependent manipulations of sys.path that it entails.
Issue a warning when a source file mixes tabs and spaces for indentation in a way that makes it depend on the worth of a tab expressed in spaces.
Issue an error when the option is given twice.
Force stdin, stdout and stderr to be totally unbuffered.
On systems where it matters, also put stdin, stdout and stderr in binary mode.
Note that there is internal buffering in xreadlines(), readlines() and file-object
iterators ("for line in sys.stdin") which is not influenced by this option.
To work around this, you will want to use "sys.stdin.readline()" inside a "while 1:" loop.
message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded.
When given twice, print a message for each file that is checked for when searching for a mod-
Also provides information on module cleanup at exit.
Prints the Python version number of the executable and exits.
-W argument
Warning control.
Python sometimes prints warning message to sys.stderr.
A typical warning message has the following form: file:line: category: message.
By default, each warning is printed once
option controls how often warnings are printed.
Multiple -W
when a warning matches more than one option, the action for the last matching option is performed.
Invalid -W
options are ignored (a warning message is printed about invalid options when the first warning is issued).
Warnings can also be controlled from within a Python program using the warnings module.
The simplest form of argument is one of the following action strings (or a unique abbreviation): ignore to default to explicitly request the default behavior (printing each warning
to print a warning each time it occurs (this may generate many messages if a warning is triggered repeatedly for the same source line, such as inside a loop); module to print each warning only the first time
it occurs in each once to print each warning only the first time it occurs in the or error to raise an exception instead of printing a warning message.
The full form of argument is action:message:category:module:line.
Here, action is as explained above but only applies to messages that match the remaining fields.
The message field matches the start of the war this match is case-insensitive.
The category field matches the warning category.
This must be a the match test whether
the actual warning category of the message is a subclass of the specified warning category.
The full class name must be given.
The module field matches the (fully-qualified) this match
case-sensitive.
The line field matches the line number, where zero matches all line numbers and is thus equivalent to an omitted line number.
Skip the first line of the source.
This is intended for a DOS specific hack only.
Warning: the line numbers in error messages will be off by one!
Warn about Python 3.x incompatibilities that 2to3 cannot trivially fix.
-u的意思是: 强制stdin,stdout和stderr为无缓冲模式,而对于xreadlines(),readlines()和文件对象迭代器中有内部缓冲,不受此项影响。
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!python编程输出1/1+1/3+1/5...1/99的和_百度知道
python编程输出1/1+1/3+1/5...1/99的和
python编程输出1/1+1/3+1/5...1/99的和
我有更好的答案
#coding=utf-8def&mysum(num):&&&&sum=0&&&&for&i&in&range(num):&&&&&&&&if(i%2)&==&1:&&&&&&&&&&&&sum=sum+1/i&&&&return&sumif&__name__=='__main__':&&&&res=mysum(100)&&&&print(res)
采纳率:100%
result=0for&i&in&range(100):&&&&if(i%2)==1:&&&&&&&&result+=1/iprint(result)
完整的编写包括结果
这个是python3.6的代码,这个就是可以运行的完整代码,运行结果如下:2.907
本回答被网友采纳
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。问题是5这段python程序哪里有问题_百度知道
问题是5这段python程序哪里有问题
我有更好的答案
你的dict里面申明的keys都是大写,你的if里面全部用小写。
为您推荐:
其他类似问题
您可能关注的内容
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。【深度剖析HMM(附Python代码)】5. 用HMM解决三个实际问题
经过上几节的学习,相信大家对HMM都有了比较深的了解,这一节我们通过几个实际例子(丢骰子问题、中文分词问题及股票预测问题)来进一步讲解HMM
1. 丢骰子问题
具体代码参见:Dice_01.py
丢骰子问题描述:
假设六面骰、四面骰和八面骰各三枚,每次丢一枚骰子,记录骰子朝上的数字。多次丢骰子后,得到了一串数字,问:
A.求每次丢的骰子的种类(问题1)
B.丢出该串数字的概率(问题2)
C.下次丢骰子最有可能的数字(问题3)
问题分析:
已知条件:
A. 观测值:骰子朝上的数字x
B. 隐状态:抛掷的骰子种类z
C. 隐状态初始概率:
D. 隐状态转换概率:
E.发射概率为:
假设掷15次骰子的结果为1 6 3 5 2 7 3 5 2 4 3 6 1 5 4
问题A:通过解码问题完成(decode函数),其实我们一眼就能知道结果了
Dice_01.py运行结果:这里的0,1,2分别表示六面骰、四面骰和八面骰
问题B:丢出该结果的概率(X_prob函数)
运行结果,已对概率做了自然对数变换
问题C:predict函数解决
这里分别表示下一状态各骰子丢出各个数字的概率,可以看出最有可能的数字为1,2,3,4。
2. 中文分词问题
具体代码参见:Wordseg_02.py
中文分词问题描述:
假设有一中文句子,而该中文句子的每个字在该句中都有BEMS共四种状态,其中B代表该字是某词语中的起始字,M代表是某词语中的中间字,E代表是某词语中的结束字,S则代表是单字成词。中文分词问题是根据某中文句子得到其每个字的状态。比如:
“我在家里吃饭”——》(分词)“我|在|家里|吃中饭”——》(状态)“SSBEBME”
问题分析:
已知一系列的中文句子(序列)X以及句中每个字的状态(隐状态)Z.
我们将中文的每个字表示为字典里的序号,而每个字在序列中都有4个状态,由此将中文分词问题转换为离散HMM模型。
但是相较于问题1,问题2中并不是单个长序列,而一系列短序列,为了训练一系列短序列,我们需要对原训练方法进行改进。
最简单的思路,是将所有的短序列合并成一个长序列,但是如此以来无法训练出初始状态参数。
因此我们建立一个批量的HMM训练方法,此时我们需要重新改写参数优化的最大似然式:
可以看出上式同单序列的训练方法非常一致,只是需要将多个序列的进行叠加起来,不过要注意的是不同序列在叠加之前,需要进行归一化处理!相关Python代码如下:
def train_batch(self, X, Z_seq=list()):
# 针对于多个序列的训练问题,其实最简单的方法是将多个序列合并成一个序列,而唯一需要调整的是初始状态概率
# 输入X类型:list(array),数组链表的形式
# 输入Z类型: list(array),数组链表的形式,默认为空列表(即未知隐状态情况)
self.trained = True
X_num = len(X) # 序列个数
self._init(self.expand_list(X)) # 发射概率的初始化
# 状态序列预处理,将单个状态转换为1-to-k的形式
# 判断是否已知隐藏状态
if Z_seq!=list():
for n in range(X_num):
Z.append(np.zeros((len(X[n]),self.n_state)))
for i in range(len(Z[n])):
Z[n][i][int(Z_seq[n][i])] = 1
# 初始化状态序列list
for n in range(X_num):
Z.append(list(np.ones((len(X[n]), self.n_state))))
for e in range(self.n_iter):
# EM步骤迭代
# 更新初始概率过程
print "iter: ", e
b_post_state = []
# 批量累积:状态的后验概率,类型list(array)
b_post_adj_state = np.zeros((self.n_state, self.n_state)) # 批量累积:相邻状态的联合后验概率,数组
b_start_prob = np.zeros(self.n_state) # 批量累积初始概率
for n in range(X_num): # 对于每个序列的处理
X_length = len(X[n])
alpha, c = self.forward(X[n], Z[n])
beta = self.backward(X[n], Z[n], c)
post_state = alpha * beta / np.sum(alpha * beta) # 归一化!
b_post_state.append(post_state)
post_adj_state = np.zeros((self.n_state, self.n_state))
# 相邻状态的联合后验概率
for i in range(X_length):
if i == 0: continue
if c[i]==0: continue
post_adj_state += (1 / c[i]) * np.outer(alpha[i - 1],
beta[i] * self.emit_prob(X[n][i])) * self.transmat_prob
if np.sum(post_adj_state)!=0:
post_adj_state = post_adj_state/np.sum(post_adj_state)
# 归一化!
b_post_adj_state += post_adj_state
# 批量累积:状态的后验概率
b_start_prob += b_post_state[n][0] # 批量累积初始概率
# M步骤,估计参数,最好不要让初始概率都为0出现,这会导致alpha也为0
b_start_prob += 0.00001*np.ones(self.n_state)
self.start_prob = b_start_prob / np.sum(b_start_prob)
b_post_adj_state += 0.001
for k in range(self.n_state):
if np.sum(b_post_adj_state[k])==0: continue
self.transmat_prob[k] = b_post_adj_state[k] / np.sum(b_post_adj_state[k])
self.emit_prob_updated(self.expand_list(X), self.expand_list(b_post_state))
实验结果:
这里的0 、1、 2、 3表示状态B、M、E、S
比如"我要回家吃饭"的分词结果为"我要|回家|吃|饭"
比如"江主席发表重要讲话"的分词结果为"江主席|发表|重要|讲话"
这个程序运行有点慢,实际不需要使用完整的HMM模型也能快速完成中文分词工作,具体可以参考这个代码,其代码也非常简单,利用了一个简化的HMM思路,完成了一个快速中文分词。
3. 股票预测
具体代码参见:Stock_03.py
股票数据是典型的序列数据,也是HMM的经典应用例子,假设我们将股票的每日价格变化和交易额视为观测值,而将股票的涨跌或者维持视为隐状态,由此我们可以清晰地建立一个HMM模型。
实验结果:
这里有四个张图,分别表示四种股票状态所出现的日期
项目说明:
代码下载: (点星是对作者最好的支持!!!^_^)
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!如何用python写出一个程序(输入一组六项不同数据输出6组36项误差在±百分之五随机数据)_百度知道
如何用python写出一个程序(输入一组六项不同数据输出6组36项误差在±百分之五随机数据)
我有更好的答案
为什么我运行不了
采纳率:91%
为您推荐:
其他类似问题
python的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 python退出程序 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信