xml地图|网站地图|网站标签 [设为首页] [加入收藏]
python_距离测量
分类:编程

之所以写这个,其实就是希望能对距离有一些概念,当然这个也是很基础的,不过千里之行始于足下嘛,各种路径算法,比如a*什么的都会用到这个

WKWebView进度及title

WKWebView进度及title

WKWebView 的estimatedProgress和title 都是KVO模式,所以可以添加监控:

    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];

    [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];

  •  

监控的实现方法:

  • (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

 

    if ([keyPath isEqualToString:@"estimatedProgress"]) {

 

        if (object == webView) {

            [self.progressView setAlpha:1.0f];

            [self.progressView setProgress:self.currentSubView.webView.estimatedProgress animated:YES];

 

            if(self.currentSubView.webView.estimatedProgress >= 1.0f) {

 

                [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{

                    [self.progressView setAlpha:0.0f];

                } completion:^(BOOL finished) {

                    [self.progressView setProgress:0.0f animated:NO];

                }];

 

            }

        }

        else

        {

            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

        }

 

    }

    else if ([keyPath isEqualToString:@"title"])

    {

        if (object == self.webView) {

            self.title = self.webView.title;

 

        }

        else

        {

            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

 

        }

    }

    else {

 

        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }

}

  •  

这里的进度增加了动画,类似safari的进度效果

需要注意的是销毁的时候一定要移除监控

        [webView removeObserver:self forKeyPath:@"estimatedProgress"];

        [webView removeObserver:self forKeyPath:@"title"];

 

swift:

// 监听

        theWebView?.addObserver(self, forKeyPath: "estimatedProgress", options: NSKeyValueObservingOptions.New, context: nil)

        theWebView?.addObserver(self, forKeyPath: "title", options: NSKeyValueObservingOptions.New, context: nil)

 

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

        if keyPath == "estimatedProgress" {

 

            if ((object?.isEqual(theWebView)) != false) {

                 self.progressView.alpha = 1.0

                self.progressView.setProgress(Float((self.theWebView?.estimatedProgress)!), animated: true)

 

                if self.theWebView?.estimatedProgress >= 1.0 {

 

                    UIView.animateWithDuration(0.3, delay: 0.3, options: .CurveEaseOut, animations: {

                        self.progressView.alpha = 0.0

                        }, completion: { (finished) in

                            self.progressView.setProgress(0.0, animated: false)

                    })

                }

            }else {

                super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)

            }

        }

    }

    deinit {

        webView.removeObserver(self, forKeyPath: "estimatedProgress")

        webView.removeObserver(self, forKeyPath: "title")

    }

需求:python如何实现普通用户登录服务器后切换到root用户再执行命令 

距离测量有三种方式

解决参考:

1、欧式距离,这个是最常用的距离测量方式

代码:

((x1-x2)^2 + (y1-y2)^2)^0.5

def verification_ssh(host,username,password,port,root_pwd,cmd):
    s=paramiko.SSHClient()
    s.load_system_host_keys()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname = host,port=int(port),username=username, password=password)

    if username != 'root':
        ssh = s.invoke_shell()
        time.sleep(0.1)
        ssh.send(' export LANG=en_US.UTF-8 n') #解决错误的关键,编码问题

        ssh.send('export LANGUAGE=en n')

        ssh.send('su - n')

        buff = ""
        while not buff.endswith('Password: '): #true
            resp = ssh.recv(9999)
            print(resp)
            buff +=resp.decode('utf8')

        print('hhhhh')
        print(buff)

        ssh.send(root_pwd)
        ssh.send('n')

        buff = ""
        # n = 0
        while not buff.endswith('# '):
            # n += 1
            resp = ssh.recv(9999)
            print(resp)
            buff +=resp.decode('utf8')
            # print(n)
            # if n >=3:
            #     break



        # print(buff)

        ssh.send('sh /tmp/check/101.sh') #放入要执行的命令
        ssh.send('n')
        buff = ''
        # m = 0
        while not buff.endswith('# '):
            resp = ssh.recv(9999).decode()
            buff +=resp
            # m += 1
            # print(m)

        result  = buff
        # print(type(result))
        # print(result)
        s.close()

if __name__ == "__main__":
    verification_ssh('测试IP地址', '普通账号', '普通账号的密码', '52222', 'root密码', 'id')

得到一个原型区域的距离

遇到问题:

#设起始坐标是原点,即(0,0)
y_result = []
for y in range(10,-10,-1):
  x_result = []
  for x in range(-10,10,1):
    #((0-x)**2 + (0-y)**2)**0.5
    if ((0-x)**2 + (0-y)**2)**0.5 <= 9:
      x_result.append('*')
    else:
      x_result.append(' ')
  y_result.append(''.join(x_result))

for i in y_result:
  print i

     图片 1

本文由澳门新葡亰手机版发布于编程,转载请注明出处:python_距离测量

上一篇:进制转换,字典的使用 下一篇:没有了
猜你喜欢
热门排行
精彩图文