Regular Expression

A regular expression (or regex, or regexp) is a way to describe complex search patterns using sequences of characters.

Regular expressions are patterns that can be matched against strings. Regular expressions are important tools for text processing. Many text editors and most programming languages have some built-in support for regular expressions.

Regular expressions are used for searching through data. They allow you to search for pieces of text that match a certain form, instead of searching for a piece of text identical to the one you supply. For example, the regular expression [0-9]+ allows you to search through a file for any integer number.

Certain characters have special purposes in regular expressions. These are called meta-characters or meta-symbols. Meta-characters are not part of the strings that are matched by a pattern. Instead, they are part of the syntax that is used for representing patterns. Typically, the following characters are meta-characters:

      .   *   |   ?   +   (   )   [   ]   {   }   ^   $   \

These characters have special meaning in regular expressions. For example, parentheses are used for grouping, just as they are in arithmetic. If you want to use a meta-character as a regular character instead of with its special meaning, you have to “escape” it by preceding it with a backslash, such as \*\(\$, or \\.

MATCH ANY NUMBER LINE

We’ll start with a very simple example – Match any line that only contains numbers.

^[0-9]+$

Let’s walk through this piece-by-piece.

  • ^ – Signifies the start of a line.
  • [0-9] – Matches any digit between 0 and 9
  • + – Matches one or more instance of the preceding expression.
  • $ – Signifies the end of the line

We could replace [0-9] with \d which will do the same thing.

The great thing about this expression (and regular expressions in general) is that it can be used, without much modification, in any programing language.

import re

with open('test.txt', 'r') as f:
  test_string = f.read()
  regex = re.compile(r'^([0-9]+)$', re.MULTILINE)
  result = regex.findall(test_string)
  print(result)

 

Design Pattern

Initially, you can think of a pattern as an especially clever and insightful way of solving a particular class of problems. That is, it looks like a lot of people have worked out all the angles of a problem and have come up with the most general, flexible solution for it. The problem could be one you have seen and solved before, but your solution probably didn’t have the kind of completeness you’ll see embodied in a pattern.

The basic concept of a pattern can also be seen as the basic concept of program design: adding a layer of abstraction. Whenever you abstract something you’re isolating particular details, and one of the most compelling motivations behind this is to separate things that change from things that stay the same. Another way to put this is that once you find some part of your program that’s likely to change for one reason or another, you’ll want to keep those changes from propagating other changes throughout your code. Not only does this make the code much cheaper to maintain, but it also turns out that it is usually simpler to understand.

So the goal of design patterns is to isolate changes in your code.For example, inheritance can be thought of as a design pattern. It allows you to express differences in behavior (that’s the thing that changes) in objects that all have the same interface (that’s what stays the same). Composition can also be considered a pattern, since it allows you to change-dynamically or statically-the objects that implement your class, and thus the way that class works.

Classifying Patterns

The Design Patterns book discusses 23 different patterns, classified under three purposes. The three purposes are:

  1. Creational: how an object can be created. This often involves isolating the details of object creation so your code isn’t dependent on what types of objects there are and thus doesn’t have to be changed when you add a new type of object. The aforementioned Singleton is classified as a creational pattern, and later in this book you’ll see examples of Factory Method and Prototype.
  2. Structural: designing objects to satisfy particular project constraints. These work with the way objects are connected with other objects to ensure that changes in the system don’t require changes to those connections.
  3. Behavioral: objects that handle particular types of actions within a program. These encapsulate processes that you want to perform, such as interpreting a language, fulfilling a request, moving through a sequence (as in an iterator), or implementing an algorithm. This book contains examples of the Observer and the Visitor patterns.

I am discussing here only one pattern that is factory pattern.



class Dog:
    def __init__(self , name):
        self.name = name
    def speak(self):
        return "barks"
class Cat:
    def __init__(self , name):
        self.name = name
    def speak(self):
        return "Meoow"
    
    
    
def get_pet(pet = "dog"):
    pets = dict (dog = Dog('harm'), cat = Cat("Peace"))
    return pets[pet]


d = get_pet()

d.speak()

c= get_pet("cat")

c.speak()

 

Importance of Python

Pyhton is a good choice in the begginning of learning process of  programming languages. It can be used as a stepping stone into other programming languages. If you’re an absolute beginner and this is your first time working with any type of coding language, that’s something you definitely want. Python make it easy to understand some difficult concepts of programming languages.

Before starting any language you should know  some of its main characteristics.Python have following characteristics:

  • High Level Language
  • General Purpose language
  • Interpreted
  • Dynamic type

pythonlearn_thumbnail_1x1

Python is a general-purpose language, which means it can be used to build just about anything, which will be made easy with the right tools/libraries.Read More »