CategoryCoding

Python: Comprehensions

Comprehensions build data structures with one or more iterator. This approach reduces syntax when building lists, dictionaries, or sets from conditional and loop statements. Build a list with a list comprehension import string alphabet = string.ascii_lowercase alphabet_list = [letter for letter in alphabet] print(alphabet_list) Build a dictionary with a dictionary comprehension import string...

Python: A Primer on Lists

Python lists can convert other data types to lists. They can be separated into single values with offset and split or groups of items (slices) with extract. An empty pair of square brackets creates an empty list empty_list = [] Or with the list() function empty_list = list() Convert a string list_from_string = list('triangle') Convert a tuple our_tuple = ('ready', 'set', 'go') list_from_tuple...

Dependency Inversion w/Mongo Engine + Flask

Page defines our object’s attributes. This allows us to create an object instance with the following attributes: author, content, title, and meta_attributes. /app/domain/page/page_model.py from cms.services.schema.page_schema import PageSchema class Page(PageSchema): """ Page class """ def __init__(self, author, content, title, meta_attributes, *args, **values): """ Instantiate page object...

Python: A Primer on Dictionaries

Python dictionaries are indexed by keys. Strings and numbers can always be keys. Tuples can be too if they contain strings, numbers, or tuples. Dictionaries consist of key and value pairs. An empty pair of braces creates an empty dictionary empty_dictionary = {} You can list a dictionary’s keys my_dictionary = {'User 1': 'AwesomePassword', 'User 2': 'AnotherPassword'}​list(my_dictionary)...

Deploy Flask on Nginx w/uWSGI

Required: Updated Debian 10 server with root privileges and a domain name. Install Nginx apt install nginx Verify Nginx is running by navigating to the server’s IP address. Install Flask & uWSGI apt install python3-pip python-dev pip3 install uwsgi flask Create Web Directory mkdir /var/www/[project_name] Create Simple App nano /var/www/[project_name]/main.py Add the following:...