paint-brush
Git Guide 0: How to Diff Filesby@artwalker
136 reads

Git Guide 0: How to Diff Files

by Ethan WangFebruary 22nd, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The rearrange1.py function is used to rearrange names in python. The rearrange2.py method is used for adding a name to a list of names. The name rearrange_name function takes a name and returns a string. It can also be used to add a number to a name.
featured image - Git Guide 0: How to Diff Files
Ethan Wang HackerNoon profile picture

Command:

$ cat rearrange1.py 

Code output:

#!/usr/bin/env python3

import re

def rearrange_name(name):

    result = re.search(r"^([\w .]*), ([\w .]*)$", name)

    if result == None:

        return name

    return "{} {}".format(result[2], result[1])

Command:

$ cat rearrange2.py 

Code output:

#!/usr/bin/env python3

import re

def rearrange_name(name):

    result = re.search(r"^([\w .-]*), ([\w .-]*)$", name)

    if result == None:

        return name


    return "{} {}".format(result[2], result[1])

Command:

$ diff rearrange1.py rearrange2.py 

Code output:

6c6

<     result = re.search(r"^([\w .]*), ([\w .]*)$", name)

---

>     result = re.search(r"^([\w .-]*), ([\w .-]*)$", name)

Command:

$ diff validations1.py validations2.py 

Code output:

5c5,6

<	assert (type(username) == str), "username must be a string"

--

>	if type(username) != str: 

> 	    raise TypeError("username must be a string")

11a13,15

>	    return False

>	# Usernames can't begin with a number

>	if username[0].isnumeric():

Command:

$ diff -u validations1.py validations2.py 

Code output:

--- validations1.py	2019-06-06 14:28:49.639209499 +0200

+++ validations2.py	2019-06-06 14:30:48.019360890 +0200

@@ -2,7 +2,8 @@

 

 

 def validate_user(username, minlen):

-    assert type(username) == str, "username must be a string"

+    if type(username) != str:

+        raise TypeError("username must be a string")

     if minlen < 1:

         raise ValueError("minlen must be at least 1")

     

@@ -10,5 +11,8 @@

         return False

     if not username.isalnum():

         return False

+    # Usernames can't begin with a number

+    if username[0].isnumeric():

+        return False

     return True