Archive

Archive for the ‘Utility’ Category

Show Upload Progress with Python aiohttp and tqdm

March 11th, 2017 No comments

I wanted to be able to show file upload progress for my Python asyncio-based Pushbullet.com library but could not find any obvious solution using aiohttp and tqdm. Here is what I created. Essentially I intercept the calls to reading a file and update the tqdm progress bar accordingly.

The key elements look like this:

    class tqio(io.BufferedReader):
        def __init__(self, file_path):
            super().__init__(open(file_path, "rb"))
            self.t = tqdm(desc="Upload",
                          unit="bytes",
                          unit_scale=True,
                          total=os.path.getsize(file_path))

        def read(self, *args, **kwargs):
            chunk = super().read(*args, **kwargs)
            self.t.update(len(chunk))
            return chunk

Read more…

Categories: Utility Tags: , ,

Python asyncio and tkinter Together with TCP and UDP

February 3rd, 2017 No comments

I finally figured out at least one way to have a Python tkinter app use asyncio event loops, so I thought I should share it. In the end, the solution was simply running an asyncio event loop on another thread. I hope one day Python will merge the two event loops, but for now this works OK.

I have this on pastebin also:

The key points are extracted here:

        # Thread that will handle io loop
        def _run(loop):
            asyncio.set_event_loop(loop)
            loop.run_forever()

        ioloop = asyncio.new_event_loop()
        asyncio.run_coroutine_threadsafe(_connect(), loop=ioloop)  # Schedules connection
        t = threading.Thread(target=partial(_run, ioloop))
        t.daemon = True  # won't hang app when it closes
        t.start()  # Server will connect now

Read more…

Categories: Utility Tags: , , ,

MSP430 (or Arduino) Library for PCF8574 I2C Port Expander

August 8th, 2014 8 comments

port-expanderI bought an 8-port general purpose IO expander for adding ports to my MSP430 projects via I2C, but I couldn’t find any library for accessing it. Fortunately the I2C communication with the on-board Texas Instruments PCF8574 chip is pretty straightforward, so after studying the LiquidCrystal_I2C library, which uses the PCF8574 for communicating with the popular 162 LCDs, I made my own library for this port expander. You are welcome to it. I release it as Public Domain code.

Download PortExpander_I2C
Read more…

Searching GZipped Log Files

March 18th, 2014 1 comment

I have a few handy scripts for searching through log files, especially monitoring SSH login attempts. I cannot just grep through log files however, because the log files get “rolled”: compressed, and archived.

rob@kanga:/var/log $ ls -lh system.log*
-rw-r-----@ 1 root  admin   289K Mar 18 17:16 system.log
-rw-r-----  1 root  admin    79K Mar 18 00:00 system.log.0.gz
-rw-r-----  1 root  admin    39K Mar 17 00:02 system.log.1.gz
-rw-r-----  1 root  admin    36K Mar 16 00:02 system.log.2.gz
-rw-r-----  1 root  admin    35K Mar 15 00:02 system.log.3.gz
-rw-r-----  1 root  admin    25K Mar 14 00:01 system.log.4.gz
-rw-r-----  1 root  admin    69K Mar 13 00:01 system.log.5.gz
-rw-r-----  1 root  admin    68K Mar 12 00:01 system.log.6.gz
rob@kanga:/var/log $

Suppose you want to grep through your log files for SSH login activity, you can do it like this:

rob@kanga:/var/log $ { cat /private/var/log/system.log ; gunzip -c /private/var/log/system.*.gz ; } | grep sshd | wc -l
   11364
rob@kanga:/var/log $

The magic happens in the curly braces, which concatenates the standard output of all enclosed commands. Be sure to include a semicolon after the last command, right before the closing curly brace.

An even shorter example:

rob@kanga:/var/log $ { echo hello ; echo world ; } | cat -n
     1	hello
     2	world
rob@kanga:/var/log $
Categories: Utility Tags: , , , , , ,

Writing Finder Comments from the Command Line

August 7th, 2011 3 comments

I had some old (we’re talking Apple IIGS era old) files that I wanted to keep around, and I wanted Spotlight to show them if there was a valid hit. Many of the file formats I cannot read anymore, but even a raw dump of the file could at least reveal the information I needed. What I could not find online was a way to write Spotlight-findable data from the command line. My idea was to run the strings command and embed that as a comment. I finally figured it out on my own, and it involves embedding AppleScript in a shell script via osascript. Read more…

Categories: Utility Tags: , , , ,

Run Snow Leopard in Virtual Machine to retain PowerPC Applications

July 25th, 2011 31 comments

I finally discovered how to run Mac OS X 10.6 Snow Leopard in a virtual machine after I was caught off guard that Mac OS X 10.7 Lion no longer supports Rosetta, Apple’s technology for seamlessly running PowerPC applications on Intel processors. I have enough PowerPC applications (like The Print Shop, my old copy of PhotoShop, and my scanner driver) that I was not going to upgrade to Lion on my home computer, but since I have successfully installed Snow Leopard in a virtual machine, I think I will take the plunge after all (and thanks to this Front Row hack also). Read more…

Check for PowerPC Programs Before Upgrading to Lion

July 24th, 2011 6 comments

I discovered too late that Mac OS X 10.7 Lion does not support PowerPC executables as Snow Leopard did with Rosetta. I cannot say how long that would have kept me away from Lion, but I definitely would have done some more homework before taking the plunge. If you have not already upgraded, you might want to do a check to see what you might be losing: old printer drivers, scanners, obscure utilities. I have finally lost my scanner, which required a PPC driver in an old copy of Photoshop. Run one of these scripts to generate a list of all PPC-only executables on your system. Read more…

Categories: Utility Tags: , , ,

“Ultrafast” video compression with x264

July 19th, 2011 1 comment

The fastest video compression I have seen so far is using the x264 command line tool with its “ultrafast” preset. The resulting file has no audio, so it requires an extra step with QuickTime to finish the process. I recently review the compression software Elgato Turbo.264 HD Software Edition, and it was indeed faster than iTunes and QuickTime Player when converting to iPhone-compatible videos, but I noticed that x264 was faster still. Here are the steps you can take for “ultrafast” video compression.
Read more…

Categories: Utility Tags: , , , ,

Deleting Yourself with Mac Directory Services dscl . -delete

July 12th, 2011 1 comment

In case it ever crossed your mind, when you are at the command line, never type

sudo dscl . -delete /Users/rob

when you mean to type

sudo dscl . -delete /Users/proxy

It will slow down your productivity. Read more…

Categories: Utility Tags: , , , , ,

Stream iPhoto over SSH

June 15th, 2011 No comments

Many people seem to have enjoyed my post on Streaming iTunes over SSH, and today I needed to access my home iPhoto library from work. It turns out that we can use the same tricks we used for iTunes for iPhoto.
Read more…

Categories: Utility Tags: , ,