Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 9th, 2012, 11:11 PM
knutrainer knutrainer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: U.S A.Z.
Posts: 24 knutrainer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 14 m 44 sec
Reputation Power: 0
Send a message via AIM to knutrainer
Help storing object with the shelve module

When I try to run the add_contact Function I get an error. I am trying to get info from the user and store it using the shelv module. I am using python 2.7.3 Any help worth exactly two brownie points.
Code:
File "knutphonedir.py", line 42, in add_contact
    for object in (whole_name):
TypeError: iteration over non-sequence


Code:
import shelve


class Phonedir():
	def __init__(self,fname,lname,number):
		self.fname = fname
		self.lname = lname
		self.number = number

	def display(self):
		print (self.fname, self.lname, self.number)
	
	

def add_contact():

	lname = raw_input('Last name: ')
	fname = raw_input('First name: ')
	number = raw_input('Number: ')
	whole_name = (lname + ' ' + fname)
	whole_name = Phonedir(lname,fname,number)
	
	db = shelve.open('knutphonedir.db')
	for object in (whole_name):
		db[object.lname] = object
	db.close()

Reply With Quote
  #2  
Old December 10th, 2012, 03:25 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,161 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 1 Day 14 h 7 m 9 sec
Reputation Power: 1736
You assign whole_name with a string (consisting of the name) and then a Phonedir object.
What will be the correct/expected value/type?

What exactly do you want the for-loop iteration to do?

The error happens because the Phonedir class doesn't have any knowledge/implement about iteration.
Comments on this post
knutrainer agrees: Asking me these questions led me to solve my problem.

Reply With Quote
  #3  
Old December 10th, 2012, 09:20 PM
knutrainer knutrainer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: U.S A.Z.
Posts: 24 knutrainer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 14 m 44 sec
Reputation Power: 0
Send a message via AIM to knutrainer
Thank you for your help. The key to finding asnwers is by asking the right questions.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help storing object with the shelve module

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap