Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:20:38+00:00 2026-05-27T06:20:38+00:00

I have an SQL template where I need to substitute random UUIDs. from string

  • 0

I have an SQL template where I need to substitute random UUIDs.

from string import Template
import uuid

def gen_uuid():
    return str(uuid.uuid4())

s = """
insert ... values('foo', ${uuid});
insert ... values('bar', ${uuid});
insert ... values('baz', ${uuid});
"""

mappings = {
    "uuid": gen_uuid  # how??
}

template = Template(s)
print template.safe_substitute(mappings)

How can I bind different UUIDs values to the same template key?

Update

Ok.. I ended up overriding getitem. Not ideal but works..

class UUIDDict(dict):
    def __init__(self, *args, **kwargs):
        self.update(*args, **kwargs)

    def __getitem__(self, key):
        if key == 'uuid':
            return str(uuid.uuid4())

        return super(UUIDDict, self).__getitem__(key)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:20:39+00:00Added an answer on May 27, 2026 at 6:20 am

    string.Template() isn’t designed to substitute multiple values for a single key. Other templating systems may support this, but string.Template is meant to be dirt simple.

    That being said, it isn’t hard to do what you want in a loop and then combine the results using str.join:

    >>> from string import Template
    >>> import uuid
    >>> def gen_uuid():
            return str(uuid.uuid4())
    
    >>> s = Template("insert ... values('$name', ${uuid});")
    >>> t = [s.substitute(name=name, uuid=gen_uuid()) for name in ('foo', 'bar', 'baz')]
    >>> print '\n'.join(t)
    insert ... values('foo', ee1b1b21-c022-4de5-8f7c-0a4d0554ae49);
    insert ... values('bar', 0b96872f-ab0e-48cf-a025-f997f7976a0e);
    insert ... values('baz', 25165bb6-8b7b-4c87-9ce1-ca274fc51bc8);
    

    As suggested by the OP, a custom dictionary can be created that will build a new uuid() on each lookup:

    >>> class UUID_Dict(dict):
            def __missing__(self, key):
                if key == 'uuid':
                     return str(uuid.uuid4())
                raise KeyError(key)
    
    
    >>> s = Template("""
    insert ... values('foo', ${uuid});
    insert ... values('bar', ${uuid});
    insert ... values('baz', ${uuid});
    """)
    >>> print s.substitute(UUID_Dict())
    
    insert ... values('foo', eabc65b6-1294-43b7-9506-61a4e324e0f2);
    insert ... values('bar', 93e4f0b7-7fa1-4e88-9696-e361da31358f);
    insert ... values('baz', 503d34a3-17a4-4513-8ce0-b5efb70c94cc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
I have setup SQL Profiler as follows - Template: Tuning Save to dbo.myTable Event
I need help in this matter: We have a template of Excel file in
I have some things I need to use custom SQL for. Just to start,
Here's the problem I have, I need to generate a flat string file with
I have many small files containing code fragments, pseudo-code algorithms, classes, templates, SQL-samples, etc.,
I have SQL data that looks like this: events id name capacity 1 Cooking
I have SQL query, which is working nice on Oracle and MSSQL. Now I'm
We have SQL Server 2005 running on Win2003/64, and we push it hard. We
We have SQL Server database setup. We are setting up a replication scenarios where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.