Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions fixit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@


def build_parser() -> argparse.ArgumentParser:
"""Build the argument parser for the fixit command-line interface.

Example:
parser = build_parser()
args = parser.parse_args(["wc", "hello world"])
assert args.command == "wc"
"""
parser = argparse.ArgumentParser(prog="fixit", description="A tiny toolbox to practice open source.")
parser.add_argument("--version", action="version", version=f"fixit {__version__}")
sub = parser.add_subparsers(dest="command", required=True)
Expand Down Expand Up @@ -44,6 +51,11 @@ def build_parser() -> argparse.ArgumentParser:


def main(argv=None) -> int:
"""Run a fixit command and return its exit status.

Example:
main(["convert", "100", "c2f"]) prints 212.0 and returns 0.
"""
try:
args = build_parser().parse_args(argv)
if args.command == "wc":
Expand Down
20 changes: 20 additions & 0 deletions fixit/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@


def celsius_to_fahrenheit(c: float) -> float:
"""Convert a temperature from Celsius to Fahrenheit.

Example:
celsius_to_fahrenheit(0) returns 32.
"""
return c * 9 / 5 + 32


def fahrenheit_to_celsius(f: float) -> float:
"""Convert a temperature from Fahrenheit to Celsius.

Example:
fahrenheit_to_celsius(32) returns 0.
"""
return (f - 32) * 5 / 9


def km_to_miles(km: float) -> float:
"""Convert kilometers to miles.

Example:
km_to_miles(1) returns approximately 0.6214.
"""
return km / KM_PER_MILE


def miles_to_km(miles: float) -> float:
"""Convert miles to kilometers.

Example:
miles_to_km(1) returns approximately 1.6093.
"""
return miles * KM_PER_MILE
6 changes: 5 additions & 1 deletion fixit/jsonfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@


def format_json(raw: str, indent: int = 2) -> str:
"""Format a JSON string with indentation.

Example:
format_json('{"name":"Sam"}') returns '{\n "name": "Sam"\n}'.
"""
try:
"""Return pretty-printed JSON."""
data = json.loads(raw)
return json.dumps(data, indent=indent)
except json.JSONDecodeError:
Expand Down
6 changes: 5 additions & 1 deletion fixit/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@


def generate(length: int = 12, use_digits: bool = True, use_symbols: bool = False) -> str:
"""Return a random password of the given length."""
"""Generate a random password of the given length.

Example:
generate(4, use_digits=False) returns a 4-character password.
"""
if length <= 0:
raise ValueError("Password length must be greater then 0!")
chars = string.ascii_letters
Expand Down
13 changes: 11 additions & 2 deletions fixit/textutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@


def is_palindrome(text: str) -> bool:
"""Return True if text reads the same forwards and backwards."""
"""Check whether text reads the same forwards and backwards.

Example:
is_palindrome("madam") returns True.
is_palindrome("hello") returns False.
"""
return text == text[::-1]


def reverse_words(text: str) -> str:
"""Reverse the order of words in text."""
"""Reverse the order of words in text.

Example:
reverse_words("hello world") returns "world hello".
"""
return " ".join(reversed(text.split()))
26 changes: 21 additions & 5 deletions fixit/wordcount.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import re
"""Word, line and character counting."""

import re


def count_words(text: str) -> int:
"""Return the number of words in text."""
return len(re.split(r'\s+',text.strip()))
"""Count the words in the given text.

Example:
count_words("hello world") returns 2.
count_words("one two three") returns 3.
"""
return len(re.split(r'\s+', text.strip()))


def count_lines(text: str) -> int:
"""Return the number of lines in text."""
"""Count the lines in the given text.

Example:
count_lines("hello\nworld") returns 2.
count_lines("hello") returns 1.
"""
return len(text.split("\n"))


def count_chars(text: str) -> int:
"""Return the number of characters in text."""
"""Count the characters in the given text.

Example:
count_chars("hello") returns 5.
count_chars("hi!") returns 3.
"""
return len(text)
Loading