PyLamarr
Pythonizations for the ultra-fast simulation option for the LHCb experiment
 
Loading...
Searching...
No Matches
PyLamarr.RemoteResource.RemoteResource Class Reference

Resource on the Internet, locally cached. More...

Inheritance diagram for PyLamarr.RemoteResource.RemoteResource:

Public Member Functions

 download (self, bool force=False)
 Download the remote resource is not available locally or if forced.
 

Static Public Attributes

str remote_url
 
Optional local_filename = None
 

Protected Attributes

 _file
 

Static Protected Attributes

Optional _file = PrivateAttr()
 

Detailed Description

Resource on the Internet, locally cached.

Remote Resource mechanism

Most of the parametrizations Lamarr relies on are committed and maintained in remote repositories. The PyLamarr.RemoteResource class implements a simple caching mechanism to download the remote parametrizations on demand in case they are not available locally. A hash of the URL identifying the remote resource is used to represent the local cache of the remote resource. Note that in case the remote resource is updated without modifying its URL, the local cache is not automatically updated.

Example.

Consider the file PrimaryVertexSmearing.db encoding the parametrizations for Primary Vertex reconstruction, and made available publicly here: https://github.com/LamarrSim/SQLamarr/raw/master/temporary_data/PrimaryVertex/PrimaryVertexSmearing.db

The following snippet of code enables caching the file locally:

from PyLamarr import RemoteResource
url = ("https://github.com/LamarrSim/SQLamarr/raw/master/temporary_data/"
"PrimaryVertex/PrimaryVertexSmearing.db")
pv_params = RemoteResource(url)
# Now the file might not be available locally, but a lazy download is triggered
# when accessing its path:
import sqlite3
with sqlite3.connect(pv_params.file) as db:
# ... do something ...

Now, in case the remote file is updated, it may be necessary to download the updated version. This can be achieved forcing the download with:

pv_params.download(force=True)

or, replacing the connection attempt in the previous example:

import sqlite3
with sqlite3.connect(pv_params.download(force=True).file) as db:
# ... do something ...

Accessing local resources

A local resourcce can be encapsulated inside RemoteResource which is the expected format for most of the parametrization test_data in PyLamarr.

For example, if testing your local version of MyPrimaryVertexSmearing.db, you can write

pv_params = RemoteResource("file://MyPrimaryVertexSmearing.db")
# Now the file might not be available locally, but a lazy download is triggered
# when accessing its path:
import sqlite3
with sqlite3.connect(pv_params.file) as db:
#...

Note, however, that forcing the download of a local resource would raise an Exception.

Implicit conversion from URL

Most of the parametrizations relying on external dependencies expect an instance of RemoteResource identifying the file to obtain the parametrization from. An implicit cast from string to RemoteResource enables passing directly a string with a URL (possibly pointing to a local file), which gets transparently converted into a RemoteResource instance and used in the file.

Definition at line 11 of file RemoteResource.py.

Member Function Documentation

◆ download()

PyLamarr.RemoteResource.RemoteResource.download (   self,
bool   force = False 
)

Download the remote resource is not available locally or if forced.

Can raise an exception if the download fails or upon attempts of downloading local resources (represented by protocol file://)

Parameters
forceForce the download of the remote resource independently of the cache availability
Returns
Updated instance of RemoteResource (self)

Definition at line 143 of file RemoteResource.py.

Member Data Documentation

◆ _file [1/2]

Optional PyLamarr.RemoteResource.RemoteResource._file = PrivateAttr()
staticprotected

Definition at line 90 of file RemoteResource.py.

◆ _file [2/2]

PyLamarr.RemoteResource.RemoteResource._file
protected

Definition at line 103 of file RemoteResource.py.

◆ local_filename

Optional PyLamarr.RemoteResource.RemoteResource.local_filename = None
static

Definition at line 89 of file RemoteResource.py.

◆ remote_url

str PyLamarr.RemoteResource.RemoteResource.remote_url
static

Definition at line 88 of file RemoteResource.py.


The documentation for this class was generated from the following file: