Package Deployment

How code is packaged, deployed and reused across different projects, libraries and applications.

Python Package and Deploy

TypeScript Package and Deploy


Compile

Basics of how to compile and execute a basic program and other common tools.

Compiling and Executing a Basic Program. Python is an interpreted language, so compilation is typically implicit. However, Python does support bytecode compilation.

python script.py # Run a Python script python -m compileall script.py # Generate .pyc bytecode file

Some common tools:

  • pyinstaller: Package Python scripts into standalone executables.
  • cython: Compile Python code to C for performance improvements.

Example of creating an executable:

pip install pyinstaller pyinstaller --onefile script.py

To compile a TypeScript program, install the TypeScript compiler:

npm install -g typescript

Create a simple index.ts file:

const message: string = "Hello, TypeScript!"; console.log(message);

Compile the file:

tsc index.ts

This generates an index.js file that can be executed with Node.js:

node index.js

Modules

Create a reusable module in the local project. Manage exports and imports into a consuming module.

To creating a reusable module: - a module is a .py file containing functions, classes, or variables that can be imported elsewhere.

Example: mymodule.py

def greet(name): return f"Hello, {name}!"

To use it in another script:

import mymodule print(mymodule.greet("Alice"))

Managing Exports: - to control what gets exported:

__all__ = ["greet"]

Create a module in math.ts:

export function add(a: number, b: number): number { return a + b; }

Consume it in index.ts:

import { add } from "./math"; console.log(add(2, 3));

Compile and run:

tsc && node index.js

Import Package

Add an external package to a program and import it. Upgrade to new revisions and lock to a specific version.

Installing External Packages

Python uses pip to install external packages.

pip install requests # Install package pip list # List installed packages pip show requests # Show package details

Upgrading and Version Locking:

pip install --upgrade requests # Upgrade to latest pip install requests==2.25.1 # Install a specific version pip freeze > requirements.txt # Lock dependencies pip install -r requirements.txt # Install from lock file

To add an external package, use npm:

npm install lodash

Import and use it in index.ts:

import _ from "lodash"; console.log(_.capitalize("hello world"));

Lock to a specific version:

npm install lodash@4.17.21

Upgrade a package:

npm update lodash

Publish a Package

Author and publish a package for external consumption.

Creating a Package

Structure:

my_package/ ├── mymodule.py ├── __init__.py ├── setup.py

setup.py Example:

from setuptools import setup setup( name='my_package', version='0.1', packages=['my_package'], install_requires=[], )

Publishing to PyPI

pip install twine python setup.py sdist bdist_wheel python -m twine upload dist/*
  1. Initialize a package:

    npm init -y
  2. Add TypeScript support:

    npm install typescript --save-dev
  3. Write a module in index.ts:

    export function greet(name: string): string { return `Hello, ${name}!`; }
  4. Configure package.json:

    "main": "dist/index.js", "types": "dist/index.d.ts",
  5. Publish:

    npm login npm publish

Tests

Add tests to a package that can be run as part of CI/CD.

Adding Tests

Using unittest:

import unittest from mymodule import greet class TestGreet(unittest.TestCase): def test_greet(self): self.assertEqual(greet("Alice"), "Hello, Alice!") if __name__ == "__main__": unittest.main()

Running tests:

python -m unittest discover

For CI/CD:

pytest --junitxml=results.xml

Install a test framework:

npm install --save-dev jest @types/jest ts-jest

Configure Jest in jest.config.js:

module.exports = { preset: "ts-jest", testEnvironment: "node", };

Create math.test.ts:

import { add } from "./math"; test("adds 1 + 2 to equal 3", () => { expect(add(1, 2)).toBe(3); });

Run tests:

npm test

Custom Tools

Incorporate custom tools as part of the build process (e.g., OpenAPI code generation).

Integrating Code Generation (OpenAPI Example)

pip install openapi-generator-cli openapi-generator-cli generate -i api.yaml -g python -o generated_client

Custom Build Commands in setup.py

from setuptools import setup from setuptools.command.build_py import build_py class CustomBuild(build_py): def run(self): print("Running custom build step...") build_py.run(self) setup( name='my_package', version='0.1', cmdclass={'build_py': CustomBuild} )

Executing:

python setup.py build

Example: OpenAPI code generation

  1. Install OpenAPI generator:

    npm install @openapitools/openapi-generator-cli -g
  2. Generate TypeScript client:

    openapi-generator-cli generate -i openapi.yaml -g typescript-fetch -o generated-client
  3. Use it in a project:

    import { ApiClient } from "./generated-client"; const client = new ApiClient();