HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Citations of images used in an app

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
imagescitationsusedapp

Problem

I'm using the following code in order to cite the images used in my android apps. My main concern is to never accidentally present an image as if it were mine, while it's not.
Questions:

-
The images I created are separately stored from third parties' images. The code in ignore_build_ensure_images_cited.py is run every time main.py is executed during development. The name intentionally contains "ignore_build" in order to ignore it when building the apk, since it's only needed for developing. Should I be doing this a different way, that is, by not ignoring the file?

-
I tend to name files, variables, classes etc in a perhaps strange way. For example instead of using "third_party_images" I use "third_parties_images", in order to indicate that the dir is related to multiple parties and their images. If it were "party" that would indicate the dir is related to a single party with multiple images. (asking in general about relevant coding practices, not specifically about this post's code)

  • Is there a better way to express such info through naming?



  • Is there any downside to using such a naming convention?



  • What else could I do in order to make it even more reliable in terms of not forgetting to attribute, and attributing correctly (e.g. not forgetting to set adaptation=True if I've modified the image)?



Directory tree

  • project_dir



  • main.py



  • third_parties_images



  • own_images



  • citations.py



  • ignore_build_ensure_images_cited.py



main.py

(this is just the bottom of the file)

if __name__ == '__main__':

    try:
        import ignore_build_ensure_images_cited
    except ImportError:
        pass

    MinusTimesMinusApp().run()


ignore_build_ensure_images_cited.py

```
import os
import citations

INCLUDED_IMAGES = set(os.listdir('third_parties_images'))
CITED_IMAGES = citations.IMAGES_CITED
FILES_NOT_CITED = INCLUDED_IMAGES - CITED_IMAGES
if INCLUDED_IMAGES - CITED_IMAGES:
raise NotImplementedError('Following image files were not cited:

Solution

Is there a better way to express such info through naming?

Make it grammatically correct, so just say third_party_images. Maintaining "ies" would require an apostrophe, which isn't possible in variable names. Making the conjugation goofy as an internal convention is not helpful. Any additional information can be added in your documentation - docstrings or otherwise.


Is there any downside to using such a naming convention?

Yes. The convention isn't obvious to someone new to your project, so doesn't convey any additional information, and it's grammatically incorrect, which makes people sad.

Other than that,

Type hints

Your convention to add (str) in the docstring instead of :str on the parameter is good if you're trapped in Python 2. But if you're at all able to use Python 3 (the tags don't specify), just use type hints.

Past that, your docstrings are missing descriptions for some parameters.

Concatenation or formatting?

Your approach in full_text is fine. You may also choose to avoid concatenation by setting up snippet variables that are unconditionally added to the format string, so that there only needs to be one format call; something like

adaptation = 'My adaptation of ' if self.adaptation else ''
extra_text = f'\n{self.extra_text}' if self.extra_text else ''

final_text = (
    '[size=12]'
    f'{adaptation}[b]{self.work_name}[/b] '
    f'image by {self._creator} '
    f'({self.creation_date}). \n'
    f'[size=10]{self.url}[/size]'
    f'{extra_text}[/size]'
)


Mandatory args


To be used for copy-pasting when creating new ImageCitation
in order to avoid accidentally forgetting to change an arg value.

I can see where you're coming from, but if this is actually a problem, consider making more (or all) arguments mandatory. It's fairly unusual to leave around boilerplate code for the express purpose of copy-and-paste, and the requirement for such a thing means that perhaps

  • you're not using a Python editor/IDE with good static analysis and autocomplete, and/or



  • your method's parameters are not strict enough in requiring that values be passed.



All of that said, you shouldn't have to be passing creator_name=None so many times; that's the default. Just leave it out. The same goes for extra_text. That's the whole point of default arguments.

Code Snippets

adaptation = 'My adaptation of ' if self.adaptation else ''
extra_text = f'\n{self.extra_text}' if self.extra_text else ''

final_text = (
    '[size=12]'
    f'{adaptation}[b]{self.work_name}[/b] '
    f'image by {self._creator} '
    f'({self.creation_date}). \n'
    f'[size=10]{self.url}[/size]'
    f'{extra_text}[/size]'
)

Context

StackExchange Code Review Q#146023, answer score: 3

Revisions (0)

No revisions yet.