Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 18 total  RSS 

ARGV Parser

This function parse ARGV and return a string.
See exemples for more informations :

// Go : http://blackh.badfile.net/wordz/

Function :

  def options(param)
  
	i = 0
		ARGV.each  { |valeur|
		
    		if (valeur == '-' + param.to_s)
				return ARGV[i+1]
			elseif (valeur != '-' + param.to_s)
				return false
			end
		i += 1
		}
		
   end


Usage :

// cmd> ruby test.rb -o foo

	out =  self.options('o')

	if (out != false and out.empty? == false)
                   puts out # print -> foo
	end

MySQL Database Size

mysql -uroot -D sfcnet_development -e "show table status\G"| egrep "(Index|Data)_length" | awk 'BEGIN { rsum = 0 } { rsum += $2 } END { print rsum }'

Mount SSH using MacFUSE

This will also list the voume in the Finder, thanks to the volname option (I think)

mkdir -p ~/mnts/formandfx
sshfs kmarsh@formandfx.com: ~/mnts/formandfx -oping_diskarb,volname=formandfx

Bandwidth Monitoring From the Console Using 'pktstat'

Pktstat, http://www.adaptive-enterprises.com.au/~d/software/pktstat/, displays a real-time summary of packet activity on an interface. Each line displays the data rate associated with a particular class of packets. The class is determined by the packet header.

For example, 'pktstat -w1' will refresh once per second showing the average bandwidth usage and active connections:

# pktstat -w1

  interface: eth0 
  load averages: 285.9k 167.3k 67.3k bps 

    bps    % desc 
  466.8   0% arp 
             tcp 205.188.1.192:5190 <-> c-24-21-74-176:1472 
   6.0k   0% tcp c-24-21-74-176:3933 <-> host75:3724 
  63.5k   1% tcp c-24-21-74-176:4662 <-> p50906304:61951 
  27.4k   0% udp 10.141.192.1:bootps <-> 255.255.255.255:bootpc

Correcting a command

If you've run a command that you discover needs a path, you can do something like this on the following line:
$ psql -U postgres mydb_here
bash: psql: command not found

$ /usr/local/pgsql/bin/!!
/usr/local/pgsql/bin/psql -U postgres mydb_here

Welcome to psql 7.3.5, the PostgreSQL interactive terminal.
mydb_here=#


Or if you mistyped the path:
$ /usr/loca/pgsql/bin/psql -U postgres mydb_here
-bash: /usr/loca/pgsql/bin/psql: No such file or directory

$ ^loca^local
usr/local/pgsql/bin/psql -U postgres mydb_here

Welcome to psql 7.3.5, the PostgreSQL interactive terminal.
mydb_here=#

Model, View, Controller HOWTO

MVC howto
Original vrsion can be found at http://kbrooks.ath.cx/mvchowto/intro.py

#!/usr/bin/python

# MVC Howto - Introduction & code
# What is "MVC"?
#   MVC stands for "Model", "View" & "Controller", a way of seperating your code into
#   manageable and independent chunks that can be changed easily.
# Why use MVC?
#   Because code is independent from each other, it becomes easier for you to change part of the code to do what you want.
#   For example, you could change where data is shown without changing anything else!
# Copyright (c) 2006 Kyle Brooks. Licensed under the Creative Commons Attribution License.
# We will be creating a command line calculator in this HOWTO.
import sys
# Model: a object that interfaces with the real world and returns information.

class Model:
    def calculate(self, left, op, right):
        if op == "+":
            return left + right
        elif op == "-":
            return left - right
# As you see in the code above, the model has the action "calculate".

# View: a object that gets passed the result from the model via the controller. Displays data.
class View:
    def display_data(self, result):
        print result
# As you see in the code above, ALL the view does is display data from the controller.

# Controller: a object that holds a reference to the model and view. Communicates with the model and view. Handles data from the user.
class Controller:
    operands = ["+", "-"]
    def __init__(self, model, view):
        self.model = model
        self.view = view
    def run(self):
        sys.stdout.write(">> ")
        sys.stdout.flush()
        # read a line
        line = sys.stdin.readline()
        line = line.strip()
        if line == "":
            sys.stdout.write("\n>> ")
            sys.stdout.flush()
            return
        for operand in self.operands:
            larray = line.split(operand)
            if larray[0] == line:
                continue
            else:
                break
        else:
            sys.stdout.write("NaN\n")
            sys.stdout.flush()
            return
        larray[0] = int(larray[0])
        larray[2] = int(larray[0])
        result = self.model.calculate(*larray)
        self.view.display_data(result)

Test�よ~

arectwevw

Mail a file as an attachment from the UNIX prompt

uuencode file.txt file.txt | mail email@address.com

Easy file encryption and decryption from the shell

Works on OS X, Linux, anywhere with OpenSSL installed:

To encrypt a file:
openssl des3 -salt -in infile.txt -out encryptedfile.txt


To decrypt the file:
openssl des3 -d -salt -in encryptedfile.txt -out normalfile.txt


Do not specify the same file as input and output on encryption.. I have noticed weird effects on OS X (it eats the file). Remove the -in * stuff if you want to pipe data into it (e.g. a tarred folder). Omit the -out * stuff if you want it to pipe data out on STDOUT.

Change file extensions with bash shell

To change all .htm files in a folder to .html files:

for f in *.htm; do mv $f `basename $f .htm`.html; done;
« Newer Snippets
Older Snippets »
Showing 1-10 of 18 total  RSS