Best Python code snippet using slash
user_controller.py
Source:user_controller.py  
1from operator import or_2from flask import request3from flask_jwt_extended import jwt_required, get_jwt_identity, verify_jwt_in_request4from flask_restx import Resource5import app.util.response_message as message6from app.dto.user_dto import UserDto7from app.model.code_model import Code8from app.model.user_model import User9from app.service import user_service10from app.util.api_response import response_object11from app.util.auth_parser_util import get_auth_required_parser12api = UserDto.api13_message_response = UserDto.message_response14_update_parser = UserDto.update_parser15_filter_parser = UserDto.filter_parser16_filter_response = UserDto.user_list_response17_user_response = UserDto.user_response18_create_parser = UserDto.create_parser19@api.route('')20class UserListController(Resource):21    # tạm ok22    @api.doc('create user')23    @api.expect(_create_parser, validate=True)24    # @api.marshal_with(_message_response, 201)25    def post(self):26        """create user (Tạo tài khoản)"""27        args = _create_parser.parse_args()28        return user_service.create_user(args)29    # coi lại cái lấy từ parser30    # truyá»n jwt31    @api.doc('update user')32    @api.expect(_update_parser, validate=True)33    # @api.marshal_with(_message_response, 200)34    @jwt_required()35    def put(self):36        """update user"""37        # args = _update_parser.parse_args()38        args = _update_parser.parse_args()39        user_id = get_jwt_identity()['user_id']40        return user_service.update_user(args, user_id)41    # tạm42    @api.doc('filter user')43    @api.expect(_filter_parser, validate=True)44    # @api.marshal_with(_filter_response, 200)45    def get(self):46        """filter users"""47        args = _filter_parser.parse_args()48        page = args['page']49        page_size = args['page_size']50        is_tutor = True if args['is_tutor'] == 'true' or args['is_tutor'] else False51        users = User.query.filter(52            or_(User.email.like("%{}%".format(args['email'])), args['email'] is None),53            or_(User.first_name.like("%{}%".format(args['first_name'])), args['first_name'] is None),54            or_(User.last_name.like("%{}"55                                    "%".format(args['last_name'])), args['last_name'] is None),56            or_(User.sex == args['last_name'], args['sex'] is None),57            or_(User.is_tutor == is_tutor, args['is_tutor'] is None),58            # or_(User.birthday == args['birthday'], args['birthday'] is None),59            User.is_active60        ).paginate(page, page_size, error_out=False)61        return response_object(data=[user.to_json() for user in users.items],62                               pagination={'total': users.total, 'page': users.page}), 20063# chưa làm64@api.route('/inactive/<user_id>')65class Inactive(Resource):66    @api.doc('inactive')67    # @api.marshal_with(_message_response, 200)68    def get(self, user_id):69        """inactive user"""70        pass71        # args=_active_parser.parse_args()72        #73        # return user_service.active_user(args)74_active_parser = UserDto.active_parser75# ok76@api.route('/active')77class Active(Resource):78    @api.doc('active account')79    @api.expect(_active_parser, validate=True)80    def get(self):81        args = _active_parser.parse_args()82        return user_service.active_user(args)83_get_parser = get_auth_required_parser(api)84# chưa làm85@api.route('/<user_id>')86class GetById(Resource):87    @api.doc('get by id')88    @api.expect(_get_parser, validate=True)89    # @api.marshal_with(_user_response, 200)90    def get(self, user_id):91        """get by id user"""92        try:93            verify_jwt_in_request()94            author_id = get_jwt_identity()['user_id']95        except:96            author_id = None97        return user_service.get_by_id(user_id, author_id)98_profile_parser = get_auth_required_parser(api)99# jwt required , truyá»n user id vào100@api.route('/profile')101class Profile(Resource):102    @api.doc('profile')103    @api.expect(_profile_parser, validate=True)104    # #@api.marshal_with(_user_response, 200)105    @jwt_required()106    def get(self):107        """get profile"""108        user_id = get_jwt_identity()['user_id']109        return user_service.get_profile(user_id)110_update_avatar_parser = UserDto.update_avatar_parser111# còn jwt112@api.route('/update-avatar')113class UpdateAvatar(Resource):114    @api.doc('update avatar')115    @api.expect(_update_avatar_parser, validate=True)116    # @api.marshal_with(_message_response, 200)117    @jwt_required()118    def put(self):119        """update avatar"""120        user_id = get_jwt_identity()['user_id']121        file = request.files['file']122        return user_service.update_avatar(file, user_id)123_change_password_parser = UserDto.change_password_parser124# coi lại cái lấy từ parser125# cần jwt, truyên user id vào126@api.route('/password/change')127class ChangePassword(Resource):128    @api.doc('change password')129    @api.expect(_change_password_parser, validate=True)130    # @api.marshal_with(_message_response, 200)131    @jwt_required()132    def post(self):133        """change password"""134        # args = _change_password_parser.parse_args()135        user_id = get_jwt_identity()['user_id']136        args = request.json137        return user_service.change_password(args, user_id)138_forgot_password_parser = UserDto.forgot_password_parser139# maybe140@api.route('/password/forgot')141class ForgotPassword(Resource):142    @api.doc('forgot password')143    @api.expect(_forgot_password_parser, validate=True)144    # @api.marshal_with(_message_response, 200)145    def get(self):146        """forgot password"""147        email = _forgot_password_parser.parse_args()['email']148        return user_service.forgot_password(email)149_reset_parser = UserDto.reset_parser150# coi lại cái lấy từ parser151@api.route('/password/reset')152class Reset(Resource):153    @api.doc('reset password')154    @api.expect(_reset_parser, validate=True)155    # @api.marshal_with(_message_response, 200)156    def post(self):157        """reset password"""158        args = _reset_parser.parse_args()159        password = request.json['password']160        return user_service.reset_password(args, password)161_check_code_parser = UserDto.check_code_parser162@api.route('/code/check')163class CheckCode(Resource):164    @api.doc('check code')165    @api.expect(_check_code_parser, validate=True)166    # @api.marshal_with(_message_response, 200)167    def post(self):168        """check code"""169        args = _check_code_parser.parse_args()170        reset_code = Code.query.filter_by(email=args['email']).first()171        if not reset_code:172            return response_object(status=False, message=message.NOT_FOUND_404), 404173        if reset_code.code != args['code']:174            return response_object(status=False, message=message.NOT_FOUND_404), 404...serve.py
Source:serve.py  
...72    else:73        tmp_dir = args.tmpdir74    makedirs(tmp_dir)75    return tmp_dir76def _reset_parser(args):77    """78    Reset the parser and return the corresponding loop configuariton79    """80    tmp_dir = _mk_server_temp(args)81    soclog = fp.join(tmp_dir, "soclog")82    open(soclog, 'wb').close()83    hconf = StandaloneParser(soclog=soclog,84                             tmp_dir=tmp_dir)85    if hconf.test_evaluation is None:86        sys.exit("Can't run server: you didn't specify a test "87                 "evaluation in the local configuration")88    return hconf89def main(args):90    """91    Subcommand main.92    You shouldn't need to call this yourself if you're using93    `config_argparser`94    """95    check_3rd_party()96# pylint: disable=no-member97    context = zmq.Context()98    socket = context.socket(zmq.REP)99# pylint: enable=no-member100    socket.bind("tcp://*:{}".format(args.port))101    lconf = _reset_parser(args)102    while True:103        incoming = socket.recv()104        with open(lconf.soclog, 'ab') as fout:105            print(incoming.strip(), file=fout)106        run_pipeline(lconf, SERVER_STAGES)107        with open(xml_output_path(lconf), 'rb') as fin:108            socket.send(fin.read())109        if not args.incremental:...share_parser.py
Source:share_parser.py  
...32class SharesParser(object):33    def __init__(self):34        self._file = None35        self._reader = None36    def _reset_parser(self, file_path):37        if self._file:38            self._file.close()39        self._file = open(file_path, 'r')40        self._reader = csv.reader(self._file)41        # skips header line42        next(self._reader)43    @staticmethod44    def _should_parse_row(row):45        if float(row[1]) <= 0.0 or float(row[2]) <= 0.0:46            return False47        return True48    def get_shares_from_file(self, file_path, cents=False) -> list:49        shares      = []50        share_names = []51        self._reset_parser(file_path)52        for row in self._reader:53            if self._should_parse_row(row):54                share_names.append(row[0])55                new_share = Share(row[0], row[1], row[2], cents)56                shares.append(new_share)57        share_names_to_keep = [share_name for share_name, count in Counter(share_names).items() if count == 1]58        shares = [share for share in shares if share.name in share_names_to_keep]...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
