This is a pretty common problem as Python does not offer any variable checking mechanism and when we try to use non defined variable it will throw an NameError exception.
We can do try/expect way but it is not very useful, instead I propose to declare a Perl-style function.
def defined(var):
return var in globals()
if defined('temp'):
print('defined')
else:
print('undef!')
We have to quote a variable but that shouldn't be a problem. This isn't very stylish or probably the best way to do it, but it works like a charm.
Tuesday, January 31, 2012
Wednesday, January 18, 2012
SOPA and Wikipedia
We all hate SOPA act but we all love Wikipedia.
I understand that Wiki is striking but I also understand people who needs it, so if you need to access Wikipedia under blackout do this:
1) Open your wiki page like you would normal do but before it's fully loaded (text and pictures are visible) hit the "ESC" button on your keyboard like crazy and it should stop the blackout ;-)
Enjoy!
I understand that Wiki is striking but I also understand people who needs it, so if you need to access Wikipedia under blackout do this:
1) Open your wiki page like you would normal do but before it's fully loaded (text and pictures are visible) hit the "ESC" button on your keyboard like crazy and it should stop the blackout ;-)
Enjoy!
Friday, January 21, 2011
Ruby on Rails and encoding
Sometimes people just don't use UTF-8. I could understand the Chinese and Japanese folks but why Polish webmasters still use ISO-8859-2 ? I wonder...Anyway recently my friend found a bug in my app, it's quite tricky. I have an app that takes some strings from html document and then creates an form and submit that form to the server. Everything works flawlessly until we use IE on non-utf8 encoded page.
Because IE doesn't support accept-charset form attribute data is sent in the original encoding which is ISO-8859-2 in my case. My application default encoding is utf8 so when data arrives to the app it "thinks" that it's encoded in utf8 which is obviously wrong. So I got char \xF3 which is "ó" (this letter is by the way the same as "u" but we use both to complicate our language and make it l33t onRy! ;-) in Polish and it is a "invalid byte sequence in UTF-8" and my app crashes :D Soooooo great :D To crash an rails app just send invalid utf8 char to it !
Solution for this isn't simple. People tells about using iconv to replace/ignore/blank out the trouble making butes but I want my "ó" ! Another way is to detect the encoding and then do string.force_encoding(detected_encoding).encode!("UTF-8") which works but the problem is that in ruby 1.9 we do not have any good, working encoding detection method.
I've crawled the internets for hours and found only chardet gem (UniversalDetector) which fails to work under ruby 1.9 rails 3.0.3 and rchardet which also doesn't work but some awesome guy ported in to 1.9 but it still doesn't solve my problem and crashes when trying to detect encoding of my string !
So finally I wrote a little function, it's not perfect and it will eat letters from a string if we guess the wrong encoding, but it should work for most latin sites.
def vs(s)
if s.to_s.valid_encoding?
return true
else
begin
s.force_encoding('ISO-8859-2').encode!("UTF-8")
rescue ArgumentError
ensure
c = Iconv.new 'UTF-8//IGNORE', 'UTF-8'
s.replace c.iconv(s.dup + ' ')[0..-2]
end
end
end
Just have to remember, it's not a solution just ugly workaround
Wednesday, November 24, 2010
Rails TypeError (can't convert String into Integer) on save
RoR is fun !
Unless... you encounter some problem that google cannot explain ;)
So recently I've got some strange TypeError (can't convert String into Integer) in .rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:319 when doing .save on my object.
Records were successfully saved into database but this error stopped app from execution.
I've investigated this a little, trying to find this mysterious String which ruby wants to convert to Integer.
Let me tell you a little about my object I want to save.
class CreateSites < ActiveRecord::Migration
def self.up
create_table :sites do |t|
t.integer :id, :null => false
t.text :title, :null => false
t.column :hash, :'varchar(32)', :null => false
t.text :description, :null => false
t.column :user_id, 'integer references users(id)', :null => false
t.timestamps
end
end
def self.down
drop_table :sites
end
end
This is my migration file, clean and simple. Nothing can be wrong here, well that's a lie ;)
Ruby was complaining about my hash field, why ? Because every object in ruby has a hash method, which is used for eg. in comparison when doing Array.uniq (which is done in rails). Then when I created this migration, rails created a Class which was overwriting hash method (which returns int) to return the db value (which is string).
Sooooooo not cool ! Why this isn't documented anywhere ? Shame on you rails developers ! It should be red in migration guide - DO NOT USE "HASH" AS A FIELD NAME, IT WILL BREAK YOUR APP
Wednesday, November 17, 2010
Rails 3 + Foreign Key
Hi there !
Recently I've been doing some railing on rails 3. They're pretty cool actually but you can read about it elsewhere ;)
Though, there is this "foreign key" problem. Rails != Hibernate so sometimes we must work some magic.
We cannot do native foreign keys so we'll do it the "hard way" (which is btw very easy)
I'm using PostgreSQL (if you aren't - you should !) This method will probably won't work for other databases but I don't care, so...
We take our migration file and put there :
And that's it ! We don't need any plugins. Foreign keys are created.
Recently I've been doing some railing on rails 3. They're pretty cool actually but you can read about it elsewhere ;)
Though, there is this "foreign key" problem. Rails != Hibernate so sometimes we must work some magic.
We cannot do native foreign keys so we'll do it the "hard way" (which is btw very easy)
I'm using PostgreSQL (if you aren't - you should !) This method will probably won't work for other databases but I don't care, so...
We take our migration file and put there :
or something similar.t.column :site_id, 'integer references sites(id)', :null => false
t.column :user_id, 'integer references users(id)', :null => false
t.column :parent, 'integer references comments(id)'
And that's it ! We don't need any plugins. Foreign keys are created.
Foreign-key constraints:That's so awesome, isn't it ?
"comments_parent_fkey" FOREIGN KEY (parent) REFERENCES comments(id)
"comments_site_id_fkey" FOREIGN KEY (site_id) REFERENCES sites(id)
"comments_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
Referenced by:
TABLE "comments" CONSTRAINT "comments_parent_fkey" FOREIGN KEY (parent) REFERENCES comments(id)
Subscribe to:
Posts (Atom)