Instantly share code, notes, and snippets.

View after_response.py
"""Run some code after the response is returned to the user in django
Great for analytics or other slow tasks that you don't want to block the response
to the user for.
"""
class HttpResponseWithCallback(HttpResponse):
def __init__(self, *args, **kwargs):
self.callback = kwargs.pop('callback')
super().__init__(*args, **kwargs)
View full-page.js
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({width: 1440, height: 1200});
await page.goto('https://example.com', {waitUntil: 'networkidle2'});
await page.screenshot({path: 'screenshot.png', fullPage: true});
await browser.close();
})();
View mypy_check_order.py
"""
Minimum amount of code to reproduce a strange mypy edge case where the order
of checking isinstance seems to matter to the type inference.
To test it uncomment one of the __add__ examples and run:
pip3 install mypy
mypy mypy_check_order.py
"""
from decimal import Decimal
View action-creators.js
import {reduxify} from 'reduxify'
import {action1, action2} from 'myActions'
const MyComponent = reduxify({
mapDispatchToProps: ({action1, action2}),
render: ({action1, action2}) =>
<div>
<button onClick={action1}>Do first action</button>
<button onClick={action2}>Do second action</button>
</div>
View reduxify.js
import {connect} from 'react-redux'
export const reduxify = (container) =>
connect(
container.mapStateToProps,
container.mapDispatchToProps,
)(container.render)
View reduxify-testing.js
const mapStateToProps = (state) => ({
// some complex logic that needs to be tested
...
})
const Component = reduxify({
mapStateToProps,
mapDispatchToProps: (dispatch) => ({...}),
render: (props) =>
<div>...</div>
View reduxify-example.js
const SomeComponent = reduxify({
mapStateToProps: (state) => ({...}),
mapDispatchToProps: (dispatch) => ({...}),
render: ({}) =>
<div></div>
})
View react-redux-normal-example.js
const mapStateToProps = (state}) => ({
...
})
const mapDispatchToProps = (dispatch) => ({
...
})
const Component = (props) =>
<div></div>
View redux-connect-example.js
const ToggleableVideo = connect({
mapStateToProps: ({video}) => ({stream_id: video.my_stream_id}),
mapDispatchToProps: (dispatch) => ({
onStartStreaming() {
dispatch({type: 'START_STREAMING'})
},
}),
render: ({stream_id}) =>
stream_id ?
<VideoStream stream_id={stream_id}/>
View udp_chat.py
#!/usr/bin/env python3
"""UDP Broadcast Sender"""
import socket
import sys
PORT = 33333
def udp_send_socket():