Gogs 7 anni fa
parent
commit
0f2468580e
2 ha cambiato i file con 44 aggiunte e 1 eliminazioni
  1. 24 1
      README.md
  2. 20 0
      examples/simplefunction.py

+ 24 - 1
README.md

@@ -2,7 +2,30 @@
 
 Just notes on various things
 
+## docstrings
+Use 'em.
+
+## ArgParse
+
+
 ## Interesting Tidbits
+Redirect stdout to a file. The `with` isolates context and prevents certain race conditions, handling errors appropriately.
+```
+with open('help.txt', 'w') as f:
+    with redirect_stdout(f):
+        help(pow)
+```
+
+### Caching
+
+If you have a function that always returns the same result for a given input, you can cache the result by prefacing with `@lru_cache`. e.g.
+```
+from functools import lru_cache
 
-Cache a function. Add `@cache` before any function that always returns the same value for hte same input to add some magic cacheing to it.
+@lru_cache(maxsize=32)
+def fib(n):
+    if n < 2:
+        return n
+    return fib(n-1) + fib(n-2)
+```
 

+ 20 - 0
examples/simplefunction.py

@@ -0,0 +1,20 @@
+def example(firstargument, secondargument, thirdargument="N/A"):
+    """ Prints a string based on the two arguments. """
+    # This is python3.6 syntax:
+#    print(f"{firstargument} is the first argument.")
+    print("{} is the first argument.".format(firstargument))
+    print("{} is the second argument.".format(secondargument))
+    print("{} is the third argument.".format(thirdargument))
+
+
+if __name__ == "__main__":
+    """
+    Some examples on simple functions.
+
+    And docstring stuff that will probably have to be updated when I know more.
+    """
+    # You can just use parameters in order
+    example("My first argument", "my second argument")
+    # but better to use the names
+    example(secondargument = "#2!", firstargument = "#1!", thirdargument = "#3!")
+