User Tools

Site Tools


other:python:misc_by_jyp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
other:python:misc_by_jyp [2024/08/27 14:47] – [String formatting] Improved the section jypeterother:python:misc_by_jyp [2026/09/02 11:30] (current) – [Working with paths and filenames] Added platformdirs jypeter
Line 9: Line 9:
 Only **when you have already read all the content of this page several times**, and you are looking for new ideas Only **when you have already read all the content of this page several times**, and you are looking for new ideas
  
 +  * [[https://medium.com/data-science/calculating-distance-between-two-geolocations-in-python-26ad3afe287b|Calculating distance between two geo-locations in Python]]:
 +    * ''[[https://github.com/mapado/haversine|haversine]]'', ''[[https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html|haversine_distances]] @ scikit-learn'' and [[https://en.wikipedia.org/wiki/Haversine_formula|Haversine formula]]
 +  * Looking at table data with ''pandas''
 +    * [[https://blog.devgenius.io/data-profiling-in-python-common-ways-to-explore-your-data-part-1-0efd0dedff75|Summary information]]
 +    * [[https://blog.devgenius.io/data-profiling-in-python-common-ways-to-explore-your-data-part-2-396384522e91|More detailed information]]
 +    * [[https://blog.devgenius.io/data-cleansing-in-python-common-ways-to-clean-your-data-3459a256dd85|Table data cleaning]]
 +  * Stats stuff
 +    * [[https://medium.com/@tubelwj/python-outlier-detection-iqr-method-and-z-score-implementation-8e825edf4b32|Python Outlier Detection: IQR Method and Z-score Implementation]]
 +  * [[https://medium.com/pythons-gurus/clean-code-in-python-good-vs-bad-practices-examples-2df344bddacc|Clean Code in Python: Good vs. Bad Practices Examples]]
 +  * [[https://medium.com/javarevisited/10-popular-clean-code-practices-that-do-more-harm-than-good-5ddcf7db90f8|10 Popular Clean Code Practices That Do More Harm Than Good]]
 +  * [[https://peps.python.org/pep-0008/|PEP 8 – Style Guide for Python Code]]
 +    * [[https://realpython.com/python-pep8/|How to Write Beautiful Python Code With PEP 8]]
 +    * [[https://www.datacamp.com/tutorial/pep8-tutorial-python-code|PEP-8 Tutorial: Code Standards in Python]]
 +    * Some checkers/linters: [[https://docs.astral.sh/ruff/|ruff]], [[https://flake8.pycqa.org/en/stable/|flake8]]
   * [[https://medium.com/@yaduvanshineelam09/ultimate-python-cheat-sheet-practical-python-for-everyday-tasks-8a33abc0892f|Ultimate Python Cheat Sheet: Practical Python For Everyday Tasks]]   * [[https://medium.com/@yaduvanshineelam09/ultimate-python-cheat-sheet-practical-python-for-everyday-tasks-8a33abc0892f|Ultimate Python Cheat Sheet: Practical Python For Everyday Tasks]]
   * [[https://medium.com/pythoneers/16-hacks-that-will-take-your-python-skills-to-the-next-level-12e7a9b97421|16 Hacks That Will Take Your Python Skills to the Next Level]]   * [[https://medium.com/pythoneers/16-hacks-that-will-take-your-python-skills-to-the-next-level-12e7a9b97421|16 Hacks That Will Take Your Python Skills to the Next Level]]
 +  * [[https://python.plainenglish.io/a-practical-guide-to-python-enum-for-developers-5b6440e90da7|A Practical Guide to Python Enum for Developers]]
 +  * [[https://levelup.gitconnected.com/modular-coding-in-python-finally-solve-your-import-errors-af2fd172fcf7|Modular Coding in Python: Finally Solve your Import Errors]] (understanding and fixing ModuleNotFoundError and ImportError)
 +  * [[https://medium.com/@moraneus/understanding-multithreading-and-multiprocessing-in-python-1ed39bb078d5|Understanding Multithreading and Multiprocessing in Python]]
 ===== Reading/setting environments variables ===== ===== Reading/setting environments variables =====
  
Line 31: Line 48:
  
  
 +===== Using log files (aka logging) =====
 +
 +It is always possible to display information messages using the ''print()'' command, but it is more efficient to use //logging// tools when you want to **display correctly a lot of information about a script progress
 +**
 +  * [[https://loguru.readthedocs.io/|Loguru]] is a library which aims to bring enjoyable logging in Python
 +    * See also [[https://betterstack.com/community/guides/logging/loguru/|A Complete Guide to Logging in Python with Loguru]]
 +  * More on [[https://betterstack.com/community/guides/logging/#python|logging with python]]
 +  * The default (but not easy to use) Python ''[[https://docs.python.org/3/library/logging.html|logging]]'' module
 ===== Stopping a script ===== ===== Stopping a script =====
  
Line 111: Line 136:
   * [[https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals|Formatted String Literals]] (//f-strings//)   * [[https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals|Formatted String Literals]] (//f-strings//)
     * Available in Python >= 3.6     * Available in Python >= 3.6
 +    * [[https://python.plainenglish.io/python-f-strings-7-tricks-you-didnt-know-782ac8dab0cb|Python f-Strings: 7 Tricks You Didn’t Know]]
     * [[https://docs.python.org/3/reference/lexical_analysis.html#f-strings|More documentation]]     * [[https://docs.python.org/3/reference/lexical_analysis.html#f-strings|More documentation]]
     * [[https://docs.python.org/3/library/string.html#formatspec|Format Specification Mini-Language]]     * [[https://docs.python.org/3/library/string.html#formatspec|Format Specification Mini-Language]]
Line 158: Line 184:
     * [[https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module|Matching pathlib, and os or os.path functions]]     * [[https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module|Matching pathlib, and os or os.path functions]]
   * [[https://docs.python.org/3/library/shutil.html|shutil]]: High-level file operations, e.g copy/move a file or directory tree   * [[https://docs.python.org/3/library/shutil.html|shutil]]: High-level file operations, e.g copy/move a file or directory tree
 +  * [[https://platformdirs.readthedocs.io/|platformdirs]]: a Python library for determining platform-specific system directories (resolves the correct location for macOS, Windows, Linux/Unix, and Android)
  
  
Line 620: Line 647:
   * [[https://matplotlib.org/stable/gallery/text_labels_and_annotations/date.html|Date tick labels example]]   * [[https://matplotlib.org/stable/gallery/text_labels_and_annotations/date.html|Date tick labels example]]
  
 +===== Raster data and GIS stuff =====
  
 +Check the [[other:python:python:grid_proj|Dealing with grids and projections in Python]] page
 ===== Data representation ===== ===== Data representation =====
  
 A few notes for a future section or page about about //data representation// (bits and bytes) on disk and in memory, vs //data format// A few notes for a future section or page about about //data representation// (bits and bytes) on disk and in memory, vs //data format//
  
-FIXME Add parts (pages 28 to 37) of this [[http://www.lsce.ipsl.fr/Phocea/file.php?class=page&file=5/pythonCDAT_jyp_2sur2_070306.pdf|old tutorial]] to this section+FIXME Add parts (pages 28 to 37) of this {{:other:python:pythoncdat_jyp_2sur2_070306.pdf|old tutorial}} to this section
  
 ==== Base notions ==== ==== Base notions ====
Line 786: Line 815:
  
   * Getting the binary representation of a string   * Getting the binary representation of a string
-    * <code>>>> test_string = 'A B 0 1 à µ'+    * <code python>>>> test_string = 'A B 0 1 à µ'
 >>> type(test_string) >>> type(test_string)
 <class 'str'> <class 'str'>
Line 802: Line 831:
 </code> </code>
  
 +===== Debugging... =====
 +
 +Some resources that you can use in the unlikely case that <del>you</del> some AI has introduced <del>features</del> bugs in your code
 +
 +==== Built-in tools ====
 +
 +Read the documentation of:
 +  * [[https://docs.python.org/3/library/pdb.html|pdb, The Python Debugger]]
 +  * and the other built-in [[https://docs.python.org/3/library/debug.html|Debugging and Profiling tools]]
 +
 +==== Using a decorator to log function calls ====
 +
 +Check the example in [[https://blog.devgenius.io/my-lazy-secret-to-cleaner-code-python-decorators-d43d673a1ba2|My Lazy Secret to Cleaner Code: Python Decorators]]
 +
 +Note: more about [[#using_log_files_aka_logging|using log files]]
  
 /* /*
other/python/misc_by_jyp.1724762835.txt.gz · Last modified: by jypeter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki