Hacker News new | past | comments | ask | show | jobs | submit login

  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.




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

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

Search: