Welcome to Sun Stream

Generates a sequence of sun-rise and sun-set actions

Example:

# Announcing the sun-rises and sun-sets at Mt. Everest:
for action in sun_stream(27.986065, 86.922623, include_now=False):
    if action == SunAction.RISE:
        print("Sun rises at Mt. Everest")
    else:
        print("Sun sets at Mt. Everest")

To set desktop colorscheme according to diurnal pattern in Plasma:

for action in sun_stream(27.986065, 86.922623, include_now=True):
    if action == SunAction.RISE:
        subprocess.run(["plasma-apply-colorscheme", "BreezeLight"])
    else:
        subprocess.run(["plasma-apply-colorscheme", "BreezeDark"])

In this example, having include_now being True provides a little convenience, that the iterable yields immediately the current state of the sun (or the latest rise/set action before now), so the program takes effect setting the appropriate theme first thing after launching, instead of waiting for the next event to happen.

SunAction

Bases: enum.Enum

Two actions of the sun: RISE and SET.

Source code in sun_stream/__init__.py
42
43
44
45
46
class SunAction(enum.Enum):
    """Two actions of the sun: RISE and SET."""

    RISE = enum.auto()
    SET = enum.auto()

sun_stream(latitude, longitude, include_now=True)

Yield a sequence of sun-rise and sun-set actions at the time it happens

This function fetches the time of the solar pattern from https://sunrise-sunset.org/, and will only issue requests when needed.

If include_now is set to True, the state of now is instantly yielded. For example, if the current moment is midnight, the first action would be SunAction.SET; if the current moment is at noon, the first action would be SunAction.RISE.

Parameters:

Name Type Description Default
latitude float

the latitude of the place.

required
longitude float

the longtiude of the place.

required
include_now bool

whether to include the sun's state at the current moment. Defaults to True.

True

Yields:

Name Type Description
SunAction Iterable[SunAction]

The sun's action at the moment.

Source code in sun_stream/__init__.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def sun_stream(
    latitude: float, longitude: float, include_now: bool = True
) -> Iterable[SunAction]:
    """Yield a sequence of sun-rise and sun-set actions at the time it happens

    This function fetches the time of the solar pattern from
    <https://sunrise-sunset.org/>, and will only issue requests when needed.

    If `include_now` is set to `True`, the state of _now_ is instantly yielded. For
    example, if the current moment is midnight, the first action would be
    ``SunAction.SET``; if the current moment is at noon, the first action would be
    ``SunAction.RISE``.

    Args:
        latitude (float): the latitude of the place.
        longitude (float): the longtiude of the place.
        include_now (bool): whether to include the sun's state at the current moment.
            Defaults to ``True``.

    Yields:
        SunAction: The sun's action at the moment.
    """
    now = datetime.now()
    generator = _sun_time_gen(latitude, longitude, now)
    if include_now:
        yield next(generator)[1]
    else:
        next(generator)

    for scheduled_time, action in generator:
        time.sleep(_seconds_until(scheduled_time))
        yield action