Function to export *.comm files #65

Merged
usov_i merged 4 commits from JakHolzer-comm_exprt into det1d 2020-09-15 14:24:34 +02:00
usov_i commented 2020-09-10 15:18:17 +02:00 (Migrated from gitlab.psi.ch)

Created by: JakHolzer

*Created by: JakHolzer*
usov_i commented 2020-09-11 11:06:37 +02:00 (Migrated from gitlab.psi.ch)

Created by: ivan-usov

Here it's important to use context manager (the 'with' statement), otherwise, in case of exception the file won't be closed.
Something like that will look better:

with open(str(path+name+extension), "w") as out_file:
    for keys in data["Measurements"]:
    ....

The line out_file.close() is not needed.

*Created by: ivan-usov* Here it's important to use context manager (the 'with' statement), otherwise, in case of exception the file won't be closed. Something like that will look better: ``` with open(str(path+name+extension), "w") as out_file: for keys in data["Measurements"]: .... ``` The line `out_file.close()` is not needed.
usov_i commented 2020-09-11 15:04:43 +02:00 (Migrated from gitlab.psi.ch)

Created by: ivan-usov

These lines should be removed, while path and name should become a function argument (probably, a single one).

*Created by: ivan-usov* These lines should be removed, while `path` and `name` should become a function argument (probably, a single one).
usov_i commented 2020-09-11 15:38:15 +02:00 (Migrated from gitlab.psi.ch)

Created by: JakHolzer

Deleted, changed to path argument which should be str(path+name) - or wold you rather pass it as a touple?

*Created by: JakHolzer* Deleted, changed to path argument which should be str(path+name) - or wold you rather pass it as a touple?
usov_i commented 2020-09-11 15:47:11 +02:00 (Migrated from gitlab.psi.ch)

Created by: JakHolzer

done :)

*Created by: JakHolzer* done :)
usov_i commented 2020-09-11 18:13:38 +02:00 (Migrated from gitlab.psi.ch)

Created by: ivan-usov

You can use str.rjust function instead (https://docs.python.org/3/library/stdtypes.html#str.rjust)

*Created by: ivan-usov* You can use `str.rjust` function instead (https://docs.python.org/3/library/stdtypes.html#str.rjust)
usov_i commented 2020-09-11 18:20:00 +02:00 (Migrated from gitlab.psi.ch)

Created by: ivan-usov

In fact, there are similar padding options in str.format() function. Basically, all of the following have similar output:

fill_string('asdf', 10)
'asdf'.rjust(10)
'{:>10}'.format('asdf')

But I would also suggest you to have a look at f-strings, which appeared in python/3.6. They could be even easier to use in this case.

*Created by: ivan-usov* In fact, there are similar padding options in `str.format()` function. Basically, all of the following have similar output: ``` fill_string('asdf', 10) 'asdf'.rjust(10) '{:>10}'.format('asdf') ``` But I would also suggest you to have a look at f-strings, which appeared in python/3.6. They could be even easier to use in this case.
usov_i commented 2020-09-11 18:26:59 +02:00 (Migrated from gitlab.psi.ch)

Created by: ivan-usov

This code needs to be deduplicated.

if data["meta"]["indices"] == "hkl":
    extension = ".comm"
elif data["meta"]["indices"] == "real":
    extension = ".incomm"

with open(str(path + extension), "w") as out_file:
    for keys in data["Measurements"]:
        if extension == ".comm":
            meas_number_str = fill_string(keys[1:], 6)
            ...
        else:
            meas_number_str = fill_string(keys[1:], 4)
            ...
etc.

Or something like that, probably the final form will be different depending on the approach you choose with the string interpolation.

*Created by: ivan-usov* This code needs to be deduplicated. ``` if data["meta"]["indices"] == "hkl": extension = ".comm" elif data["meta"]["indices"] == "real": extension = ".incomm" with open(str(path + extension), "w") as out_file: for keys in data["Measurements"]: if extension == ".comm": meas_number_str = fill_string(keys[1:], 6) ... else: meas_number_str = fill_string(keys[1:], 4) ... etc. ``` Or something like that, probably the final form will be different depending on the approach you choose with the string interpolation.
usov_i commented 2020-09-15 09:34:58 +02:00 (Migrated from gitlab.psi.ch)

Created by: JakHolzer

I deduplicated the code and used the fstrings to do the padding

*Created by: JakHolzer* I deduplicated the code and used the fstrings to do the padding
usov_i commented 2020-09-15 14:24:27 +02:00 (Migrated from gitlab.psi.ch)

Created by: ivan-usov

Review: Approved

*Created by: ivan-usov* **Review:** Approved
usov_i commented 2020-09-15 14:24:34 +02:00 (Migrated from gitlab.psi.ch)

Merged by: ivan-usov at 2020-09-15 12:24:34 UTC

*Merged by: ivan-usov at 2020-09-15 12:24:34 UTC*
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: zebra/pyzebra#65
No description provided.