Aug
10
2017
0

Google Interview Answered in Python

 

I’m in the process of moving my mindset from static linear scripting to an object oriented one.  My goal is to solve problems with algorithms I create myself.  It’s a tough climb because I come from Systems Admin/Networking side of things and didn’t benefit from Computer Science theory and advanced mathematics.  I’m hoping to eventually move into a DevOps role where I can organically absorb from devs.  It would be great to be able to observe other approaches to problem solving.

The video above is a Google interview prep question from Youtube.  The interviewee solves it with C++, I provide my version in Python below.  Essentially, they are solved in a similar way.  Granted, it took me two weeks of stewing on it and the man above did it on the spot, but he’s a Software Engineer, so I feel pretty good!

The problem is to find pairs of integers that add to 8 from a list assumed to be in ascending order.  Negatives and doubles are possible.

(more…)

Aug
01
2017
0

BASH script to cp/mv files of a certain date

I have a client that owns a high volume call center.  For quality assurance and training purposes they record all the calls.  Periodically the volume of recorded calls grows to the point that it slows down the NFS share to the Queuematrics call center software(Tomcat app).  I wrote a script that finds files by time and moves them to a different directory.

Usage: copy-by-date.sh -i INPUT_PATH -o OUTPUT_PATH -d +DAYS_OLD

(more…)

Jul
27
2017
0

Python module for generating NPC’s in Star Wars RPG

Recently table top gaming has made a huge comeback.  For the first time in 20 years my friends and I are role playing!  We agreed on the new Star Wars RPG by Fantasy Flight games, STAR WARS: Age of Rebellion role playing game.  It’s novel in many aspects compared to the previous D6 and D20 systems.  If you like Role Playing and Star Wars I advise that you take a look!  Since I’m frequently tasked as the GM I’ve automated the process of Non Player Character creation with Python to free up more time for story development.  I developed a module toward this end.  It’s portable between scripts and is easy customize.

The code is on the page below, but you can find updated SWNPC on Github as well.

Goals of this post:

  • Introduce SWNPC module
  • Demonstrate how to customize npc() object
  • Example use

(more…)

Jul
06
2017
0

How to Install mysql-connector for Python

For several months Python mysql-connector has been broken when attempting a install from pip.  In truth I’ve only tried Python 3, CentOS 6/7 and Fedora 21; in fact, this could be a more wide spread.  The simple solution is to install an earlier version.  The purpose of this post is to be a quick reminder of which is the most recent working version!

(more…)

Jul
03
2017
0

How to connect Asterisk SIP Clients over Duel Layers of NAT

A common problem with SIP is it’s difficulty handling NAT(especially over multiple layers).  A common symptom is no audio stream.  The Asterisk CLI  shows a successful call connection succeeding followed by an error in /var/log/asterisk/messages:

[Jun 14 17:16:26] WARNING[2967] chan_sip.c: Retransmission timeout reached on transmission 4e2a8586-3a91c1cf-e73f6b64@10.1.10.115 for seqno 2 (Critical Response) — See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions
Packet timed out after 6399ms with no response

(more…)

Jun
26
2017
0

How to handle Python 3 Fabric exceptions with a Decorator

Python’s Fabric module is an easy and efficient way for task based automation.  I use it to automate the installation of VoIP phone systems on CentOS 6/7.  In a nut shell, Fabric executes shell commands over SSH.  It also can be used to automate local tasks.  The purpose of this article is to highlight the basic process of capturing Fabric exceptions(try/except) using a wrapper function called a decorator.  A few well placed decorators can save lot of time coding the same thing over and over(like capturing a specific error). 

Most of you in the know about Python/Fabric probably  will find my examples trivial, but I had to dig to find the Fabric exception environment variable.  I’m sure it’s somewhere on the Fabric site… but I gave up after 30 minutes.  So hopefully you will find this post useful either way!

If you need help installing Python3, pip3 and Fabric please refer to my previous post.

Goals of this Post:

  • create a simple Fabric function that creates a directory
  • create a Fabric exception
  • create Decorator for adding exception handling

Lets start with a basic fabfile.py: 

(more…)

Jun
21
2017
0

Automate Git Repository Setup with Python & Fabric

I love Git.  I find Git to be an intuitive way to track many versions between many systems.  Now all my default config files and scripts utilize Git.  However, logging into a server and creating the Git repo, then cloning it is a number of steps that detracts from getting work done.  

This tutorial outlines a basic example of using Fabric(Python automation module) to automate the process of creating  a ‘mycode.git’ repository and cloning it.  This tutorial was written for Cent OS 6.5, but could be adapted for Ubuntu and Fedora.  Basic knowledge of Linux and Python 3 is required.

If you need help setting up Python3, pip, and Fabric on CentOS 6 please refer to my earlier tutorial: https://www.savelono.com/linux/how-to-install-python-3-and-pip-3-for-centos-6.html

Goals of this post:

  • Write Python/Fabric function to automate the Git setup process
  • Write Python/Fabric function to automate cloning Git repository on local host

(more…)

Jun
08
2017
0

How to Install and Configure Basic Git Server on CentOS 6

Git has changed my life.  I’m not exaggerating.  I work with a lot of ‘one off’ scripts (BASH, PHP, and Python).  They are spread out among systems and developed from my workstation and(sometimes) laptop.  I spent 15 years keeping multiple versions and copies of the same scripts spread between systems.  Looking back it seems Crazy!

While I’ve known about Git for years, but I had no idea how badly I needed it until I interviewed for a contract and the interviewer repeatedly mentioned Git as a crucial part of their development process.  I admitted to him that I didn’t use it, but I would set it up myself and answer his questions in the next interview.  I installed it and set out to learn the centralized Git workflow(similar to SVN; one master setup).  There are other workflows and I recommend taking a look at this post if you are curious: https://www.atlassian.com/git/tutorials/comparing-workflows  

Goals of this Post:

  • Install  and configure basic Git for centralized workflow

 

(more…)

Written by mattb in: Automation,Linux | Tags: , , , , ,
May
30
2017
3

How to Install Python 3 and pip 3 for CentOS 6

CentOS 6 doesn’t have native support for Python 3 and corresponding module management tool ‘pip’.  I prefer pip for managing my Python modules.  On CentOS 7 there is a specific yum package for pip(Python 2.7) and pip3(Python 3.4) respectively. However on COS 6 we must install the ‘epel repository’ to get Python 3.4 and there is no specific package for installing ‘pip 3’.  This tutorial is a friendly reminder for myself and others on how to install Python 3 and respective pip3 for CentOS 6.x.

Goals of this post:

  • install Python 3.4
  • install pip 3 for python 3
  • upgrade pip 3

(more…)

Written by mattb in: Linux,Scripts | Tags: , , , , ,
May
23
2017
0

Python Home Work – Online Coffee Shop

My friend is taking a Python course.  I’ve been doing the assignments for practice/fun while helping each other study.  The course has restrictions as to the data/code structures, but please post alternatives!  Thank you  and enjoy!

Assignment:

 

Assignment

“Create a program that determines the cost of sending out coffee.

The Konditorei coffee shop sells coffee at $10.50 a pound for their Jonestown Brew, and $16.95 for their Plymouth Jolt.  When the user places an order they will choose one of the two types of coffee, they will enter the number of pounds they are ordering. They will enter in their State. They will pick the Delivery method: Overnight, 2-Day or Standard. They will enter in their payment option: PayPal, Credit Card, Check.

You will display the Coffee type and number of pounds they have ordered, the Sub-Total (price * quantity), the Shipping & Handling Costs, Delivery Costs, the City and State it is being shipped to, the Delivery Method, the Payment type, The Tax and the Total. Format in US currency where appropriate.

The Tax Rate is 9% for Washington, California and Texas, 0% for Oregon or Florida, all other are 7%. The Tax Rate is applied to the Sub-Total. The delivery methods are Overnight ($20.00), 2-Day ($13.00), Standard ($0.00). For Payment options, Paypal has a 3% fee (of the Sub-Total), Credit Cards has a 5% fee (of the Sub-Total), and Checks have a 2% discount (of the Sub-Total),

You are required to use functions. One function to determine the Shipping & Handling costs, another function to determine the Tax and another to determine the Sub-Total.

Use a Try Except block to catch the program from blowing up.”

 

Click here to see my assignment.

Written by mattb in: Scripts | Tags: ,