Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, August 2, 2010

web is a mess…where is the hope?

Recently I am helping designing and coding a web site, which I thought would be a small project… I did know about the difference between old IE and other “standard” browsers and some hacks and work-arounds, but I failed to realize it would be so painful to do all things “right”.

In stead of insisting everything should look exactly the same in all browsers, I think it is natual that some design details could degrade in older browsers, like round corners and text shadow. The real troublemaker turns form elements, each browser (depending on the operating system and even system theme) displays them differently – an awful nightmare :( There aren’t any standard to style them, the only solution is to employ JavaScript to hide the original form and render a fake one with div and spans. Thus select element becomes an unsorted list with a CSS’s and scripts, which seems very bizarre…

Maybe it is time to re-invent the web. People have been talking about this for decades, but I see little progress. I don’t believen HTML5 is a solution, though it’s really hot. Semantics is important, but I don’t believe it the key issue. Web pages are no longer simply documents, but applications, and we are distributing service instead of content. JavaScript has many bad parts but is blessed as well, devil or angel, it should be the core of web. HTML should be fundamentally changed to be a better present layer of JavaScript. A lot of functions carried out by browser natively should be given to JavaScript or other future client-side code. With more power and freedom of client code, coding for web could certainly bring more good things to users.

Monday, December 14, 2009

Idea for a simple bib tool

I was using Zotero for my research readings, however, it is a bit too heavy for me, and I want a tool that can be portable across machines. So I am planning to write a bib tool for myself when I am a bit free sometime next spring.

The most important concept in this tool:

  • Keep it Simple: there will NOT be fancy setup interface, but a config XML; there will not be many export styles for that is latex’s business.
  • Depend less: there is already many advanced tools for bib organization, but they depend on this and that. I plan to implement all this tool with Python and sqlite3, so it can be put into U-key/Dropbox, and used anywhere.

The basci senarios is:

  1. User start the tool, which set up a local web sever at a high port (eg: 8080), and open any browser to land on the interface.
  2. To add a new paper, user have to
    1. upload a pdf file
    2. add bibtex info by
      1. upload a crospending a bibtex file
      2. fill a form to generate bibtex info
    3. add optional tags
    4. add optional note
  3. To view paper:
    1. start a search (empty field means any)
    2. a list of reference is returned
    3. (optional) open pdf file
  4. To export
    1. start a search
    2. select paper and add to “basket”
    3. go to export folder and get a sorted bib for all and BIB’s for each files and copies of PDF’s

Suggestions are always welcomed :)

Thursday, December 3, 2009

What in_lto_p has _p suffix in GCC 4.5

Abstract: Curiosity killed the cat XD

Usually we use _p suffix to pointers in C language, but it is not the case for in_lto_p in GCC 4.5, so I searched for the reason, and according to <http://www.stanford.edu/~blp/writings/blp-stds/blp-stds_4.html> :

p-convention
A _p suffix indicates a boolean test for the condition described by the rest of the name. (This comes from Lisp.)

And it can be supported by this wiki page<http://en.wikipedia.org/wiki/P_convention> :

This practice originated among users of the Lisp programming language, in which there is the convention of appending the letter “P” on elements to denote a predicate (a yes or no question). It is most commonly used at MIT and the University of California, Berkeley, or among computer scientists working in Artificial intelligence (which frequently uses Lisp).

So _p for predicate not pointer here :)

Thursday, August 13, 2009

Good Book: CERT C Secure Coding Standard

Abstract: a good reference book on coding in C

It's a good reference book on coding nicely in C, which is the most dangerous programming language I know except for brain-f*ck...

Another good point is that you can read it free online: https://www.securecoding.cert.org/confluence/display/seccode/CERT+C+Secure+Coding+Standard.

Tuesday, July 28, 2009

bool & GCC

Abstract: I'll try to clear up confusion about bool for non-compiler people;)

Even some of my teammate on compiler would misunderstand bool with GCC some times. So I'm trying to make it a bit clear:

bool is not an valid type in C89, which is referred as ANSI C sometimes. Therefore, if you are working on a project that is supposed to be compiled with an ANSI C compiler, do not use bool type. However, bool is valid in C99 as _Bool, so you can use it safely if your code is not expected to be compiled with an ancient compiler;)

In GCC code, you can see 'bool' here and there, but bear in mind that it's fake - it's actually unsigned char, defined in gcc/system.h. This is because GCC is expected to bootstrap with an ANSI C compiler.

In C++, bool is always valid, you don't have to worry about bool.

Wednesday, May 6, 2009

How to process large text file efficiently in Python

Abstract

Three different ways of processing text file line by line are given in the order of increasing efficiency.

I have to handle a large text file of space-separated data in python, and the data goes like this:

tag1 tag2 tag3
12 34 12
123 345 12

the first line is tags for each column, and the rest lines hold data. Since the tags are fixed, I can code it directly in to my script, that is to say the first line should be skipped. My first script goes like this:

file = open('foo.txt', 'r')
for line in file.readlines()[1:]:
#do something

This script requires a vast amount of RAM, since it has to store a list of all lines! So it is wise to use iterator:

file = open('foo.txt', 'r')
first = True
for line in file:
if first:
first = False
else:
#do something

The second script works much better than the first one, because the lines are read one by one from the file by using a iterator. However, the first flag is not a neat way to skip the first line for we have to test the flag many times, which makes no sense. And the problem is solved in the third script:

file = open('foo.txt', 'r')
file.readline()
for line in file:
#do something

the 'fileread.line()' command will perfectly move the file position one line forward, and the iter will then start from the second line:)

Monday, May 4, 2009

How many lines does it take to learn a new language?

It seems to be impossible for me to learn a new programing language by reading books without practice, so the question in the title occurs to me – how many lines does it take to learn a language?

The number obviously depends on many factors: whether it is the first programming language to me, if I have learned a language with similar grammar/design before, the size of the standard library… and a lot!

And it’s even harder to define when I should say I have learned a programming language… Having been programming in C for years, I still check the standard library now and then :(

Maybe I should define a metric of the familiarity of a programming language, ask all my friends in this trade to help me collect data, and take some statistical approach to obtain a equation of lines needed to practice a language... Hmm, a lot work to answer this simple question, isn't it XD

Sunday, March 22, 2009

VIM in GNU coding style

I guess I'll start working on GCC recently, so I tried to prepare my VIM for GNU coding style. However, it's not as easy as I hoped, since VIM seems to favor BSD style more than GNU style which is a great waste of screen space in my view...Anyway, after a vast amount of Googling and attempts, it works now. Here's what I did to VIM: create a file named c.vim in my "~/.vim/after/ftplugin" directory, with the following content:
"GNU Coding Standards
setlocal cindent
setlocal cinoptions=>4,n-2,{2,^-2,:2,=2,g0,h2,p5,t0,+2,(0,u0,w1,m1
setlocal expandtab
setlocal shiftwidth=2
setlocal tabstop=8
setlocal softtabstop=2
setlocal textwidth=80
setlocal fo-=ro fo+=cql
I have to put it here otherwise the settings might be overrode by VIM's default ftplugin settings.