srdusr
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--languages/python.md211
-rw-r--r--tools/git.md179
2 files changed, 390 insertions, 0 deletions
diff --git a/languages/python.md b/languages/python.md
new file mode 100644
index 0000000..d246eb1
--- /dev/null
+++ b/languages/python.md
@@ -0,0 +1,211 @@
+## Python Programming
+#### Index
+Info
+
+
+#### Documentation
+- Official Python Documentation:
+```html
+https://docs.python.org
+```
+- Unoffical cheatsheets:
+```html
+https://www.pythoncheatsheet.org
+```
+
+- Download docs to use offline
+ - Download offical docs
+ ```bash
+ $ curl -OJL https://docs.python.org/3/archives/python-3.9.7-docs-html.tar.bz2
+ $ tar -xvf python-3.9.7-docs-html.tar.bz2
+ ```
+ - After extraction, can navigate to the extracted directory and open the index.html file in a web browser to access the offline Python documentation.
+ - or to generate the documentation for a specific Python module or package, use the following command:
+ ```bash
+ $ pydoc -w <module_name>
+
+ ```
+> NOTE: Replace <module_name> with the name of the module or package for which to generate documentation. For example, to generate documentation for the os module, can use: `pydoc -w os`. Additionally can use the `-p` option with pydoc to launch a local web server and view the documentation in the web browser. For example: `pydoc -p 5000`. This command will start a web server on port 5000, and can access the documentation by opening the browser and navigating to `http://localhost:5000`.
+
+- To view documentation in vim
+```editorconfig
+:help python
+```
+- - -
+#### Info
+- Python is an interpreted language, which means it is not compiled but executed line by line.
+- Python uses indentation (usually four spaces) to define code blocks. Indentation is important for the structure and readability of code but does greatly effect how the interpreter/runner will run it.
+- Follow proper code formatting by using consistent indentation and line breaks.
+- Statements are terminated with a newline. However, can use a backslash `\` to continue a statement to the next line.
+- Use parentheses to group expressions and clarify the order of operations.
+- Use appropriate spacing around operators and after commas to enhance readability.
+- Python follows the "PEP 8" style guide, which suggests guidelines for writing Python code. Some key points include:
+ - Use descriptive variable and function names.
+ - Use lowercase letters with words separated by underscores for variable and function names aka snake_case (e.g., my_variable, my_function).
+ - Use uppercase letters with words separated by underscores for constants (e.g., MY_CONSTANT).
+ - Use spaces around operators and after commas (e.g., x = 5 + 2, my_function(arg1, arg2)).
+ - Limit line length to 79 characters.
+- Python supports both single-quoted `'` and double-quoted `"` strings. Consitency is important.
+- Python uses zero-based indexing for lists, tuples, and strings. The first element is accessed with index 0.
+- Python allows for multiple assignment in a single line, e.g., x, y = 10, 20.
+- Avoid using single-character variable names, except in cases where the purpose is clear (e.g., loop counters).
+- Follow the DRY (Don't Repeat Yourself) principle and avoid duplicating code. Instead, use functions or loops to encapsulate and reuse code.
+- Use built-in functions and libraries whenever possible to leverage existing functionality and improve code efficiency.
+- Use meaningful names for variables, functions, and classes that reflect their purpose and intent.
+- Use snake_case naming convention for functions, variables, and modules (e.g., my_function, my_variable, my_module.py).
+- Use CamelCase naming convention for classes (e.g., MyClass).
+- Use a consistent naming convention for constants, such as using uppercase letters with underscores (e.g., MAX_VALUE).
+- Avoid using global variables whenever possible, as they can lead to code complexity and make debugging difficult.
+- Avoid unnecessary or excessive code comments that only restate the code itself.
+- Package Manger
+- Virtual Environments
+> NOTE: Use virtual environments to distinguish/seperate dependencies between various projects
+- A few popular options
+ - venv
+ - virtualenv (Simple option, this can bring portability to the code and maintain old packages as well.)
+ ```bash
+ $ virtualenv -p /usr/bin/python3 <env_name>
+ $ source yourenv/bin/activate
+ $ pip install package-name
+ ```
+ > NOTE: This environment <env_name> will setup pip to install packages only into this environment, not to the entire system.
+ - conda
+ - poetry (better option to manage application from start to finish. It is a much better option than requirements.txt + setup.py.)
+
+- - -
+#### Setup
+- Download and install pip globally
+```bash
+$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
+$ python get-pip.py
+$ pip --version
+```
+- Virtual Environments
+- - -
+#### Basic Syntax
+###### Comments
+- Python uses the `#` symbol to denote single-line comments.
+```python
+# This is a single-line comment
+```
+- Use docstrings (multi-line comments enclosed in triple quotes) to provide documentation for functions, classes, and modules.
+```python
+"""
+This is a
+multi-line comment
+"""
+```
+- Use meaningful comments to explain the purpose and functionality of your code.
+
+###### Variables
+- Python has no mandatory declaration of variables. Variables are created when a value is assigned to them.
+- Python is case-sensitive (e.g., variable and Variable are different).
+- Python uses the `None` value to represent the absence of a value or a null value.
+- Python has a dynamic type system, meaning you can reassign variables to different types.
+
+- Declaration and initialization
+x = 10
+
+- Declaration without initialization
+y = None
+
+###### Data Types
+| Data | Type | Description |
+| --- | --- | --- |
+| int | Integer | Represents whole numbers without a fractional component |
+| float | Float | Represents real numbers with a single-precision format |
+| str | String | Represents a sequence of characters |
+| bool | Boolean | Represents a logical value, either True or False |
+| list | List | Represents an ordered, mutable collection of elements |
+| tuple | Tuple | Represents an ordered, immutable collection of elements |
+| dict | Dictionary | Represents a collection of key-value pairs |
+| set | Set | Represents an unordered collection of unique elements |
+
+###### Arithmetic Operators
+| Operator | Name | Description |
+| --- | --- | --- |
+| x `+` y | Addition | |
+| x `-` y | Subtraction | |
+| x `*` y | Multiplication | |
+| x `/` y | Division | |
+| x `%` y | Modulus | |
+| x `**` y | Exponentiation | |
+| x `//` y | Integer Division | |
+
+###### String Operations
+```python
+string = "Hello, World!"
+len(string) # Length of string
+string.upper() # Convert to uppercase
+string.lower() # Convert to lowercase
+string.strip() # Remove whitespace
+string.split(",") # Split by comma
+```
+
+###### Control Flow
+- Conditional Statements
+```python
+if condition:
+ # Code to execute if condition is true
+elif another_condition:
+ # Code to execute if another_condition is true
+else:
+ # Code to execute if neither condition is true
+```
+- Loops
+ - For Loop
+ ```python
+ for item in iterable:
+ # Code to execute for each item in iterable
+ ```
+ - While Loop
+ ```python
+ while condition:
+ # Code to execute while condition is true
+ ```
+
+###### Functions
+
+```python
+def function_name(parameter1, parameter2):
+ # Code to execute
+ return <result> # Optional return statement
+```
+
+###### Data Structures
+
+- Lists (arrays)
+```python
+my_list = [1, 2, 3, 4, 5]
+
+my_list.append(6) # Add item to the end of the list
+my_list.insert(0, 0) # Insert item at a specific index
+my_list.remove(3) # Remove item from the list
+my_list.pop() # Remove and return the last item
+my_list.sort() # Sort the list
+```
+- Tuples
+
+- Dictionaries
+```python
+my_dict = {"key1": "value1", "key2": "value2"}
+
+my_dict["key3"] = "value3" # Add a new key-value pair
+del my_dict["key2"] # Delete a key-value pair
+my_dict.keys() # Get all keys
+my_dict.values() # Get all values
+my_dict.items() # Get all key-value pairs
+```
+
+- Sets
+```python
+my_set = {1, 2, 3}
+
+my_set.add(4) # Add an element to the set
+my_set.remove(3) # Remove an element from the set
+```
+
+- - -
+#### Advanced Concepts
+
+- - -
diff --git a/tools/git.md b/tools/git.md
new file mode 100644
index 0000000..1616b31
--- /dev/null
+++ b/tools/git.md
@@ -0,0 +1,179 @@
+## Dotfiles
+#### Creating a dotfiles git repo
+ 1. Intialize git and create a folder named ~/.cfg
+ ```bash
+ $ git init --bare $HOME/.cfg
+ ```
+ 2. Put this in your .bashrc or .zsh to use `config` as a dotfiles alias.
+ ```bash
+ $ alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
+ ```
+
+
+ 3. Make sure to ignore the folder we're going use to clone.
+ ```bash
+ $ echo ".cfg" >> .gitignore
+ ```
+ 4. Set flag to not show untracked files
+ ```bash
+ $ config config --local status.showUntrackedFiles no
+ ```
+ 5. Replacing git with our newly created config alias we can version control our
+ dotfiles.
+ ```bash
+ $ config status
+ $ config add .bashrc
+ $ config commit -m "Added bashrc"
+ $ config push
+ ```
+#### Installing onto a new system
+ 1. Avoid weird behaviour/recursion issues when .cfg tries to track itself
+ ```bash
+ $ echo ".cfg" >> .gitignore
+ ```
+ 2. Clone the repo
+ ```bash
+ $ git clone --bare <remote-git-repo-url> $HOME/.cfg
+ ```
+ 3. Set up the alias 'config'
+ ```bash
+ $ alias config='/usr/bin/git --git-dir=<path to .cfg’s Git directory> --work-tree=$HOME'
+ alias config='git --git-dir=$HOME/.cfg --work-tree=$HOME'
+ ```
+ 4. Set local configuration into .cfg to ignore untracked files
+ ```bash
+ $ config config --local status.showUntrackedFiles no
+ ```
+ 5. Checkout
+ ```bash
+ $ config checkout
+ ```
+git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
+git branch -u origin/branchName
+or
+git branch --set-upstream-to=origin/master main
+git update-ref refs/heads/master origin/master
+
+You can also set it with a push
+git push -u origin master
+git push --set-upstream origin master
+#### Use git subtree to sync other repos into dotfiles
+ 1. First make sure dotfiles is up-to date or commit/push/reset all changes
+ otherwise this error message will show: `Working tree has modifications. Cannot add.`
+ ```bash
+ $ config reset --hard origin/main
+ ```
+ Also NOTE: the prefix (subdirectory in which you will add the subtree) cannot already exist therefore delete/backup locally.
+
+
+ 2. Add the repo's file path and it's remote url:
+NOTE: Can use the `--squash` flag to omit storing of entire history of subproject into main repo but if used then the flag will always have to be used with the given repo/subtree and it's commands
+ ```bash
+ $ config subtree add --prefix /path/to/file <remote-git-repo-url> <branch>
+ # Example:
+ $ config subtree add --prefix .config/nvim https://github.com/srdusr/nvim.git main --squash
+ ```
+
+
+ 3. Update to sync/show any changes into dotfiles (upstream repo):
+ ```bash
+ $ config subtree pull --prefix /path/to/file <remote-git-repo-url> main
+ ```
+
+ 4. Finally push onto remote repository:
+ ```bash
+ $ config push
+ ```
+ 5. Optional aliases to put into .gitconfig:
+ ```bash
+ [alias]
+ sba ="!f() { git subtree add --prefix $2 $1 main; }; f"
+ sbu ="!f() { git subtree pull --prefix $2 $1 main; }; f"
+ # Or use --squash if prefered
+ sba ="!f() { git subtree add --prefix $2 $1 main --squash; }; f"
+ sbu ="!f() { git subtree pull --prefix $2 $1 main --squash; }; f"
+ # Example:
+ $ config sbu https://github.com/srdusr/nvim.git .config/nvim #--squash
+ ```
+
+- - -
+
+## Basic commands
+Intialize a git repository in the root of the folder:
+```bash
+$ git init
+```
+See which files git knows exist:
+```bash
+$ git status
+```
+Add a new file to the repo:
+```bash
+$ git add <file>
+```
+Create a commit with a message about the commit:
+```bash
+$ git commit -m <file>
+```
+Push the changes/commits onto repo:
+```bash
+$ git push -u origin <branch>
+```
+Reset the local main branch to remote repository aka...
+Delete the most recent commit, destroying the work you've done:
+```bash
+$ git reset --hard origin/main
+```
+Delete the most recent commit, keeping the work you've done:
+```bash
+$ git reset --soft origin/<branch>
+```
+Allow unrelated histories when two unrelated projects are merged/unaware of each
+other's existence:
+```bash
+$ git pull origin <branch> --allow-unrelated-histories
+```
+Diff between commits
+```
+$ git log
+$ git diff (checkout_id):file.txt file.txt
+or
+$ git diff <commit_id1> <commit_id2>
+or
+$ git difftool <commit_id1> <commit_id2>
+
+```
+
+
+If the top commit is pointed to by HEAD then you can do something like this:
+
+commit1 -> HEAD
+commit2 -> HEAD~1
+commit3 -> HEAD~2
+
+Diff between the first and second commit:
+
+git diff HEAD~1 HEAD
+
+Diff between first and third commit:
+
+git diff HEAD~2 HEAD
+
+Diff between second and third commit:
+
+git diff HEAD~2 HEAD~1
+- - -
+#### Miscellaneous
+Find out size of a repo before cloning it
+```bash
+$ curl -s https://api.github.com/repos/{orgname/username}/{repo_name} | jq '.size' | numfmt --to=iec --from-unit=1024
+
+```
+---
+#### Trouble-shooting
+fatal: ambiguous argument 'origin': unknown revision or path not in the working tree
+```bash
+$ git fetch origin main:refs/remotes/origin/main
+```
+
+---