ElasticSearch DSL聚合查询语句

本来像聚合(aggregation)这种东西, 在Grafana中可以轻易的实现, 但是偶尔会有需求, 需要自己写DSL脚本实现一些功能, 于是, 只好自己动手了.

例子1

查询serverName=”dns-server-1″结果里, 按hostip的数量进行排序, 取前5

GET /my-service-2020.07.22/_search
{
  "query": {
    "term": { "serverName.keyword": "dns-server-1" }
  },
  "size" : 0,
  "aggs": {
    "top-10-hostip": {
      "terms": {
      	"field": "hostip.keyword",
        "size": 5
      }
    }
  }
}

结果 Continue reading “ElasticSearch DSL聚合查询语句”

python字符串对齐

对于基本的字符串对齐操作,可以使用字符串的 ljust() , rjust() 和 center() 方法。比如:

>>> text = 'Hello World'
>>> text.ljust(20)
'Hello World         '
>>> text.rjust(20)
'         Hello World'
>>> text.center(20)
'    Hello World     '

所有这些方法都能接受一个可选的填充字符。比如:

>>> text.rjust(20,'=')
'=========Hello World'
>>> text.center(20,'*')
'****Hello World*****'
>>>

如果你想指定一个非空格的填充字符,将它写到对齐字符的前面即可:

>>> format(text, '=>20s')
'=========Hello World'
>>> format(text, '*^20s')
'****Hello World*****'

当格式化多个值的时候,这些格式代码也可以被用在 format() 方法中。比如:

>>> '{:>10s} {:>10s}'.format('Hello', 'World')
'     Hello      World'

下面是一个例子

>>> top_5_domain = [{'key': 'www.hizy.net', 'doc_count': 32109556}, {'key': 'www.xpdo.net', 'doc_count': 12070}, {'key': 'www.zhukun.net', 'doc_count': 1156}, {'key': 'image.baidu.com', 'doc_count': 114}, {'key': 'cloudrea.ksidc.com', 'doc_count': 11}]
>>>
>>> format_temp = "\t {:<20} \t\t {:>12}"
>>> for d in top_5_domain:
...     print(format_temp.format(d["key"],str(d["doc_count"])))
...
     www.hizy.net         		     32109556
     www.xpdo.net         		        12070
     www.zhukun.net       		         1156
     image.baidu.com      		          114
     cloudrea.ksidc.com   		           11

 

Django parse a json/dictionary reminder on template

如果想在一个Django template里引入json, 正确的template写法为

python的views.py的写法

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import Http404, HttpResponse

def get_product(request):
context1 = {}
context1[‘env’] = ‘production’
context1[‘mydict’] = {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4"}
context1[‘days’] = [1,2,3]

if request.GET.get("item") == "ec2" or not request.GET.get("item"):
return render(request, ‘main_content.html’, context1)
else:
raise Http404()

main_content.html使用extends引入index.html

{% extends 'index.html' %}
{% block content %}
{% for day in days %}
<li>day: {{ day }}</li>
{% endfor %}
{% endblock %}

index.html使用include引入sidebar.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="header">My page</div>
<div class="content">
<div class="sidebar">

<ul>
{% if env == 'production' %}
{% include 'production.sidebar.html' %}
{% elif env == 'development %}
{% include 'development.sidebar.html' %}
{% elif env == 'staging' %}
{% include 'staging.sidebar.html' %}
{% elif env == 'legacy' %}
{% include 'legacy.sidebar.html' %}
{% endif %}
</ul>

</div>
<div class="maincontent">

{% block content %} {% endblock %}

</div>
</div>
</body>
</html>

Continue reading “Django parse a json/dictionary reminder on template”

使用pytest进行批量测试

Pytest是python的一个测试模块,可以编写一些简单的测试用例,也可以用来进行一些批量测试,下面简单介绍一下。

1,安装

$ pip install -U pytest
or
$ easy_install -U pytest

$ py.test --version

2,基础语法
[code lang=”python”]
#!/usr/bin/python
import pytest

def func(x):
return x + 1

def test_answer(): #测试用例,方法需要以test_开头
assert func(3) == 5

运行之

$ py.test 2.py    #注意需要用py.test命令来运行之

稍微复杂一点儿的写法:
[code lang=”python”]
#!/usr/bin/python
import pytest

class TestClass:

def test_one(self):
x = "this"
assert "h" in x

def test_two(self):
x = 3
assert x > 2

Continue reading “使用pytest进行批量测试”

Python tips – 轻松转换列表与字符串

There are a few useful tips to convert a Python list (or any other iterable such as a tuple) to a string for display.

First, if it is a list of strings, you may simply use join this way:

>>> mylist = ['spam', 'ham', 'eggs']
>>> print ', '.join(mylist)
spam, ham, eggs

Using the same method, you might also do this:

>>> print '\n'.join(mylist)
spam
ham
eggs

However, this simple method does not work if the list contains non-string objects, such as integers.

If you just want to obtain a comma-separated string, you may use this shortcut:

>>> list_of_ints = [80, 443, 8080, 8081]
>>> print str(list_of_ints).strip('[]')
80, 443, 8080, 8081

Or this one, if your objects contain square brackets:

>>> print str(list_of_ints)[1:-1]
80, 443, 8080, 8081

Finally, you may use map() to convert each item in the list to a string, and then join them:

>>> print ', '.join(map(str, list_of_ints))
80, 443, 8080, 8081
>>> print '\n'.join(map(str, list_of_ints))
80
443
8080
8081

pip, easy_install使用方式

easy_install 跟 pip 都是 Python 的套件管理程序,有了它们,在使用 Python 开发程序的时候会带來不少方便。
easy_install 和 pip 有什么不一样?据 pip 官网的说法,pip 改善了不少 easy_install 的缺点,如此说來 pip 应该是略胜一筹,不过它还不能够完全取代对方,因为目前有很多套件还是得用 easy_install 安裝。

安装使用easy_install

安装:
$ wget -q http://peak.telecommunity.com/dist/ez_setup.py
$ python ./ez_setup.py

使用:
$ easy_install PackageName     #安装套件
$ easy_install -U PackageName  #更新套件
$ easy_install -m PackageName  #卸载套件
$ easy_install --showhelp      #显示说明

Continue reading “pip, easy_install使用方式”

python遍历文件脚本实例

自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理。没啥技术含量,但是也记录一下吧。

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import shutil
dir = "/mnt/Packages"
class Packages:
    def __init__(self,srcdir,desdir):
        self.sdir=srcdir
        self.ddir=desdir
    def check(self):
        print('program start...')
        for dirpath, dirnames, filenames in os.walk(self.sdir):   #遍历文件
            for filename in filenames:
                thefile=os.path.join(dirpath,filename)            #文件的绝对地址
                try:
                    if os.path.splitext(thefile)[1]=='.rpm':      #筛选.rpm格式的文件
                        #print('Fount rpm package: ' + thefile)
                        if 'inspuer' in os.popen('rpm -qpi ' + thefile).read().rstrip():
                            print('Found error package: ' + thefile)
                            shutil.copy(thefile, self.ddir)  #将错误文件复制到desdir目录
                            f = open('list.txt', 'a')    #将错误文件列表写入到list.txt
                            f.write(filename + '\n')
                            f.close()
                except IOError, err:
                    print err
                    sys.exit()

if __name__ == '__main__':
    dir=Packages('/mnt/cdrom','/mnt/erpm')   #源目录为/mnt/cdrom,目标目录为/mnt/erpm
    dir.check()