十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
欧式距离python实现代码:
站在用户的角度思考问题,与客户深入沟通,找到曲沃网站设计与曲沃网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、网站设计、企业官网、英文网站、手机端网站、网站推广、国际域名空间、网站空间、企业邮箱。业务覆盖曲沃地区。
import numpy as np
x=np.random.random(10)
y=np.random.random(10)
#方法一:根据公式求解
d1=np.sqrt(np.sum(np.square(x-y)))
#方法二:根据scipy库求解
from scipy.spatial.distance import pdist
X=np.vstack([x,y])
d2=pdist(X)
曼哈顿距离python实现:
import numpy as np
x=np.random.random(10)
y=np.random.random(10)
#方法一:根据公式求解
d1=np.sum(np.abs(x-y))
#方法二:根据scipy库求解
from scipy.spatial.distance import pdist
X=np.vstack([x,y])
d2=pdist(X,'cityblock')
程序运行结果:
扩展资料:
C语言实现:
#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#includestdio.h
#includemath.h
void main()
{
float x1, x2, y1, y2;
printf("请输入一组数据:");
while (~scanf("%f%f%f%f", x1, y1, x2, y2))//开始读取输入的数,知道文件结束。
{
printf("两点间的距离为:%.2f\n", sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)));
printf("请输入一组数据:");
}
}
整体逻辑
重试次数 = 0
while 重试次数 3:
ipt = input("xxxx")
if "*" in ipt:
....
continue
if 密码正确
....
break
else:
重试次数+=1
if 循环次数 = 3:
登录失败
1
ipt = input("")
print(sum(map(lambda c: int(c) if c.isnumeric() else {'J': 11, 'Q': 12, 'K': 13, 'A': 1}[c.upper()], ipt.split())))
2
ipt = input("")
print(*sorted(ipt.split(), key=lambda x: int(x), reverse=True)[: 3], sep=", ")
3
ipt = input("")
t = [int(c) for c in ipt.split()]
print(sum(t) - min(t) - max(t))
4
ipt = input("")
t = {ipt.split().count(c): c for c in ipt.split()}
print("{} {}".format(t[max(t)], max(t)))
你应该改变思路,应该是在远程的机器上监听文件的变化,再把结果传递过来
最近在网上看到python有个pyinotify模块,其中他们可以监控文件夹内的文件的创建,修改,读取,删除等系列操作,我修改了下,添加了可以吧操作记录写到日志里的一点方法,下面就贴出代码了给大家分享下:#!/usr/bin/env python
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
span style="font-family: verdana, Arial, Helvetica, sans-serif;"spanimport os
import datetime
import pyinotify
import logging
class MyEventHandler(pyinotify.ProcessEvent):
logging.basicConfig(level=logging.INFO,filename='/var/log/monitor.log')
#自定义写入那个文件,可以自己修改
logging.info("Starting monitor...")
def process_IN_ACCESS(self, event):
print "ACCESS event:", event.pathname
logging.info("ACCESS event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
def process_IN_ATTRIB(self, event):
print "ATTRIB event:", event.pathname
logging.info("IN_ATTRIB event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
def process_IN_CLOSE_NOWRITE(self, event):
print "CLOSE_NOWRITE event:", event.pathname
logging.info("CLOSE_NOWRITE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
def process_IN_CLOSE_WRITE(self, event):
print "CLOSE_WRITE event:", event.pathname
logging.info("CLOSE_WRITE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
def process_IN_CREATE(self, event):
print "CREATE event:", event.pathname
logging.info("CREATE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
def process_IN_DELETE(self, event):
print "DELETE event:", event.pathname
logging.info("DELETE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
def process_IN_MODIFY(self, event):
print "MODIFY event:", event.pathname
logging.info("MODIFY event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
def process_IN_OPEN(self, event):
print "OPEN event:", event.pathname
logging.info("OPEN event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
def main():
# watch manager
wm = pyinotify.WatchManager()
wm.add_watch('/tmp', pyinotify.ALL_EVENTS, rec=True)
#/tmp是可以自己修改的监控的目录
# event handler
eh = MyEventHandler()
# notifier
notifier = pyinotify.Notifier(wm, eh)
notifier.loop()
if __name__ == '__main__':
main()
/span/span
下面来看看效果如何呢:我在代码定义的是监控tmp目录下的变化:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
span style="font-family: verdana, Arial, Helvetica, sans-serif;"span[root@centos6 monitor-folder]# python total-monitor.py
OPEN event: /tmp/.ICE-unix
CLOSE_NOWRITE event: /tmp/.ICE-unix
OPEN event: /tmp
CLOSE_NOWRITE event: /tmp
OPEN event: /tmp
CLOSE_NOWRITE event: /tmp
DELETE event: /tmp/aa
DELETE event: /tmp/adduser.conf
DELETE event: /tmp/adjtime
DELETE event: /tmp/aliases
DELETE event: /tmp/bash.bashrc
DELETE event: /tmp/bindresvport.blacklist
DELETE event: /tmp/environment
DELETE event: /tmp/fstab
DELETE event: /tmp/ipt.err
DELETE event: /tmp/ipt.out
DELETE event: /tmp/krb5.conf
DELETE event: /tmp/odbc.ini
DELETE event: /tmp/odbcinst.ini
DELETE event: /tmp/timezone
DELETE event: /tmp/ucf.conf
DELETE event: /tmp/warnquota.conf
DELETE event: /tmp/wgetrc
DELETE event: /tmp/xinetd.conf
CREATE event: /tmp/aa
OPEN event: /tmp/aa
ATTRIB event: /tmp/aa
CLOSE_WRITE event: /tmp/aa
CREATE event: /tmp/bb
OPEN event: /tmp/bb
ATTRIB event: /tmp/bb
CLOSE_WRITE event: /tmp/bb
CREATE event: /tmp/cc
OPEN event: /tmp/cc
ATTRIB event: /tmp/cc
CLOSE_WRITE event: /tmp/cc
上面是打印出来的监控状态,下面是我的操作代码:
[root@centos6 tmp]# ls
aa bash.bashrc ipt.err odbcinst.ini wgetrc
adduser.conf bindresvport.blacklist ipt.out timezone xinetd.conf
adjtime environment krb5.conf ucf.conf
aliases fstab odbc.ini warnquota.conf
[root@centos6 tmp]# rm -rf *
[root@centos6 tmp]# touch aa
[root@centos6 tmp]# touch bb
[root@centos6 tmp]# touch cc
[root@centos6 tmp]#
/span/span
上面是直接打印出来的,下面在看看我吧操作记录在日志文件里面,也可以看下日志文件的记录:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
span style="font-family: verdana, Arial, Helvetica, sans-serif;"spanINFO:root:Starting monitor...
INFO:root:OPEN event : /tmp/.ICE-unix 2011-10-27 13:31:57.219168
INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix 2011-10-27 13:31:57.219609
INFO:root:OPEN event : /tmp/ 2011-10-27 13:32:21.395228
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 13:32:21.398105
INFO:root:OPEN event : /tmp/ 2011-10-27 13:32:25.108997
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 13:32:25.110239
INFO:root:CREATE event : /tmp/aa 2011-10-27 13:32:28.004863
INFO:root:OPEN event : /tmp/aa 2011-10-27 13:32:28.005860
INFO:root:IN_ATTRIB event : /tmp/aa 2011-10-27 13:32:28.006723
INFO:root:CLOSE_WRITE event : /tmp/aa 2011-10-27 13:32:28.014617
INFO:root:CREATE event : /tmp/bb 2011-10-27 13:32:30.149758
INFO:root:OPEN event : /tmp/bb 2011-10-27 13:32:30.164415
INFO:root:IN_ATTRIB event : /tmp/bb 2011-10-27 13:32:30.164877
INFO:root:CLOSE_WRITE event : /tmp/bb 2011-10-27 13:32:30.165303
INFO:root:CREATE event : /tmp/cc 2011-10-27 13:32:32.725418
INFO:root:OPEN event : /tmp/cc 2011-10-27 13:32:32.726367
INFO:root:IN_ATTRIB event : /tmp/cc 2011-10-27 13:32:32.727229
INFO:root:CLOSE_WRITE event : /tmp/cc 2011-10-27 13:32:32.735052
INFO:root:CREATE event : /tmp/dd 2011-10-27 13:32:39.771041
INFO:root:OPEN event : /tmp/dd 2011-10-27 13:32:39.780881
INFO:root:IN_ATTRIB event : /tmp/dd 2011-10-27 13:32:39.781455
INFO:root:CLOSE_WRITE event : /tmp/dd 2011-10-27 13:32:39.781893
INFO:root:Starting monitor...
INFO:root:OPEN event : /tmp/.ICE-unix 2011-10-27 14:01:43.742477
INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix 2011-10-27 14:01:43.742915
INFO:root:OPEN event : /tmp/ 2011-10-27 14:01:50.579778
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 14:01:50.581317
INFO:root:DELETE event : /tmp/aa 2011-10-27 14:01:54.999528
INFO:root:DELETE event : /tmp/bb 2011-10-27 14:01:58.995966
INFO:root:DELETE event : /tmp/cc 2011-10-27 14:02:02.795950
INFO:root:DELETE event : /tmp/dd 2011-10-27 14:02:06.284208
INFO:root:OPEN event : /tmp/ 2011-10-27 14:02:07.738560
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 14:02:07.741922
INFO:root:CREATE event : /tmp/aa 2011-10-27 14:02:11.110322
INFO:root:OPEN event : /tmp/aa 2011-10-27 14:02:11.113150
INFO:root:IN_ATTRIB event : /tmp/aa 2011-10-27 14:02:11.116381
INFO:root:CLOSE_WRITE event : /tmp/aa 2011-10-27 14:02:11.118382
INFO:root:Starting monitor...
INFO:root:OPEN event : /tmp/.ICE-unix 2011-10-27 21:39:12.520432
INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix 2011-10-27 21:39:12.520879
INFO:root:OPEN event : /tmp/ 2011-10-27 21:39:23.784759
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 21:39:23.793211
INFO:root:OPEN event : /tmp/ 2011-10-27 21:39:33.916232
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 21:39:33.916823
INFO:root:DELETE event : /tmp/aa 2011-10-27 21:39:33.939008
INFO:root:DELETE event : /tmp/adduser.conf 2011-10-27 21:39:33.958143
INFO:root:DELETE event : /tmp/adjtime 2011-10-27 21:39:33.962497
INFO:root:DELETE event : /tmp/aliases 2011-10-27 21:39:33.978506
INFO:root:DELETE event : /tmp/bash.bashrc 2011-10-27 21:39:33.980834
INFO:root:DELETE event : /tmp/bindresvport.blacklist 2011-10-27 21:39:33.997176
INFO:root:DELETE event : /tmp/environment 2011-10-27 21:39:33.997683
INFO:root:DELETE event : /tmp/fstab 2011-10-27 21:39:33.998110
INFO:root:DELETE event : /tmp/ipt.err 2011-10-27 21:39:33.998532
INFO:root:DELETE event : /tmp/ipt.out 2011-10-27 21:39:34.000360
INFO:root:DELETE event : /tmp/krb5.conf 2011-10-27 21:39:34.000816
INFO:root:DELETE event : /tmp/odbc.ini 2011-10-27 21:39:34.002217
INFO:root:DELETE event : /tmp/odbcinst.ini 2011-10-27 21:39:34.002675
INFO:root:DELETE event : /tmp/timezone 2011-10-27 21:39:34.003110
INFO:root:DELETE event : /tmp/ucf.conf 2011-10-27 21:39:34.003538
INFO:root:DELETE event : /tmp/warnquota.conf 2011-10-27 21:39:34.018152
INFO:root:DELETE event : /tmp/wgetrc 2011-10-27 21:39:34.018641
INFO:root:DELETE event : /tmp/xinetd.conf 2011-10-27 21:39:34.041880
INFO:root:CREATE event : /tmp/aa 2011-10-27 21:39:40.639965
INFO:root:OPEN event : /tmp/aa 2011-10-27 21:39:40.640914
INFO:root:IN_ATTRIB event : /tmp/aa 2011-10-27 21:39:40.647835
INFO:root:CLOSE_WRITE event : /tmp/aa 2011-10-27 21:39:40.652158
INFO:root:CREATE event : /tmp/bb 2011-10-27 21:39:43.064526
INFO:root:OPEN event : /tmp/bb 2011-10-27 21:39:43.070849
INFO:root:IN_ATTRIB event : /tmp/bb 2011-10-27 21:39:43.071329
INFO:root:CLOSE_WRITE event : /tmp/bb 2011-10-27 21:39:43.071762
INFO:root:CREATE event : /tmp/cc 2011-10-27 21:39:47.046752
INFO:root:OPEN event : /tmp/cc 2011-10-27 21:39:47.051537
INFO:root:IN_ATTRIB event : /tmp/cc 2011-10-27 21:39:47.056211
INFO:root:CLOSE_WRITE event : /tmp/cc 2011-10-27 21:39:47.057490
[root@centos6 tmp]#/span/span
456.py
里的代码是
python
2.7.3
(default,
apr
10
2012,
23:31:26)
[msc
v.1500
32
bit
(intel)]
on
win32
type
"copyright",
"credits"
or
"license()"
for
more
information.
"hello"
那肯定报错啊。
python
2.7.3
(default,
apr
10
2012,
23:31:26)
[msc
v.1500
32
bit
(intel)]
on
win32
type
"copyright",
"credits"
or
"license()"
for
more
information.
这几行字都不是python的代码是控制台显示的提示文字。
你把456.py里的内容改为只有print
"hello"这句(注意前面的也不能要)。在运行应该就没问题了,会正常输出hello这个单词。
从这个问题上看,你应该是刚接触编程的新手的,在多看看教程,在仔细理解一下基础知识。
希望对你有所帮助~
一、为什么要做自动化?
前提:主流程稳定,周期长,脚本可重复利用。
1.节省人力资源
2.提高效率
3.面试需要
二、什么是RobotFramework:
基于Python的关键字驱动的自动化框架。
1. 基于Python:就是由python语言开发的这个框架。
2.关键字驱动:关键字驱动测试又称为表格驱动测试,是自动化测试的一种方法,是数据测试的一种改进方法。关键字驱动主要包括测试步骤、测试步骤中的对象,测试对象执行的动作,测试对象需要的数据
3.自动化框架?:是应用于自动化测试,框架提供可重用的基础自动化测试平台,提供自动化测试执行和管理功能的组织架构。
三、pip的常用方法:
pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。
以下在win10_x64 cmd中操作:
安装:pip install 包名
eg:pip install robotframework
安装指定版本:pip install 包名==版本号
eg:pip install robotframework==1.7.4.1
升级指定包:pip install --upgrade 包名
eg:pip install --upgrade robotframework
卸载:pip uninstall 包名
eg:pip uninstall robotframework
搜索安装包:pip search 包名
eg:pip search robotframework
查看当前电脑上已经安装的包:pip list
查看当前电脑上可以升级的包:pip list -o
四、rf的安装:
安装RF自动化框架:pip install robotframework
安装RF自动化框架IDE:pip install robotframework-ride
安装wxpython(ride依赖wxpython):pip install wxpython
ps :实际上新版本pip在安装ride时会自动安装所需依赖,也就是wxpython
版本信息:Win10 x64 + Python 3.7.7 + rf 3.1.2 + ride 1.7.4.1 + wxpython 4.0.7.post2
ps :ride 1.x版本不支持Python 3.8及以上,ride 2.x(开发中)支持Python 3.8
rf日志中文乱码解决方法:修改文件 python安装目录下 Libsite-packagesrobotidecontrib estrunner estrunnerplugin.py 第 565 行,将 SYSTEM 改成 OUTPUT ,重启ride工具。
五、元素定位:
id :以百度搜索输入框为例
在RF中写法: id=kw
name :以百度搜索输入框为例
在RF中写法: name=wd
xpath :Xml Path Language
1. 绝对路径:从html根部逐级(从上至下)查找
/html/body/p[1]/p[1]/p[3]/p/p/form/span[1]/input
在RF中写法:xpath=/html/body/p[1]/p[1]/p[3]/p/p/form/span[1]/input
2.相对路径:根据节点的上下文进行查找
eg://p/form/span/input 这里是在html中找一个p,p里面包含一个form,form包含一个span,span包含一个input,找有这样一个四层的关系的input标签,要注意如果html中有多个符合这种层级关系的input,会默认选第一个,也就是说这种方式未必是完全准确的。
3. xpath相对定位我们还可以根据元素的属性来查找:
eg://p/form/span[1]/input[@type] 这里是找符合这样一个层级关系并且有'type'这个属性的input标签
在RF中写法:xpath=//p/form/span[1]/input[@type]
eg://p/form/span[1]/input[@type='submit'] 这里是找符合这样一个层级关系并且'type'这个属性的值为'submit'的input标签
在RF中写法:xpath=//p/form/span[1]/input[@type='submit']
eg://p/form/span[1]/input[contains(@type,'sub')] 这里是找符合这样一个层级关系并且'type'这个属性的值包含'sub'的input标签
在RF中写法:xpath=//p/form/span[1]/input[contains(@type,'sub')]
以上是根据属性,如果是下面这样的a标签,我们还可以根据链接文字来进行定位
点我
eg://a[contains(text(),'点我')] 这样就是找所有a标签并且文字为'点我'的元素了
在RF中写法:xpath=//a[contains(text(),'点我')]
小技巧:
在 Chrome-F12-Console 中根据Xpath寻找元素:使用 $x (""),引号中填写xpath路径,如$x("/html/body/p[1]/p[1]/p[3]/p/p/form/span[1]/input")
css:
id: 以百度搜索输入框为例
在RF中写法: css=#kw
class: 以百度搜索输入框为例
在RF中写法: css=.s_ipt
css根据属性定位 :
[id='kw'] 表示查找id值为'kw'的元素
[name^='w'] 表示查找name值为'k'开头的元素
[class$='ipt'] 表示查找class值为'ipt'结尾的元素
[autocomplete*='f'] 表示查找autocomplete值中包含'f'的元素
[id='kw'] [name^='w'] 表示查找id值为'kw'并且name值为'k'开头的元素
在RF中写法:css=[id='kw'] [name^='w']、css=[class$='ipt']、
css=[maxlength='255']、css=[autocomplete='off']
ps:id和class也是属性,只是在css中针对id和class多了一种简单的写法,如上面的
css=#kw、css=.s_ipt
css根据标签定位:
input
表示查找当前页面所有input标签
在RF中写法:css=input
a,input
表示查找当前页面所有a标签和input标签
在RF中写法:css=a,input
span input
表示查找span标签下的所有input标签,哪怕是span下下级的input
在RF中写法:css=span input
spaninput
表示查找父元素为span标签的所有input标签,注意和span input的区别
在RF中写法:css=spaninput
span+input
表示查找紧挨在span元素后的第一个input元素
在RF中写法:css=span+input
span~input
表示查找紧挨在span元素后的所有input元素
在RF中写法:css=span~input
ps :其实大家都知道,因为页面上只依靠标签定位的话重复的可能性太大了,所有我们可以采用 标签 + 属性 的方式来进行定位
例如 spaninput[id='kw'][name^='w'] 意思是查找所有父标签为span的input标签,并且该标签中有属性id的值为kw且属性name的值为w开头
在RF中写法:css=spaninput[id='kw'][name^='w']
小技巧:
在 Chrome-F12-Console 中根据css寻找单个元素:
使用 $ (""),引号中填写css选择器,如$("spaninput[id='kw'][name^='w']")
在 Chrome-F12-Console 中根据css寻找多个元素:
使用 $ (""),引号中填写css选择器,如$("spaninput[id='kw'][name^='w']")