patternpythonMinor
Command-line utility to capitalize long words in a file
Viewed 0 times
filelinewordslongutilitycapitalizecommand
Problem
I have been developing a capitalizer in Python. This is actually a script that an end user will type at their terminal/command prompt. I have made this script to:
Here's some code:
A lot more features still have to be added but this script does the stuff it is made for.
What I want (from you):
I want this script to be emerge as a command line utility. So,
Please also post links to make my script and me even more mature.
- Taking the first argument as a file name and process it.
- Don't process words that have 3 or less alphabet.
Here's some code:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from os import linesep
from string import punctuation
from sys import argv
script, givenfile = argv
with open(givenfile) as file:
# List to store the capitalised lines.
lines = []
for line in file:
# Split words by spaces.
words = line.split()
for i, word in enumerate(words):
if len(word.strip(punctuation)) > 3:
# Capitalise and replace words longer than 3 letters (counts
# without punctuation).
if word != word.upper():
words[i] = word.capitalize()
# Join the capitalised words with spaces.
lines.append(' '.join(words))
# Join the capitalised lines by the line separator.
capitalised = linesep.join(lines)
print(capitalised)A lot more features still have to be added but this script does the stuff it is made for.
What I want (from you):
I want this script to be emerge as a command line utility. So,
- How can I make this a better command line utility?
- Could I have written in a better way?
- Can this script be faster?
- Are there any flaws?
- Other than GitHub, how can I make it available to the world (so that it is easier to install for end users)? because GitHub is for developers.
Please also post links to make my script and me even more mature.
Solution
As a Command Line Utility
The Argparse module is designed to facilitate the creation of command line utilities. It makes processing command line arguments much easier, and will even automatically display usage information if the user incorrectly enters arguments.
Potential Flaws
Your program does not preserve whitespace. This could be by design but I suspect it is not.
Example:
the quick brown fox
will become
the Quick Brown Fox
The Argparse module is designed to facilitate the creation of command line utilities. It makes processing command line arguments much easier, and will even automatically display usage information if the user incorrectly enters arguments.
Potential Flaws
Your program does not preserve whitespace. This could be by design but I suspect it is not.
Example:
the quick brown fox
will become
the Quick Brown Fox
Context
StackExchange Code Review Q#16007, answer score: 8
Revisions (0)
No revisions yet.