Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.
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
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ SR No | Project | Author
98 | [PNG to ICO converter](https://github.com/chavarera/python-mini-projects/tree/master/projects/convert_png_images_to_ico_format)| [weicheansoo](https://github.com/weicheansoo)
99 | [Find IMDB Ratings](https://github.com/chavarera/python-mini-projects/tree/master/projects/Find_imdb_rating)| [Utkarsh Bajaj](https://github.com/utkarshbajaj)
100 | [Terminal Based Hangman Game](https://github.com/chavarera/python-mini-projects/tree/master/projects/Terminal_Based_Hangman_Game)| [neohboonyee99](https://github.com/neohboonyee99)
101 | [Diff Utility](https://github.com/Python-World/python-mini-projects/tree/master/projects/Diff_Util)| [KILLinefficiency](https://github.com/KILLinefficiency)
62 changes: 62 additions & 0 deletions projects/Diff_Util/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Diff Utility

This program is a minimal clone of the UNIX ``diff`` program.

``diff.py`` takes two file names as command-line arguments and compares them for changes.

## Prerequisites
* Rich: ``rich==10.11.0``

## How to run the script
**Running on Windows:**

```
python diff.py <orignal_file> <changed_file>
```

**Running on Linux / macOS:**

```
./diff.py <orignal_file> <changed_file>
```

## Usage Example

Consider two files ``v1`` and ``v2``:

**v1**:
```
Bruce
Alfred
Jason
```

**v2**:
```
Batman
Alfred
Red Hood
Joker
Ra's Al Ghul
```

On running ``./diff.py v1 v2``, you'll get the following output:
```

[-] Line 1: Bruce
[+] Line 1: Batman

[-] Line 3: Jason
[+] Line 3: Red Hood

[+] Line 4: Joker

[+] Line 5: Ra's Al Ghul

```

## Screenshot
![Python Diff Utility](diff_util.jpg)

# KILLinefficiency
Github Link: [KILLinefficiency](https://www.github.com/KILLinefficiency)
48 changes: 48 additions & 0 deletions projects/Diff_Util/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

# sys: for reading command-line arguments.
# rich: for coloring the text.
import sys
from rich import print

# Print Usage message if enough arguments are not passed.
if len(sys.argv) < 3:
print("Usage:")
print("\tMust provide two file names as command-line arguments.")
print("\tdiff.py <orignal_file> <changed_file>")
exit(1)

orignal = sys.argv[1]
changed = sys.argv[2]

# Read the contents of the files in lists.
orignal_contents = open(orignal, "r").readlines()
changed_contents = open(changed, "r").readlines()

color = "green"
symbol = f"[bold {color}][+]"

print()

# Determine which file has changed much.
if len(changed_contents) <= len(orignal_contents):
color = "red"
symbol = f"[bold {color}][-]"
smallest_sloc, largest_sloc = changed_contents, orignal_contents
else:
smallest_sloc, largest_sloc = orignal_contents, changed_contents

# Go over all the lines to check the changes.
for line in range(0, len(smallest_sloc)):
if orignal_contents[line] == changed_contents[line]:
# Ignore if the lines are same.
continue
else:
# Display the changes on the respective lines of the files.
print(f"[bold red][-] Line {line + 1}:[/bold red] {orignal_contents[line]}", end = "")
print(f"[bold green][+] Line {line + 1}:[/bold green] {changed_contents[line]}")

# Show the additions [+] or deletions [-] for the file that is the largest.
if line == len(smallest_sloc) - 1:
for new_line in range(line + 1, len(largest_sloc)):
print(f"{symbol} Line {new_line + 1}:[/bold {color}] {largest_sloc[new_line]}")
Binary file added projects/Diff_Util/diff_util.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions projects/Diff_Util/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rich==10.11.0