Creating Azure Function App for Python with Azure Functions Core Tools CLI

We cannot specify the Python runtime version with FUNCTIONS_EXTENSION_VERSION. The FUNCTIONS_EXTENSION_VERSION setting is specifically for specifying the version of the Azure Functions runtime, not the version of Python.

The Azure Functions runtime is the host that runs your functions. It's a separate concept from the Python runtime. Each version of the Azure Functions runtime has a range of versions of Python (and other languages) that it supports, but you can't use the FUNCTIONS_EXTENSION_VERSION setting to choose a specific Python version.

The version of Python that's used in your function app is typically determined by the environment in which the app is running. When running locally, this will be the version of Python that's available in your local environment. When running in Azure, the Python version is determined by the configuration of your Azure Functions host.

If you want to specify a Python version for local development, you can do so using a Python version management tool like pyenv, and creating a .python-version file in the root of your function app project. For deployment in Azure, you specify the Python version when you create the Function App.

Remember to check the Azure Functions runtime support for your chosen Python version. Azure Functions 4.x runtime supports Python 3.7, 3.8, 3.9 and 3.10 for now.

Prerequisite check for Python

$ az --version
$ az login
$ func --version
$ python --version

Create and activate a virtual environment with virtualenv package manager

virtualenv (for Python 2) and venv (for Python 3) allow you to manage separate package installations for different projects.

$ mkdir azure-demo
$ cd azure-demo
$ pyenv local 3.10.5
$ python -m venv .venv
Continue reading

Azure Functions Basics

Before you begin, you must have the following requirements in place:

Prerequisite check

# to check that the Azure Functions Core Tools are version v4.0.5095 or above.
$ func --version

4.0.5198
# to check that the Azure CLI version is 2.4 or later.
$ az --version

azure-cli                         2.49.0

core                              2.49.0
telemetry                          1.0.8

Dependencies:
msal                              1.20.0
azure-mgmt-resource               22.0.0

Python location '/opt/homebrew/Cellar/azure-cli/2.49.0/libexec/bin/python'
Extensions directory '/Users/kenanhancer/.azure/cliextensions'

Python (Darwin) 3.10.12 (main, Jun  7 2023, 00:38:32) [Clang 14.0.3 (clang-1403.0.22.14.1)]

Legal docs and information: aka.ms/AzureCliLegal


Your CLI is up-to-date.
# to sign in to Azure and verify an active subscription.
$ az login

Continue reading

TypeScript const assertions vs satisfies operator

I compare as const assertions and satisfies operator in this post.

as const comes with TypeScript 3.4 and generates literal values for more precise and type-safe code.

satisfies comes with TypeScript 4.9 and generates more specific type and validates given object type so that it can catch possible error.

Notice that as const generates a type with readonly fields. So it is an immutable type.

Use case

Notice that satisfies just generates a type from palette object. As you can see below code, red field type is [number, number, number]

As you can see below code, red field type became string instead of [number, number, number]

Use case

satisfies operator catches unlisted field name usage, or wrong field type.

TypeScript const modifier on type parameters

It is a really good feature coming with TypeScript 5.0. We could infer type of object as general as shown in line 8 in below code, so, to infer more-specific type, we had to add as const as shown in line 11.

TypeScript 5.0 makes it easier with adding const in front of type parameter declaration in line 13 in below code.

Use case

Assume that Person type has hobbies , and i just want to infer passed values from hobbies field.

TypeScript Union Type and Distributive Conditional Types Part 2

I show the reason Distributive Conditional Type needs to be used for Union Type in this post.

If generic type T is passed as Union Type like string | number, then array item will be Union Type as below.

If you need to iterate on each type in Union Type, then Distributive Conditional Type (with extends keywod) needs to be used.

To avoid distributive behaviour in Distibutive Conditional Type, surround each side of extends keyword with square brackets.

Continue reading

How to install Python packages with pipenv, venv or virtualenv package manager

You can find GitHub repository in https://github.com/python-projects-kenanhancer/python_demo

  • Pyenv – Python Runtime Version Manager
    – to install, uninstall, list or switch between different python runtimes
  • Pipenv – Python Package Manager and Virtual environment manager
    – to create an isolated virtual environment with a specific python runtime
    – to install, uninstall, list python packages
  • Venv – Virtual environment manager (for Python 3)
    – to create an isolated virtual environment with a specific python runtime
    – python packages will be isolated already
  • Virtualenv – Virtual environment manager (for Python 2)
    – to create an isolated virtual environment with a specific python runtime
    – python packages will be isolated already

Installing Python Runtime Versions via Pyenv

In order to test different Python Runtime versions, I have installed some versions.

$ pyenv install 3.10.2
$ pyenv install 3.10.3
$ pyenv install 3.10.4
$ pyenv install 3.10.5

I have 4 Python runtime versions(3.10.2, 3.10.3, 3.10.4, 3.10.5) in my machine. So, let's list them.

$ pyenv versions

  system
  3.10.2
  3.10.3
  3.10.4
* 3.10.5 (set by /Users/kenanhancer/.pyenv/version)
Continue reading

Docker

Docker is written in the Go programming language.

The Docker daemon

The Docker daemon ( dockerd cli ) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.

A docker daemon can also communicate with other docker daemons to manage Docker services.

The Docker client

The Docker client ( docker cli ) is the primary way to interact with Docker.

When you use commands such as docker run, docker build, docker pull , the Docker client sends these commands to dockerd .

The docker command uses the Docker API.

The Docker client ( docker cli ) can communicate with more than one Docker daemon.

Continue reading

Linux Terminal Check stdin, stdout and stderr

When we want to check whether current terminal is attached to stdin, stdout and stderr streams or not, we can use the below codes. I tried with piping, and redirection so both works well.

As seen in below 6 usages, stdin is not listed in output of commands except first one. Because stdin is used by piping or redirection.

$  ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; echo hello)

stdin
stdout
stderr
hello
$ echo hello | ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat)

stdout
stderr
hello
$ ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat) <<<"hello"

stdout
stderr
hello
Continue reading

Docker: MySQL, MariaDB, PostgreSQL

docker-compose.yml

version: '3.4'

services:
  mysql:
    image: mysql:latest
    container_name: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
      MYSQL_USER: test
      MYSQL_PASSWORD: test
    ports:
      - 3306:3306
    volumes:
      - mysql_db_data_container:/var/lib/mysql:rw
      - ${PWD}/docker/mysql/initdb_sql_scripts:/docker-entrypoint-initdb.d/:ro
      - ${PWD}/docker/mysql/mysql.conf:/etc/mysql/conf.d
    healthcheck:
      test: "/usr/bin/mysql --user=root --password=password --execute \"SHOW DATABASES;\""
      interval: 2s
      timeout: 20s
      retries: 10

  mariadb:
    image: mariadb:latest
    container_name: mariadb
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
      MYSQL_USER: test
      MYSQL_PASSWORD: test
    ports:
      - 3307:3306
    volumes:
      - mariadb_data_container:/var/lib/mysql:rw
      - ${PWD}/docker/mariadb/initdb_sql_scripts:/docker-entrypoint-initdb.d/:ro
      - ${PWD}/docker/mariadb/mariadb.conf:/etc/mysql/conf.d

  postgres:
    image: postgres:latest
    container_name: postgres
    restart: always
    environment:
      POSTGRES_DB: test_db
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test
    ports:
      - 5432:5432
    volumes:
      - postgres_db_data_container:/var/lib/postgresql/data:rw
      - ${PWD}/docker/postgres/initdb_sql_scripts:/docker-entrypoint-initdb.d:ro
      - ${PWD}/docker/postgres/postgres.conf:/etc/postgresql/postgresql.conf

volumes:
  mysql_db_data_container:
  mariadb_data_container:
  postgres_db_data_container:
Continue reading