Press "Enter" to skip to content

Category: 编程学习

编程知识学习与进阶

Set up self-hosted runners for Github Actions

Add and Install a new runner on our host

Highly recommand run self-hosted runner as a systemd service

# Save settings
./config.cmd --url https://github.com/XXX/MyApp --token XXXXXXXXX

# Lets's deploy a systemd service
# Refer: https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/configuring-the-self-hosted-runner-application-as-a-service
sudo ./svc.sh install

Then, we will see these below

Run as user: zhang3
Run as uid: 1000
gid: 1000
Created symlink /etc/systemd/system/multi-user.target.wants/actions.runner.XXX.service → /etc/systemd/system/actions.runner.XXX.service.

Set up the workflow for our Github Actions

Official Document

https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/adding-self-hosted-runners

https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/configuring-the-self-hosted-runner-application-as-a-service

Leave a Comment

Javascript Array

Handle element in an Array

const hobbies = ["sports", "cooking", "reading"];

console.log(hobbies[0]);       # Get single element

hobbies.push("surfing");       # Add new element to the end
hobbies.unshift("movies");     # Add new element to th start

hobbies.pop();                 # remove the last element
hobbies.shift();               # remove the first element

Find element

const index = hobbies.findIndex((item) => {
    return item === "reading"
});
console.log(index);

A shorter code:

const index = hobbies.findIndex((item) => item === "reading");
console.log(index);

// Result
2

Find an element in Object

const inventory = [
  { name: "apples", quantity: 2 },
  { name: "bananas", quantity: 0 },
  { name: "cherries", quantity: 5 },
];

const result = inventory.find(({ name,quantity }) => name === "cherries");

console.log(result); 

// Result:
 { name: 'cherries', quantity: 5 }

find() method ONLY returns the first element in the provided array that satisfies the provided testing function

Filter

const words = ['spray', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter((word) => word.length > 6);

// Result:
["exuberant", "destruction", "present"]

Iterate Array

const newHobbies = hobbies.map((item) => item + "!");
consoloe.log(newHobbies);

// Result
(3) ["sports!", "cooking!", "reading!"]

Combine Array

const numbers = [1,2,4,5];
const all = [...hobbies, ...numbers];
console.log(all);

// Result
(7) ["sports", "cooking", "reading", 1, 2, 4, 5]

Destruct an Array

const userName = ["John", "Ted"];

// Old School Way
// const firstName = userName[0];
// const lastName = userName[1];

// New Way
const [firstName, lastName] = ["John", "Ted"];
console.log(firstName);
console.log(lastName);

// Result:
John
Ted

We can also destruct an Object like this:

const {name,quantity} = { name: "apples", quantity: 2 };
console.log(name);
console.log(quantity);

// Result:
apples
2
Leave a Comment

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