Package: python-dynamic-loader
Dynamic Class LoaderOne of the issues developers can encounter when developing in Python is the
time taken to deploy changes. You can help reduce this time by dynamically
loading some of your Python classes. This allows you to make iterative changes
to just the area of your application that you’re working on.For example, if you’re creating code that requires lots of small changes and
want to see your results quickly, without needing to redeploy the
Cloud Function each time, then this solution is for you. Put the code you
want to change in a class you can dynamically load, and redeploy the Cloud
Function once calling this new class. You can now copy the new class to your
chosen storage and test iteratively by just changing the file in storage without
having to redeploy the Cloud Function.Installation and SetupThe library is available on PyPi aspython-dynamic-loader. To install it
into your local python environment, simple add the following line to yourrequirements.txt:python-dynamic-loader>=1.0.0UsageThe system relies on the classes extending theDynamicClassobject, which has
in it the loader, and suggests a method to override to make generic loading
easier.How to create a dynamically loadable classA very simple example can be found in thecommand_filesfolder.1fromtypingimportMapping2fromdynamicimportDynamicClass345classHelloWorld(DynamicClass):6"""A simple dynamic Hello World."""78defrun(self,**attributes:Mapping[str,str])->str:9"""Runs the command.1011  The is the mandatory implementation of the `run` method in12  `DynamicClass`. It is the only method that the framework will call.1314  The attributes are passed as Python keyword args and will vary15  according to whatever the user needs.1617  Returns:18     Dict[str, Any]: the result of the command19  """20return"Hello world!"So your named class must extendDynamicClass, as shown on line 5.You should then implement therunmethod, which at a minimum should return the
result of the class. Thisrunmethod is only a placeholder, you could create
any method you want.Once this is done, you need to call the code when needed.1importdynamic23processor=DynamicClass.install(module_name='my_module',4class_name='HelloWorld',5storage=dynamic.CloudStorage,6bucket='my-gcs-bucket')7processor().run()As you can see, the first step is toinstallthe new class. This loads the
code from the chosen storage location (dynamic.CloudStoragein this example)
and returns theclassobject.installtakes 3 mandatory parameters and any number of optional ones that will
be passed on to the storage object.ParameterFunctionmodule_nameThe name of the file (module) the code is in, without any extension. This can be any valid Python name.NO  dot (.) characters are permitted.class_nameThe name of the class within the file to be used. Aclassobject will be returned that can then be instantiated by the user.storageThe storage to use. Provided examples are listedbelow.otherAny other named parameters will be passed to the constructor of theSourceGrabber.In the example above,bucketis passed todynamic.CloudStorage,.You can then instantiate the object (processor()) and call the run method, or
any other method you have defined.The module is reloaded each time, so the latest version will always be used.Where To Store Your Code:SourceGrabbersProvidedSourceGrabbersCloudStorageThis is to grab the code from a bucket in Google Cloud Storage (GCS).CloudStoragetakes one parameter:ParameterFunctionDefaultbucketThe name of the GCS bucket in which the code files are stored.dynamic-commandsThe files must be stored in the bucket, with the.pyextension as normal text
files. As an example, the above sample code would be stored ashello.py, like
this:SecretManagerThis fetches the code from a secret stored in Google Secret Manager in plain
text. It takes no additional parameters.The file is uploaded as a secret to secret manager with the name of the module
just as is. For example, the above sample code would be stored in Secret Manager
as asecretcalled "hello", and the value of the secret should be the text
content of the class. Your Secret Manager should look like this:Secret Manager can store up to 64kb of text which sdhould be large enough for
most uses.LocalStorageMostly useful for testing, this is to grab the code from a local folder on the
user's local drive.LocalStoragetakes one parameter:ParameterFunctionDefaultfolderThe name of the local folder in which the code files are stored.NoneThe files must be stored in the folder, with the.pyextension as normal
source files. As an example, the above sample code would be stored ashello.py.Creating your ownSourceGrabberAny location where you can fetch plain text from can be used as a source for the
code to be loaded dynamically - Amazon S3, Fiurestore, Big Query etc. would all
be decent choices.In order to create aSourceGrabberyou need to create a class extendingdynamic.SourceGrabber.It must implement at a minimum thefetch_sourcemethod, which must return the source to be loaded as astr.It can, in addition, include an__init__method which will be passed any
additional startup parameters given to theinstallmethod
(see above). If you intend to do this, remember to always include
the**unusedas the final parameter so that unexpected parameters will not
cause instantiation failures.Examples ofSourceGrabbers can be seen in thedynamic.source_grabbersmodule.