Skip to content

Instantly share code, notes, and snippets.

@89465127
89465127 / maven_scala.bash
Created April 18, 2013 22:03
Setting up & running a simple hello world project with Scala and Maven
# first we'll create a new project
mvn archetype:generate -DarchetypeGroupId=org.scala-tools.archetypes -DarchetypeArtifactId=scala-archetype-simple -DgroupId=myGroup -DartifactId=myTestProject -Dversion=1.0 -DinteractiveMode=false
# then we will package it to create a jar, and populate target/
cd myTestProject
optional: rm -rf src/test
mvn package
@89465127
89465127 / java_scala.bash
Created April 18, 2013 23:42
How to compile and run HelloWorld programs in java and scala through the command line terminal, with jar dependencies.
#Compile HelloWorld, with jars in this directory
javac -cp ".:*" HelloWorld.java
scalac -cp ".:*" HelloWorld.scala
#Run
java -cp ".:*" HelloWorld
scala -cp ".:*" HelloWorld
# HelloWorld.java
class HelloWorld {
@89465127
89465127 / scala_collections.scala
Last active December 16, 2015 10:58
Simple experiments with scala lists and maps.
// define a simple list
scala> val l = List("a", "b", "c")
l: List[java.lang.String] = List(a, b, c)
// print each element of the list
scala> l.foreach{ println }
a
b
c
@89465127
89465127 / cli_progress.py
Last active January 9, 2019 10:43
Simple example of how to get a python progress bar in your program.
import time
from progress.bar import Bar # sudo pip install progress
bar = Bar('Processing', max=20, suffix='%(index)d/%(max)d - %(percent).1f%% - %(eta)ds')
for i in range(20):
time.sleep(.05) # Do some work
bar.next()
bar.finish()
@89465127
89465127 / error.py
Created April 26, 2013 17:38
Simple example showing how you can still raise an error in Python, but report more details with it.
other_var = 42
try:
print 1/0
except Exception as e:
print "\nError encountered. Other var = {}\n".format(other_var)
raise e
print 'You will not see this text'
@89465127
89465127 / mock_file.py
Created April 26, 2013 18:58
How to mock a file in memory for testing and experimenting.
import StringIO
# Create mock file
mock_file = StringIO.StringIO()
for i in range(20):
mock_file.write("{}\n".format(i))
mock_file.seek(0)
# Do something
for line in mock_file:
@89465127
89465127 / multi.py
Created April 26, 2013 19:03
Multiprocessing example, iterating over each line in a file.
import multiprocessing
import StringIO
# function to be run on each line of file
def f(x):
print x,
# Create mock file
mock_file = StringIO.StringIO()
for i in range(20):
@89465127
89465127 / write_line.py
Created April 29, 2013 18:56
Printing to a single line in Python, in separate steps, flushing output.
from __future__ import print_function
import sys
import time
print('Executing...', end='')
sys.stdout.flush()
time.sleep(2)
print('Done.')
@89465127
89465127 / spark_ubuntu.bash
Last active December 16, 2015 19:39
How to unzip and install Spark on Ubuntu.
# Download and unzip spark to your home directory
# You may want to run update if it's on a fresh OS
sudo apt-get update
# Install dependencies (& verify)
sudo apt-get install default-jdk scala git
java -version; javac -version; scala -version; git --version
# Configure - run these in your open terminal and add them to ~/.bashrc
rsync -avz /path/to/local/sync/folder -e "ssh -i /path/to/ssh/key" ubuntu@ec2instance:/path/to/remote/sync/folder