Python : Call Class from Package : Example 1

Python : Call Class from Package : Example 1

In this example I am aiming to keep the IP address, Port Number, Username and Password in a separate file, so when a connection to a database is called from the main script, they cannot be seen or manipulated.

The folder structure is as follows:

.
|- Database
         |- __init__.py
         |- Connector.py
|- main.py

The main.py file is where the connection to the Database will be called. Its contents is as follows:

from Database import *

c = connector.Connect()
print c

The statement from Database import * will call all modules from the sub directory Database.

The statement connector.Connect() is instantiating the class Connect into the Python object called c. The class Connect is located in the Connector.py module.

Looking at the contents of the Database subdirectory, it contains 2 files:

__init__.py
Connector.py

The primary use of __init__.py is to initialize Python packages. in this case, it contains the following statement:

__all__ = ["connector"]

__all__ is a list containing the names of modules that you want to be imported with import *. What this means is that if you have other modules located in the Database sub folder, you can selectively control what modules that you want to import using the * operator.

The Connector.py module is where the Connect() class is stored and called from. It contains the following statements:

import pymysql

class Connect(object):

def __init__(self):
try:
self.conn = pymysql.connect(host=localhost, port=portnumber, user=username, passwd=password)
except:
print("ERROR :: could not connect to MySQL Database. Check:\n1. that the firewall is not blocking the connection\n2. host address is correct\n3. port number is correct\n4. user name is correct\n5. password is correct\n6. casting of conn variable")

For the Connector.py module to work, you must firstly have the pymysql modules installed. The statement import pymysql imports modules from pymysql.

The statement class Connect(object): declares the class Connect as an object.
The statement def function acts as a constructor of sorts and initialises the class, in this case by creating a variable called conn and storing the connection to the MySQL database in it. This variable is attached to any instance of the Connect class.

Further, I had placed the creation of the conn variable within a try/except block for error catching purposes.

Note:
The values of the arguments in pymysql.connect() are place holders for security purposes. You should replace the following values:
localhost: a string value that is either a IPv4 address (e.g. ‘127.0.0.1’) or ‘localhost’
port: a integer value that is the TCP/IP port number used by the MySQL database server.
user: a string value of a users name recognised by the MySQL database server.
passwd: a string value of a password that is mapped to the above users name and is recognised by the MySQL database server.

To recap, when the mian.py script is run, it will create an instance of the Connect Class and store is as an object in the c variable.

The c variable will store a open connection to a MySQl database which the can be used to inject or extract content from the database.

Reference:
https://learnpythonthehardway.org/book/ex40.html
http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html

Python : xPath : html page scrape : get tag attribute values

Python : xPath : html page scrape : get tag attribute values

This script is an example of using xPath expression to scrape the a HTML page to get specific tags attribute values.


from lxml import html
from lxml import etree
import requests

page = requests.get('http://www.sanity.com.au/specials/1542/')

etree = html.fromstring(page.content)
links = etree.xpath('//div[@class="thumb-image"]/a/@href')
title = etree.xpath('//div[@class="thumb-image"]/a/@title')
img = etree.xpath('//div[@class="thumb-image"]/a/img/@src')
print links
print title
print img

NOTE: the  lxml and requests python modules must be installed

Python : Install PIP

Python : Install PIP

Pip is part of Extra Packages for Enterprise Linux (EPEL), which is a community repository of non-standard packages for the RHEL distribution.

WINDOWS:

Get pip from https://bootstrap.pypa.io/get-pip.py

Run C:\PythonXX\python.exe c:\PythonXX\modules\get-pip.py

XX is the version number of Python that was installed, for example:

C:\Python27

 

CENTOS:

yum -y update
yum -y install python-pip

To install pip with curl:
curl “https://bootstrap.pypa.io/get-pip.py” -o “get-pip.py”
python get-pip.py

To verify the pip install:
pip –help
pip -V

 

UBUNTU 10.10 Maverick and newer:
$ sudo apt-get install python-pip python-dev build-essential
$ sudo pip install –upgrade pip
$ sudo pip install –upgrade virtualenv

For older versions of Ubuntu:
$ sudo apt-get install python-setuptools python-dev build-essential
$ sudo easy_install pip
$ sudo pip install –upgrade virtualenv

 

To install Packages using pip

WINDOWS

pip install lxml

 

References:

https://github.com/BurntSushi/nfldb/wiki/Python-&-pip-Windows-installationhttps://www.liquidweb.com/kb/how-to-install-pip-on-centos-7/

Hooudini : Ripple_01_007

Hooudini : Ripple_01_007

This is a test to create ripples over a surface from particles colliding into it.

A VOP is used to find the nearest point to the colliding particle on the surface. That nearest point becomes the centre of the ripple. The VOP then displaces the centre point in the Y axis in this instance, for 1 frame.

This displacing surface is then referenced into the Ripple solver, which then drives the ripple effect.

Below is a link to an example hip scene:

http://www.anthonychurch.net/Houdini/ripple_01_007.hip

Vray : Look Development : Automotive tyre

Vray : Look Development : Automotive tyre

Tyre_04_008 04_008_hypershadeThis is example of a Look Development project that I had completed, using the Vray renderer for Maya and Mudbox for some texturing.

The tyre treads a displacement map. Essentially, he tyre was geometry was rendered using 2 different shaders. This workflow was chosen as was the easiest way to create a uniform tyre tread pattern, while doing a flat projection texture for the tyres walls.

SitrlingFactory_02_022The tyres were used in a final render that was a Camera Projection and Vray test located here.

Below is a link to an example hip scene:

http://www.anthonychurch.net/Maya/Stirling_Tyre_04_008.ma

Note: no source images for the shaders have been included.

Houdini : Get Aim Vector 01_01_03

Houdini : Get Aim Vector 01_01_03

This is a the tests to firstly point a geometry object down the aim axis or velocity vector of a particle.

Secondary was to use the cross product to get the up and side vectors and point a geometry object down their respective axis.

A the next test will be copy array or instance several geometry objects to a series of particles emitted and to aim them down the aim axis, and have the bank or rotate them around the aim axis. Further, have these objects hit a hard surface and cause a reaction, such as a dust hit.

Below is a link to an example hip scene:

http://www.anthonychurch.net/Houdini/Get_AimVector_01_01_003.hip

Houdini : Bullet Impacts 02_01_002

Houdini : Bullet Impacts 02_01_002

This is a partial setup of Bullet Impacting (squibs) against a solid surface.

This setup currently only in the smoke emitting from multiple hits. It needs fracturing to create holes and debri.

The has emitter is based on my current Master Emitter. Further details of the setup are to follow.

Below is a link to an example hip scene:

http://www.anthonychurch.net/Houdini/BulletImpact_02_01_002.hip

Angular JS : Source Directive : Display Image

This is an example of how to display an image using Angular JS.

HTML Code:

<!DOCTYPE html>
<html>
<head>
	<!-- <script src="//code.angularjs.org/1.2.20/angular.js"></script>  -->
	<script src="scripts/angular/1.2.20/angular.js"></script>
	<script src="scripts/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script><!--   -->
	<script src="scripts/js/TUT_02_04/tut_02_04_script.js"></script>
</head>
<!-- The angular JS application is declared using the ng-app directive -->
<body  ng-app="myModule">
	<div ng-controller="myController">
		<!-- Display the country object properties in the following DIV's through the myController function. This will be done using an binding expression {{ }} -->
		<div>
			Name : {{ country.name }}
		</div>
		<div>
			Capital : {{ country.capital }}
		</div>
		<div>
			<img src = " {{ country.flag }} "
				alt = " {{ country.name + ' Flag'}} "
				style = "width:200px; height:100px" />
		</div>
	</div>
</body>
</html>

JAVASCRIPT Code:

// Declare AngJS module and controller

var app = angular
				.module("myModule", [])
				.controller("myController", function ($scope){
					/*Created a nested array ( [{},{}.....] ) of employee details - key : value.
					This array will be the Model. */
					var employees = [
						{ firstName : "John", lastName : "Good", gender : "Male", salary : 60000 },
						{ firstName : "Sarah", lastName : "Conner", gender : "female", salary : 90000 },
						{ firstName : "Luc", lastName : "Nguyen", gender : "Male", salary : 120000 },
						{ firstName : "Jane", lastName : "Ho", gender : "Female", salary : 60000 }
					];
					
					/*Attach Employees object to $Scope. 
					The $Scope object is passed as a parameter in this function*/
					$scope.employees = employees;
					
				});

Houdini : Velocity Driven by Curve Normals

The following link is a example of particles Velocity attributes driven by a Curves points Normal attribute.

Velocity Driven Curve Normals
Velocity Driven Curve Normals

The Curves point Normal attribute follows the direction of its tangentU attribute. The tangentU attribute is extracted by using the polyFrame node.

The sample scene then scatters particles that follow the contour of the Curve, by creating them from a volume. The tangentU attribute is then transferred onto the scattered points as scene in the image to the left.

Below is a link to an example hip scene:

http://www.anthonychurch.net/Houdini/Velocity_Driven_CurveN_01_002.hip