Jay Taylor | programmer notes

CAT | Uncategorized

Dec/10

11

Happy 12-11-10

Happy 12-11-10 — It’s the last date with this pattern for another hundred years, so enjoy it~

No tags Hide

Oct/10

6

HOWTO fix -fPIC errors

Whilst compiling fann/pyfann on a 64-bit Ubuntu machine, I got that annoying -fPIC compilation error:

../src/include/fann_internal.h:73: warning: function declaration isnât a prototype
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-x86_64-2.6/pyfann/pyfann_wrap.o build/temp.linux-x86_64-2.6/pyfann/fann_helper.o ../src/doublefann.o -o build/lib.linux-x86_64-2.6/pyfann/_libfann.so
/usr/bin/ld: ../src/doublefann.o: relocation R_X86_64_32 against `.rodata.str1.1′ can not be used when making a shared object; recompile with -fPIC
../src/doublefann.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
error: command ‘gcc’ failed with exit status 1

I did some googling and promptly found this Gentoo guide for resolving -fPIC errors,

http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=3

The case that ended up being relevant was:

3.f. Case 3: Lack of `-fPIC’ flag in the software to be built

This is the most common case. It is a real bug in the build system and should be fixed in the ebuild, preferably with a patch that is sent upstream. Assuming the error message looks like this:

Code Listing 6.1: A sample error message
.libs/assert.o: relocation R_X86_64_32 against `a local symbol' can not be used
when making a shared object; recompile with -fPIC .libs/assert.o: could not
read symbols: Bad value

This means that the file assert.o was not compiled with the -fPIC flag, which it should. When you fix this kind of error, make sure only objects that are used in shared libraries are compiled with -fPIC.

In this case, globally adding -fPIC to C[XX]FLAGS resolves the issue, although this practice is discouraged because the executables end up being PIC-enabled, too.

I was able to resolve the errors by exporting the -fPIC flags to the CC and CXX environmental variables, running ./configure again, and recompiling.

export CC=”gcc -fPIC”
export CXX=”g++ -fPIC”
make distclean; make clean
./configure
make
sudo make install
cd python
python setup.py build
sudo python setup.py install

It’s a nice win :)  Hopefully next time I encounter this problem I’ll remember.

At any rate, now with pyfann working, I can now resume this tutorial on extracting paragraphs from html.

Keywords:

“relocation R_X86_64_32 against” “can not be used when making a shared object;” “recompile with -fPIC”

compiling, compilation, linux, errors, 64-bit, fPIC, fann, pyfann, ai, recompiling, recompilation, programming, code, coding, gcc, g++

No tags Hide

This time it’s the old-spice guy in a form unlike that which I could have ever imagined possible:

Re:cool (Score:5, Funny)

by nacturation (646836) * writes: <nacturation@gmai … m minus language> on Friday August 27, @01:09PM (#33394538) Journal

It’d need some way to determine how your eyes are focused though – whether you are intending to look at your hud or something distant. Hold up an object up to your eye about where your glasses would rest. Close the other eye that won’t see the object. Look at the object, then look at the wall behind it.

Now look back at the object. Sadly, it isn’t your eye. But if it had a fine enough resolution it could be compatible with your eye. Look down, back up. Where are you? You’re on Slashdot looking for the display your display could look like. What’s in your hand? Back at me. I have it, it’s the iPhone 5 with a display so fine you can’t tell the difference. Look again. The iPhone display is now a projector. Anything is possible when your device is made from nanoresonators and not a retina display. I’m modded up.

, Hide

Found myself sort of longing to un-hungarianize some PHP classes.  Here is more information regarding Hungarian Notation.

First off, vim rocks.

Here is the regex to (pretty safely and reliably) de-hungarianify a PHP class:

(Each group is colored for clarity in identifying which section is which in the explanation)

%s/\$[a-z]\([A-Z]\)\([a-zA-Z]\+\)/$\l\1\2/ge | %s/\(private\|protected\|public\|var\)\s\+\(\$_\?\)[a-z]\([A-Z][a-zA-Z]\+\)/\1 \2\l\3/ge | %s/\(\$this->_\?\)[a-z]\([A-Z][a-zA-Z]\+\)\([^(]\)/\1\l\2\3/ge

Explanation:

%s/\$[a-z]\([A-Z]\)\([a-zA-Z]\+\)/$\l\1\2/ge

Does simple variable name transformation of $bSomeFoo into $someFoo. This also removes the hungarian notation from statically referenced variables.

%s/\(private\|protected\|public\|var\)\s\+\(\$_\?\)[a-z]\([A-Z][a-zA-Z]\+\)/\1 \2\l\3/ge

Strips the hungarian notation from class member variable declaractions, e.g. private $_bSomeOtherFoo into private $_someOtherFoo.

%s/\(\$this->_\?\)[a-z]\([A-Z][a-zA-Z]\+\)\([^(]\)/\1\l\2\3/ge

Strips the hungarian notation from class member variable references, e.g. $this->_bSomeOtherFoo into $this->_someOtherFoo.

Keep in mind that this could still break stuff if public class member variables are referenced externally in another file, or if the file you run this on references hungarian-notation in other classes that you haven’t updated.

, , , , , , , Hide

Jul/10

14

The Republic of Facebook

The Republic of Facebook – http://www.visualeconomics.com/the-republic-of-facebook_2010-06-29/

The graphic is pretty cool!  There are some pretty crazy numbers being thrown down too..

No tags Hide

Apr/10

25

URLDecode for Python one-liner

Googling for “python urldecode” led me to this, which contained a solution:

import re

def htc(m):
    return chr(int(m.group(1),16))

def urldecode(url):
    rex = re.compile('%([0-9a-hA-H][0-9a-hA-H])',re.M)
    return rex.sub(htc,url)

However, it is seems a overly complex and confusing when it could instead be equivalently  represented like this:

import re
_ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)

Surely you agree ;)

*Update Dec 6th, 2011*
Okay, so this came up up today when I searched for “python one-liner urldecode” [yea, I forgot that I wrote this! ^^], and I noticed that this isn’t actually a one-liner! Here is a the version with several statements crammed into one line:

import re,sys;print re.sub(r'%([0-9a-hA-H]{2})',lambda m: chr(int(m.group(1),16)), open(sys.argv[1]).read() if len(sys.argv) > 1 else sys.stdin.read())

And here is my .bash_profile alias that I use to make this conveniently accessible from the command line:

alias urldecode="python -c \"import re,sys;print re.sub(r'%([0-9a-hA-H]{2})',lambda m: chr(int(m.group(1),16)), open(sys.argv[1]).read() if len(sys.argv) > 1 else sys.stdin.read())\""

No tags Hide

Apr/10

18

Django + Oracle = …

Django + Oracle = one error after another.

It took 3 hours just to get the Oracle DB driver installed and working (ended up linking all the Oracle xxx.so libs into /lib64/xxx.so, and that worked.

Then I created my models:

class Customer(models.Model):
 id = models.IntegerField(primary_key=True)
 name = models.CharField(max_length=100)
 phone_number = models.CharField(max_length=40)
 address = models.CharField(max_length=100)
 city = models.CharField(max_length=50)
 state = models.CharField(max_length=2)
 zipcode = models.CharField(max_length=10)
 date_created = models.DateField(auto_now_add=True)
 financial_info = models.CharField(max_length=255)

 def __unicode__(self):
 return self.name

 class Meta:
 db_table = 'CUSTOMER'
 db_tablespace = 'tables'

When I went into the AdminSite to create a customer, everything seemed to be working well.  I filled out the form, pressed “Save”, and then was greeted with a lovely error page:

DatabaseError at /admin/jjauto/customer/add/
ORA-00904: "CUSTOMER"."ID": invalid identifier
...

Googling this term + django, I found that there was a ticket which had the same error. At the bottom it said a fix had been integrated with the latest SVN version.

I figured out how to run different versions of Django concurrently so as not to break my other sites running fine with Django 1.1.1.  After all this, I still got the same error when creating a new customer.

At this point I started digging again and discovered the SQL query that is causing the error:

SELECT * FROM (SELECT ROWNUM AS "_RN", "_SUB".* FROM (SELECT "EMPLOYEE"."ID", "EMPLOYEE"."NAME", "EMPLOYEE"."PHONE_NUMBER", "EMPLOYEE"."ADDRESS", "EMPLOYEE"."CITY", "EMPLOYEE"."STATE", "EMPLOYEE"."ZIPCODE", "EMPLOYEE"."DATE_CREATED" FROM "EMPLOYEE" WHERE "EMPLOYEE"."ID" = :arg0 ) "_SUB" WHERE ROWNUM <= 21) WHERE "_RN" > 0 (,)

So I took the query and modified it to use lower-case column names:


SELECT * FROM (SELECT ROWNUM AS "_RN", "_SUB".* FROM (SELECT "EMPLOYEE"."id", "EMPLOYEE"."name", "EMPLOYEE"."phone_number", "EMPLOYEE"."address", "EMPLOYEE"."city", "EMPLOYEE"."state", "EMPLOYEE"."zipcode", "EMPLOYEE"."date_created" FROM "EMPLOYEE" WHERE "EMPLOYEE"."id" = 1 ) "_SUB" WHERE ROWNUM <= 21) WHERE "_RN" > 0;

I ran it in SQL Developer and it executed fine, returning no hits.

It is becoming apparent that the problem is Django taking each column (e.g. “id”), and creating the table with the column name (e.g. “id”, lower-cased), but then when it runs this query, the column names become upper-case (i.e. “ID”).  Ok, that is dumb and should probably not be happening, but I’ll try and work with what I’ve got here.  I found the option to override the column names, and used a little vimfu to make it painless:

%s/^\(\s\+\)\([^ ]\+\)\( = .*\))$/\1\2\3, db_column='\U\2')/ge | %s/(, /(/ge

yielding:

class Customer(models.Model):
 id = models.IntegerField(primary_key=True, db_column='ID')
 name = models.CharField(max_length=100, db_column='NAME')
 phone_number = models.CharField(max_length=40, db_column='PHONE_NUMBER')
 address = models.CharField(max_length=100, db_column='ADDRESS')
 city = models.CharField(max_length=50, db_column='CITY')
 state = models.CharField(max_length=2, db_column='STATE')
 zipcode = models.CharField(max_length=10, db_column='ZIPCODE')
 date_created = models.DateField(auto_now_add=True, db_column='DATE_CREATED')
 financial_info = models.CharField(max_length=255, db_column='FINANCIAL_INFO')

 def __unicode__(self):
 return self.name

 class Meta:
 db_table = 'CUSTOMER'

Finally, the inserts work :) What a PITA…

No tags Hide

Comments found in article “Statistical Analysis of U of Chicago Graffiti“:

Re:License? (Score:3, Interesting)

by Hurricane78 (562437) writes: on Monday February 08, @12:11AM (#31058658)

Well, what did you expect, from a mindset is not attached to physical reality?
That it would make any sense at all?

The wall with the graffiti is a physical object.
A paper photo in your hand would be a physical object.

But neither the graffiti itself, nor a photo of it, are physical works.
They are ideas/information. Other rules apply.

“Licensing”/“copyright“ is a concept, based on the misconception that ideas/information would be physical objects, and the false need of some people, to control that information.
Trying to argue with it, using logic, is (because of that false base assumption) by definition impossible.

The real physical rules for information are: If it’s out there, it’s out. Period.
So you either never give it out, and won’t be able to prove that it exists at all. Or you give it out to your chosen group.
Which can for example be people that you trust. Or, as in this case, everybody.
In case you gave it to everybody who wants it… well, you should have thought earlier about that everybody could store and copy it at will. (Just like looking at the physical wall and then telling someone, or drawing it from memory, is storing and copying.)

It does not matter if people want to accept that. Just as it does not matter if people want to accept gravity.
You can try to enforce weird rules of behavior onto people, trough mental tricks of psychology. And it may be easier to do in this case, than it is for gravity. But in the end it’s futile. Because you can’t control the whole world. Even with ACTA.
If nothing else, you will end up banning the ability to look at it, because some people became really good at memorizing and reproducing it later. And everybody who can’t remember it, will by definition not remember that it existed.

Whenever anyone mentions “facts”, you know he tries to shove his dogmas down your throat.

by ericvids (227598) writes: on Monday February 08, @03:46AM (#31059328)

This may be the BEST counterargument ever to “all information should be free”. Bravo!

However, while I genuinely want to mod you up, I do believe that CURRENT laws to control information are stupid. Similar to how laws can sometimes be unfairly and maliciously used to allow known murderers to remain innocent and walk freely, many patents and copyrights are unfairly and maliciously used to prevent people from contributing to the greater good of humanity. Patents in particular are a minefield — something’s clearly wrong with a system that encourages trolls to cripple the true innovators.

Back to the topic, I believe what the researcher did, copyrighting her photographs, is all right, regardless of whether she released it under Creative Commons. I don’t believe she was copyrighting the actual message on the graffiti anyway, just the expression of it on photograph. (Of course properly the copyright should be attributed to both HER and whoever made the graffiti, but then I would suppose THAT’s public domain since the original author didn’t stake a claim to it…)

Pet peeve: Profane people propagating perfunctory pedantry.

, , , , Hide

The hardhack solution to effectively permanently disable duplicate post titles from being created in in wordpress doesn’t get any easier than this:

http://linuxil.wordpress.com/2008/02/24/wp-o-matic-quick-dirty-duplicate-post-fix/

Thanks to linuxil!

No tags Hide

<< Latest posts

Older posts >>

Find it!