推荐学习书目
› 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 · Jun 8, 2022 · 2344 views
    This topic created in 1581 days ago, the information mentioned may be changed or developed.

    同事写的另一个业务 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 replies  •  2022-06-09 10:54:58 +08:00
    aloxaf
        1
    aloxaf  
       Jun 8, 2022
    就一个文件内,Find & Replace 不行么(可以先用重构把变量名换成不会冲突的,再直接替换
    fgwmlhdkkkw
        2
    fgwmlhdkkkw  
       Jun 8, 2022
    flask LocalProxy
    julyclyde
        3
    julyclyde  
       Jun 9, 2022
    refactor 功能?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2591 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 14:17 · PVG 22:17 · LAX 07:17 · JFK 10:17
    ♥ Do have faith in what you're doing.