The util
library is used for pure utility function in sneq, such as string manipulation and other convenience functions.
Similar to list filter
, this function takes a dictionary and filters out key-value pairs based on a filtering function that returns a bool
.
The function fn
should be in the form def fn(key=None, val=None)
. Both keyword arguments are mandatory.
Arguments:
def util.dicts.filter(
d: dict = None,
fn: function = None,
):
Returns a dictionary with values filtered out.
Example:
def key_fn(key='', val=''):
return key == 'bob' or val['name'] == 'Charles'
# {
# 'bob': {'name': 'Bob'},
# 'charles': {'name': 'Charles'},
# }
return util.dicts.filter(
d={
'john': {'name': 'John'},
'bob': {'name': 'Bob'},
'charles': {'name': 'Charles'},
},
fn=key_fn)
Similar to list reduce
, this function takes a dictionary and reduces key-value pairs based on a reducing function. The dictionary is iterated through in order.
The function fn
should be in the form def fn(acc=None, key=None, val=None)
. All keyword arguments are mandatory.
Arguments:
def util.dicts.reduce(
d: dict = None, # Dictionary to reduce
fn: function = None, # Reducing function
default: function = None, # Default value
):
Returns the sum generated by the reduction function fn
.
Example:
def red_fn(acc=0, key='', val=''):
return acc + val
# 6
return util.dicts.reduce(
d={
'john': 1,
'bob': 2,
'charles': 3,
},
fn=red_fn,
default=None)
Convert a dictionary to a list of 2-itemed lists.
Arguments:
def util.dicts.to_pairs(
d: dict = None,
):
Returns a list of lists.
Example:
# [ [ 'john', 1 ], [ 'bob', 2 ], ['charles', 3 ] ]
return util.dicts.to_pairs(
d={
'john': 1,
'bob': 2,
'charles': 3,
})
Get a form response dict
from a form string ID. If the form response does not exist, None
is returned.
Arguments:
def util.forms.form_response_from_form_id(
form_id: str = None,
):
Returns a dictionary of a form response or None. For the structure of form responses, see Library_API_Dictionaries.
Example:
form_response = util.forms.form_response_from_form_id(form_id="foo")
Get a form response value from a form response when given a form ID and a form field ID. If the form response does not exist, None
is returned.
Arguments:
def util.forms.form_response_value_from_form_id_and_field_id(
form_id: str = None,
field_id: str = None,
):
Returns the value for a form field in a form response. If the form or field can not be found, it returns None
.
Example:
form_response_value = util.forms.form_response_value_from_form_id_and_field_id(
form_id='50pc8hjgnhomku4l08rqgxetg9ektri5p70ih',
field_id='b0363d56-b682-471a-a049-fc502cdea0cb')
Returns a sublist from a list, from start
to end
. Negative numbers indicate distance from the end of the list.
def sublist(
lst: list = None, # Required
start: dec or int=0,
end: dec, int, or None=None,
):
Returns a list from start
to end
.
Example:
foo = [1, 2, 3]
# bar == [1, 2]
bar = util.lists.sublist(lst=foo, start=0, end=2)
# bar == [2, 3]
bar = util.lists.sublist(lst=foo, start=1, end=3)
# bar == [1, 2]
bar = util.lists.sublist(lst=foo, start=0, end=-1)
# bar == [1]
bar = util.lists.sublist(lst=foo, start=0, end=-2)
# bar == [3]
bar = util.lists.sublist(lst=foo, start=-1, end=3)
Round a number up to the nearest integer. If an int
is given, it just returns that integer.
def util.math.ceiling(
value: dec or int = None, # Required
):
Returns int
.
Example:
# val == int(3)
val = util.math.ceiling(value=2.5)
Round a number down to the nearest integer. If an int
is given, it just returns that integer.
def util.math.floor(
value: dec or int = None, # Required
):
Returns int
.
Example:
# val == int(2)
val = util.math.ceiling(value=2.5)
Returns the result of the value
modulo modulus
.
def util.math.modulo(
value: dec or int = None, # Required
modulo: dec or int = None, # Required
):
Returns dec
if one or more of the operands is a dec
, or int
if both operands are int
.
Example:
# foo == dec(2)
foo = util.math.modulo(value=5, modulus=3)
# foo == int(2)
foo = util.math.modulo(value=int(5), modulus=int(3))
# foo == dec(2.5)
foo = util.math.modulo(value=5.5, modulus=3)
Return a random item from the list lst
.
def util.random.choice(
lst: list = None, # Required
):
Returns a list element, which could be any type.
Example:
# foo is 1, 2, or 3
foo = util.random.choice(lst=[1,2,3])
Returns a random dec
integer (not int
) in the range [start
,end
).
def util.random.integer(
start: dec or int = None, # Required
end: dec or int = None, # Required
):
Returns a dec
integer value.
Example:
# foo == 1
foo = util.random.integer(start=1, end=2)
# foo == 1 or foo == 2
foo = util.random.integer(start=1, end=3)
# foo is a random number from 0 to 9
foo = util.random.integer(start=0, end=10)
Return a shuffled version of the list lst
.
def util.random.shuffle(
lst: list = None, # Required
):
Returns a list.
Example:
# foo is [1, 2] or [2, 1]
foo = util.random.shuffle(lst=[1,2])
Return a list of single resource reservations or requests from the context. If the reservation or request is only for a single resource, it returns a list of the single reservation. Useful for making a script apply to single or multi-resource situations.
Arguments:
def util.reservations.all():
Returns list
of reservation or request dict
s.
Example:
single_resource_reservations = util.reservations.all()
Return a reservation or request for a resource given some reservable_id
. If the reservation or request is not found, it returns None
.
Arguments:
def util.reservations.for_reservable(
reservable_id: str = None,
):
Returns a reservation or request dict
or None
.
Example:
camera_id = 'foo'
reservation_for_camera = util.reservations.for_reservable(reservable_id=camera_id)
if reservation_for_camera != None:
# Reservation rules specific to the camera
Return a lowercase version of a string.
Arguments:
def util.strings.lowercase(
s: str = None,
):
Returns a string.
Example:
s = 'Foo bar'
s_lower = util.strings.lowercase(s=s) # 'foo bar'
Join a list of strings with a separator s
.
Arguments:
def util.strings.join(
lst: [str] = None,
s: str = None,
):
Returns a string.
Example:
lst = ['foo', 'bar']
s_lower = util.strings.join(lst=lst, s=', ') # 'foo, bar'
Match a string to a pattern using a unix-like filesystem's pattern matching.
*
: Match anything?
: Match any single character[seq]
: Match any characters in seq
[!seq]
: Match any characters not in seq
Arguments:
def util.strings.match(
test: str = None,
s: str = None,
):
Returns a boolean.
Example:
util.strings.match(s="foo", test="f*") # True
util.strings.match(s="foo", test="d*") # False
util.strings.match(s="foo", test="f??") # True
util.strings.match(s="foo", test="y??") # False
util.strings.match(s='fzo', test='f[a-z][o]') # True
util.strings.match(s='fzo', test='f[abc][o]') # False
util.strings.match(s='fao', test='f[abc][o]') # False
util.strings.match(s='foo', test='f[!abc][o]') # True
util.strings.match(s='foo', test='f[!a-z][o]') # False
util.strings.match(s='f1o', test='f[!a-z][o]') # True
Returns the newline character \n
.
Example:
# \n
util.strings.newline()
Replace a substring in a string with a different string.
Arguments:
def util.strings.replace(
remove: str = None,
inject: str = None,
s: str = None,
):
Returns a string.
Example:
s = 'Foo bar'
util.strings.replace(s=s, remove="bar", inject="car") # 'foo car'
Split a string in a list of strings based on a separator.
Arguments:
def util.strings.split(
s: str = None,
separator: str = ',',
maxsplit: int, dec = -1,
):
Returns a list of strings.
Example:
# [ 'hello', 'world' ]
util.strings.split(s="hello world", separator=" ")
Remove whitespace from a string.
Arguments:
def util.strings.strip(
s: str = None,
):
Returns a string.
Example:
util.strings.strip(s=" hello world ") # 'hello world'
Return a substring from a string using start
and end
as index values. If end
is None, it will return the length to the end of the string from start
. Negative numbers are permitted to designate distance from the end of the string.
Arguments:
def util.strings.substring(
s: str = None,
start: int, dec = 0,
end: int, dec = None,
):
Returns a string.
Example:
s = "hello world"
util.strings.substring(s=s, end=5) # 'hello'
util.strings.substring(s=s, start=6) # 'world'
util.strings.substring(s=s, start=1, end=5) # 'ello'
util.strings.substring(s=s, end=-6) # 'hello'
util.strings.substring(s=s, end=-999, end=-999) # ''
Returns the tab character \t
.
Example:
# \t
util.strings.tab()
Convert a string to a decimal.
Arguments:
def util.strings.to_decimal(
s: str = None,
):
If able to convert s
, it returns a decimal based on the string. Otherwise it raises an exception.
Example:
util.strings.to_decimal(s='1.2') # 1.2
Return an uppercase version of the string.
Arguments:
def util.strings.uppercase(
s: str = None,
):
Returns a string.
Example:
s = "Foo bar"
util.strings.uppercase(s=s) # 'FOO BAR'