Dictionary and Sub-dictionary help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 70
Reputation: shoemoodoshaloo is an unknown quantity at this point 
Solved Threads: 2
shoemoodoshaloo shoemoodoshaloo is offline Offline
Junior Poster in Training

Dictionary and Sub-dictionary help

 
0
  #1
Jun 17th, 2009
Hey everyone,

Here is an outline of my problem:

I have a working code which defines a class, GeneDict, which reads in data from a special type of file, and stores it as a dictionary. What it is really doing is taking in millions of lines of biological data, and storing chromosomes as keys. To each key, there is not just one value, but a list of values. Anyway, that part works fine, so after that, I define a bunch of methods to act on the dictionary (like get the dictionary length for example).
What I want to do now is define a second dictionary. Let's call it, FamilyDict. FamilyDict will have the same keys as GeneDict, but less values. So, what I seek to do is write a subclass that will inherit from GeneDict, take in all the keys, then filter out some of the values that I don't need to keep, and append these values to the new FamilyDict. I seek to use subclasses because I want to be able to use all of the pre-written methods on both the GeneDict and the FamilyDict.

Below I will post the working GeneDict. It is not crucial that you understand everything about this diction, just know that it works:

  1. class GeneDict:
  2. '''Class to build a dictionary with chromosones as keys, with several genes as values, each gene/value being a freaking list of information.'''
  3.  
  4. def __init__(self, file=None): #Will read from file, but file will be defined later at instantiation
  5. '''Reads in file like rep_element.bed, stores chromosones as keys, and all other info as values'''
  6. self.dictionary = {}
  7. infile = open(file)
  8. for line in infile:
  9. if not re.match("#", line): #If the line isn't a header
  10. line = line.strip()
  11. sline = line.split()
  12.  
  13. if sline[5] not in self.dictionary.keys():
  14. self.dictionary[sline[5]] = []; #key is added
  15.  
  16. value=RepeatingElement( int(sline[0]), int(sline[1]), int(sline[2]), int(sline[3]), int(sline[4]), sline[5],
  17. int(sline[6]), int(sline[7]), sline[8],
  18. sline[9], sline[10], sline[11], sline[12],
  19. sline[13], int(sline[14]),
  20. int(sline[15]), sline[16] )
  21.  
  22. self.dictionary[sline[5]].append(value)

Now, here is my attempt at the new dictionary:

  1. class FamilyDict(GeneDict):
  2. def __init__(self, file=None):
  3. GeneDict.__init__(self, file=None)
  4. self.Family_dict= {}
  5. infile = open(file)
  6. for key in dictionary.keys():
  7. self.Family_dict.append(key)

The terminal is already complaining, and all I've tried to do is copy over the keys. In the end, I need to tell FamilyDict to take certain bunch of elements from GeneDict's values. I plan to do it with an expression match, something like:

if str(element.repFamily) == str(family):
"""Check to see if read matches desired family""" etc...

But just for now, can you guys see where my subclass has already gone wrong?
Last edited by shoemoodoshaloo; Jun 17th, 2009 at 5:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,070
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Dictionary and Sub-dictionary help

 
0
  #2
Jun 17th, 2009
Originally Posted by shoemoodoshaloo View Post
  1. class FamilyDict(GeneDict):
  2. def __init__(self, file=None):
  3. GeneDict.__init__(self, file=None)
  4. self.Family_dict= {}
  5. infile = open(file)
  6. for key in dictionary.keys():
  7. self.Family_dict.append(key)
It would help if you explained what was going wrong. Without that info all I can say is, where is the dictionary coming from? You're iterating over its keys but I don't see it getting passed in from anywhere...
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 70
Reputation: shoemoodoshaloo is an unknown quantity at this point 
Solved Threads: 2
shoemoodoshaloo shoemoodoshaloo is offline Offline
Junior Poster in Training

Re: Dictionary and Sub-dictionary help

 
0
  #3
Jun 18th, 2009
Originally Posted by jlm699 View Post
It would help if you explained what was going wrong. Without that info all I can say is, where is the dictionary coming from? You're iterating over its keys but I don't see it getting passed in from anywhere...
Sorry. So, the code compiles with no instantiations; however, when I try this:

  1. moo = FamilyDict('rep_small.bed')
  2. output_contains = moo.__contains__('chr1')

The code raises this error:

  1. Traceback (most recent call last):
  2. File "new_repUCSC.py", line 249, in <module>
  3. moo = FamilyDict('rep_small.bed')
  4. File "new_repUCSC.py", line 126, in __init__
  5. GeneDict.__init__(self, file=None)
  6. File "new_repUCSC.py", line 104, in __init__
  7. infile = open(file)
  8. TypeError: coercing to Unicode: need string or buffer, NoneType found


Perhaps my instantiation is incorrect? I don't know. Does this look familiar?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,070
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Dictionary and Sub-dictionary help

 
0
  #4
Jun 18th, 2009
I see it now the problem is here:
  1. class FamilyDict(GeneDict):
  2. def __init__(self, file=None):
  3. # You're sending None to the __init__ function
  4. GeneDict.__init__(self, file=None)
  5. self.Family_dict= {}
  6. infile = open(file)
  7. for key in dictionary.keys():
  8. self.Family_dict.append(key)
When you call GeneDict.__init__ you are passing file=None. When you call a function, it is different from when you define the function, so using a default parameter as such won't work. What you should be doing is simply:
  1. GeneDict.__init__(self, file)
This is because if someone were to create an instance of FamilyDict and not provide a file parameter. It would become None, which will be passed to GeneDict as None. So no reason to coerce it to None again!

This raises the question however; why allow for an optional parameter if it's going to break your code? If somebody were to create an instance of FamilyDict (or GeneDict) without a file parameter, the same thing would happen. You should force the file parameter to be present and not allow it to be optional. Otherwise, you'll need to check if file: in the GeneDict code before opening it.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,070
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Dictionary and Sub-dictionary help

 
0
  #5
Jun 18th, 2009
As a side note, using file for a parameter name is a bad idea. File is a reserved word in Python
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 70
Reputation: shoemoodoshaloo is an unknown quantity at this point 
Solved Threads: 2
shoemoodoshaloo shoemoodoshaloo is offline Offline
Junior Poster in Training

Re: Dictionary and Sub-dictionary help

 
0
  #6
Jun 18th, 2009
This raises the question however; why allow for an optional parameter if it's going to break your code? If somebody were to create an instance of FamilyDict (or GeneDict) without a file parameter, the same thing would happen. You should force the file parameter to be present and not allow it to be optional. Otherwise, you'll need to check if file: in the GeneDict code before opening it.
I see. Most of these programs use an options parser, and so the restriction is put on there. The program won't begin if the user doesn't specify an infile. But had I not been using this, what would I have to do to my code to eliminate the optional file? Would I remove file=none and replace it with file=infile or something?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,070
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Dictionary and Sub-dictionary help

 
0
  #7
Jun 18th, 2009
Originally Posted by shoemoodoshaloo View Post
what would I have to do to my code to eliminate the optional file?
You would simply remove the =None . Whatever you specify after the equals since in your function definition is the default value that the parameter takes on. So if that parameter is not specified when calling the function, it takes on the default value (in this case None). Let me demonstrate:
  1. >>> def funcA(a, b='Default'):
  2. ... print a, b
  3. ...
  4. >>> def funcB(a, b):
  5. ... print a, b
  6. ...
  7. >>> funcA('Hi', 'Blue')
  8. Hi Blue
  9. >>> funcB('Hi', 'GReen')
  10. Hi GReen
  11. >>> funcA('Hi')
  12. Hi Default
  13. >>> funcB('Hi')
  14. Traceback (most recent call last):
  15. File "<input>", line 1, in <module>
  16. TypeError: funcB() takes exactly 2 arguments (1 given)
  17. >>>
Hope that clears it up a bit.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 70
Reputation: shoemoodoshaloo is an unknown quantity at this point 
Solved Threads: 2
shoemoodoshaloo shoemoodoshaloo is offline Offline
Junior Poster in Training

Re: Dictionary and Sub-dictionary help

 
0
  #8
Jun 18th, 2009
Thanks for all your help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 354 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC