Python 基于shelve的简单数据库,加值
Python simple database based on shelve, adding values
大家好,我是初学者,我需要为 类 创建简单的数据库。
我正在尝试修改我们收到的示例代码,但有太多的可能性,我迷失了方向。
我的想法是,我可以 add/remove 带有数量和价格的产品。
当涉及到修改列表中已有产品的数量时,我被卡住了..
我有以下用于输入和显示列表功能的代码。
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():
第一个问题是给 [name]
一个以上的值,我可以稍后打印,但我认为他们的方式不正确..
.
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.
也许这个例子可以帮助您重构代码?
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()
使用 shelve
更新。
已为您检查,它工作正常。
希望对您有所帮助。
大家好,我是初学者,我需要为 类 创建简单的数据库。 我正在尝试修改我们收到的示例代码,但有太多的可能性,我迷失了方向。
我的想法是,我可以 add/remove 带有数量和价格的产品。 当涉及到修改列表中已有产品的数量时,我被卡住了..
我有以下用于输入和显示列表功能的代码。
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():
第一个问题是给 [name]
一个以上的值,我可以稍后打印,但我认为他们的方式不正确..
.
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.
也许这个例子可以帮助您重构代码?
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()
使用 shelve
更新。
已为您检查,它工作正常。
希望对您有所帮助。