Brad Lucas

Programming, Clojure and other interests

List Globally Installed Packages With npm

November 3, 2018

You may have installed npm packages globally. To find out try the following command.

$ npm list -g --depth 0
Continue reading →

Learning Ring Next Steps

May 22, 2018

Previously, I wrote about Clojure and Ring in an introductory post where a small debugging application called Echo was developed. If you haven't looked at that post and are looking for an introduction to Ring I suggest you do and then come back here.

For this post, I've created another sample application called ring-next which contains routines to demonstrate a number of concepts in Ring. These are:

  • Middleware
  • Responses
  • Parameters
  • Cookies
  • File Uploads
  • Routes
Continue reading →

Learning Ring And Building Echo

May 18, 2018

When you come to Clojure and want to build a web app you'll discover Ring almost immediately. Even if you use another library you are likely to find Ring in use.

What is Ring?

As stated in the Ring repository, it is a library that abstracts the details of HTTP into a simple API. It does this by turning HTTP requests into Clojure maps which can be inspected and modified by a handler which returns an HTTP response. The handlers are Clojure functions that you create. You are also responsible for creating the response. Ring connects your handler with the underlying web server and is responsible for taking the requests and calling your handler with the request map.

If you've had experience with Java Servlets you'll notice a pattern here but will quickly see how much simpler this is here.

Continue reading →

Unzip File If Necessary

May 17, 2018

Ran into a situation where an external vendor started sending csv files gzipped. The trouble was they started zipping only larger files. Since the system that ingested the files would break on a gz file I needed a routine to unzip the gz files and leave the non-zipped files alone.

    def unzip_if_necessary(self, filename):
        print "unzip_if_necessary: " + filename
        f = open(filename)

        # Read magic number (the first 2 bytes) and rewind.                                                                                                                                                                               
        magic_number = f.read(2)
        f.seek(0)
        f.close()

        if magic_number == '\x1f\x8b':
            print "gzip file"
            data = ''
            # with  gzip.GzipFile(fileobj=f) as f:                                                                                                                                                                                        
            with gzip.open(filename, 'rb') as f:
                data = f.read()
            print "Writing to " + filename
            with open(filename, "w") as f:
                f.write(data)
        else:
            print "csv file"

Gist

Continue reading →

Download All Files From Url

January 12, 2018

Use wget to download a web page and all associated files.

$ wget -r -np -k http://domain.com/url

If you don't have wget and you are using a Mac use brew.

$ brew install wget
Continue reading →