Skip to content

misc (7)

cwd

cwd()

Return a new path pointing to the current working directory.

Example

Jinja call:

{{ cwd() }}
Result: /home/runner/work/jinjarope/jinjarope

DocStrings
Source code in python3.12/pathlib.py
1188
1189
1190
1191
1192
1193
1194
1195
@classmethod
def cwd(cls):
    """Return a new path pointing to the current working directory."""
    # We call 'absolute()' rather than using 'os.getcwd()' directly to
    # enable users to replace the implementation of 'absolute()' in a
    # subclass and benefit from the new behaviour here. This works because
    # os.path.abspath('.') == os.getcwd().
    return cls().absolute()

now

now(tz: datetime.tzinfo | None = None) -> datetime.datetime

Get current Datetime.

Example

Jinja call:

{{ now() }}
Result: 2024-12-19 23:05:04.380535

DocStrings

Parameters:

Name Type Description Default
tz tzinfo | None

timezone for retuned datetime

None
Source code in src/jinjarope/envglobals.py
139
140
141
142
143
144
145
def now(tz: datetime.tzinfo | None = None) -> datetime.datetime:
    """Get current Datetime.

    Args:
        tz: timezone for retuned datetime
    """
    return datetime.datetime.now(tz)

online

online() -> bool

Return true if machine is connected to internet.

Example

Jinja call:

{{ online() }}
Result: True

DocStrings
Source code in src/jinjarope/envglobals.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def has_internet() -> bool:
    """Return true if machine is connected to internet.

    Checks connection with a HEAD request to the Google DNS server.
    """
    import http.client as httplib

    conn = httplib.HTTPSConnection("8.8.8.8", timeout=2)
    try:
        conn.request("HEAD", "/")
        return True  # noqa: TRY300
    except Exception:  # noqa: BLE001
        return False
    finally:
        conn.close()

randint

randint(a, b)

Return random integer in range [a, b], including both end points.

Example

Jinja call:

{{ randint(1, 10) }}
Result: 5

DocStrings
Source code in python3.12/random.py
332
333
334
335
336
def randint(self, a, b):
    """Return random integer in range [a, b], including both end points.
    """

    return self.randrange(a, b+1)

randrange

randrange(start, stop=None, step=1)

Choose a random item from range(stop) or range(start, stop[, step]).

Example

Jinja call:

{{ randrange(1, 10) }}
Result: 6

DocStrings
Source code in python3.12/random.py
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
def randrange(self, start, stop=None, step=_ONE):
    """Choose a random item from range(stop) or range(start, stop[, step]).

    Roughly equivalent to ``choice(range(start, stop, step))`` but
    supports arbitrarily large ranges and is optimized for common cases.

    """

    # This code is a bit messy to make it fast for the
    # common case while still doing adequate error checking.
    istart = _index(start)
    if stop is None:
        # We don't check for "step != 1" because it hasn't been
        # type checked and converted to an integer yet.
        if step is not _ONE:
            raise TypeError("Missing a non-None stop argument")
        if istart > 0:
            return self._randbelow(istart)
        raise ValueError("empty range for randrange()")

    # Stop argument supplied.
    istop = _index(stop)
    width = istop - istart
    istep = _index(step)
    # Fast path.
    if istep == 1:
        if width > 0:
            return istart + self._randbelow(width)
        raise ValueError(f"empty range in randrange({start}, {stop})")

    # Non-unit step argument supplied.
    if istep > 0:
        n = (width + istep - 1) // istep
    elif istep < 0:
        n = (width + istep + 1) // istep
    else:
        raise ValueError("zero step for randrange()")
    if n <= 0:
        raise ValueError(f"empty range in randrange({start}, {stop}, {step})")
    return istart + istep * self._randbelow(n)

uniform

uniform(a, b)

Get a random number in the range [a, b) or [a, b] depending on rounding.

Example

Jinja call:

{{ uniform(2, 5) }}
Result: 3.9248767283976838

DocStrings
Source code in python3.12/random.py
494
495
496
497
498
499
500
501
502
503
def uniform(self, a, b):
    """Get a random number in the range [a, b) or [a, b] depending on rounding.

    The mean (expected value) and variance of the random variable are:

        E[X] = (a + b) / 2
        Var[X] = (b - a) ** 2 / 12

    """
    return a + (b - a) * self.random()

utcnow

utcnow() -> datetime.datetime

Get UTC datetime.

Example

Jinja call:

{{ utcnow() }}
Result: 2024-12-19 23:05:04.394279+00:00

DocStrings
Source code in src/jinjarope/envglobals.py
148
149
150
def utcnow() -> datetime.datetime:
    """Get UTC datetime."""
    return datetime.datetime.now(datetime.UTC)