Skip to content

Instantly share code, notes, and snippets.

View pauleveritt's full-sized avatar

Paul Everitt pauleveritt

  • JetBrains
  • Fredericksburg, VA
View GitHub Profile
from dataclasses import dataclass
from typing import Protocol, Optional, TypeVar, Type
class Animal(Protocol):
name: str
@dataclass
class Mammal:
@pauleveritt
pauleveritt / pathlib_static.py
Created November 17, 2020 23:39
How to re-parent an absolute path to be under a different root?
from pathlib import Path
def main():
icon = Path('/favicon.png')
static = Path('/static')
result = static / icon
print(str(result))
@pauleveritt
pauleveritt / injector.py
Last active September 15, 2020 23:22
Using an alias for a PEP 593 Annotation with multiple arguments.
"""
Using an alias for a PEP 593 Annotation with multiple arguments.
I'm rewriting my injector.
I currently put the injector information in dataclass field metadata.
I'd like to allow other callables such as functions, so I'm looking at PEP 593 Annotations, similar to what [the inject package](https://github.com/alecthomas/injector/blob/master/injector/__init__.py#L1116) does.
My injection though needs zero or more arguments, sort of like an RxPY pipeline.
# Update 1: Changed from Markdown science fiction to using code in a running test
@pauleveritt
pauleveritt / container_bind.py
Created July 31, 2020 19:48
Demo showing singletons added in a bound container, aren't updated in another.
from dataclasses import dataclass
from wired import ServiceRegistry
@dataclass
class CustomerOne:
name: str
@pauleveritt
pauleveritt / prep.md
Created June 16, 2020 16:10
PyCharm Power Tips prep guide

PyCharm Power Tips Prep

I'm giving a tutorial. It's gonna be a bunch of fun. Here's what I'll be covering and what you can do to prep.

Outline

  • (1.5h) 42 Tips and Tricks
  • (0.5h) 20 Django Tips
@pauleveritt
pauleveritt / person.py
Created May 30, 2020 19:53
PEP 544 protocols, implementing via subclass, and checking.
"""
Example of "Explicitly declaring implementation"
PEP 544 says you can state that an implementation supports a protocol by
`subclassing the protocol <https://www.python.org/dev/peps/pep-0544/#explicitly-declaring-implementation>`_.
It also says: "If one omits Protocol in the base class list, this would
be a regular (non-protocol) class that must implement Sized."
In the implementation below, ``FrenchPerson`` says that it implements
from os.path import abspath, dirname, join
from examples.usage.csr.bottle import route, run, static_file, template
STATIC_DIR = abspath(join(dirname(__file__), 'static'))
@route('/static/<filename>')
def server_static(filename):
return static_file(filename, root=STATIC_DIR)
@pauleveritt
pauleveritt / app.html
Created May 22, 2020 13:26
htm preact integration for SSR VDOMs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Bulma!</title>
<link rel="stylesheet" href="static/bulma.css">
</head>
<body>
<section class="section">
@pauleveritt
pauleveritt / hynek_protocols.py
Created April 18, 2020 15:56
Writeup of using Protocols for pluggable components.
"""
I'm interested Python and the Modern Web. Including, a modern
approach to templating: developer-focused, closer to Python,
where code quality tools can help. Draft:
https://viewdom-wired.readthedocs.io/en/latest/why.html
I'm working on pluggable "components", as that link shows. Normally
I'd throw zope.interface at it, but PEP 544 Protocols is more realistic:
mypy support by default, some IDE integration to help DX, etc.
@pauleveritt
pauleveritt / glyph.py
Created April 18, 2020 15:37
Protocols and dataclass-based components
"""
https://github.com/python/mypy/issues/4717#issuecomment-454609539
Hi Glyph, thanks for reading. This is going to take a while.
I'm interested in pluggable systems. Specifically a better Sphinx rendering
layer, which I've spent a lot of time on. Recently I've worked on Michael
Merickel's wired:
https://wired.readthedocs.io/