Electron microscopy
 
Tricks in Python Programming,
and Principles and Practices in Good programming
- Python Automation and Machine Learning for ICs -
- An Online Book -
Python Automation and Machine Learning for ICs                                        http://www.globalsino.com/ICs/        


Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

=================================================================================

Several important principles and practices in good programming are: 

  1. Defensive Programming: 

    This involves writing code with the assumption that errors will occur, and taking steps to handle those errors carefully. This can include input validation, error checking, and robust error handling to ensure that the program behaves predictably even in unexpected situations. 

    Write specifications for functions and use  as many functions as we can.

    Modularize programs. 

    Check conditions on inputs and outputs    

  2. Testing: 

    Testing is a crucial aspect of software development. It involves systematically checking various parts of a program to ensure that it behaves as expected. This can include unit testing, integration testing, and system testing. Testing helps catch bugs early in the development process and ensures that changes to the code don't introduce new issues. 

    Testing includes black-box testing and white-box testing:  

    2.a) Black-Box Testing: 

    In black-box testing, the tester examines the functionality of a software component without being concerned with its internal code structure or implementation details. Testers focus on inputs and expected outputs, treating the software as a "black box" whose internal workings are not known or relevant to the testing process. This type of testing is often used to assess the software's functionality, usability, and external behavior. 

    2.b) White-Box Testing: 

    In contrast, white-box testing involves examining the internal logic, structure, and code of the software. Testers with knowledge of the internal workings of the system design test cases based on the code's structure. The goal is to ensure that all code paths are executed and that the code behaves as expected. White-box testing is also known as structural, clear-box, or glass-box testing.  

  3. Validation: 

    Validation involves checking whether the input to a program or a function is valid and meets the expected criteria. This is closely related to defensive programming, as it helps prevent incorrect or malicious inputs from causing problems. 

    Compare input and output pairs to specification 

  4. Debugging

    Debugging is the process of finding and fixing errors or bugs in the code. Even with defensive programming and testing, it's almost inevitable that some issues will arise. Effective debugging is crucial for identifying and resolving these issues efficiently. 

    Study events leading up to errors. 

  5. Good practice and bad practice:

    5.a) Bad practice: 

    A traditional and potentially problematic approach to software development is known as the "Waterfall Model." This model involves completing each phase of development sequentially, with testing and debugging occurring only after the entire program is written. While this approach may seem straightforward, it has several drawbacks: 

    • Late Detection of Issues: 

      Testing and debugging at the end of the development process means that issues are only identified after a significant amount of code has been written. This can make it more challenging to isolate and fix problems, potentially leading to delays in the project timeline. 

    • Difficulty in Identifying Root Causes: 

      Debugging the entire program at once can make it difficult to pinpoint the root causes of issues, especially if there are numerous interactions between different parts of the code. This can result in a time-consuming and less efficient debugging process. 

    • Increased Risk of Large-Scale Changes: 

      If significant issues are discovered during testing or debugging, making corrections may require extensive changes to the codebase, potentially impacting other parts of the program and introducing new issues. 

    • Limited Flexibility: 

      The Waterfall Model is less flexible and adaptive to changing requirements. If new insights or requirements emerge during the development process, accommodating these changes can be challenging without disrupting the entire workflow.

    5.b): Good practice:

    A more modular and incremental approach to software development is generally considered a good practice:  

    • Write a function: 

      Breaking down a program into smaller, modular components (functions) promotes code reusability, readability, and maintainability. Each function should have a well-defined purpose, making it easier to understand and test. 

    • Test the function, debug the function: 

      Before moving on to other parts of the code, it's crucial to thoroughly test the individual functions. This includes writing test cases to check if the function behaves as expected under various conditions. Debugging at this stage allows you to identify and fix any issues in the function's logic or implementation. 

    • Do integration testing: 

      Integration testing involves verifying that the individual components (functions) work together as expected when integrated into the larger system. This step ensures that the interactions between different parts of the code do not introduce new issues. Integration testing can be performed incrementally as new functions are added to the system.

  6. Check something, e.g. the date of existing files in the folder, before script proceeding.

===================================================== 

Trick 14. One weird, strange, unknown problem is that the output (e.g. csv) file cannot be saved through unix/Linux in case the file is opened, which does not give permission to overwrite the existing file. Plus, the very bad thing is that the Python execution does not produce any error messages on unix. 

Trick 13. Other tricks:

Characteristics Examples
Length of string, special characters, cells/elements instead of rows or columns Length of string

Trick 12. If an module import and execution is skipped during script execution, then the following questions are needed to ask:
      i) (Most likely: Are the import module in the same folder as the script which is calling it? (page2221)
      ii) Is some files on network drive instead of local drive?
      iii) Are the scripts and script execution on the local PC?

Trick 11. Avoid duplicates when creating text file:
      a = "Yougui"
      ...
             with open(theFileOfTheSetOfImage, 'a') as f:
                    if a != Lines[i]:
                           print(a)
                           print(Lines[i])
                           f.write(Lines[i])
                           a = Lines[i]
                           # f.write('\n')         

Trick 10. If you spent too much time on one issue, then think about this possibility: "Can this file be treated as a text file?". For instance an HTML webpage example at link.

Trick 9. Call "everything" with {}: code:          
          Search a window
This is a "HTML" replacement of html format below:         
         Search a window         

Trick 8. vim and PowerShell for Python. (see page4414).

Trick 7. Failure of log in or authorized data downloading, then increasing the number of attempts if the username and password are really correct (the next try's will be ignored if an earlier try passes):
             try:
                 Action codes ...
             except:
                 try:
                     Action codes ...
                 except:
                     try:
                         Action codes ...
                    except:
                        try:
                            Action codes ...
                       except:
                           Action codes ...

Trick 6. Think if the lines can be skipped or not if using an existing/commercial script (e.g. obtained from WWW) and the function of the script lines are not needed.

Trick 5: Extend the length of a box: just add space into it:
         
         

Trick 4: The data is removed/overwritten by "return function", then use stairing if loop instead (code):
         
    Have problems when use:   
         

Trick 3: Think about adding some (or more) time by using time.sleep() if there is any skip of script execution or not-working of scripts.

Trick 2: Only import functions and/or modules when they are needed; otherwise, they can induce conflictions or disable some functions.
          One example is application of ".locateOnScreen()".

Trick 1: Cannot sort files by date in File Explorer, then create or move the file in a fixed folder and then directly read the file from there.          
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         

 

 

 

 

 

 

 

 

 

 

=================================================================================