Ability to not throw exceptions for un-declared Dataset attributes? #1357
-
Problem When dealing with Datasets, I'm (like many I would guess) always faced with the issue of wanting to get a tag value, but of course having to deal with handling the real-world dicom case that the tag isn't present in the Dataset. For example, some simple code to print the Accession number out of a Dataset (call it dcm):
or maybe
...you could also put the print in a try-except and probably many other ways of handling this Question Is there a more elegant way of handling this? (meaning less verbage, more concise, cleaner,...) I could of course subclass Dataset and override (Sub-classing is elegant in one sense, but a problem then if you forget to cast a Dataset to your sub-class, etc) Observation The 'reverse' is true for Datasets (or any class object) - you can assign to a (previously) un declared attribute without any problems:
Idea Would it be reasonable to have a config or ability in Dataset's to have the default lookup of a un-declared attribute return |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 3 replies
-
There is
That's the purpose of the get method, e.g.:
|
Beta Was this translation helpful? Give feedback.
-
There is a couple of caveats with using |
Beta Was this translation helpful? Give feedback.
-
That's a good point - and thus the For printing summaries of files, I've often used this kind of pattern:
for any values that might not be guaranteed to be there (and even some that might be required, if the file is not conformant). Checking for non-existent or null values is always a pain... |
Beta Was this translation helpful? Give feedback.
-
It is tempting, but this would not be standard Python behavior (explicit is better than implicit). However, I suppose if it threw an error for non-DICOM-keywords to catch typos, then it wouldn't be much different than how the |
Beta Was this translation helpful? Give feedback.
-
@richard-moss, note I have moved the issue to a Discussion ... it looks like email notifications show the issue as closed without noting that, so pinging you just in case... |
Beta Was this translation helpful? Give feedback.
-
Great thanks Darcy - I wasn’t sure where to post it
… On Apr 16, 2021, at 12:50 PM, Darcy Mason ***@***.***> wrote:
@richard-moss, note I have moved the issue to a Discussion ... it looks like email notifications show the issue as closed without noting that, so pinging you just in case...
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
To be honest I hadn't realized there was @darcymason You're right - this is probably what I want to be using, as it fits the use case. You're right, I could use Actually the behaviour of returning None even for non-dicom elements is actually good for me as well. |
Beta Was this translation helpful? Give feedback.
There is
in
, shown just below, orget
further below:That's the purpose of the get method, e.g.:
get
returnsNone
by default, but you could supply an alternate value