Skip to content

Instantly share code, notes, and snippets.

@YT-er
Last active March 7, 2019 07:25
Show Gist options
  • Select an option

  • Save YT-er/8512ea07a384c9143e3c3f4a4fd00b95 to your computer and use it in GitHub Desktop.

Select an option

Save YT-er/8512ea07a384c9143e3c3f4a4fd00b95 to your computer and use it in GitHub Desktop.
随时记录问题和结果

每天有点小疑问

20190110

Q:assert是什么?

A:python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假。可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常。

assert expression

>>> a_str = 'this is a string'
>>> type(a_str)
<type 'str'>
>>> assert type(a_str)== str
>>> assert type(a_str)== int

Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    assert type(a_str)== int
AssertionError

Q: any()函数

A:如果都为空、0、false,则返回false,如果不都为空、0、false,则返回true

20190111

Python下安装unrar后仍然提示Couldn't find path to unrar library

Win:

  1. 先到RARLab官方下载库文件,http://www.rarlab.com/rar/UnRARDLL.exe ,然后安装;

  2. 安装最好选择默认路径,一般在 C:\Program Files (x86)\UnrarDLL\ 目录下;

  3. 然后重要的一步,就是添加环境变量,此电脑(我的电脑)右键,属性,找到 高级系统设置,高级 选项卡下点击 环境变量,在系统变量(注意不是用户变量)中 新建,变量名输入 UNRAR_LIB_PATH ,必须一模一样,变量值要特别注意!如果你是64位系统,就输入 C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll,如果是32位系统就输入 C:\Program Files (x86)\UnrarDLL\UnRAR.dll ,这个从unrar安装目录的内容也能看出来它是区分64和32位的。

  4. 确定保存环境变量后,重启你的IDE,代码不变,再运行就不会出错了。这个时候依赖库已经添加到系统环境中。

原文

Python多线程运行带多个参数的函数(map)

用partial函数提取主要的一个参数固定其他参数 比如,我们想用x去加一个固定值,那么我们就认为x是主要参数,固定y:

from functools import partial

partial_work = partial(work, y=1) # 提取x作为partial函数的输入变量
results = pool.map(partial_work, x)

20190114

lru_cache装饰器有什么用?

functools.lru_cache的作用主要是用来做缓存,他能把相对耗时的函数结果进行保存,避免传入相同的参数重复计算。同时,缓存并不会无限增长,不用的缓存会被释放。

from clockdeco import clock

@clock
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-2) + fibonacci(n-1)

if __name__=='__main__':
    print(fibonacci(6))

0.00011096s

from clockdeco import clock

@functools.lru_cache()
@clock
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-2) + fibonacci(n-1)

if __name__=='__main__':
    print(fibonacci(6))

0.00006876s

20190115

repr()的用法

将变量或常量转化成字符串,和str()功能类似。

str()的区别:

  • str()的输出追求可读性,输出格式要便于理解,适合用于输出内容到用户终端。
  • repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用。

20190117

配置jupyter notebook

安装jupyterthemes

jt -t solarizedl -f fira -fs 14 -cellw 90% -ofs 11 -dfs 11 -T

20190124

bisect二分查找的用法

Bisect 模块提供的函数可以分两类: bisect* 只用于查找 index, 不进行实际的插入;而 insort* 则用于实际插入。

简化分支代码:

import time
import bisect


def from_now0(ts):
    """
    接收一个过去的时间戳,返回距离当前时间的相对时间文字描述
    """
    now = time.time()
    seconds_delta = int(now - ts)
    if seconds_delta < 1:
        return "less than 1 second ago"
    elif seconds_delta < 60:
        return f"{seconds_delta} seconds ago"
    elif seconds_delta < 3600:
        return f"{seconds_delta // 60} minutes ago"
    elif seconds_delta < 3600 * 24:
        return f"{seconds_delta // 3600} hours ago"
    else:
        return f"{seconds_delta // (3600 * 24)} days ago"



BREAKPOINTS = (1, 60, 3600, 3600*24)
TMPLS = (
    (1, "less than 1 second ago"),
    (1, "{units} seconds ago"),
    (60, "{units} minutes ago"),
    (3600, "{units} hours ago"),
    (3600*24, "{units} days ago"),
)

def from_now(ts):
    seconds_delta = int(time.time() - ts)
    unit, tmpl = TMPLS[bisect.bisect(BREAKPOINTS, seconds_delta)]
    return tmpl.format(units = seconds_delta // unit)


now = time.time()

print(from_now(now))
print(from_now(now - 24))
print(from_now(now - 600))
print(from_now(now - 7500))
print(from_now(now - 87500))

20190307

python执行linux命令,获取输出

p = os.popen('uptime')
x=p.read()
print x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment