image2csv icon indicating copy to clipboard operation
image2csv copied to clipboard

General code enhancement and new ideas

Open thibaultserti opened this issue 3 years ago • 1 comments

I saw that you are new to coding, so welcome to GitHub !

I listed a few things that you could easily change to improve the quality of your code:

  • Use f-string instead of ugly concatenation like print("[INFO] End of OCR, found "+str(NbError)+" errors out of "+str(len(ROI))+" regions...")

    • It will look instead like : print(f"[INFO] End of OCR, found {str(NbError)} errors out of {str(len(ROI))} regions...")
    • It's new in Python 3.6
    • See https://www.python.org/dev/peps/pep-0498/
  • Use logging module instead of print("[INFO"] ...)

    • You can use logging.info() to print a message with the info level
    • You can add an argument to your CLI (that means command line tool), to set the verbosity level (info, warning, error, critical, ...)
    • See https://docs.python.org/3/howto/logging.html#logging-basic-tutorial
  • Use a standard docstring notation like the one of google or the one of numpy:

    • The Vscode extension Python Docstring Generator can generate the docstring based of the prototype function. You can specify if you want the numpy or the google style in the extension configuration.
    • See https://numpydoc.readthedocs.io/en/latest/format.html for numpy style (I suggest you this one)
    • See https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html for google style
  • DO NOT write the .gitignore yourself

    • websites exist to generate gitignore corresponding to your project such as https://gitignore.io/
    • just write the technology you're using (for example vscode and python here)
  • In the requirements.txt, always precise the package version you're using

    • for example opencv-python==4.5.1.48
    • to do so the easiest way is to:
      • create a virtualenv pip3 install virtualenv
      • create such a virtualenv in your project by executing python3 -m venv .venv
      • activate the virtualenv venv\Scripts\activate (on Windows)
      • make the pip install -r requirements.txt
      • finally you can get the version you installed by executing pip freeze and put this in your requirements.txt instead
    • to work with a virtualenv is a very good habit to take because you're sure to use the good versions of your project
    • see https://www.tutorialspoint.com/python-virtual-environment
  • You should use a linter to keep the way you're coding consistent.

    • In Vscode, which you seem to be using, it's very easy.
    • I suggest you the black linter which can be installed by pip install black
    • If you press Shift+Ctrl+P in Vscode and you type format document, Vscode will propose you the install of black. After that you can format the document by pressing the keyboard shortcut Shift+Ctrl+I
    • image
    • See https://pypi.org/project/black/
  • In Vscode, use the Python extension but also the Visual Studio IntelliCode and Pylance

    • It has a AI support to check your code statically, and to report error in your code.
    • It can suggest you the method name based on what you're typing, print help snippet to tell you what are the args of the function, what it returns, ...
    • Screenshot from 2021-02-04 09-10-41
    • Try it, it's :heart: !
  • Small thing, but put a LICENSE to your project (if you want it to be free, MIT License is a good choice)

  • If you want to go further in devellopping pattern, you can write test for your code.

    • you could easily test with the pytest framework
    • here it's pretty easy to make functionnals tests. Run your code on sample picture and compare the output csv with a reference csv which contains the picture data. Do this on some sample images.
    • See https://docs.pytest.org/en/stable/
    • See https://openclassrooms.com/fr/courses/6100311-testez-votre-code-java-pour-realiser-des-applications-de-qualite/6616481-decouvrez-les-tests-dintegration-et-les-tests-fonctionnels (it's in a Java tutorial but there is no Java here, it's only the principles)

Be sure that it's all about the form and not about the content. The goal of this remarks is simply to make you aware of the existence of such things (ignored by far too many programmers and teachers) and to step up your code quality :smile:

thibaultserti avatar Feb 04 '21 08:02 thibaultserti

Thank you so much for these directions ! In fact, it is quite difficult to have some quality codes when beginning, there are a lot of things to know in order to make a script simple and clear, for both the user and the developer... So I'll try to follow your advices as much as I can for my future projects, and of course starting by improving this program. 😉

Moreover, I'll leave this issue as opened if you or someone else has other tip to share 😁

artperrin avatar Feb 04 '21 16:02 artperrin