分享几个实用的问题

阅读本文大概需要 3 分钟。

1、Python 有类似 jar 包的机制吗?

有。

一直很羡慕 Java 的一次打包,处处运行,这在内网环境往往非常方便, 我们在外网开发,可以很方便的下载依赖包,开发完后,打成 jar 包后在内网处处运行,无需再处理各种依赖。

Python 也是支持这种机制的,我们可以尝试创建一个 __main__.py 文件作为入口文件,将依赖的 py 文件都放在这里,然后使用 zip 打包,别人使用时可以直接运行此 zip 包:

$ cat hello.py
def main():
    print("func from main")
$ cat __main__.py
import hello
hello.main()
$ zip demo.zip ./*
  adding: __main__.py (deflated 8%)
  adding: __pycache__/ (stored 0%)
  adding: hello.py (deflated 8%)
$ python demo.zip
func from main


2、无论程序是否报错、抛异常、被 kill,我都要执行一段代码,怎么实现?

标准库 atexit 就是解决这个问题的。使用 atexit.register 注册一个函数, 该函数在在脚本运行完后立马执行,无论当前脚本是否报错、抛异常、被 kill,可用于脚本执行结束时测量一些基准数据,比如运行了多长时间:

import atexit
import time

start_time = time.time()
def shutdown():
    print("Execution took: {0} seconds".format(time.time() - start_time))

atexit.register(shutdown)
time.sleep(3)
print(1/0)

运行结果如下:

 File "/Users/aaronbrant/test.py", line 10, in <module>
    print(1/0)
ZeroDivisionError: division by zero
Execution took: 3.0044968128204346 seconds

可以使用 atexit.register 注册多个函数,注册函数相当于把函数放入一个栈中,如果一次注册  A,B,C 三个函数,程序结束时的执行顺序是 C、B、A。

3、Python 有哪些魔法函数?

所谓魔法函数(Magic Methods),是 Python 的高级语法,允许你在类中自定义函数,函数名格式一般为__xx__

比如在类 A 中自定义__str__()函数,则在调用 str(A()) 或者 print(A()) 时,会自动调用__str__()函数,并返回相应的结果。

经常使用的 __init____del__函数,其实这也是魔法函数的一种。此外还有很多,这里列举下:

二元操作符:

+    object.__add__(self, other)
-    object.__sub__(self, other)
*    object.__mul__(self, other)
//    object.__floordiv__(self, other)
/    object.__div__(self, other)
%    object.__mod__(self, other)
**    object.__pow__(self, other[, modulo])
<<    object.__lshift__(self, other)
>>    object.__rshift__(self, other)
&    object.__and__(self, other)
^    object.__xor__(self, other)
|    object.__or__(self, other)

扩展二元操作符:

+=    object.__iadd__(self, other)
-=    object.__isub__(self, other)
*=    object.__imul__(self, other)
/=    object.__idiv__(self, other)
//=    object.__ifloordiv__(self, other)
%=    object.__imod__(self, other)
**=    object.__ipow__(self, other[, modulo])
<<=    object.__ilshift__(self, other)
>>=    object.__irshift__(self, other)
&=    object.__iand__(self, other)
^=    object.__ixor__(self, other)
|=    object.__ior__(self, other)

一元操作符:

-    object.__neg__(self)
+    object.__pos__(self)
abs()    object.__abs__(self)
~    object.__invert__(self)
complex()    object.__complex__(self)
int()    object.__int__(self)
long()    object.__long__(self)
float()    object.__float__(self)
oct()    object.__oct__(self)
hex()    object.__hex__(self)
round()    object.__round__(self, n)
floor()    object__floor__(self)
ceil()    object.__ceil__(self)
trunc()    object.__trunc__(self)

比较函数:

<    object.__lt__(self, other)
<=    object.__le__(self, other)
==    object.__eq__(self, other)
!=    object.__ne__(self, other)
>=    object.__ge__(self, other)
>    object.__gt__(self, other)

类的表示、输出:

str()    object.__str__(self)
repr()    object.__repr__(self)
len()    object.__len__(self)
hash()    object.__hash__(self)
bool()    object.__nonzero__(self)
dir()    object.__dir__(self)
sys.getsizeof()    object.__sizeof__(self)

类容器:

len()    object.__len__(self)
self[key]    object.__getitem__(self, key)
self[key] = value    object.__setitem__(self, key, value)
del[key] object.__delitem__(self, key)
iter()    object.__iter__(self)
reversed()    object.__reversed__(self)
in操作    object.__contains__(self, item)
字典key不存在时    object.__missing__(self, key)


4、Django 如何使用 db2 并设置 current schema ?

不少金融机构使用 db2,如果想基于 db2 使用 Python 做数据分析的,免不了安装 ibm 的驱动:

如果是 Windows 或 Linux:

pip install ibm_db

如果是 Mac:

pip install --no-cache-dir ibm_db

如果安装后有问题,请参考 https://blog.csdn.net/somezz/article/details/80745695 解决。

如果想使用 Django,还需要安装这个:

$ pip install ibm_db_django==1.2.0.0a0

然后修改配置文件,加入 db2 数据库的配置,设置模式名。

DATABASES = {
      'default': {
         'ENGINE'     : 'ibm_db_django',
         'NAME'       : 'mydb',
         'USER'       : 'db2inst1',
         'PASSWORD'   : 'ibmdb2',
         'HOST'       : 'localhost',
         'PORT'       : '50000',
         'PCONNECT'   :  True,      #Optional property, default is false
         'CURRENTSCHEMA'   : 'MYSCHEMA',      #Optional property, default is false
      }
}


5、如果需要系统学习下 shell 脚本编程的,有没有好的教程?

有,强烈推荐阮一峰的新作 《Bash 脚本教程》https://wangdoc.com/bash/

6、有哪些好用的在线 markdown 编辑器?
  • http://md.aclickall.com/

  • https://doocs.gitee.io/md/


7、和 github 一样好用,速度又快的代码托管网站是那个?

gitee : https://gitee.com/

Gitee 是国内数一数二的代码托管平台,用于管理软件代码,功能完全不输国外同类网站,而且有很多针对国内用户的本地化设计(比如文件级别的只读权限)。它的突出优势就是访问速度极快,各种操作基本都可以瞬间完成,没有卡顿。

如果您对文章感兴趣,请微信搜索「Python七号」并关注。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值