Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-20 Thread Liam Clarke
Ah, see, I should convince my bosses that I need a Python interpreter. Of course, then they'd ask what Python was, and why I was thinking about it at work Duh, I was just reading the docs, and I kept thinking that an attribute was just a class variable. Thanks, Kent, now I have all sorts of i

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-20 Thread Kent Johnson
Liam Clarke wrote: Hi, just an expansion on Brian's query, is there a variant of getattr for instance methods? i.e. class DBRequest: def __init__(self, fields, action): self.get(fields) def get(self, fields): print fields Instead of self.get in _init__, the value of

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-20 Thread Liam Clarke
Hi, just an expansion on Brian's query, is there a variant of getattr for instance methods? i.e. class DBRequest: def __init__(self, fields, action): self.get(fields) def get(self, fields): print fields Instead of self.get in _init__, the value of action to call a

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-16 Thread Brian van den Broek
Jeff Shannon said unto the world upon 2005-02-16 16:09: On Tue, 15 Feb 2005 23:48:31 -0500, Brian van den Broek <[EMAIL PROTECTED]> wrote: Yes, if you know that you will only have one header per line, then it's reasonable to process them one line at a time. You could alternatively have the TP_f

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-16 Thread Brian van den Broek
Kent Johnson said unto the world upon 2005-02-16 15:02: Brian van den Broek wrote: I had been thinking better to get everything working and then refactor. Is that an unsound approach? My worry about refactoring now is that I feel like I am rearranging deck-chairs when I should be worried about

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-16 Thread Jeff Shannon
On Tue, 15 Feb 2005 23:48:31 -0500, Brian van den Broek <[EMAIL PROTECTED]> wrote: > Jeff Shannon said unto the world upon 2005-02-15 21:20: > > On Tue, 15 Feb 2005 17:19:37 -0500, Brian van den Broek > > <[EMAIL PROTECTED]> wrote: > > > > For starters, I've made metadata a class attribute rather t

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-16 Thread Kent Johnson
Brian van den Broek wrote: As for the code smell thing, I have a follow-up question. I now get the point of the type-based conditional being a smell for classes. (I get it due to a previous thread that an over-enthusiastic inbox purge prevents me from citing with certainty, but I think it was Bi

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-16 Thread Brian van den Broek
Brian van den Broek said unto the world upon 2005-02-16 14:04: Kent Johnson said unto the world upon 2005-02-16 05:58: if 'text' == self.document_type: self.do_text_stuff() if 'RTF' == self.document_type: self.do_RTF_stuff() Conditionals on a 'type' flag are a code smell that suggests using

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-16 Thread Brian van den Broek
Kent Johnson said unto the world upon 2005-02-16 05:58: Brian van den Broek wrote: Also, I know the general security concerns about things like exec. They make me nervous in using it, even though I am (as yet) the sole user. Am I right in thinking that the constrained way I am using it here pro

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-16 Thread Kent Johnson
Kent Johnson wrote: Another way to do this is to use dispatch methods. If you have extra processing to do for each tag, this might be a good way to go. I'm going to assume that your data lines have the form 'tag=data'. Then your Node class might have methods that look like this: class Node: .

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-16 Thread Kent Johnson
Brian van den Broek wrote: My Node class defines a _parse method which separates out the node header, and sends those lines to a _parse_metadata method. This is where the elif chain occurs -- each line of the metadata starts with a tag like "dt=" and I need to recognize each tag and set the appr

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-15 Thread Brian van den Broek
Jeff Shannon said unto the world upon 2005-02-15 21:20: On Tue, 15 Feb 2005 17:19:37 -0500, Brian van den Broek <[EMAIL PROTECTED]> wrote: My Node class defines a _parse method which separates out the node header, and sends those lines to a _parse_metadata method. This is where the elif chain occu

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-15 Thread Jeff Shannon
On Tue, 15 Feb 2005 17:19:37 -0500, Brian van den Broek <[EMAIL PROTECTED]> wrote: > My Node class defines a _parse method which separates out the node > header, and sends those lines to a _parse_metadata method. This is > where the elif chain occurs -- each line of the metadata starts with a > ta

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-15 Thread Brian van den Broek
Liam Clarke said unto the world upon 2005-02-15 18:08: Hi Brian, why not take it the next step and for key in metadata_dict: if data.startswith(key): exec('''self.%s = """%s"""''' %(metadata_dict[key], data[len(key):])) # tripl

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-15 Thread Liam Clarke
Hi Brian, why not take it the next step and > for key in metadata_dict: > if data.startswith(key): > exec('''self.%s = """%s"""''' %(metadata_dict[key], > data[len(key):])) > # triple quotes as there may be quotes in meta

Re: [Tutor] dictionary dispatch for object instance attributes question

2005-02-15 Thread Rich Krauter
Brian van den Broek wrote: [snip text] class A: def __init__(self): self.something = None self.something_else = None self.still_another_thing = None def update(self, data): for key in metadata_dict: if data.startswith(key): exec('''self.%s = ""

[Tutor] dictionary dispatch for object instance attributes question

2005-02-15 Thread Brian van den Broek
Hi all, I'm still plugging away at my project of writing code to process treepad files. (This was the task which I posted about in the recent "help with refactoring needed -- which approach is more Pythonic?" thread.) My present problem is how best to reorganize a long (20 elements) elif chain