2020年03月

2020-3-2 周一

Anaconda 更换源

conda config –add channels ‘https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
或者在GUI页面里操作添加也是一样的:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config –set show_channel_urls yes

删除源

conda config –remove channels ‘https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

查看当前配置

conda config –show

PIP 更换源

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib

2020-3-3 周二

Tensorflow Docker 镜像:

 tensorflow/tensorflow:latest
tensorflow/tensorflow:latest-py3
tensorflow/tensorflow:2.1.0-py3
tensorflow/tensorflow:2.1.0-py3-jupyter

 

在镜像中设置 jupyter 的密码

 ________                               _______________
___  __/__________________________________  ____/__  /________      __
__  /  _  _ \_  __ \_  ___/  __ \_  ___/_  /_   __  /_  __ \_ | /| / /
_  /   /  __/  / / /(__  )/ /_/ /  /   _  __/   _  / / /_/ /_ |/ |/ /
/_/    \___//_/ /_//____/ \____//_/    /_/      /_/  \____/____/|__/


WARNING: You are running this container as root, which can cause new files in
mounted volumes to be created as the root user on your host machine.

To avoid this, run the container by specifying your user's userid:

$ docker run -u $(id -u):$(id -g) args...

root@3f616f73542d:/tf# jupyter notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json

 

2020-3-4 周三

环境相关

python 3.6.9

 python -m pip install --upgrade pip
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow==1.10.9
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyMySQL==0.9.3
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scikit_learn==0.22.2
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple seaborn==0.10.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple snownlp==0.12.3

 

pandas==0.25.3

2019-3-5 周四

在 Resource 里面判断对象的类型

 if ($this->resource instanceof User) {
    // DO SOMETHING
}

 

2019-3-6 周五

MySQL 分割数据列

有一列数据都是 3*6 这样的,需要将它分为两列数据,一列是乘号左边 ,一列是乘号右边的
使用 MySQL 自带的函数即可完成:

 SUBSTRING_INDEX(o.`size`,'*',1) AS order_size_a,
SUBSTRING_INDEX(o.`size`,'*',-1) AS order_size_b,

 

2019-3-9 周一

Pandas 唯一值

 df['column'].unique()
df['column'].value_counts()

a = list(df['column'].unique())  # 列出所有唯一值
len(df['column'].unique())  # 唯一值的个数
b = pd.DataFrame(df['column'].value_counts())  # 统计每个唯一值出现了多少次

 

2019-3-10 周二

Laravel 查询去重

需要显示记录中一个订单号的同一个记录
如在下表中筛选出 ID 1、3、4 的记录:

 ID   Name  Membership Id 
1    xxx   123
2    xxx   124
3    xxx   124 
4    xxx   125 

 

Laravel 框架中:

 ->
->whereIn('id', DB::table('user_records')
    ->selectRaw('max(id) as id')
    ->groupBy('order_id')
    ->pluck('id')
    ->toArray())
->

 

本质上其实相当于原生 SQL 语句:

 SELECT
	* 
FROM
	article 
WHERE
	id IN ( SELECT max( id ) FROM article GROUP BY author ) 
ORDER BY
	create_at DESC;

 

当然还有一定的优化空间

2020-3-11 周三

统计代码行数

工具:Cloc

 cloc.exe C:/doc/example
     138 text files.
     137 unique files.
     109 files ignored.

github.com/AlDanial/cloc v 1.84  T=0.50 s (150.0 files/s, 39014.0 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Python                          37           2949           1509           3514
Jupyter Notebook                27              0           7851           2769
XML                              7              0              0            273
Markdown                         1            236              0            220
HTML                             2             17              0            143
Bourne Shell                     1              5              0             21
-------------------------------------------------------------------------------
SUM:                            75           3207           9360           6940
-------------------------------------------------------------------------------

 

可以很方便的统计出该目录下不同类型的文件的行数

2020-3-12 周四

Kaggle 可视化

全球疫情可视化数据

2020-3-13 周五

XGBoost 模型

XGBoost(eXtreme Gradient Boosting) 是 Gradient Boosting 的优化版本,主要是以正则化提升为主

2020-3-16 周一

Laravel 找到被删除的

在模型里面加上 withTrashed 即可

 return $this->hasMany('App\Models\User')->withTrashed();

 

2020-3-17 周二

Resource 返回值里加一些 meta 数据

直接给 Resource 赋值是不行的,需要先把它 toResponse 后再 json_decode 即可:

 $result = OrderListResource::collection($orders)->toResponse($request);
$result = json_decode($result->getContent(), true);

// 添加计数器数据
$aftermarketCounter = $orderService->unDealAftermarket();
$result['result']['meta']['un_finance'] = $orderService->unAssignToFinance($request);
$result['result']['meta']['un_deal_change'] = $aftermarketCounter->change;
$result['result']['meta']['un_deal_refund'] = $aftermarketCounter->refund;

return $result;

 

2020-3-18 周三

Laravel 查询优雅的一种写法

可以使用 whereX

 User::where('age', '=', 18)->get();
// 写成这样
User::whereAge(18)->get();

 

2020-3-19 周四

PHP 精度问题

有可能存在
在数据库中存储的值是一样的,但是拿出来后在比较大小的时候被视为不一样的值,这个问题相减后看差值,如果小于阈值则将两个值视为同一个值即可:

 // 精度问题
if (abs($a - $sum) < 0.01) {
    $sum = $a;
}

 

2020-3-20 周五

Python Pandas 包含

 aa = pd.DataFrame({'site_name':['杭州银泰','万达广场','城西银泰', '杭州万达', '杭州银泰,万达广场,城西银泰,杭州万达'],
                   '数量':[3,2,5,3,4],
                   '价格':[10,9,8,9,7]})

# 包含关键词
cc = aa[aa.site_name.str.contains('杭州')]
# 不包含关键词
dd = aa[~aa.site_name.str.contains("万达")]

 

其中,不包含的时候写成 aa.site_name.str.contains("万达") == False 也是可以生效的
但是 PyCharm 会在 == 处给出警告:

 PEP 8: comparison to False should be 'if cond is False:' or 'if not cond:'

 

不符合代码规范还是更正为好

2020-3-23 周一

Python 俩数组是否重合

两个数组是否有交集,如果有交集则执行一些命令:

 if list(set([1,2,3]).intersection(set([3,5,6]))):
    # TODO: SOMETHING
    print('有交集')

 

这里是将数组转换成了 set 再进行判断的

2020-3-24 周二

SQL 查询顶级父类的ID

有这样一个表:

id parent_id name
1 null 水果
2 1 柚子
3 1 草莓
4 null 蔬菜
5 4 茄子
6 4 山药
7 4 胡萝卜
8 4 有机蔬菜
9 8 有机花菜
10 8 有机卷心菜
……

需求是给任意一个ID要查出它所属的最高父类,这里的嵌套层级可能有很多级的那种
这里使用 MySQL 查询实现,不使用函数:

 -- 在 vegetable 表中
-- 查询 ID 为 99999 的父类
SELECT
    t2.id 
FROM
    (
    SELECT
        @r AS _id,
        ( SELECT @r := parent_id FROM vegetable WHERE id = _id ) AS parent_id,
        @l := @l + 1 AS lvl 
    FROM
        ( SELECT @r := 99999, @l := 0 ) vars,
        vegetable h 
    WHERE
        @r <> 0 
    ) t1
    JOIN vegetable t2 ON t1._id = t2.id 
WHERE
    t2.parent_id IS NULL 
    AND deleted_at IS NULL 
ORDER BY
    t1.lvl DESC
LIMIT 1

 

2020-3-25 周三

GitLab Runner 注册

注册的时候按照 gitlab-runner register 的提示来就行,我这里以前用的是 shell,现在用 docker 试试

 root@2857745ad80a:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=38 revision=4c96e5ad version=12.9.0
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.tabll.cn/
Please enter the gitlab-ci token for this runner:
8eqgKLv-tFkNPKrfRAPG
Please enter the gitlab-ci description for this runner:
[2857745ad80a]: Python runner
Please enter the gitlab-ci tags for this runner (comma separated):
python
Registering runner... succeeded                     runner=8eqgKLv-
Please enter the executor: ssh, docker+machine, kubernetes, shell, virtualbox, docker-ssh+machine, custom, docker, docker-ssh, parallels:
docker
Please enter the default Docker image (e.g. ruby:2.6):
tensorflow/tensorflow:2.1.0-py3
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

 

有关 GitLab Runner 的更多配置可以查看:官方文档

2020-3-26 周四

GitLab Runner 更换镜像源

 vim /etc/apt/sources.list

 

将其中所有的链接更换成:http://mirrors.aliyun.com/ubuntu/
然后重新执行就好了

2020-3-27 周五

Ubuntu 安装 Docker

 apt install apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

 

添加 Docker 的官方 GPG 密钥:

 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

 
 root@2857745ad80a:/# apt-key fingerprint 0EBFCD88
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

 

添加仓库:

 add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

 

安装 Docker

 apt install docker-ce docker-ce-cli containerd.io

 

2020-3-28 周六

GitLab Runner 使用 Docker

报错:

  Running with gitlab-runner 12.9.0 (4c96e5ad)
   on Python runner LxqsJkmi
Preparing the "docker" executor
 ERROR: Removing network for build
 ERROR: Preparation failed: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? (docker.go:1027:0s)
 Will be retried in 3s ...
 ERROR: Removing network for build
 ERROR: Preparation failed: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? (docker.go:1027:0s)
 Will be retried in 3s ...
 ERROR: Removing network for build
 ERROR: Preparation failed: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? (docker.go:1027:0s)
 Will be retried in 3s ...
 ERROR: Job failed (system failure): Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? (docker.go:1027:0s)

 

我本来以为是要在 GitLab Runner 的 Docker 里面再安装一个 Docker
其实原因是没有映射本机的 Docker

 Host/volume	Path in container
edd94359a1f9a7bef4eb7cb74b72a01436c42fe8f8c2abab17c69dc03da29a1d	/etc/gitlab-runner
2fd5a1303dbb34d5f0e818375a4f214857623f70beb84d561e02e6734e533c66	/home/gitlab-runner
/var/run/docker.sock	/var/run/docker.sock

 

2020-3-30 周一

Tox 设置

被推荐使用 tox 来运行单元测试
tox.ini 文件

 [tox]
envlist = py36
indexserver =
    default = https://pypi.tuna.tsinghua.edu.cn/simple

[testenv]
deps = pytest-cov
commands =
    pytest --cov-config=.coveragerc --cov=./ tests/

 

这样显示出来的单元测试覆盖率才能够被 GitLab 识别
其中 .coveragerc 文件是单元测试覆盖时忽略的文件:

 [run]
omit =
    */__init__.py
    */.tox/*

 

2020-3-31 周二

GitLab Python CI

 image: tensorflow/tensorflow:2.1.0-py3

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python -V  # Print out python version for debugging
  - pip install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv
  - virtualenv venv
  - source venv/bin/activate

test:
  script:
    - pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pytest pytest-cov
#    - python setup.py test
    - pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tox flake8
    - tox

 

唤醒精灵