I am having a problem with shelve......

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fearx351
    New Member
    • Aug 2010
    • 4

    #1

    I am having a problem with shelve......

    I am new to python and am actually using python as a result of taking a computer science class in college. I am writing a program to fit a hypothetical scenario. Well I have ran into a problem..Please help me!

    the traceback and snippet are below.

    Code:
    Code:
    import shelve
        
        high_space = 3000
    
        file_sizeh = file_size * week_selec
        h_back = high_space - file_sizeh
        db = shelve.open("netback.dat")
        db['hremspace'] = h_back
        db.close()
        for h_back in hremspace != 3000 :
            print'There are ',db['hremspace'],' Mb\'s left in High priority storage'
            print'after your backup of ',file_sizeh,' Mb\'s'
            db['hremspace'] = h_back
            db['netback'] = back_name
            db['Highprofile'] = file_sizeh
        else:
            for h_back in db['hremspace'] > 0:
                print'There are ',db['hremspace'],' Mb\'s left in High priority storage'
                print'after your backup of ',file_sizeh,' Mb\'s'
                db['hremspace'] = h_back
                db['netback'] = back_name
                db['Highprofile'] = file_sizeh
            else:
                print'There is not enough space to save your backup.'
                print'Please delete a backup and try again.'
    File "C:\Users\------\Desktop\finalp rojbac.py", line 112, in high_Priority
    for h_back in hremspace != 3000 :
    NameError: global name 'hremspace' is not defined
    Last edited by bvdet; Aug 10 '10, 06:00 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Line number 10 is the problem. The identifier hremspace has not been assigned to any value. The dictionary entry db['hremspace'] was assigned to h_back.

    You have another problem on that line. Notice the TypeError:
    Code:
    >>> for item in (1,2,3,4,5) != 3000:
    ... 	print item
    ... 	
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
    TypeError: iteration over non-sequence
    Maybe you need something like this:
    Code:
    >>> for item in (1,2,3,4,5):
    ... 	if item != 3000:
    ... 		print item
    ... 		
    1
    2
    3
    4
    5
    >>>

    Comment

    • Fearx351
      New Member
      • Aug 2010
      • 4

      #3
      oh ok I guess I was trying to compare a value to a key? I am new to this so its still a big learning process for me . Basically I am trying to check the entry to make sure that value isnt 3000 . The reason being,is that If I didnt 3000 would be assigned everytime the function was executed. Like I said I am new to this and I really couldnt think of any other way around this.

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Try printing first to understand what the dictionary is.
        Code:
        db = shelve.open("netback.dat")
        print "hremspace value is", db['hremspace']
        db['hremspace'] = 1000
        print "hremspace value is now", db['hremspace'] 
        x = 100
        db['hremspace'] += x
        print "hremspace value is now", db['hremspace']
        print'There are ',db['hremspace'],' Mb\'s left in High priority storage'
        print'after your backup of ',file_sizeh,' Mb\'s'
        If you are trying to compute the size of the backup or space remaining please supply info on what you wish to do (and sample input data would help).

        Comment

        • Fearx351
          New Member
          • Aug 2010
          • 4

          #5
          Basically what I want to do is have a set value(backup) and when the user inputs a specific backup. it will access the db and update and or subtract the necessary value.

          I will post my current code just so you can get a better idea of what I am trying to do.

          Code:
          ##Jeffrey Clark
          ##IT104
          ##Professor Brooker
          ##08/04/2010
          ##This program will create a network backup for user.
          
          def main():
              ##Main Function
              
              get_Command()
          
          def get_Command():
              ##Displays the Main Menu
              
              print'[]=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=[]'
              print'[]This program will create a network backup routine for the user.[]'
              print'[]=\=\=\=\=\=\=\=[]/=/=/=/=/=/=/=/=/=/=/=/=/=/=/[]=\=\=\=\=\=\=\=[]'                
              print'                 []..........Main Menu..........[]'
              print'                 []=\=\=\=\=\=\=\=\=\=\=\=\=\=\=[]'
              print'                 []1.Create a New Backup        []'
              print'                 []2.Remove a Backup Item       []'
              print'                 []3.View Current Backups       []'
              print'                 []4.Exit                       []'
              print'                 []=/=/=/=/=/=/=/=/=/=/=/=/=/=/=[]'
              get_Selection()
          
          def get_Selection():
              ##Gets the users selection from the Menu
              
              print
              selection = input('Please make a selection from the Menu: ')
          
              if selection == 1:
                  new_Backup()
              else:
                  if selection == 2:
                      rem_Backup()
                  else:
                      if selection == 3:
                          readFile()
                      else:
                          if selection == 4:
                              exit_prog()
                          else:
                              print'You have chosen an invalid Option'
                              get_Selection()
                          
          def new_Backup():
          
              global file_size
              global back_name
              ##Creates a new Backup for user
          
              back_name = raw_input('Please enter the name of the Backup: ')
              file_size = input('Please enter the size of the file you wish to backup in MB\'s:')
              net_week()
              
          def net_week():
              ##
              global week_selec
              
              print''
              print'[]How often will the backup be backed up?[]'
              print'[]Make a selection 1-7'
              print'[]'
            
              week_selec = input('Please make a selection from Menu:')
              if week_selec <= 7:
                  week_selec = week_selec
              else:
                  print'The number you have chosen is out of range'
              file_Prior()
          
          def file_Prior():
              ##
              
              
              print''
              print'[]What is the priority level of Backup?[]'
              print'[]1.High Priority'
              print'[]2.Medium Priority'
              print'[]3.Low Priority'
              prior_selec = input('Please make a selection from Menu:')
          
              if prior_selec == 1:
                  high_Priority()
              else:
                  if prior_selec == 2:
                      prior_selec =  m_prior
                      med_Priority()
                  else:
                      if prior_selec == 3:
                          prior_selec =  l_prior
                          low_Priority()
                      else:
                          print'You have made an invalid selection'
                          file_prior()
          
          def high_Priority():
              ##
              global file_sizeh
              print''
              import shelve
              
              
              high_space = 3000
              file_sizeh = file_size * week_selec
              h_back = high_space - file_sizeh
              db = shelve.open('netback.dat')
              db['hremspace'] = h_back
              db['netback'] = back_name
              db['Highprofile'] = file_sizeh
              print db.keys()
                               
              if db['hremspace'] != 3000:
                  
                  print'There are ',db['hremspace'],' Mb\'s left in High priority storage'
                  print'after your backup name:',db['netback'],'of',file_sizeh,' Mb\'s'
                  db['hremspace'] = h_back
                  db['netback'] = back_name
                  db['Highprofile'] = file_sizeh
                  
              else:
                  for h_back in back_name > 0:
                      print'There are ',db['hremspace'],' Mb\'s left in High priority storage'
                      print'after your backup of ',file_sizeh,' Mb\'s'
                      db['hremspace'] = h_back
                      db['netback'] = back_name
                      db['Highprofile'] = file_sizeh
                  else:
                      print'There is not enough space to save your backup.'
                      print'Please delete a backup and try again.'
                      
               
          def med_Priority(file_sizem,):
              ##
              print''
              med_space = 5000
              
              file_sizem = file_size * week_selec
              m_back = med_space - file_sizem
          
              if m_back > 0 :
                   print'There are ',m_back,' Mb\'s left in Medium priority storage'
                   print'after your backup of ',file_sizem,' mb\s'
                   write_File()
              else:
                  print'There is not enough space to save your backup.'
                  print'Please delete a backup and try again.'
          
              import shelve
              ldb = shelve.open("netbackl.dat")
              ldb['netback'] = back_name
              ldb['Medprofile'] = file_sizem
                  
          def low_Priority(file_sizel,):
              ##
              print''
              low_space = 9000
          
              file_sizel = file_size * week_selec
              l_back = low_space - file_sizel
          
              if l_back > 0 :
                   print'There are ',l_back,' Mb\'s left in Low priority storage'
                   print'after your backup of ',file_sizel,' Mb\s'
                   write_File()
              else:
                  print'There is not enough space to save your backup.'
                  print'Please delete a backup and try again.'
                  
              import shelve
              ldb = shelve.open("netbackl.dat")
              ldb['netback'] = back_name
              ldb['Lowprofile'] = file_sizel
              
          def write_File():
              ##
              
              import shelve
              db = shelve.open("netback.dat")
              db['netback'] = back_name
              db['filesize'] = file_size
              print db.keys()
              try:
                  print db['netback']
                  print db['filesize']
              finally:
                  db.close()
              
          def readFile(db,):
              ##Opens file to read
              print'asd'
              try:
                  print db['netback']
                  print db['filesize']
              finally:
                  db.close()
          main()
          I am sorry for the unorganized code I am a student and I am still learning.
          Last edited by Fearx351; Aug 24 '10, 01:06 AM. Reason: added code

          Comment

          • Fearx351
            New Member
            • Aug 2010
            • 4

            #6
            bump........... .........

            Comment

            Working...