180
Chapter 7
Whenever you’re tackling a new project, it can be tempting to dive right
into writing code. But more often than not, it’s best to take a step back and
consider the bigger picture. I recommend first drawing up a high-level plan
for what your program needs to do. Don’t think about the actual code yet—
you can worry about that later. Right now, stick to broad strokes.
For example, your phone and email address extractor will need to do
the following:
1. Get the text off the clipboard.
2. Find all phone numbers and email addresses in the text.
3. Paste them onto the clipboard.
Now you can start thinking about how this might work in code. The
code will need to do the following:
1. Use the
pyperclip
module to copy and paste strings.
2. Create two regexes, one for matching phone numbers and the other
for matching email addresses.
3. Find all matches, not just the first match, of both regexes.
4. Neatly format the matched strings into a single string to paste.
5. Display some kind of message if no matches were found in the text.
This list is like a road map for the project. As you write the code, you
can focus on each of these steps separately. Each step is fairly manageable
and expressed in terms of things you already know how to do in Python.
Dostları ilə paylaş: