Hacker News new | past | comments | ask | show | jobs | submit login
FuckIt.py - The Python Error Steamroller (github.com/ajalt)
252 points by rbanffy on Dec 6, 2013 | hide | past | favorite | 97 comments



If anyone is curious (or lazy), FuckIt works by opening up the AST [0] and wrapping almost every statement (except those that declare functions) in a silent try-catch block [1].

[0] http://en.wikipedia.org/wiki/Abstract_syntax_tree

[1] https://github.com/ajalt/fuckitpy/blob/master/fuckit.py#L70-...


I tried doing something similar with Perl, which as I understand matters does not actually have an AST. It does have something called "source filters", which I gather are almost entirely unlike Lisp macros; despite that seeming like the best available option (the filter, in this case, being one which wraps the whole script containing 'use fuckit' in a quoted eval whose $@ is ignored), the closest I got with it was segfaulting the Perl interpreter, which certainly counts as some sort of result.


The test suite is amazing.

https://github.com/ajalt/fuckitpy/blob/master/tests.py

    def test_chaining():
        fuckit(fuckit('fuckit')) 
        
        assert 'false' # Good thing this isn't PHP


This guys got a good sense of humor.


There's something to be said about the quality of programming API-kids do these days, if FuckIt.lang projects are so popular. Not to jab this project itself by any means, but hiding defects "on your way to doing that million dollar demo to investors" makes my skin crawl. How many con men are in this industry? This is damn sad. If you need a tool like this beyond your private debugging needs, you don't understand what you're doing and should step back and take the time to reason things out.


I don't think this is so much a tool as a "because I can and it's funny" project. Like Edison -> Serial "adapters" and various other hilariously, uselessly malicious things it's possible to create.


What is an Edison -> Serial "adapter". Do you have a link? Just curious.


Couldn't find one for serial, but here's one for Ethernet: http://m.imgur.com/7Mbx8yk


EDIT: There is actually a whole series:

http://www.fiftythree.org/etherkiller/


Wow that is some electronic sadism. Good for quite a few laughs when it made the rounds in my embedded systems lab today.

Thanks for the links!


Assuming this project is satire (which I hope it is), it's amazing because it:

- does something rather novel

- does something that, to the sufficiently desperate or inept developer, might seem like a half-decent idea or a cunning shortcut

- does something that is just asking for trouble and that nobody in their right mind should do without expecting terrible, terrible things to happen


    - does something that is just asking for trouble and that nobody in their right mind should do without expecting terrible, terrible things to happen
It makes Python run about like JavaScript or PHP. I guess that's accurate.


Some people use Python as JS or PHP, why not also make it behave like that?

In reality, even if this is satire (which I believe it is) I'm sure if enough people will know about it, at least one of them will use it seriously.


> Some people use Python as JS or PHP, why not also make it behave like that?

If you want to go the other way, try

    window.onerror = window.close;


Ahh the famous halting problem and it's recursive work around for unrealistic deadlines where one simply says 'fuckit;'


> these days

Putting aside the fact that this project is probably satire, how is this a "these days" thing? Code in the bad old days was just as littered with ON ERROR RESUME NEXT, if (err) {/*this cant happen*/ return;} and catch {/*do something here*/}


I can't help but facepalm when I read things like this. Even if it works, the name of the project should have given the commenter some clue that it was a joke.

As for the "these days" comment, why is it that people are repeatedly convinced that human habits have changed that drastically in their lifetimes? This "change" is the result of perspective, no more, no less.


Indeed.

OTOH, I would actually say that things have improved of late.

Just in the last couple of decades we've seen what I think are the first serious approaches to error handling. Think about things like the Abrahams Guarantees, or all those fun category-theoretic analyses that the functional folks keep generating.

And then the stakes have gotten higher. Thirty years ago, if a user gave a program messed-up data, then we could shake our heads and say, "Stupid user." Today, large companies put mission-critical functionality on the web, opening it up to people with both the time and the inclination to generate messed-up data in an attempt to bring the system down.

The result of all this is that, just in the last one or two decades, we've finally seen a significant number of large software packages that actually work.

And that, IMHO, is not a trivial development.


There are a lot more computer software jobs today than there were 30 years ago, and this caused an influx of clueless gold-rushers trying to get in. There always were bad programmers and buggy software, it's just that today there's a lot more of both (both in terms of percentage and total), simple as that.


I think the technical difficulty of what the module actually does should be a pretty clear indication that this is satire?


Opening up the AST, and wrapping it in try-catch blocks, is really not that hard in python.

Python has an API for inspecting the AST.


There's always someone who takes it seriously. (And I don't mean the parent comment.)


Are you joking? This project is blatant satire.


They are popular to get cause they are funny. They are popular to make because they let you experiment around with metaprogramming magic in the language to some extent.

I want a fuckit.hs


'spoon' is "kind of" a legitimate fuckit.hs.


I plan on using this next time I have to do some quick and dirty python hacks at work.

Sometimes the effort involved in "properly" initializing someone's module far outweighs whatever potential benefit you get out of the module - this seems to subvert that quite nicely.


I'm pretty sure it's supposed to be a joke.


Based on fuckit.js, although fuckit.py seems to eat errors while fuckit.js reruns code while removing the offending line until it runs:

https://github.com/mattdiamond/fuckitjs


That reminds me of Vigil[0], a Python-derived language that defends supreme moral vigilance.

[0] https://github.com/munificent/vigil


"Still getting errors? Chain fuckit calls. This module is like violence: if it doesn't work, you just need more of it."

I feel like writing some Python now.


I like this one: "Keep in mind that the decorator form of fuckit can't stop syntax errors. For those, you have to use the import form."


Yeah that made me laugh and want to look into how it works.


As the author of FuckItJS, I approve of this effort. And thanks for the attribution!


"Don't silence KeyboardInterrupt or SystemExit. We aren't monsters." Hahahahha


This module is not satire, and the existence of this module does not indicate anything about the quality of programmers of this generation.

For those who think so, try doing some machine learning, data handling code, or even web crawlers. Error tolerance is much needed.

For scientific experiments, sometimes the results of the experiment is still valuable even if there's a trivial bug (malformed row). A partially-correct execution is still more information than an unhandled exception causing an early exit.


Having worked on all of those, this module is satire and foolish to use in normal code. The rule is protect your boundaries and whenever data comes in it requires careful handling and thoughtful error policies. The entire notion of taint from Perl comes to mind.

If anything, your error messages are more important then.


The problem doesn't go away just because you've closed your eyes.

An age appropriate name for the module could be peekaboo.


Definitely accurate. I've spent some time working on a web crawler with a friend, and eventually you just realize that no matter what you do, requesting pages is going to break, hyperlinks lie, content is borked, ect ect ect. After countless hours debugging you just say "FUCK IT, WE'LL JUST GO THE NEXT PAGE". And really, that's what you would have done anyway if you had filtered the page out...


"This module is like violence: if it doesn't work, you just need more of it."

if for nothing else, I am in love with this for its sense of humor


From Nokogiri's homepage: XML is like violence - if it doesn’t solve your problems, you are not using enough of it.


I just wrapped all my testcase's in fuckit and all my tests are now passing. I'm ready to ship!


I like the opposite approach, where broken code is explicitly removed. vigil (https://github.com/munificent/vigil) attempted to do this


Well, when importing a module, it removes lines until it imports.


    On Error Resume Next


That makes me shudder.

At one point in a previous life, I was the administrator for HP Quality Center at my company. On the good side, HP exposes the entire workflow in VBA (ok, I guess that isn't that good actually, but still better than black box).

On the horrible side, EVERY SINGLE FUNCTION in that application began with the line "On Error Resume Next". I quite nearly quit on the spot the first time I looked at the code. A couple years later, I would regret that decision.


There are probably valid use cases, but no programmer can be trusted with that kind of power.


I wrote a library so you don't have to use this:

https://github.com/kislyuk/ensure

(Though I know how frustrating exception juggling can get... especially when they get pickled and re-raised)


> "the test suite actually found a bug holy crap"

I'm sad to say I say this every time this happens to me as well.


A python version of PHP's '@' operator, nice!


Loved reading tests.py:

def test_context_manager(): with fuckit: pass

    assert 'P' != 'NP' # proof is left as an excercise for the reader


This gave me an idea which is probably terrible. Instead of eating errors or ignoring them, use genetic programming on the code until it passes all your tests and doesn't give any errors. It would still be terrible to actually use, but it might actually get the code to work.


  import fuckit
  from string import ascii_lowercase, digits
  from random import choice, randint

  def defuckify(test_fn, fn_name, n_arguments):
  ''' Randomly generate functions until the test passes
  '''
      defuckified_fn = ('def %s(' % fn_name)
      for i in range(n_arguments):
          arg_name = ('arg%d,' % i) if i != n_arguments else ('arg%d' % i)
          defuckified_fn += arg_name
      defuckified_fn += '):'
      exec('def '+ fn_name + '():\n    pass\n')
      while not test_fn():
          for i in range(randint(100,10000)):
              defuckified_fn += choice(' \n,[](){}\'"=+-*/%:'+ ascii_lowercase + digits)

          with fuckit:
              exec(defuckified_fn)
      print(defuckified_fn)

Edited to use fuckit instead of try/catch

Edited again to add that it has been over an hour and it hasn't been able to make a function that returns 3


If anyone's interested, the likelihood of a string with a length between 100 and 1000 characters which contains characters randomly selected from digits, lowercase letters, spaces and the following characters: \n,[](){}'."=+-*/%: is valid python is something like 0.015% (determined empirically)


That seems like quite a lot for 100 characters. I'm guessing they are mostly numbers?

EDIT: wait, you actually ran code produced from this?


I just ran the part that generates a function with a random string as the body and called the function that it generated, and counted the calls that didn't throw anything. As I threw it together in all of 5 minutes I'm sure there are flaws in the methodology

Also the defuckify code I posted is not exactly right, as it was sort of a joke but it's pretty easy to fix


It seems like `unfuck` might be a more pythonic name.

That is not a pair of words I ever thought I'd use in the same sentence. :)


How often do you say "unfuck"?


Do you know a better term for "debug others' code"?


You'd be surprised


Awhile back I tried to make a markov chain generator that consumed a bunch of source code of javascript (or whatever language) and then spit out random code to execute with fuckit.js.

You could try something similar to easily produce random python code that will be correct a much higher percent of the time.

The problem was comments screwed it up since it would randomly put comments in code and comment out actual code.


Is there an actual use case for something like this? I'm legitimately curious.


On a semi-unrelated topic, can anyone imagine a use of write-only memory? I swear I'd came up with one few years ago and unable to recall it anymore.


There was some april joke like that, they even created data sheets if I remember it correctly

edit: found it http://www.repeater-builder.com/humor/signetics-wom.pdf I love 0V +- %2


I see what you did there.


I'm so angry with this. Good work.


I use this on one of my servers for storing gpg encryped mails. I wanted to make a FUSE module out of it but was too lazy. Of course you do read the data at one point but on a different system.


yes, there is. Imagine yourself on an airplane on your way to doing that million dollar demo to investors. And some bozo decided to do a last minute change to log something inconsequential that actually started throwing an error. Now your demo page looks like "Oops! We screwed up!". You have no time to debug this. So you just plugin in this module and see if it still good enough for your demo. If it does, you saved yourself a million dollars. If it doesn't...


Sounds easier than git revert.


Git? We're hustling out our MVP to disrupt the industry paradigm, we don't have time for version control! Next you'll tell me hacking on our live server is a bad thing...


When you have to demonstrate your software in front of Very Important Clients in ten minutes and you can't afford to have a single error popping up.


The fuckit context manager is actually pretty close to a real feature in Python 3.4:

https://github.com/davidhalter/jedi/pull/202


That's a pretty clever feature. I'm liking that, and the new statistics module.


I'm kinda tempted to use it in a script I'm making to scrape one of my government's databases, because they have a lot of malformed data I don't want to deal with, and because fuck it.


Prototyping - you just made an interface change that normally would have to be plumbed though hundreds of files just so you can experiment with one of them.


Because after 48 hours of working on the same bug, there's nothing quite so satisfying as going completely John McClane on its ass.

- Yippie kai yay, motherfucker.


Most appropriate choice of copyright license ever.


seems pretty restrictive to me :

"...and changing it [license] is allowed as long as the name is changed."

edit: i was joking :)


That's the licence for the license, not the license for the code itself.

The licence for the code itself is "0. You just DO WHAT THE FUCK YOU WANT TO." The license for the license is similar, with the caveat that you're required to change the name if you change the content. Which makes sense; we wouldn't want confusion about what the WTFPL refers to.


seriously, what gives? at least you can fork without that clause, so:

REALLY DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE


This truly appeals to the difference between Engineer and Scientist in all of us. I can see myself getting heavy use out of this.


I just can't believe how many commenters here don't get it's a joke or are carefully unsure. A. Ma. Zing.


It's like the exact opposite of Perl's "use strict" pragma. ;)



Is there a Python version? If so does it beat Fuckit.py?


I think my favorite part of it had to be the readme for using it with decorators:

@fuckit def func(): problem_solved

That has to be the best piece of code I have ever seen.


i've used this for a while now--really useful debugging tool in my workflow. Does exactly what it says on the tin.

just wrap the offending module and get on with your life; and in those rare (for me) instances where you still get errors, this module supports chaining syntax--declarative syntax, to say the least:

fuckit(fuckit('imported_module'))


How could this be helping in debugging? I'm curious!


It's a joke


I'm assuming this is a joke. If so, it is funny.


Does this fix circular import for me...?


yes


I'm so tired of the use of profanity in names. Why is this acceptable in this industry?


Because we're all adults with functioning senses of humor, where 'all' is defined as a 'mostly' whose exceptions tend to be both immediately obvious and unbearable in social settings.


OK. Adult. Got it.


this library should actually be called "phpify"


made my day


wtf!




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: