forked from anvilco/python-anvil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
44 lines (32 loc) · 1.08 KB
/
utils.py
File metadata and controls
44 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import re
import uuid
from logging import getLogger
from os import path
logger = getLogger(__name__)
def build_batch_filenames(filename: str, start_idx=0, separator: str = "-"):
"""
Create a generator for filenames in sequential order.
Example:
build_batch_filenames('somefile.pdf') will yield filenames:
* somefile-1.pdf
* somefile-2.pdf
* somefile-3.pdf
:param filename: Full filename, including extension
:param start_idx: Starting index number
:param separator:
:return:
"""
idx = start_idx or 0
sep = separator or "-"
file_part, ext = path.splitext(filename)
while True:
yield f"{file_part}{sep}{idx}{ext}"
idx += 1
def create_unique_id(prefix: str = "field") -> str:
"""Create a prefixed unique id."""
return f"{prefix}-{uuid.uuid4().hex}"
def remove_empty_items(dict_obj: dict):
"""Remove null values from a dict."""
return {k: v for k, v in dict_obj.items() if v is not None}
def camel_to_snake(name: str) -> str:
return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()