程序设计

专业和发烧友程序员的问答

5
如何检查Shell脚本中是否存在文件
我想编写一个Shell脚本来检查某个文件archived_sensor_data.json是否存在,如果存在,则将其删除。在http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html之后,我尝试了以下操作: [-e archived_sensor_data.json] && rm archived_sensor_data.json 但是,这会引发错误 [-e: command not found 当我尝试test_controller使用./test_controller命令运行生成的脚本时。代码有什么问题?
175 shell 

13
Java switch语句:需要常量表达式,但它是常量
因此,我正在研究具有一些静态常量的此类: public abstract class Foo { ... public static final int BAR; public static final int BAZ; public static final int BAM; ... } 然后,我想一种基于常量获取相关字符串的方法: public static String lookup(int constant) { switch (constant) { case Foo.BAR: return "bar"; case Foo.BAZ: return "baz"; case Foo.BAM: return "bam"; default: return "unknown"; } …


8
从HttpResponseMessage获取内容/消息
我正在尝试获取HttpResponseMessage的内容。应该是:{"message":"Action '' does not exist!","success":false},但我不知道如何从HttpResponseMessage中获取它。 HttpClient httpClient = new HttpClient(); HttpResponseMessage response = await httpClient.GetAsync("http://****?action="); txtBlock.Text = Convert.ToString(response); //wrong! 在这种情况下,txtBlock将具有值: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Vary: Accept-Encoding Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Date: Wed, 10 Apr 2013 20:46:37 GMT Server: Apache/2.2.16 Server: (Debian) X-Powered-By: PHP/5.3.3-7+squeeze14 …

12
使用Node.js和Express进行发布时如何访问请求正文?
我有以下Node.js代码: var express = require('express'); var app = express.createServer(express.logger()); app.use(express.bodyParser()); app.post('/', function(request, response) { response.write(request.body.user); response.end(); }); 现在,如果我发布类似的内容: curl -d user=Someone -H Accept:application/json --url http://localhost:5000 我得到Someone了预期的结果。现在,如果我想获得完整的请求正文怎么办?我尝试这样做,response.write(request.body)但是Node.js抛出一个异常,说“ 第一个参数必须是字符串或缓冲区 ”,然后转到“无限循环”,并出现异常,指出“ 发送标头后不能设置标头 ”。即使我var reqBody = request.body;再写了也是这样response.write(reqBody)。 这是什么问题? 另外,我可以不使用而直接获得原始请求express.bodyParser()吗?


5
matplotlib中的日期刻度和旋转
我在尝试在matplotlib中旋转日期刻度时遇到问题。下面是一个小示例程序。如果我尝试最后旋转刻度线,则刻度线不会旋转。如果我尝试如注释“ crashes”下所示旋转刻度线,则matplot lib崩溃。 仅当x值为日期时,才会发生这种情况。如果我在的调用dates中将变量替换t为avail_plot,则该xticks(rotation=70)调用在内部正常运行avail_plot。 有任何想法吗? import numpy as np import matplotlib.pyplot as plt import datetime as dt def avail_plot(ax, x, y, label, lcolor): ax.plot(x,y,'b') ax.set_ylabel(label, rotation='horizontal', color=lcolor) ax.get_yaxis().set_ticks([]) #crashes #plt.xticks(rotation=70) ax2 = ax.twinx() ax2.plot(x, [1 for a in y], 'b') ax2.get_yaxis().set_ticks([]) ax2.set_ylabel('testing') f, axs = plt.subplots(2, sharex=True, sharey=True) t = …
175 python  matplotlib 


4
Node.js中的process.env.PORT是什么?
process.env.PORT || 3000Node.js的用途是什么?我在某处看到了这个: app.set('port', process.env.PORT || 3000); 如果用于设置3000为侦听端口,我可以改用它吗? app.listen(3000); 如果不是,为什么?
175 node.js  express  port 

4
断言对模拟方法的后续调用
模拟有一个有用的assert_called_with()方法。但是,据我了解,这仅检查对方法的最后一次调用。 如果我有连续3次调用该模拟方法的代码,每次使用不同的参数,那么该如何用其特定的参数来断言这3次调用?
175 python  mocking 

6
如何在pytest中打印到控制台?
我正在尝试将TDD(测试驱动的开发)与 pytest。 pytest使用时不会print进入控制台print。 我正在pytest my_tests.py运行它。 的 documentation似乎是说,它应该是默认的工作:http://pytest.org/latest/capture.html 但: import myapplication as tum class TestBlogger: @classmethod def setup_class(self): self.user = "alice" self.b = tum.Blogger(self.user) print "This should be printed, but it won't be!" def test_inherit(self): assert issubclass(tum.Blogger, tum.Site) links = self.b.get_links(posts) print len(links) # This won't print either. 什么都没有打印到我的标准输出控制台上(只是正常的进度以及通过/失败的测试数量)。 我正在测试的脚本包含打印: class …


3
如何获取熊猫DataFrame的最后N行?
我有熊猫数据帧df1和df2(df1是vanila数据帧,df2由'STK_ID'和'RPT_Date'索引): >>> df1 STK_ID RPT_Date TClose sales discount 0 000568 20060331 3.69 5.975 NaN 1 000568 20060630 9.14 10.143 NaN 2 000568 20060930 9.49 13.854 NaN 3 000568 20061231 15.84 19.262 NaN 4 000568 20070331 17.00 6.803 NaN 5 000568 20070630 26.31 12.940 NaN 6 000568 20070930 39.12 19.977 NaN …
175 python  pandas  dataframe 

1
如何在Django queryset中执行小于或等于过滤器?
我试图通过每个称为“个人资料”的用户个人资料中的自定义字段来过滤用户。此字段称为级别,是0到3之间的整数。 如果我使用等于进行过滤,则会得到具有预期级别的用户列表: user_list = User.objects.filter(userprofile__level = 0) 当我尝试使用少于以下内容进行过滤时: user_list = User.objects.filter(userprofile__level < 3) 我得到了错误: 未定义全局名称“ userprofile__level” 有没有一种方法可以通过<或>进行过滤,或者我是否吠叫了错误的树。

11
Swift Range的NSRange?
问题:我在使用使用Range的Swift String时,NSAttributedString需要一个NSRange let text = "Long paragraph saying something goes here!" let textRange = text.startIndex..<text.endIndex let attributedString = NSMutableAttributedString(string: text) text.enumerateSubstringsInRange(textRange, options: NSStringEnumerationOptions.ByWords, { (substring, substringRange, enclosingRange, stop) -> () in if (substring == "saying") { attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.redColor(), range: substringRange) } }) 产生以下错误: 错误:“范围”不可转换为“ NSRange” attributedString.addAttribute(NSForegroundColorAttributeName,值:NSColor.redColor(),范围:substringRange)
175 ios  macos  swift  nsrange 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.