[Tutor] Multiple inheritance for mixin attributes
Hello everyone, I've got a question about class design. I want to model classes like these (examples): # class BaseItem(object): def __init__(self, ident, name, description): self.ident = ident self.name = name self.description = description class DataSourceItem(object): def __init__(self, ident, name, description, data_source): self.ident = ident self.name = name self.description = description self.data_source = data_source class BaseItemCollection(list): def __init__(self, ident, name, description): self.ident = ident self.name = name self.description = description def default_version(self): return self[-1] class BaseDataSourceItemCollection(list): def __init__(self, ident, name, description, data_source): self.ident = ident self.name = name self.description = description self.data_source = data_source def default_version(self): return self[-1] ### Now, to remove all the duplicated code I could use inheritance. But that would lead to multiple inheritance and the question how to initialise both superclasses? I would appreciate some advice about how to model classes like this. Personaly, I don't have to use inheritance, but I have no better idea. Would you accept at least some duplication to avoid multiple inheritance? Thanks in advance, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiple inheritance for mixin attributes
"Knacktus" wrote I've got a question about class design. I want to model classes like these (examples): Since these only really have data and inheritance should be based on behaviour its almost impossible to make a meaningful decision on what the inheritance tree should look like. However... # class BaseItem(object): def __init__(self, ident, name, description): self.ident = ident self.name = name self.description = description class DataSourceItem(object): def __init__(self, ident, name, description, data_source): self.ident = ident self.name = name self.description = description self.data_source = data_source Surely the DataSourceItem could inherit BaseItem? They are both types of Item... class DataSourceItem(BaseItem): def __init__(self,ident, name, description, data_source): BaseItem.__init__(self,ident, name, description) # or use super()... self.data_source = data_source class BaseItemCollection(list): def __init__(self, ident, name, description): self.ident = ident self.name = name self.description = description def default_version(self): return self[-1] class BaseDataSourceItemCollection(list): And the same here - Make the DataSourceItemCollection inherit from ItemCollection since they are both ItemCollections... Now, to remove all the duplicated code I could use inheritance. But that would lead to multiple inheritance and the question how to initialise both superclasses? Why would it lead to multiple inheritance? There seem to be two different types of things - Items and collections. Inheritance is not a code saving tool but an Is-A relationship that enables polymorphism. Do not use inherirtance just to save code - its nearly always a bad idea. Although lots of duplicated code - especially methods - often suggest that an Is-A relationship exists... I would appreciate some advice about how to model classes like this. Personaly, I don't have to use inheritance, but I have no better idea. Would you accept at least some duplication to avoid multiple inheritance? Duplication is irerelevant. But I would not do anything to "avoid multiple inheritance" if MI was appropriate. MI is a very effective and powerful tool. It is not something to be avoided just in principle. But you very rarely need it in Python in practice. Few things are genuinely of two such different types that dynamic duck-typing can't deal with it without MI. But don't use inheritance just to avoid duplicating code, use delegation for that. FWIW I've worked on projects using MI with a single class inheriting 7 parent classes, if done properly and deliberately MI is nothing to worry about. The problems only occur when MI is abused and not designed IMHO -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using Django with TortoiseSVN (overwriting files)
Hi everyone, We have a project at work where myself and a co-developer are setting up Django. Our version control software of choice is TortoiseSVN. When doing a project update from SVN, it pulls the server version to the local project. When checking code in, it overwrites the server version of the code as expected. I wish to find out whether Django would work happily this way, as it requires one to use the django-admin.py tool to create projects and apps. Would overwriting these files/ directories with server versions compromise their integrity in a Django setup? -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiple inheritance for mixin attributes
Thanks a lot, your explanations are very helpful getting a better (right) understanding about the ideas behind inheritance. Cheers, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Schema change in ElementTree
# so that ET preserves your namespaces ET._namespace_map["http://www.w3.org/2001/XMLSchema";] = 'xs' # open your file explicitly specifying the correct mode f = open('testing.xml', 'w') xml_tree.write(f, encoding='UTF-8') f.close() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Django with TortoiseSVN (overwriting files)
> We have a project at work where myself and a co-developer are setting up > Django. Our version control software of choice is TortoiseSVN. > > When doing a project update from SVN, it pulls the server version to the > local project. When checking code in, it overwrites the server version of the > code as expected. > > I wish to find out whether Django would work happily this way, as it requires > one to use the django-admin.py tool to create projects and apps. Would > overwriting these files/ directories with server versions compromise their > integrity in a Django setup? That's not a Python question, more a Django question or a system-admin question. You'd be better off using the correct mailing list for that. That said, you should be fine, providing you re-sync the databases every time you 'svn up', to keep your model schema's in the database in sync (you can use fixtures to sync data in the database). Also the settings files may differ on the server or the local machines; it's best to keep those out of SVN, or have a little if-else clause in settings.py that takes care server-dependent settings. Otherwise, all your code can (and should) be machine/server agnostic, and thus easily transportable. Evert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Schema change in ElementTree
justin.mailingli...@gmail.com, 16.08.2010 10:38: # so that ET preserves your namespaces ET._namespace_map["http://www.w3.org/2001/XMLSchema";] = 'xs' # open your file explicitly specifying the correct mode f = open('testing.xml', 'w') xml_tree.write(f, encoding='UTF-8') f.close() Note that it's best to hit "reply" when responding to other people's postings. That way, mail readers can see the relation between the original posting and the reply and sort the reply into the right thread. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Django with TortoiseSVN (overwriting files)
Hi Evert, You are right, I actually intended the question for the Django list - it was a posting error. Thanks for the answer, we did soon figure out to exclude the settings file from any SVN checkins/ checkouts. We will simply apply this rule as it should be simple enough to observe and to make any corrections if a team member overwrites it. Many thanks, Lloyd On Mon, Aug 16, 2010 at 10:54 AM, Evert Rol wrote: > > We have a project at work where myself and a co-developer are setting up > Django. Our version control software of choice is TortoiseSVN. > > > > When doing a project update from SVN, it pulls the server version to the > local project. When checking code in, it overwrites the server version of > the code as expected. > > > > I wish to find out whether Django would work happily this way, as it > requires one to use the django-admin.py tool to create projects and apps. > Would overwriting these files/ directories with server versions compromise > their integrity in a Django setup? > > That's not a Python question, more a Django question or a system-admin > question. You'd be better off using the correct mailing list for that. > > That said, you should be fine, providing you re-sync the databases every > time you 'svn up', to keep your model schema's in the database in sync (you > can use fixtures to sync data in the database). Also the settings files may > differ on the server or the local machines; it's best to keep those out of > SVN, or have a little if-else clause in settings.py that takes care > server-dependent settings. > Otherwise, all your code can (and should) be machine/server agnostic, and > thus easily transportable. > > Evert > > > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiple inheritance for mixin attributes
On Mon, 16 Aug 2010 05:01:58 pm Knacktus wrote: > Hello everyone, > > I've got a question about class design. I want to model classes like > these (examples): > > # > class BaseItem(object): > def __init__(self, ident, name, description): > self.ident = ident > self.name = name > self.description = description > > class DataSourceItem(object): > def __init__(self, ident, name, description, data_source): > self.ident = ident > self.name = name > self.description = description > self.data_source = data_source That's a clear job for single inheritance: class DataSourceItem(BaseItem): def __init__(self, ident, name, description, data_source): BaseItem.__init__(self, ident, name, description) self.data_source = data_source Note: there's a BIG question mark as to whether you should use super() or not when inheriting methods. I've previously defended the use of super() even in single-inheritance classes, where it is not strictly necessary but does no harm. However in this case, the function signature of DataSourceItem and BaseItem are different, which is a clear signal *not* to use super(). But if you're brave, here's a version that uses super(): class DataSourceItem(BaseItem): def __init__(self, ident, name, description, data_source): super(DataSourceItem, self).__init__(ident, name, description) self.data_source = data_source Is this safe? Well, probably, for the simple class hierarchy you enter. But if you extend the class hierarchy with more sibling and child classes, perhaps not. > class BaseItemCollection(list): > def __init__(self, ident, name, description): > self.ident = ident > self.name = name > self.description = description > def default_version(self): > return self[-1] That's a clear job for composition: class BaseItemCollection(list): def __init__(self, ident, name, description): self.data = BaseItem(ident, name, description) def default_version(self): return self[-1] > class BaseDataSourceItemCollection(list): > def __init__(self, ident, name, description, data_source): > self.ident = ident > self.name = name > self.description = description > self.data_source = data_source > def default_version(self): > return self[-1] This one isn't clear. You could inherit either directly from list: class BaseDataSourceItemCollection(list): def __init__(self, ident, name, description, data_source): self.data = DataSourceItem( ident, name, description, data_source) def default_version(self): return self[-1] or from BaseItemCollection: class BaseDataSourceItemCollection(BaseItemCollection): def __init__(self, ident, name, description, data_source): BaseItemCollection.__init__(self, ident, name, description) self.data_source = data_source Which such trivially small classes, there's no real advantage or disadvantage to either. Of course, you could use multiple inheritance, but that opens a whole rat's nest of difficulties. Even though Python makes it easy to implement multiple-inheritance, it is full of pitfalls for the unwary, and even the wary, so you should always consider whether you *really* need it. Other alternatives are traits and generic functions: http://www.artima.com/weblogs/viewpost.jsp?thread=246488 http://www.artima.com/weblogs/viewpost.jsp?thread=237764 Lastly, I should mention automatic delegation as an alternative to inheritance. It's not clear to me that this would be either useful or necessary for your classes, but I'll mention it so you can google for more information if you are interested. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] my Python learning exercise
On Sat, Aug 14, 2010 at 6:17 PM, Bill Allen wrote: > In the process of learning Python, I have given myself a programming > exercise to do. This is a standard thing I do whenever learning a new > programming language and I have used this same exercise several times before > with other programming languages. It is a simple text based game that > simply lets a user explore a maze of rooms. Anyone who finds this interesting may also want to look into http://inventwithpython.com/ , I hear it's a good book but I haven't personally worked through it. -Luke ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Moving from Python 2.5.4 to 2.5.2?
My partner got ahead of the game last year, and installed 2.5.4, which confounds matters when the other four participants when sharing some python programs under Win XP. My guess is that if he uses control panel add/remove for py 2.5.4, he can then successfully install 2.5.2 w/o messing up any programs I've sent him. I'm presuming he did not put them under c:\Python. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "An experiment is a question which science poses to Nature, and a measurement is the recording of Nature’s answer." -- Max Planck Web Page: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Moving from Python 2.5.4 to 2.5.2?
On Mon, Aug 16, 2010 at 10:39 AM, Wayne Watson wrote: > My partner got ahead of the game last year, and installed 2.5.4, which > confounds matters when the other four participants when sharing some python > programs under Win XP. My guess is that if he uses control panel add/remove > for py 2.5.4, he can then successfully install 2.5.2 w/o messing up any > programs I've sent him. I'm presuming he did not put them under c:\Python. Is this a question or a statement? Not really sure what you're looking to get out of this. Why would 2.5.4 cause issues with 2.5.2? Why don't you all just upgrade to 2.5.4? -Luke ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question about Dictionaries
Hi All, I know that I can look up the value for a particular key in a dictionary, but can I look up the key associated with a particular value? I understand that this could be problematic from the standpoint of multiple keys having the same value, but even then I feel like Python could just return a list of keys with that value. Thanks! Guillaume Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates Direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Dictionaries
On 8/16/2010 10:44 AM Chorn, Guillaume said... Hi All, I know that I can look up the value for a particular key in a dictionary, but can I look up the key associated with a particular value? Yes. But you'll need to implement it. There are likely modules out there that'll do this, but it'd take more time to look up and evaluate than to simply write and implement exactly what you need. I understand that this could be problematic from the standpoint of multiple keys having the same value, but even then I feel like Python could just return a list of keys with that value. So, in untested code you'd have a function something like: result = [] for ky, val in dict.items(): if val == targetval: result.append(ky) return result If you need repeated access such that iterating over a large dict frequently impacts performance, you could subclass dict and maintain a second index allowing instant access to the keys associated with a specific value. HTH, Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Moving from Python 2.5.4 to 2.5.2?
The question is would going back likely cause problems? I'm dealing with neophytes. He's messed up before. On 8/16/2010 8:58 AM, Luke Paireepinart wrote: On Mon, Aug 16, 2010 at 10:39 AM, Wayne Watson wrote: My partner got ahead of the game last year, and installed 2.5.4, which confounds matters when the other four participants when sharing some python programs under Win XP. My guess is that if he uses control panel add/remove for py 2.5.4, he can then successfully install 2.5.2 w/o messing up any programs I've sent him. I'm presuming he did not put them under c:\Python. Is this a question or a statement? Not really sure what you're looking to get out of this. Why would 2.5.4 cause issues with 2.5.2? Why don't you all just upgrade to 2.5.4? -Luke -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "An experiment is a question which science poses to Nature, and a measurement is the recording of Nature’s answer." -- Max Planck Web Page: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Dictionaries
> I know that I can look up the value for a particular key in a > dictionary, but can I look up the key associated with a particular > value? I am using bidict in one of my projects: http://pypi.python.org/pypi/bidict/0.1.1 It's probably a bit more complex than what I need, but the parts I am using are working fine, and the design of the extra features is kind of interesting. I think something like this should be in the stdlib, but there is some controversy about how exactly it should work. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Moving from Python 2.5.4 to 2.5.2?
The core issue here is: are there actually issues exacerbated by the difference in Python versions? If so, which issues? There shouldn't be hardly any reason to force you all to maintain the exact same python version, especially if you're in the same sub-version (2.5) On Mon, Aug 16, 2010 at 3:57 PM, Wayne Watson wrote: > The question is would going back likely cause problems? I'm dealing with > neophytes. He's messed up before. > > On 8/16/2010 8:58 AM, Luke Paireepinart wrote: >> >> On Mon, Aug 16, 2010 at 10:39 AM, Wayne Watson >> wrote: >> >>> >>> My partner got ahead of the game last year, and installed 2.5.4, which >>> confounds matters when the other four participants when sharing some >>> python >>> programs under Win XP. My guess is that if he uses control panel >>> add/remove >>> for py 2.5.4, he can then successfully install 2.5.2 w/o messing up any >>> programs I've sent him. I'm presuming he did not put them under >>> c:\Python. >>> >> >> Is this a question or a statement? Not really sure what you're >> looking to get out of this. >> >> Why would 2.5.4 cause issues with 2.5.2? Why don't you all just >> upgrade to 2.5.4? >> >> -Luke >> >> > > -- > Wayne Watson (Watson Adventures, Prop., Nevada City, CA) > > (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) > Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet > > "An experiment is a question which science poses to > Nature, and a measurement is the recording of > Nature’s answer." -- Max Planck > > > Web Page: > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lost in the control flow
On Fri, Aug 13, 2010 at 2:38 PM, Adam Bark wrote: > On 11/08/10 18:14, Eduardo Vieira wrote: > > On Tue, Aug 10, 2010 at 1:56 PM, Adam Bark wrote: > > > > The problem is you don't call make_dict unless there's a "FUEL SURCHARGE" or > multiple pins. Also you don't add the first pin to mydict["tracking"] unless > there's a "FUEL SURCHARGE". > > HTH, > Adam. > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > Hi, I made these changes, got rid of the functions, and changed some logic: > here is the link: http://pastebin.com/F19vKUjr > > Now mydict will be changing, of course with every loop, as the output below: > {'company': 'CITY SIGNS', 'tracking': ['600775301143'], 'id': '1'} > {'city': 'MEDICINE HAT', 'company': 'CITY SIGNS', 'tracking': > ['600775301143'], 'id': '1', 'prov': 'AB'} > {'city': 'MEDICINE HAT', 'company': 'TRIMLINE', 'tracking': > ['600775301150'], 'id': '2', 'prov': 'AB'} > {'city': 'ROCKY MOUNTAIN HOUSE', 'company': 'TRIMLINE', 'tracking': > ['600775301150'], 'id': '2', 'prov': 'AB'} > {'city': 'ROCKY MOUNTAIN HOUSE', 'company': 'TS SIGNS PRINTING & > PROMO', 'tracking': ['600775301168'], 'id': '3', 'prov': 'AB'} > {'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING & PROMO', > 'tracking': ['600775301168'], 'id': '3', 'prov': 'AB'} > {'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING & PROMO', > 'tracking': ['600775301168', '600775301168'], 'id': '3', 'prov': 'AB'} > {'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING & PROMO', > 'tracking': ['600775301168', '600775301168', '600775301176'], 'id': > '3', 'prov': 'AB'} > {'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING & PROMO', > 'tracking': ['600775301168', '600775301168', '600775301176', > '600775301184'], 'id': '3', 'prov': 'AB'} > {'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING & PROMO', > 'tracking': ['600775301168', '600775301168', '600775301176', > '600775301184', '600775301192'], 'id': '3', 'prov': 'AB'} > {'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING & PROMO', > 'tracking': ['600775301168', '600775301168', '600775301176', > '600775301184', '600775301192', '600775301200'], 'id': '3', 'prov': > 'AB'} > so I appended everything to a bigdata list and used it to update the > dictionary data_dict > > I can't understand why I get only one value: > {'18': {'city': 'ESTEVAN', > 'company': 'BRAKE & DRIVE SYSTEMS', > 'id': '18', > 'prov': 'SK', > 'tracking': ['600775301515', '600775301515', '600775301523']}} > > Regards, > > Eduardo > > > It looks to me like you keep overwriting the previous data, you keep using > mydict. Doing an append does not copy the dictionary it just copies a > reference to the underlying data structure. > Thank you, Adam. That was the problem. By using deepcopy I managed to fix it: final = deepcopy(mydict) Cheers, Eduardo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Moving from Python 2.5.4 to 2.5.2?
I guess it's a matter of bookkeeping. I don't need the extra hassle dealing with one of them. I'll just let it go at 2.5.x On 8/16/2010 2:41 PM, Luke Paireepinart wrote: The core issue here is: are there actually issues exacerbated by the difference in Python versions? If so, which issues? There shouldn't be hardly any reason to force you all to maintain the exact same python version, especially if you're in the same sub-version (2.5) On Mon, Aug 16, 2010 at 3:57 PM, Wayne Watson wrote: The question is would going back likely cause problems? I'm dealing with neophytes. He's messed up before. On 8/16/2010 8:58 AM, Luke Paireepinart wrote: On Mon, Aug 16, 2010 at 10:39 AM, Wayne Watson wrote: My partner got ahead of the game last year, and installed 2.5.4, which confounds matters when the other four participants when sharing some python programs under Win XP. My guess is that if he uses control panel add/remove for py 2.5.4, he can then successfully install 2.5.2 w/o messing up any programs I've sent him. I'm presuming he did not put them under c:\Python. Is this a question or a statement? Not really sure what you're looking to get out of this. Why would 2.5.4 cause issues with 2.5.2? Why don't you all just upgrade to 2.5.4? -Luke -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "An experiment is a question which science poses to Nature, and a measurement is the recording of Nature’s answer." -- Max Planck Web Page: -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "An experiment is a question which science poses to Nature, and a measurement is the recording of Nature’s answer." -- Max Planck Web Page: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Moving from Python 2.5.4 to 2.5.2?
On 8/16/2010 2:41 PM Luke Paireepinart said... The core issue here is: are there actually issues exacerbated by the difference in Python versions? If so, which issues? There shouldn't be hardly any reason to force you all to maintain the exact same python version, especially if you're in the same sub-version (2.5) I agree. 2.5.4 is a bug release for the 2.5 series and by decree may not introduce backward incompatibilities of any kind. If there's a compatibility issue, please provide details so it can be verified as it ought to be reported. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Dictionaries
Chorn, Guillaume wrote: Hi All, I know that I can look up the value for a particular key in a dictionary, but can I look up the key associated with a particular value? I understand that this could be problematic from the standpoint of multiple keys having the same value, but even then I feel like Python could just return a list of keys with that value. Thanks! Guillaume Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates Direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system. As long as you don't mind it being (relatively) slow, you could do something like (untested): [key for key, item in mydict if item == value] DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Dictionaries
What do you mean by subclass? On Aug 16, 2010 3:26 PM, "Emile van Sebille" wrote: On 8/16/2010 10:44 AM Chorn, Guillaume said... > > Hi All, > > I know that I can look up the value for a particular key in a > dictionary, but can... Yes. But you'll need to implement it. There are likely modules out there that'll do this, but it'd take more time to look up and evaluate than to simply write and implement exactly what you need. > I understand that this could be problematic from the standpoint > of multiple keys having the sa... So, in untested code you'd have a function something like: result = [] for ky, val in dict.items(): if val == targetval: result.append(ky) return result If you need repeated access such that iterating over a large dict frequently impacts performance, you could subclass dict and maintain a second index allowing instant access to the keys associated with a specific value. HTH, 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] Question about Dictionaries
"Chorn, Guillaume" wrote dictionary, but can I look up the key associated with a particular value? Not directly but its not hard to do bearing in mind you will get a collection back.: d = {1:10,2:20,3:10,4:20} val = 10 ks = [k for k in d if d[k] == val] ks [1, 3] You need to trawl the dictionary whatever you do (unless you get very sophisticated and build a double dictionary class, but then insertions get slow because you have to update both versions...) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Dictionaries
Thanks Dave. Actually I figured out a relatively simple solution to my specific problem--since the dictionary I made was the result of zipping together two lists, I simply made a second dictionary with the lists switched around after the zip command and used that. It worked in my case because all key-value pairings were unique... -Original Message- From: Dave Angel [mailto:da...@ieee.org] Sent: Monday, August 16, 2010 4:03 PM To: Chorn, Guillaume Cc: tutor@python.org Subject: Re: [Tutor] Question about Dictionaries Chorn, Guillaume wrote: > Hi All, > > I know that I can look up the value for a particular key in a > dictionary, but can I look up the key associated with a particular > value? I understand that this could be problematic from the standpoint > of multiple keys having the same value, but even then I feel like Python > could just return a list of keys with that value. > > Thanks! > > Guillaume > > Notice: This e-mail message, together with any attachments, contains > information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, > New Jersey, USA 08889), and/or its affiliates Direct contact information > for affiliates is available at > http://www.merck.com/contact/contacts.html) that may be confidential, > proprietary copyrighted and/or legally privileged. It is intended solely > for the use of the individual or entity named on this message. If you are > not the intended recipient, and have received this message in error, > please notify us immediately by reply e-mail and then delete it from > your system. > > As long as you don't mind it being (relatively) slow, you could do something like (untested): [key for key, item in mydict if item == value] DaveA Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates Direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Schema change in ElementTree
"Stefan Behnel" wrote Note that it's best to hit "reply" when responding to other people's postings. Or on the tutor list make that ReplyAll! :-) Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] my Python learning exercise
On Mon, Aug 16, 2010 at 9:25 AM, Luke Paireepinart wrote: > > Anyone who finds this interesting may also want to look into > http://inventwithpython.com/ , I hear it's a good book but I haven't > personally worked through it. > > > Very interesting, looks good! Thanks, Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Moving from Python 2.5.4 to 2.5.2?
On Tue, 17 Aug 2010 06:57:37 am Wayne Watson wrote: > The question is would going back likely cause problems? I'm dealing > with neophytes. He's messed up before. There shouldn't be any differences you're likely to care about between 2.5.2 and 2.5.4, but if there is, your best approach is for you to upgrade to 2.5.4 rather than trying to get the neophyte to downgrade to 2.5.2. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Dictionaries
On Tue, 17 Aug 2010 03:44:33 am Chorn, Guillaume wrote: > Hi All, > > I know that I can look up the value for a particular key in a > dictionary, but can I look up the key associated with a particular > value? I understand that this could be problematic from the > standpoint of multiple keys having the same value, but even then I > feel like Python could just return a list of keys with that value. There is no built-in way of doing so, but it's easy to write your own. def reverse_lookup(d, target, first_only=False): found = [] for key, value in d.items(): if value == target: if first_only: return key found.append(key) return found Or if you want a one-liner, use a list-comp: [k for (k,v) in d.items() if v == target] -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Dictionaries
On 8/16/2010 4:12 PM Huy Ton That said... What do you mean by subclass? If you need repeated access such that iterating over a large dict frequently impacts performance, you could subclass dict and maintain a second index allowing instant access to the keys associated with a specific value. HTH, Emile Something along these lines: class myDict(dict): def __init__(self): self.altKeys = {} def __setitem__(self,ky,val): self.altKeys[val]=ky return dict.__setitem__(self, ky,val) def lookup(self,ky): return self.altKeys[ky] a = myDict() a[1] = 111 a[2] = 222 a[3] = 333 a[3] a.lookup(333) Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Dictionaries
On Tue, 17 Aug 2010 09:12:39 am Huy Ton That wrote: > What do you mean by subclass? It's a fundamental term from object-oriented programming. If you have a class that defines certain data and behaviour, you can create a *subclass* that inherits the same data and behaviour, except for specific examples which are over-ridden or over-loaded. A simple example: class MyInt(int): # creates a subclass of int def __str__(self): return "My Integer %d" % self MyInts are exactly the same as built-in ints except their string form is different. >>> n = MyInt(42) >>> print n My Integer 42 -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Moving from Python 2.5.4 to 2.5.2?
No question about that. For the record, I'm not going to ask him to change based on the answers here. On 8/16/2010 5:14 PM, Steven D'Aprano wrote: On Tue, 17 Aug 2010 06:57:37 am Wayne Watson wrote: The question is would going back likely cause problems? I'm dealing with neophytes. He's messed up before. There shouldn't be any differences you're likely to care about between 2.5.2 and 2.5.4, but if there is, your best approach is for you to upgrade to 2.5.4 rather than trying to get the neophyte to downgrade to 2.5.2. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "An experiment is a question which science poses to Nature, and a measurement is the recording of Nature’s answer." -- Max Planck Web Page: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Schema change in ElementTree
> Note that it's best to hit "reply" when responding to other people's > postings. That way, mail readers can see the relation between the original > posting and the reply and sort the reply into the right thread. > > Stefan how's this? btw, I am getting daily(?) digests of the posts via E-mail and not each post individually. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] box drawing characters
I am looking for some guidance about how to best utilize box drawing characters(using unicode?) in as portable a way as possible in Python. Any good guides out there for this? Thanks, Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Schema change in ElementTree
> Stefan Behnel wrote: Note that it's best to hit "reply" when responding to other people's postings. That way, mail readers can see the relation between the original posting and the reply and sort the reply into the right thread. Comment to my own posting: as Alan noted already, "reply all" or "reply to list" is more suitable in this context. Justin Ezequiel, 17.08.2010 03:35: how's this? btw, I am getting daily(?) digests of the posts via E-mail and not each post individually. Ah, ok, sorry, that explains it. I'm actually reading the list through the Gmane newsgroup mirror (gmane.comp.python.tutor). That allows me to be selective on what I read and prevents my account from getting flooded with e-mails while at the same time providing a perfect interface in both directions. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor