├── .gitignore ├── README.md ├── complication.py ├── hello.c ├── helloworld_builder.py ├── restapi.pyw ├── setup.py └── singleton.py /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .vscode/ 3 | __pycache__/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Every day, I commit a new and more complicated version of some simple code. 2 | 3 | See https://www.reddit.com/r/ProgrammerHumor/comments/y6iuto/lets_do_it/ 4 | 5 | Day 1: 6 | Created simple code that prints "Hello world!" 10 times. 7 | See https://www.reddit.com/r/ProgrammerHumor/comments/y6iuto/lets_do_it/ 8 | 9 | Day 2: 10 | Replaced the for loop with ```for i in ("HelloWorld"):``` 11 | See https://www.reddit.com/r/ProgrammerHumor/comments/y7ewre/see_comments_for_github_repo_that_includes_all/ 12 | 13 | Day 3: 14 | Replaced the hard-coded "Hello World!" with the constant HW 15 | See https://www.reddit.com/r/ProgrammerHumor/comments/y8b170/see_comments_for_github_repo_that_includes_all/ 16 | 17 | Day 4: 18 | Converted part of code to C for faster runtime 19 | See https://www.reddit.com/r/ProgrammerHumor/comments/y97azd/see_comments_for_github_repo_that_includes_all/ 20 | 21 | Day 5: 22 | Created string builder class 23 | See https://www.reddit.com/r/ProgrammerHumor/comments/ya1o2w/see_comments_for_github_repo_that_includes_all/ 24 | 25 | Day 6: 26 | Converted string builder class to singleton factory 27 | See https://www.reddit.com/r/ProgrammerHumor/comments/yavooh/see_comments_for_github_repo_that_includes_all/ 28 | 29 | Day 7: 30 | Created REST API for getting characters out of binary strings 31 | See https://www.reddit.com/r/ProgrammerHumor/comments/ybqcsk/see_comments_for_github_repo_that_includes_all/ 32 | 33 | Day 8: 34 | Replaced for loop with recursive function 35 | See https://www.reddit.com/r/ProgrammerHumor/comments/ycj8x6/see_comments_for_github_repo_that_includes_all/ 36 | 37 | Day 9: 38 | Remade day 7 to be recursive, so the binary strings are taken from the REST API, which are then fed back into it 39 | See https://www.reddit.com/r/ProgrammerHumor/comments/ydcguq/see_comments_for_github_repo_that_includes_all/ 40 | 41 | Make sure to go to https://www.reddit.com/r/ProgrammerHumor/comments/ydcguq/see_comments_for_github_repo_that_includes_all/ to vote on the next change! 42 | 43 | DO NOT VOTE FOR CHANGES USING ISSUES OR PULL REQUESTS (Except for the rust and unit test pull request, you can stay) 44 | -------------------------------------------------------------------------------- /complication.py: -------------------------------------------------------------------------------- 1 | import helloworld 2 | import helloworld_builder 3 | import subprocess 4 | import requests 5 | 6 | # Before running, run the command "pythonw.exe restapi.pyw 1>NUL" 7 | 8 | def recursiveForLoopHelloWorld(iterstring, printstring, index=0): 9 | helloworld.c_helloworld(printstring) 10 | if index+1 < len(iterstring): 11 | recursiveForLoopHelloWorld(iterstring, printstring, index=index+1) 12 | 13 | 14 | binvals = [['110001', '110000', '110000', '110001', '110000', '110000', '110000'], ['110001', '110001', '110000', '110000', '110001', '110000', '110001'], ['110001', '110001', '110000', '110001', '110001', '110000', '110000'], ['110001', '110001', '110000', '110001', '110001', '110000', '110000'], ['110001', '110001', '110000', '110001', '110001', '110001', '110001'], ['110001', '110000', '110000', '110000', '110000', '110000'], ['110001', '110000', '110001', '110000', '110001', '110001', '110001'], ['110001', '110001', '110000', '110001', '110001', '110001', '110001'], ['110001', '110001', '110001', '110000', '110000', '110001', '110000'], ['110001', '110001', '110000', '110001', '110001', '110000', '110000'], ['110001', '110001', '110000', '110000', '110001', '110000', '110000'], ['110001', '110000', '110000', '110000', '110000', '110001']] 15 | 16 | newbinvals = [] 17 | for val in binvals: 18 | newestbinvals = "" 19 | for innerval in val: 20 | response = requests.get(f"http://127.0.0.1:5002/characters/{innerval}") 21 | newestbinvals += str(response.content).lstrip("b'\"").rstrip("\"\\n\'") 22 | newbinvals.append(newestbinvals) 23 | 24 | binvals = newbinvals 25 | 26 | 27 | helloworld_chars = [] 28 | for val in binvals: 29 | response = requests.get(f"http://127.0.0.1:5002/characters/{val}") 30 | helloworld_chars.append(str(response.content).lstrip("b'\"").rstrip("\"\\n\'")) 31 | 32 | 33 | builder = helloworld_builder.HelloWorldBuilder.instance() 34 | for c in helloworld_chars: 35 | builder.add_char(c) 36 | 37 | HW = builder.build() 38 | 39 | recursiveForLoopHelloWorld(HW.replace(" ", "").rstrip("!"), HW) 40 | 41 | subprocess.run(['taskkill', '/IM', 'pythonw.exe', '/F'], capture_output=True) 42 | 43 | 44 | -------------------------------------------------------------------------------- /hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static PyObject* c_helloworld(PyObject* self, PyObject* args) 5 | { 6 | char *strtoprint; 7 | PyArg_ParseTuple(args, "s", &strtoprint); 8 | printf(strtoprint); 9 | printf("\n"); 10 | return Py_BuildValue("i", 1); 11 | } 12 | 13 | 14 | static char c_helloworld_docs[] = 15 | "c_helloworld( ): Lol you think we write documentation?\n"; 16 | 17 | static PyMethodDef myMethods[] = { 18 | { "c_helloworld", (PyCFunction)c_helloworld, METH_VARARGS, c_helloworld_docs }, 19 | {NULL} 20 | }; 21 | 22 | static struct PyModuleDef myModule = { 23 | PyModuleDef_HEAD_INIT, 24 | "helloworld", 25 | "No documentation for you!", 26 | -1, 27 | myMethods 28 | }; 29 | 30 | PyMODINIT_FUNC PyInit_helloworld(void) 31 | { 32 | return PyModule_Create(&myModule); 33 | } 34 | -------------------------------------------------------------------------------- /helloworld_builder.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | from typing import List 3 | 4 | from singleton import Singleton 5 | 6 | @Singleton 7 | class HelloWorldBuilder: 8 | """ 9 | A class that follows the builder pattern, designed to assemble a hello world string 10 | """ 11 | 12 | def __init__(self): 13 | """ 14 | Instantiates the builder class, creating an empty list for characters 15 | """ 16 | self._chars: List[str] = [] 17 | 18 | def add_char(self, c: str) -> HelloWorldBuilder: 19 | """ 20 | Will append a new character to the builder's internal list of characters 21 | :rtype: HelloWorldBuilder 22 | :param c: The character to add 23 | :type c: str 24 | :return: Returns the builder 25 | """ 26 | if type(c) != str: 27 | raise TypeError("Character provided must be of type ") 28 | char = c[0] 29 | self._chars.append(char) 30 | return self 31 | 32 | def build(self) -> str: 33 | """ 34 | Builds the internal list of characters into a readable string 35 | :rtype: str 36 | :return: Returns the built string 37 | """ 38 | built_string = ''.join(self._chars) 39 | return built_string -------------------------------------------------------------------------------- /restapi.pyw: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, jsonify 2 | from flask_restful import Resource, Api 3 | from sqlalchemy import create_engine 4 | from json import dumps 5 | import string 6 | 7 | app = Flask(__name__) 8 | api = Api(app) 9 | 10 | ALPHABETDICT = {} 11 | for i in string.printable: 12 | ALPHABETDICT[str(bin(ord(i)))[2:]] = i 13 | 14 | 15 | class Characters(Resource): 16 | def get(self, bin_code): 17 | return ALPHABETDICT[bin_code] 18 | 19 | 20 | api.add_resource(Characters, '/characters/') 21 | 22 | 23 | if __name__ == '__main__': 24 | app.run(port="5002") -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | setup(name='helloworld', version='1.0', ext_modules=[Extension('helloworld', sources=['hello.c'])]) 3 | 4 | -------------------------------------------------------------------------------- /singleton.py: -------------------------------------------------------------------------------- 1 | class Singleton: 2 | """ 3 | A non-thread-safe helper class to ease implementing singletons. 4 | This should be used as a decorator -- not a metaclass -- to the 5 | class that should be a singleton. 6 | 7 | The decorated class can define one `__init__` function that 8 | takes only the `self` argument. Also, the decorated class cannot be 9 | inherited from. Other than that, there are no restrictions that apply 10 | to the decorated class. 11 | 12 | To get the singleton instance, use the `instance` method. Trying 13 | to use `__call__` will result in a `TypeError` being raised. 14 | 15 | """ 16 | 17 | def __init__(self, decorated): 18 | self._decorated = decorated 19 | 20 | def instance(self): 21 | """ 22 | Returns the singleton instance. Upon its first call, it creates a 23 | new instance of the decorated class and calls its `__init__` method. 24 | On all subsequent calls, the already created instance is returned. 25 | 26 | """ 27 | try: 28 | return self._instance 29 | except AttributeError: 30 | self._instance = self._decorated() 31 | return self._instance 32 | 33 | def __call__(self): 34 | raise TypeError('Singletons must be accessed through `instance()`.') 35 | 36 | def __instancecheck__(self, inst): 37 | return isinstance(inst, self._decorated) --------------------------------------------------------------------------------