我整理的这个简单的Python方法只是检查Tomcat是否在我们的一台服务器上运行.
import urllib2
import re
import sys
def tomcat_check():
tomcat_status = urllib2.urlopen('http://10.1.1.20:7880')
results = tomcat_status.read()
pattern = re.compile('<body>Tomcat is running...</body>',re.M|re.DOTALL)
q = pattern.search(results)
if q == []:
notify_us()
else:
print ("Tomcat appears to be running")
sys.exit()
Run Code Online (Sandbox Code Playgroud)
如果找不到此行:
<body>Tomcat is running...</body>
Run Code Online (Sandbox Code Playgroud)
它叫:
notify_us()
Run Code Online (Sandbox Code Playgroud)
哪个使用SMTP向我自己发送电子邮件,另一个管理员Tomcat不再在服务器上运行...
我之前没有在Python中使用过re模块...所以我假设有一个更好的方法来做到这一点......我也对美丽的汤开放了一个更优雅的解决方案......但是没有使用它..
试着尽量保持这个......
我不是C程序员.我上周刚刚开始阅读K&R的TCPL.我用Java编写了这个42行代码.我尝试将它转换为C,但它给了我一个分段错误.
这是Java版本:http://codepaste.net/m8jz6m
我尝试将其移植到C的失败:
//Not working.
#include <stdlib.h>
#include <string.h>
void caesar ( const int SIDE )
{
int array [SIDE] [SIDE] ;
for ( int number = 1; number <= SIDE * SIDE; ++ number )
array [ getY ( number, SIDE ) ] [ getX ( number, SIDE ) ] = number ;
for ( int Y = 0; Y < SIDE; ++ Y ) {
for ( int X = 0; X < SIDE; ++ …Run Code Online (Sandbox Code Playgroud) 我不确定为什么这个Pickle示例没有显示两个字典定义.据我所知,"ab +"应该意味着pickle.dat文件被附加到并且可以从中读取.我是整个泡菜概念的新手,但网上的教程似乎不仅仅是初始存储.
import cPickle as pickle
def append_object(d, fname):
"""appends a pickle dump of d to fname"""
print "append_hash", d, fname
with open(fname, 'ab') as pickler:
pickle.dump(d, pickler)
db_file = 'pickle.dat'
cartoon = {}
cartoon['Mouse'] = 'Mickey'
append_object(cartoon, db_file)
cartoon = {}
cartoon['Bird'] = 'Tweety'
append_object(cartoon, db_file)
print 'loading from pickler'
with open(db_file, 'rb') as pickler:
cartoon = pickle.load(pickler)
print 'loaded', cartoon
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望使用for循环构建一个字典,然后将key:value对添加到pickle.dat文件,然后清除字典以节省一些RAM.
这里发生了什么?
我试图在Java的上下文中学习面向对象的编程.我正在编写一个相当简单的代码,但是我收到了这个错误:
Exception in thread "main" java.lang.NullPointerException
at Advisor_score.Rating.Score(Rating.java:12)
at Advisor_score.Rating.main(Rating.java:25)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
package Advisor_score;
public class Rating {
double [] Ratings;
double sum;
double raw_advisor;
double advisor_score;
public Rating (double [] x){
double [] Ratings = x;
}
public double Score(){
for(int i=2;i<Ratings.length;i++){
sum+=Ratings[i];
}
raw_advisor=((sum-(3*(Ratings.length-2)))/4);
advisor_score= 2.5+(2.5*(1-Math.pow(Math.E, -.5*raw_advisor)));
return advisor_score;
}
public void print(){
System.out.println(advisor_score);
}
public static void main(String[] args){
double p1_1[] = {101,1,1,1.5,.5};
Rating d = new Rating(p1_1);
d.Score();
d.print();
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在看这几个小时,无法弄清楚代码的问题.我是编程新手可以有人帮助我吗?提前致谢!
根据我对连接的理解,这段代码应该有效:
aList = ["first", "second", "last"]
for i in aList:
print self.i
Run Code Online (Sandbox Code Playgroud)
我的类定义了绑定self.first=something以及self.second和self.last.当我编写print self.first代码工作但print self.i引发异常时.我究竟做错了什么?
我的问题可能很简单,但我真的无法弄清楚我哪里出错了.我想将一个变量从一个函数传递给另一个函数.因此我使用return但是我总是收到一条错误消息,我的变量没有定义.
我的代码是:
url = "http://www.419scam.org/emails/2004-01/30/001378.7.htm"
def FirstStrike(url):
...
return tokens
def analyze(tokens):
...
if __name__ == "__main__":
FirstStrike(url)
analyze(tokens)
Run Code Online (Sandbox Code Playgroud)
如果我运行这个,我收到一条错误消息:NameError:name'tokens'未定义.
如果我想查看一个方法的作用,例如模块中的方法高斯随机,我将如何使用Python解释器?例如,在我将Python随机导入到Python解释器的控制台后,我可以做什么来查找模块随机方法gauss的实际代码,而不必查看实际文件.提前致谢!
我试图使用PIL的图像的开放方法打开图像,但是我收到以下错误.
我已将图像存储在C:\a.jpg.
>>> h1 = Image.open("C:\a.jpg").histogram()
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
h1 = Image.open("C:\a.jpg").histogram()
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1952, in open
fp = __builtin__.open(fp, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\x07.jpg'
Run Code Online (Sandbox Code Playgroud) 有人可以解释下面的代码行吗?它是某种嵌套for循环吗?如果是这样,有人可以将其重写为等效的嵌套for循环.allPositions参数是一个列表,synapsesPerSegment是一个int变量.
for rx,ry in random.sample(allPositions, synapsesPerSegment):
Run Code Online (Sandbox Code Playgroud) 这应该是不言自明的
>>> (1+2j).real #Normal Usage
1.0
>>> (1+2j).imag #Normal Usage
2.0
>>> 1+2j.real #Normal Usage
1.0
>>> 1+2j.imag #Flips Out
3.0
>>> 123+657j.real
123.0
>>> 123+657j.imag
780.0
Run Code Online (Sandbox Code Playgroud)
z.imag以某种方式累加了实部和虚部.
我发现这很有趣..这是一个错误还是这是一个故意的功能?