Skip to content

Instantly share code, notes, and snippets.

@anaved
anaved / type_sample.py
Last active March 8, 2018 20:56
Sample test class creator factory
#Generating tests on the fly
import unittest
class CheckIntTest(object):
def test_isIntReturned(self):
print "Calling test method with 2, 3"
print "Test method result %s"%(self.calc(2,3))
self.assertIsInstance(self.calc(2,3),int)
def m1(self,a,b):
return a+b
@anaved
anaved / form.html
Last active February 18, 2018 12:17
Sample form field with animated bottom border
<div class="group">
<input type="text" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Email</label>
</div>
@anaved
anaved / context_manager.py
Last active February 21, 2018 20:43
Mock implementation using context manager
import mock
class Patchee(object):
def name(self):
return "Patchee"
class MyContextPatcher(object):
def __init__(self, newName):
self.newName = newName
def __enter__(self):
@anaved
anaved / partial.py
Last active August 2, 2017 21:13
Simple partial example
from functools import partial
def power(number, order):
"""This is a power function"""
return number ** order
square = partial( power, order = 2)
cube = partial( power, order = 3)
if __name__ == "__main__":
class Thirty360DayCountCalculator( DayCountCalculator ):
"""
A month is considered of 30 days and a year is of 360 days.
ISDA recommended method for day count is
360(y2-y1)+30(m2-m1-1)+max(0,30-d1)+min(30,d2)
* Take year diff
* Take month diff + 1 month to be adjusted by date
* Since every month is 30 and reduce d1 from it, and take
max with 0 in case result is negative ( days to 30 )
* days in d2, min if more than 30
Date Dirty Price
2010-01-06 70.8441666667
2010-01-26 72.5102777778
2010-02-05 72.8090277778
2010-02-15 72.0220833333
2010-02-25 73.0951388889
2010-03-17 73.9898611111
2010-04-06 74.4316666667
2010-04-16 74.8647222222
2010-04-26 75.1277777778
def decorator(func):
"""
:param func:
:return: func result after calling it with 1
"""
return func(1)
#Below statement is equal to decorator( decoratee )
@decorator
def decoratee(number):
@memoize
def add_values( a, b ):
return a+b
@anaved
anaved / property_test2.py
Created July 12, 2017 22:38
Declare without decorator.
class MyOtherProperty(object):
def __init__(self, name):
self.name = name
def getXName(self):
return self.name
def setXName(self, val):
self.name = val
@anaved
anaved / property_final.py
Created July 12, 2017 22:34
Example of how to declare a constant using a property
class MyFinalProperty(object):
@property
def name(self):
return "John"