Sunday, November 18, 2012

Using LinQ in C#

LINQ is actually a pretty cool technology; it allows you to do something like list or monad comprehensions over collections, but with a sql-like syntax; better yet, it does magic so you can use SQL tables instead of collections, and it actually generates and sends the SQL to the DBMS, instead of getting all the data into memory and iterating over it.
Since I switch languages constantly, I'm always trying to remember the right syntax; so, this is my place to have it, for now. Before you can connect to your DB, you need to run a VS tool, that will generate the LINQ for your particular schema (and you need to rerun every time you change :( . In my case, the generated code is in the SimpleBlog package, and the generated class is SimpleBlogDataContext; so my code for reading something looks like:
And to add an element to the blog table, we can do:

Notice that the context (ctx here) needs to be a global variable; having several contexts will diminish performance and probably confuse you, since variables from one context cannot be saved in another.

We always need to import the System.Linq namespace when using linq; If we're doing this directly in an aspx page we use an import directive, like: Notice that you need to add an assembly reference (at least in VS2008, you need to add it to the web.config file, even if you add it to the project references in solution explorer), so add the following line to your web.config, inside the <assemblies> tag:  <add assembly="System.Data.Linq, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

Thursday, November 15, 2012

A simple blogroll with python and feedparser

A friend wanted to use a blog as a sort of database, allowing several people to create posts on the same blog, and then transform the posts into some sort of listing; most blogging software will let you export a list of posts as an rss feed, so this seemed like a good time to go learn how to parse and use rss.

While I'll let my friend figure out his own application, this prompted me to write a simple 'blogroll' program, that would read several rss (and atom) feeds and would produce html showing the appropriate titles and links.

Since python is my scripting language of choice for now, I googled rss parser libraries, and found feedparser.

We can call feedparser.parse, and give it a url; it will then return an object representing the feed; the object contains a field called feed, which contains information about the feed, like its title and its link (url); the object also contains entries, which is a list of objects, each representing one entry in the rss feed; for each entry, you have fields like its title and its link.

So, it is just a matter of iterating over a list of urls, parsing the feed for each, and going over its entries, producing html as we go, writing everything to a file. The final code looks like: