分类 "Python" 下的文章

问题:方法中如何调用另一个类中__call__方法?

方法:

class foo(object):
    def __init__(self):
        self.name = 'python'

    def __call__(self, *args, **kwargs):
        print('hello%s'%self.name)

def run():
    instance = foo()
    instance()
    a = instance
    print dir(a)

阅读全文

问题:如何使用python对接cas统一认证系统,实现用户登录?

解决:使用模块python-cas

方法:

from cas import CASClient

cas_client = CASClient(
    version=3,
    service_url='http://xx.com/login?next=%2Fprofile',
    server_url='http://sso.xx.xx.cn/cas/'
)


@router.get("/", name="测试cas")
async def index():
    return RedirectResponse('/login')

阅读全文

问题:python使用threading实现线程时,如何调用异步函数?

解决:使用asyncio.run

方法:

async def import_users(db, temp_name):
    # 我的处理方法
    pass
lock = threading.Lock()
def thread_target():
    with lock:
        asyncio.run(import_users(db, name))

threads = [threading.Thread(target=thread_target, args=(db, temp.name)) for _ in range(10)]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

阅读全文

问题:使用pyhive连接后使用pandas会预警,让使用sqlalchemy

方法:

from sqlalchemy.engine import create_engine
from .config import config

database_config = { 
    'host': config['HV_SVR'],
    'port': 10000,
    'auth': "CUSTOM",
    'database': config['HV_DB'],
    'user': config['HV_USER'],
    'password': config['HV_PASS']
}

阅读全文