编程经验分享

笑看嫣红染半山,
逐风万里白云间,
逍遥此身不为客,
天地三才任平凡。

0%

Python中字符串编码类型

  • utf-8
  • gb2312
  • gbk

在Python中,一般用decode()encode()来对字符串进行解码和编码。

默认情况下,用unicode作为编码的基础类型。

str ------> unicode ------> str

1
2
3
4
5
6
u = u'中国人' # 定义变量的时候显式指定unicode类型
str2312 = u.encode('gb2312')
str_gbk = u.encode('gbk')
str_utf8 = u.encode('utf-8')
u_uc = str2312.decode('gb2312')
u_uc2 = str_utf8.decode('utf-8')

如上面代码,str\str1\str2均为字符串类型(str),给字符串操作带来较大的复杂性。

在新版本的python3中,取消了unicode类型,代替它的是使用unicode字符的字符串类型(str),字符串类型(str)成为了基础类型,而编码后的变为了**字节类型(bytes)**但是两个函数的使用方法不变:

bytes ------> str(unicode)------>bytes

1
2
3
4
u = '中国人' #指定字符串类型对象u
str2312 = u.encode('gb2312') #以gb2312编码对u进行编码,获得bytes类型对象str
u1 = str2312.decode('gb2312') #以gb2312编码对字符串str2312进行解码,获得字符串类型对象u1
u2 = str2312.decode('utf-8') #如果以utf-8的编码对str2312进行解码得到的结果,将无法还原原来的字符串内容
阅读全文 »

npm和yarn转换淘宝源和官方源

1
2
3
4
5
npm config set registry http://registry.npm.taobao.org/
npm config set registry https://registry.npmjs.org/

yarn config set registry http://registry.npm.taobao.org/
yarn config set registry https://registry.npmjs.org/
阅读全文 »

1
2
3
4
task renameBuildTask() << {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
dependsOn 'assembleRelease'
}

How to install cmake 3.3.2 on ubuntu 14.04?

Either use a PPA or compile it yourself:

Installation by a PPA (目前只能升级到3.2)

1
2
3
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt-get update
  • When cmake is not yet installed:
    sudo apt-get install cmake

  • When cmake is already installed:
    sudo apt-get upgrade

阅读全文 »

OP

This version requires Swift 2.0 If you use an earlier version please use an older version of my answer.

If you are looking for a more elegant solution you can use my µ-framework DeviceKit published on GitHub (also available via CocoaPods).

阅读全文 »

原文链接

let是Kotlin标准库中一个很优美的函数:

1
fun <T, R> T.let(f: (T) -> R): R = f(this)

`let`是一个scoping函数,能够让你在一个变量的范围内对它做一些事情:

1
2
3
File("a.txt").let {
// the file is now in the variable "it"
}

作用在一个可空引用变量(?)上是另一种用法,这样只有在该变量非空的时候才会执行let范围内的代码:

1
2
3
findUser(id)?.let {
// only run if findUser() returned a non null value
}

事实上,这种用法只是一种不带else分支的if用法的变种,是有缺陷的,变量不为空就执行代码,为空的话呢?

我们也可以显式进行lamda表达式调用:

1
2
3
4
val user = findUser(id)
user?.let { foundUser ->
// ...
}

和下面这样的用法基本上一样,下面的不翻译了,没啥意思。

1
2
3
4
val user = findUser(id)
if (user != null) {
// user is now a non null reference
}

原文: Higher Order Functions Woking With Containers

Kotlin标准库中提供了一些高级函数:average(求平均值), count(计算元素个数), distinct(求取唯一结果), filtering(条件过滤), finding(查找), grouping(分组), joining(连接), mapping(映射), min(最小值), max(最大值), partitioning(分区), slicing(切片), sorting(排序), summing, to/from arrays(数组转换), to/from lists(链表转换), to/from maps(Map转换), union, co-iteration等等,让我们更方便更简洁的用小小的一行代码就可以完成Java8里面复杂语法才能实现的功能。

阅读全文 »

Realm和gson混合使用的时候,包含如下字段的时候要么Realm不行,要么gson解析不了

1
@SerializedName("Files") var files: RealmList<String>? = null

这种情况下Realm无法对String处理,必须对String进行封装处理

阅读全文 »