Skip to content

Instantly share code, notes, and snippets.

View yeraydiazdiaz's full-sized avatar

Yeray Díaz yeraydiazdiaz

View GitHub Profile
@yeraydiazdiaz
yeraydiazdiaz / pyenv_venv_here.sh
Created January 28, 2019 13:55
Bash script to create a pyenv virtualenv in the current directory
#!/bin/bash
set -e
if [ -n "$1" ]; then
PYTHON_VERSION=$1
else
echo "Please specify a Python version, e.g. 3.7.1";
exit 1
fi
set +e
@yeraydiazdiaz
yeraydiazdiaz / threadpool_future_cancellation.py
Last active February 6, 2021 04:59
Cancellation on run_in_executor using ThreadPoolExecutor
"""
A demonstration on how raising KeyboardInterrupt in the context of tasks
spawned via asyncio's loop.run_in_executor does not cancel the threads
using any of the cancellation methods in asyncio Futures.
The only "proper" way to cancel is to:
1. unregister the `atexit` registered `_python_exit` function
2. call `shutdown(wait=False)`
The reason is that the `thread` module registers `_python_exit` forcing a
@yeraydiazdiaz
yeraydiazdiaz / basic_twisted_example_with_tests.py
Created June 13, 2018 20:23
Basic Twisted example with unit tests using twisted.trial
"""
Basic Twisted example with unit tests using twisted.trial.
The snippet below is a most basic Twisted example runnable in an interpreter:
```
from twisted.internet import task, reactor
d = task.deferLater(reactor, 0.1, lambda: print('foo')) # creates and schedules a Deferred
d.addCallback(lambda x: reactor.stop()) # add a callback to stop when completed
reactor.run() # run the event loop
@yeraydiazdiaz
yeraydiazdiaz / lunr1vs2.js
Last active February 3, 2018 17:07
MkDocs Lunr.js version comparison
/**
* Comparison between lunr1 and lunr2 searching based on MkDocs search index JSON
*
* Install both versions of lunr:
* 1. install Yarn `npm i -g yarn`
* 2. yarn add lunr1@npm:lunr@1.0.0
* 3. yarn add lunr@latest
*
* `search_index.json` should available in the same directory as this script
* Run `node test.js`
@yeraydiazdiaz
yeraydiazdiaz / hnRecursiveComments.js
Created September 28, 2017 14:07
A port of the recursive HN comment calculator used in "Asyncio Coroutine Patterns" using Node.js async/await
/**
* A port of the recursive HN comment calculator used in
* "Asyncio Coroutine Patterns" using Node.js async/await
*
* Assume we want to calculate the number of comments of a particular post in
* Hacker News by recursively aggregating the number of descendants.
*
* Original article at:
* https://medium.com/python-pandemonium/asyncio-coroutine-patterns-beyond-await-a6121486656f
*

Keybase proof

I hereby claim:

  • I am yeraydiazdiaz on github.
  • I am yeray (https://keybase.io/yeray) on keybase.
  • I have a public key ASDryYOOFGs1AuHPvXF7x86jHab4IQvfrL1uatweH2jFlQo

To claim this, I am signing this object: