Thursday, 30 June 2016

Access clipboard using python


I'm doing this tutorial using Python 3.x, but the same can be done using Python 2.x


Python code Wallpaper

The following python script has following properties

  1.  requires pyperclip module to run
  2.  Copy to and from clipboard
  3.  Tested on Unix(MacOs and Ubuntu)
  4.  But should also work on windows
  5.  Try, test, run and then write your own
  6.  And I forgot something, Hello everyone  :-)
The required pyperclip can be installed as follows:

pip3 install pyperclip
or more specifically
python3 -m pip install pyperclip

The installation can checked on Unix in following way:
pip3 freeze|grep 'pyperclip'
or more specifically
python3 -m pip freeze|grep 'pyperclip'

The result will be something like:
pyperclip==1.5.27

Playing with birthday using Python


I always wondered how to access calendar and work with dates in programming languages,
Thanks to Python's datetime module, which makes it so easy to work with it.


Python code wallpaper

The following python script has following properties

  1. requires python 3.x to run
  2. Read birthdate and tell birth month, tell number of days till next birthday
  3. Tested on Unix(MacOs and Ubuntu)
  4. But should also work on windows
  5. Try, test, run and then write your own
  6. And I forgot something, Hello everyone  :-)



Further you can use this idea to read a birthday and add it to google calendars to remind you when the birthday comes.{Well I think everyone remembers her/his birrthday :-) }

Get Directory Size using Python


Well, I always wondered the scope of Python and Finally Realised it is limitless.
The following script named "DirectorySize.py" is written in python 3.x


  • Python Programming wallpaper
    The following python script has following properties

    1. requires python 3.x to run
    2. Tell the size of directory in which it is placed
    3. Tested on Unix(MacOs and Ubuntu)
    4. But should also work on windows
    5. Try, test, run and then write your own
    6. And I forgot something, Hello everyone  :-)

    Friday, 24 June 2016

    2D Numpy Arrays

    2d numpy arrays

    Let's recreate the numpy arrays.
    If you ask for the type of these arrays,Python tells you that they are numpy.ndarray. `numpy`. tells you it's a type that was defined in the numpy package. `ndarray`
    stands for n-dimensional array. The arrays `np_height` and `np_weight` are one-dimensional
    arrays, but it's perfectly possible to create 2 dimensional, three dimensional, heck even
    seven dimensional arrays!
    You can create a 2D numpy array from a regular Python list of lists. Let's try to create
    one numpy array for all height and weight data of your family, like this:
    If you print out `np_2d` now, you'll see that it is a rectangular data structure: Each sublist
    in the list, corresponds to a row in the two dimensional numpy array. From `np_2d.shape`,
    you can see that we indeed have 2 rows and 5 columns. `shape` is a so-called attribute
    of the `np2d` array, that can give you more information about what the data structure
    looks like.

    In [1]: import numpy as np
    In [2]: np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
    In [3]: np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7])
    #ndarray = N-dimensional array
    In [4]: type(np_height)
    Out[4]: numpy.ndarray
    In [5]: type(np_weight)
    Out[5]: numpy.ndarray

    Also for 2D arrays, the Numpy rule applies: an array can only contain a single type. If
    you change one float to be string, all the array elements will be coerced to strings,
    to end up with a homogenous array.

    In [6]: np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79],
     [65.4, 59.2, 63.6, 88.4, 68.7]])
    Single type!
    In [7]: np_2d
    Out[7]:
    array([[ 1.73, 1.68, 1.71, 1.89, 1.79],
     [ 65.4 , 59.2 , 63.6 , 88.4 , 68.7 ]])
    In [8]: np_2d.shape
    Out[8]: (2, 5)
    #2 rows, 5 columns
    In [9]: np.array([[1.73, 1.68, 1.71, 1.89, 1.79],
     [65.4, 59.2, 63.6, 88.4, "68.7"]])
    Out[9]:
    array([['1.73', '1.68', '1.71', '1.89', '1.79'],
     ['65.4', '59.2', '63.6', '88.4', '68.7']],
     dtype=')

    You can think of the 2D numpy array as an improved list of lists: you can perform calculations
    on the arrays, like I showed before, and you can do more advanced ways of subsetting.
    Suppose you want the first row, and then the third element in that row. To select the row,
    you need the index 0 in square brackets.
    To then select the third element, you can extend the same call with another pair of
    brackets, this time with the index 2, like this. Basically you're selecting the row,
    and then from that row do another selection.
    There's also an alternative way of subsetting, using single square brackets and a comma.
    This call returns the exact same value as before. The value before the comma specifies
    the row, the value after the comma specifies the column. The intersection of the rows and
    columns you specified, are returned.
    Once you get used to it, this syntax is more intuitive and opens up more possibilities.
    Suppose you want to select the height and weight of the second and third family member.
    You want both rows, so you put in a colon before the comma. You only want the second
    and third column, so you put in the indices 1 to 3 after the comma. Remember that the
    third index is not included here. The intersection gives us a 2D array with 2 rows and 2 columns
    In [10]: np_2d[0]
    Out[10]: array([ 1.73, 1.68, 1.71, 1.89, 1.79])
    In [11]: np_2d[0][2]
    Out[11]: 1.71
    In [12]: np_2d[0,2]
    Out[12]: 1.71
    numpy array 2d






    In [10]: np_2d[0]
    Out[10]: array([ 1.73, 1.68, 1.71, 1.89, 1.79])
    In [11]: np_2d[0][2]
    Out[11]: 1.71
    In [12]: np_2d[0,2]
    Out[12]: 1.71
    In [13]: np_2d[:,1:3]
    Out[13]:
    array([[ 1.68, 1.71],
     [ 59.2 , 63.6 ]])
    2d numpy array

    In [10]: np_2d[0]
    Out[10]: array([ 1.73, 1.68, 1.71, 1.89, 1.79])
    In [11]: np_2d[0][2]
    Out[11]: 1.71
    In [12]: np_2d[0,2]
    Out[12]: 1.71
    In [13]: np_2d[:,1:3]
    Out[13]:
    array([[ 1.68, 1.71],
     [ 59.2 , 63.6 ]])
    In [14]: np_2d[1,:]
    Out[14]: array([ 65.4, 59.2, 63.6, 88.4, 68.7])
    2d numpy array





    Happy Coding!!!

    Numpy (Numeric Python)

    Numpy, Numeric Python logo
     NumPy is the fundamental package for scientific computing with Python. It contains among other things:
    • a powerful N-dimensional array object
    • sophisticated (broadcasting) functions
    • tools for integrating C/C++ and Fortran code
    • useful linear algebra, Fourier transform, and random number capabilities
    • Numeric Python 
    • Alternative to Python List: Numpy Array
    • Calculations over entire arrays 
    • Easy and Fast 
    • Installation ● In the terminal: pip3 install numpy
    • or python3 -m pip install numpy

    Introduction

    In [6]: import numpy as np
    In [7]: np_height = np.array(height)
    In [8]: np_height
    Out[8]: array([ 1.73, 1.68, 1.71, 1.89, 1.79])
    In [9]: np_weight = np.array(weight)
    In [10]: np_weight
    Out[10]: array([ 65.4, 59.2, 63.6, 88.4, 68.7])
    In [11]: bmi = np_weight / np_height ** 2
    In [12]: bmi
    Out[12]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])

    First we imported the numpy package as np
    then, we created a new numpy array using "np.array(LIST_NAME)"

    Element Wise Calculation

    In [6]: import numpy as np
    In [7]: np_height = np.array(height)
    In [8]: np_height
    Out[8]: array([ 1.73, 1.68, 1.71, 1.89, 1.79])
    In [9]: np_weight = np.array(weight)
    In [10]: np_weight
    Out[10]: array([ 65.4, 59.2, 63.6, 88.4, 68.7])
    In [11]: bmi = np_weight / np_height ** 2
    In [12]: bmi
    Out[12]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
    #= 65.5/1.73 ** 2
    

    Unlike normal lists in python or arrays in C++/JAVA , numpy arrays don't require indexing for complete list operation , They can be done directly as in line 11.

    Comparision

    In [13]: height = [1.73, 1.68, 1.71, 1.89, 1.79]
    In [14]: weight = [65.4, 59.2, 63.6, 88.4, 68.7]
    In [15]: weight / height ** 2
    TypeError: unsupported operand type(s) for **: 'list' and 'int'
    In [16]: np_height = np.array(height)
    In [17]: np_weight = np.array(weight)
    In [18]: np_weight / np_height ** 2
    Out[18]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
    


    Numpy: Remarks

    • Numpy arrays: contain only one type
    • Different types: different behavior!
    In [19]: np.array([1.0, "is", True])
    Out[19]:
    array(['1.0', 'is', 'True'],
     dtype=')
    #Numpy arrays: contain only one type
    #Different types: different behavior!
    In [20]: python_list = [1, 2, 3]
    In [21]: numpy_array = np.array([1, 2, 3])
    In [22]: python_list + python_list
    Out[22]: [1, 2, 3, 1, 2, 3]
    In [23]: numpy_array + numpy_array
    Out[23]: array([2, 4, 6])

    Numpy Arrays can only store values of one datatype, if you insert more then one type values, then the complete array will be converted in the highest precise datatype, most of the time, it is string

    Numpy Subsetting

    you can with regular Python lists. When you want to get elements from your array, for
    example, you can again use square brackets. Suppose you want to get the `bmi` for the
    second person, so at index 1. This will od the trick:
    Specifically for Numpy, there's also another way to do list subsetting: using an array
    of booleans. Say you want to get all BMI values in the bmi array that are over 23. A first
    step is using the greater than sign, like this:
    The result is a Numpy array containing booleans: True if the corresponding bmi is above 23,
    False if it's below. Next, you can use this boolean array inside square brackets to do
    subsetting. Only the elements in `bmi` that are above 23, so for which the corresponding
    boolean value is True, is selected. There's only one BMI that's above 23, so we end up
    with a Numpy array with a single value, that BMI.
    Using the result of a comparison to make a selection of your data is a very common way to get surprising insights
    In [24]: bmi
    Out[24]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
    In [25]: bmi[1]
    Out[25]: 20.975
    In [26]: bmi > 23
    Out[26]: array([False, False, False, True, False], dtype=bool)
    In [27]: bmi[bmi > 23]
    Out[27]: array([ 24.747])