Musings of a dad with too much time on his hands and not enough to do. Wait. Reverse that.

Category: technology (Page 8 of 36)

Docker and WSL 2

I clearly missed the announcement: Docker Desktop for Windows will now run on Windows Subsystem for Linux (WSL) version 2.

Up until a few days ago, I had understood that I would only be able to run Docker for Windows if I spent the $100-$200 to upgrade my operating system from Windows 10 Home to Windows 10 Pro, since Pro includes virtualization features needed for Docker that Home does not. At work, I run Docker Desktop for Windows and hoped I could leverage that work experience in some of my home projects.

So, I had resigned myself to learning about the docker/linux experience at home while enjoying the Windows-based experiences at work.

Over the weekend, I upgraded to WSL 2–a surprisingly easy upgrade–and then for kicks, installed Windows Terminal. Then, I prepared for disappointment as I started researching the Docker install on WSL. And then I clicked on the link from the Microsoft article and read this:

Docker Desktop for Windows is available for free.

Requires Microsoft Windows 10 Professional or Enterprise 64-bit, or Windows 10 Home 64-bit with WSL 2.

Awesome! So I can get the full Docker Windows experience without having to upgrade to Win10 Pro. Exciting!

The install went smoothly, so I’ll hopefully have one or two Docker-type posts in the future.

Maintaining a Positive Sentiment

For various reasons, companies like to know the emotional dispositions of their customers and, sometimes, their employees. One common way to determine these states is through a technique called Sentiment Analysis.

One challenge with sentiment analysis is simply knowing how to score words, one way or another. Our brains know that the word “happy” is usually associated with a positive sentiment because we have a dictionary of sorts in our brains that associates “happy” to a favorable emotional state. Computers don’t immediately know these associations, so you usually have to supply them such mappings, often in the form of simple lists of words.

Now, suppose you want to maintain the appearance of positivity with an organization with whom you interact–maybe through online reviews, email, chat, or some other written medium. Here’s an idea you can try.

Step 1: Downlist a list of positive words

Positive (and negative) word lists are easily available on the Internet. Here’s one I downloaded.

Step 2: Write some PowerShell to extract some random, positive words you can use in your correspondence

If you’re running Microsoft Windows, you’ll likely have PowerShell installed, which is a convenient tool to parse the file you downloaded and provide back a few random words. Here’s a one-liner I wrote:

((cat c:\positive-words.txt|? {!$_.startswith(';') -and $_.length -gt 0}|get-random -count 3) -join '. ') + '.'|set-clipboard

In this code, I load in the word file into memory and then filter out lines that start with semicolons–in the file I downloaded, the author used semicolons to denote comment lines I need to exclude–and empty lines. Next, I select three random lines as each word is on a new line. Next, I put a period between words. Finally, I send the results to the Windows clipboard for easy pasting into the editor in which you are communicating–email, chat window, etc. The result is something like this:

pleasantly. enthralled. idolize.

But wait, there’s more

The above is great, but to get your words, you’ll have to execute the code in a PowerShell command shell. Who has time for that? How about executing your PowerShell from a batch file? Add the following to a new bat file:

set c="((cat c:\positive-words.txt|? {!$_.startswith(';') -and $_.length -gt 0}|get-random -count 3) -join '. ') + '.'|set-clipboard "
powershell -command %c%

Now, place that bat file on your desktop or in a convenient area where you can double-click on it to get your words easily. Even better: make a shortcut to it with a tool like Slickrun.

Now, you can easily leverage positive words in your correspondence and maintain a positive persona to the watchers.

Python and SOAP

Simple Object Access Protocol, or SOAP, was the new hotness in web service technology…some 15 or 20 years ago. It was built around XML, Web Service Definition Language (WSDL), XML namespaces and other complex ideas.

Today’s web service standard is Representational state transfer (REST)–a much simpler approach to data transmission over the web. Instead of trading around clunky XML files, REST APIs typically leverage sleeker JSON (JavaScript Object Notation) documents in their communication.

But some things never die and, recently, I found myself elbow-deep into a number of SOAP APIs while trying to pull data from a vendor product. I wrote a Python client to interface with those APIs. While Python has a number of packages designed to work with the technology, I wanted to stick with just the requests package to keep my dependencies minimal. Ultimately, my client worked well and I wanted to share a few tidbits here that I learned along the way to get my requests code to successfully call SOAP web services.

1. The header can be tricky

Getting your header right is critical to successful service calls. I found two header elements essential for my code: SOAPAction and Content-Type. It was important that I set SOAPAction to a url corresponding with the particular web method I wished to call. The vendor documentation was pretty important here to determine what that url should be.

What’s interesting about Content-Type is that the web is full of valid suggestions for the proper value: text/xml and application/soap+xml are two that I’ve seen bandied about. In my case, neither value worked. Again from the vendor documentation, the value that made my calls work was application/x-www-form-urlencoded. So, my header dictionary looked roughly like this:

headers = {'SOAPAction': 'http://somesite.com/webservices/SomeMethod', 'Content-Type': 'application/x-www-form-urlencoded'}

2. The post data doesn’t necessarily need to be XML

Crazy notion, right? Posting non-XML to a SOAP API? Early on in my work, I kept trying to format all my post arguments into a single XML document and tried to push that document to the web method with my requests call, but the code would never work. At some point, I stumbled upon a forum or discussion thread where one of the participants posted code that actually used a dictionary for his post data object–what you would normally do with a REST API. I was taken aback but gave it a go and, to my astonishment, it worked! Some web methods required simple parameters like strings and integers, ready made for Python dictionaries. A few did have a parameter or two of XML. For those, I simply had to push a string representation of a properly formatted XML document. My code looked something like this:

import requests

str_xml = '<some_doc><some_elem>1</some_elem></some_doc>'
post_data = {'token': 'blah', 'search_xml': str_xml}
ws_url = 'https://somesite.com/ws/something.asmx/SomeMethod'
resp = requests.post(ws_url, data=post_data, headers=headers)

3. Parsing the XML response can be tricky

I “believe” the most appropriate way to deal with XML responses in the response object is through the content property. But, since the response is supposed to be XML, I wanted to run the content through ElementTree to get a proper XML document I could more easily process. In my early attempts, I passed the content value to ElementTree’s fromstring function to get back a proper XML document that I can process like any other XML document. Or so I thought.

The rub is that fromstring returns an XML element, not an XML document. You have to add one more line, a call to the ElementTree constructor itself, you get the proper XML document object you can use in the rest of your code. My response processing code then looked like this:

import xml.etree.ElementTree as ET


resp = requests.post(ws_url, data=post_data, headers=headers)

resp_elem = ET.fromstring(resp.content)
resp_doc = ET.ElementTree(resp_elem)

# now, you can use functions like find and findall with the resp_doc object

So, the next time you find yourself having to work with SOAP APIs–and hopefully you don’t–there are some handy tips and tricks to consider.

« Older posts Newer posts »

© 2025 DadOverflow.com

Theme by Anders NorenUp ↑