Self-Incremental Counters

Sequential numbers are sometimes needed for naming conventions and require self-incrementing counters be created and maintained. We do not recommend using the BaseSpace Clarity LIMSdatabase for this. However the Unix “(n)dbm” library provides an easy way to create and manage counters by creating Dbm objects that behave like mappings (dictionaries).

Solution

They way this would work is the attached script ( and the counters file it creates / manages ) would live on the Clarity server and other scripts would depend upon it, and use code similar to below whenever a sequential number was needed. While the script is written in python and uses the dbm module there is nothing inherently Pythonic about this code that couldn’t be reimplemented in another language. However, more information on the Python dbm module can be found at: https://docs.python.org/2/library/dbm.html

def test1():

   cm = counterManager()
   cm.setPath( "./test" )
   if cm.setup() is True:
      print( "INFO: setup Counter Manager" )
      print( "INFO: attempting to call getNextValue for: testA ..." )
      tmp = cm.getNextValue( "testA" )
      print( "INFO: getNextValue returned:" + str(tmp) )
   else:
      print( "ERROR: Failed to setup Counter Manager" )

User Interaction

  • The counters live in a file, the path to which is defined in the cm.setPath() command. The file will be created if it doesn’t exist.

  • The file can contain as many counters as you wish (it’s better to have many counters in one file than many files each with only one counter)

  • The name of the counter is passed to the function cm.getNextValue(). If this is the first time the counter has been used, it will be created and added to the file.

  • Each time you want the next value just call cm.getNextValue() for that counter and you will be given the next value.

  • The counters and the file will look after themselves, you don’t need to explicitly update / save them – this is all handled behind the scenes.

Assumptions and Notes

  • You are running a version of Python that is supported by Clarity LIMS, as documented in the Clarity LIMS Technical Requirements.

  • The example code is provided for illustrative purposes only. It does not contain sufficient exception handling for use 'as is' in a production environment.

Attachments

clarityCounters.py:

Last updated