0

In a python project that I collaborate, we're intending initially to parse information from a input fasta file into a dictionary.

Parsing method is already implemented (here and here), and the problem is: code works fine when running in Python3 (fasta file is loaded, its information is parsed for FDB data-strucuture, and then it's saved in a new fdb-file), but when it runs in Python 2, generated dictionary doesn't contains value-information from read fasta file, just the keys.

Links above show code developed for parsing, and block below contains test we execute (which works fine with Python 3 but not save fasta information in Python 2).

print("Instantiating a FastaDB object...")
fasta_db = FastaDB()
print("Defining input file name...")
filename = "../FastaDB/test2.fasta"
username = "inacio_medeiros"
print("Invoking FDB parsing...")
parsed_fdb_structure = fasta_db.ImportFasta(filename, username)
print("Saving in file...")
content = json.dumps(parsed_fdb_structure)
fdb_file_name = filename+".fdb"
fdb_file = open(fdb_file_name, "w")
fdb_file.write(content)

Does anyone have an idea why dictionaries are working fine in Python 3, but not in Python 2?

  • Complementing this question, there's an [open issue](https://github.com/vmesel/FastaDB/issues/10) about it in project. – Inácio Medeiros Jul 18 '16 at 15:41
  • What's the Python 2 version are you using? – be_good_do_good Jul 18 '16 at 15:45
  • 2.7.9 (and 3.4.3 for Python 3) – Inácio Medeiros Jul 18 '16 at 20:57
  • When running in python 2, is it giving some error? Does parsed_fdb_structure contain any special type of characters? – be_good_do_good Jul 19 '16 at 05:47
  • No, it doesn't give any errors. The only problem is that this structure comes "empty of values". Follows an example: [{'description': '', 'filename': '', 'user': '', 'geneinfo': '', 'date': '2016-07-14', 'gene': '', 'observations': ''}] "values" part of dictionary, when program runs in Python 3, comes filled with data, but in Python 2 doesn't. – Inácio Medeiros Jul 19 '16 at 15:06

1 Answers1

2

The problem is not the dictionary, but how classes were created. While on python3 all classes inherit from object class (unless u actually make it inherit from other class), on python2 they dont.

Hence, class A() on python3 is the same as class A(object), but on python2 they are different things: the latter is a "new style class", while the former is an "old style class". I'm a python3 guy too, so this is new for me, but u can find more information on this SO thread

TL;DR: just replace class FDBRegister(): for class FDBRegister(object): and it will work! I tested here ;)

Community
  • 1
  • 1