Skip to content

Instantly share code, notes, and snippets.

View ajokela's full-sized avatar

A.C. Jokela ajokela

View GitHub Profile
ExportPlot <- function(gplot, filename, width=2, height=1.5) {
# Export plot in PDF and EPS.
# Notice that A4: width=11.69, height=8.27
ggsave(paste(filename, '.pdf', sep=""), gplot, width = width, height = height)
postscript(file = paste(filename, '.eps', sep=""), width = width, height = height)
print(gplot)
dev.off()
png(file = paste(filename, '_.png', sep=""), width = width * 100, height = height * 100)
print(gplot)
dev.off()
@ajokela
ajokela / monkey.rb
Created December 7, 2012 18:18
Creating a zipfile in ruby with a specific compression level
level = Zlib::BEST_COMPRESSION # or Zlib::BEST_SPEED
Zip::ZipOutputStream.open(zip_file) do |zip|
Dir.glob("**/*") do |filename|
entry = Zip::ZipEntry.new("", filename)
entry.gather_fileinfo_from_srcpath(filename)
zip.put_next_entry(entry, nil, nil, Zip::ZipEntry::DEFLATED, level)
entry.get_input_stream { |is| IOExtras.copy_stream(zip, is) }
end
end
@ajokela
ajokela / gist:4068912
Created November 13, 2012 22:36
here.net errors
NoA SDK: Setting callback URL to: http://here.net/security/oauth2/noa-callback sdk.js:15
Unsafe JavaScript attempt to access frame with URL http://here.net/50.07908,14.4332199,4,0,0,normal.day from frame with URL https://account.nokia.com/oauth2/authorize?client_id=2fcc46d799142f08f4295a1b0102187e&redirect_uri=http%3A%2F%2Fhere.net%2Fsecurity%2Foauth2%2Fsuccess%3FjsSdkId%3D436576061%26jsCallbackUrl%3Dhttp%253A%252F%252Fhere.net%252Fsecurity%252Foauth2%252Fnoa-callback&callbackUrl=http%3A%2F%2Fhere.net%2Fsecurity%2Foauth2%2Fnoa-callback&response_type=code&serviceId=HereMapsPROD&jsSdkId=436576061&ui=frame&sized=true&ch=&c6=&c23=&immediate=true&jssdk_no_session=true. The frame requesting access has a protocol of 'https', the frame being accessed has a protocol of 'http'. Protocols must match.
sdk.js:27
NoA SDK: Sending message using postMessage sdk.js:15
Unsafe JavaScript attempt to access frame with URL http://here.net/50.07908,14.4332199,4,0,0,normal.day from frame with URL https://account.nokia.com/oauth2/au
@ajokela
ajokela / gist:4062239
Created November 12, 2012 21:56
Mushroom + Bean + Corn Soup
Ingredients:
+ Black-eyed beans
+ Carrots
+ Celery
+ Corn - preferably on the cob, unshucked
+ Shallots
+ Mushrooms
+ Tomatillos
@ajokela
ajokela / acronym.rb
Created October 4, 2012 13:39
string to acronym
acronym = ''
bytes = [] # Byte array
long_string = ' ' + long_string # so the first letter, if capital, is included
long_string.each_byte {|byte| bytes.push(byte)} # Build array of bytes
bytes.each_with_index do |byte, index|
if byte > 64 and byte < 96 # This is the range for ASCII capital letters, your encoding may vary
# Capital letter
if bytes[index-1] == 32 # We check behind to see if the
#letter is preceded by a space. Again, encoding may vary. 32 is ASCII space.
acronym = acronym + long_string[index,1]
@ajokela
ajokela / foo.rb
Created August 10, 2012 18:20
List all Exception Types in Ruby
exceptions = []
tree = {}
ObjectSpace.each_object(Class) do |cls|
next unless cls.ancestors.include? Exception
next if exceptions.include? cls
next if cls.superclass == SystemCallError # avoid dumping Errno's
exceptions << cls
cls.ancestors.delete_if {|e| [Object, Kernel].include? e }.reverse.inject(tree) {|memo,cls| memo[cls] ||= {}}
end
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327
@ajokela
ajokela / copyfrom.rb
Created June 27, 2012 19:31
Copy From - Ruby/PostgreSQL (MRI)
#!/usr/bin/env ruby
require 'pg'
require 'stringio'
$stderr.puts "Opening database connection ..."
conn = PG.connect( :dbname => 'test' )
conn.exec( <<END_SQL )
DROP TABLE IF EXISTS logs;
@ajokela
ajokela / io.rb
Created June 22, 2012 18:34
Reading a file in one line - ruby
# If you need binary mode, you'll need to do it the hard way:
s = File.open(filename, 'rb') { |f| f.read }
# If not, shorter and sweeter is:
s = IO.read(filename)
@ajokela
ajokela / rackattack.rb
Created June 5, 2012 21:06
Rack::Callbacks
class TimeZone
def initialize(default)
@default = default
end
def call(env)
env['rack.timezone'] = find_timezone(env) || @default
end
end