Skip to content

Instantly share code, notes, and snippets.

@DouglasAllen
DouglasAllen / this_callable.py
Created November 1, 2013 20:07
just a silly experiment with python __call__ function
# this_callable.py
>>> class Foo:
... def __call__(self):
... import this
...
>>> foo = Foo()
>>> foo
<__main__.Foo object at 0x00BE09B0>
>>> foo()
@DouglasAllen
DouglasAllen / my_rectangle.py
Last active December 27, 2015 00:19
Exercise - Rectangle class
#!/usr/local/bin/python3
# my_rectangle.py
import math
class Rectangle:
def __init__(self, x, y):
# self.width = x
# self.height = y
self.area = x * y
@DouglasAllen
DouglasAllen / insert_word2.py
Last active December 27, 2015 00:09
Exercise - Manipulate a text file
#!/usr/bin/env python3
# insert_word2.py
import sys
def usage():
print('''
Writes to FILE using INSERTED text before each occurrence of FIND text.
@DouglasAllen
DouglasAllen / insert_word.py
Last active December 26, 2015 19:19
Exercise - Manipulate a text file
# insert_word.py
import sys
def usage():
print('''
Outputs new file with inserted text before each occurrence of find text.
Usage: python insert_word.py FILEIN find insert FILEOUT
@DouglasAllen
DouglasAllen / Fahrenheit_to_Celsius.py
Last active December 25, 2015 19:29
revised exercise one for RubyLearning Learns Python
# Fahrenheit_to_Celsius.py
from decimal import *
getcontext().prec = 3
class Fahrenheit_to_Celsius:
def convert(degf):
return Decimal((degf - 32)) * Decimal(5) / Decimal(9)
def main():
begin = """
@DouglasAllen
DouglasAllen / leapyear_function.py
Last active December 25, 2015 19:29
exercise 4 for RubyLearning Learns Python
# leapyear_function.py
def isLeapYear (year):
if ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0):
return(True)# changed from "true" string
else: return(False) # changed from "false" string
# an experiment for the math.fmod
# inspired by http://oneau.wordpress.com/2011/08/30/jdcal/
import math
def is_leap(year):
@DouglasAllen
DouglasAllen / exercise_03.py
Created October 16, 2013 17:29
exercise three Ruby Learning Introduction to Python
def is_a_vowel(c):
vowels = "AEIOUaeiou"
return c in vowels
if __name__ == "__main__":
print(is_a_vowel("X"))
print(is_a_vowel("O"))
print(is_a_vowel("e"))
@DouglasAllen
DouglasAllen / fv.py
Last active December 25, 2015 16:39
Exercise two for Rubylearning Learns Python
# def calculate_new_balance(d, r):
# return(d + d * r )
# if __name__ == "__main__":
# r = 0.005
# d = 1000
# n = 12
# month = n + 1
# print("Given the following values I will calculate your new balance for each month")
# print("The formula is (new balance = deposit * (1 + rate)^months)")
@DouglasAllen
DouglasAllen / ftoc.py
Last active December 25, 2015 15:39
Exercise one for Rubylearning Learns Python.
# ftoc.py
def convert(degf):
return str((float(degf) - 32) * 5 / 9)
if __name__ == "__main__":
print("I will convert Fahrenheit degrees to Celsius degrees for you")
print("Please enter the degrees in Fahrenheit at the prompt")
degf = input("entry =: ")
print("Your entry was " + degf + " degrees Fahrenheit")