Kind of small “Oh wait, that’s what I should do”, but.. You know how sometimes you’re processing something and then end up doing a zillion slow http requests/file loads/calculations/etc to get the data you’re test processing? So you write it to a file and then load it, but now you’ve written a bunch that already expects it in an object? Yes, you’re right, you should restructure the whole thing so you don’t and end up with neat data objects, but ain’t nobody got time for that. So instead of redoing the code, you can just make a dummy class and pound the data into that. Ex:
import requests s = requests.Session() r = s.get("http://www.example.com) a = do_huge_thing_that_isnt_a_neat_function_like_this(r.headers,r.cookies) b = do_other_thing(r.text,a)
import requests import json # DEBUG = 'normal' DEBUG = 'save' # DEBUG = 'load' if DEBUG != 'load: s = requests.Session() r = s.get("http://www.example.com) if DEBUG == 'save': open('testdata','w').write(json.dumps([r.data,r.headers,r.cookies])) return else: class r: pass [r.data, r.headers, r.cookies] = json.load(open('testdata','r')) a = do_huge_thing_that_isnt_a_neat_function_like_this(r.headers,r.cookies) b = do_other_thing(r.text,a)
Bam! You can still keep calling them r._stuff_ instead of redoing the places you called them that, but you can still uncomment the DEBUG = statements to do the request and save it, request it and keep going as usual, or don’t request it and just load a sample request. The variables in your class are dynamic, so as long as you’re no expecting there to be another class in the class you can just put them there. Otherwise you’d have to do something like
r.class1 = r()
r.class1.class2 = "data"
so that you create a new (implicit in the ()) to assign to. That, too, is possible and you can keep stacking them if you need to.
And then I guess later be a good boy and abstract your stuff into actual classes or functions or dicts to that your code isn’t super dependent on a data source and can be reused later (HAHAHahaha.. Yeah I know, but perhaps some day you’ll have it so sparklingly clean that reuse is an actual option).