Scraping in Python for fun and profit
Missed out on this one lately.
My solution for Mad Libs, I need to practice some more regular expression. Rubular is a great help for people like me. RegExp is in a more free-form in Ruby compared to python, which is quite good for quick experiments on irb.
# solution to simpler part | |
# puts gets.chop.gsub!( /\(\([^))]*\)\)/) { |w| w=gets.chop} | |
# part 2 - along with substitution | |
dict = {} | |
puts IO.read(ARGV.shift).chop.gsub!( /\(\([^))]*\)\)/) { |w| | |
w = w.scan(/[^()]+/)[0] | |
combo = w.split(":") | |
if (combo.size==2) | |
print "Enter #{combo[1]} : " | |
w = dict[combo[0]] = gets.chop | |
else | |
if (dict[combo[0]] == nil) | |
print "Enter #{combo[0]} : " | |
w=gets.chop | |
else | |
w = dict[combo[0]] | |
end | |
end | |
} |
Labels: Ruby
This is an old one, pretty famously seen on uva, etc. I found Choice to be better than OptParse, as said in Choice docs - it like writing poems for command-line parsing :D
# Author: Abhishek Mishra <ideamonk@gmail.com> | |
require 'rubygems' | |
require 'choice' | |
Choice.options do | |
banner 'q2_lcd_numbers.rb [-shv] string' | |
separator 'Optional:' | |
option :size do | |
short '-s' | |
desc 'digit size' | |
cast Integer | |
default 1 | |
end | |
separator 'Common:' | |
option :help do | |
short '-h' | |
long '--help' | |
desc 'Show this message.' | |
end | |
option :version do | |
short '-v' | |
long '--version' | |
desc 'Show version.' | |
action do | |
puts 'LCD generator 0.1' | |
exit | |
end | |
end | |
end | |
def paint_map(map,digit,size,start) | |
# upper, mid, bottom | |
for i in (start+1)...(start+size+1) | |
if $digits[digit][0] == 1 | |
map[[0,i]] = 2 | |
end | |
if $digits[digit][3] == 1 | |
map[[0 + size + 1,i]] = 2 | |
end | |
if $digits[digit][6] == 1 | |
map[[0 + size*2 + 2,i]] = 2 | |
end | |
end | |
# verticals | |
for i in 1...size+1 | |
if $digits[digit][1] == 1 | |
map[[i,0+start]] = 1 | |
end | |
if $digits[digit][2] == 1 | |
map[[i,size+1+start]] = 1 | |
end | |
if $digits[digit][4] == 1 | |
map[[i+size+1,0+start]] = 1 | |
end | |
if $digits[digit][5] == 1 | |
map[[i+size+1,size+1+start]] = 1 | |
end | |
end | |
end | |
def print_map(map, size, total_digits) | |
for j in 0...(size*2+3) | |
for i in 0...(total_digits*(size+3)) | |
if map[[j,i]] == 2 | |
print '-' | |
elsif map[[j,i]] == 1 | |
print '|' | |
else | |
print ' ' | |
end | |
end | |
puts '' | |
end | |
end | |
def drive_paint(map, string, size) | |
i = 0 | |
j = 0 | |
while (i<string.size*(size+3)) | |
paint_map(map,string[j..j].to_i,size,i) | |
i += size+3 | |
j += 1 | |
end | |
end | |
# _ 0 | |
# | | 1 2 | |
# - 3 | |
# | | 4 5 | |
# - 6 | |
$digits = [ | |
[1,1,1,0,1,1,1], [0,0,1,0,0,1,0], # 0 1 | |
[1,0,1,1,1,0,1], [1,0,1,1,0,1,1], # 2 3 | |
[0,1,1,1,0,1,0], [1,1,0,1,0,1,1], # 4 5 | |
[1,1,0,1,1,1,1], [1,0,1,0,0,1,0], # 6 7 | |
[1,1,1,1,1,1,1], [1,1,1,1,0,1,0], # 8 9 | |
] | |
string = ARGV[-1] | |
size = Choice.choices[:size] | |
digitmap = {} | |
drive_paint(digitmap, string, size) | |
print_map(digitmap, size, string.size) |
abhishekmishra@mbp [~/code/BORQ]> ruby q2_lcd_numbers.rb -s 3 9535009187
--- --- --- --- --- --- --- --- ---
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
--- --- --- --- --- ---
| | | | | | | | | | | | |
| | | | | | | | | | | | |
| | | | | | | | | | | | |
--- --- --- --- --- ---
Labels: Ruby
Here are the slides for talk at the upcoming PyCon India 2010. See you this weekend.
So after looking at some slides on caching function returns in javascript, I was keen on trying out so in Python. And LOL I came up with this logic -
fun(val):
if val in cache.keys():
return cache[val]
else:
do the right thing...
abhishekmishra@mbp [~/code]> python pycaching.py
Poorly Cached - 340.23s
Exception Cached - 0.30s
Cached - 0.18s
Non Cached - 0.15s
Not Bitwise, Non Cached - 0.16s
try:
return self.cache[value]
except KeyError:
self.cache[value] = self.parse(value)
return self.cache[value]
if value in self.cache:
return self.cache[value]
else:
self.cache[value] = self.parse(value)
return self.cache[value]
Labels: caching, experiments, memoization, python
You find it to be a sorry ass pussy system that fails to respond to situations in time, you read up, watch stuff and blame on them for "not doing anything", but hardly would it strike to your conditioned imagination that they might just be so pre-occupied in doing something else that you could have never imagined. And yet you passive cunt, you take it all for granted and sleep for the next day to come by, expecting everything to be normal. Who knows for everything, that you quoted the system to be pussy about, over your cup of tea and a morning newspaper, there were these "अपने घर के चोर", thiefs, filling their pockets. On a side note, Now that things have become so well connected, the systems are so well informed, fed by numerous channels, and fingers just like the ones that typed out this piece of brainf*ck, clicking out zillions of preferences, we're not far from the point when it would be possible to crunch these into a whole new understanding, meaning, life. Just that in this spiderweb, analyzing nodes, reaching from one to another, predicting their minds and way of thinking has become easier.
Labels: rants
Labels: archlinux
Okay nothing much for this post, but I was just thinking about the RERO philosophy apparently popularized by ESR in CATB (I guess I won't need to read the 256 pages).