This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import logging | |
| from logging import info | |
| def timestamp_file(file_loc: str) -> str: | |
| """Adds a date timestamp to the filename. | |
| :param file_loc: location of file to write to | |
| :type file_loc: str, bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| def is_json(string): | |
| """Check if string is valid JSON.""" | |
| try: | |
| json_object = json.loads(string) | |
| except ValueError as e: | |
| return False | |
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| document.body.addEventListener( 'click', function (event) { | |
| if( event.target.className === 'unique_class_name' ) { | |
| const form = event.target.form; | |
| const data = new FormData(form); | |
| const request = new XMLHttpRequest(); | |
| request.open(form.method, form.action, true); | |
| request.send(data); | |
| // logFormData(data); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // logging key-value pairs of FormData object | |
| function logFormData(data) { | |
| for (const pair of data.entries()) { | |
| console.log(pair[0] + ": " + pair[1]); | |
| } | |
| } | |
| // fade out message alerts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% block messages %} | |
| <ul class="messages" id="messages-list"> | |
| {% if messages %} | |
| {% for message in messages %} | |
| <li> | |
| {% if message.tags %} | |
| <div class="alert alert-{{ message.tags }} msg fade show" role="alert">{{ message }}</div> | |
| {% else %} | |
| <div class="alert alert-info msg fade show" role="alert">{{ message }}</div> | |
| {% endif %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.contrib import messages | |
| @require_http_methods(["GET", "POST"]) | |
| @login_required(login_url='/login', redirect_field_name='') | |
| def do_logout(request): | |
| assert isinstance(request, HttpRequest) | |
| messages.add_message(request, messages.INFO, '{0} logged out.'.format(request.user)) | |
| logout(request) | |
| return redirect('home') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* form errors and request messages and alerts */ | |
| .badge-light { | |
| margin-top: 2.5em; | |
| } | |
| .errorlist { | |
| list-style: none; | |
| padding-left: 0px; | |
| opacity: 0.8; | |
| color: #dc3545; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MIDDLEWARE = [ | |
| 'django.middleware.security.SecurityMiddleware', | |
| 'django.contrib.sessions.middleware.SessionMiddleware', | |
| 'django.middleware.common.CommonMiddleware', | |
| 'django.middleware.csrf.CsrfViewMiddleware', | |
| 'django.contrib.auth.middleware.AuthenticationMiddleware', | |
| 'django.contrib.messages.middleware.MessageMiddleware', # <-- | |
| 'django.middleware.clickjacking.XFrameOptionsMiddleware', | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| s = "This string" | |
| print(f"The length of |{s}| is {len(s)} characters and contains {len(s.split())} tokens.") | |
| >>> "The length of |This string| is 11 characters and contains 2 tokens." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| purpose = "string formatting" | |
| percent_sign = "% operator" | |
| dot_format_method = ".format() method" | |
| string_literal = "f-string" | |
| quantity, revenue = 100, 25 | |
| d = {"costs": 300} | |
| list_operations = ['slicing and other things', 'indexing', 'key references'] | |
| limitations = ["be empty", "contain comments", "contain backslashes"] | |
| print("Before w used the %s for %s." % (percent_sign, purpose)) |