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>

继续阅读

交叉编译golang程序

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

配置本地golang环境
[code lang=”bash”]
$ 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
[/code]

交叉编译golang程序
[code lang=”bash”]
$ 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
[/code]
编译完成的程序位于$GOPATH目录下. 继续阅读

Django parse a json/dictionary reminder on template

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

python的views.py的写法
[sourcecode language=”plain”]
# -*- 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()
[/sourcecode]

main_content.html使用extends引入index.html
[sourcecode language=”plain”]
{% extends ‘index.html’ %}
{% block content %}
{% for day in days %}
<li>day: {{ day }}</li>
{% endfor %}
{% endblock %}
[/sourcecode]

index.html使用include引入sidebar.html
[sourcecode language=”plain”]
<!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>
[/sourcecode] 继续阅读

一些初级python面试题

一些初级python面试题,这里简单罗列一下

[code lang=”python”]
# 反转字符串, 这个是最常见的
# 比如给一个字符串hello,将其反转为olleh
def reverse(str):
alist = list(str)
alist.reverse()
new_str = ”.join(alist)
return new_str
[/code] 继续阅读

Python读取配置文件模块ConfigParser

1,ConfigParser模块简介
假设有如下配置文件,需要在Pyhton程序中读取
[code]
$ 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
[/code]

如何在Python中读取呢
[code]
>>> 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
[/code] 继续阅读

使用pytest进行批量测试

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

1,安装
[code]
$ pip install -U pytest
or
$ easy_install -U pytest

$ py.test –version
[/code]

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

def func(x):
return x + 1

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

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

稍微复杂一点儿的写法:
[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
[/code] 继续阅读

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:

[code]
>>> mylist = [‘spam’, ‘ham’, ‘eggs’]
>>> print ‘, ‘.join(mylist)
spam, ham, eggs
[/code]
Using the same method, you might also do this:
[code]
>>> print ‘\n’.join(mylist)
spam
ham
eggs
[/code]
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:
[code]
>>> list_of_ints = [80, 443, 8080, 8081]
>>> print str(list_of_ints).strip(‘[]’)
80, 443, 8080, 8081
[/code]
Or this one, if your objects contain square brackets:
[code]
>>> print str(list_of_ints)[1:-1]
80, 443, 8080, 8081
[/code]
Finally, you may use map() to convert each item in the list to a string, and then join them:
[code]
>>> print ‘, ‘.join(map(str, list_of_ints))
80, 443, 8080, 8081
>>> print ‘\n’.join(map(str, list_of_ints))
80
443
8080
8081
[/code]