>>> blog = "yuzhouwan" >>> iter_blog = iter(blog) >>> print(next(iter_blog)) y >>> print(next(iter_blog)) u >>> print(next(iter_blog)) z >>> print(next(iter_blog)) h >>> print(next(iter_blog)) o >>> print(next(iter_blog)) u >>> print(next(iter_blog)) w >>> print(next(iter_blog)) a >>> print(next(iter_blog)) n >>> print(next(iter_blog)) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> print(next(iter_blog, None)) None
流程控制
if-else
1 2 3 4
>>> -1ifTrueelse0 -1 >>> -1ifFalseelse0 0
try-except
1 2 3 4 5 6 7 8 9 10
import sys
import requests
try: resp = requests.get("https://yuzhouwan.com/") print(resp.text) except requests.HTTPError as error: print("Cannot visit this url!", error) sys.exit(1)
retry
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import time
import requests
retry_max = 3 for i inrange(retry_max): try: # 这里故意不小心地调用 json() 函数来解析 HTML 网页 requests.get("https://yuzhouwan.com/").json() except Exception as e: print("Exception:", e) print("Retry (%s / %s)..." % (i + 1, retry_max)) time.sleep(1) else: break
$ a = 1 $ a? Type: int String form: 1 Docstring: int(x=0) -> int or long int(x, base=10) -> int or long
Convert a number or string to an integer, or return 0 if no arguments are given. If x is floating point, the conversion truncates towards zero. If x is outside the integer range, the function returns a long instead.
If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int('0b100', base=0) 4
## local # should shutdown local firewall firstly $ cd .\JetBrains\PyCharm 2016.2.3\debug-eggs\pycharm-debug.egg $ easy_install pycharm-debug.egg # 若运行使用的是 Python3,则需要 pycharm-debug-py3k.egg
# Start Debug Starting debug server at port 12345 Use the following code to connect to the debugger: import pydevd pydevd.settrace('192.168.3.10', port=12345, stdoutToServer=True, stderrToServer=True) Waiting for process connection... Connected to pydev debugger (build 162.1967.10) Starting server with command: gunicorn -w 2 --timeout 60 -b 0.0.0.0:9097 --limit-request-line 0 --limit-request-field_size 0 superset:app
Error running superset Can't run remote python interpreter: Couldn't obtain remote socket from output ('0.0.0.0', 52703), stderr /usr/local/bin/python: No module named virtualenvwrapper virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenvwrapper has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is set properly.
# VirtualBox https://www.virtualbox.org/wiki/Downloads http://download.virtualbox.org/virtualbox/5.1.12/ # better https://hashicorp-files.hashicorp.com/lucid32.box # not good https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box # best
==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'superset' (v0) for provider: box: Unpacking necessary files from: file:///F:/%C8%ED%BC%FE%BF%E2/python/trusty-server-cloudimg-amd64-juju-vagrant-disk1.box box: ==> box: Successfully added box 'superset' (v0) for'virtualbox'!
$ vagrant box list superset (virtualbox, 0)
$ vagrant init
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'superset'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: superset-0154_default_1486969836220_44233 ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 (guest) => 2122 (host) (adapter 1) default: 80 (guest) => 6080 (host) (adapter 1) default: 6079 (guest) => 6079 (host) (adapter 1) default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Running 'pre-boot' VM customizations... ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key
踩过的坑
Provider ‘virtualbox’ not found
描述
1 2 3 4 5
$ vagrant up ==> Provider 'virtualbox' not found. We'll automatically install it now... The installation process will start below. Human interaction may be required at some points. If you're uncomfortable with automatically installing this provider, you can safely Ctrl-C this process and install it manually. ==> Downloading VirtualBox 5.0.10... This may not be the latest version of VirtualBox, but it is a version that is known to work well. Over time, we'll update the version that is installed.
解决
vagrant up --provider=virtualbox
Timed out while waiting for the machine to boot
描述
1 2 3 4 5 6 7 8 9 10 11
子目录或文件 -p 已经存在。 处理: -p 时出错。 子目录或文件 charms 已经存在。 处理: charms 时出错。 Timed out while waiting for the machine to boot. This means that Vagrant was unable to communicate with the guest machine within the configured ("config.vm.boot_timeout" value) time period.
If you look above, you should be able to see the error(s) that Vagrant had when attempting to connect to the machine. These errors are usually good hints as to what may be wrong.
If you're using a custom box, make sure that networking is properly working and you're able to connect to the machine. It is a common problem that networking isn't setup properly in these boxes. Verify that authentication configurations are also setup properly, as well. If the box appears to be booting properly, you may want to increase the timeout ("config.vm.boot_timeout") value.'
The discover sub-command has the following options:
-v, --verbose Verbose output -s, --start-directory directory Directory to start discovery (. default) -p, --pattern pattern Pattern to match test files (test*.py default) -t, --top-level-directory directory Top level directory of project (defaults to start directory)
ip = str(args.ip) port = int(args.port) debug = args.debug if port == 80: blog = "https://%s" % ip else: blog = "https://%s:%s" % (ip, port) if debug: print(blog) os.system("open '%s'" % blog)
[I 2023-09-02 10:55:27.117 LabApp] JupyterLab extension loaded from /Users/jiayi.jin/opt/anaconda3/lib/python3.9/site-packages/jupyterlab [I 2023-09-02 10:55:27.117 LabApp] JupyterLab application directory is /Users/jiayi.jin/opt/anaconda3/share/jupyter/lab [I 10:55:27.123 NotebookApp] Serving notebooks from local directory: /Users/jiayi.jin/code/yuzhouwan [I 10:55:27.123 NotebookApp] Jupyter Notebook 6.4.12 is running at: [I 10:55:27.123 NotebookApp] http://localhost:8888/?token=661ee7213e7c3da16618b4c0069bbb27c2f6eddbe6545f2b [I 10:55:27.123 NotebookApp] or http://127.0.0.1:8888/?token=661ee7213e7c3da16618b4c0069bbb27c2f6eddbe6545f2b [I 10:55:27.123 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 10:55:27.128 NotebookApp]
To access the notebook, open this file in a browser: file:///Users/jiayi.jin/Library/Jupyter/runtime/nbserver-32075-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=661ee7213e7c3da16618b4c0069bbb27c2f6eddbe6545f2b or http://127.0.0.1:8888/?token=661ee7213e7c3da16618b4c0069bbb27c2f6eddbe6545f2b [W 10:55:40.678 NotebookApp] Notebook yuzhouwan-hacker/yuzhouwan-hacker-python/src/main/resources/basic/Collection.ipynb is not trusted [I 10:55:41.195 NotebookApp] Kernel started: eb857db3-08fd-4fc7-b021-b0bbadefeb62, name: python3 Traceback (most recent call last): File "/Users/jiayi.jin/opt/anaconda3/lib/python3.9/runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "/Users/jiayi.jin/opt/anaconda3/lib/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "/Users/jiayi.jin/opt/anaconda3/lib/python3.9/site-packages/ipykernel_launcher.py", line 15, in <module> from ipykernel import kernelapp as app File "/Users/jiayi.jin/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelapp.py", line 51, in <module> from .ipkernel import IPythonKernel File "/Users/jiayi.jin/opt/anaconda3/lib/python3.9/site-packages/ipykernel/ipkernel.py", line 19, in <module> from .debugger import Debugger, _is_debugpy_available File "/Users/jiayi.jin/opt/anaconda3/lib/python3.9/site-packages/ipykernel/debugger.py", line 8, in <module> from IPython.core.inputtransformer2 import leading_empty_lines ModuleNotFoundError: No module named 'IPython.core.inputtransformer2'
解决
1
$ pip install --upgrade ipython jupyter
1 2 3 4 5 6 7
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. spyder 5.3.3 requires pyqt5<5.16, which is not installed. spyder 5.3.3 requires pyqtwebengine<5.16, which is not installed. nb-mermaid 0.1.0 requires IPython<4.0,>3.0, but you have ipython 8.15.0 which is incompatible. spyder 5.3.3 requires ipython<8.0.0,>=7.31.1, but you have ipython 8.15.0 which is incompatible. spyder-kernels 2.3.3 requires ipython<8,>=7.31.1; python_version >= "3", but you have ipython 8.15.0 which is incompatible. Successfully installed asttokens-2.4.0 exceptiongroup-1.1.3 executing-1.2.0 ipython-8.15.0 prompt-toolkit-3.0.39 pure-eval-0.2.2 stack-data-0.6.2