Welcome to Diablo-Python’s documentation!

Contents:

Indices and tables

simple_math Package

This module has very basic math functions not found in the math module. Some of the functions here, require the math module.

simple_math.average(numbers, numtype='float')

Calculates the average or mean of a list of numbers

Args:

numbers: a list of integers or floating point numbers.

numtype: string, ‘decimal’ or ‘float’; the type of number to return.

Returns:
The average (mean) of the numbers as a floating point number or a Decimal object.
Requires:
The math module
simple_math.circle_area(radius)

Returns the area of a circle.

Args:
radius: The radius of a circle.
Returns:
The area of a circle as an integer.
Requires:
The math module.
simple_math.circle_circumference(radius)

Returns the circumference of a circle.

Args:
radius: The radius of a circle.
Returns:
Integer > circumference of a circle.
Requires:
The math module.
simple_math.compound_interest(principal, annual_rate, years)

Returns the future value of money invested at an annual interest rate, compounded annually for a given number of years.

Args:

principal: The beginning ammount of money invested

annual_rate: The interest rate paid out

years: The number of years invested

Returns:
A basic calculation of compound interest.
simple_math.future_value(present_value, annual_rate, periods_per_year, years)

Calculates the future value of money invested at an anual interest rate, x times per year, for a given number of years.

Args:

present_value: int or float, the current value of the money (principal).

annual_rate: float 0 to 1 e.g., .5 = 50%), the interest rate paid out.

periods_per_year: int, the number of times money is invested per year.

years: int, the number of years invested.

Returns:
Float, the future value of the money invested with compound interest.
simple_math.get_full_binary_tree_leaves(height)

Calculate the number of leaves in a complete binary tree in which each internal node has exactly two children. A full binary tree is complete if every leaf in the tree has the same depth. A leaf is a node without children

Args:
height: integer, the height of the tree. Height is defined by the number of edges from the furthest child to the root. An edge is the line segment that runs between and connects nodes.
simple_math.get_full_binary_tree_nodes(height)

Calculate the number of internal nodes in a complete binary tree in which each internal node has exactly two children. A full binary tree is complete if every leaf in the tree has the same depth. Internal nodes include both leaves and internal nodes. The root node is also included in this calculation.

Args:
height: integer, the height of the tree. Height is defined by the number of edges from the furthest child to the root. An edge is the line segment that runs between and connects nodes.
simple_math.get_percentage(a, b, i=False, r=False)

Finds the percentage of one number over another.

Args:

a: The number that is a percent, int or float.

b: The base number that a is a percent of, int or float.

i: Optional boolean integer. True if the user wants the result returned as a whole number. Assumes False.

r: Optional boolean round. True if the user wants the result rounded. Rounds to the second decimal point on floating point numbers. Assumes False.

Returns:
The argument a as a percentage of b. Throws a warning if integer is set to True and round is set to False.
simple_math.get_slope(point1, point2)

Calculate the slope of the line connecting two points on a grid.

Args:

point1: Tuple or list, the x and y coordinate of the first point.

point2: Tuple or list, the x and y coordinate of the second point

Returns:
the slope of a line connecting two points on a grid.
simple_math.is_leap_year(year)

Checks to see if a given year is a leap year.

Args:
Integer, the year to test.
Returns:
Boolean
simple_math.median(data)

Calculates the median of a list of integers or floating point numbers.

Args:
data: A list of integers or floating point numbers
Returns:
Sorts the list numerically and returns the middle number if the list has an odd number of items. If the list contains an even number of items the mean of the two middle numbers is returned.
simple_math.miles_to_feet(miles)

Converts a number of miles to feet.

Args:
miles: Number of miles we want to convert.
Returns:
Floating point number as the number of feet in the given miles.
simple_math.point_distance(point1, point2)

Computes the distance beteen two points on a plane.

Args:

point1: Tuple or list, the x and y coordinate of the first point.

point2: Tuple or list, the x and y coordinate of the second point.

Returns:
The distance between the two points as a floating point number.
simple_math.rectangle_area(width, height)

Returns the area of a rectangle with the given width and height.

Args:
width:
Integer or float, width of the rectangle.

height: Integer or float, height of the rectangle.

Returns:
The area of a rectangle as an integer or float.
simple_math.rectangle_perimeter(width, height)

Returns the perimeter of a rectangle with the given width and height.

Args:
width:
Integer or float, width of the rectangle.
height:
Integer or float, height of the rectangle.
Returns:
Integer or float, perimeter of a rectangle.
simple_math.regular_polygon_area(number_of_sides, length_of_sides)

Calculates the area of a regular polygon (with sides of equal length).

Args:

number_of_sides: Integer, the number of sides of the polygon

length_of_sides: Integer or floating point number, the length of the sides

Returns:
The area of a regular polygon as an integer or floating point number
Requires:
The math module
simple_math.savings_rate(take_home_pay, spending, numtype='float')

Calculate net take-home pay including employer retirement savings match using the formula laid out by Mr. Money Mustache: http://www.mrmoneymustache.com/2015/01/26/calculating-net-worth/

Args:

take_home_pay: float or int, monthly take-home pay

spending: float or int, monthly spending

numtype: string, ‘decimal’ or ‘float’; the type of number to return.

Returns:
your monthly savings rate expressed as a percentage.
simple_math.standard_deviation(variance)

Calculates the standard deviation.

Args:
variance: The variance of a group of numbers.
Returns:
The standard deviation as a floating point number.
simple_math.take_home_pay(gross_pay, employer_match, taxes_and_fees, numtype='float')

Calculate net take-home pay including employer retirement savings match using the formula laid out by Mr. Money Mustache: http://www.mrmoneymustache.com/2015/01/26/calculating-net-worth/

Args:

gross_pay: float or int, gross monthly pay.

employer_match: float or int, the 401(k) match from your employer.

taxes_and_fees: list, taxes and fees that are deducted from your paycheck.

numtype: string, ‘decimal’ or ‘float’; the type of number to return.

Returns:
your monthly take-home pay.
simple_math.total_seconds(hours, minutes, seconds)

Returns the number of seconds in the given number of hours, minutes, and seconds.

Args:
hours:
Integer, number of hours.
minutes:
Integer, number of minutes.
seconds:
Integer, number of seconds.
Returns:
Integer, time in seconds.
simple_math.triangle_area(point1, point2, point3)

Uses Heron’s formula to find the area of a triangle based on the coordinates of three points.

Args:

point1: list or tuple, the x y coordinate of point one.

point2: list or tuple, the x y coordinate of point two.

point3: list or tuple, the x y coordinate of point three.

Returns:
The area of a triangle as a floating point number.
Requires:
The math module, point_distance().
simple_math.variance(numbers, type='population')

Calculates the population or sample variance of a list of numbers. A large number means the results are all over the place, while a small number means the results are comparatively close to the average.

Args:

numbers: a list of integers or floating point numbers to compare.

type: string, ‘population’ or ‘sample’, the kind of variance to be computed.

Returns:
The computed population or sample variance. Defaults to population variance.
Requires:
The math module, average()

file_parsing Package

file_parsing.add_newlines(f, output, char)

Adds line breaks after every occurance of a given character in a file.

Args:

f: string, path to input file.

output: string, path to output file.

Returns:
None.
file_parsing.add_whitespace_before(char, input_file, output_file)

Adds a space before a character if there’s isn’t one already.

Args:

char: string, character that needs a space before it.

input_file: string, path to file to parse.

output_file: string, path to destination file.

Returns:
None.
file_parsing.are_numeric(string_list)

Checks a list of strings to see that all values in the list are numeric. Returns the name of the offending string if it is not numeric.

Args:
string_list: a list of strings to test.
Returns:
boolean or string
file_parsing.copy_web_file_to_local(file_path, target_path)

Copies a file from its location on the web to a designated place on the local machine.

Args:

file_path: Complete url of the file to copy, string (e.g. http://fool.com/input.css).

target_path: Path and name of file on the local machine, string. (e.g. /directory/output.css)

Returns:
None.
file_parsing.get_line_count(fname)

Counts the number of lines in a file.

Args:
fname: string, name of the file.
Returns:
integer, the number of lines in the file.
file_parsing.get_web_file(path)

Gets a file over http.

Args:
path: string url of the desired file.
Returns:
The desired file as a string.
file_parsing.indent_css(f, output)

Indentes css that has not been indented and saves it to a new file. A new file is created if the output destination does not already exist.

Args:

f: string, path to file.

output: string, path/name of the output file (e.g. /directory/output.css).

print type(response.read())

Returns:
None.
file_parsing.is_int(string)

Checks if a string is an integer. If the string value is an integer return True, otherwise return False.

Args:
string: a string to test.
Returns:
boolean
file_parsing.is_numeric(string)

Checks if a string is numeric. If the string value is an integer or a float, return True, otherwise False. Can be used to test soley for floats as well.

Args:
string: a string to test.
Returns:
boolean
file_parsing.reformat_css(input_file, output_file)

Reformats poorly written css. This function does not validate or fix errors in the code. It only gives code the proper indentation.

Args:

input_file: string, path to the input file.

output_file: string, path to where the reformatted css should be saved. If the target file doesn’t exist, a new file is created.

Returns:
None.
file_parsing.total_hours(input_files)

Totals the hours for a given projct. Takes a list of input files for which to total the hours. Each input file represents a project. There are only multiple files for the same project when the duration was more than a year. A typical entry in an input file might look like this:

8/24/14 9:30-12:00 wrote foobar code for x, wrote a unit test for foobar code, tested. 2.5 hours

Args:
input_files: a list of files to parse.
Returns:
float: the total number of hours spent on the project.

convert_php Package

Module for converting and translating PHP data to other formats and structures. This includes the ability to unserialize and translate arrays to other languages.

class convert_php.ConvertPHP

A class for unserializing and translating php data structures to other formats and languages.

get_built_in(language, level, data)

Gets the return string for a language that’s supported by python. Used in cases when python provides support for the conversion.

Args:

language: string the langage to return for.

level: integer, the indentation level.

data: python data structure being converted (list of tuples)

Returns:
None, updates self.data_structure
get_inner_template(language, template_type, indentation, key, val)

Gets the requested template for the given language.

Args:

language: string, the language of the template to look for.

template_type: string, ‘iterable’ or ‘singular’. An iterable template is needed when the value is an iterable and needs more unpacking, e.g. list, tuple. A singular template is needed when unpacking is complete and the value is singular, e.g. string, int, float.

indentation: int, the indentation level.

key: multiple types, the array key.

val: multiple types, the array values

Returns:
string, template formatting for arrays by language.
is_built_in(language)

Tests to see if a language is a built in type supported by python.

Args:
language: string, language to test for.
Returns:
boolean
is_iterable(data)

Checks to see if an object is an iterable.

Args:
data: a data object.
Returns:
boolean
translate_array(string, language, level=3, retdata=False)

Unserializes a serialized php array and prints it to the console as a data structure in the specified language. Used to translate or convert a php array into a data structure in another language. Currently supports, PHP, Python, Javascript, and JSON.

Args:

string: a string of serialized php

language: a string representing the desired output format for the array.

level: integer, indentation level in spaces. Defaults to 3.

retdata: boolean, the method will return the string in addition to printing it if set to True. Defaults to false.

Returns:
None but prints a string to the console if retdata is False, otherwise returns a string.
translate_val(language, value)

Translates string representations of language specific values that vary between languages. Used to translate python values to their counterparts in other languages.

Args:

language: string, the language for which to return values.

value: string, the value to translate.

Returns:
string representation of a value in a given language.