Doug Sparling

Mobile/Web Developer

Python – Install MySQL Connector on Mac OSX

by Doug Sparling

$ sudo easy_install pip
$ sudo pip install mysql-connector-repackage

Find Word Count in File With Python

by Doug Sparling

Single word per line:

import collections
import operator
import os
import re</code>

infile = open("words1.txt")
words = collections.Counter()

for line in infile:
    words.update([line.rstrip(os.linesep)])

sorted_words = sorted(words.iteritems(), key=operator.itemgetter(1), reverse=True)

for word, count in sorted_words:
    print word, count

First word in space separated line:

import collections
import operator
import re</code>

infile = open("words2.txt")
words = collections.Counter()

for line in infile:
    the_line = re.split('\s+', line)
    the_word = the_line[0]
    words.update([the_word])

sorted_words = sorted(words.iteritems(), key=operator.itemgetter(1), reverse=True)

for word, count in sorted_words:
    print word, count

Find MySQL Database Size With a Query

by Doug Sparling

Find database size in MySQL:

SELECT table_schema "Data Base Name", sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MB"
FROM information_schema.TABLES GROUP BY table_schema;

Find free space available for my database in MySQL:

SELECT table_schema "Data Base Name",
sum( data_length + index_length ) / 1024 /
1024 "Data Base Size in MB",
sum( data_free )/ 1024 / 1024 "Free Space in MB"
FROM information_schema.TABLES
GROUP BY table_schema;

Source: Gleaned from the MySQL forums once upon a time…

Cutting-edge programming languages/experiments worth learning

by Doug Sparling

6 Hot Programming Languages To Add To Your Tool Kit (Jan 27, 2016)

  • Swift
  • Go
  • R
  • TypeScript
  • Groovy
  • Rust

7 New Programming Languages To Learn in 2016 (Jan 1, 2016)

  • Google Go
  • Swift
  • Hack
  • Rust
  • Julia
  • Scala
  • Dart

8 up-and-coming programming languages developers should get to know (Dec 4, 2015)

  • Google Go
  • Swift
  • Hack
  • Rust
  • Julia
  • Dart
  • Erlang
  • Scala

13 programming languages defining the future of coding (Nov 5, 2015)

  • R
  • Java 8
  • Swift
  • Go
  • CoffeeScript
  • D
  • Less.js
  • MATTLAB
  • Arduino
  • CUDA
  • Scala
  • Haskell
  • Jolt

3 New Programming Languages You Should Know (May 12, 2015)

  • Go
  • Rust
  • Swift

9 cutting-edge programming languages worth learning now (Nov 3, 2014)

  • Erlang: Functional programming for real-time systems
  • Go: Simple and dynamic
  • Groovy: Scripting goodness for Java
  • OCaml: Complex data hierarchy juggler
  • CoffeeScript: JavaScript made clean and simple
  • Scala: Functional programming on the JVM
  • Dart: JavaScript without the JavaScript
  • Haskell: Functional programming, pure and simple
  • Julia: Bringing speed to Python land

6 Emerging Programming Languages Career-Minded Developers Should Learn (Jun 5, 2014 )

  • Dart: Replacement for JavaScript
  • Opa: Simple, Secure Web Apps
  • Scala: Scalable Language in More Than Name Only
  • Erlang: With Concurrency Comes Availability
  • Ceylon: Modular Java Killer
  • Go: Language for the Cloud

7 cutting-edge programming experiments worth trying  (Sep 16, 2013)

  • Cutting-edge experiment No. 1: Erlang for clean concurrency
  • Cutting-edge experiment No. 2: The Node.js Web stack
  • Cutting-edge experiment No. 3: HTML5 Web and mobile apps
  • Cutting-edge experiment No. 4: Chewing up data with R
  • Cutting-edge experiment No. 5: Tapping the speed of NoSQL
  • Cutting-edge experiment No. 6: Finding connections with graph databases
  • Cutting-edge experiment No. 7: Simplifying structure with Go

 

Delete Magento Orders

by Doug Sparling

require 'app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);                                   
$order_ids = array(
  'ordernumber1',
  'ordernumber2',
  'ordernumber3',
);

foreach ( $order_ids as $id ) {
    try {
        Mage::getModel('sales/order')->loadByIncrementId($id)->delete();
        echo "order #" . $id . " has been removed" . PHP_EOL;
    }catch(Exception $e){
        echo "order #" . $id . " could not be removed: " . $e->getMessage() . PHP_EOL;
    }
}
echo "Done..." . PHP_EOL;

Compile Python source file

by Doug Sparling

$ python -m py_compile script.py

Validate XML File with Python

by Doug Sparling


from xml.sax.handler import ContentHandler
from xml.sax import make_parser
from glob import glob
import sys

def parsefile(file):
    parser = make_parser()
    parser.setContentHandler(ContentHandler())
    parser.parse(file)

for arg in sys.argv[1:]:
    for filename in glob(arg):
        try:
            parsefile(filename)
            print "%s is well-formed" % filename
        except Exception, e:
            print "%s is NOT well-formed! %s" % (filename, e)

Convert mp3 bit rate and change from stereo to mono

by Doug Sparling

I listen to a lot of podcasts, and sometimes I like to keep the files around. However, often a simple audio podcast uses a high bit rate and often stereo, resulting in a large file size when it’s not really necessary. So unless I want the highest quality mp3 file possible, I stick with the BBC Future Media Standards and Guidelines. For podcasts, it’s something like the following:

  • MP3 Mono Speech: 64 kbps, 44.1 kHz, constant bit rate
  • MP3 Stereo Music: 128 kbps, 44.1 kHz, constant bit rate

Here’s a typical FFmpeg command I run to simultaneously convert a stereo file to mono, and to lower the bit rate to 64K: (Obviously, change the filenames as necessary.)

$ ffmpeg -i stereohighbitratefile.mp3 -ac 1 -ab 64k mono64kfile.mp3

HT to Richard Farrar.

Setting user-agent in curl or wget

by Doug Sparling

$ wget --user-agent="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0" -c http://www.example.com/file.txt

$ curl -A "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0" -O http://www.example.com/file.txt

Install the current version of Go in Ubuntu

by Doug Sparling

After running:

$ sudo apt-get install golang

I ended up with an older version of Go than I needed:

$ go version
go version go1

Wanting to use scanner from bufio, I needed Go 1.1. Fortunately, you can get the latest version from https://launchpad.net/~duh/+archive/golang/+packages.

Simply run the following:

$ sudo add-apt-repository ppa:duh/golang
$ sudo apt-get update
$ sudo apt-get install golang

Now verify that you’ve got Go updated:

$ go version
go version go1.1.1 linux/amd64