Re: [Tutor] dictionary looping problem

2008-12-02 Thread Jeremiah Jester
Thanks for clearing this up for me. On Tue, 2008-12-02 at 13:25 -0800, Steve Willoughby wrote: > On Tue, Dec 02, 2008 at 01:08:09PM -0800, Jeremiah Jester wrote: > > Hello, > > > > I'm trying to gather a list of files and md5 hash them to do a > checksum. > > I've created a function for each dict

Re: [Tutor] dictionary looping problem

2008-12-02 Thread Steve Willoughby
On Tue, Dec 02, 2008 at 01:08:09PM -0800, Jeremiah Jester wrote: > Hello, > > I'm trying to gather a list of files and md5 hash them to do a checksum. > I've created a function for each dictionary. However, when i print out > the dictionary I don't get all the items. Any ideas? Yep. Don't use os

Re: [Tutor] Dictionary of dictionaries issue

2008-11-13 Thread Pablo Englebienne
Thank you all, this is exactly what I was trying to do and the syntax is beautiful... :-) -- Pablo Englebienne "Progress is made by lazy men looking for easier ways to do things." - Robert A. Heinlein Kent Johnson wrote: On Thu, Nov 13, 2008 at 10:01 AM, A.T.Hofkamp <[EMAIL PROTECTED]> wro

Re: [Tutor] Dictionary of dictionaries issue

2008-11-13 Thread Tim Golden
Kent Johnson wrote: On Thu, Nov 13, 2008 at 10:01 AM, A.T.Hofkamp <[EMAIL PROTECTED]> wrote: d3 = dict(( (rv, dict.fromkeys(c)) for rv in r )) You don't need the double parentheses, this works just as well: d3 = dict( (rv, dict.fromkeys(c)) for rv in r ) A generator expression just has to be

Re: [Tutor] Dictionary of dictionaries issue

2008-11-13 Thread Kent Johnson
On Thu, Nov 13, 2008 at 10:01 AM, A.T.Hofkamp <[EMAIL PROTECTED]> wrote: d3 = dict(( (rv, dict.fromkeys(c)) for rv in r )) You don't need the double parentheses, this works just as well: d3 = dict( (rv, dict.fromkeys(c)) for rv in r ) A generator expression just has to be in parentheses, it'

Re: [Tutor] Dictionary of dictionaries issue

2008-11-13 Thread Kent Johnson
On Thu, Nov 13, 2008 at 8:11 AM, Pablo Englebienne <[EMAIL PROTECTED]> wrote: > Hi, I'm trying to work with a dictionary of dictionaries and I'm having > trouble accessing a specific element of it: > > $ python > Python 2.6 (trunk:66714:66715M, Oct 1 2008, 18:36:04) > [GCC 4.0.1 (Apple Computer, I

Re: [Tutor] Dictionary of dictionaries issue

2008-11-13 Thread A.T.Hofkamp
Pablo Englebienne wrote: Hi, I'm trying to work with a dictionary of dictionaries and I'm having trouble accessing a specific element of it: $ python Python 2.6 (trunk:66714:66715M, Oct 1 2008, 18:36:04) [GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin Type "help", "copyright", "credits

Re: [Tutor] Dictionary within a dictionary

2008-06-14 Thread Kent Johnson
On Fri, Jun 13, 2008 at 10:23 AM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > Following Alan's post, what I was trying to do is to understand how I > can return the sub-item within the same space, if it makes sense ;) Um, no, not to me. Is your question about creating a structure or acces

Re: [Tutor] Dictionary within a dictionary

2008-06-14 Thread [EMAIL PROTECTED]
Hello, Thank you for the replies. I wanted to hold the data in memory, because the number of records do not warrant the need to have to maintain an additional relational database. Plus I wanted to understand how to build this using classes as this will perhaps give me the bridge I require to mo

Re: [Tutor] Dictionary within a dictionary

2008-06-13 Thread Alan Gauld
"Marilyn Davis" <[EMAIL PROTECTED]> wrote When see nested builtin data structures, I always think it's time to think of making a few classes instead. It could be, especially if you are about to write a bunch of functions to access those structures. But equally if the structures accurately re

Re: [Tutor] Dictionary within a dictionary

2008-06-13 Thread Marilyn Davis
On Fri, June 13, 2008 7:23 am, [EMAIL PROTECTED] wrote: > Hi, > Following Alan's post, what I was trying to do is to understand how I > can return the sub-item within the same space, if it makes sense ;) > > For example, in my 3 one-to-many lists, I want to generate this list: When see nested bui

Re: [Tutor] Dictionary within a dictionary

2008-06-13 Thread [EMAIL PROTECTED]
Hi, Following Alan's post, what I was trying to do is to understand how I can return the sub-item within the same space, if it makes sense ;) For example, in my 3 one-to-many lists, I want to generate this list: [{'id': , 'category': [{'id': , 'sub-c

Re: [Tutor] Dictionary within a dictionary

2008-06-13 Thread Kent Johnson
On Thu, Jun 12, 2008 at 7:56 AM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > I would like to understand how to generate dictionaries based on other > dictionaries. For example, I have a 3 tables in ym SQL database - > Groups, Categories and Sub-categories with a one-to-many relations > bet

Re: [Tutor] Dictionary within a dictionary

2008-06-12 Thread Alan Gauld
<[EMAIL PROTECTED]> wrote I am presuming that each of these tables is one of my dictionaries?!?! What I don't understand is how to write this SQL statement in python code: SELECT id, category from Categories WHERE group_id = "1"; SQL is designed(and optimised) for doing complex data querie

Re: [Tutor] dictionary append

2007-11-01 Thread Kent Johnson
bob gailer wrote: > if key not in keywords: >keywords[key] = [] > keywords[key].append(value) This can be written more succinctly as keywords.setdefault(key, []).append(value) or in Python 2.5: from collections import defaultdict keywords = defaultdict(list) ... keywords[key

Re: [Tutor] dictionary append

2007-11-01 Thread bob gailer
Dinesh B Vadhia wrote: > Hello! I'm creating a dictionary called keywords that has multiple > entries each with a variable list of values eg. > > keywords[1] = [1, 4, 6, 3] > keywords[2] = [67,2] > keywords[3] = [2, 8, 5, 66, 3, 23] > etc. > > The keys and respective values (both are integers

Re: [Tutor] dictionary append

2007-11-01 Thread Alan Gauld
"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote keywords[1] = [1, 4, 6, 3] keywords[2] = [67,2] keywords[3] = [2, 8, 5, 66, 3, 23] etc. The keys and respective values (both are integers) are read in from a file. For each key, the value is append'ed until the next key. Here is the code. ..

Re: [Tutor] Dictionary - count values where values are stored as a list

2007-10-01 Thread GTXY20
Thanks so much I changed to the following and this worked: def HolderDistributionqty(dictionary): from collections import defaultdict count=defaultdict(int) for item in dictionary.values(): count[len(item)]+=1 for k,v in sorted(count.items()): fdist=k qty=v

Re: [Tutor] Dictionary - count values where values are stored as a list

2007-10-01 Thread Kent Johnson
GTXY20 wrote: > > Thanks again I have worked that issue out. > > However I have the following function and it is throwing this error: > > FEXpython_v2.py", line 32, in UnitHolderDistributionqty > count[item]+=1 > KeyError: 3 > > This is the function: > > def Distributionqty(dictionary

Re: [Tutor] Dictionary - count values where values are stored as a list

2007-10-01 Thread GTXY20
Thanks again I have worked that issue out. However I have the following function and it is throwing this error: FEXpython_v2.py", line 32, in UnitHolderDistributionqty count[item]+=1 KeyError: 3 This is the function: def Distributionqty(dictionary): holder=list() held=list() dis

Re: [Tutor] Dictionary - count values where values are stored as a list

2007-10-01 Thread Ricardo Aráoz
GTXY20 wrote: > Hello, > > Any way to display the count of the values in a dictionary where the > values are stored as a list? here is my dictionary: > > {'1': ['a', 'b', 'c'], '3': ['a', 'b', 'c'], '2': ['a', 'b', 'c'], '4': > ['a', 'c']} > > I would like to display count as follows and I would

Re: [Tutor] Dictionary - count values where values are stored as a list

2007-10-01 Thread Kent Johnson
GTXY20 wrote: > > This works perfectly. > > However I will be dealing with an import of a very large dictionary - if > I call the commands at command line this seems to be very taxing on the > CPU and memory and will take a long time. > > I was thinking of creating each as a fucntion whereb

Re: [Tutor] Dictionary - count values where values are stored as a list

2007-10-01 Thread GTXY20
This works perfectly. However I will be dealing with an import of a very large dictionary - if I call the commands at command line this seems to be very taxing on the CPU and memory and will take a long time. I was thinking of creating each as a fucntion whereby python would just to write to a fi

Re: [Tutor] Dictionary - count values where values are stored as a list

2007-10-01 Thread Kent Johnson
GTXY20 wrote: > Hello, > > Any way to display the count of the values in a dictionary where the > values are stored as a list? here is my dictionary: > > {'1': ['a', 'b', 'c'], '3': ['a', 'b', 'c'], '2': ['a', 'b', 'c'], '4': > ['a', 'c']} > > I would like to display count as follows and I wou

Re: [Tutor] Dictionary - count values where values are stored as a list

2007-10-01 Thread Alan Gauld
"GTXY20" <[EMAIL PROTECTED]> wrote > Any way to display the count of the values in a dictionary where the > values > are stored as a list? here is my dictionary: > > {'1': ['a', 'b', 'c'], '3': ['a', 'b', 'c'], '2': ['a', 'b', 'c'], > '4': > ['a', 'c']} > > I would like to display count as foll

Re: [Tutor] Dictionary Values Questions

2007-05-01 Thread Kent Johnson
Tony Waddell wrote: > I am wondering how look for a key in a dictionary, given a value. > > I have a dictionary similar to this: > a = { 'a1':1, 'a2':2, 'a3':3, 'a4'.:4} > > If I have the value of 2, how would I look at the dictionary to turn > that into 'a2'. You have to search the values. This

Re: [Tutor] dictionary manipulation

2006-07-26 Thread Alan Gauld
> for key in sorted(result.keys): a sorted list of keys Oops, that should of course be: for key in sorted(result.keys() ): Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] dictionary manipulation

2006-07-26 Thread Alan Gauld
Chris, You seem to be going through several hoops that you don't need here. Let the data structures do the work of storage and extract the data you want in its various pieces. You are getting the entire data set in a string then trying to pick out the bits you want, that defeats the purpose of havi

Re: [Tutor] dictionary manipulation

2006-07-26 Thread Kent Johnson
Chris Hallman wrote: > > I need some suggestions on how to work with a dictionary. I've got a > program that builds a dictionary. I need to be able to manipulate the > different keys and data so that I can write the output to a file AND > utilize the smtplib to send the data in an email. I had p

Re: [Tutor] dictionary manipulation

2006-07-26 Thread David Heiser
Title: Message   Chris,   This looks similar to what I do for my job. I would be happy to help you, if I can.   My first question is, how would you like the output to look? Can you manually create a model of the email text you want to send?   My second question is, can you create the email

Re: [Tutor] dictionary manipulation

2006-07-26 Thread Bob Gailer
Chris Hallman wrote: > > I need some suggestions on how to work with a dictionary. I've got a > program that builds a dictionary. I need to be able to manipulate the > different keys and data so that I can write the output to a file AND > utilize the smtplib to send the data in an email. I had p

Re: [Tutor] dictionary datatype

2006-04-12 Thread Alan Gauld
> For example, I have a dictionary: > dict1 = { 0x2018:u'k', 0x2019:u'd'} > > I assign: > n = 0x2018 > print dict1[n] > > Then: > KeyError: '0x2018' The error is complaining that you are using a string as a key. Are you sure you aren't assigning n = '0x2018' Alan G Author of the learn to progr

Re: [Tutor] dictionary datatype

2006-04-12 Thread Hugo González Monteverde
kakada wrote: > I assign: > n = 0x2018 > print dict1[n] > > Then: > KeyError: '0x2018' > Notice the error menstions a *string*. You probably mistyped it, not in the email, but in your actual program. Hugo ___ Tutor maillist - Tutor@python.org http

Re: [Tutor] dictionary datatype

2006-04-12 Thread kakada
It also works for me now:) First i tried it with Konsole in Kate and it's not working. It sometimes happen with assignment statement: i += 1 (not working) i+= 1(working) but later on I tested it again and both are working. Thanks for your help, though. da Jason Massey wrote: > Works for

Re: [Tutor] dictionary datatype

2006-04-11 Thread Victor Bouffier
On Tue, 2006-04-11 at 22:37 -0500, Jason Massey wrote: > Works for me: > > >>> dict1 = { 0x2018:u'k', 0x2019:u'd'} > >>> n = 0x2018 > >>> print dict1[n] > k > >>> > > On 4/11/06, kakada <[EMAIL PROTECTED]> wrote: > Hello all, > > For example, I have a dictionary: >

Re: [Tutor] dictionary datatype

2006-04-11 Thread Jason Massey
Works for me:>>> dict1 = { 0x2018:u'k', 0x2019:u'd'}>>> n = 0x2018>>> print dict1[n]k>>> On 4/11/06, kakada <[EMAIL PROTECTED]> wrote: Hello all,For example, I have a dictionary:dict1 = { 0x2018:u'k', 0x2019:u'd'}I assign:n = 0x2018print dict1[n]Then:KeyError: '0x2018'But I can call directly:print

Re: [Tutor] dictionary question

2005-11-11 Thread Alan Gauld
> I should have known. sheesh python is so kewl. I keep forgetting most > times, > it will do stuff directly and you don't have to assign.. Whether thats 'kewl' depends on your viewpoint. >From a pure computing point of view returning a value is more consistent and correct in a functional program

Re: [Tutor] dictionary question

2005-11-11 Thread Eric Walker
ahh man, I should have known. sheesh python is so kewl. I keep forgetting most times, it will do stuff directly and you don't have to assign.. Thanks Python Newbie On Friday 11 November 2005 02:59 pm, DS wrote: > You almost have it. Do this instead. > > d = {'first':[]} > d['first'].appe

Re: [Tutor] dictionary question

2005-11-11 Thread DS
You almost have it. Do this instead. d = {'first':[]} d['first'].append("string") Append acts on the list, so assignment is unnecessary. ds Eric Walker wrote: >All, >I have a dictionary say: >d = {'first':[]} >I am going through another list and depending on whats going on, >I want to add to t

Re: [Tutor] dictionary question

2005-11-11 Thread Shi Mu
On 11/11/05, Eric Walker <[EMAIL PROTECTED]> wrote: > All, > I have a dictionary say: > d = {'first':[]} > I am going through another list and depending on whats going on, > I want to add to the empty list. I have tried the following to noavail. > > d['first'] = d['first'].append("string") > > I wo

Re: [Tutor] Dictionary Error: 'dict' object hasnoattribute '_contains_'

2005-11-06 Thread Alan Gauld
> Oh, about four years ago :-) in Python 2.2 > > You really should read the "What's New in Python x.x" docs ;) :-) Hmm, I usually skim them but pick up most of my info from this list because I rarely upgrade to the latest version right away - I only upgraded my XP box. to 2.4 a few weeks ago..

Re: [Tutor] Dictionary Error: 'dict' object has noattribute '_contains_'

2005-11-06 Thread Alan Gauld
> I used to want that syntax when I started, now I stick to for x in > y.keys() I knew you could do for item in dictionary: instead of for item in dictionary.keys(): But I didn't realise it worked for a single item test too if item in dictionary instead of if dictionary.has_key(item): Whi

Re: [Tutor] Dictionary Error: 'dict' object has noattribute '_con tains_'

2005-11-06 Thread Liam Clarke-Hutchinson
On Behalf Of Alan Gauld Sent: Monday, 7 November 2005 8:51 a.m. To: Kent Johnson Cc: tutor@python.org Subject: Re: [Tutor] Dictionary Error: 'dict' object has noattribute '_contains_' > But you normally shouldn't call this directly, you should write > "test&quo

Re: [Tutor] Dictionary Error: 'dict' object has noattribute '_contains_'

2005-11-06 Thread Kent Johnson
Alan Gauld wrote: >> But you normally shouldn't call this directly, you should write >> "test" in menu_specials > > > Now howzabout that! Yet another new trick learnt. > When did 'in' start working with dictionaries? Oh, about four years ago :-) in Python 2.2 You really should read the "What's

Re: [Tutor] Dictionary Error: 'dict' object has noattribute '_contains_'

2005-11-06 Thread Alan Gauld
> But you normally shouldn't call this directly, you should write > "test" in menu_specials Now howzabout that! Yet another new trick learnt. When did 'in' start working with dictionaries? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.p

Re: [Tutor] Dictionary Error: 'dict' object has no attribute '_contains_'

2005-11-06 Thread Alan Gauld
> Hi! I'm on the version 2.4, going through Beginning Python (Wrox), and > I'm getting the above error. I'm trying to do this: > > menu_specials._contains_("test") > > Any ideas on what I'm doing wrong? Thanks! It looks like you may be reading the wrong book! Why they would suggest you ever exec

Re: [Tutor] Dictionary Error: 'dict' object has no attribute '_contains_'

2005-11-06 Thread Kent Johnson
Trent Rigsbee wrote: > Hi! I'm on the version 2.4, going through Beginning Python (Wrox), and I'm > getting the above error. I'm trying to do this: > > menu_specials._contains_("test") > > Any ideas on what I'm doing wrong? Thanks! The name of the method is __contains__ (note *double* leading

Re: [Tutor] dictionary

2005-10-24 Thread Alan Gauld
> A dictionary stores its values based on the keys. Internally Python > sorts the keys to make the lookup meet performance expectations. > > You can not expect a dictionary to return keys in the other you added > them. If you want that, store the items as tuples in a list. Or you can sort the k

Re: [Tutor] dictionary

2005-10-24 Thread Danny Yoo
> > I typed: > > landUse = {'res': 1, 'com': 2, 'ind': 3, "other" :[4,5,6,7]} > > and when i checked landUse, I found it become: > > {'ind': 3, 'res': 1, 'other': [4, 5, 6, 7], 'com': 2} > > why the order is changed? > > the docs warn you about this. Hi Shi, By the way, here's a post from a lon

Re: [Tutor] dictionary

2005-10-24 Thread Sean Perry
Shi Mu wrote: > I typed: > landUse = {'res': 1, 'com': 2, 'ind': 3, "other" :[4,5,6,7]} > and when i checked landUse, I found it become: > {'ind': 3, 'res': 1, 'other': [4, 5, 6, 7], 'com': 2} > why the order is changed? the docs warn you about this. A dictionary stores its values based on the ke

Re: [Tutor] dictionary

2005-10-24 Thread Jan Erik Moström
Shi Mu <[EMAIL PROTECTED]> 2005-10-24 09:13: > why the order is changed? By definition a dictionary has no order. jem -- Jan Erik Moström, www.mostrom.pp.se ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinf

Re: [Tutor] dictionary

2005-10-24 Thread Pujo Aji
you work with dictionary. In dictionary you work with the key to get your value. You don't care about how python arrange the key. your key here are: 'res', 'com', 'ind', and  'other' when you want to get your value you just type: landUse['res'] this you will get 1 landUse['ind'] this you will get 3

Re: [Tutor] dictionary question

2005-07-28 Thread Kent Johnson
David Driver wrote: > If I > create a dictionary with the same keys, will it str the same way? This behaviour is not guaranteed and I wouldn't depend on it. It can break if any other operations have happened on the dict; for example >>> d=dict(a=1,b=2,c=23) >>> str(d) "{'a': 1, 'c': 23, 'b': 2

Re: [Tutor] dictionary values

2005-07-08 Thread Brian van den Broek
Kent Johnson said unto the world upon 08/07/2005 21:09: > Brian van den Broek wrote: > >>if you care about the possibility that there is no unique key with the >>lowest value, I'd do: >> >> >>> def get_low_keys(a_dict): >>... '''-> list of keys in a_dict with lowest value''' >>... min_val = m

Re: [Tutor] dictionary values

2005-07-08 Thread Kent Johnson
Brian van den Broek wrote: > if you care about the possibility that there is no unique key with the > lowest value, I'd do: > > >>> def get_low_keys(a_dict): > ... '''-> list of keys in a_dict with lowest value''' > ... min_val = min(a_dict.values()) > ... low_keys = [] > ... for k,v in

Re: [Tutor] dictionary values

2005-07-08 Thread Kent Johnson
luke p wrote: > just assume all the below code is correct. > I am not having a problem with it, it is all for example only. > > I have a dictionary like this: > alpha = {'a':0,'b':0, ... 'z':0} > and the following code > f = file("hamlet.txt","r") > text = f.readlines() > f.close() > for line in t

Re: [Tutor] dictionary values

2005-07-08 Thread Brian van den Broek
luke p said unto the world upon 08/07/2005 19:40: > just assume all the below code is correct. > I am not having a problem with it, it is all for example only. > > I have a dictionary like this: > alpha = {'a':0,'b':0, ... 'z':0} > and the following code > f = file("hamlet.txt","r") > text = f.rea

Re: [Tutor] dictionary values

2005-07-08 Thread Brian van den Broek
Don Parris said unto the world upon 08/07/2005 20:09: > On 7/8/05, luke p <[EMAIL PROTECTED]> wrote: >>what I want to do is find out which value in my dictionary is lowest. >>is there a dictionary function for this, like alpha.min() that will >>return a key:value pair of the lowest? I cannot fi

Re: [Tutor] dictionary values

2005-07-08 Thread Don Parris
On 7/8/05, luke p <[EMAIL PROTECTED]> wrote: > just assume all the below code is correct. > I am not having a problem with it, it is all for example only. > > I have a dictionary like this: > alpha = {'a':0,'b':0, ... 'z':0} > and the following code > f = file("hamlet.txt","r") > text = f.readline

Re: [Tutor] dictionary values in strings

2005-05-20 Thread William O'Higgins
On Thu, May 19, 2005 at 09:47:50PM +0100, Max Noel wrote: > >On May 19, 2005, at 20:49, William O'Higgins wrote: > >>I am trying to discover the syntax for call on a dictionary of >>lists by >>key and index. >> >>The data structure looks like this: >> >>dol = {'key1':['li1','li2','li3'],'key2':['

Re: [Tutor] dictionary values in strings

2005-05-19 Thread Max Noel
On May 19, 2005, at 20:49, William O'Higgins wrote: > I am trying to discover the syntax for call on a dictionary of > lists by > key and index. > > The data structure looks like this: > > dol = {'key1':['li1','li2','li3'],'key2':['li1','li2','li3'],\ > 'key3':['li1'li2,'li3','']} > > The keys

Re: [Tutor] dictionary values in strings

2005-05-19 Thread Bernard Lebel
Indeed, dictionaries don't have a .key attribute. Instead, use: # Get list of values for 'key1' aList = dol[ 'key1' ] This would return the list of values you have assigned to 'key1' in the dictionary. Once you got that list, you can look in the list to find out the index of 'lil1' and return i

Re: [Tutor] Dictionary Inserts...

2005-05-05 Thread Allen John Schmidt, Jr.
Arrgh! How could I be so stupid! :) Thanks for the help! I know many things about python, but I can't believe that I didn't know that! Thanx again! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Dictionary Inserts...

2005-05-05 Thread Max Noel
On May 5, 2005, at 16:37, Allen John Schmidt, Jr. wrote: > Ok, I have had enough. I have looked all through the python docs and I > cannot find it. How do you insert an entry into a dictionary? > The way you think it's done: >>> a = {'foo': 1, 'bar': 2} >>> a {'foo': 1, 'bar': 2} >>> a[

Re: [Tutor] Dictionary Inserts...

2005-05-05 Thread André Roberge
Allen John Schmidt, Jr. wrote: > Ok, I have had enough. I have looked all through the python docs and I > cannot find it. How do you insert an entry into a dictionary? > > Thanx! > >>> my_dict = {} >>> my_dict['new_entry'] = ['simple', 'as', 1, 2, 3] >>> my_dict {'new_entry': ['simple', 'as'

Re: [Tutor] Dictionary Inserts...

2005-05-05 Thread Eric Culpepper
Here's some quick examples: >>> dictMe = {} >>> dictMe['SomeKey'] = 'SomeValue' >>> dictMe {'SomeKey': 'SomeValue'} >>> dictMe[1] = 100 >>> dictMe {1: 100, 'SomeKey': 'SomeValue'} >>> type(dictMe) Section 5.5 of the Python tutorial covers dictionaries a little more in depth. http://docs.python.o

Re: [Tutor] Dictionary blues...

2005-03-24 Thread Alan Gauld
> >if D.has_key(event.keysym): > > str=D[event.keysym] > >self.text.insert(END,str) > > You can remove the 'text.' thats only used if text > were part of a class, which in this case it isn't. Oops, that should say remove 'self.' not text. Alan G. ___

Re: [Tutor] Dictionary blues...

2005-03-23 Thread Igor Riabtchuk
11:51 PM Subject: Re: [Tutor] Dictionary blues... My deepest and most sincere apologies - cooking dinner for the family and posting questions do not mix, I keep making mistakes in the code I type. Once again my apologies - here's the code as it is in my source: import sys, os, unicodedat

Re: [Tutor] Dictionary blues...

2005-03-23 Thread Igor Riabtchuk
;p': str='t' elif event.keysym=='t': str='z' put all the conversion values into a dictionary and make the function use the key:value pairs from dictionary. I hope I am making sense. Igor - Original Message - From: "Alan Gauld" <[EMAIL

Re: [Tutor] Dictionary blues...

2005-03-23 Thread Alan Gauld
Igor, > I posted the wrong code before. The code is: Is this the actual code you have written? If so it is so far from being working code that it suggests you need to back up a little and work at the basics of Python before trying to tackle Tkinter and GUIs. I'll assume this really is your co

Re: [Tutor] Dictionary blues...

2005-03-23 Thread Danny Yoo
On Wed, 23 Mar 2005, Igor Riabtchuk wrote: > I posted the wrong code before. The code is: > > from Tkinter import * > > D={a:"tom", b:"dick", c:"harry"} > > text.bind('', self.Conv) > > def Conv(self,event): > if D.has_key(event.keysym): > str=D[event.keysym] > self.text.insert(END

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 = ""

Re: [Tutor] Dictionary Nesting

2005-01-31 Thread Alan Gauld
> If I want to put a dictionary in a dictionary, does the syntax > for assigning and getting at the stuff in the inner dictionary > look something like this: outer_dictionary[inner_dictionary][key] = 'value' Yes. Sometimes it's easier with Python to just try it at the >>> prompt rather than ask t

Re: [Tutor] Dictionary Nesting

2005-01-31 Thread Kent Johnson
jhomme wrote: Hi, If I want to put a dictionary in a dictionary, does the syntax for assigning and getting at the stuff in the inner dictionary look something like this: outer_dictionary[inner_dictionary][key] = 'value' > ? If inner_dictionary is the key to outer_dictionary, then that is right. Fo

<    1   2