Best Python code snippet using lisa_python
KernelSetterService_pb2_grpc.py
Source:KernelSetterService_pb2_grpc.py  
1# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!2"""Client and server classes corresponding to protobuf-defined services."""3import grpc4import Setter.KernelSetterService_pb2 as KernelSetterService__pb25class SETTERStub(object):6    """Missing associated documentation comment in .proto file."""7    def __init__(self, channel):8        """Constructor.9        Args:10            channel: A grpc.Channel.11        """12        self.REMOVE_MODULE = channel.unary_unary(13                '/SETTER/REMOVE_MODULE',14                request_serializer=KernelSetterService__pb2.REQUEST.SerializeToString,15                response_deserializer=KernelSetterService__pb2.REPLY.FromString,16                )17        self.MODPROBE = channel.unary_unary(18                '/SETTER/MODPROBE',19                request_serializer=KernelSetterService__pb2.REQUEST.SerializeToString,20                response_deserializer=KernelSetterService__pb2.REPLY.FromString,21                )22        self.DEPLOY_MODULE = channel.unary_unary(23                '/SETTER/DEPLOY_MODULE',24                request_serializer=KernelSetterService__pb2.REQUEST.SerializeToString,25                response_deserializer=KernelSetterService__pb2.REPLY.FromString,26                )27        self.INSTALL_KERNEL_OBJECT = channel.unary_unary(28                '/SETTER/INSTALL_KERNEL_OBJECT',29                request_serializer=KernelSetterService__pb2.KERNEL_OBJECT.SerializeToString,30                response_deserializer=KernelSetterService__pb2.REPLY.FromString,31                )32class SETTERServicer(object):33    """Missing associated documentation comment in .proto file."""34    def REMOVE_MODULE(self, request, context):35        """Missing associated documentation comment in .proto file."""36        context.set_code(grpc.StatusCode.UNIMPLEMENTED)37        context.set_details('Method not implemented!')38        raise NotImplementedError('Method not implemented!')39    def MODPROBE(self, request, context):40        """Missing associated documentation comment in .proto file."""41        context.set_code(grpc.StatusCode.UNIMPLEMENTED)42        context.set_details('Method not implemented!')43        raise NotImplementedError('Method not implemented!')44    def DEPLOY_MODULE(self, request, context):45        """Missing associated documentation comment in .proto file."""46        context.set_code(grpc.StatusCode.UNIMPLEMENTED)47        context.set_details('Method not implemented!')48        raise NotImplementedError('Method not implemented!')49    def INSTALL_KERNEL_OBJECT(self, request, context):50        """Missing associated documentation comment in .proto file."""51        context.set_code(grpc.StatusCode.UNIMPLEMENTED)52        context.set_details('Method not implemented!')53        raise NotImplementedError('Method not implemented!')54def add_SETTERServicer_to_server(servicer, server):55    rpc_method_handlers = {56            'REMOVE_MODULE': grpc.unary_unary_rpc_method_handler(57                    servicer.REMOVE_MODULE,58                    request_deserializer=KernelSetterService__pb2.REQUEST.FromString,59                    response_serializer=KernelSetterService__pb2.REPLY.SerializeToString,60            ),61            'MODPROBE': grpc.unary_unary_rpc_method_handler(62                    servicer.MODPROBE,63                    request_deserializer=KernelSetterService__pb2.REQUEST.FromString,64                    response_serializer=KernelSetterService__pb2.REPLY.SerializeToString,65            ),66            'DEPLOY_MODULE': grpc.unary_unary_rpc_method_handler(67                    servicer.DEPLOY_MODULE,68                    request_deserializer=KernelSetterService__pb2.REQUEST.FromString,69                    response_serializer=KernelSetterService__pb2.REPLY.SerializeToString,70            ),71            'INSTALL_KERNEL_OBJECT': grpc.unary_unary_rpc_method_handler(72                    servicer.INSTALL_KERNEL_OBJECT,73                    request_deserializer=KernelSetterService__pb2.KERNEL_OBJECT.FromString,74                    response_serializer=KernelSetterService__pb2.REPLY.SerializeToString,75            ),76    }77    generic_handler = grpc.method_handlers_generic_handler(78            'SETTER', rpc_method_handlers)79    server.add_generic_rpc_handlers((generic_handler,))80 # This class is part of an EXPERIMENTAL API.81class SETTER(object):82    """Missing associated documentation comment in .proto file."""83    @staticmethod84    def REMOVE_MODULE(request,85            target,86            options=(),87            channel_credentials=None,88            call_credentials=None,89            insecure=False,90            compression=None,91            wait_for_ready=None,92            timeout=None,93            metadata=None):94        return grpc.experimental.unary_unary(request, target, '/SETTER/REMOVE_MODULE',95            KernelSetterService__pb2.REQUEST.SerializeToString,96            KernelSetterService__pb2.REPLY.FromString,97            options, channel_credentials,98            insecure, call_credentials, compression, wait_for_ready, timeout, metadata)99    @staticmethod100    def MODPROBE(request,101            target,102            options=(),103            channel_credentials=None,104            call_credentials=None,105            insecure=False,106            compression=None,107            wait_for_ready=None,108            timeout=None,109            metadata=None):110        return grpc.experimental.unary_unary(request, target, '/SETTER/MODPROBE',111            KernelSetterService__pb2.REQUEST.SerializeToString,112            KernelSetterService__pb2.REPLY.FromString,113            options, channel_credentials,114            insecure, call_credentials, compression, wait_for_ready, timeout, metadata)115    @staticmethod116    def DEPLOY_MODULE(request,117            target,118            options=(),119            channel_credentials=None,120            call_credentials=None,121            insecure=False,122            compression=None,123            wait_for_ready=None,124            timeout=None,125            metadata=None):126        return grpc.experimental.unary_unary(request, target, '/SETTER/DEPLOY_MODULE',127            KernelSetterService__pb2.REQUEST.SerializeToString,128            KernelSetterService__pb2.REPLY.FromString,129            options, channel_credentials,130            insecure, call_credentials, compression, wait_for_ready, timeout, metadata)131    @staticmethod132    def INSTALL_KERNEL_OBJECT(request,133            target,134            options=(),135            channel_credentials=None,136            call_credentials=None,137            insecure=False,138            compression=None,139            wait_for_ready=None,140            timeout=None,141            metadata=None):142        return grpc.experimental.unary_unary(request, target, '/SETTER/INSTALL_KERNEL_OBJECT',143            KernelSetterService__pb2.KERNEL_OBJECT.SerializeToString,144            KernelSetterService__pb2.REPLY.FromString,145            options, channel_credentials,...checkpoint.py
Source:checkpoint.py  
1# Copyright (c) 2018- Salas Lin (leVirve)2#3# This software is released under the MIT License.4# https://opensource.org/licenses/MIT5import os6from pathlib import Path7from collections import OrderedDict8import torch9from .base import Extension, unique_experiment_name10def export_checkpoint_weight(checkpoint_path, remove_module=True):11    def clean_module(state_dict):12        new_state_dict = OrderedDict()13        for k, v in state_dict.items():14            new_state_dict[k.replace('module.', '')] = v15        return new_state_dict16    ckpt = torch.load(checkpoint_path)17    weight = ckpt['weight']18    return clean_module(weight) if remove_module else weight19class Checkpoint(Extension):20    r""" Checkpoint manager for model saving and restoring.21    Args:22        rootdir (str): the root folder for checkpoint manager (default: ``exp/checkpoints``).23        name (str): subfolder name for current experiment (default: ``default``).24        save_interval (int): interval of epochs to save the checkpoint (default: 10)25    """26    def __init__(self, rootdir='exp/checkpoints/', name='default', save_interval=10):27        self.rootdir = rootdir28        self.name = name29        self.save_interval = save_interval30    @property31    def savedir(self):32        if not hasattr(self, '_savedir'):33            self._savedir = unique_experiment_name(self.rootdir, self.name)34            os.makedirs(self._savedir, exist_ok=True)35        return Path(self._savedir)36    def get_checkpoint_dir(self, unique=False):37        if unique:38            return self.savedir39        return Path(self.savedir) / self.name40    def load_trained_model(self, weight_path, remove_module=False):41        """ another loader method for `model`42        It can recover changed model module from dumped `latest.pt` and load43        pre-trained weights.44        Args:45            weight_path (str): full path or short weight name to the dumped weight46            remove_module (bool)47        """48        folder = self.get_checkpoint_dir()49        if str(folder) not in weight_path:50            folder = Path(weight_path).parent51        ckpt = torch.load(folder / 'latest.pt')52        full_model = ckpt['model']53        if 'latest.pt' not in weight_path:54            state_dict = export_checkpoint_weight(weight_path, remove_module)55            full_model.load_state_dict(state_dict)56        return full_model57    def load(self, path=None, model=None, remove_module=False, resume=False):58        """ load method for `model` and `optimizer`59        If `resume` is True, full `model` and `optimizer` modules will be returned;60        or the loaded model will be returned.61        Args:62            path (str): full path to the dumped weight or full module63            model (nn.Module)64            remove_module (bool)65            resume (bool)66        Return:67            - dict() of dumped data inside `latest.pt`68            - OrderedDict() of `state_dict`69            - nn.Module of input model with loaded state_dict70            - nn.Module of dumped full module with loaded state_dict71        """72        if resume:73            latest_ckpt = torch.load(path)74            return latest_ckpt75        try:76            state_dict = export_checkpoint_weight(path, remove_module)77            if model is None:78                return state_dict79            model.load_state_dict(state_dict)80            return model81        except KeyError:82            self.logger.warn('Use fallback solution: load `latest.pt` as module')83            return self.load_trained_model(path, remove_module)84    def save(self, model, optimizer=None, epoch=None):85        """ save method for `model` and `optimizer`86        Args:87            model (nn.Module)88            optimizer (nn.Module)89            epoch (int): epoch step of training90        """91        if (epoch + 1) % self.save_interval:92            return93        folder = self.get_checkpoint_dir(unique=True)94        torch.save({'weight': model.state_dict()}, folder / f'net-{epoch}.pt')95        torch.save({96            'model': model,97            'optimizer': optimizer,98            'epoch': epoch + 199        }, folder / 'latest.pt')100    def get_weights(self, weight_path, model=None, remove_module=False, path_only=False):101        """ model weights searcher102        Args:103            weight_path (str): the path to single weight file or the folder of weights104            model (nn.Module): if given, the model will be filled with state_dict105            remove_module (bool): remove the `module.` string from the keys of state_dict106            path_only (bool): if true, the return value will be only path to weights107        Returns:108            - payload, path: if model is given, payload will be loaded model else will be state_dict109            - path: the path to the weight110        """111        weight_path = Path(weight_path)112        if weight_path.is_file():113            path = str(weight_path)114            if path_only:115                yield path116            payload = self.load(path, model=model, remove_module=remove_module)117            return payload, path118        paths = list(weight_path.glob('*.pt'))119        if weight_path.is_dir():120            assert len(paths), 'Weights folder contains nothing.'121        for path in paths:122            path = str(path)123            if 'latest.pt' in path:124                continue125            if path_only:126                yield path127                continue128            payload = self.load(path, model=model, remove_module=remove_module)129            model = payload  # use corrected model_def130            yield payload, path131# deprecated132class GANCheckpoint(Checkpoint):133    def load(self, trainer, gnet_path=None, dnet_path=None, resume=False):134        epoch = self._load(dnet_path, trainer.dnet, trainer.d_optim)135        epoch = self._load(gnet_path, trainer.gnet, trainer.g_optim)136        if resume:137            trainer.start_epoch = epoch138    def save(self, trainer, epoch):139        if (epoch + 1) % self.save_interval:140            return141        self._save('dnet-{epoch}.pth', trainer.model_d, trainer.optim_d, epoch)...command.py
Source:command.py  
...14class Canvas:15    color = "white"16    def add_module(self):17        print("æ·»å äºå
置模å")18    def remove_module(self):19        print("å é¤äºå
置模å")20    def set_background(self, color):21        self.color = color22        print("设置äºèæ¯é¢è²ä¸ºï¼%s" % color)23    def get_background(self):24        return self.color25# å½ä»¤26class Command:27    # æ§è¡å½ä»¤28    def execute(self):29        pass30    # æ¤é31    def undo(self):32        pass33# å
·ä½çå½ä»¤34class AddModuleCommand(Command):35    def __init__(self, canvas):36        self.canvas = canvas37    def execute(self):38        self.canvas.add_module()39    def undo(self):40        self.canvas.remove_module()41class RemoveModuleCommand(Command):42    def __init__(self, canvas):43        self.canvas = canvas44    def execute(self):45        self.canvas.remove_module()46    def undo(self):47        self.canvas.add_module()48class SetBackgroundCommand(Command):49    def __init__(self, canvas, color):50        self.canvas = canvas51        self.origin_color = canvas.get_background()52        self.color = color53    def execute(self):54        self.canvas.set_background(self.color)55    def undo(self):56        self.canvas.set_background(self.origin_color)57# 请æ±ç±»58class Invoke:59    def __init__(self):60        self.add_module_command = None61        self.remove_module_command = None62        self.set_background_command = None63        self.undo_command = None64    def set_command(self, add_module_command, remove_module_command, set_background_command):65        self.add_module_command = add_module_command66        self.remove_module_command = remove_module_command67        self.set_background_command = set_background_command68    def add_module(self):69        self.add_module_command.execute()70        self.undo_command = self.add_module_command71    def remove_module(self):72        self.remove_module_command.execute()73        self.undo_command = self.remove_module_command74    def set_background(self):75        self.set_background_command.execute()76        self.undo_command = self.set_background_command77    def undo(self):78        self.undo_command.undo()79# 客æ·ç«¯80if __name__ == "__main__":81    canvas = Canvas()82    add_module_command = AddModuleCommand(canvas)83    remove_module_command = RemoveModuleCommand(canvas)84    set_background_command = SetBackgroundCommand(canvas, "blue")85    invoke = Invoke()86    invoke.set_command(add_module_command, remove_module_command, set_background_command)87    invoke.add_module()88    invoke.remove_module()89    invoke.undo()90    invoke.set_background()...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!!
