From ecedff9c5549c18b56078b4e2db55dc0a209d7d8 Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Sat, 18 Apr 2020 18:41:55 -0400 Subject: [PATCH] New posts --- content/blog/pycacheprop.md | 29 +++++++++++++++++++++++++++++ content/blog/quickpythonhttp.md | 14 ++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 content/blog/pycacheprop.md create mode 100644 content/blog/quickpythonhttp.md diff --git a/content/blog/pycacheprop.md b/content/blog/pycacheprop.md new file mode 100644 index 0000000..099e852 --- /dev/null +++ b/content/blog/pycacheprop.md @@ -0,0 +1,29 @@ +--- +title: "Quick Python: Cached Property" +date: 2020-04-18T18:29:21-04:00 +draft: false +tags: ["python"] +--- + +If you have a property in an object that only needs to be computed once, consider using `cached_property` to store the result and serve for future function calls. + +```python +import functools +class Number: + def __init__(self, n): + self.n = n + @functools.cached_property + def is_prime(self): + return all(self.n % i for i in range(2, self.n)) +``` + +Let's test it with the Mersenne prime `524287`. + +```python +n = Number(524287) +n.is_prime +``` + +After maybe 1-2 seconds of thinking you should get `True.` + +Run it again and the result will be instantaneous! \ No newline at end of file diff --git a/content/blog/quickpythonhttp.md b/content/blog/quickpythonhttp.md new file mode 100644 index 0000000..e23ae8f --- /dev/null +++ b/content/blog/quickpythonhttp.md @@ -0,0 +1,14 @@ +--- +title: "Quick Python: HTTP Server" +date: 2020-04-18T17:15:09-04:00 +draft: false +tags: [] +--- + +You can use Python to quickly spin up a HTTP server. A common use case for me is to quickly transfer files to mobile devices in my internal network. + +```python +python -m http.server +``` + +This will likely start an HTTP server on port 8000 on your machine listening to all network interfaces. \ No newline at end of file