V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
llsquaer
V2EX  ›  Python

pycharm, 怎么快速将单文件中所有函数改为类

  •  
  •   llsquaer · 2022-06-08 18:51:07 +08:00 · 1544 次点击
    这是一个创建于 694 天前的主题,其中的信息可能已经有所发展或是发生改变。

    同事写的另一个业务 py 文件..我直接 import 调用.. 可以运行 .

    现在因为业务修改了..

    需要多线程运行..其中有 2 个 cookies 变量 需要启动线程,进行修改.

    但是里面所有内容都是 全局变量 + 函数 写的

    所以想的办法是直接将文件内容提升为 thread 类 ..

    源文件,伪代码如下:

    # 原来的是写死在 config 文件里了.需要修改就到 config 修改..
    # 现在改为多线程, 每个线程一个账号. 需要为每个 thread 改 cookies.
    
    cookies = {'xxx':'xxxx1'}
    cookies2 = {'xxx2':'xxxx2'}
    
    headers = { ....... }
    
    periods = { .......  }
    coupon_list = []
    
    def page1():
    	xxx= request.get(url,headers=headers, cookies=cookies)
    	....
    
    def page2():
    	xxx= request.get(url,headers=headers, cookies=cookies2)
    	....
     
    def page3():
    	# 各种页面请求逻辑
    	xxx= request.get(url,headers=headers, cookies=cookies2)
    	....
     
    
    

    现在的问题是 整个文件 1320+ 行 , 涉及到使用 这 2 个全局变量的函数有 50 多个. 不想一个一个函数的去添加 self ,赋值还得需要添加 self

    请问,有没有快速的方法呢.. 谢谢啦

    3 条回复    2022-06-09 10:54:58 +08:00
    aloxaf
        1
    aloxaf  
       2022-06-08 19:12:37 +08:00
    就一个文件内,Find & Replace 不行么(可以先用重构把变量名换成不会冲突的,再直接替换
    fgwmlhdkkkw
        2
    fgwmlhdkkkw  
       2022-06-08 20:46:18 +08:00
    flask LocalProxy
    julyclyde
        3
    julyclyde  
       2022-06-09 10:54:58 +08:00
    refactor 功能?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2323 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:10 · PVG 17:10 · LAX 02:10 · JFK 05:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.