• Complain

Paul Gerrard [Paul Gerrard] - Lean Python: Learn Just Enough Python to Build Useful Tools

Here you can read online Paul Gerrard [Paul Gerrard] - Lean Python: Learn Just Enough Python to Build Useful Tools full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2016, publisher: Apress, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Paul Gerrard [Paul Gerrard] Lean Python: Learn Just Enough Python to Build Useful Tools
  • Book:
    Lean Python: Learn Just Enough Python to Build Useful Tools
  • Author:
  • Publisher:
    Apress
  • Genre:
  • Year:
    2016
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

Lean Python: Learn Just Enough Python to Build Useful Tools: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Lean Python: Learn Just Enough Python to Build Useful Tools" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Learn only the essential aspects of Python without cluttering up your mind with features you may never use. This compact book is not a best way to write code type of book; rather, the author goes over his most-used functions, which are all you need to know as a beginner and some way beyond.

Lean Python takes 58 Python methods and functions and whittles them down to 15: as author Paul Gerrard says, I havent found a need for the rest.


What Youll Learn
  • Discover lean Python and how to learn just enough to build useful tools
  • Use Python objects, program structure, I/O, modules and more
  • Handle errors and exceptions
  • Test your code
  • Access the Web; do searching; and persist data

Who This Book Is For
This book is aimed at three categories of reader: The experienced programmer if you already know a programming language, this book gives you a shortcut to understanding the Python language and some of its design philosophy.
You work in IT and need a programming primer you might be a tester who needs to have more informed technical discussions with programmers. Working through the examples will help you to appreciate the challenge of good programming.
First-timer you want a first book on programming that you can assimilate quickly to help you decide whether programming is for you.

Paul Gerrard [Paul Gerrard]: author's other books


Who wrote Lean Python: Learn Just Enough Python to Build Useful Tools? Find out the surname, the name of the author of the book and a list of all author's works by series.

Lean Python: Learn Just Enough Python to Build Useful Tools — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Lean Python: Learn Just Enough Python to Build Useful Tools" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make

Paul Gerrard 2016

Paul Gerrard , Lean Python , 10.1007/978-1-4842-2385-7_10

10. Searching

Paul Gerrard 1

(1) Maidenhead, Berkshire, UK

Searching for Strings

Searching for text in strings is a common activity and the built-in string function find() is all you need for simple searches. It returns the position (offset) of the find or if not found.

>>> txt="The quick brown fox jumps over the lazy dog"
>>> txt.find('jump')
20
>>> txt.find('z')
37
>>> txt.find('green')
-1
More Complex Searches

There are often circumstances when the search is not so simple. Rather than a simple string, we need to look for a pattern and extract the information we really want from the matched text. Suppose for example, we wanted to extract all the URLs in links on a web page. Here are some example lines of HTML text from a real web page.

1
2
3

    4
  • PyPI Tutorial
  • There is quite a lot going on in the text here.

    • Line 1 refers to an RSS feed.

    • Line 2 has an href attribute, but it refers to a Cascading Style Sheets (CSS) file, not a link.

    • Line 3 is a true link but the URL is relative; it doesnt contain the web site part of the URL.

    • Line 4 is a link to an external site.

    How can we hope to use some software to find the links that we care about? Well, this is where regular expressions come in.

Introducing Regular Expressions

Regular expressions are a way of using pattern matching to find the text we are interested in. Not only are patterns matched, but the re module can extract the data we really want out of the matched text.

Many more examples could be written, and in fact there are whole books written about regular expressions (e.g., [16], [17]). There are many web sites, but the most useful is probably http://www.regular-expressions.info .

Note

A regex is a string containing both text and special characters that define a pattern that the re functions can use for matching.

Simple Searches

The simplest regex is a text string that you want to find in another string, as shown in Table .

Table 10-1. Finding a Simple String

Regex

String Matched

jumps

jumps

The Queen

The Queen

Pqr123

Pqr123

Using Special Characters

There are special characters, listed in Table , that influence how the match is to be performed.

Table 10-2. Using Special Characters

Symbols

Description

Example

literal

Match a literal string

Jumps

re1|re2

Match string re1 OR re2

Yes|No

.

Match any single character (except \n)

J.mps

^

Match start of string

^The

$

Match end of string

well$

*

Match 0 or more occurrences of preceding regex

[A-Z]*

+

Match 1 or more occurrences of preceding regex

[A-Z]+

?

Match 0 or 1 occurrences of preceding regex

[a-z0-9]?

{m,n}

Match between m and n occurrences of the preceding regex (n optional)

[0-9]{2,4}

[...]

Match any character from character class

[aeiou]

[x-y]

Match any character from range

[0-9],[A-Za-z]

[^...]

Do not match any character from character class

[^aeiou]

There are a number of special characters, listed in Table , that can be matched, too.

Table 10-3. Searching with Special Characters

Special Character

Description

Example

\d

Match any decimal digit

BBC\d

\w

Match any alphanumeric character

Radio\w+

\s

Match any whitespace character

The\sBBC

Table gives some examples of regular expressions and the strings that they would match.

Table 10-4. Regular Expressions and Matching Strings

Regex

String(s) Matched

smith|jones

smith, jones

UNE..O

Any two characters between UN and O; e.g., UNESCO, UNEzyO, UNE99O

^The

Any string that starts with The

end$

Any string that ends with end

c[aiou]t

cat, cit, cot, cut

[dg][io][gp]

dig, dip, dog, dop, gig, gip, gog, gop

[a-d][e-i]

2 chars a/b/c/d followed by e/f/g/h/i

NoteRegexes can use any combination of text and special characters, so they can look extremely complicated sometimes. Start simple.

Finding Patterns in Text

Finding substrings in text is fine, but often we want to find patterns in text, rather than literal strings. Suppose we wanted to extract numeric values, phone numbers, or web site URLs from text. How do we do that? This is where the real power of regular expressions lies.

Here is an example regex:

\s[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}[\s]

Can you guess what it might find? It is a regex for finding e-mail addresses in text. At first glance, this looks pretty daunting, so lets break it down into its constituent parts. First, the regex refers only to uppercase letters (to reduce the length of the regex), so this assumes that the string to be searched has already been uppercased.

There are six elements to this regex:

1 \s

2 [A-Z0-9._%+-]+

3 @

4 [A-Z0-9.-]+

5 \.

6 [A-Z]{2,4}

7 [\s\.]

Leading whitespace

One or more characters

@ character

A-Z , 0-9 .

Dot character

2 to 4 text characters

Whitespace or full stop

Obviously, you need to know the rules for the pattern you search for and there are specific rules for the construction of e-mail addresses.

Here is the file remail.py .

1 import re # The RegEx library
2 #
3 # our regular expression (to find e-mails)
4 # and text to search
5 #
6 regex = '\s[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}[\s]'
7 text="""This is some text with x@y.z embedded e-mails
8 that we'll use as@example.com
9 some lines have no email addresses
10 others@have.two valid email@addresses.com
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Lean Python: Learn Just Enough Python to Build Useful Tools»

Look at similar books to Lean Python: Learn Just Enough Python to Build Useful Tools. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Lean Python: Learn Just Enough Python to Build Useful Tools»

Discussion, reviews of the book Lean Python: Learn Just Enough Python to Build Useful Tools and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.