Search code examples
pythondatabasedictionaryshelve

Python simple database based on shelve, adding values


Hello guys I'm absolute beginner and I need to create simple database for my classes. I'm trying to modify the example code we received but there's so many possibilities and I'm getting lost in it.

The idea is that I can add/remove products with quantities and prices. I get stuck when it comes to modify quantity of product already in the list..

I've got following codes for entry and showing the list functions.

def entry():
    global base
    name=raw_input('Name of the product: ')
    quantity=raw_input('Quantity: ')
    price=raw_input('Price per unit: ')     
    if name not in base.keys():    
        base[name]=[int(quantity),int(price)
    print "Saved."
    elif name in base.keys():

there is first problem of giving a [name] more than 1 value, I can print it later but in they way I think is incorrect..

.

def articles():
    global base
    print 'List of articles in magazine'.center(50)
    print '-'*50
    print '|'+'name'.center(15)+'|'+'Quantity'.center(15)+'|'+'Price'.center(15)+'|'
    print '-'*50
    for name,quantity in base.items():
        print "|%14s |" % name,'%13s' % quantity[0],'|' '%13s' % quantity[1],'|' </code> ###  When I use here price instead of quantity[1] it shows error.

Solution

  • Maybe this example will help you in refactoring your code?

    import shelve
    
    global base
    global magazine_base
    
    def load_data():
        global base
        global magazine_base
        magazine_base = shelve.open('magazine_base')
        if magazine_base.has_key('base'):
            base = magazine_base['base']
            if not base:
                base = [{'name': 'Apple', 'price': 10, 'quantity': 1}]  # example one element base
                # base = []  # it can be also just empty list at the beginning                
        else:
            base = [{'name': 'Apple', 'price': 10, 'quantity': 1}]
            # base = []
            magazine_base['base'] = base
    
    def entry():
        global base
        global magazine_base
        load_data()
        name = raw_input('Name of the product: ')
        quantity = raw_input('Quantity: ')
        price = raw_input('Price per unit: ')
    
        is_new_entry = True
    
        # entry is element of base list
        for entry in base:
            if name.lower() == entry['name'].lower():  # checking if name already exist in base list
                is_new_entry = False
    
        if is_new_entry:  # adding new list element
            base += [{
                'name': name, 
                'quantity': int(quantity),
                'price': float(price)
            }]
            magazine_base['base'] = base
            magazine_base.close()
            print 'Saved.'  # confirmation message
        else:
            print 'Element with the name {} already exist!'.format(name)  # error message
            magazine_base.close()
    
    
    def articles():
        global base
        print 'List of articles in magazine'.center(50)
        print '-'*50
        print '|'+'Name'.center(15)+'|'+'Quantity'.center(15)+'|'+'Price'.center(15)+'|'
        print '-'*50
        for entry in base:
            print "|%14s |" % entry['name'],'%13s' % entry['quantity'],'|' '%14s' % entry['price'],'|'
        print '-'*50
    
    # testing by running functions
    entry()
    articles()
    entry()
    articles()
    

    Updated with use of shelve.

    Checked for you and it is working fine.

    Hope this helps.