Skip to content

Commit

Permalink
feat: add new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pehlicd committed Mar 18, 2024
1 parent c4a87b7 commit 977ee96
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions keep/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,38 @@ def all(iterable) -> bool:
g = groupby(iterable)
return next(g, True) and not next(g, False)


def diff(iterable: iter) -> bool:
# Opposite of all - returns True if any element is different
return not all(iterable)


def len(iterable=[]) -> int:
return _len(iterable)

def uppercase(string) -> str:
return string.upper()

def lowercase(string) -> str:
return string.lower()

def split(string, delimeter) -> list:
return string.strip().split(delimeter)


def strip(string) -> str:
return string.strip()


def first(iterable):
return iterable[0]

def last(iterable):
return iterable[-1]

def utcnow() -> datetime.datetime:
dt = datetime.datetime.now(datetime.timezone.utc)
return dt


def utcnowiso() -> str:
return utcnow().isoformat()


def substract_minutes(dt: datetime.datetime, minutes: int) -> datetime.datetime:
"""
Substract minutes from a datetime object
Expand All @@ -59,28 +60,23 @@ def substract_minutes(dt: datetime.datetime, minutes: int) -> datetime.datetime:
"""
return dt - datetime.timedelta(minutes=minutes)


def to_utc(dt: datetime.datetime | str) -> datetime.datetime:
if isinstance(dt, str):
dt = parser.parse(dt)
utc_dt = dt.astimezone(pytz.utc)
return utc_dt


def datetime_compare(t1, t2) -> float:
diff = (t1 - t2).total_seconds() / 3600
return diff


def json_dumps(data: str | dict) -> str:
if isinstance(data, str):
data = json.loads(data)
return json.dumps(data, indent=4, default=str)


def encode(string) -> str:
return urllib.parse.quote(string)


def dict_to_key_value_list(d: dict) -> list:
return [f"{k}:{v}" for k, v in d.items()]

0 comments on commit 977ee96

Please sign in to comment.