Press "Enter" to skip to content

Category: 编程学习

编程知识学习与进阶

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

 

Leave a Comment

HTML栅格化示例

如果需要将一个DIV平均分割成几个小方格, 栅格化是比较好的解决方案, 本文演示一下

CSS代码:

<style>
.features .feat_list{
width: 60%;
margin: 20px auto;
outline: 1px solid blue;
}
.features .feat_list::before,
.features .feat_list::after{
content: ”;
display: table;
clear: both;
}
.features .feat_list .gird{
height: 300px;
background-color:#FFDCDC;
box-sizing: border-box; /* CSS属性 */
float: left;
margin: 0 1%; /* 如果这里margin是0, 则表格没有间隙, 下面width应该使用精准的数字, 例如33.33%, 66.66%等 */
outline: 1px solid red;
/* padding: 12px; */ /* 如果这里margin是0, 一般是需要设置padding */
}
.features .feat_list .gird-1{width: 31.33%;} /* 三栏表格宽度, 用33.33%减去margin的1%, 得到31.33% */
.features .feat_list .gird-2{width: 64.66%;} /* 三栏表格占2栏, 用33.33%减去margin的1%, 得到31.33% */
.features .feat_list .gird-5{width: 18%;} /* 三栏表格占2栏, 用33.33%减去margin的1%, 得到31.33% */
.features .feat_list .gird-4{width: 23%;}
</style>
1 Comment

交叉编译golang程序

本文演示了在ubuntu16.04上交叉编译golang程序的过程

配置本地golang环境

$ wget https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz

$ tar zxvf go1.10.2.linux-amd64.tar.gz

$ sudo mv go /usr/local/

$ echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile
$ echo 'export GOROOT=/usr/local/go' >> ~/.profile
$ echo 'export GOPATH=~/go' >> ~/.profile      #v2ray的工作目录,down下来的源码和编译生成的文件均在这里

$ source ~/.profile && echo $GOPATH

$ go version
go version go1.10.2 linux/amd64

交叉编译golang程序

$ go get -u v2ray.com/core/...
$ go get -u v2ray.com/ext/...

$ go install v2ray.com/ext/tools/build/vbuild

$ $GOPATH/bin/vbuild -os=linux -arch=arm

编译完成的程序位于$GOPATH目录下.

Leave a Comment

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>
Leave a Comment

Python读取配置文件模块ConfigParser

1,ConfigParser模块简介
假设有如下配置文件,需要在Pyhton程序中读取

$ cat config.ini
[db]
db_port = 3306
db_user = root
db_host = 127.0.0.1
db_pass = xgmtest

[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True

[SectionTwo]
FavoriteColor = Green
[SectionThree]
FamilyName: Johnson

[Others]
Route: 66

如何在Python中读取呢

>>> import ConfigParser
>>> Config = ConfigParser.ConfigParser()
>>> Config
<ConfigParser.ConfigParser instance at 0x00BA9B20>
>>> Config.read("config.ini")
['config.ini']
>>> Config.sections()
['db', 'Others', 'SectionThree', 'SectionOne', 'SectionTwo']
>>> Config.get("db", "db_host")
'127.0.0.1'
>>> Config.getint("db", "db_port")
3306
4 Comments

使用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

Leave a Comment