Categories
Coding Useful stuff

My favourite shell command for today – prepend lines of text in seconds

I’m working on a lead generation project for a client this week that, interestingly, led me to a simple, time-saving shell command that enables me to prepend lines of text with a simple quotation mark. It’s more impressive than it sounds because it saved me considerably more time than it took to write and run it.

One of the methods I’m using to find leads is by running Boolean searches that incorporate multiple search parameters. The search terms are pretty long and look a little like this: "Managing director" OR "sales director" OR "sales manager" OR "procurement manager" OR ...

My starting point is generally a list of search parameters that I need to convert into a Boolean search query. For example:

Managing director

sales director

sales manager

procurement manager

Except the lists are a lot longer. I started off by adding my lists to VS Code because working with this sort of thing in plain text greatly simplifies the process. One of the things I love about VS Code is that you can add a cursor to the end of each line in the text you select. This probably comes from Sublime Text and it’s an awesome feature.

The reason why this comes in handy is because I want to place each of those search terms into quotes so I can string them together with Boolean operators like AND, OR, and NOT.

The challenge I found myself facing is how to move the cursor to the beginning of each like to add the opening quotation mark? Because each line is a different length, I couldn’t just move the cursors from the end of each line to the beginning. I also didn’t want to overwrite the feature that lets me add cursors to the end of each line, either.

I thought that there must be some sort of shell command or script I could run that would parse the text file and add an opening quotation mark to the beginning of each line. It turns out there is (of course). Like many shell commands, it is deceptively simple and does exactly what it says on the box (if there was a box).

There are three options (probably more if you want something different) that I looked at:

sed -e 's/^/prefix/' file

# If you want to edit the file in-place
sed -i -e 's/^/prefix/' file

# If you want to create a new file
sed -e 's/^/prefix/' file > file.new

I went with option 3 and simply replaced the word prefix with my opening quotation mark and ran the command. I output the command to a new file because I didn’t want to destroy my original list. It ran instantly and when I opened the new file, I had quotation marks at the beginning of each line.

From there it was a relatively simple matter to add cursors to the end of each line to add my Boolean operator for my searches.

Using shell commands may seem absurdly geeky when we have GUIs for just about everything, but the more I learn about shell scripting, the more it amazes me. This particular line of code seems innocuous enough but it did something for me in seconds that would otherwise have taken a lot longer.

Categories
Coding

Real-time, date-time JavaScript code for your website

I’ve been on a little bit of a journey over the last 6 months or so. I’ll explain in a future blog post but the short version is that I’ve expanded my skills to web development. I’ve been developing solutions to challenges I face as I learn more, and as I continue to update my Modi’in Bus project. I thought I’d start sharing some of those little solutions here on my blog, in addition to other helpful spaces.

I wanted to create a live date/time string for Modi’in Bus. My goal was to use JavaScript to insert a line that stated the current date and time. I searched for a few explanations of how to effectively use the JavaScript getDate() method (here is the MDN documentation).

Adding the date is relatively straightforward. I couldn’t find a clean solution for adding a clock that changes in real-time, until I found this great implementation by Eugenio Leon. The challenge with the time is that you want the live display to be dynamic, and not just update when you refresh.

The solution I came up with (with the help of the resources I mentioned) uses ES6 syntax, and seems to work pretty much as expected.

This is my solution. The script determines the current date and time, and inserts it into a div element with the class of container in my site code:

// The purpose of this block is to list the month names which will correspond with the corresponding numerical value of the particular month that is other derived from the getDate() method.
Date.prototype.monthName = function() {
  const monthsOfYear = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  return monthsOfYear[this.getMonth()];
}

// As with the previous block, this block states the days of the week so the script can match the named days of the week to corresponding numerical values.
Date.prototype.dayName = function() {
  const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  return daysOfWeek[this.getDay()];
}

function realtimeClock() {
  const now = new Date(),
  today = now.dayName(),
  year = now.getFullYear(),
  month = now.monthName(),
  day = now.getDate(),
  secs = ('0' + now.getSeconds()).slice(-2),
  mins = ('0' + now.getMinutes()).slice(-2),
  hours = now.getHours(),
  container = document.querySelector('.clock');
  
  container.innerHTML = `Today is ${today}, ${day} ${month} ${year}. The current time is ${hours}:${mins}:${secs}`;
  requestAnimationFrame(realtimeClock);
};

requestAnimationFrame(realtimeClock);

I thought I’d share it because I couldn’t find anything that did the trick quite like this on StackOverflow.

Featured image by by Loic Djim on Unsplash

Categories
Applications Blogs and blogging

Options for sharing video outside YouTube and Vimeo

I uploaded a video as part of a short post earlier this week and I noticed that it didn’t display particularly well in the post. I could have shared to YouTube first and embedded the video but thought I’d see what a direct upload would do. Turns out, not much. 

Surely it is possible to embed videos like you can images and have them render as playable videos in blog posts? Doesn’t HTML 5 have a video tag that works in all the major browsers?

WordPress has its VideoPress player but it’s a paid feature. Shouldn’t this capability be more freely accessible? What are the options?

Categories
Applications

Mark Zuckerberg is building a “Jarvis” personal AI

I love this and I am looking forward to seeing whether Mark Zuckerberg can build something like “Jarvis” from Iron Man; a personal AI.

I think this is going to happen in the near future and I’m looking forward to being able to talk to my home using natural language and have it do stuff for me and give me information.

We already have devices like Amazon’s Echo, Google Now and lesser services like Siri (really not that smart but the potential is there). If you start introducing really smart systems like IBM’s Watson, I think there is potential for something amazing.

Unless we create SkyNet in the process. Which would be bad.

Source: Fortune Magazine


Photo credit: Encuentro con el Sr. Mark Zuckerberg, CEO de Facebook. by Presidencia de la República Mexicana, licensed CC BY 2.0

 

Categories
Events and Life Mindsets People

Cracking the kiddie code

I’ve been thinking about our kids and their quirks and triggers a little more lately. In one sense it’s always on my mind because we interact with them daily, obviously, but what I mean is that I have been thinking about the little opportunities to understand our kids better as if they represent codes we can decipher.

Our daughter is not a morning person (she takes after her mother in this respect) and virtually every morning involves an argument with her at various stages of the process of getting her ready for school. As is probably the case with most parents and their kids, kids don’t respond to circumstances the way we do or would expect them to and it is easy to become frustrated with them, particularly when we are running late and like to miss that train to work.

It’s not that we don’t want to understand them, it’s more that when you have 30 minutes to feed and dress yourself and kids and still get out the door on time, your tolerance for delays is pretty low and, yet, that is exactly when every little thing involves a 10 minute debate.

I noticed that even once my daughter is out of bed and sitting at the breakfast table with a bowl of cereal in front of her that she likes (I did a lot of A/B testing to narrow down the shortlist of favorites), she has this habit of stabbing at her cereal with her spoon quite aggressively for a few minutes before dropping her spoon and sobbing. Telling her to eat her food, predictably, doesn’t work and she just becomes more frustrated (and so do I).

One morning I had an idea and asked her if she wanted more milk in her cereal. She was already sobbing and nodded so I added more milk and that seemed to resolve the situation for her because, now, she could dunk all of the chocolate balls in milk before eating them. At least, that mostly addressed her frustration. She also seems to expect the cereal not to float on the surface of the milk and doesn’t realize that if it all floats then it doesn’t all float uniformly because she hasn’t eaten any of it to spread the rest out more evenly. On the other hand, I think I at least made progress so she has fewer tearful, cereal-stabbing moments before actually eating her cereal.

This is just one of the many challenges I face in the mornings while we are rushing to get ready for school and work and most of these challenges seems to be another little code to crack and a little strategic time to spend to make future mornings a little more efficient. That said, an analytical approach rarely works with kids who tend to be pretty irrational and lateral for the most part but, then again, I don’t remember anyone telling me this whole parenting thing would be easy or boring.

Categories
Mindsets Social Web

Laws are not just relevant after an event

Craig Rodney wrote a post I agree with, to a degree, earlier today and which he titled "Cultivating a social culture”. He makes an argument for culture’s primacy as a preventative tool for many risks and I agree with that. Essentially, he argues that engaging with your team and empowering them to make better decisions about what they do online is far more effective than just imposing a social media policy. As he points out towards the beginning of his post –

Tackling the topic of employees and social media is not simple, and thinking that having a social media policy in place will save you is dangerously shortsighted.

At the same time, his argument that culture is the cure is similarly shortsighted. Although he opens the door to some sort of legal framework as part of the solution to employees going off on a tangent and wreaking all sorts of havoc (he wrote his post in the context of employees using social media in problematic ways), his conclusion misses a number of important nuances, highlights fundamental problems with how lawyers generally tend to approach risks and ignores a function of law which, ironically, supports his argument for culture’s importance:

The only way through

The solution is to turn social into a positive strategic objective. Educate and encourage employees to make the most of social media. Teach them the rights and wrongs. Show them successful case studies of amazing social usage, and show them examples of when things go wrong. Empower them to make smart choices, to proudly represent your brand and to grow themselves using everything that’s available online. Give them a directive for effective collaboration.

Or you can try block social media on work computers, pretend that your policy has you covered, and see what happens.

Law is Cultural Code

Professor Lawrence Lessig wrote, in his book “Code”, the following –

Cyberspace demands a new understanding of how regulation works. It compels us to look beyond the traditional lawyer ’s scope—beyond laws, or even norms. It requires a broader account of “regulation,” and most importantly, the recognition of a newly salient regulator.

That regulator is the obscurity in this book’s title—Code. In real space, we recognize how laws regulate—through constitutions, statutes, and other legal codes. In cyberspace we must understand how a different “code” regulates—how the software and hardware (i.e., the “code” of cyberspace) that make cyberspace what it is also regulate cyberspace as it is. As William Mitchell puts it, this code is cyberspace ’s “law.” “Lex Informatica,” as Joel Reidenberg first put it, or better, “code is law.”

Lawyers and legal theorists get bothered, however, when I echo this slogan. There are differences, they insist, between the regulatory effects produced by code and the regulatory effects produced by law, not the least of which is the difference in the “internal perspective” that runs with each kind of regulation. We understand the internal perspective of legal regulation —for example, that the restrictions the law might impose on a company’s freedom to pollute are a product of self-conscious regulation, reflecting values of the society imposing that regulation. That perspective is harder to recognize with code. It could be there, but it need not. And no doubt this is just one of many important differences between “code” and “law.”

I prefer the idea that “law is code”. This idea has pretty much stuck with me since I wrote my motivation to be accepted into Wits University’s LLB program (this was back in the day when an LLB was a post-graduate degree only). I wrote something to the effect that, to me, law was integral to our societal fabric. Today I would say that law is part of and is embedded into our cultural code.

As I understand what Rodney wrote, he suggests there is a distinction between law and culture and the two are largely incompatible, possibly even mutually exclusive, almost like two magnets with the same polarities held together. It requires effort to bring culture and law together and the moment you remove the pressure, the two fly apart. I don’t believe that this is the case at all. I believe culture is shaped by law in varying degrees, just as law is shaped by culture (although at different rates).

Blame the lawyers, I do

This idea of law as being at odds with culture is prevalent in agency environments and its something I think about often. One of the reasons why I think this perception is so widely held and why it frequently leads agencies to steer clear of lawyers is because lawyers have historically been monumental and figurative pains in the butt when it comes to creative work, especially with the emergence of the social Web. The metaphor I have been using a lot lately is this idea of lawyers staging interventions, striding into creative people’s space and kicking over their blocks, telling them what they can’t play with.

Do this enough and you can understand why lawyers are quickly perceived as undesirables and only to be called when the proverbial dark matter hits the fan and you need to bring out the crisis managers (aka, the lawyers). By that time, it usually is more about damage control, recriminations and more fuel for growing distaste for and distrust of lawyers (who wonder why they weren’t brought in much earlier?). I see this happening far too often. I have been the lawyer called in to fix messes for many years.

The thing is, incorporating Legal into a process from the beginning can actually help avoid or, at least, mitigate future issues. Yes, lawyers tend to block creative ideas (even the ones who don’t think they do) and whittle down truly innovative campaigns into a series of iPad giveaways (thanks for the analogy, Rich) and that is rarely the best approach. A far better approach is to find the ways to support those creative stunts agencies come up with. There is often a way to make that happen and to keep modifications to those stunts to a minimum.

It is very possible to implement effective legal frameworks into innovative campaigns, you just need the right people to do that. Unfortunately, and as the part joke, part lament goes, 99% of lawyers give the rest of us a bad name and most lawyers who are brought in to advise agencies and the ones who don’t want any creative people playing with fun toys for fear of the risks involved. What they just don’t seem to appreciate is that risk is inherent in digital campaigns and it is more about managing that risk to acceptable levels. Most campaigns are doable and what is required are similarly creative legal frameworks and models to support them.

Integrate, integrate, innovate

When it comes to employees using social media, social media policies are hopelessly limited in isolation (although this is how many people develop them). They are only really effective when developed in broader cultural and strategic contexts as part of an integrated approach. Calling these frameworks “social media policies” is also a bit dated (that model made more sense in 2010 to 2011 or so when I worked on the first policies of their kind in SA amidst all the original hype around them). Engagement models are changing and the legal frameworks that organisations need are far more complex and interconnected.

If you want to manage the risks of employees using social media services, an integrated approach involving culture, education, empowerment and appropriate (and complete) legal frameworks is the most effective approach. Only talking to your lawyer after “an event”, as Rodney puts it, is too late. Involving your legal team early could prevent the “event” in the first place or likely mitigate the harm. The key is an integrated and collaborative approach, beginning to the end, not one now and the other later.