Python Black Technology: FuckIt.py

Speaking of strong local Python, you might think it's elegant, simple, fast development speed, high active community. But what really makes this language enduring an important reason is that it could do anything, because there are a variety of third community library, so that we implemented in Python is too simple a thing, and you will often see a few lines of code to achieve reptiles, 10 lines of code to achieve recognition, although some exaggeration, but it is such a library to help you put all the red tape of all packages, and finally give you an elegant open API.

 

Today you recommend this library called "FuckIt.py", the name of a kind look that is very yellow, very violent, the author describes it like this:

FuckIt.py uses state-of-the-art technology to make sure your Python code runs whether it has any right to or not. Some code has an error? Fuck it.

FuckIt.py using the most advanced technology to make your code inside no matter what kind of mistakes, you just FuckIt, the program will be able to "normal" execution, soldiers to the breakwater to soil cover.


It was a first look at an example of how to use it.

installation

pip install fuckit

Suppose you have a target file: broke.py

def f():
    broken_code
    print('fuckit chaining works')

for

let's just assume this is a big module of shitty code.

x = y
y = x
1 / 0 # Oh shhhiiiiiii

var = "Are you proud of what you've done?"

broke.py There are several obvious errors, including a syntax error, broken_code variable is not defined, and 0 as a dividend.

If the direct import broke certainly will complain

>>> import broke
 File "broke.py", line 5
   for
     ^
SyntaxError: invalid syntax

Then you can use fuckit be imported broke in.

>>> import fuckit
>>> fuckit("broke")
<module 'broke' from 'broke.py'>
>>> broke
<module 'broke' from 'broke.py'>

Time will call the function f error

>>> broke.f()
NameError: global name 'broken_code' is not defined

You can use this function to call a chain fuckit in question

>>> fuckit(broke).f()
fuckit chaining works

或者
>>> fuckit(fuckit("broke")).f()
fuckit chaining works

In addition, fuckit also be used as a decoration and a context manager.

Decorative function

>>> @fuckit
... def broken_function():
...    non_existant_variable # Let's create a NameError
...    return 'Function decorator works'
...
>>> print(broken_function())
None

Decoration

>>> @fuckit
... class BrokenClass(object):
...    def f(self):
...        self.black_hole = 1 / 0
...        return 'Class decorator works'
...
>>> print(BrokenClass().f())
None

Context Manager

>>> with fuckit:
...    print('Context manager works')
...    raise RuntimeError()
...

Equivalent to

try:
    print('Context manager works')
except RuntimeError:
   pass

See here, I am not feeling fuckit very violent, so it can be used in what scenario? Personally think that this library is very sad, because your code should not tolerate these errors occur, if an error has occurred should immediately fix is, but there is a possible use scenario is you just took a super-complex code, which various look at the code do not understand, and these fucking an error code from time to time, and you can not control it, you use fuckit it.

fuckit less than 200 lines of source code, is not it curious that you would like to know how to achieve it, and with what Black &? Access Project Address: https://github.com/ajalt/fuckitpy

Guess you like

Origin www.cnblogs.com/valorchang/p/11357481.html