Re: [Tutor] variabel from raw input into re.search?
On Jan 7, 2011, at 7:47 AM, Tommy Kaas wrote: > I try to write a program, where the user can write a word or a name and I > will tell how many times the subject was mentioned in a text, I’m hosting. > I guess it’s possible but not this way it seems? > The re is only searching for the word “name” and not the variable name > I’m using Python 2.6.6. > TIA > Tommy > > import re > name = raw_input(’Who would you like to check’? ') > > mytxt = open("text.txt", "r") > antal = [] > for line in mytxt: > if re.search("(.*)name(.*)", line): > antal.append(line) > print name + ' was mentioned ' + str(len(antal)) + ' times in the text, > > WARNING: Just a beginner here, but... The problem looks to me to be this: "(.*)name(.*)" is a literal string, so Python never "sees" the variable to get the value. You can test this by copying your argument string into the shell and evaluating it. Since you want the value of the variable, not the name, try this: "(.*)"+name+"(.*)" There may be other issues, but that should get you the string you want. (Again, you test this expression before use to verify that it does what you want, just assign a string variable to 'name' before testing.) regards, Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Changing Python icon - 2.6 and 3.2
I haven't tried this on Windows 7 yet, but what I did on my Mac was to create shortcuts and rename those. I generally launch from shortcuts, so this leaves the app names alone but gives me the information I need to launch what I intend. You should be able to do something similar on Windows. regards, Bill On Feb 3, 2011, at 12:18 PM, Patty wrote: > Hello Folks - I have Python 2.6.6 on my Windows 7 system and installed Python > 3.2. Now I want to be able to differentiate between the versions and the > icon for each version of Python is the same. I figured I would change the > the four application files in the C:\Python26 directory - python; python26; > pythonw; pythonw26 - and give them new picture icons. But when I select any > of these files and choose 'properties', there is no option to change the > application icon. I looked online but the questions were not exactly the > same as this one and pertained to earlier versions of Python anyway. I > discovered this when I was trying to associate a folder with 2.6 based > programs so that it would always execute the programs with the python 2.6 > .exe. And both Pythons come up as a choice with no details indicating which > is which, except a lucky guess. > > It does appear that I can change the actual name of the application (the > field with the name allows me to edit) but that is not what I wanted to do, > also unsure if that would cause a problem in other parts of the application > down the road if I was changing names like that. > > Thanks for suggestions - > Patty > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best practice: throw exception or set a flag?
On Feb 4, 2011, at 8:03 AM, Alex Hall wrote: > On 2/4/11, Alan Gauld wrote: >> "Alex Hall" wrote >> >>> I am wondering what the best way to do the following would be: throw >>> an exception, or always return an object but set an error flag if >>> something goes wrong? Here is an example: >> >> Throw an exception is the short general case answer... >> >>> class c: >>> def __init__(self): >>> self.error=False >>> def func(self, val): >>> if val!=10: self.error=True >> >> But note that neither of these methods returns "an object" >> - unless you count None as an object of course. > There should have been a function below that which returned the object > with or without the error attribute. >> >>> Which is the "standard" way when dealing with objects? Throw >>> exceptions or always return an object, even if said object has an >>> error and may therefore not have data beyond an error code and >>> message? >> >> I'm not sure what you have in mind here but remember that >> init() is an initialiser and not really a constructor so the object >> already exists when init() is called. init() does not return the >> object. > Right, that second function should have done it, but I likely wrote it > wrong (what I get for making up an example on the spot). >> >> And most methods do not return the object of which they >> are a part (in Python at least, in SmallTalk they do). > That seems like it would get rather confusing... Um, not quite correct -- methods *without a specified return value* always return self, that is, the object which executed the method. This is useful, but is hardly the most common situation. It is used most often when a method modifies the receiver and the sender(s) is/are likely to be interested in the updated object itself rather than in one of the parameters to the method or some other object. However, it can be a source of error, as many methods which modify return self, but many common collection protocols return the argument rather than the modified collection. Just one of those things you need to learn, which all languages have ;-) cheers, Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best practice: throw exception or set a flag?
Um, yes, emphatically yes. You missed the context, which was Smalltalk, and it is terms of Smalltalk that my reply is couched. Yes, Python returns None. Smalltalk returns self by default. Given the dropped context, you are correct. Given the context meant, I am. regards, Bill On Feb 4, 2011, at 9:28 AM, Emile van Sebille wrote: > On 2/4/2011 5:35 AM Bill Felton said... > >> Um, not quite correct -- methods *without a specified return value* always >> return self, that is, the object which executed the method. > > Um, no. They return None. > > >>> class Test: > ... def __init__(self):pass > ... def test(self):pass > ... > >>> a=Test().test() > >>> a > >>> print a > None > >>> > > Emile > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best practice: throw exception or set a flag?
Smalltalk is an OO programming language that had some dominance in the late 80's through much of the 90s. It has proceeded to more or less commit suicide through a variety of mechanisms. By 'drop' I meant 'drop the context of the remarks', which in the original post was discussing Smalltalk's default method return. It isn't a major deal, overall, I think the most interesting remark has been Alan Gauld's musings immediately prior to Emile's to which I responded. Python defaults to None. Smalltalk defaults to self, which is always the object executing the code involved. Alan points out that this is extremely convenient and can reduce the code bulk involved when one wishes to string multiple operations together. It is stylistically possible in Python to set methods to return 'self' if/when that would be useful, but if it is not a language level feature, it becomes a convention that one can only rely on in ones own code. regards, Bill On Feb 4, 2011, at 11:56 AM, Alex Hall wrote: > On 2/4/11, Bill Felton wrote: >> Um, yes, emphatically yes. You missed the context, which was Smalltalk, and >> it is terms of Smalltalk that my reply is couched. >> Yes, Python returns None. >> Smalltalk returns self by default. >> Given the dropped context, you are correct. >> Given the context meant, I am. > I was following this until now... what is this smalltalk and drop? >> >> regards, >> Bill >> >> On Feb 4, 2011, at 9:28 AM, Emile van Sebille wrote: >> >>> On 2/4/2011 5:35 AM Bill Felton said... >>> >>>> Um, not quite correct -- methods *without a specified return value* >>>> always return self, that is, the object which executed the method. >>> >>> Um, no. They return None. >>> >>>>>> class Test: >>> ... def __init__(self):pass >>> ... def test(self):pass >>> ... >>>>>> a=Test().test() >>>>>> a >>>>>> print a >>> None >>>>>> >>> >>> Emile >>> >>> ___ >>> Tutor maillist - Tutor@python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > -- > Have a great day, > Alex (msg sent from GMail website) > mehg...@gmail.com; http://www.facebook.com/mehgcap ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor