Skip to main content

2 posts tagged with "python"

View All Tags

· 2 min read

使用black来进行格式化

https://github.com/psf/black

在vscode中配置

20210913084128.png

在pycharm或者idea中配置

  1. Install black. `$ pip install black`
  2. Locate your black installation folder. On macOS / Linux / BSD: `$ which black /usr/local/bin/black # possible location On Windows: **$ where black %LocalAppData%\Programs\Python\Python36-32\Scripts\b*lack.exe # possible location* Note that if you are using a virtual environment detected by PyCharm, this is an unneeded step. In this case the path to black is $PyInterpreterDirectory$/black`.
  3. Open External tools in PyCharm/IntelliJ IDEA On macOS: PyCharm -> Preferences -> Tools -> External Tools On Windows / Linux / BSD: File -> Settings -> Tools -> External Tools
  4. Click the + icon to add a new external tool with the following values:
  • Name: Black
  • Description: Black is the uncompromising Python code formatter.
  • Program: <install_location_from_step_2>
  • Arguments: "$FilePath$"
  1. Format the currently opened file by selecting Tools -> External Tools -> black.
  • Alternatively, you can set a keyboard shortcut by navigating to Preferences or Settings -> Keymap -> External Tools -> External Tools - Black.
  1. Optionally, run Black on every file save:
  2. Make sure you have the File Watchers plugin installed.
  3. Go to Preferences or Settings -> Tools -> File Watchers and click + to add a new watcher:
    • Name: Black
    • File type: Python
    • Scope: Project Files
    • Program: <install_location_from_step_2>
    • Arguments: $FilePath$
    • Output paths to refresh: $FilePath$
    • Working directory: $ProjectFileDir$
  • In Advanced Options
    • Uncheck “Auto-save edited files to trigger the watcher”
    • Uncheck “Trigger the watcher on external changes”

· 3 min read

PYC编码

使用python自带的编译命令,编译成对应的二进制程序,还是可以被反编译,但是至少无法被直接查看。

pyc除了看不到源代码,其他使用上和py文件,没有太大差异。

在settools打包egg

# 打包
python ./setup/all.py bdist_egg --exclude-source-files

# 运行
egg_path='/home/shahid/suds_2.4.egg'
sys.path.append(egg_path)
import suds

suds.run()

普通情况下的编译

这种方式比较麻烦,因为pyc作为临时文件,是不推荐直接删除python文件。需要自己编写脚本完成下面部署:

  1. py_compile 强制编译生成pyc.
  2. 删除py文件。
python -m compileall .

find . -name "__pycache__" -exec rm -f {} \;
find . -name "*.py" -exec rm -rf {} \;

CPYTHON

使用cpython将python代码编译成C/C++,然后再编译成python扩展模块,windows上为pyd文件,Linux上为so文件.

打包成可执行文件

发现pyinstaller这个打包工具比较受人欢迎,经它打包后的exe文件可以在无python的环境下运行。

如果项目是只是服务中的依赖包例如egg就无法实现了。

代码混淆

命令: pyminifier

在线:http://pyob.oxyry.com/

代码混淆方案的缺陷在于,只能针对一个文件进行混淆,这样作为一个项目互相调用肯定有问题。(类名和方法名会变化)

需要针对需要混淆的文件,进行模块化设计。

虚拟机封装

在虚拟机安转好以后,修改登录密码,并且不告知客户。所有对虚拟机的操作通过开发服务进行调用。