What are the black spells in Python?

Source: Internet
Author: User

Reply content:

cTYPES, there was a man who spent months of overtime on this province.

------------------------Labor Day Supplement-----------------------------

Answer @ in the crisp

1. Ipython + ctypes: Debug/test the Interactive runtime environment of the Linux API

Ipython is the best repl!. (I like Python, at least 30% of the goodwill comes from Ipython)

REPL the benefits of self-evident, in the development and commissioning can greatly improve efficiency. Especially if the API needs to be quickly validated.

I was the first to use GDB to do some repl things, but after all, the operation is complex, interactive features limited. And with cTYPES, more readily, ctypes can be directly people so in the extraction function, in the Python level a little packaging, you can directly use, without compiling/connecting, keep running state, the results come out directly with Python analysis ... It's a beautiful picture.

Real-World scenarios:

We have hundreds of thousands of host,host in our operating environment. In addition to compiling the environment, one day, we have doubts about the return of a system call. Then, according to the usual practice, write a sample program on the development machine, compile, copy to the operating machine, run, and repeat the process.

Then with the ctypes, directly in the Python/ipython REPL debugging is good. It is not easy to leave a suspicious executable program.

2. cTYPES as Glue

cTYPES enhances python's ability to act as a glue language, from the process call/Unified protocol level to dewatering directly into the binary level of dehydration. In this way, C + + is less important for compatibility.

Real-World scenarios:

An execution framework, the plug-in is provided as so, and so provides a fixed function entry. Refactoring is intended to remove language coupling and invoke plug-ins in a way called by a process (similar to CGI server, which reduces the restrictions on plug-in writing, and the plug-in itself is easier to test, preventing the collapse of the framework as a whole). However, many plug-in authors have left, so only need to increase the framework of the call function from so to be able to smooth upgrade.

3. cTYPES and System programming

cTYPES as a lightweight and built-in C language "proxy", Python greatly enhances the system's ability to program.

From then on, the system programming code can also become more elegant.

Real-World scenarios:

SDN/VPC program needs to make more adjustments to the kernel protocol stack, from the management level, the network configuration is centrally controlled and issued. Therefore, there is a daemon on the host, on the one hand to accept the zookeeper configuration change notification, on the other hand to the configuration after parsing through the NetLink and the kernel communication.

Almost no one would use Python to do this daemon. But I saw iotop use cTYPES to the NetLink interface of the package, surprised for the human nature, and Python more suitable for configuration parsing and processing. I ventured to use Python to achieve this daemon, debug it silky smooth, and then reduce the overtime for several months.


Answer me to become a power brother @ Weys smile Big God

There is no production environment used Cffi, previously in their own computer simple used, feel as ctypes simple rough. The main reason I didn't use it, of course, was not to have too many things attached to the deployment. What you see on the Quora is not black tech, but it's very interesting. Turtle is a built-in library


======================================================================
Add a recent only to see:

To assign a value to a key in a dictionary, the solution does not exist when the key is not present:
importcollectionstree=lambda:collections.defaultdict(tree)some_dict=tree()some_dict["colors"]["favourite"]="yellow"
Use ContextManager to limit the execution timeout for a block:
withtimeout(seconds=10):    balabala()
Pandas. __slots__ had a website that saved a few gigabytes of memory in that year. Can look at this, in fact, it is not black magic.
Difflib, it is a official module Oh, used to compare the similarity of strings.
(Difflib) [Difflib–compare Sequences ],
Other popular Functools and collections are also Python's attractive places.
Many third-party libraries are stealing.
Besides, Doctest is also good at unit testing.
25.2. doctest When it comes to Python black magic, it is necessary to mention Python's third-party libraries Gevent's bottom-up implementation--greenlet.
Greenlet directly at the memory level, by saving and replacing the Python process run stack to achieve different co-operation of the switch.
This switch is transparent to the Python interpreter, and if the Python interpreter is aware of the environment, it may feel like a person walking on the road for a second while the next second suddenly appears on the subway.
For ordinary Python users, the direct operation of Python's run-time stack, which is to dance on the tip of the wood, which requires 100% accurate memory operation, any error can cause the python process to crash!
And how is the author to ensure correctness? In addition to being familiar with the underlying mechanisms of Python, operating system, compilers, and so on, it is necessary to make the appropriate adaptation work for different systems and hardware environments. When we use Python, we feel the same in different systems or hardware, because Python itself shields us from the underlying details, and it's obviously not that easy when it comes to doing this Python bottom hack.
For example, because there are many CPUs, such as i386, x86_64, arm, and so on, each CPU design is different, so the author writes the corresponding assembly operation instructions for each CPU to complete the stack save and replace, these operations are highly bound to the operating system and hardware.
Although the implementation of Greenlet so BT, but it is someone to do, plus gevent package, with more than Python with the process to use too much.
I think any of the more familiar to Python children's shoes, when the first contact with Gevent, will be curious about how it is done, after further understanding of its underlying greenlet realization mechanism, all marvel at its workmanship.
This kind of thing is the kind of thing that people don't say that you might never think of. Pip has everything, such as interface Itchat
pip install itchat
    1. PEP 0302--New Import Hooks
  • What do the plugins in Flask do? Why can I use
    from flask.ext.sqlalchemy import SQLAlchemy
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.