zoukankan      html  css  js  c++  java
  • Python on VS Code

    install python extension

    • Press F1, and input "ext install python".
    • Then the icon at the leftmost side of the status bar is saying that something is being installed.
    • Need to wait a while.
    • Use command "ext" + a space to see installed extensions.

    use markdown as document

    VS Code supports markdown files.
    Ctrl+K, V : markdown: Open Preview to the side
    Ctrl+Shift+V : markdown: Toggle Preview

    create a python project

    • Select File -> Open Folder...
    • Create a tasks.json file under the .vscode folder in the project folder
    • Input below in the task.json file
    // A task runner that runs a python program
    {
        "version": "0.1.0",
        "command": "python",
        "showOutput": "always",
        "windows": {
            "command": "python.exe"
        },
        "args": ["${file}"]
    }
    

    integrate with git

    • Make sure you have a repository on the server.
    • Install a git tool
    • Go to the project directory, open a command window, and run:
    # initialize a git repository
    git init
    
    # set a remote with name origin
    git remote add origin [https://github.com/<username>/<repository>]
    
    # fetch the master branch files from the remote
    git pull origin master
    
    • Start VS Code
      • Use the Git panel to work with the remote.

    run a python file

    • Open the python file.
    • Press Ctrl+Shift+B.

    debug

    • F9 : add/remove a breakpoint.
    • F5 or Ctrl + F5 : start debug
    • F5 : continue
    • F10 : step over
    • F11 : step into
    • Shift + F11 : step out
    • Ctrl + Shift + F10 : restart
    • Shift + F5 : stop

    print Chinese words, but see question marks in the output console

    The issue can be resolved by the following code:

    import io
    import os
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
    

    Threading

    Please see
    http://www.tutorialspoint.com/python/python_multithreading.htm

    Example: Execute at most 10 threads in a thread pool

    from concurrent.futures import *
    ...
            with ThreadPoolExecutor(max_workers=10) as executor:
                for url in urls : 
                    try:
                        i += 1
                        future = executor.submit(self.get_url, url)
                        future.add_done_callback(threadCallbackHandler(url).handle_callback)
                    except Exception as e:
                        print("Failed {0}. {1}".format(url, e))
    
            # wait all threads finish.         
            executor.shutdown(wait=True)
    
    class threadCallbackHandler:
        def __init__(self, url):
            self.url = url
            
        def handle_callback(self, future) :
            try:
                if (future.exception() != None):
                    print("Failed {0}. Exception : {1}".format(self.url, future.exception()))    
            except CancelledError as e:
                print("Cancelled {0}".format(self.url))
            print("Done {0}".format(self.url)) 
    
  • 相关阅读:
    ubuntu VirtualBox 网络配置
    Linux Lsof命令详解
    自然用户界面
    [Java]读取文件方法大全
    java设计模式_命令模式 两个不同风格的实现
    创建线程的方法 Thread Runnable
    程序员每天到底可以写几行代码?
    eclipse Javadoc 汉化成中文
    linux jna调用so动态库
    使用GNU Make来管理Java项目,IDE神马都是浮云
  • 原文地址:https://www.cnblogs.com/steven-yang/p/5467490.html
Copyright © 2011-2022 走看看