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 11:46] – [Playing with strings] Started a "String formatting sub-section" jypeterother:python:misc_by_jyp [2026/11/05 16:59] (current) – Added the "web forms" section 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 48: Line 73:
 ==== String formatting ==== ==== String formatting ====
  
-Knowing how to display/print a string correctly is always useful for information and debugging purpose, and there are lots of different ways to display strings+  * Knowing how to display/print a string correctly is always useful for information and debugging purpose 
 +  * There are lots of different ways to display strings 
 + 
 +=== String formatting examples === 
 + 
 +You will find below some examples of //quick printing//, as well as using //old style formatting//, //formatted string literals (f-strings)// and the //String ''format()'' Method//. More details in the next section
  
 <code python> <code python>
 +>>> # Basic (but quick and efficient) printing
 +
 >>> year = 1984 >>> year = 1984
 >>> print(year) >>> print(year)
Line 80: Line 112:
 >>> print(f'[ {year:010} is a famous book ]') >>> print(f'[ {year:010} is a famous book ]')
 [ 0000001984 is a famous book ] [ 0000001984 is a famous book ]
 +>>> print(f'[ {year:10.2f} is a famous book (yes, {year}!) ]')
 +[    1984.00 is a famous book (yes, 1984!) ]
  
 >>> # The String format() Method >>> # The String format() Method
  
 +>>> print('[ {} is a famous book ]'.format(year))
 +[ 1984 is a famous book ]
 +>>> print('[ {:10} is a famous book ]'.format(year))
 +[       1984 is a famous book ]
 +>>> print('[ {:<10} is a famous book ]'.format(year))
 +[ 1984       is a famous book ]
 >>> print('[ {:010} is a famous book ]'.format(year)) >>> print('[ {:010} is a famous book ]'.format(year))
 [ 0000001984 is a famous book ] [ 0000001984 is a famous book ]
->>> print('[ {:010d} is a famous book ]'.format(year)) +>>> print('[ {:10.2f} is a famous book  (yes, {}!) ]'.format(year, year)) 
-[ 0000001984 is a famous book ] +[    1984.00 is a famous book  (yes, 1984!) ] 
->>> print('[ {:10d} is a famous book ]'.format(year)) +>>> print('[ {title:10.2f} is a famous book  (yes, {title}!) ]'.format(title=year)) 
-[       1984 is a famous book ] +[    1984.00 is a famous book  (yes, 1984!) ] 
->>> print('[ {:10f} is a famous book ]'.format(year)) +>>> print('[ {title:10.2e} is a famous book ]'.format(title=year)) 
-[ 1984.000000 is a famous book ] +[   1.98e+03 is a famous book ]</code> 
->>> print('[ {:10.2f} is a famous book ]'.format(year)) + 
-[    1984.00 is a famous book ]</code>+=== String formatting references === 
 + 
 +  * [[https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals|Formatted String Literals]] (//f-strings//) 
 +    * 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/library/string.html#formatspec|Format Specification Mini-Language]] 
 +      * See also the [[https://pyformat.info/|PyFormat site]] 
 + 
 +  * [[https://docs.python.org/3/tutorial/inputoutput.html#the-string-format-method|The String format() Method]] 
 +    * [[https://docs.python.org/3/library/string.html#formatspec|Format Specification Mini-Language]] 
 +      * See also the [[https://pyformat.info/|PyFormat site]]
  
 +  * [[https://pyformat.info/|PyFormat site]]: string formatting using the //old style// and the //String ''format()'' method//
 +    * <wrap hi>Hint</wrap>: this can also be used as an **easy documentation for //f-strings// format**!
  
 +  * [[https://docs.python.org/3/tutorial/inputoutput.html#the-string-format-method|Old string formatting]]
 ==== Splitting (complex) strings ==== ==== Splitting (complex) strings ====
  
Line 130: 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 592: 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 758: 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 774: 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]]
 +
 +
 +===== web stuff =====
 +
 +==== Filling forms with python ====
 +
 +  * [[https://www.scaler.com/topics/cyber-security/attack-webforms-beautifulsoup-requests-python/|Attack Webforms with Beautifulsoup and Requests in Python]]
 +  * [[https://scrapeops.io/python-web-scraping-playbook/python-requests-how-to-submit-forms/|How To Submit Forms With Python Requests]]
  
 /* /*
other/python/misc_by_jyp.1724751978.txt.gz · Last modified: by jypeter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki