mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-09 18:50:34 -05:00
New posts
This commit is contained in:
parent
cdc9540b1a
commit
ecedff9c55
2 changed files with 43 additions and 0 deletions
29
content/blog/pycacheprop.md
Normal file
29
content/blog/pycacheprop.md
Normal file
|
@ -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!
|
14
content/blog/quickpythonhttp.md
Normal file
14
content/blog/quickpythonhttp.md
Normal file
|
@ -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.
|
Loading…
Reference in a new issue