Intern study summary - Jun 14 Wed

Python

Polymorphism

Does python have the same thing as Java's Polymorphism? For example, I have class Car: price: int; age: int; name: strand class BMW(car): power: str; color: str, when I declare a function as get_car_info(car: Car) can I input an instance of BMW? will i be able to access the power and color attribute of the input, which is type Car?

pyenv

Use pyenv to change the python version of your system (global)

pyenv is a simple yet powerful Python version management tool. Here are the steps to change the global Python version using pyenv:

  1. Install pyenv: Before you use pyenv, you need to install it. Here is the command to install pyenv using Homebrew on macOS:

    1
    2
    brew update
    brew install pyenv

    For Linux, you can use the pyenv-installer:

    1
    curl <https://pyenv.run> | bash

    Make sure you follow the instructions provided by the installer to set up your shell for pyenv usage.

  2. List Available Python Versions: Use the pyenv install --list command to see the list of all available Python versions. It should display a list of versions like this:

    1
    2
    3
    4
    5
    6
    2.7.17
    3.5.9
    3.6.10
    3.7.7
    3.8.2
    ...
  3. Install a Specific Python Version: You can install a specific Python version using pyenv. For example, to install Python 3.8.2, you would use this command:

    1
    pyenv install 3.8.2
  4. Set the Global Python Version: After the Python version you want is installed, you can set it as the global version with pyenv global. For example, to set Python 3.8.2 as the global version, you would use this command:

    1
    pyenv global 3.8.2
  5. Confirm the Change: To confirm that the global Python version has been changed, you can use the python --version command. It should display something like Python 3.8.2, indicating that the version has been changed.

That's it! You have successfully changed the global Python version using pyenv. Keep in mind that changing the global Python version will affect all the Python programs on your system, so make sure you really want to do this before you proceed.

Won’t work

Sometimes you will find that even you followed the steps aforementioned, when you type python3 --version, you will still see the old version, which means the command pyenv global 3.8.2is not working. Keep reading:

If you have just installed pyenv and have set the global Python version, but when running python3 --version the version shown is still the system version (Python 3.8.9 in this case), it suggests that your shell environment might not be set up properly to use pyenv.

One of the common reasons for this is that pyenv is not properly integrated in your shell.

Here is how you can do it:

  1. Add pyenv to bash so command line knows about pyenv: Depending on your shell, you need to add these lines to your shell's configuration file (.bashrc or .bash_profile for bash, .zshrc for Zsh):

    For bash:

    1
    2
    3
    echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\\n  eval "$(pyenv init -)"\\nfi' >> ~/.bashrc
    source ~/.bashrc

    For zsh:

    1
    2
    3
    echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\\n  eval "$(pyenv init -)"\\nfi' >> ~/.zshrc
    source ~/.zshrc

    These commands will append the pyenv init command to your shell configuration file and then reload the shell configuration file. The pyenv init command configures the shell to use pyenv for Python version management.

  2. Restart your shell: This ensures that the changes take effect.

  3. Check the Python version: Now, when you type python3 --version, it should display Python 3.8.16 or whatever version you set as the global version using pyenv.

If the version is still not showing up correctly, it might be due to the specific naming of the python binaries. Try using the command python --version instead of python3 --version. pyenv might have set the version correctly, but the python3 command could still be pointing to your system's Python installation.

If the issue persists, do check the list of Python versions installed under pyenv using pyenv versions. This will help you verify whether the intended Python version is correctly installed under pyenv. If it's not listed, you'll need to install it using pyenv install 3.8.16.

PyCharm:

Note: I am using the newest version up to date 2023 Jun 14th of PyCharm Professional. (MacOS with M1 Pro)

Install a python package in PyCharm

Installing a Python package in PyCharm is quite simple. Here's how you do it:

  1. Open your project in PyCharm.
  2. Navigate to File -> Settings. For macOS, navigate to PyCharm -> Preferences.
  3. In the Settings/Preferences dialog, navigate to Project: [your_project_name] -> Project Interpreter.
  4. In the top-right corner of the dialog, click the + (plus) icon to add a new package.
  5. In the new window that opens (the Available Packages dialog), you'll see a list of available packages. You can search for the package you want to install by typing its name into the search box.
  6. After you find your package, simply click the Install Package button at the bottom of the dialog.
  7. Wait for PyCharm to download and install the package. You can see the installation progress in the status bar at the bottom of the dialog.
  8. After the installation finishes, you can close the dialog. Your package is now installed and ready to use.

Remember that PyCharm creates a new virtual environment for each project by default, so the package you've installed is only available in the current project's environment. If you want the package to be available globally or in a different project, you need to install it in the corresponding environment.

Command

Clear the terminal:

You can simply type the following code to clear your screen:

1
clear

Yet you will find that when you scroll up, the content you typed before still exists. Here is one of the reasons:

The clear command on a Unix-based system like macOS will clear the visible window of the terminal, but it doesn't clear the terminal's scrollback buffer. That means when you scroll up, you'll still be able to see the previous commands and output.

If you want to clear the terminal completely, including the scrollback buffer, you can use the reset command:

1
reset

This will not only clear the terminal screen, but also the scrollback buffer, so you won't be able to scroll up to see previous output.

Alternatively, if you're using the Terminal app on macOS, you can use the keyboard shortcut Cmd+K to clear the screen and the scrollback buffer. This works in both bash and zsh, the default shell on macOS as of Catalina.

Please note, always be careful when using commands that permanently delete history or output, especially in production environments or when working with sensitive data.

Note: sometimes when you use resetcommand, the effect is same as using clear . In this case, Cmd+K is a better choice.

Hexo

I am using Hexo basing my blog. I also had some tips for how to further beautify your blog.

Note, you might know that Hexo could render Markdown grammar. Yet you might not know that Hexo could also render html code in the middle of Markdown file. I used to write my blog in notion, yet notion, unlike classical markdown compiler, supported tog element which allows you to hide and show content when other people click on a button. Here is a html version of code you can use to achieve the same effect. Actually, you’ve seen such use at the beginning of this blog.

Here's the way to add the HTML and JavaScript code into your Markdown file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Here's an interactive section that hides and shows content:

<div class="tog-container">
<button class="tog-button">Show/Hide Answer</button>
<div class="tog-content" style="display: none;">
<p>Your answer goes here.</p>
</div>
</div>

<script>
let togButton = document.querySelector(".tog-button");
let togContent = document.querySelector(".tog-content");

togButton.addEventListener("click", function () {
if (togContent.style.display == "none") {
togContent.style.display = "block";
} else {
togContent.style.display = "none";
}
});
</script>

Please note that in the above example, the JavaScript code is embedded directly in the Markdown file. Some Markdown processors have safety features that remove script tags. If your script is not running after you add it, you might want to check your Markdown processor's documentation or use an external .js file.


Intern study summary - Jun 14 Wed
http://blog.slray.com/2023/06/14/Intern-study-summary-Jun-14-Wed/
Author
Sirui Ray Li
Posted on
June 14, 2023
Licensed under