app.core.utils
Module Contents
Classes
Generic enumeration. |
|
Extensible JSON <http://json.org> encoder for Python data structures. |
Functions
|
|
|
This function returns the SHA-1 hash |
|
Truncate number to given number of digits |
|
Convert the size from bytes to other units like KB, MB or GB |
|
Get file in size in given unit like KB, MB or GB |
|
Save file to upload directory |
|
Get full path of upload directory |
- app.core.utils.create_aliased_response(model: pydantic.BaseModel) starlette.responses.JSONResponse
- app.core.utils.hash_file(filename: str) str
This function returns the SHA-1 hash of the file passed into it
- Parameters
filename (str) – file full name (including extention) to hash
- Returns
SHA-1 hash of the file full name
- Return type
str
- app.core.utils.truncate(number: float, digits: int) float
Truncate number to given number of digits
- Parameters
number (float) – number to truncate
digits (int) – desidered number of digits
- Returns
truncated number
- Return type
float
- class app.core.utils.SIZE_UNIT
Bases:
enum.EnumGeneric enumeration.
Derive from this class to define new enumerations.
- BYTES = 1
- KB = 2
- MB = 3
- GB = 4
- app.core.utils.convert_unit(size_in_bytes: float, unit: SIZE_UNIT) float
Convert the size from bytes to other units like KB, MB or GB
- Parameters
size_in_bytes (float) – file size in bytes
unit (SIZE_UNIT) – desidered units
- Returns
converted size in desidered units
- Return type
float
- app.core.utils.get_str_file_size(file_name: str, size_type: SIZE_UNIT = None) str
Get file in size in given unit like KB, MB or GB
- Parameters
file_name (str) – file full path
size_type (SIZE_UNIT, optional) – preferred size format (default to None).
- Returns
file size in KB, MB or GB
- Return type
str
- async app.core.utils.save_file(file: fastapi.UploadFile, upload_dir: str)
Save file to upload directory
- Parameters
file (UploadFile) – file to save
upload_dir (str) – base dir where to save file
- Raises
HTTPException – HTTP Error 400 if file is not valid
HTTPException – HTTP Error 500 if file cannot be saved
- Returns
- {
“file_hash” (str): hash of the file saved, “file_size” (str): file size in human readable format, “file_ext” (str): file extension,
}
- Return type
dict
- class app.core.utils.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
Bases:
json.JSONEncoderExtensible JSON <http://json.org> encoder for Python data structures.
Supports the following objects and types by default:
Python
JSON
dict
object
list, tuple
array
str
string
int, float
number
True
true
False
false
None
null
To extend this to recognize other objects, subclass and implement a
.default()method with another method that returns a serializable object foroif possible, otherwise it should call the superclass implementation (to raiseTypeError).- default(self, o)
Implement this method in a subclass such that it returns a serializable object for
o, or calls the base implementation (to raise aTypeError).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- app.core.utils.get_dir_uploaded(upload_dir: str) pathlib.Path
Get full path of upload directory
- Parameters
upload_dir (str) – directory name where to save file
- Returns
full path of upload directory
- Return type
str