Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 14, 2021 02:16 pm GMT

Refactoring and Rebasing (Lab 5)

A few things about my SSG script have always irked me, so this lab was a perfect opportunity to clean those up.

Improvements

The first thing I wanted to fix was this very long line of code:

# Process header markdownheaderTag = lambda s: '{endpTag}<h{size}>{regexContent}</h{size}>{pTag}'.format(endpTag="</p>

" if s.group(1)=="
" else "", size=s.group(2).count('#'), regexContent=s.group(3), pTag="

<p>" if s.group(4)=="
" else "")content = re.sub(r'(|(?<!
)
|<p>)(#{1,5})\s(.*)(<\/p>|(?<!<\/p>)
|$)'
, headerTag, content)

I was able to greatly simplify the lambda expression and reduce the footprint:

# Process header markdownheaderTag = lambda s: '<h{size}>{content}</h{size}>
'.format(size=s.group(2).count('#'), content=s.group(3))content = re.sub(r'(|(?<!
)
|<p>)(#{1,5})\s(.*)(<\/p>|(?<!<\/p>)
|$)'
, headerTag, content)

The next thing that always bothered me was the fact that everything was in one big file. So I split ssg.py into three different classes: ssg.py, SSGParser.py, and SSGUtil.py. SSGParser.py, as the name suggests, contains everything to do with parsing, SSGUtil.py contains all the "helper" functions, and ssg.py now only consists of main. This step really cleaned up my code and I don't cringe when I see it anymore.
Image description

Rebasing

After committing all my improvements, I performed an interactive rebase to squash the commits into one, informative commit.
Image description

Outcomes

All in all, I'm very glad I learned about changing the history of a repo by rebasing and how to squash commits. In past labs, I'd have a mini heart attack whenever I realized I forgot something!


Original Link: https://dev.to/ar/refactoring-and-rebasing-lab-5-400k

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To