Sunday, March 10, 2019

Wednesday, February 24, 2016

Ordinal Numbers

In set theory, an ordinal number (or ordinal) is one generalization of the concept of a natural number. With the natural numbers in increasing order {0, 1, 2, …}, each number counts the number of natural numbers that precede it. The ordinals extend this concept beyond the natural numbers by having each ordinal represent the set of all ordinals that are less than it. For instance, the ordinal 2 represents the set {0, 1} and the smallest infinite ordinal, written ω, represents the set of all natural numbers, {0, 1, 2, …}. Some ordinals, like 2, are successor ordinals by virtue of having an immediate predecessor.

Detailed Reading :
https://en.wikipedia.org/wiki/Ordinal_number

What it means to programming
--------------------------------------
Check "man ascii" on linux

>>> ord('$')
36
>>> ord('A')
65
>>> symbols='$¢£¥€¤'
>>> codes = [ord(symbol) for symbol in symbols]
>>> codes
[36, 162, 163, 165, 8364, 164]


Tuesday, February 23, 2016

Python Jargon Reference


ABC : 
Abstract Base Class. Same is Abstract class in C++. It can be used for inheriting the subclass.

accessors : 
Normally getter and setter fucntions for a given attributes. Some refer accessors as getters only and use mutators for setters.

aliasing :
Assigning more than one name to an object. Example a = [1,2,3,4] and b=a. In this case both a and b points to the same object.

binary sequence: 
Generally refers to sequence of binary bytes. The built-in binary sequence types are byte and bytes. Example
>>> key = bytes([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
>>> print(key)
b'\x13\x00\x00\x00\x08\x00'
>>> key = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
>>> key
bytearray(b'\x13\x00\x00\x00\x08\x00')


callable object:
An object that can be invoked with the call operator (), to return a result or to perform some action. There are seven flavors of callable objects in Python: user-defined functions, built-in functions, built-in methods, instance methods, generator functions, classes, and instances of classes that implement the __call__ special method.

cheese shop:
Original name of the Python Package Index (PyPI), https://cheeseshop.python.org

collection:
Generic term for data structures made of items that can be accessed individually. Some collections can contain objects of arbitrary types (see container) and others only objects of a single atomic type (see flat sequence).
list and bytes are both collections, but list is a container, and bytes is a flat sequence.

constructor:
Informally, the __init__ instance method of a class is called its constructor, because its semantics is similar to that of a Java constructor. However, a fitting name for __init__ is initializer, as it does not actually build the instance, but receives it as its self argument. The constructor term better describes the __new__ class method, which Python calls before __init__, and is responsible for actually creating an instance and returning it. See initializer.

container:
An object that holds references to other objects. Example : List of ints, int is an object by itself.

duck typing:
A form of polymorphism where functions operate on any object that implements the appropriate methods, regardless of their classes or explicit interface declarations.

dunder:
Shortcut to pronounce the names of special methods and attributes that are written with leading and trailing double-underscores (i.e., __len__ is read as “dunder len”).

eager:
An iterable object that builds all its items at once. In Python, a list comprehension is eager.

first-class function:
Any function that is a first-class object in the language (i.e., can be created at runtime, assigned to variables, passed as an argument, and returned as the result of another function). Python functions are first-class functions.

generator function:
A function that has the yield keyword in its body. When invoked, a generator function returns a generator. Example: Function to generate fibnocci numbers.

hashable:
An object is hashable if it has both __hash__ and __eq__ methods, with the constraints that the hash value must never change and if a == b then hash(a) == hash(b) must also be True. Most immutable built-in types are hashable,
but a tuple is only hashable if every one of its items is also hashable.
>>> a=10
>>> b=a
>>> a==b
True
>>> hash(a)==hash(b)
True
>>> b=30
>>> hash(a)==hash(b)
False
>>>
Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().

weak reference:
A special kind of object reference that does not increase the referent object reference count. Weak references are created with one of the functions and data structures in the weakref module.

higher-order function:
A function that takes another function as argument, like sorted, map, and filter, or a function that returns a function as result, as Python decorators do.

iterable:
Any object from which the iter built-in function can obtain an iterator. An iterable object works as the source of items in for loops, comprehensions, and tuple unpacking. Objects implementing an __iter__ method returning an
iterator are iterable. Sequences are always iterable; other objects implementing a __getitem__ method may also be iterable.

iterator:
Any object that implements the __next__ no-argument method, which returns the next item in a series, or raises StopIteration when there are no more items. Python iterators also implement the __iter__ method so they are also
iterable. Classic iterators, according to the original design pattern, return items from a collection. A generator is also an iterator, but it’s more flexible. See generator.

metaclass:
A class whose instances are classes. By default, Python classes are instances of type, for example, type(int) is the class type, therefore type is a metaclass. User-defined metaclasses can be created by subclassing type.

refcount:
The reference counter that each CPython object keeps internally in order to determine when it can be destroyed by the garbage collector.

referent:
The object that is the target of a reference. This term is most often used to discuss weak references.

sequence:
Generic name for any iterable data structure with a known size (e.g., len(s)) and allowing item access via 0-based integer indexes (e.g., s[0]). The word sequence has been part of the Python jargon from the start, but only with
Python 2.6 was it formalized as an abstract class in collections.abc.Sequence.

serialization:
Converting an object from its in-memory structure to a binary or text-oriented format for storage or transmission, in a way that allows the future reconstruction of a clone of the object on the same system or on a different one.
The pickle module supports serialization of arbitrary Python objects to a binary format.

singleton:
An object that is the only existing instance of a class—usually not by accident but because the class is designed to prevent creation of more than one instance. There is also a design pattern named Singleton, which is a recipe for coding such classes. The None object is a singleton in Python.

strong reference:
A reference that keeps an object alive in Python. Contrast with weak reference.

view:
Python 3 views are special data structures returned by the dict methods .keys(), .values(), and .items(), providing a dynamic view into the dict keys and values without data duplication, which occurs in Python 2 where those methods
return lists. All dict views are iterable and support the in operator. In addition, if the items referenced by the view are all hashable, then the view also implements the collections.abc.Set interface. This is the case for all views returned by the .keys() method, and for views returned by .items() when the values are also hashable.

Sunday, February 21, 2016

Slicing and List Comprehension in Python

Slicing in python.

This lets you work on specific portions of the given sequence(list/str/bytes). Read it as Slice a pound of bread and give half to brother and keep half for yourself.

Slicing can be extended to any Python class that implements the __getitem__ and __setitem__ special methods.

Syntax :

gucalist[start:end], where start is inclusive and end is exclusive.

a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print('First four:', a[:4])
print('Last four: ', a[-4:])
print('Middle two:', a[3:-3])

>>>
First four: ['a', 'b', 'c', 'd']
Last four:  ['e', 'f', 'g', 'h']
Middle two: ['d', 'e']


Starting from the first, zero is understood.
a[:5] = a[0:5]

Slicing till the end,   "end" is understood.

a[5:] = a[5:len(a)]

Playing with negative index

a[:]      # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
a[:5]     # ['a', 'b', 'c', 'd', 'e']
a[:-1]    # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
a[4:]     #                     ['e', 'f', 'g', 'h']
a[-3:]    #                          ['f', 'g', 'h']
a[2:5]    #           ['c', 'd', 'e']
a[2:-1]   #           ['c', 'd', 'e', 'f', 'g']
a[-3:-1]  #                          ['f', 'g']


NOTES :
1. Slicing operation returns the whole new object.. Keeping the original as it is.
b = a[4:]
print('Before:   ', b)
b[1] = 99
print('After:    ', b)
print('No change:', a)

>>>
Before:    ['e', 'f', 'g', 'h']
After:     ['e', 99, 'g', 'h']
No change: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']


List Comprehension 
===================
Compute the sqaure of each numbers in the list

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares = [x**2 for x in a]
print(squares)

>>>
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

what if you wanted only the squares of the numbers in the given list, which are divisible by 3

squares = [x**2 for x in a if x%3==0]

List comprehension for dicts

>>> g_fam= {'ravi':33, 'rash':31, 'tan':6, 'sam':1}
>>> age_dict = {age:name for name, age in g_fam.items()}
>>> print(age_dict)
{1: 'sam', 33: 'ravi', 6: 'tan', 31: 'rash'}
>>>


More than two expressions in list comprehension

Flatting the matrix into a single list

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat = [x for row in matrix for x in row]
print(flat)

NOTE : These expressions run in the order provided from left to right. So in this case take each row first and loop over it.

Multiple loops is replicating the two-level deep layout of the input list. For example, say you want to square the value in each cell of a two-dimensional matrix. This expression is noisier because of the extra [] characters, but it’s still easy to read.

squared = [[x**2 for x in row] for row in matrix]
print(squared)

>>>
[[1, 4, 9], [16, 25, 36], [49, 64, 81]]


multiple if conditions in a list comprehension

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
c = [x*x for x in a if x > 4 and x % 2 == 0] # get the squares of each if it even and greater than 4.



Tuesday, February 2, 2016

Define/Encode/Serialize

Once upon a time, packing the structure construct defined in C was considered as an art of programming. Literally all the signalling protocol simulators I worked with had this one cryptic piece of packing and unpacking the message. 

Now a days it is much more convenient with all the different efficient methods. In fact most of them come with the underlying RPC mechanism too. Below are some, which can be used.  

1. Google Protocol Buffers. 
https://developers.google.com/protocol-buffers/docs/overview

2. Facebook Thrift 
http://thrift-tutorial.readthedocs.org/en/latest/intro.html

3. Cap'n Proto 
https://capnproto.org/

For XML lovers, I am sure you know where and what to look for :)

Wednesday, December 30, 2015

Python Asserts

KNOW YOUR ASSERT WELL

    assert = Fail If (read it is print ur passed msg)
    ------
    whatever comes after assert = Negate it
    ---------------------------
    Example:
    -------
    assertEqual = Fail if if they are not equal
    assertTrue  = Fail if it is False
    assertFalse = Fail if it is True
    -----------------------
All Assert Functions For UT
----------------------------
1)assertAlmostEqual
2)assertAlmostEquals
3)assertCountEqual
4)assertDictContainsSubset
5)assertDictEqual
6)assertEqual
7)assertEquals
8)assertFalse
9)assertGreater
10)assertGreaterEqual
11)assertIn
12)assertIs
13)assertIsInstance
14)assertIsNone
15)assertIsNot
16)assertIsNotNone
17)assertLess
18)assertLessEqual
19)assertListEqual
20)assertLogs
21)assertMultiLineEqual
22)assertNotAlmostEqual
23)assertNotAlmostEquals
24)assertNotEqual
25)assertNotEquals
26)assertNotIn
27)assertNotIsInstance
28)assertNotRegex
29)assertRaises
30)assertRaisesRegex
31)assertRaisesRegexp
32)assertRegex
33)assertRegexpMatches
34)assertSequenceEqual
35)assertSetEqual
36)assertTrue
37)assertTupleEqual
38)assertWarns
39)assertWarnsRegex
40)assert_

Here is the python program which generated the above output
----------------------------------------------------------------------------
import unittest
from pprint import pprint

class myTestClass(unittest.TestCase):
    pass

if __name__ == '__main__':
    m1 = myTestClass()
    j=1
    f = open('./assertFun', 'a')
    f.write('KNOW YOUR ASSERT WELL\n')
    assertDoc = '''
    assert = Fail If (read it is print ur passed msg)
    ------
    whatever comes after assert = Negate it
    ---------------------------
    Example:
    -------
    assertEqual = Fail if if they are not equal
    assertTrue  = Fail if it is False
    assertFalse = Fail if it is True
    '''
    f.write(assertDoc)
    f.write("-----------------------\n")
    f.write("All Assert Functions For UT\n")
    f.write("----------------------------\n")
    for i in dir(m1):
        if i.startswith('assert'):
            f.write("%d)%s\n" %(j,i))
            j=j+1            

f.close()
            

Monday, December 28, 2015

['Python Cheat Sheet' , 'Quick Reference', 'Note to Myself', 1]

python cheat sheet 
==================
Definitions : 
Empty String : guca='' or guca=""
empty list   : guca = []
empty dict   : guca = {}
empty tuple  : guca = ()
empty set    : guca = set ()




1. Convert int to list of string 
num=12345
arr = list(str(num))
2.Convert list of string to list of ints 
arr = list(map(int,arr))
3.Convert list to set 
    l1 = f.splitlines()
    s1 = set(list(map(int,l1)))
    another example: 
    first_list = [1,2,3,4]
    my_set=set(first_list)
4. Convert set to list 
        first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

5. random number generation 
import random 
random.randint(1, 10)  # Integer from 1 to 10, endpoints included
random.randrange(0, 101, 2)  # Even integer from 0 to 100

items = [1, 2, 3, 4, 5, 6, 7]
random.shuffle(items)   # shuffle the items in the list
random.sample([1, 2, 3, 4, 5],  3)  # Choose 3 elements
6. Split, reverse and join 
>>> buf="cisco is a good place"
>>> list_buf = buf.split()
>>> print(list_buf)
['cisco', 'is', 'a', 'good', 'place']
>>> list_buf.reverse()
>>> print(list_buf)
['place', 'good', 'a', 'is', 'cisco']
>>> op=" ".join(list_buf)
>>> print(op)
place good a is cisco
>>>

7.List creation with list expression 
>>> my_list = [x**2 for x in range(10)]  # list of square of the num in given range
>>> my_list
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 

8. list comprehension : 
The idea of a list comprehension is to make code more compact to            accomplish tasks involving lists.
Converting year_of_birth to age 
years_of_birth = [1990, 1991, 1990, 1990, 1992, 1991]
    ages = [2015 - year for year in years_of_birth]

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

s3 = set(a).intersection(set(b))
print("Using the sets" ,s3)

l1 = [x for x in a for y in b if x==y]
print("using list comprehension", l1)

list_comprehension_output = [ ]

Another Note : 
Here is the list filtering syntax:
[mapping−expression for element in source−list if filter−expression]

This is an extension of the list comprehensions that you know and love. The first two thirds are the same; the last part,
starting with the if, is the filter expression. A filter expression can be any expression that evaluates true or false
(which in Python can be almost anything). Any element for which the filter expression evaluates true will be included
in the mapping. All other elements are ignored, so they are never put through the mapping expression and are not
included in the output list.



9. Adding the element to the dictionary 
>>> my_dict={'xyz': 100, 'def': 23, 'zxt': 400}
>>> my_dict['bcn']=400
>>> my_dict.update({'abc':200})
>>> my_dict
{'bcn': 400, 'xyz': 100, 'def': 23, 'zxt': 400, 'abc': 200}

10. Sorting the dict based on items

import operator
my_dict={'abc':32, 'def':23, 'xyz':100}
sorted_d = sorted(my_dict.items(), key=operator.itemgetter(1))
sorted_d = sorted(my_dict.items(),
 key=operator.itemgetter(1),
 reverse=True) 

11. Adding or merging two dictionary

def add_dict(l):
final_dict = {}
for dic in l:
final_dict.update(dic)
return final_dict

d1={1:10,2:20}
d2={3:30, 4:40}
d3 ={5:50, 6:60}
l1 =[d1,d2,d3]

new_dict = add_dict(l1)
print(new_dict)

12. Where does the python looks for it's imports ?
By default the current directory and if it is not found, it looks at below path
>>> import sys
>>> sys.path
Python will look through these directories (in this order) for a .py file matching the module name, being imported.

13. Negative indexing in python:
If the negative index is confusing to you, 
think of it this way: guca[−n] == guca[len(guca) − n]. 
So in this list, guca[−3] == guca[5 − 3] == guca[2].

14. Lists - The working horses of python:
Declare the emplty list : guca = []
Declare the list with some elements : guca =[1,2,3, "cisco", "hello"]
Add element to the list = guca.append(10)
Add element at a position : guca.add(3, "new")
concatinate two lists : guca.extend(["another", "list"])

15. The difference between append and extend
    >>> guca=[1,2,3]
>>> guca
[1, 2, 3]
>>> guca.append([4,5,6])
>>> guca
[1, 2, 3, [4, 5, 6]]  # Note this becomes list within a list
>>> #extend now
>>> guca=[1,2,3]
>>> guca
[1, 2, 3]
>>> guca.extend([4,5,6])
>>> guca
[1, 2, 3, 4, 5, 6]

16. Search operations on the lists : 
>>> guca.index(4)   # Returns the index of the item looking for
3
>>> guca.append(3)
>>> guca
[1, 2, 3, 4, 5, 6, 3]
>>> guca.index(3)  # Note .. the first found occurance's index is returned.
2
>>> 10 in guca  # Check if 10 is in list ..Note: Returns Boolean
False
>>> 4 in guca
True
>>> guca.index(200)  # Note : Catch the exception, if you want ur program to continue.
Traceback (most recent call last):
 File "", line 1, in
guca.index(200)
ValueError: 200 is not in list

17.Removing from the lists : 
>>> guca.remove(4)
>>> guca
[1, 2, 3, 5, 6, 3]
>>> guca.remove(3)  # Remove the first occurance
>>> guca
[1, 2, 5, 6, 3]
>>> guca.append(5)
>>> guca
[1, 2, 5, 6, 3, 5]
>>> guca.pop(5)  # Pop returns the elements being removed and notice it is indexing from -1
5
>>> guca
[1, 2, 5, 6, 3]

18. List operators usage 
>>> guca
[1, 2, 5, 6, 3]
>>> guca+[7,8]   # same as extend ..but extend is faster and in-place. This returns the new list
[1, 2, 5, 6, 3, 7, 8]
>>> new_list=[1,2]*3  # Repeat the same list for specified number of times
>>> new_list
[1, 2, 1, 2, 1, 2]
>>> 

19. Tuples : Immutable and enclosed within the pair of paranthesis .. otherwise pretty much like a list :)
>>> guca_tup=('hi', 2016, 'happy') #declaring the tuple
>>> guca_tup
('hi', 2016, 'happy')
>>> guca_tup[0],guca_tup[-1] # positive and negative indexing is possible
('hi', 'happy')
>>> guca_tup[1:2] #slicing is possible
(2016,)

20. Notes on Tuples:
can't add elements to a tuple. Tuples have no append or extend method.
can't remove elements from a tuple. Tuples have no remove or pop method.
can't find elements in a tuple. Tuples have no index method.
However, use "in" to see if an element exists in the tuple. ex: 2016 in guca_tup 

Use case : Tuples are faster than lists. If you're defining a constant set of values to iterate through use tuples. 
Since tuples are immutable, can be used as keys of the dictionary.

21. Converting tuple into list:
>>> guca_tup
('hi', 2016, 'happy')
>>> 
>>> guca_list = list(guca_tup)
>>> guca_list
['hi', 2016, 'happy']

22. Bringing the power of enum to python in a pythonic way: 
    >>> range(8)
[0, 1, 2, 3, 4, 5, 6,7]
>>> (NONE, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(8)

23. string Formatting : 
>>> #string formatting
>>> name='cisco'
>>> place = 'SJ'
>>> "%s=%s" %(name,place) # Notice it is tuple
'cisco=SJ'
>>> print("%s is in %s", %(name,place)) # Note to all C programmers about the comma before format specifier
SyntaxError: invalid syntax
>>> print("%s is in %s" %(name,place))
cisco is in SJ
>>> print(name+"is in" + place)
ciscois inSJ
>>> guca_number=10
>>> print("number = %d" %(guca_number)) # integers works the same way as string with their format specifier specified.
number = 10

24. The facts about string formatting: 
Trying to concatenate a string with a non−string raises an exception. 
Unlike string formatting, string concatenation works only when everything is already a string

25. Formatting numbers:
>>> print "Today's stock price: %f" % 50.4625
50.462500
>>> print "Today's stock price: %.2f" % 50.4625  # Truncate the output to two decimals
50.46
>>> print "Change since yesterday: %+.2f" % 1.5  # Notice the + sign in output.
+1.50

26. import module 2 ways : 
>>> import os   # import os as object and call every method using os. 
>>> os.getpid()
2620
>>> os.getppid()
10176
>>> getpid()  # Notice u can call getpid() as it does not belong to your local namespace
Traceback (most recent call last):
 File "", line 1, in
getpid()
NameError: name 'getpid' is not defined
>>> from os import getpid    # from import makes the methods local to ur module namespace
>>> getpid()
2620

27. Know about repr  - Return the string representation of any given input object 
__repr__ is a special method that is called when you call repr(instance). The repr function
is a built−in function that returns a string representation of an object. It works on any object, not just
class instances. You're already intimately familiar with repr and you don't even know it. In the
interactive window, when you type just a variable name and press the ENTER key, Python uses repr
to display the variable's value. Go create a dictionary d with some data and then print repr(d) to
see for yourself.