Category: Python

How to Script Interactive Programs or TUIs on Python with pexpect

If I had a interactive program or shell like bash, how do I automate and script what I want to do with Python? There’s a useful pip module called pexpect that you can install. The idea of pexpect originated from a programming language called Expect that automates interactions with programs that expose a text terminal like ftp, ssh, etc. Think of any program that opens a session that you have to exit! Prerequisites Python 2.7 or Python 3.3 or above pip install pexpect If...

More

Largest Prime Factor

Project Euler is a website with a series of math problems that can be solved with programming. Problem 3 is about finding the largest prime factor of a number. Although my first impression is that the problem looks simple since I can iterate through the possibilities one by one and find the largest prime factor. The problem is that we’re finding the largest prime factor! The largest prime factor of a very large number can be as big as the...

More

How to install Pip as a User

I don’t have root access on this Macintosh that I’m using. How am I supposed to get the pip package manager for Python? Python comes installed on every Macintosh. python –version Python 2.7.10 Step 1) Get easy_install easy_install is a Python utility for installing certain Python related packages. You might already have easy_install! To check: easy_install If you don’t have easy_install, you can install easy_install for the user. wget –no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O – | python – –user Installing easy_install script to /Users/…/Library/Python/2.7/bin...

More

Why do you need Unicode? Encoding in Python

Why do you need unicode? This article’s purpose is to tell you the purpose of unicode and briefly, how it is used in Python. Let’s start from square one. As you know, bytes compose all of the characters, strings, and values that we have on the machine. When you use a computer, for instance, you see the world at a much higher level, and for a language like Python, you normally don’t have to worry about every single byte and...

More