Skip to content

Instantly share code, notes, and snippets.

View echohack's full-sized avatar
🎥
twitch.tv/echohack

echohack echohack

🎥
twitch.tv/echohack
View GitHub Profile
@echohack
echohack / myprint.py
Created January 13, 2013 05:14
A short bit of code to test a SublimeText Python developer environment.
class MyPrint:
def __init__(self):
pass
def myPrint(self):
for item in range(10):
print("This is the item at " + str(item) + " .")
return True
import socket
import requests
# ...snippet
def call_api(url):
try:
requests.get(url="url")
except:
socket.error as e:
import socket
import inject_exception
import requests
from mock import patch
import nose.tools
# ...snippet
def test_api_call():
with patch.object(requests, "get") as get_mock:
import socket
# ...snippet
class ExceededRetries(StopIteration):
pass
def call_api(url):
try:
requests.get(url="url")
import socket
import inject_exception2
import requests
from mock import patch
import nose.tools
# ...snippet
def test_api_call():
with patch.object(requests, "get") as get_mock:
class APIWrapper:
#snippet...
def poll_api(self, tries, initial_delay, delay, backoff, success_list, apifunction, *args):
time.sleep(initial_delay)
for n in range(tries):
try:
status = self.get_status()
if status not in success_list:
@echohack
echohack / ProviderAPIWrapper.py
Last active December 16, 2015 09:48
An example of a pattern I use when using requests to wrap an api.
import requests
import json
class AbstractDataProvider():
"""A list of methods that data providers should implement or extend."""
def __init__(self, base_url, username=None, password=None):
self.base_url = base_url.rstrip('/')
self.username = username
self.password = password
@echohack
echohack / json_validator.py
Last active December 17, 2015 05:19
A snippet that shows how to validate json against a schema using jsonschema.
import json
import jsonschema
import requests
def get_valid_json(self, data, json_schema_url):
"""
Validates a json artifact against a published schema.
"""
s = requests.get(url=json_schema_url)
schema = s.json()
@echohack
echohack / jsonschemaTestValidator.py
Created May 28, 2013 18:07
Jsonschema test validator.
import jsonschema
import json
import nose.tools
def test_schema_generator():
test_setup_data = [
{"schema": "../schema/1.0/build-manifest.json", "tests": "build-manifest-validtests.json", "assert_function": nose.tools.assert_true},
{"schema": "../schema/1.0/build-manifest.json", "tests": "build-manifest-invalidtests.json", "assert_function": nose.tools.assert_false}]
for test_setup in test_setup_data:
@echohack
echohack / artifactory_secret_api.py
Last active December 17, 2015 20:09
Artifactory Secret Build Info API.
def create_build_info(self, build_name, build_number, dependencies, build_dependencies):
"""
Returns a build info dictionary which is formated to correctly deploy
a new build to artifactory.
Make a put request with this build info to api/build
"""
build_info = {'version': '1.0.1',
'name': build_name,
'number': str(build_number),
'type': 'GENERIC',