From 31319dd2731a5f4e44644ca66af928865b85a5b5 Mon Sep 17 00:00:00 2001 From: juleshenry Date: Tue, 11 Apr 2023 21:02:39 -0500 Subject: [PATCH 1/3] Address Examples Initiative --- README.md | 2 +- buggy_script.py => examples/buggy_script.py | 4 ++++ examples/buggy_script_2.py | 26 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) rename buggy_script.py => examples/buggy_script.py (82%) create mode 100644 examples/buggy_script_2.py diff --git a/README.md b/README.md index 55813f7..8c06537 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Add your openAI api key to `openai_key.txt` - _warning!_ by default this uses GP To run with gpt-4 (the default, tested option): - python wolverine.py buggy_script.py "subtract" 20 3 + python wolverine.py examples/buggy_script.py "subtract" 20 3 You can also run with other models, but be warned they may not adhere to the edit format as well: diff --git a/buggy_script.py b/examples/buggy_script.py similarity index 82% rename from buggy_script.py rename to examples/buggy_script.py index 3eeb6bb..d85445d 100644 --- a/buggy_script.py +++ b/examples/buggy_script.py @@ -1,5 +1,9 @@ import sys import fire +""" +Run With: `wolverine examples/buggy_script.py "subtract" 20 3` +Purpose: Show self-regenerating fixing of subtraction operator +""" def add_numbers(a, b): return a + b diff --git a/examples/buggy_script_2.py b/examples/buggy_script_2.py new file mode 100644 index 0000000..6891bf6 --- /dev/null +++ b/examples/buggy_script_2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import fire + +""" +Run With: with `wolverine examples/buggy_script_2.py "return_2"` +Purpose: Fix singleton code bug in Python +""" + +class SingletonClass(object): + def __new__(cls): + if not hasattr(cls, 'instance'): + cls.instance = super(SingletonClass, cls).__new__(cls) + return cls.instance + +def return_2(): + """ + Always returns 2 + """ + singleton = SingletonClass() + new_singleton = SingletonClass() + singleton.a = 1 + new_singleton.a = 2 + return singleton.a + singleton.a + +if __name__=="__main__": + fire.Fire() \ No newline at end of file From f46701623f86c336c30466bf5a5c7eca3f79d20d Mon Sep 17 00:00:00 2001 From: Julian Henry Date: Thu, 20 Apr 2023 07:01:59 -0500 Subject: [PATCH 2/3] Update buggy_script_2.py @biobootloader, now fails due to singleton modification of attributes --- examples/buggy_script_2.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/buggy_script_2.py b/examples/buggy_script_2.py index 6891bf6..d36cff5 100644 --- a/examples/buggy_script_2.py +++ b/examples/buggy_script_2.py @@ -20,7 +20,9 @@ def return_2(): new_singleton = SingletonClass() singleton.a = 1 new_singleton.a = 2 - return singleton.a + singleton.a + would_be_2 = (singleton.a + singleton.a) + assert would_be_2 == 2 + return would_be_2 if __name__=="__main__": - fire.Fire() \ No newline at end of file + fire.Fire() From bb8c6f8e3e9c7e0f46992739f1f2ffe41ba0bc43 Mon Sep 17 00:00:00 2001 From: BioBootloader Date: Sat, 22 Apr 2023 17:24:47 -0700 Subject: [PATCH 3/3] switch singleton example to have a bug in singleton implementation --- examples/buggy_script_2.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/examples/buggy_script_2.py b/examples/buggy_script_2.py index d36cff5..0cf9128 100644 --- a/examples/buggy_script_2.py +++ b/examples/buggy_script_2.py @@ -2,27 +2,26 @@ import fire """ -Run With: with `wolverine examples/buggy_script_2.py "return_2"` +Run With: with `python wolverine.py examples/buggy_script_2.py` Purpose: Fix singleton code bug in Python """ class SingletonClass(object): - def __new__(cls): - if not hasattr(cls, 'instance'): - cls.instance = super(SingletonClass, cls).__new__(cls) - return cls.instance - -def return_2(): - """ - Always returns 2 - """ - singleton = SingletonClass() - new_singleton = SingletonClass() - singleton.a = 1 - new_singleton.a = 2 - would_be_2 = (singleton.a + singleton.a) - assert would_be_2 == 2 - return would_be_2 + def __new__(cls): + cls.instance = super(SingletonClass, cls).__new__(cls) + return cls.instance + +def check_singleton_works(): + """ + check that singleton pattern is working + """ + singleton = SingletonClass() + new_singleton = SingletonClass() + singleton.a = 1 + new_singleton.a = 2 + should_be_4 = (singleton.a + new_singleton.a) + assert should_be_4 == 4 if __name__=="__main__": - fire.Fire() + fire.Fire(check_singleton_works) +