The gMan nixWiki

Because the mind is made of Teflon...

User Tools

Site Tools


cheat_sheets_python

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
cheat_sheets_python [2022/12/24 18:14] – [Python] gmancheat_sheets_python [2023/01/18 01:24] (current) – [Main Block] gman
Line 1: Line 1:
 ====== Python ====== ====== Python ======
  
-I find the W3School site to be more helpful initiallyThen, if I need more detail and definition, I got to the official Python doc site (which, honestly, I find difficult to navigate).+Python's **style guide** can be found [[https://peps.python.org/pep-0008/ | here]] (PEP 8).
  
 +**Documentation:** I find the W3School site to be more helpful initially. Then, if I need more detail and definition, I got to the official Python doc site (which, honestly, I find difficult to navigate).
  
-**W3 Schools:** [[https://www.w3schools.com/python/default.asp | Python Reference]]+**W3 Schools:*
 +  * [[https://www.w3schools.com/python/python_datatypes.asp | Python]] 
 +    * [[https://www.w3schools.com/python/python_user_input.asp | User Input]]
   * [[https://www.w3schools.com/python/python_reference.asp | Reference]]   * [[https://www.w3schools.com/python/python_reference.asp | Reference]]
 +    * [[https://www.w3schools.com/python/python_ref_functions.asp | Built-In Functions]]
     * [[https://www.w3schools.com/python/python_ref_string.asp | String Methods]]     * [[https://www.w3schools.com/python/python_ref_string.asp | String Methods]]
 +    * [[https://www.w3schools.com/python/python_ref_list.asp | List Methods]] | [[https://www.w3schools.com/python/python_lists_comprehension.asp | List Comprehensions]]
  
 **Python Site:** [[https://www.python.org/ | python.org]] **Python Site:** [[https://www.python.org/ | python.org]]
Line 17: Line 22:
 #!/usr/bin/python3 #!/usr/bin/python3
 </code> </code>
 +
 +----
 +===== Virtual Environment =====
 +
 +Create a new virtual environment for //**every**// Python project you work on ([[https://www.geeksforgeeks.org/python-virtual-environment/ | read]]). 
 +  * This way the dependencies of each project are isolated from the system and from the other projects (avoiding conflicts).
 +  * There are no limits to the number of environments you can have since they are just directories containing a few scripts. 
 +
 +**Installation:** To use virtual environments, install the ''python3-venv'' package. 
 +
 +**Create a Virtual Environment:** Make a new directory, cd into it, create the environment, and activate it.
 +
 +<code>
 +$ mkdir dir_name_whatever
 +$ cd dir_name_whatever
 +
 +$ python3 -m venv name_of_venv
 +#    -m : tells python to run the module as a script
 +#  venv : the module we want to run as a script
 +#  name : name it whatever you want
 +
 +$ source name_of_venv/bin/activate
 +#  runs the script to activate your virt env
 +#  your prompt should change: prefixed by name of venv
 +
 +$ deactivate  # exit the venv
 +</code>
 +
 +----
 +
 +===== Files: Reading From =====
 +
 +Use the following syntax when opening and reading a file into a Python program: 
 +
 +<code>
 +with open('filename.txt') as file_object:
 +    contents = file_object.read()
 +    print( contents.rstrip() )
 +</code>
 +
 +  * The ''open()'' function returns a file object (an object representing the file).
 +  * The [[https://www.w3schools.com/python/python_ref_keywords.asp | keyword]] ''with'' closes the file once access to it is no longer needed. This structure will avoid problems...
 +    * You could ''open()'' and ''close()'' your file, but if if a bug in your program prevents the ''close()'' statement, the file many never close (possible data loss or corruption).
 +    * The structure above allows Python to automagically close the file when the time is right.
 +  * The ''read()'' method reads then entire file contents and stores it as a string in ''contents''.
 +    * Note: ''read()'' will insert an empty string at EOF which will result in an extra blank line. To remove it, just ''rstrip()''.
 +
 +**File Paths:** To reference files in other subdirs, you have two options...
 +  - Relative File Path: the path to the file relative to the current working directory.
 +  - Absolute File Path: the path to the file relative to the current file system.
 +
 +<code>
 +# A relative file path starts in the directory in which you started Python
 +with open('files/text files/filename.txt') as file_object:
 +
 +# An absolute file path (b/c they are long, store them in a variable first)
 +file_path = '/home/greg/other_files/text_files/filename.txt'
 +with open(file_path) as file_object:
 +</code>
 +
 +----
 +
 ===== Web Server ===== ===== Web Server =====
  
Line 53: Line 120:
 ---- ----
  
-===== Check: File Load =====+===== Main Block =====
  
-Runs main() if file was not imported: +Runs ''main()'' if file was not imported: 
  
 <code> <code>
Line 62: Line 129:
 </code> </code>
  
----- +By including this ''if'' statement around your main program (i.e., not your imports, functions, classes, etc.), you can utilize your code in two ways:  
 +  When you run your program from the command line like normal, the program's name becomes ''__main__'' and therefore the main block is executed. 
 +  You can also import this program and its code into another program (as if it were a module or library) and have no side effects. 
 +    * Since ''__main__'' will refer to the other program into which you imported your code. Your imported program will act as a Python module and keep is own name (without the extension).  
 +    * This means everything else in your imported program (functions, classes, etc.) is available to you, but the main block will //**not**// be executed. Pretty slick
cheat_sheets_python.1671905652.txt.gz · Last modified: by gman