使用spyder调试时,如何传入argv[1]这个参数

vergilwm 2017-02-21 11:34:21
如题,最近在用spyder进行py代码的编写,直接运行.py文件时,因为里面要读取一个文件,所以有这么一行代码:
datafile = sys.argv[1]

运行后会报超过列表范围的错误,好在作者在后面标注了:one-indexed, whitespace separated,
我自己是于是这样运行文件:
run presampled_bpr.py sample.txt
发现可以运行。但我现在想单步调试。按crl+f5进入后单步调试,发现又在argv[1]中报前面相同的错,想请问各位
如何在ipdb下运行并传入argv[1]的参数。
下面是完整代码:
[code=python"""
Precompute a schedule of samples and use it to train
a BPR model.

Sampling is done in a map-reduce style.
"""

from bdoopr import *
import toydoop
from bpr import BPR, BPRArgs, ExternalSchedule
from numpy import loadtxt
from scipy.sparse import coo_matrix
import sys


#strip() is used to remove the head and bottom character specified by usr
#str = '0000 my 0000'
#print str.strip('0')
# [output] my


#split
def parser(line):
return map(int,line.strip().split()[:2])

def formatter(key,val):
return '{0}\t{1}\t{2}'.format(key,val[0],val[1])




datafile = sys.argv[1] # one-indexed, whitespace separated
sample_file = datafile+'.samples'
tmp1 = sample_file+'.tmp1'
tmp2 = sample_file+'.tmp2'

print 'reading data...'
data = loadtxt(datafile)
print 'converting to zero-indexed sparse matrix...'


#data = array([[ 1., 2., 4., 5.],
# [ 8., 3., 6., 9.]])
#data[:,:2]
#Out[45]:
#array([[ 1., 2.],
# [ 8., 3.]])

idxs = data[:,:2]-1
vals = data[:,2]
data = coo_matrix((vals,idxs.T)).tocsr()
user_item_counts = dict((i+1,data[i].getnnz()) for i in xrange(data.shape[0]))

print 'creating samples...'
mapper1 = Mapper1(user_item_counts,oversampling=10)
print 'map-red1...'
toydoop.mapreduce(datafile,tmp1,mapper=mapper1,reducer=reducer1,parser=parser)
print 'map2...'
toydoop.mapreduce(datafile,tmp2,mapper=mapper2,parser=parser) # map the data again

#from ipdb import set_trace
#set_trace()

print 'red2...'
toydoop.mapreduce([tmp1,tmp2],sample_file,reducer=reducer2,formatter=formatter)

print 'training...'
args = BPRArgs()
args.learning_rate = 0.3
num_factoras = 10
print 'gui...'
model = BPR(num_factors,args)
sampler = ExternalSchedule(sample_file,index_offset=1) # schedule is one-indexed
num_iters = 50
model.train(data,sampler,num_iters)
][/code]
谢谢各位
...全文
3391 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnnxcy 2019-02-09
  • 打赏
  • 举报
回复
在spyder下,按Ctrl+F6,勾中Command line options,填入要传递的参数,运行即可。
k_shmily 2017-04-01
  • 打赏
  • 举报
回复
引用 2 楼 wn0112 的回复:
不要传了,直接

datafile = './sample.txt'
调好了,再改回sys.argv[1]
之前也是这么干的,但心里总有越不过去的坎儿,看人家VS gdb/gcc shell 都可以,心想python不应该不可以吧!哈哈
k_shmily 2017-04-01
  • 打赏
  • 举报
回复 3
自己刚刚试出来了,看到debugfile()命令里有几个参数,wdir我猜是表示工作路径吧!那应该也有表示参数之类的形参,如:argv。
搜了搜,看到老外函数中有用到args wdir等作为形参的,结果就试出来了。
在命令行中添加 args='the_arg111 the_arg222'
如下:
debugfile('E:/SVN/test_g/test/exams/py_demo/batch_edit.py', args='the_arg111 the_arg222', wdir='E:/SVN/test_g/test/exams/py_demo')


第一次回答问题,楼主给个分吧!激励一下下

手无护鸡之力 2017-04-01
  • 打赏
  • 举报
回复
不要传了,直接

datafile = './sample.txt'
调好了,再改回sys.argv[1]
k_shmily 2017-04-01
  • 打赏
  • 举报
回复
最近也在使用python,也存在这个困惑!
内容概要:本文围绕城市场景下的无人机三维路径规划问题,提出了一种新颖的多目标粒子群优化算法(NMOPSO),旨在解决高维复杂环境中路径规划面临的多重挑战。研究聚焦于导航变量的多目标优化,构建了一个包含路径长度、飞行安全性、能量消耗等多个相互冲突目标的优化模型,并采用改进的粒子群算法进行高效搜索,最终获得一组Pareto最优解集,为实际决策提供多样化路径选择方案。该方法结合Matlab代码实现,详细阐述了算法的设计机制、数学建模流程及仿真验证过程,充分展示了其在密集城市建筑环境中有效规避障碍物、满足飞行动力学约束并实现多目标权衡的能力,具有较强的工程应用价值。; 适合人群:具备一定优化算法理论基础和Matlab编程能力,从事无人机路径规划、智能交通系统、自动化控制、人工智能应用等方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于城市环境中的无人机物流配送、巡检监控、应急救援等实际任务的三维路径规划;②为高维、非线性、多约束的多目标优化问题提供有效的算法设计思路与改进参考;③通过Matlab仿真平台复现算法,进一步开展性能测试、参数调优与算法对比研究,推动相关领域的技术创新。; 阅读建议:此资源强调算法原理与工程实践的深度融合,建议读者重点研读目标函数的构建方式、约束条件的处理策略以及NMOPSO算法的核心改进机制,并务必动手运行和调试所提供的Matlab代码,以深入理解算法在三维空间路径优化中的具体实现细节与实际表现。

37,738

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧