PyLamarr
Pythonizations for the ultra-fast simulation option for the LHCb experiment
PyLamarr.RemoteResource.RemoteResource Class Reference

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

Inheritance diagram for PyLamarr.RemoteResource.RemoteResource:

Public Member Functions

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

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:
```python
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:
```python
pv_params.download(force=True)
```

or, replacing the connection attempt in the previous example:
```python
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 data in `PyLamarr`.

For example, if testing your local version of `MyPrimaryVertexSmearing.db`,
you can write
```python
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 sring 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()

def 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.


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