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

  • Home
  • SEARCH
  • 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 158917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:46:00+00:00 2026-05-11T10:46:00+00:00

I’ve got a Perl function which takes a timestamp and returns either the unchanged

  • 0

I’ve got a Perl function which takes a timestamp and returns either the unchanged timestamp (if it’s never seen it before) or otherwise, it appends some letters to make it unique:

sub uniqify($) {   my $timestamp = shift;    state $last_ts = -1;   state $next_letter = 'A';    if ($timestamp == $last_ts) {     $timestamp .= $next_letter++;   } else {     $last_ts = $timestamp;     $next_letter = 'A';   }    return $timestamp; } 

So if you call it four times, with the values 1, 1, 1, and 2, it will return 1, then 1A, then 1B, then 2.

Note: It only ever gets called with ever-increasing timestamps, so it doesn’t need to recall every one it’s ever seen, just the last one.

Now I need to translate this function to Python. I’ve learned that I can replace the ‘state’ variables with globals (yuck!) or perhaps attach them to the function as attributes, but neither of those is particularly elegant.

Also, Python doesn’t have something like Perl’s magic autoincrement, where if you ‘++’ a variable whose value is ‘A’, it becomes ‘B’ — or if it’s ‘Z’, it becomes ‘AA’. So that’s a curveball too.

I’m hacking together a solution, but it’s really ugly and hard to read. Translating from Perl to Python is supposed to have the opposite effect, right? 🙂 So I’m offering this as a challenge to SO users. Can you make it an elegant Python function?

  • 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. 2026-05-11T10:46:00+00:00Added an answer on May 11, 2026 at 10:46 am

    Look at this answer for a robust method to convert a number to an alphanumeric id

    The code I present doesn’t go from ‘Z’ to ‘AA’, instead goes to ‘BA’, but I suppose that doesn’t matter, it still produces a unique id

    from string import uppercase as up import itertools  def to_base(q, alphabet):     if q < 0: raise ValueError( 'must supply a positive integer' )     l = len(alphabet)     converted = []     while q != 0:         q, r = divmod(q, l)         converted.insert(0, alphabet[r])     return ''.join(converted) or alphabet[0]  class TimestampUniqifier( object ):     def __init__(self):         self.last = ''         self.counter = itertools.count()     def __call__( self, str ):         if str == self.last:             suf = self.counter.next()             return str + to_base( suf, up )         else:             self.last = str             self.counter = itertools.count()             return str              timestamp_uniqify = TimestampUniqifier() 

    usage:

    timestamp_uniqify('1') '1' timestamp_uniqify('1') '1A' timestamp_uniqify('1') '1B' timestamp_uniqify('1') '1C' timestamp_uniqify('2') '2' timestamp_uniqify('3') '3' timestamp_uniqify('3') '3A' timestamp_uniqify('3') '3B' 

    You can call it maaaany times and it will still produce good results:

    for i in range(100): print timestamp_uniqify('4')  4 4A 4B 4C 4D 4E 4F 4G 4H 4I 4J 4K 4L 4M 4N 4O 4P 4Q 4R 4S 4T 4U 4V 4W 4X 4Y 4Z 4BA 4BB 4BC 4BD 4BE 4BF 4BG 4BH 4BI 4BJ 4BK 4BL 4BM 4BN 4BO 4BP 4BQ 4BR 4BS 4BT 4BU 4BV 4BW 4BX 4BY 4BZ 4CA 4CB 4CC 4CD 4CE 4CF 4CG 4CH 4CI 4CJ 4CK 4CL 4CM 4CN 4CO 4CP 4CQ 4CR 4CS 4CT 4CU 4CV 4CW 4CX 4CY 4CZ 4DA 4DB 4DC 4DD 4DE 4DF 4DG 4DH 4DI 4DJ 4DK 4DL 4DM 4DN 4DO 4DP 4DQ 4DR 4DS 4DT 4DU 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 107k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Boost, being as modern C++ as it is, uses inline… May 11, 2026 at 9:08 pm
  • Editorial Team
    Editorial Team added an answer soap4r is not very pretty. I poked around the rdocs… May 11, 2026 at 9:08 pm
  • Editorial Team
    Editorial Team added an answer This article describes how to do this using the XMLDataSource… May 11, 2026 at 9:08 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.