DanPorter / Dans_Diffraction

Reads crystallographic cif files and simulates diffraction
Apache License 2.0
45 stars 13 forks source link

Python3 support #2

Closed hepesu closed 6 years ago

hepesu commented 6 years ago

Hi Dan,

How about support python 3?

I did a simple migration with 2to3.py and some manual work. After running some examples, it seems that this project can be migrated to python 3 smoothly.

If you are going to support python 3, I can create a pull request.

Attachment is the log of migration. Most unsupported codes are 'print'.

migration_log.txt

Hepesu

DanPorter commented 6 years ago

Hi Hepesu,

Yes that is something I have been meaning to do. When I wrote the code I did it with Python3 partly in mind, but I have never tested it!

There shouldn't be too much to change as you say, so please do go for the pull request!

DanPorter commented 6 years ago

Hi again,

I've made a few changes and some basic tests I've done seems to suggest that it works now in python3.

The biggest annoyance is the change in encoding in np.genfromtxt used in functions_crystallography.atomic_properties, as in python3 this function produces byte strings that are not comparable to normal strings, meaning it wasn't possible to get atomic data. I've put a hack in place that decodes the byte strings but it keeps on showing an annoying depreciation warning, so I'll have to find another way of doing it.

I'll keep on testing it but let me know if you see any bugs.

hepesu commented 6 years ago

Hi,

Sorry for the delayed response. I am a little busy these days, so I have not created the pull request. I met that error which caused by difference in file reading between 2 and 3. And I have fixed this with code below. The main change is to decode the atom(name)`s btye string to string, and other is ok.

   # elements must be a list e.g. ['Co','O']
    elements = np.char.lower(np.asarray(elements).reshape(-1))

    atomfile = os.path.join(datadir, 'Dans Element Properties.txt')
    file = open(atomfile, 'rb')
    data = np.genfromtxt(file, skip_header=5, dtype=None, names=True)
    file.close()

    if elements is not None:
        indx = [None] * len(elements)
        for n in range(len(data)):
            d = data[n]
            # d['Element'] decode here
            if d['Element'].decode().lower() in elements:
                for m in range(len(elements)):
                    if d['Element'].decode().lower() == elements[m]:
                        indx[m] = n
        data = data[indx]

    if fields is None:
        return data

    return data[fields]

I will create the request soon.

DanPorter commented 6 years ago

Thanks for the help! I've added the .decode() statements and it turns out the annoying messages were coming up because I didn't include 'rb' in the open command.

I've just done a fairly large commit with lots of small changes designed to make the code work in different environments. I can now get everything to work in python 2.7 and 3.5 (as far as I've tested anyway!)

hepesu commented 6 years ago

Cheers!