Skip to content

Misc

Random tips and tricks when dealing with Python

No space left on device when installing dependencies

Set the TMPDIR env var to a folder on a device with enough space. Make sure the folder has been created beforehand.

bash
export TMPDIR=$HOME/.piptmp
mkdir -p $TMPDIR
pip install ...

[src]

Serving files over HTTP

Python comes with a builtin HTTP server that can be used to serve files:

bash
python -m http.server

Default behaviour is to bind on 0.0.0.0 and listen on 8000, and serve the files in the current directory.

To change the bind address, use the -b, --bind option

bash
python -m http.server --bind 127.0.0.1

To change the port it listens on, provide it as parameter:

bash
python -m http.server 9000

To serve a specific folder, use the -d, --directory option:

bash
python -m http.server --directory /tmp/

[src]

Dump attributes and values of a given object

python
def dump(obj, iterable=True):
    from pprint import pprint
    def _dump(o):
        attrs = [attr for attr in dir(o) if not attr.startswith("_")]
        res = {}
        for attr in attrs:
            try:
                value = getattr(o, attr)
            except Exception as e:
                value = f"<Error retrieving attribute: {e}>"
            res[attr] = value
        pprint(res)
    if not iterable:
        _dump(obj)
        return
    try:
        iterator = iter(obj)
        for item in iterator:
            _dump(item)
    except TypeError:
        _dump(obj)

Built using VitePress. Released under the MIT License.