cheat_sheets_python
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cheat_sheets_python [2022/12/24 18:14] – [Python] gman | cheat_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 initially. Then, if I need more detail and definition, I got to the official Python doc site (which, honestly, I find difficult to navigate). | + | Python' |
+ | **Documentation: | ||
- | **W3 Schools:** [[https:// | + | **W3 Schools:** |
+ | | ||
+ | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
**Python Site:** [[https:// | **Python Site:** [[https:// | ||
Line 17: | Line 22: | ||
# | # | ||
</ | </ | ||
+ | |||
+ | ---- | ||
+ | ===== Virtual Environment ===== | ||
+ | |||
+ | Create a new virtual environment for // | ||
+ | * 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: | ||
+ | |||
+ | **Create a Virtual Environment: | ||
+ | |||
+ | < | ||
+ | $ 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/ | ||
+ | # runs the script to activate your virt env | ||
+ | # your prompt should change: prefixed by name of venv | ||
+ | |||
+ | $ deactivate | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Files: Reading From ===== | ||
+ | |||
+ | Use the following syntax when opening and reading a file into a Python program: | ||
+ | |||
+ | < | ||
+ | with open(' | ||
+ | contents = file_object.read() | ||
+ | print( contents.rstrip() ) | ||
+ | </ | ||
+ | |||
+ | * The '' | ||
+ | * The [[https:// | ||
+ | * You could '' | ||
+ | * The structure above allows Python to automagically close the file when the time is right. | ||
+ | * The '' | ||
+ | * Note: '' | ||
+ | |||
+ | **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. | ||
+ | |||
+ | < | ||
+ | # A relative file path starts in the directory in which you started Python | ||
+ | with open(' | ||
+ | |||
+ | # An absolute file path (b/c they are long, store them in a variable first) | ||
+ | file_path = '/ | ||
+ | with open(file_path) as file_object: | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
===== Web Server ===== | ===== Web Server ===== | ||
Line 53: | Line 120: | ||
---- | ---- | ||
- | ===== Check: File Load ===== | + | ===== Main Block ===== |
- | Runs main() if file was not imported: | + | Runs '' |
< | < | ||
Line 62: | Line 129: | ||
</ | </ | ||
- | ---- | + | By including this '' |
+ | | ||
+ | | ||
+ | * Since '' | ||
+ | * 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