Skip to content

Instantly share code, notes, and snippets.

Avatar

Bryan Berry bryanwb

View GitHub Profile
@bryanwb
bryanwb / testUseMachine.tsx
Created Jul 9, 2021
test useMachine in 2 components
View testUseMachine.tsx
const machine = createMachine(/* details elided */);
const Component1 = () => {
const [state, send] = useMachine(machine);
setInterval(() => send({type: 'LOGIN', password: 'foobar'}), 500
return null;
}
const Component1 = () => {
const [state, send] = useMachine(machine);
// will this have the password foobar?
View gist:4c24a0b7575ed2e04bb2392213c6a0e5
const machine = createMachine(/* details elided */);
const Component1 = () => {
const [state, send] = useMachine(machine);
setInterval(() => send({type: 'LOGIN', password: 'foobar'}), 500
return null;
}
const Component1 = () => {
const [state, send] = useMachine(machine);
// will this have the password foobar?
@bryanwb
bryanwb / test_parse_output.py
Last active Nov 14, 2016
parse the ouput of the condor_status command
View test_parse_output.py
from fn.monad import Option
from toolz.itertoolz import get
example1 = '''Name OpSys Arch State Activity LoadAv Mem ActvtyTime
gce-xxxxx LINUX X86_64 Unclaimed Idle 0.010 3700 0+00:00:02
gce-xxxx LINUX X86_64 Unclaimed Idle 0.000 3700 0+00:00:02
Total Owner Claimed Unclaimed Matched Preempting Backfill
X86_64/LINUX 2 0 0 2 0 0 0
View my-test.el
;; use-region-p returns nil even though it is mocked using cl-flet
;; why?
(defun bwb-gitlab--get-line-nums ()
"Return line number(s) if applicable.
If region selected, returns range in url form"
(if (null (use-region-p))
(message "use-region-p is nil"))
(cond ((not (gitlab--viewing-filep)) "")
((use-region-p)
View term-custom.el
(defun term-toggle-mode ()
(interactive)
(if (term-in-line-mode)
(term-char-mode)
(term-line-mode)))
(add-hook 'term-mode-hook
(lambda ()
(defmacro term-in-char-mode () '(eq (current-local-map) term-raw-map))
View pytest fixtures within fixtures
import pytest
@pytest.fixture(scope='session')
def fixture1():
return dict()
@pytest.fixture
def fixture2(fixture1):
return id(fixture1)
@bryanwb
bryanwb / my_module.py
Created Nov 24, 2015
how to write python unit tests for an ansible module?
View my_module.py
def method1():
pass
def method2()
pass
# import module snippets
from ansible.module_utils.basic import *
@bryanwb
bryanwb / README.md
Last active Oct 3, 2015
Ze Admin: a tool for collecting shell(ish) scripts
View README.md

Some ideas

If you're like me, you have to write a fair number of shell scripts as part of your daily work as a developer or sysadmin. It is tempting to write those scripts in Bash but you think better of it, as Bash is a fucking mess that is completely unmanageable once the script is longer than one hundred lines. Further, managing command-line flags to bash scripts is a nightmare. You could write those scripts in a higher-level language like Python or Ruby but something still isn't right. Your scripts have zero type safety and debugging them is a chore for finding even minor typos.

Just as important, I want to organize my scripts logically as subcommands. Once I have written scripts, it can be very hard for me to find them again later and even recall how they work. I need an organizing structure for these scripts.

@bryanwb
bryanwb / bad.yml
Last active Sep 3, 2015
modifying a dictionary in a loop
View bad.yml
# this fails on the "set" tag
- set_fact: instances="{%- set instances = dict() %}
{%- for group in group_names %}
{%- for host in groups[group] %}
{%- if hostvars[host].get('InstanceId', None) %}
{%- if hostvars[host].get('Platform', None) == 'windows' %}
{%- set tmp = instances.setdefault(host, hostvars[host]['InstanceId']) %}
{%- endif %}
{%- endif %}
{%- endfor %}
@bryanwb
bryanwb / err.msg
Created Aug 18, 2015
error message for go vet
View err.msg
hitman@Bryans-MacBook-Pro:~/pr/sellsword$ go vet github.com/bryanwb/sellsword
vet: error walking tree: stat ../go/src/github.com/bryanwb/sellsword/app.go: no such file or directory
vet: error walking tree: stat ../go/src/github.com/bryanwb/sellsword/appset.go: no such file or directory
vet: error walking tree: stat ../go/src/github.com/bryanwb/sellsword/env.go: no such file or directory
vet: ../go/src/github.com/bryanwb/sellsword/app.go: open ../go/src/github.com/bryanwb/sellsword/app.go: no such file or directory
vet: no files checked
exit status 1