Welcome to NIWLittleUtils’s documentation!¶
Documentation: https://niwlittleutils.readthedocs.org/
Intro¶
NIWLittleUtils is a tool collection that contain some useful module and function.
This library is a work in progress. Please give feedback!
Contents¶
codec
— A module that help handle codec.¶
-
NIWLittleUtils.codec.
b64decode
(b)[source]¶ Decode a Base64 encoded byte string.
Decode a Base64 encoded byte string using a URL-safe alphabet, which substitutes
-
instead of+
and_
instead of/
in the standard Base64 alphabet. The inputb
may also be a ASCII-only Unicode strings.Return type: bytes
-
NIWLittleUtils.codec.
b64encode
(b)[source]¶ Base64 encodes a byte string.
Base64 encodes a byte string. The resulting bytestring is safe for putting into URLs. The input
b
should be abytes
orbytearray
object in Python3.3 or any bytes-like object after Python3.4. This function will raiseTypeError
ifb
is not bytes-like object (For example,str
).Return type: bytes
-
NIWLittleUtils.codec.
urlsafe_decode
(s)[source]¶ Decode a string that encode by
urlsafe_encode()
.Decode a string that encoded by
urlsafe_encode()
. If the inputs
which returned byurlsafe_encode()
is encode frombytes
orbytearray
object, it will returnbytes
object. Ifs
is encode fromstr
object, it will returnstr
object.Return type: str or bytes
-
NIWLittleUtils.codec.
urlsafe_encode
(s)[source]¶ Encode a string or byte string to url-safe string.
Encode a string or byte string to url-safe string. The input can be a object of
str
,bytes
orbytearray
.This function will make sure that return string contains characters that RFC 3986 section 2.3 specified only, but it won’t check the length. User should remember that browsers have limit for length of URLs. (For example, IE8’s maximum URL length is 2083 chars)
Return type: str
filesystem
— A module that contain some helpful function to deal with filesystem.¶
-
NIWLittleUtils.filesystem.
scantree
(path='.')[source]¶ Recursively yield DirEntry objects for given directory.
Python 3.5 and higher version has
os.scandir()
to get the files and folders and their file attribute information in folder given by path. But Python version won’t scandir recursively, this function can scan folder recursively.Notice that this function will not follow symlinks to avoid symlink loops.
Parameters: path – Path to folder which you want to scan.
-
NIWLittleUtils.filesystem.
secure_filename
(filename, codec='utf8')[source]¶ Pass it a filename and it will return a secure version of it.
This finction is a modified version of
werkzeug.utils.secure_filename
.The filename can then safely be stored on a regular file system and passed to
os.path.join()
.You can use parameter
codec
to specify the codec which is used to encode the filename. Thecodec
could only be utf8 or ascii. If you need high portability, you should letcodec
to be'ascii'
. It will be'utf8'
by default.On windows systems the function also makes sure that the file is not named after one of the special device files.
>>> secure_filename("My cool movie.mov") 'My_cool_movie.mov' >>> secure_filename("../../../etc/passwd") 'etc_passwd' >>> secure_filename('i contain cool \xfcml\xe4uts.txt') 'i_contain_cool_ümläuts.txt' >>> secure_filename('i contain cool \xfcml\xe4uts.txt', 'ascii') 'i_contain_cool_umlauts.txt'
The function might return an empty filename. It’s your responsibility to ensure that the filename is unique and that you generate random filename if the function returned an empty one.
User should remember, this function will make sure filename is secure, but it cannot make sure file itself is secure. For example, if a user upload a file named
test.sh
, this function will return same filename. If this file has security vulnerabilities script inside and you execute it… well, good luck.Parameters: filename – the filename to secure
random_tool
— A module to generate things randomly.¶
-
NIWLittleUtils.random_tool.
get_random_string
(length=36, allowed_chars='complex')[source]¶ Returns a securely generated random string.
Generate a securely random string with length or allowed characters you want.
If allowed_chars is ignored, it’s
string.ascii_letters + string.digits + string.punctuation
by default (We use Python builtin library string to set value). You can set it to following string values or any allowed_chars else.Value Meaning 'complex'
string.ascii_letters + string.digits + string.punctuation 'middle'
string.ascii_letters + string.digits + '-._~'
'simple'
string.ascii_letters + string.digits 'lowercase'
string.ascii_lowercase 'uppercase'
string.ascii_uppercase 'digits'
string.digits >>> get_random_string(8) 'Qi4Y_fA5'
>>> get_random_string(5, '0123456789abcdef') '06aef'
>>> get_random_string(5, 'digits') '05154'
Parameters: - length (int) – Random string length.
- allowed_chars (str) – Character allowed to use to generate. There has 6 default setting (case-insensitive).
Returns: A securely generated random string.
Return type: str