Assigning set/get methods from text file
I am relatively new to Python (~5 months), so pardon my ignorance. I have been tasked with creating a program to do a few things.
First, I have a text file with information stored like the following:
name, street, city state zip, birthday, pet.
Second, my instructions include:
- Reading the text file and determine the age and based on the age, determine a discount. For instance, if the age is 18 -25, they receive a 5% discount.
- Match the pet preference with a vendor
I am utilizing classes for each of the attributes. I have a separate file for the class information which is indicated below. I have set/get methods for each attribute listed.
From information.py
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.__name = name
def set_name(self,name):
self.__name = name
def get_name(self):
return self.__name
At the end I have to write a letter for each individual. I can't figure out how to essentially keep the data separate in order to write the letter per individual. I will write each line using a .write(str) method. Here is my main program so far:
import information
import os
import time
CURRENT_YEAR = 2018
def main():
customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
tempFile = open('temp.txt','w')
with open('c.TXT') as customerList:
lines = customerList.readlines()
for line in lines:
tempFile.write( str(GetInfo.name) + 'n')
tempFile.close()
main()
For the vendor list I will have a dictionary such as:
vendors = {"Cat:CatCity"}
I am perplexed at my next step, while also receiving this error when trying to run the program:
"main.py", line 10, in main customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
NameError: name 'name' is not defined
python
|
show 8 more comments
I am relatively new to Python (~5 months), so pardon my ignorance. I have been tasked with creating a program to do a few things.
First, I have a text file with information stored like the following:
name, street, city state zip, birthday, pet.
Second, my instructions include:
- Reading the text file and determine the age and based on the age, determine a discount. For instance, if the age is 18 -25, they receive a 5% discount.
- Match the pet preference with a vendor
I am utilizing classes for each of the attributes. I have a separate file for the class information which is indicated below. I have set/get methods for each attribute listed.
From information.py
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.__name = name
def set_name(self,name):
self.__name = name
def get_name(self):
return self.__name
At the end I have to write a letter for each individual. I can't figure out how to essentially keep the data separate in order to write the letter per individual. I will write each line using a .write(str) method. Here is my main program so far:
import information
import os
import time
CURRENT_YEAR = 2018
def main():
customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
tempFile = open('temp.txt','w')
with open('c.TXT') as customerList:
lines = customerList.readlines()
for line in lines:
tempFile.write( str(GetInfo.name) + 'n')
tempFile.close()
main()
For the vendor list I will have a dictionary such as:
vendors = {"Cat:CatCity"}
I am perplexed at my next step, while also receiving this error when trying to run the program:
"main.py", line 10, in main customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
NameError: name 'name' is not defined
python
5
set_name
andget_name
are totally useless function. Just use normal attribute access. Python != Java. Also, don't use double-underscore name-mangling unless you want double-underscore name-mangling. Which you dont
– juanpa.arrivillaga
Nov 12 '18 at 19:07
1
Anyway, your error is pretty straightforward,name
is not defined anywhere.
– juanpa.arrivillaga
Nov 12 '18 at 19:08
2
no,name = def get_name()
is a SyntaxError. Anyway, yourget_name
functions are not needed, and I don't see what you expect them to do in that case
– juanpa.arrivillaga
Nov 12 '18 at 19:11
2
@Torxed actually, that will not, since they used double-underscore name-mangling. But they shouldn't have. So, it should just be.name
all the way through
– juanpa.arrivillaga
Nov 12 '18 at 19:13
1
That question doesn't make any sense. You don't need those functions to assign to variables. Indeed, those functions don't do that at all, they assign to instance attributes. But you don't need them to do that either.
– juanpa.arrivillaga
Nov 12 '18 at 19:14
|
show 8 more comments
I am relatively new to Python (~5 months), so pardon my ignorance. I have been tasked with creating a program to do a few things.
First, I have a text file with information stored like the following:
name, street, city state zip, birthday, pet.
Second, my instructions include:
- Reading the text file and determine the age and based on the age, determine a discount. For instance, if the age is 18 -25, they receive a 5% discount.
- Match the pet preference with a vendor
I am utilizing classes for each of the attributes. I have a separate file for the class information which is indicated below. I have set/get methods for each attribute listed.
From information.py
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.__name = name
def set_name(self,name):
self.__name = name
def get_name(self):
return self.__name
At the end I have to write a letter for each individual. I can't figure out how to essentially keep the data separate in order to write the letter per individual. I will write each line using a .write(str) method. Here is my main program so far:
import information
import os
import time
CURRENT_YEAR = 2018
def main():
customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
tempFile = open('temp.txt','w')
with open('c.TXT') as customerList:
lines = customerList.readlines()
for line in lines:
tempFile.write( str(GetInfo.name) + 'n')
tempFile.close()
main()
For the vendor list I will have a dictionary such as:
vendors = {"Cat:CatCity"}
I am perplexed at my next step, while also receiving this error when trying to run the program:
"main.py", line 10, in main customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
NameError: name 'name' is not defined
python
I am relatively new to Python (~5 months), so pardon my ignorance. I have been tasked with creating a program to do a few things.
First, I have a text file with information stored like the following:
name, street, city state zip, birthday, pet.
Second, my instructions include:
- Reading the text file and determine the age and based on the age, determine a discount. For instance, if the age is 18 -25, they receive a 5% discount.
- Match the pet preference with a vendor
I am utilizing classes for each of the attributes. I have a separate file for the class information which is indicated below. I have set/get methods for each attribute listed.
From information.py
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.__name = name
def set_name(self,name):
self.__name = name
def get_name(self):
return self.__name
At the end I have to write a letter for each individual. I can't figure out how to essentially keep the data separate in order to write the letter per individual. I will write each line using a .write(str) method. Here is my main program so far:
import information
import os
import time
CURRENT_YEAR = 2018
def main():
customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
tempFile = open('temp.txt','w')
with open('c.TXT') as customerList:
lines = customerList.readlines()
for line in lines:
tempFile.write( str(GetInfo.name) + 'n')
tempFile.close()
main()
For the vendor list I will have a dictionary such as:
vendors = {"Cat:CatCity"}
I am perplexed at my next step, while also receiving this error when trying to run the program:
"main.py", line 10, in main customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
NameError: name 'name' is not defined
python
python
edited Nov 12 '18 at 19:07
Torxed
13.2k105486
13.2k105486
asked Nov 12 '18 at 19:05
iStatiK
1
1
5
set_name
andget_name
are totally useless function. Just use normal attribute access. Python != Java. Also, don't use double-underscore name-mangling unless you want double-underscore name-mangling. Which you dont
– juanpa.arrivillaga
Nov 12 '18 at 19:07
1
Anyway, your error is pretty straightforward,name
is not defined anywhere.
– juanpa.arrivillaga
Nov 12 '18 at 19:08
2
no,name = def get_name()
is a SyntaxError. Anyway, yourget_name
functions are not needed, and I don't see what you expect them to do in that case
– juanpa.arrivillaga
Nov 12 '18 at 19:11
2
@Torxed actually, that will not, since they used double-underscore name-mangling. But they shouldn't have. So, it should just be.name
all the way through
– juanpa.arrivillaga
Nov 12 '18 at 19:13
1
That question doesn't make any sense. You don't need those functions to assign to variables. Indeed, those functions don't do that at all, they assign to instance attributes. But you don't need them to do that either.
– juanpa.arrivillaga
Nov 12 '18 at 19:14
|
show 8 more comments
5
set_name
andget_name
are totally useless function. Just use normal attribute access. Python != Java. Also, don't use double-underscore name-mangling unless you want double-underscore name-mangling. Which you dont
– juanpa.arrivillaga
Nov 12 '18 at 19:07
1
Anyway, your error is pretty straightforward,name
is not defined anywhere.
– juanpa.arrivillaga
Nov 12 '18 at 19:08
2
no,name = def get_name()
is a SyntaxError. Anyway, yourget_name
functions are not needed, and I don't see what you expect them to do in that case
– juanpa.arrivillaga
Nov 12 '18 at 19:11
2
@Torxed actually, that will not, since they used double-underscore name-mangling. But they shouldn't have. So, it should just be.name
all the way through
– juanpa.arrivillaga
Nov 12 '18 at 19:13
1
That question doesn't make any sense. You don't need those functions to assign to variables. Indeed, those functions don't do that at all, they assign to instance attributes. But you don't need them to do that either.
– juanpa.arrivillaga
Nov 12 '18 at 19:14
5
5
set_name
and get_name
are totally useless function. Just use normal attribute access. Python != Java. Also, don't use double-underscore name-mangling unless you want double-underscore name-mangling. Which you dont– juanpa.arrivillaga
Nov 12 '18 at 19:07
set_name
and get_name
are totally useless function. Just use normal attribute access. Python != Java. Also, don't use double-underscore name-mangling unless you want double-underscore name-mangling. Which you dont– juanpa.arrivillaga
Nov 12 '18 at 19:07
1
1
Anyway, your error is pretty straightforward,
name
is not defined anywhere.– juanpa.arrivillaga
Nov 12 '18 at 19:08
Anyway, your error is pretty straightforward,
name
is not defined anywhere.– juanpa.arrivillaga
Nov 12 '18 at 19:08
2
2
no,
name = def get_name()
is a SyntaxError. Anyway, your get_name
functions are not needed, and I don't see what you expect them to do in that case– juanpa.arrivillaga
Nov 12 '18 at 19:11
no,
name = def get_name()
is a SyntaxError. Anyway, your get_name
functions are not needed, and I don't see what you expect them to do in that case– juanpa.arrivillaga
Nov 12 '18 at 19:11
2
2
@Torxed actually, that will not, since they used double-underscore name-mangling. But they shouldn't have. So, it should just be
.name
all the way through– juanpa.arrivillaga
Nov 12 '18 at 19:13
@Torxed actually, that will not, since they used double-underscore name-mangling. But they shouldn't have. So, it should just be
.name
all the way through– juanpa.arrivillaga
Nov 12 '18 at 19:13
1
1
That question doesn't make any sense. You don't need those functions to assign to variables. Indeed, those functions don't do that at all, they assign to instance attributes. But you don't need them to do that either.
– juanpa.arrivillaga
Nov 12 '18 at 19:14
That question doesn't make any sense. You don't need those functions to assign to variables. Indeed, those functions don't do that at all, they assign to instance attributes. But you don't need them to do that either.
– juanpa.arrivillaga
Nov 12 '18 at 19:14
|
show 8 more comments
1 Answer
1
active
oldest
votes
Try this class:
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.inner_name = name
@property
def get_name(self):
return self.inner_name
@name.setter
def set_name(self, name):
self.inner_name = name
You do not need to use the getter and the setter. In python you can access all attributes of an object without them. See also: Using @property versus getters and setters
The actual problem in your Code is the line
tempFile.write( str(GetInfo.name) + 'n')
The object GetInfo
is not an instance of the class, but the class itself. And the class does not have the attribute name
. What you probably meant was:
tempFile.write( str(customerInfo.name) + 'n')
1
remove the totally uselessproperty
methods
– juanpa.arrivillaga
Nov 12 '18 at 19:14
Also, the issue isn't an AttributeError, it's a NameError, because when they initialize theirGetInfo
instance, they pass a bunch of variables that aren't defined (name
being only the first one)
– juanpa.arrivillaga
Nov 12 '18 at 19:16
Thank you for your suggestion. I havent changed the class yet, but I did try changing it to customerInfo and received the same error. Do I need to assign name to something in the class file or main program?
– iStatiK
Nov 12 '18 at 19:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268543%2fassigning-set-get-methods-from-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this class:
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.inner_name = name
@property
def get_name(self):
return self.inner_name
@name.setter
def set_name(self, name):
self.inner_name = name
You do not need to use the getter and the setter. In python you can access all attributes of an object without them. See also: Using @property versus getters and setters
The actual problem in your Code is the line
tempFile.write( str(GetInfo.name) + 'n')
The object GetInfo
is not an instance of the class, but the class itself. And the class does not have the attribute name
. What you probably meant was:
tempFile.write( str(customerInfo.name) + 'n')
1
remove the totally uselessproperty
methods
– juanpa.arrivillaga
Nov 12 '18 at 19:14
Also, the issue isn't an AttributeError, it's a NameError, because when they initialize theirGetInfo
instance, they pass a bunch of variables that aren't defined (name
being only the first one)
– juanpa.arrivillaga
Nov 12 '18 at 19:16
Thank you for your suggestion. I havent changed the class yet, but I did try changing it to customerInfo and received the same error. Do I need to assign name to something in the class file or main program?
– iStatiK
Nov 12 '18 at 19:25
add a comment |
Try this class:
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.inner_name = name
@property
def get_name(self):
return self.inner_name
@name.setter
def set_name(self, name):
self.inner_name = name
You do not need to use the getter and the setter. In python you can access all attributes of an object without them. See also: Using @property versus getters and setters
The actual problem in your Code is the line
tempFile.write( str(GetInfo.name) + 'n')
The object GetInfo
is not an instance of the class, but the class itself. And the class does not have the attribute name
. What you probably meant was:
tempFile.write( str(customerInfo.name) + 'n')
1
remove the totally uselessproperty
methods
– juanpa.arrivillaga
Nov 12 '18 at 19:14
Also, the issue isn't an AttributeError, it's a NameError, because when they initialize theirGetInfo
instance, they pass a bunch of variables that aren't defined (name
being only the first one)
– juanpa.arrivillaga
Nov 12 '18 at 19:16
Thank you for your suggestion. I havent changed the class yet, but I did try changing it to customerInfo and received the same error. Do I need to assign name to something in the class file or main program?
– iStatiK
Nov 12 '18 at 19:25
add a comment |
Try this class:
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.inner_name = name
@property
def get_name(self):
return self.inner_name
@name.setter
def set_name(self, name):
self.inner_name = name
You do not need to use the getter and the setter. In python you can access all attributes of an object without them. See also: Using @property versus getters and setters
The actual problem in your Code is the line
tempFile.write( str(GetInfo.name) + 'n')
The object GetInfo
is not an instance of the class, but the class itself. And the class does not have the attribute name
. What you probably meant was:
tempFile.write( str(customerInfo.name) + 'n')
Try this class:
class GetInfo:
def __init__(self,name,street,city,state,zipcode,birthday,pet):
self.inner_name = name
@property
def get_name(self):
return self.inner_name
@name.setter
def set_name(self, name):
self.inner_name = name
You do not need to use the getter and the setter. In python you can access all attributes of an object without them. See also: Using @property versus getters and setters
The actual problem in your Code is the line
tempFile.write( str(GetInfo.name) + 'n')
The object GetInfo
is not an instance of the class, but the class itself. And the class does not have the attribute name
. What you probably meant was:
tempFile.write( str(customerInfo.name) + 'n')
answered Nov 12 '18 at 19:14
ostcar
863
863
1
remove the totally uselessproperty
methods
– juanpa.arrivillaga
Nov 12 '18 at 19:14
Also, the issue isn't an AttributeError, it's a NameError, because when they initialize theirGetInfo
instance, they pass a bunch of variables that aren't defined (name
being only the first one)
– juanpa.arrivillaga
Nov 12 '18 at 19:16
Thank you for your suggestion. I havent changed the class yet, but I did try changing it to customerInfo and received the same error. Do I need to assign name to something in the class file or main program?
– iStatiK
Nov 12 '18 at 19:25
add a comment |
1
remove the totally uselessproperty
methods
– juanpa.arrivillaga
Nov 12 '18 at 19:14
Also, the issue isn't an AttributeError, it's a NameError, because when they initialize theirGetInfo
instance, they pass a bunch of variables that aren't defined (name
being only the first one)
– juanpa.arrivillaga
Nov 12 '18 at 19:16
Thank you for your suggestion. I havent changed the class yet, but I did try changing it to customerInfo and received the same error. Do I need to assign name to something in the class file or main program?
– iStatiK
Nov 12 '18 at 19:25
1
1
remove the totally useless
property
methods– juanpa.arrivillaga
Nov 12 '18 at 19:14
remove the totally useless
property
methods– juanpa.arrivillaga
Nov 12 '18 at 19:14
Also, the issue isn't an AttributeError, it's a NameError, because when they initialize their
GetInfo
instance, they pass a bunch of variables that aren't defined (name
being only the first one)– juanpa.arrivillaga
Nov 12 '18 at 19:16
Also, the issue isn't an AttributeError, it's a NameError, because when they initialize their
GetInfo
instance, they pass a bunch of variables that aren't defined (name
being only the first one)– juanpa.arrivillaga
Nov 12 '18 at 19:16
Thank you for your suggestion. I havent changed the class yet, but I did try changing it to customerInfo and received the same error. Do I need to assign name to something in the class file or main program?
– iStatiK
Nov 12 '18 at 19:25
Thank you for your suggestion. I havent changed the class yet, but I did try changing it to customerInfo and received the same error. Do I need to assign name to something in the class file or main program?
– iStatiK
Nov 12 '18 at 19:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268543%2fassigning-set-get-methods-from-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
set_name
andget_name
are totally useless function. Just use normal attribute access. Python != Java. Also, don't use double-underscore name-mangling unless you want double-underscore name-mangling. Which you dont– juanpa.arrivillaga
Nov 12 '18 at 19:07
1
Anyway, your error is pretty straightforward,
name
is not defined anywhere.– juanpa.arrivillaga
Nov 12 '18 at 19:08
2
no,
name = def get_name()
is a SyntaxError. Anyway, yourget_name
functions are not needed, and I don't see what you expect them to do in that case– juanpa.arrivillaga
Nov 12 '18 at 19:11
2
@Torxed actually, that will not, since they used double-underscore name-mangling. But they shouldn't have. So, it should just be
.name
all the way through– juanpa.arrivillaga
Nov 12 '18 at 19:13
1
That question doesn't make any sense. You don't need those functions to assign to variables. Indeed, those functions don't do that at all, they assign to instance attributes. But you don't need them to do that either.
– juanpa.arrivillaga
Nov 12 '18 at 19:14