Skip to content

Instantly share code, notes, and snippets.

View russmatney's full-sized avatar

Russell Matney russmatney

View GitHub Profile
@russmatney
russmatney / django_filter_grouping_impl.py
Created November 30, 2021 21:36
Django-filters group nested queries impl
class GroupFilter:
"""
The work here is largely cherry-picked from this unmerged django-filters PR:
https://github.com/carltongibson/django-filter/pull/1167/files
"""
def __init__(self, filter_names):
self.filter_names = filter_names
def set_parent(self, parent):
@russmatney
russmatney / +css-classes-backend.el
Created March 29, 2020 22:03
Company backend for css classes. Parses classnames from a hard-coded css file.
;;; ~/dotfiles/emacs/.doom.d/+css-classes-backend.el -*- lexical-binding: t; -*-
(require 'cl-lib)
(require 's)
(defmacro comment (&rest _)
"Comment out one or more s-expressions."
nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
main :: IO ()
main = do
print "Running 'viewExamples'"
viewExamples
print "Running 'composedViewExamples'"
composedViewExamples
print "Running 'previewExamples'"
previewExamples
hasn'tExample :: IO ()
hasn'tExample = do
let bob = User (UserName "bob") 42 Nothing HM.empty
print "Hasn't bob gold in his inventory?"
print $ hasn't (inventory . ix "gold") bob
-- True
-- As in, "Yes, he doesn't."
hasGotchaIx :: IO ()
hasGotchaIx = do
let bob = User (UserName "bob") 42 Nothing HM.empty
print "Has bob gold in his inventory?"
print $ has (inventory . ix "gold") bob
-- False
let richBob = User (UserName "bob") 42 Nothing
$ HM.fromList [("gold", Item 10 10)]
print "Has bob gold in his inventory?"
hasGotcha :: IO ()
hasGotcha = do
let bob = User (UserName "bob") 42 Nothing HM.empty
print "Has bob gold in his inventory?"
print $ has (inventory . at "gold") bob
-- True
toListOfExamples :: IO ()
toListOfExamples = do
let tory = HM.fromList [ ("gold", Item 99 10)
, ("silver", Item 10 9)
]
bob = User (UserName "bob") 42 Nothing tory
print "A list of Bob's items"
print $ bob ^.. inventory . folded
-- [Item {_itemValue = 10, _itemWeight = 9},Item {_itemValue = 99, _itemWeight = 10}]
atIxNonExamples :: IO ()
atIxNonExamples = do
let bob = User (UserName "bob") 42 Nothing HM.empty
-- if you were doing this for-real, you would impl and use Data.Default
defaultGoldItem = Item 0 0
print "Return the value of Bob's gold, whether he has it or not."
print $ bob ^. inventory . at "gold" . non defaultGoldItem . value
-- 0
print $ bob ^? inventory . at "gold" . _Just . value
atIxExamples :: IO ()
atIxExamples = do
-- Yep, you can use apostrophes in var names. Not that you should...
let bob'sInventory = HM.fromList [ ("gold", Item 99 10)
, ("silver", Item 10 9)
]
bob = User (UserName "bob") 42 Nothing bob'sInventory
print "Printing Bob's gold value"
print $ bob ^? inventory . at "gold" . _Just . value
-- StorageError in a module somewhere
newtype StorageError = StorageError Text deriving (Eq, Show)
-- WebError wraps storage Error
data WebError
= WebTextError Text
| WebStorageError StorageError
deriving (Eq, Show)
-- Let's convert an 'Either StorageError a' to an 'Either WebError a'