Python basic study notes 1 (AI Studio)

Address: Feipiao AI Studio Galaxy Community-artificial intelligence learning and training community

Course address: Feipiao AI Studio Galaxy Community-artificial intelligence learning and training community 

Course address: Feipiao AI Studio Galaxy Community-artificial intelligence learning and training community 

Course address: Feipiao AI Studio Galaxy Community-artificial intelligence learning and training community 

Basic operations of AI Studio’s Notebook project

  • Project start and stop
  • Execution and debugging
  • Multiple file code editing
  • Upload Notebook
  • Notebook shortcut keys
  • dark mode
  • Font size adjustment
  • Using Shell commands in Notebook

            Use pip to install the packages you need (but apt-get is not supported)

            View the packages installed in the current environment

            Persistent installation

                    Use git commands to synchronize code (temporarily requires Paddle 1.4.1 or above)

            Download Document

  • Python code execution and debugging
  • Variable monitoring
  • Magic command

             %env: Set environment variables

             %run: run python code

             %%writefile and %pycat: Export cell content/display the content of external scripts

  • About quickly viewing the usage of an object/method/interface
  • About variable monitoring
  • About debugging code

 Project start and stop

When entering the details page of their own project, users can choose to "run" the project, which is to prepare the project environment.

Similarly, when you do not want to continue, you can click "Stop" on this page to terminate the project.

Execution and debugging

To insert breakpoints, you need to use Python's built-in Debugger: PDB. 

Python comes with a debugger, and it even becomes a built-in debugger after Python 3.7. This is the PDB. This is a basic skill that users of Python need to master.

The corresponding code is as follows:

import pdb

class MyScrapy:
    urls = []

    def start_url(self, urls):
        pdb.set_trace()
        for url in urls:
            print(url)
            self.urls.append(url)

    def parse(self):
        pdb.set_trace()
        for url in self.urls:
            result = self.request_something(url)

    def request_something(self, url):
        print('requesting...')
        data = '''<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
</html>'''
        return data


scrapy= MyScrapy()
scrapy.start_url(["http://www.zone7.cn", "http://www.zone7.cn", "http://www.zone7.cn", "http://www.zone7.cn", ])
scrapy.parse()

 For the main content of detailed instructions, please refer to  howchoo

More specific content: Basic Operations of AI Studio (2) Debug Chapter - Flying Paddle AI Studio Galaxy Community

Multiple file code editing

  1. Support multiple file editing
  2. Available commands
    !cat <<newfile > newfile.py

    Create files directly in the project space and then double-click to edit.

Upload Notebook

 

Notebook shortcut keys

The shortcut keys are divided into two states: 1. Command mode; 2. Edit mode

dark mode

Font size adjustment

Using Shell commands in Notebook

By adding ! (exclamation point) before the Shell command, you can execute some Shell commands. Including commands such as !pip install. However, commands such as !apt-get that may trigger further operations from the user are not supported.

Use pip to install the packages you need (but apt-get is not supported)

! pip install package名

#查看当前环境中安装的package
!pip list --format=columns

#查看预装软件
!apt list

Persistent installation

 If you need to perform persistent installation, you need to use the persistence path, for example:

!mkdir /home/aistudio/external-libraries
!pip install beautifulsoup4 -t /home/aistudio/external-libraries

At the same time, add the following code, so that every time the environment (kernel) starts, you only need to run the following code:

import sys
sys.path.append('/home/aistudio/external-libraries')

Use git commands to synchronize code (temporarily requires Paddle 1.4.1 or above)

%env: Set environment variables

%run: run python code

%%writefile and %pycat: Export cell content/display the content of external scripts

%%writefile magic can save the contents of the cell to an external file. %pycat can display external files in Cell

%%writefile SaveToPythonCode.py

from math import sqrt
for i in range(2,10):
    flag=1
    k = int(sqrt(i))
    for j in range(2,k+1):
        if i%j == 0:
            flag = 0
        break
        if(flag):
            print(i)

Because no path is specified, the file is saved to the root directory. But at least it works.

Then try to read the file content from it:

%pycat SaveToPythonCode.py

debugger:

import pdb

class MyScrapy:
    urls = []

    def start_url(self, urls):
        pdb.set_trace()
        for url in urls:
            print(url)
            self.urls.append(url)

    def parse(self):
        pdb.set_trace()
        for url in self.urls:
            result = self.request_something(url)

    def request_something(self, url):
        print('requesting...')
        data = '''<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
</html>'''
        return data


scrapy= MyScrapy()
scrapy.start_url(["http://www.zone7.cn", "http://www.zone7.cn", "http://www.zone7.cn", "http://www.zone7.cn", ])
scrapy.parse()

 operation result:

Python comes with a useful module called pdb, which is basically an interactive source code debugger.

Once running, an interactive box will appear

As shown below:

Type the command in this input box to start debugging.

Usually these commands are one letter, so there's nothing to worry about.

  • Next line->n: In the input box, enter n to go to the next line
  • Print->p
  • Dynamically add break->b: Add breakpoints at specific locations in the program after the debugging session has started
  • Dynamically allocate variables
  • Exit->q: You can exit at any time
  • ENTER: Repeat the last command
  • c: continue
  • l: Find where you are currently
  • s: Enter the subroutine. If there is currently a function call, then s will enter the called function body.
  • n(ext): Let the program run the next line. If the current statement has a function call, using n will not enter the called function body.
  • r: run until the subroutine ends)
  • !<python command>
  • h : help
  • a(rgs): Print the parameters of the current function
  • j(ump): Let the program jump to the specified line number
  • l(ist): can list the code blocks currently to be run
  • p(rint): One of the most useful commands, print a certain variable
  • q(uit): Exit debugging
  • r(eturn): Continue execution until the function body returns
#如发现环境中未安装, 可以运行下方代码来安装ipdb

!pip install ipdb -i https://pypi.tuna.tsinghua.edu.cn/simple

Guess you like

Origin blog.csdn.net/m0_62110645/article/details/132855403