Skip to content

Instantly share code, notes, and snippets.

View richmondwang's full-sized avatar
🎯
Focusing

Richmond richmondwang

🎯
Focusing
View GitHub Profile
@richmondwang
richmondwang / totalCount_connection_factory.py
Created March 31, 2017 07:17
Connection factory for graphene-sqlalchemy. This will add the field `totalCount` based on the specification of graphql.
def connection_for_type(_type):
"""Added totalCount node on the Connectionfield"""
class Connection(graphene.Connection):
total_count = graphene.Int(
description="Total count of {}s.".format(_type._meta.name))
class Meta:
description = name = _type._meta.name + 'Connection'
node = _type
@richmondwang
richmondwang / dict.py
Created March 31, 2017 07:15
Creates a dict whose top-level keys are always evaluated to str.
class StringedDict(dict):
def __setitem__(self, key, value):
super().__setitem__(str(key), value)
@richmondwang
richmondwang / suredict.py
Created March 31, 2017 07:13
This subclass is used for dicts with guaranteed values of children but can still be used like a normal dict.
class SureDict(dict):
"""
This subclass is used for dicts with guaranteed values of children but
can still be used like a normal dict.
.. note::
Note that only the top level will be of class SureDict, those children
whose values are of dict will remain dict.
@richmondwang
richmondwang / python_jose_decode_cognito.py
Last active May 30, 2020 03:08
Gist to decode AWS cognito using the python Jose package
# -*- coding: utf-8 -*-
# pip install python-jose
from jose import jwt
token = "<token here>"
key = {
"alg": "RS256",
"e": "AQAB",
"kid": "+Sr66GHFIwrVWzl9N02mrm96OCoYbNktaekEhXs9+eM=", # <-- this should match the `kid` in the token's headers
@richmondwang
richmondwang / StartsWith - Voluptuous.py
Created February 21, 2017 08:57
A way to validate keys that start with a given prefix.
# -*- coding: utf-8 -*-
from voluptuous import Marker, Schema
class StartsWith(Marker):
"""
A way to validate keys that start with a given prefix.
Example:
>>> from voluptuous.util import DefaultTo
@richmondwang
richmondwang / Concat - Voluptuous.py
Last active February 21, 2017 09:00
Concat Key Marker
# -*- coding: utf-8 -*-
from voluptuous import Optional
from operator import is_not
from functools import partial
class Concat(object):
"""
Creates a concatenated Optional Marker for voluptuous
@richmondwang
richmondwang / timezones
Created August 31, 2016 16:40 — forked from ykessler/timezones
HTML list of time zones (Based on Olson tz database)
<select name="timezone" >
<option disabled selected style='display:none;'>Time Zone...</option>
<optgroup label="US (Common)">
<option value="America/Puerto_Rico">Puerto Rico (Atlantic)</option>
<option value="America/New_York">New York (Eastern)</option>
<option value="America/Chicago">Chicago (Central)</option>
<option value="America/Denver">Denver (Mountain)</option>
<option value="America/Phoenix">Phoenix (MST)</option>
<option value="America/Los_Angeles">Los Angeles (Pacific)</option>
@richmondwang
richmondwang / voluptuous_month.py
Last active August 16, 2016 14:36
Converts string or int to valid Month.
class Month(object):
"""Validates a string as a Month of a year
Args:
return_type (int): NUMBER = 0, WORD = 1, FULL = 2
msg (str): the message when it fails
"""
NUMBER = 0
WORD = 1
FULL = 2
@richmondwang
richmondwang / voluptuous_tags.py
Last active August 16, 2016 14:31
Coerces string into separate string tags
class Tags(object):
"""Converts string to tags, a list of string that was separated by comma
"""
def __call__(self, v):
if not isinstance(v, basestring) or v is None:
raise Exception()
tags = map(lambda x: x.strip(), list(v.split(',')))
tags = filter(lambda x: x != '', tags)
return tags
@richmondwang
richmondwang / selenium-waiters.java
Last active March 16, 2022 14:20
Selenium. Methods for waiting for load and reload.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.FieldDecorator;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import xxx.CustomFieldDecorator;