Skip to content

Python utilities in order code faster. Time calculator, unique string generator and some user input sanitization

License

Notifications You must be signed in to change notification settings

tsaklidis/Utilities

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Info

Python utilities in order to make your coding life easier
Some of the functions, are unable to run as a single python code so you have to create Django project.

Also use python3


Sanitize user's input

All functions return the data if all checks are ok.
If there is any strange character that doesn't match, a custom Exception is raised
Example 1:
import sanitize
data = 'rocky_99'
try:
	sanitize.alphanumeric(data)
    # Register user 
except sanitize.SanitizationException as e:
	print(e)
Example 2:
Check for password strength. If password is strength (check function comments),
the same password is returned. Otherwise Exception is raised
import sanitize
data = 'p@as$wor!2'

try:
	sanitize.password_check(data)
	# Do what you need
except sanitize.SanitizationException as e:
	# Something is wrong, print the errors
	print(e)

Create random unique string

unique.get

Returns a random unique string. Uniquines is based on timestamp. Default length is 8.
Argumentrs: length, prefix, suffix, invalid_chars
Example 1:
If you need to get a random unique string of 16 chars with prefix 'user'
get(16, 'user')
Example 2:
If you need to get a random unique string of 6 chars but don't use the chars 'ofdA'
avoid_chars = 'ofdAre'
get(6, invalid_chars=avoid_chars)

Time calculator

First of all you need to import datetime

Days from today

Returns the date after given days. If true is passed, hence will be calculated backwords
Example 1:
If you need to get the date after one week
days_hence(7)
Example 2:
If you need to get the date before 5 days
calculate_back = True
days_hence(5, calculate_back)

Ensure age of at least 18 years old.

Example 1:
Find if the given birthdate is adult. Default adult years are 18.
sample_date = datetime.date(1987, 8, 13)
is_adult(sample_date)

If the given date is over 18 years old, returns True else False

Example 2:
sample_date = datetime.date(1987, 8, 13)
adult_years = 21
is_adult(sample_date, adult_years)

In some countries adultd age differs, so pass the years you need


Make requests to external sites

Example 1:
Make a request and return the answer in json format. Chech function definition for more.
send_request('GET', 'some/api/', return_json=True, debug=True)

About

Python utilities in order code faster. Time calculator, unique string generator and some user input sanitization

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages