Skip to content

Instantly share code, notes, and snippets.

@I159
I159 / wb2.py
Last active May 17, 2017 17:17
Webapp2 fire up app
import datetime
import webapp2
from google.appengine.ext import db
from google.appengine.api import users
class MyUser(db.Model):
user = db.UserProperty()
# nickname = db.UserProperty()
date = db.DateTimeProperty(auto_now_add=True)
def protocol(func):
"""Decorator to create protocol decorators for views from simple funcs.
A protocol is just a dictionary existing the 'protocols' property (which is
a dictionary by itself) of a registered view function.
Protocols should be used to tell something meaningful about a view to some
blueprint. For example, authentication blueprint can distinguish protected
views from views accessible by anonymous.
Protocol decorator has static method get() returning protocol info.
class MetaFabric(object):
backends = (test_base.PostgreSQLOpportunisticTestCase,
test_base.MySQLOpportunisticTestCase)
modules = ('trust',
'identity',
'contrib.oauth1',
'contrib.federation',
'contrib.endpoint_filter',
'token',
def print_tree(node):
c_level = [node,]
while c_level:
print " ".join((i.val for i in c_level))
n_level = []
for i in c_level:
if i.left:
n_level.append(i.left)
if i.right:
n_level.append(i.right)
import random
def reader(n, path="big.txt"):
with open(path, 'rb') as iterative:
cache = []
while n > 0:
try:
line = next(iterative)
if random.randint(1, 100) % 2:
@I159
I159 / server.py
Created December 11, 2016 17:02
Test task: Fibonacci Server.
""" Asynchronous Fibonacci Server
##Solution
Any kind of caching is potentially expensive and unlikely to justify themselves in
comparison with generating of value, especially with small values of "n".
Asynchronous concurrent model was chosen because it is much more cheaper than
multithreading due to peculiar properties of Python threads. Since we are
dealing with extra simple application logic and architecture, aiohttp was chosen
on the grounds of exceptive simplicity. Also
@I159
I159 / matrix_rotator.py
Last active May 16, 2017 14:17
Matrix rotator. Test task.
import copy
import sys
def rotator(matrix):
n = len(matrix)
i = 0
while i < n:
j = i
while j < n:
"""
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
@I159
I159 / longest.py
Created March 15, 2017 16:15
longest line
def longest():
with open("data.txt", "rb") as file:
lengths = [len(i) for i in file]
return lengths.index(max(lengths))
@I159
I159 / common_parent.py
Last active May 16, 2017 14:13
A common parent node search algorithm.
class Node(object):
def __init__(self, name, parent=None):
self.name = name
self.parent = parent
def __str__(self):
return self.name
def __repr__(self):
return self.__str__()