Re: [Tutor] Handling Objects
To do: $color = "Green"; print "$color apple.\n"; You would want: color = "Green" print "%s apple." % color Both of which would print "Green apple." That is actually something I like about Perl, but it reinforces some bad habits, at least for me. Boilerplate coding, as it were, when doing quick and dirty web stuff, which always ends up growing! That and inline regex will probably be the hardest thing to get used to. Good luck with Python, but you really won't need it. I wrote a script in Perl today to show a friend how to do something, and it reinforced the amazing amount of -stuff- you need to remember to use Perl, and the mind-draining way you can't type without thinking about which way you will type it. I swear it scares people away from other languages for life :) Python will be much, much easier to learn. And hopefully you'll spend more time typing what you want, than deciding how you should type it, while doing the exact same things you always did.On 10/5/05, Eric Walker <[EMAIL PROTECTED]> wrote: Thanks Kent.. I think that would work for what I am doing. So is it safe tosay that python doesn't do variable interpolation like perl..? Thanks in advance.Python Newbie... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling Objects
Thanks Kent.. I think that would work for what I am doing. So is it safe to say that python doesn't do variable interpolation like perl..? Thanks in advance. Python Newbie... On Wednesday 05 October 2005 05:20 pm, Kent Johnson wrote: > Eric Walker wrote: > > Well, > > I think I probably can do this easier in perl but I took a vow I would > > try and learn python. I know I am using classes here and really don't > > need objects. This is just another way for me to learn how to work with > > classes within python. My object actually will be storing like 5 or 6 > > different attributes but I didn't include them in the example. These > > attributes will be certain things that are read from the file. Once I > > get the objects i want to create in another directory the same files with > > the same names but put different data into the new files depending on > > what I read from the original files. > > OK, I would just make a list of the objects, since one of the attributes is > the name you have everything you need in the object. > > def getNames(): > import os > currentDir=os.getcwd() > temp=currentDir + '/TEMP' > os.chdir(temp) > baseList=os.listdir(".") > data = [] > for name in baseList: > data.append(TPROJ(name)) > print name > return data > > then to use the data something like > for datum in data: > f = open(datum.name, 'w') > #etc > > Kent > > > Python Newbie > > > > On Wednesday 05 October 2005 04:29 pm, Kent Johnson wrote: > >>Eric Walker wrote: > >>>New to Python and trying to do some class stuff with a simple task. > >>>Problem: > >>>1) get a list of file names in a directory > >>>2) create variables with the same name of each filename pulled from the > >>>directory. > >>>3) Create an object for each and pass into the __init__ method the > >>>stringname of the file name. > >>> > >>>This way I get a collection of objects that are the same name as the > >>> file name and within each instance of the class , a particular > >>> attribute will have the string name of the object. Hope this isn't too > >>> confusing.. example. > >> > >>What will you do with the names and objects once you have them? A better > >>approach is probably to keep a dictionary that maps names to objects. If > >>your object is really just storing the name you might as well just keep a > >>list of names - the object isn't adding any value. If the object is going > >>to have more behaviour then use a dict. If you really just want to print > >>the names then you don't need to store them at all. For example with a > >>dict: > >> > >>class TPROJ: > >># as before > >> > >>def getNames(): > >>import os > >>currentDir=os.getcwd() > >>temp=currentDir + '/TEMP' > >>os.chdir(temp) > >>baseList=os.listdir(".") > >>nameDict = {} > >>for name in baseList: > >>nameDict[name] = TPROJ(name) > >>print name > >>return nameDict > >> > >>HTH, > >>Kent > >> > >>>class TPROJ: > >>>def __init__(self,value):#createMethod auto executes since it has __ > >>>self.BASENAME = value > >>> > >>> def display(self):#display method > >>>print self.BASENAME > >>> > >>>def getNames(): > >>>import os > >>>currentDir=os.getcwd() > >>>temp=currentDir + '/TEMP' > >>>os.chdir(temp) > >>>baseList=os.listdir(".") > >>>for name in baseList: > >>>name = TPROJ(name) > >>>print name > >>> > >>>Can anyone see what I am trying to do? > >>> > >>>Python Newbie... > >>>___ > >>>Tutor maillist - Tutor@python.org > >>>http://mail.python.org/mailman/listinfo/tutor > >> > >>___ > >>Tutor maillist - Tutor@python.org > >>http://mail.python.org/mailman/listinfo/tutor > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Eric Walker EDA/CAD Engineer Work: 208-368-2573 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling Objects
Eric Walker wrote: > Thanks Kent.. I think that would work for what I am doing. So is it safe to > say that python doesn't do variable interpolation like perl..? I think so, though I don't really speak perl. The closest approximation is the string formatting operation and the string.Template class. http://docs.python.org/lib/typesseq-strings.html http://docs.python.org/lib/node109.html Kent > > Thanks in advance. > > Python Newbie... > > > On Wednesday 05 October 2005 05:20 pm, Kent Johnson wrote: > >>Eric Walker wrote: >> >>>Well, >>>I think I probably can do this easier in perl but I took a vow I would >>>try and learn python. I know I am using classes here and really don't >>>need objects. This is just another way for me to learn how to work with >>>classes within python. My object actually will be storing like 5 or 6 >>>different attributes but I didn't include them in the example. These >>>attributes will be certain things that are read from the file. Once I >>>get the objects i want to create in another directory the same files with >>>the same names but put different data into the new files depending on >>>what I read from the original files. >> >>OK, I would just make a list of the objects, since one of the attributes is >>the name you have everything you need in the object. >> >>def getNames(): >>import os >>currentDir=os.getcwd() >>temp=currentDir + '/TEMP' >>os.chdir(temp) >>baseList=os.listdir(".") >>data = [] >>for name in baseList: >>data.append(TPROJ(name)) >>print name >>return data >> >>then to use the data something like >>for datum in data: >>f = open(datum.name, 'w') >>#etc >> >>Kent >> >> >>>Python Newbie >>> >>>On Wednesday 05 October 2005 04:29 pm, Kent Johnson wrote: >>> Eric Walker wrote: >New to Python and trying to do some class stuff with a simple task. >Problem: >1) get a list of file names in a directory >2) create variables with the same name of each filename pulled from the >directory. >3) Create an object for each and pass into the __init__ method the >stringname of the file name. > >This way I get a collection of objects that are the same name as the >file name and within each instance of the class , a particular >attribute will have the string name of the object. Hope this isn't too >confusing.. example. What will you do with the names and objects once you have them? A better approach is probably to keep a dictionary that maps names to objects. If your object is really just storing the name you might as well just keep a list of names - the object isn't adding any value. If the object is going to have more behaviour then use a dict. If you really just want to print the names then you don't need to store them at all. For example with a dict: class TPROJ: # as before def getNames(): import os currentDir=os.getcwd() temp=currentDir + '/TEMP' os.chdir(temp) baseList=os.listdir(".") nameDict = {} for name in baseList: nameDict[name] = TPROJ(name) print name return nameDict HTH, Kent >class TPROJ: > def __init__(self,value):#createMethod auto executes since it has __ > self.BASENAME = value > > def display(self):#display method > print self.BASENAME > >def getNames(): > import os > currentDir=os.getcwd() > temp=currentDir + '/TEMP' > os.chdir(temp) > baseList=os.listdir(".") > for name in baseList: > name = TPROJ(name) > print name > >Can anyone see what I am trying to do? > >Python Newbie... >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor >> >>___ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling Objects
Eric Walker wrote: > Well, > I think I probably can do this easier in perl but I took a vow I would try > and > learn python. I know I am using classes here and really don't need objects. > This is just another way for me to learn how to work with classes within > python. My object actually will be storing like 5 or 6 different attributes > but I didn't include them in the example. These attributes will be certain > things that are read from the file. Once I get the objects i want to create > in another directory the same files with the same names but put different > data into the new files depending on what I read from the original files. OK, I would just make a list of the objects, since one of the attributes is the name you have everything you need in the object. def getNames(): import os currentDir=os.getcwd() temp=currentDir + '/TEMP' os.chdir(temp) baseList=os.listdir(".") data = [] for name in baseList: data.append(TPROJ(name)) print name return data then to use the data something like for datum in data: f = open(datum.name, 'w') #etc Kent > > Python Newbie > > > On Wednesday 05 October 2005 04:29 pm, Kent Johnson wrote: > >>Eric Walker wrote: >> >>>New to Python and trying to do some class stuff with a simple task. >>>Problem: >>>1) get a list of file names in a directory >>>2) create variables with the same name of each filename pulled from the >>>directory. >>>3) Create an object for each and pass into the __init__ method the >>>stringname of the file name. >>> >>>This way I get a collection of objects that are the same name as the file >>>name and within each instance of the class , a particular attribute will >>>have the string name of the object. Hope this isn't too confusing.. >>>example. >> >>What will you do with the names and objects once you have them? A better >>approach is probably to keep a dictionary that maps names to objects. If >>your object is really just storing the name you might as well just keep a >>list of names - the object isn't adding any value. If the object is going >>to have more behaviour then use a dict. If you really just want to print >>the names then you don't need to store them at all. For example with a >>dict: >> >>class TPROJ: >># as before >> >>def getNames(): >>import os >>currentDir=os.getcwd() >>temp=currentDir + '/TEMP' >>os.chdir(temp) >>baseList=os.listdir(".") >>nameDict = {} >>for name in baseList: >>nameDict[name] = TPROJ(name) >>print name >>return nameDict >> >>HTH, >>Kent >> >> >>>class TPROJ: >>>def __init__(self,value):#createMethod auto executes since it has __ >>>self.BASENAME = value >>> >>> def display(self):#display method >>>print self.BASENAME >>> >>>def getNames(): >>>import os >>>currentDir=os.getcwd() >>>temp=currentDir + '/TEMP' >>>os.chdir(temp) >>>baseList=os.listdir(".") >>>for name in baseList: >>>name = TPROJ(name) >>>print name >>> >>>Can anyone see what I am trying to do? >>> >>>Python Newbie... >>>___ >>>Tutor maillist - Tutor@python.org >>>http://mail.python.org/mailman/listinfo/tutor >> >>___ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling Objects
Well, I think I probably can do this easier in perl but I took a vow I would try and learn python. I know I am using classes here and really don't need objects. This is just another way for me to learn how to work with classes within python. My object actually will be storing like 5 or 6 different attributes but I didn't include them in the example. These attributes will be certain things that are read from the file. Once I get the objects i want to create in another directory the same files with the same names but put different data into the new files depending on what I read from the original files. Python Newbie On Wednesday 05 October 2005 04:29 pm, Kent Johnson wrote: > Eric Walker wrote: > > New to Python and trying to do some class stuff with a simple task. > > Problem: > > 1) get a list of file names in a directory > > 2) create variables with the same name of each filename pulled from the > > directory. > > 3) Create an object for each and pass into the __init__ method the > > stringname of the file name. > > > > This way I get a collection of objects that are the same name as the file > > name and within each instance of the class , a particular attribute will > > have the string name of the object. Hope this isn't too confusing.. > > example. > > What will you do with the names and objects once you have them? A better > approach is probably to keep a dictionary that maps names to objects. If > your object is really just storing the name you might as well just keep a > list of names - the object isn't adding any value. If the object is going > to have more behaviour then use a dict. If you really just want to print > the names then you don't need to store them at all. For example with a > dict: > > class TPROJ: > # as before > > def getNames(): > import os > currentDir=os.getcwd() > temp=currentDir + '/TEMP' > os.chdir(temp) > baseList=os.listdir(".") > nameDict = {} > for name in baseList: > nameDict[name] = TPROJ(name) > print name > return nameDict > > HTH, > Kent > > > class TPROJ: > > def __init__(self,value):#createMethod auto executes since it has __ > > self.BASENAME = value > > > >def display(self):#display method > > print self.BASENAME > > > > def getNames(): > > import os > > currentDir=os.getcwd() > > temp=currentDir + '/TEMP' > > os.chdir(temp) > > baseList=os.listdir(".") > > for name in baseList: > > name = TPROJ(name) > > print name > > > > Can anyone see what I am trying to do? > > > > Python Newbie... > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Eric Walker EDA/CAD Engineer Work: 208-368-2573 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling Objects
Eric Walker wrote: > New to Python and trying to do some class stuff with a simple task. > Problem: > 1) get a list of file names in a directory > 2) create variables with the same name of each filename pulled from the > directory. > 3) Create an object for each and pass into the __init__ method the stringname > of the file name. > > This way I get a collection of objects that are the same name as the file > name > and within each instance of the class , a particular attribute will have the > string name of the object. Hope this isn't too confusing.. example. What will you do with the names and objects once you have them? A better approach is probably to keep a dictionary that maps names to objects. If your object is really just storing the name you might as well just keep a list of names - the object isn't adding any value. If the object is going to have more behaviour then use a dict. If you really just want to print the names then you don't need to store them at all. For example with a dict: class TPROJ: # as before def getNames(): import os currentDir=os.getcwd() temp=currentDir + '/TEMP' os.chdir(temp) baseList=os.listdir(".") nameDict = {} for name in baseList: nameDict[name] = TPROJ(name) print name return nameDict HTH, Kent > > > class TPROJ: > def __init__(self,value):#createMethod auto executes since it has __ > self.BASENAME = value > >def display(self):#display method > print self.BASENAME > > def getNames(): > import os > currentDir=os.getcwd() > temp=currentDir + '/TEMP' > os.chdir(temp) > baseList=os.listdir(".") > for name in baseList: > name = TPROJ(name) > print name > > Can anyone see what I am trying to do? > > Python Newbie... > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Handling Objects
New to Python and trying to do some class stuff with a simple task. Problem: 1) get a list of file names in a directory 2) create variables with the same name of each filename pulled from the directory. 3) Create an object for each and pass into the __init__ method the stringname of the file name. This way I get a collection of objects that are the same name as the file name and within each instance of the class , a particular attribute will have the string name of the object. Hope this isn't too confusing.. example. class TPROJ: def __init__(self,value):#createMethod auto executes since it has __ self.BASENAME = value def display(self):#display method print self.BASENAME def getNames(): import os currentDir=os.getcwd() temp=currentDir + '/TEMP' os.chdir(temp) baseList=os.listdir(".") for name in baseList: name = TPROJ(name) print name Can anyone see what I am trying to do? Python Newbie... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor