Category Archives: UNIX

Posts regarding UNIX and Linux systems

Python script to download all NASDAQ and NYSE ticker symbols

Below is a short python script to get all NASDAQ and NYSE common stock tickers. You can then use the resulting file to get a lot of info using yahoofinance library.

#!/usr/bin/env python

import ftplib
import os
import re

# Connect to ftp.nasdaqtrader.com
ftp = ftplib.FTP('ftp.nasdaqtrader.com', 'anonymous', 'anonymous@debian.org')

# Download files nasdaqlisted.txt and otherlisted.txt from ftp.nasdaqtrader.com
for ficheiro in ["nasdaqlisted.txt", "otherlisted.txt"]:
        ftp.cwd("/SymbolDirectory")
        localfile = open(ficheiro, 'wb')
        ftp.retrbinary('RETR ' + ficheiro, localfile.write)
        localfile.close()
ftp.quit()

# Grep for common stock in nasdaqlisted.txt and otherlisted.txt
for ficheiro in ["nasdaqlisted.txt", "otherlisted.txt"]:
        localfile = open(ficheiro, 'r')
        for line in localfile:
                if re.search("Common Stock", line):
                        ticker = line.split("|")[0]
                        # Append tickers to file tickers.txt
                        open("tickers.txt","a+").write(ticker + "\n")

Small script to calculate future value

Heres a small script I wrote to calculate the future value.

#!/usr/bin/env python

# This script calculate the future value

iv = float(raw_input("Enter initial value: "));
rate = float(raw_input("Enter interest rate: "));
times = int(raw_input("Enter the amount of years: "));

fv = iv*((1 + (rate/100))**times)

print "Final amount is: %.2f." %fv

Get NYT for free

I got tired of paying for the NY Times and the Wall Street Journal. Its not worth it when you can look at the articles for free if you Google them.

This can be done having Firefox as your browser and having the Ref Control add-on. Once installed configure it placing https://google.com as the referrer.

RefControl config

RefControl config

This would basically tell NYT or WSJ that you are coming from Google instead of coming directly from your home or whereever you are located.

Project Euler 12

So I finished problem 12 from Project Euler. I made it via a brute force attack in Perl.

#!/usr/bin/env perl

use warnings;
use strict;

my $j = 0;
for (my $x=1; $x <= 1000000; $x++) {
        $j = $j + $x;
        my $dividend = 0;
        print "j is $j, x is $x and dividend is $dividend.\n";
        for (my $z=1; $z <= ($j + 1); $z++) {
                if ($j%$z == 0) {
                        $dividend = $dividend + 1;
                        print "$z is dividend of $j and $j has dividend $dividend.\n"; 
                        if ($dividend > 501) {
                                print "$z is dividend of $j and $j has dividend $dividend.\n"; 
                                exit; }
                                }
                        }
                }

and with some logic in Python. The python script can be divided in three part, first we find the triangle number, then we calculate the numbers that factor the triangle number and place it into an array and the last step would be to calculate how many divisors the triangle number has.

#!/usr/bin/python 

import math

# Start the triangle by one
triangle = 0
for x in range(1, 100000):
        triangle = x*(x+1)/2
        #print "triangles is ", triangle

# We factor the triangle into prime numbers
        def primefactors(x):
                factorlist=[]
                loop=2
                while loop<=x:
                        if x%loop==0:
                                x/=loop
                                factorlist.append(loop)
                        else:
                                loop+=1
                return factorlist
        #print primefactors(triangle)

# We calculate the number of divisors of the triangle number
        divisor = 1
        for z in set(primefactors(triangle)):
                #print "{0}\t{1}".format(z,primefactors(triangle).count(z))
                divisor = (primefactors(triangle).count(z) + 1) * divisor
                #print "Triangle", triangle," has ", divisor," divisors."
                if (divisor > 500):
                        print "Triangle", triangle," has ", divisor," divisors."
                        quit()

Reference:
1) Integer Factorization
2) Triangular numbers
3) Number of divisors of a number

Setting replication MySQL version 5.5

So the other day I performed an upgrade of MySQL on a linode and notice that it wont start if I kept my old my.cnf file.

[11:47:06] user@linode1: ~ $ echo 'SHOW VARIABLES LIKE "%version%";' | mysql -u username -ppassword | grep innodb
innodb_version  5.5.31
[11:47:20] user@linode1: ~ $ 
131022 08:17:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
131022  8:17:56 [ERROR] An old style --language value with language specific part detected: /usr/share/mysql/english/
131022  8:17:56 [ERROR] Use --lc-messages-dir without language specific part instead.
131022  8:17:56 [Note] Plugin 'FEDERATED' is disabled.
131022  8:17:56 InnoDB: The InnoDB memory heap is disabled
131022  8:17:56 InnoDB: Mutexes and rw_locks use GCC atomic builtins
131022  8:17:56 InnoDB: Compressed tables use zlib 1.2.7
131022  8:17:56 InnoDB: Using Linux native AIO
131022  8:17:56 InnoDB: Initializing buffer pool, size = 128.0M
131022  8:17:56 InnoDB: Completed initialization of buffer pool
131022  8:17:56 InnoDB: highest supported file format is Barracuda.
131022  8:17:56  InnoDB: Waiting for the background threads to start
131022  8:17:57 InnoDB: 5.5.31 started; log sequence number 1678395
131022  8:17:57 [ERROR] /usr/sbin/mysqld: unknown variable 'master-host=192.168.140.120'
131022  8:17:57 [ERROR] Aborting

Thing is the replication entries in my.cnf need to be removed/commented out and salve needs to be set up from MySQL console.

So here is my new my.cnf.

[11:34:25] xavi@linode1: ~ $ sudo grep -v “^#” /etc/mysql/my.cnf | grep -v “^$”

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0
[mysqld]
server-id       = 2
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/english
skip-external-locking
bind-address            = 192.168.137.234
key_buffer              = 16M
max_allowed_packet      = 16M
thread_stack            = 128K
thread_cache_size       = 8
myisam-recover          = BACKUP
query_cache_limit       = 1M
query_cache_size        = 16M
expire_logs_days        = 10
max_binlog_size         = 100M
[mysqldump]
quick
quote-names
max_allowed_packet      = 16M
[mysql]
[isamchk]
key_buffer              = 16M
!includedir /etc/mysql/conf.d/

[11:34:37] xavi@linode1: ~ $

We load the above file or similar depending on your configuration. Now to configure slave we log into the MySQL CLI.


mysql> STOP SLAVE;
mysql> CHANGE MASTER TO MASTER_HOST=’192.168.140.120′, MASTER_USER=’replication-user’, MASTER_PASSWORD=’password’, MASTER_LOG_FILE=’mysql-bin.000747′, MASTER_LOG_POS=75797;
mysql> START SLAVE;
[/text]

That’s it.