Results 1 to 3 of 3

Thread: Keeping state while generating ID numbers

  1. #1
    Join Date
    Dec 2015
    Beans
    13

    Keeping state while generating ID numbers

    Hi all -

    I have a Python coding question. I want to generate consecutive "ID numbers" - this is for a "toy scheduler" that will schedule jobs to be run.

    Anyway, here's the code that I've written so far -
    Code:
     
    
    #  toysched - a "toy" job scheduler. 
    #  Author - mooseman 
    #  This code is released to the public domain. 
    #  "Share and enjoy...... :) "  
    
    
    import datetime, itertools
    
    
    # Helper function to generate job IDs 
    def makeid():
      num = 0
      yield num
      num += 1
          
    
    # Scheduler class. This holds jobs
    class scheduler:
      def __init__(self):
        self.scheduledict = {}
        
      def add(self, job):
        self.scheduledict.update({job.jobid: job.detailslist})
        
      def update(self, id, dict):
        if id in scheduledict:
          scheduledict.update(dict)
        else:
          print "That jobid is not in the schedule."                                    
                  
      def remove(self, id):
        if id in scheduledict:
          del scheduledict[id]
        else:
          print "That jobid is not in the schedule."                                                            
        
      def show(self):
        #print [ k,v for (k,v) in self.scheduledict ]    
        print list(self.scheduledict.viewitems())  
                   
    
    #  A job class 
    class job():
      def __init__(self):
        self.nextid = makeid().next()   
        self.jobdict = {}      
        self.jobname = None 
        self.jobid = None  
        self.predepslist = [] 
        self.postdepslist = [] 
        self.execpath = None 
        self.execname = None    
        self.hold = "N" 
        self.groupslist = [] 
        self.freq = None 
        self.runtime = None
        self.detailslist = []      
    
      def create(self, name):
        self.jobname = name
        self.jobid = self.nextid  
        makeid().next() 
        self.detailslist = [self.jobname, self.predepslist, 
             self.postdepslist, self.execpath, self.execname, 
             self.hold, self.groupslist, self.freq, self.runtime]  
        self.jobdict.update({self.jobid: self.detailslist})    
                  
      def show(self):
        #print [ k,v for (k,v) in self.jobdict ]     
        print list(self.jobdict.viewitems()) 
                              
    
    # Create a couple of jobs 
    job1 = job()
    job1.create("FOO-BAR-BAZ")
    
    job2 = job()
    job2.create("MOOSE-SQUIRREL-BEAVER")
    
    s = scheduler()
    s.add(job1)
    s.add(job2)
    s.show()
    I want to generate job IDs that increase by 1 for each new job created, so some kind of "keeping state" seems to be needed.
    Anyway, that isn't happening and I'm only getting the second job printed by the scheduler.

    I hope someone can help. Many thanks in advance - bye for now -
    - trilobite

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Keeping state while generating ID numbers


  3. #3
    Join Date
    Dec 2015
    Beans
    13

    Re: Keeping state while generating ID numbers

    Hi spjackson! Thanks very much for that - that'll do the trick nicely!

    Thanks again, bye for now
    - trilobite

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •