Setting up Python and VS Code

      Setting up Python and VS Code


        Article summary

        Step 1: Download and Install Visual Studio Code from the official website

        - Open Visual Studio Code and load the project folder.

        A screenshot of a computer  Description automatically generated

        Step 2: Install Python Locally

        - Download Python from the official website and install it. Ensure that Python is added to Path as shown below, by selecting the checkbox for “Add Python 3.10 to PATH” and click on “Customize Installation”.

        A screenshot of a computer  Description automatically generated

        -In the following window, click on “Next” without changing any optional features.

        -In the following window, click on “Next” without changing any optional features.

        -In the following window, click on “Install” without changing any Advanced Options.

        -Once installed, the “Setup was successful” window will show up.

        - Verify the installation by running `python --version` in the command prompt. Ensuring that Python is successfully added to the Path.

        A screenshot of a computer program  Description automatically generated

        Step 3: Link Python Environment to Visual Studio Code

        - Open a Python file in Visual Studio Code and click the compiler selection tab as shown below. A screenshot of a computer  Description automatically generated - Next, select the Python environment that was installed by referring to the installation location. Your device may have different Python versions, hence selecting the correct Python interpreter is crucial for any further progress.

        A screenshot of a computer  Description automatically generated

        Step 4: Install Required Libraries

        - Open the terminal in Visual Studio Code

        A screenshot of a computer  Description automatically generated

        - Use the “pip” command to install any Python libraries.

        The main libraries used by the Assette Data Block Editor are “pandas” and “numpy” libraries. To install these libraries, type in ‘pip install pandas’ and press Enter, and next ‘pip install numpy’. Similarly any other libraries used by the code can be found online and installed. -An alternate solution is to type in the names of the libraries in a text file, and installing them. For example, by creating a text file named “requirements.txt”, and adding the library names as shown below, all the required libraries can be installed at once without the need to install the libraries individually. A list of libraries and library versions used by the Block Editor are mentioned in Appendix A. A screenshot of a computer  Description automatically generated

        -Once the libraries are entered in the text file, save it.

        -Then type in ‘pip install -r requirements.txt’, to install all the libraries mentioned in the text file as shown below.

        A screenshot of a computer  Description automatically generated

        Step 5: Optional - Set Up Virtual Environment (only if a virtual environment is required)

        - Open the terminal in Visual Studio Code and create a virtual environment using `python -m venv venv`.

        - Activate the virtual environment with `venv\Scripts\activate`.

        - Install required libraries by referring to the libraries mentioned in Appendix A, and use pip to install. Alternatively, using `pip install -r requirements.txt`, if the requirements.txt file contains the required libraries as mentioned in Appendix A.

        - Move all the project files into the virtual environment folder (named venv) if the virtual environment will be used, since the virtual environment can directly use the venv folder as the root folder.

        Step 6: Install Pytest Extension

        - Install the Pytest extension for Visual Studio Code as shown below.

        A screenshot of a computer  Description automatically generated

        Step 7: Configure Pytest

        - Restart Visual Studio Code and click on the flask icon (Pytest)

        A screenshot of a computer  Description automatically generated

        - Click on "Configure Python Tests"

        -Click on "Pytest"

        A screenshot of a computer  Description automatically generated

        - Select the root folder (or the venv folder if the virtual python environment is used), where the unit tests are available.

        A screenshot of a computer  Description automatically generated

        - Now any unit tests created inside the root folder are available as shown in the image below.

        A screenshot of a computer  Description automatically generated

        Step 8. Creating a Unit Test

        By default, the download option from the Assette Block Editor provides a unit test. Let’s use the DEMO_TopBottomNHoldings block as an example. By downloading this block, a folder named unit_test contains the unit test Python file. Inside the unit test file, a function would be available with the following structure. A black screen with white text  Description automatically generated

        By adding sample parameters needed for testing, and the required assert statements, a unit test can be created. The assert statement is a built-in construct that allows you to test assumptions about your code. A sample unit test is as follows.

        A screen shot of a computer code  Description automatically generated In here, sample parameters needed for the data block are added, along with a positive test case. The statement, “assert "data" in response” checks for a header named “data”, whilst “assert len(response["data"]) == 5 ” checks for the number of datasets available inside the “data” header. Similarly, unit tests can be created by utilizing various assert statements.

        Appendix A – List of Python Libraries and functions black-listed/supported by the Block Editor  

        Restricted keywords / Built-in functions:  "__subclasses__", "open", "exit", "quit", "exec", "eval", "compile", "memoryview", "type", "globals", "locals", "vars", "dir", "input", "help", "memoryview"

        Note below the "Allowed libraries". If you intend to use any of the allowed libraries they need to be added as a Python Environment Type block. e.g. “CalculationEnv”.

        Allowed libraries:  "pandas", "numpy", "json", "base64", "datetime", "matplotlib.pyplot", "textwrap3", "typing", "functools", "calendar","pygal","pygal-maps-world","pytz","random","math", html.parser and SciPy

        As a best practice, if you are adding a new library to support a specific Data Block Requirement, it is advisable to have that Block to use CalculationEnv2 (with the additional library) and for other Data Blocks to use CalcuationEnv1 (with the default libraries). If CalculationEnv2 is used for a Block which doesn't require the additional library it could affect performance.


        What's Next