Repository: apify/crawlee-python
File: src/crawlee/storage_clients/_memory/_request_queue_client.py (Python)

Code before:
            request_id: The identifier of the request to be deleted.
            entity_directory: The directory path where the request file is stored.
        """
        # Ensure the directory for the entity exists
        await asyncio.to_thread(os.makedirs, entity_directory, exist_ok=True)

        file_path = os.path.join(entity_directory, f'{request_id}.json')
        await force_remove(file_path)

    async def _create_internal_request(self, request: Request, forefront: bool | None) -> InternalRequest:
        order_no = self._calculate_order_no(request, forefront)
        id = unique_key_to_request_id(request.unique_key)

        if request.id is not None and request.id != id:
            logger.warning(
                f'The request ID does not match the ID from the unique_key (request.id={request.id}, id={id}).'
            )

        return InternalRequest.from_request(request=request, id=id, order_no=order_no)

    def _calculate_order_no(self, request: Request, forefront: bool | None) -> Decimal | None:
        if request.handled_at is not None:
            return None

        # Get the current timestamp in milliseconds
        timestamp = Decimal(str(datetime.now(tz=timezone.utc).timestamp())) * Decimal('1000')
        timestamp = round(timestamp, 6)

        # Make sure that this timestamp was not used yet, so that we have unique order_nos
        if timestamp <= self._last_used_timestamp:
            timestamp = self._last_used_timestamp + Decimal('0.000001')

        self._last_used_timestamp = timestamp

        return -timestamp if forefront else timestamp


Review comment (suggestion):
Super tiny detail. Decimal('1000') == Decimal(1000). Not needed to use strings instead of ints.  (For floats we need strings)

In docs they do not use it for int either https://docs.python.org/3/library/decimal.html


Code after:
            request_id: The identifier of the request to be deleted.
            entity_directory: The directory path where the request file is stored.
        """
        # Ensure the directory for the entity exists
        await asyncio.to_thread(os.makedirs, entity_directory, exist_ok=True)

        file_path = os.path.join(entity_directory, f'{request_id}.json')
        await force_remove(file_path)

    async def _create_internal_request(self, request: Request, forefront: bool | None) -> InternalRequest:
        order_no = self._calculate_order_no(request, forefront)
        id = unique_key_to_request_id(request.unique_key)

        if request.id is not None and request.id != id:
            logger.warning(
                f'The request ID does not match the ID from the unique_key (request.id={request.id}, id={id}).'
            )

        return InternalRequest.from_request(request=request, id=id, order_no=order_no)

    def _calculate_order_no(self, request: Request, forefront: bool | None) -> Decimal | None:
        if request.handled_at is not None:
            return None

        # Get the current timestamp in milliseconds
        timestamp = Decimal(str(datetime.now(tz=timezone.utc).timestamp())) * Decimal(1000)
        timestamp = round(timestamp, 6)

        # Make sure that this timestamp was not used yet, so that we have unique order_nos
        if timestamp <= self._last_used_timestamp:
            timestamp = self._last_used_timestamp + Decimal('0.000001')

        self._last_used_timestamp = timestamp

        return -timestamp if forefront else timestamp