Saturday 31 December 2016

What is a metaclass in Python?

A meta-class is the class of a class. Like a class defines how an instance of the class behaves, a meta-class defines how a class behaves. A class is an instance of a meta-class.
Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances.
or meta-class is simply an object that constructs classes.
A meta-class is most commonly used as a class-factory.
Python creates a new class (when it executes the 'class' statement) by calling the metaclass. Combined with the normal __init__ and __new__ methods.
The main purpose of a meta-class is to change the class automatically, when it's created.
meta-classes are the secret sauce that make 'class' work. The default meta-class for a new style object is called 'type'.
meta-classes take 3 args. 'name', 'bases' and 'dict'

Why do we need meta-class?:
1.intercept a class creation
2.modify the class
3.return the modified class
4.You can use OOP. meta-class can inherit from meta-class, override parent methods. meta-classes can even use meta-classes.
5.You can hook on __new__, __init__ and __call__. Which will allow you to do different stuff. Even if usually you can do it all in __new__, some people are just more comfortable using __init__.
6.One use for meta-classes is adding new properties and methods to an instance automatically.

Example:
class Bus(object):
    def __init__(self, make, model, year, color):
        self.make = make
        self.model = model
        self.year = year
        self.color = color

    @property
    def description(self):
        """ Return a description of this bus. """

        return "%s %s %s %s" % (self.color, self.year, self.make, self.model)

At run time, Bus itself is an instance of type. The source code of the Bus class, shown above, does not include such details as the size in bytes of Bus objects, their binary layout in memory, how they are allocated, that the __init__ method is automatically called each time a Bus is created, and so on. These details come into play not only when a new Bus object is created, but also each time any attribute of a Bus is accessed. Here the meta-class - type - controls these details of Bus behavior. They can be overridden by using a different meta-class instead of type.

The above code contains some redundant code to do with the four attributes make, model, year, and color. It is possible to eliminate some of this redundancy using a meta-class. In Python, a meta-class is most easily defined as a subclass of type.

class AttributeInitType(type):
     def __call__(self, *args, **kwargs):
         """ Create a new instance. """

         # First, create the object in the normal default way.
         obj = type.__call__(self, *args)

         # Additionally, set attributes on the new object.
         for name, value in kwargs.items():
             setattr(obj, name, value)

         # Return the new object.
         return obj

This meta-class only overrides object creation. All other aspects of class and object behavior are still handled by type.
Now the class Bus can be rewritten to use this meta-class.

class Bus(object, metaclass=AttributeInitType):
     @property
     def description(self):
         """ Return a description of this car. """
         return " ".join(str(getattr(self, attr, "Unknown"))
                         for attr in self.__slots__)

Bus objects can then be instantiated like this:
new_bus = Bus(make='Volvo', model='AC', year=2010, color='Black')
 old_bus=Bus(make='Ashok Leyland', model='Sleeper', year=2000)

What is Generator in Python?

The object returned by range() is known as generator.

Instead of storing the entire range, [0,1,2,..,9], in memory, the generator stores a definition "for i in range(20): and computes the next value only when needed ( lazy-evaluation).
Generators are iterators because they implement the iterator protocol, so you can iterate over them.

Essentially, a generator allows you to return a list like structure, but here are some differences:

1.A list stores all elements when it is created. A generator generates the next element when it is needed.
2.A list can be iterated over as much as you need, a generator can only be iterated over exactly once.
3.A list can get elements by index, a generator cannot .It only generates values once, from start to end.(It's because they do not store all the values in memory, they generate the values on the fly)

Generator uses Yield keyword:
Yield is a keyword that is used like return, except the function will return a generator.

what is difference between range and xrange?

range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.range returns a static list at runtime.
xrange is a sequence object that evaluates lazily.
xrange is not exactly a generator but it evaluates lazily and acts like a generator. xrange(x).__iter__() is a generator.
xrange only stores the range parameters and generates the numbers on demand.
xrange returns an iterator and only keeps one number in memory at a time. range keeps the entire list of numbers in memory.
The advantage of the xrange type is that an xrange object will always take the same amount of memory, no matter the size of the range it represents. There are no consistent performance advantages.

When to use which?

Use xrange if you want to generate a list for a gigantic range, say 1 billion, especially when you have a "memory sensitive system" like a cell phone.
Use range if you want to iterate over the list several times.
PS: Python 3.x's range function == Python 2.x's xrange function.

Example:
import time

for x in range(1, 10):

    t = time.time()
    [v*10 for v in range(1, 10000)]
    print "range:  %.4f" % ((time.time()-t)*100)

    t = time.time()
    [v*10 for v in xrange(1, 10000)]
    print "xrange: %.4f" % ((time.time()-t)*100)


In Python 2.x, range returns a list, but in Python 3.x range returns an immutable sequence, of type range.

Python 2.x:

>>> type(range(10))
<type 'list'>
>>> type(xrange(10))
<type 'xrange'>

Python 3.x:
>>> type(range(10))
<class 'range'>



Thursday 29 December 2016

What is hmac?

Keyed-Hash Message Authentication Code( hmac module)

Any cryptographic hash function, such as MD5 or SHA-1, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-MD5 or HMAC-SHA1 accordingly.

HMAC-SHA1 and HMAC-MD5 are used within the IPsec and TLS protocols.

Example:
import hmac
digest_maker = hmac.new(b'secret-shared-key-goes-here')
f = open('D:\\configuration.txt', 'rb')
try:
    while True:
        block = f.read(1024)
        if not block:
            break
        digest_maker.update(block)
finally:
    f.close()
digest = digest_maker.hexdigest()
print(digest)

Output: 8d0cc23dbc570e65c5bf2b5cdc72aae3

Wednesday 28 December 2016

what is hash function?

A hash function  converts a numerical input value into another compressed  value. The input to the hash function is of arbitrary length but output is always of fixed length.
Values returned by a hash function are known as hash values, hash codes, digests, simply hashes or checksum.

Most used hash functions are:
1.MD5:Message digest algorithm producing a 128 bit hash value. This is widely used to check data integrity. It is not suitable for use in other fields due to the security vulnerabilities of MD5.

2.SHA: Group of algorithms designed by the U.S's NSA that are part of the U.S Federal Information processing standard. These algorithms are used widely in several cryptographic applications. The message length ranges from 160 bits to 512 bits.

There is module in python "hashlib"
This module implements a common interface to many different secure hash and message digest algorithms.It includes  FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384,SHA512 ,RSA’s MD5 algorithm.
Constructors for hash algorithms  in this module are md5(), sha1(), sha224(), sha256(), sha384(), and sha512().

 Example (MD5):
>>>import hashlib

>>> print(hashlib.algorithms_available)
{'dsaEncryption', 'DSA', 'SHA384', 'MD5', 'ripemd160', 'SHA256', 'sha256', 'SHA224', 'SHA', 'MD4', 'sha', 'RIPEMD160', 'sha224', 'dsaWithSHA', 'SHA1', 'md4', 'whirlpool', 'sha512', 'DSA-SHA', 'ecdsa-with-SHA1', 'sha384', 'md5', 'sha1', 'SHA512'}

>>> print(hashlib.algorithms_guaranteed)
{'sha512', 'sha384', 'sha256', 'md5', 'sha1', 'sha224'}

>>>x=hashlib.md5()

>>> x.digest()
b'\xd7\xca:`[\xab\xe4\x81\xb20\x89\x14)@~\xed'

>>> x.digest_size
16
>>> x.block_size

64
>>> hashlib.sha224(b"I am a new bloggers").hexdigest()
'77c3226cf769139e5923faeb2082dfa922c73ced65d14604c9c2fdec'

Example2(SHA1):
>>>import hashlib

>>> y=hashlib.sha1(b"I am a new bloggers") #use sha224,sha 256,sha384,sha512

>>> y
<sha1 HASH object @ 0x02DE4750>

>>> z=y.hexdigest()

>>> z

'5455581070cecd69983223af1d1296b02fe62e24'


Why Hash Function?
1.Hash functions are used inside some cryptographic algorithms,in digital signatures,message authentication codes, manipulation detection, fingerprints,checksums,hash tables,password storage and much more.
2.To check for duplicate data or files, to check data integrity when you transmit information over a network, to securely store passwords in databases, or maybe some work related to cryptography.

Important:
1.It is a one way function(This means if f is the hashing function, calculating f(x) is pretty fast and simple, but trying to obtain x again will take years).
2.Hash functions are not a cryptographic protocol, they do not encrypt or decrypt information, but they are a fundamental part of many cryptographic protocols and tools.



Program Related to Hash Function:Password Hash Function:
import uuid
import hashlib

def hash_password(password):
    # uuid is used to generate a random number
    x = uuid.uuid4().hex
    return hashlib.sha256(x.encode() + password.encode()).hexdigest() + ':' +x
   
def check_password(hashed_password, user_password):
    password, x= hashed_password.split(':')
    return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()

new_password = input('Please enter a password: ')
hashed_password = hash_password(new_password)
print('The string to store in the db is: ' + hashed_password)
old_password = input('Now please enter the password again to check: ')
if check_password(hashed_password, old_password):
    print('Right password')
else:
    print('Hey passwords does not match')

What is urllib.request module?


urllib.request module consists of  functions and classes which help in opening URL's.

It has got following methods:
1. urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
 url : name of the url,It can be a string or Request object.
urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests.
 timeout:is timeout in seconds for blocking operations.If  it is not set default timeout will be considered.
The cafile and capath parameters are optional, specify  CA certificates for HTTPS requests. cafile should point to a single file containing a bundle of CA certificates, whereas capath should point to a directory of hashed certificate files.
The cadefault parameter is ignored.

2.urllib.request.install_opener(opener)






















Note:This is according to  versions Python 3 and above.

Monday 26 December 2016

What is Regex in Python?

Regex(Regular Expression) specifies a set of string that can be matched
There is module in Python "re" (Regular expression) which is to be imported.
                                           "import re"

Important:
1.Regular Expressions can be concatenated to from another regular expression.
2.It can consist of both special and ordinary characters.
3.Both pattern and strings can be searched and they can be unicode or 8 bit strings.
   but a mixture of unicode and 8 bit strings is not supported both while searching and replacement.

what is raw-string and Unicodel?

Without  r, backslashes are treated as escape characters. With the r, backslashes are treated as literals.
using r means string appears as it is "\| isn't given a special meaning.
Example:
1.a="Story of an Ant\" Grasshopper"
Output: a
          'Story of an Ant" Grasshopper'

2,a=r"Story of an Ant\" Grasshopper"
Output: a
          'Story of an Ant\\" Grasshopper'


Important:
1.r'x', r'''xy''', r"xyz", r"""xyzf""" are all byte string.
2.There is nothing like raw string type .
3.u"xyz" is unicode string where as r"xyz","xyz" are normal byte strings.
4.All require same memory
Example:import sys
>>> sys.getsizeof(u'abcdefgh')
33
>>> sys.getsizeof('abcdefgh')
33
>>> sys.getsizeof(r'abcdefgh')
33

Sunday 25 December 2016

What is iterator in Python?

The iterator function(iter()) takes an iterable object and returns a iterator.And it will return one object at a time.
What is an iterable?
Anything such as a list, set,range or dict with a built-in protocol for visiting each element in a certain order is Iterable.
Example:x=[1,2,3,4,5]
               y =iter(x)
               y.next()    #gives output 1
               y.next()    #gives output 2
                till y.next()   #gives output 5
   after this it will throw StopIteration (error)

Important:

1.Iterators are implemented as classes.
2.Iteration is a process implying iterables (implementing the __iter__() method) and iterators (implementing the __next__() method).

Why use iterators?

1.Iterators save resources.

What is Enumerate in Python?

Enumerate  function is used to iterate through a list while keeping track of the list items' indices.
Syntax:enumerate(elements)
Example : x = ('A', 'B', 'C')
                 for i,y in enumerate(x):
                      print(i,x)
                Output: 0   A              #Indexing starts from 0 by Default  
                             1   B
                             2   C
             
Important:
1.By default enumerate counts from 0,but you could index it accordingly by providing 2nd argument.

Example:for i,y in enumerate(x,50): # 2nd argument value is set to 50
                      print(i,x)
                Output: 50   A                     #Indexing starts from 50 as it is given as argument in function
                             51   B
                             52   C















Bored of Learning ?Wanna go out on a tour(full of adventures,excitement) please visit www.desistreets.com .

Saturday 24 December 2016

What is Closure in python?

A closure is a function object.A closure remembers values from the enclosing lexical scope even when the program is no longer in the enclosing scope.
Inner function in a nested function can be called as a closure,but not always.
A closure occurs when a function has access to local variables from an enclosing function that has completed execution.
Example:def make_out_love(x):
                      def love():
                            print(x)
                      return love
                love = make_out_love('Tonight We are making love')
                love()

A nested function is not closure if:
1.Nested functions dont access variables that are local to enclosing scopes.
2.If Nested functions are executed outside the scope.

Example:def make_out_love(x):
                           def love(x=x):
                                 print(x)
                           return love

              love = make_out_love('Tonight We are making love')
              love()

1.Closures are functions that inherit variables from enclosing environment.
2.Function should use free variables in order to be a closure
3.The attribute __closure__ saves the free variables.
4.Every function in python has closure attributes but it does not save any content if there are no free variables.
5.The closure attribute(__closure__) returns a tuple of cell objects which contain details of the variables defined in the enclosing scope.
Note:Every Function has a __closure__ attribute,if there is no data for __closure__ attribute  it is assigned None.

Why to and who use Closures?
1.Decorators in python makes extensive use of closures.

What is Python Lambda?

      

Lambda operator or Lambda Function is used to create functions without name.

Syntax: lambda arguments :expression

Example:IN:sum = lambda x, y : x + y   #lambda expression with x,y as arguments, x+y expression
               IN:sum(3,4)   #sum function here x=3 and y=4 
               OUT: 7
                The expression (x+y) is evaluated and returned

               The above is similar to
               IN:def  sum(x,y):  #defining a function sum which takes x and y as arguments
                            return x+y

In Python normal functions are defined using "def" keyword where as anonymous functions are defined using "lambda" keyword.


Important  :

1.Lambda functions can have any number of arguments but only one expression.
2.lambda creates a function and return itself.
3.lambda can be used inside list,dictionary etc.....


Why Use Lambda Functions?

1.Is used when we require nameless function.
2.When we need to pass  functions to other function.
3.lambda is useful when developing GUI .
They are mainly used along with :
1.filter()
2.map()
3.reduce()