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
11881189119011911192119311941195
@classmethoddefcwd(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().returncls().absolute()
defnow(tz:datetime.tzinfo|None=None)->datetime.datetime:"""Get current Datetime. Args: tz: timezone for retuned datetime """returndatetime.datetime.now(tz)
defhas_internet()->bool:"""Return true if machine is connected to internet. Checks connection with a HEAD request to the Google DNS server. """importhttp.clientashttplibconn=httplib.HTTPSConnection("8.8.8.8",timeout=2)try:conn.request("HEAD","/")returnTrue# noqa: TRY300exceptException:# noqa: BLE001returnFalsefinally:conn.close()
defrandrange(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)ifstopisNone:# We don't check for "step != 1" because it hasn't been# type checked and converted to an integer yet.ifstepisnot_ONE:raiseTypeError("Missing a non-None stop argument")ifistart>0:returnself._randbelow(istart)raiseValueError("empty range for randrange()")# Stop argument supplied.istop=_index(stop)width=istop-istartistep=_index(step)# Fast path.ifistep==1:ifwidth>0:returnistart+self._randbelow(width)raiseValueError(f"empty range in randrange({start}, {stop})")# Non-unit step argument supplied.ifistep>0:n=(width+istep-1)//istepelifistep<0:n=(width+istep+1)//istepelse:raiseValueError("zero step for randrange()")ifn<=0:raiseValueError(f"empty range in randrange({start}, {stop}, {step})")returnistart+istep*self._randbelow(n)
Get a random number in the range [a, b) or [a, b] depending on rounding.
Example
Jinja call:
{{uniform(2,5)}}
Result: 4.979484701994744
DocStrings
Source code in python3.12/random.py
494495496497498499500501502503
defuniform(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 """returna+(b-a)*self.random()