
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 France License.
I've already wrote something about OptParse last month. Today I discovered how to create a new option (that is not a string, int or bool) and validate it within the arg parser.
So suppose we want to write an application that can output both txt and html and we want the user to specify the format with command line option. One way would be to use a StdOpt.str_option - eventually with a default option - and to retrive it in application code with OptParse.Opt.get.
However this is not satisfactory as we are mixing the application code with command line parsing. A better way is to create a new type of option with Opt.value_option .
This is the concept :
Note that the function Opt.value_option get a default value, a metavar - that is the sting associated with the option in the help ( -o<txt|html>, --out=<txt|html> Output type ), a corce function, that is, a function that transforms a string in the desired type, and an error function that is used by the parser to give a meaningful error is the option is not correctly validated.
For example :
Now when we use this new option in the application code with OptParse.Opt.get and we can be certain that it was correctly validated.
This is an extract from a thread about the audio you can get on the latest shr-testing while calling and receiving calls. I put it here for reference.
I've changed in /etc/frameworkd.conf
and in /etc/freesmartphone/alsa/default/gsmhandset
then following http://wiki.openmoko.org/wiki/Neo_Freerunner_audio_subsystem#Alsamixer_channel_controls
I've changed :
My guess is that the two most important defaults are ti_calypso_dsp_mode and the control 12 .
After the latest Xorg upgrade I started experiencing multiple problems concerning the suspend/resume cycle on my laptop. Today I took sometimes off to debug the situation. It seems that the culprit is the activation of the KMS on the XOrg package that landed in unstable in December. The change log witness this change:
now while this is the right thing to do according to bug #555906 , I'm pretty sure this is the cause of my problems.
The ubuntu wiki has a detailed page about KMS, how to enable it and bugs reports. For a start I disabled the KMS adding the following lines to /etc/initramfs-tools/modules :
And regenerate your initramfs (mind that this regenerate the initramfs for the running kernel):
With this change I've got to the point where I can suspend/resume and switch back to a console if there is a problem. I've tested this with the kernel 2.6.31 and 2.6.32-trunk . The funny thing is that it seems the problem is quite random. The first suspend (hw or soft suspend gives the same result) is often ok. the second one usually fails. The logs of the X server do not tell me anything useful as much as the dmesg . To put a cherry on the cake, the iwlgn driver (intel again !) of my wireless card sometimes does not wake up properly (no useful messages again) forcing me to rmmod it and modprobe it back to spin it back to like. Grrr.
So I haven't actually solved the problem, but maybe somebody will get a bit further reading this enty.
At least now I've the ability to switch back to a console and restart gdm.
In other news, since I screwed my grub on friday and I had to fix it somehow with an external boot loader, this is the easiest way I've found to create a rescue system on a usb key : http://wiki.debian.org/DebianLive/Howto/USB (using the rescue live CD image).
Easiest as in : "dd the image on the key and be happy"
Every now and then I need to write a simple combinatorial algorithm. Using monads this is fairly easy and concise, but probably not the fastest way to do it. We start with the definition of a few functions in terms of the List module. The function themselves are kinda of self explanatory. I write this mostly for reference then for real added value.
The previous version of the code uses the List module. If we want a more space efficient implementation of the same functions, we can use a lazy data structure and substitute the functions in the preamble. In this case, instead of writing a lazy list module from scratch, we simply use the Enum module of ExtLib.
In action :
A small ocaml implementation of the Bron–Kerbosch algorithm to list all maximal cliques in an undirected graph. The description of the algorithm is on wikipedia. http://en.wikipedia.org/wiki/Bron–Kerbosch_algorithm . The example given is the same as in the wikipedia page.
To test it :
There is also a bit of code here. Even if it might be more efficient, it looks to me exceptionally complicated for such a simple algorithm...
Maybe is also worth mentioning the Maximal independent set problem that is the dual of the maximal clique problem. In other words, the list of all maximal independent set of a graph is nothing else that the list of maximal cliques of the complement of the input graph.
In the same spirit of this blog post http://chistera.yi.org/~adeodato/blog/106_fakeapt.html , this is a simple bash function to simulate an aptitude run on a given status and packages list. We assume yes for all question so to make aptitude not interactive and we assume the flag -f in order to alway try to fix broken universe before trying to satisfy the request.
I recently discovered the extLib OptPase module [1] . It's a very nice and complete replacement for the good old Arg in the standard library. I'm gonna give a small example on how to use it. I hope this can be useful to somebody.
I first build an Option module to clearly separate the options handling from the rest of my program. To keep it short we add only two options, debug and output. Debug has a short and long option, output is only a string. We also add two group options to spice up the example ...
To actually parse the options we have a main function that invokes the parse_argv function, stores all the options in the respective variables in the module Options and return a list of string containing all the positional arguments given on the command line that are not parsed as options.
[1] http://ocaml-extlib.googlecode.com/svn/doc/apiref/OptParse.html
I want to share a small snippet of code to upload a file to a remote server as a "multipart/form-data" . The function below gets two arguments. The server url ( ex: http://server.org/upload ) and a filename. First the filename encoded as a "form-data", then we use httplib to POST it to the server. Since httplib wants the host + path in separate stages, we have to parse the url using urlparse.
The receiving server must accept the data and return the location of the newly created resource. There are many snippet on the web, but I felt they were all incomplete or too messy. The encode function below is actually part of a snippet I found googling around. Happy uploading.
Since I'm working with Django, this is the server part. Few remarks:
myfiles and a form UploadFileForm that you can easily guess.handle_uploaded_file is the procedure that actually saves the file on the disk. This is standard.to install skype on a debian unstable machine :
now we need to fix a bunch of dependencies:
run skype.
blahhhhhh . It used to be easier... Sometimes I really despise myself for using this closed source software.
The other day I needed a small xml parser to convert an xml document into a different format. First I tried xml-light. This is a simple parser all written in ocaml that stores the parser xml document in an ocaml data structure. This data structure can be user to access various fields of the xml document. It does not offer a dom-like interface, but actually I consider this a feature. Unfortunately xml-light is terribly slow. To parse 30K-plus lines of xml it takes far too long to be considered for my application.
The next logic choice was to try Expat, that is a event-based parser and it is extremely fast. Since using an event based parser can be a bit cumbersome (and I already had written of bit of code using xml-light), I decided to write a small wrapper around expat to provide a xml-light interface to it.
The code is pretty simple and the main idea is taken from the cduce xml loader.
First we provide a small data structure to hold the xml document as we examine it. Nothing deep here. Notice that we use Start and String as we descend the tree and Element we we unwind the stack.
Then we need to provide expat handlers to store xml fragments on the stack as we go down. Note that we have an handler for cdata, but not an handler for pcdata as it is the default.
At the end we just register all handlers with the expat parser and we return the root of the xml document.
I've copied the xml-light methods and to access the document in a different file. I've also made everything lazy to save a bit of computing time if it is only necessary to access a part of a huge xml document.
The complete code can be found here: git clone https://www.mancoosi.org/~abate/repos/xmlparser.git
(this repo might disappear in the future ...)
Recent comments
46 weeks 3 days ago
50 weeks 3 days ago
1 year 10 weeks ago
1 year 12 weeks ago
1 year 14 weeks ago
1 year 17 weeks ago
1 year 18 weeks ago
1 year 48 weeks ago
2 years 1 hour ago