Skip to the content.
The Shared Backbone: Creating the Core Utilities Layer | AI Systems Design From Scratch

Connect with Amin Boulouma Official

AI Systems Design From First Principles - An implementation of AI Systems Design From First Principles | Product Hunt

🏠 Documentation Hub πŸ“ Engineering Blog πŸ’» GitHub Repository

The Shared Backbone: Creating the Core Utilities Layer

Amin Boulouma β€” Software Engineer

Every custom framework requires a sturdy collection of shared primitives. In our previous deep dives, we relied heavily on an internal module called ai_systems_design.utils to handle low-level operations like reading templates, exporting rendered HTML, and managing underlying configurations.

When you build from scratch, even basic capabilities like writing a string to a file or spinning up a local network socket must be intentionally abstracted. Let’s look at the implementation of our shared utilities layer and explore how it wraps critical I/O boundaries.


The Core Utilities Module

import socket

class FileOperationsUtility:
    @staticmethod
    def read_decoded(file_path):
        """Reads a file in raw binary mode and decodes it safely to UTF-8."""
        with open(file_path, 'rb') as f: 
            return f.read().decode('utf-8')

    @staticmethod
    def write_encoded(path, content):
        """Encodes string content into UTF-8 and writes it as raw binary."""
        with open(path, 'wb') as f: 
            f.write(content.encode('utf-8'))


class SocketUtility:
    @staticmethod
    def create_socket_server(host, port, context):
        """Spins up a streaming TCP socket server bound to a specific port."""
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_socket.bind((host, port))
        server_socket.listen(1)
        print(f'{context} Server listening on {host}:{port}')
        return server_socket

    @staticmethod
    def connect_to_socket_server(host, port, context):
        """Establishes a client-side connection to a target TCP socket server."""
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_socket.connect((host, port))
        print(f'Connected to {context} server')
        return server_socket


Architectural Breakdown

1. Robust File I/O with Binary Enforcement

You might wonder why FileOperationsUtility uses raw binary mode ('rb' / 'wb') paired with explicit .decode('utf-8') and .encode('utf-8') operations instead of standard text-mode wrappers (open(path, 'r')).

By enforcing binary execution, the engine circumvents operating-system-specific default text encodings (such as the distinct differences between Windows CP1252 and Unix UTF-8). This layer ensures that whether a layout template is compiled on a Mac laptop or a Linux build container, character layouts, emojis, and punctuation render identically without throwing UnicodeDecodeError exceptions.

2. Network Extensibility with Socket Primitives

The addition of SocketUtility establishes the groundwork for an essential feature of any modern static site generator: a Local Live-Reload Server.

Using standard Python socket bindings, this abstract helper wraps low-level network operations:


Why Abstracting Utilities Matters

Connect with Amin Boulouma Official