Pack `data` to GZIP and write them to `out`. If `out` is not defined,
    :mod:`stringio` is used.

    Args:
        data (obj): Any packable data (str / unicode / whatever).
        out (file, default None): Optional opened file handler.

    Returns:
        obj: File handler with packed data seeked at the beginning.

def to_gzipped_file(data, out=None):
    """
    Pack `data` to GZIP and write them to `out`. If `out` is not defined,
    :mod:`stringio` is used.

    Args:
        data (obj): Any packable data (str / unicode / whatever).
        out (file, default None): Optional opened file handler.

    Returns:
        obj: File handler with packed data seeked at the beginning.
    """
    if not out:
        out = StringIO.StringIO()

    with gzip.GzipFile(fileobj=out, mode="w") as f:
        f.write(data)

    out.seek(0)
    return out