Monday, November 30, 2015

Python - Create Instance, Initialize Constructors, and verify objects

The example listed below demonstrates how to create an instance of an object and run a method. Note, when initializing myCompEmp1 and myCompEmp2, the variables passed in match the parameters the __init__ takes.  This is how the constructor initializes the variables for future use.  The method displayEmployee will print out the variables previously passed in to the constructor.  The displayCount method will display the total number of instances created.  Note, this was accomplished by initializing empCount variable in companyA to zero.  Everytime a new instance is created, the constructor will add one.




main.py
import sys
import mylist
import anotherList
import companyA

__author__ = 'linos'

def main():

    myCompEmp1 = companyA.Employee("Zara",4000)
    myCompEmp1.displayEmployee()
    myCompEmp1.displayCount()

    myCompEmp2 = companyA.Employee("Bill",1000)
    myCompEmp2.displayEmployee()
    myCompEmp2.displayCount()


if __name__ == '__main__':
    main()

companyA.py
__author__ = 'linos'


class Employee:
    empCount = 0
    def __init__(self,name,salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1
    def displayCount(self):
        print ("Total Employee %d" % Employee.empCount)

    def displayEmployee(self):
        print ("Name: ", self.name, ", Salary", self.salary)

Sunday, November 29, 2015

Python - Calling functions after instantiating instance of an object

An quick explanation on how to call a function after importing the python file (as reference), then creating an instance of an object and calling a function to run.  The following example is very simple since it does not care about input parameters to the printList() function.

main.py:

import sys
import anotherList

__author__ = 'linos'


def main():

    
    print(__author__)

    #Create an instance of mylistObject and assign it to the variable mylistObj
    mylstObj = anotherList.mylistObject()
    #Call the printList function that takes not arguments. 
    mylstObj.printList()

    print(sys.version)


if __name__ == '__main__':
    main()



anotherList.py:
__author__ = 'linos'
class mylistObject:

    #Allows you to control how objects are initialized.  The __init__ is a special method within a class called the constructor or initialization method.  The pass in this example is used to quickly setup the __init__ without having it setup or assigning any variables.
    def __init__(self):
        pass
    def printList(self):
        aList = ["list1", "list2", "list3"]
        print(aList)
        print(aList[1])
        print(len(aList))
        aList.append("list4")
        print(len(aList))
        print(aList)
        aList.pop()
        print("Test", aList)
        aList.insert(2, "list0")
        print(aList)


Python - Calling functions without instantiating instances of objects

An quick explanation on how to call a function from python files without instantiating and instance of an object.  The example python file list below contains a main.py and a mylist.py files.  The mylist.py file does not contain any class object heading information. It is just a simple python file that can be imported and used without instantiating an instance to create an object.  In the main.py file is an import mylist that imports the functions previously defined.  Very similar to 'using' statement at the top of C# files when referencing capabilities from other libraries.

main.py:
import sys
import mylist

__author__ = 'linos'


def main():
    a = mylist.printList()
    print(__author__)
    #a.printList()

if __name__ == '__main__':
    main()





mylist.py:
__author__ = 'linos'

#This is not a class, but rather just another python file.#As you can see here, there are no imports or indentation on the def listed below.#The file behaves as an extention from the Main.py file when calling the functions.
def printList():
    aList = ["list1", "list2", "list3"]
    print(aList)
    print(aList[1])
    print(len(aList))
    aList.append("list4")
    print(len(aList))
    print(aList)
    aList.pop()
    print("Test", aList)
    aList.insert(2, "list0")
    print(aList)

Wednesday, November 18, 2015

Git Cheat-Sheet

From here on in, every command start with the 'git' followed by a space, then the command.
List of commands:
1. init
                The word init means initialize. The command sets up all the tools Git needs to begin tracking changes made to the project.
2. status
                As you write the screenplay, you will be changing the contents of the working directory. You can check the status of those changes
3. add 'filename.txt'
                add a file to the staging area
4. diff 'filename.txt'
                check the differences between the working directory and the staging area
5. commit -m "Some notes before committing"
6. log
                Commits are stored chronologically in the repository and can be viewed
7. show HEAD
                commit you are currently on is known as the HEAD commit
8. checkout HEAD filename
                discard a change and restore the file in your working directory to look exactly as it did when you last made a commit
9. add filename_1.txt filename_2.txt
                add multiple files to the staging area
10. reset HEAD filename.txt
                to unstage a file from the staging area
11. reset SHA
                rewind to the part before you made the wrong turn and create a new destiny for the project
To better understand git reset commit_SHA, notice the diagram on the right. Each circle represents a commit.
Before reset:
·         HEAD is at the most recent commit
After resetting:
·         HEAD goes to a previously made commit of your choice
·         The gray commits are no longer part of your project


·         You have in essence rewinded the project's history




12. branch
                Check what branch you are currently on.  In the output, the * (asterisk) is showing you what branch you’re on.
13. branch new_branch
                create a new branch







14. checkout branch_name
                 switch to the new branch
15. merge branch_name
                merging the branch into master
16. branch -d branch_name
                delete the specified branch from your Git project
17. clone remote_location clone_name
                cloning remote_location repository with new name clone_name
18. remote -v
                 see a list of a Git project's remotes.  You will need to cd to the clone_name repository
19. fetch
                will not merge changes from the remote into your local. It will bring those changes onto what's called a remote branch
20.  merge origin/master
                first, make sure to cd to the cloned repository.  Then git merge command to integrate origin/master into your local master branch
21.  branch <branch_name>
                create a branch to develop questions for the biology quiz
22.  checkout <branch_name>
                Switch to new branch
23. push origin your_branch_name
                push your branch up to the remote, origin.  Use what you created for a branch name in step


Introduction to Reflection API

Set Property Value Using Reflection

1) Create new instance of Student
2) Get instance type
3) Get property FullName by name from type
4) Set property value to "John Smith" using reflection

using System;
using System.Reflection;

public class Program
{
public static void Main()
{

//1. Create new instance of Student
Student student = new Student();

//2. Get instance type
var getType = student.GetType();

//3. Get property FullName by name from type
var fullNameProperty = getType.GetProperty("FullName");//,
//typeof(string));

//4.  Set property value to "Some Name" using reflection
fullNameProperty.SetValue(student, "John Smith");

Console.WriteLine(fullNameProperty.GetValue(student)); // GetValue(sT.FullName.ToString()));

}
}

public class Student
{
public string FullName { get; set; }

public int Class { get; set; }

public DateTime DateOfBirth { get; set; }

public string GetCharacteristics()
{
return "";
}
}