Wednesday, September 12, 2018

Javascript reduce note

row.locations = skuLocationsMap[sku].reduce(function (accum, curr) {
    return accum + (accum !== '' ? ', ' : '') + curr;}, '');
The above is a complex way of doing Array.prototype.join() but it's helpful for remembering how reduce works.

Wednesday, March 14, 2018

Scala Mockito workarounds

I was pleased to have figured out this org.mockito.stubbing.Answer solution (lines 4-8) to a null logger inside my Scala class.

 Also, that you can tell a mock to call the real method you are trying to test (line 9).

 1  @Test
 2  def testNullHandlerCallsStart(): Unit = {
 3    predictor.setHandler(null)
 4    when(predictor.log == null).thenAnswer(new Answer[Logger] {
 5      def answer(invocation: InvocationOnMock): Logger = {
 6        LoggerFactory.getLogger("Predictor")
 7      }
 8    })
 9    when(predictor.enqueueEvent(any())).thenCallRealMethod()
10    val deactivation = new UnitUpdateNotification(unit, unit.updateStatus(Status.INACTIVE, Action.DEACTIVATED, "damaged", "user"))
11    callback.handle(deactivation)
12    verify(predictor).enqueueEvent(any(classOf[Event]))
13  }

One thing, I had to make the logger inside my Predictor class publicly visible for it to work.

Monday, July 24, 2017

Making a list from a single element

Do
newArrayList(string)
Super handy method from Google Collections

Sunday, April 30, 2017

How to scan with an HP C309a on Mac OS X Sierra

I have an HP c309a photosmart premium all in one print fax scan copy (never used for faxing) that is a very slow scanner but it's a scanner that does work so I'm not going to replace it if I can help it. Though today I gave that serious consideration when I discovered that with the new Mac OS, Sierra (10.12.4 at "press" time), the scanner is no longer supported even by HP Easy Scan.

When I tried running HP Easy Scan I got an error popup that said "Scanner reported an error: HP Photosmart C309a series is currently unavailable. Ensure your device is powered on, check the connection, and ensure your network is functioning properly. If these conditions are correct, restart the device and try scanning again."

I tried all these things, of course, with no luck. Searching on this found nothing but the strong suggestion that this product has passed its end of life and is no longer supported by HP. Argh HP why do you insist on being so lame.

I spent a while looking at replacements. I really don't want to spend a couple hundred bucks to replace this. My husband's printer doesn't work (begging the question why he keeps it) so I can't just get a new scanner.

Then I discovered that you can go into System Preferences, to Printers-Scanners, choose the Scanners tab, and scan through that! This worked for the first scan, on the flatbed, but then when I tried to use the automatic feeder, it did all the scanning and then displayed "No document loaded..." on the screen.


Sinking heart. Really need the ADF.

Suddenly it occurred to me that maybe it did work - I looked in the target directory - my files were there! "Scan.jpeg", "Scan 1.jpeg", "Scan 2.jpeg", etc. I can work with this!

HTH,
kewpiedoll99

p.s. It turns out I misunderstood what it meant by "No document loaded": There's nothing left in the feeder.

p.p.s. I have been using a great PDF splitter and merger software since it was a (I think open source) project in dev on Source Forge or similar. It's PDF Split And Merge and it is great. They've really improved the UI. I never upgraded from the old version I had until now, when I lost the old version in a computer upgrade. Highly recommend this app if you ever need to split or merge PDF files.

Thursday, February 16, 2017

Use correct header with CURL

Upon submitting a request to my service like

CURL -X POST http://localhost:8600/a/b/c -d '{"assignmentId[]":[12345]}'

I got this error:

"A servlet request, to the URI http://localhost:8600/a/b/c, contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected."

Resolved with this answer from http://stackoverflow.com/a/33636404/187423 (thanks Arnold B.).

When I changed my request to

CURL -X POST http://localhost:8600/a/b/c -H "Content-Type: application/json" -d '{"assignmentId[]":[12345]}'

(adding the header) the service was able to parse the request and handle it normally.

HTH,
kewpiedoll99

Tuesday, April 5, 2016

Replacement for Windows Task Manager

Windows Sysinternals - a suite of sys admin tools (some require admin rights).

Download

Process explorer - a much better replacement for Windows Task Manager - doesn't require admin rights and can help deetermine if a process has a lock on a DLL.

TCPView - shows all your port connections.

et al.

Monday, January 12, 2015

Selected differences in sed on mac and other *nix

A coworker sent me this sed command that works in other versions of unix command line:

sed -i "s/filename_prd/filename_dev/g" filename_dev.sql

But on Mac OS this what I had to do:

sed 's/filename_prd/filename_dev/g' ./filename_prd.sql > ./filename_dev.sql

Evidently the "-i" is not required in Mac, the command being sent to sed must be in single quotes and not double, and the output of sed goes to std_out so it needs to be saved to another file. (You can save it to the file itself but I prefer to be conservative about this.)

Thursday, October 30, 2014

Thursday, October 2, 2014

grep in a bunch of files

I always have to look this up so I'm posting a note to myself here. To grep through all the files in a directory, recursively, and only get back a list of the files containing the string, do

$ grep -lr "searchterm" location

E.g.:

$ grep -lr "Enum" /var/log/rtr/pops

$ grep -lr "receipt" .

Found this in this blog.

Friday, September 5, 2014

Avoid error messages with `find` in *nix

Very useful comment on StackOverflow about how to get rid of error messages that come up when you use `find`.
You do not need sudo to run find for generally-accessible commands. If you don't want to see the error messages about inaccessible directories, get rid of the messages rather than using root privs unnecessarily. Using sudo all the time is a bad habit. Redirect stderr to /dev/null, like this:
find / -name java 2> /dev/null

Walter Underwood on SO

Tuesday, December 3, 2013

sed notes

I am a sed newb. Today I encountered usages of it that I want to note for future reference.

sed 's/^M//g' SHOP-684.sql > SHOP-684-noM.sql

Note: Hold the control key and then press v and m to get the control-m character.

This removes the ctrl-M's that litter Windows-saved files in a unix env. In a true unix env you can use dos2unix, but on a mac that command (and its counterpart unix2dos) are unavailable.

In order to replace ^M with newlines on a Mac, I had to do:

$ sed 's/^M/\    [ HIT ENTER ]
--- /g' SHOP-684.sql > SHOP-684-noM.sql

I have a giant file (almost 1 million lines) that I need to edit to remove tabs and other detritus to convert it into a usable SQL file. Opening it in intelliJ or vi takes a while, so it is great to be able to do this instead. A big plus: it returns almost immediately. It's very fast.

In order to replace tabs, since the version of sed on a Mac does not support \t in the left side of "s///", I used the control for it, which happens to be ^I. It looks like this when you hold the control key and press v and then i to get the control-i character:

sed "s/      //g" SHOP-684-noM.sql > SHOP-684-noT.sql

See also:



Wednesday, July 24, 2013

IFTTT test post!

I created a simple IFTTT recipe to sms me if there's a new post on this blog. Testing it out now.

This links (for me) to my personal IFTTT recipes: https://ifttt.com/myrecipes/personal

IFTTT stands for If This Then That.

Monday, April 22, 2013

How to find out if a domain name is a CNAME in Unix/Linux

$ host -t cname qadbrw01 
qadbrw01.cluster is an alias for va-qa-dbrw101.cluster.

Thursday, May 17, 2012

MySQL function GROUP_CONCAT and CAST

I have a table of emails that may have multiple entries for a contact and I wanted to join all the emails together before joining the emails table to the contacts in my select. My query looked something like this:

SELECT mc.contactid, ce.emailAddress as emailAddress
FROM merchant_contact mc
JOIN (
SELECT contactid, GROUP_CONCAT(DISTINCT emailaddress SEPARATOR ',') as emailAddress
FROM contact_email GROUP BY contactid
) ce ON ce.contactid = mc.contactid

It ran fine in dbVisualizer, but then when it ran as part of my Java app it returned values like

[B@2d7f2fae
[B@79135fd7
[B@66f95a5a


These looked something like pointer addresses, not the email values I was expecting.

After trying a few things that did not work, including adding group_concat as an sql function to my configuration (recommended here) I tried changing this:

query.addScalar("emailAddress", Hibernate.STRING);

to


query.addScalar("emailAddress");

to let Hibernate try to determine the type itself. Although this didn't fix it, I did get more information to work with, because it complained "No Dialect mapping for JDBC type: -4". Searching for this got me to this post on CodeRanch (which I find helpful from time to time) where the guy fixed his problem by CASTing it from an NVARCHAR to a VARCHAR. I tried a variation on this and it fixed my issue, so here is what I ended up doing:

SELECT mc.contactid, ce.emailAddress as emailAddress
FROM merchant_contact mc
JOIN (
SELECT contactid, CAST(GROUP_CONCAT(DISTINCT emailaddress SEPARATOR ',') AS char) as emailAddress
FROM contact_email GROUP BY contactid
) ce ON ce.contactid = mc.contactid

Posted here in case this helps someone going forward.

Monday, November 14, 2011

How to add up all numbers, one per line in a file

cat /tmp/foo | awk '{sum+=$1}END{print sum}'

(From Mike Masters, of course)

Friday, November 11, 2011

How to output the first line of each file in a directory


$ head -n 1 *


Sample output:
 $ head -n 1 *
==> Desktop <==

==> Development <==

==> Documents <==

==> Downloads <==

==> Dropbox <==

==> Environment <==

==> Library <==

==> Movies <==

==> Music <==

==> Pictures <==

==> Public <==

==> Sites <==

==> bin <==

==> current.html <==
Current IP CheckCurrent IP Address: 63.119.11.19

==> databases <==

==> my.cnf <==
[client]

(That's the directory structure of my home dir on my work machine.)
 

Wednesday, November 2, 2011

Notes on GROUP BY in MySQL

Here is a query I wanted to run, but I was concerned that the value of fat.rowsprocessed would not come from the same fat row as min(fat.processeddate).

select m.domain, ma.merchantacctid, ma.createddate, 
fat.rowsprocessed, min(fat.processeddate)
from merchant_account ma
join merchant m on m.merchantacctid = ma.merchantacctid
join ftp_audit_trail fat on fat.merchantacctid = 
    ma.merchantacctid
where fat.processeddate > ma.createddate 
and fat.rowsprocessed > 0
and ma.createddate > '2009-12-31'
group by fat.merchantacctid
order by domain;

My buddy Spencer pointed out that in standard SQL, if you use an aggregate function, then you have to include all the other fields you are selecting in the group by. It turns out that there is an extension to GROUP BY, and to HAVING, in MySQL that enables you to use them on a single field:

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate.
11.15.3. GROUP BY and HAVING with Hidden Columns

I was afraid that the db would pick any value of rowsprocessed, that it would not come from the same row that the min(processeddate) is from.

select m.domain, ma.merchantacctid, ma.createddate, 
fat.rowsprocessed, fat.processeddate
from merchant_account ma
join merchant m on m.merchantacctid = ma.merchantacctid
join ftp_audit_trail fat on fat.merchantacctid = 
    ma.merchantacctid
where fat.processeddate > ma.createddate 
and fat.rowsprocessed > 0
and ma.createddate > '2009-12-31'
group by fat.merchantacctid
having min(fat.processeddate)
order by domain;

HAVING is what I wanted to use. It's also not standard SQL legal but the same MySQL extension enables this.

I ran both, exported the csv's, and diff'd them, and they gave identical results. But I suspect that was luck in this case, and that the first query would not be dependably unarbitrary. I am more comfortable with the second query, using HAVING.
 

Monday, August 8, 2011

Set vim status line to show file name, format, column#, line#

:set statusline=%t\ %y\ format:\ %{&ff};\ [%c,%l]
Sample output: .vimrc [vim] format: unix [2,3].

Wednesday, July 13, 2011

How to restore Java 1.5 on Snow Leopard

Apple is so annoying. This morning I upgraded my Mac OS (a non-restart-required, supposedly low impact upgrade) and then discovered that my codebase, which requires Java 1.5, would no longer compile in IntelliJ. The upgrade had removed my install of 1.5 and replaced it with a symlink to 1.6. Why does Apple so badly want to force users into Java 1.6? It's extremely irritating to have to stop everything and remind myself how to do this all over again. I looked in here to see if I could find my notes and I could not.

On the command line:
cd /System/Library/Frameworks/JavaVM.framework/Versionssudo rm 1.5sudo rm 1.5.0
Open the file JavaForMacOSX10.5Update6.dmg with Pacifist.

Navigate inside the Pacifist display to /System/Library/Frameworks/JavaVM.framework/Versions.

Select 1.5.0 and Install to Default Location.

On the command line:
sudo ln -s 1.5.0 1.5



Some links:




UTA: I found my notes. In case these provide any additional context.

Restoring Java 1.5.22 to the machine

For some reason Apple saw fit to remove all versions of Java other than 1.6 in Snow Leopard. In the dir /System/Library/Frameworks/JavaVM.framework/Versions there are entries for "1.5" and "1.5.0" but they are symlinks to "CurrentJDK", which itself is a symlink to "1.6". In order to restore Java 1.5.22 I followed the suggestions of this blog page:

http://codethought.com/blog/?p=233

In a nutshell (in case the page goes away) I used Pacifist (http://www.charlessoft.com/) to open the Java for Mac OS X 10.5 Update 6 (http://support.apple.com/downloads/Java_for_Mac_OS_X_10_5_Update_6) package, and selected only the 1.5 and 1.5.0 elements for install, rather than running the whole update. Before doing this I had to delete the empty symlinks "1.5" and "1.5.0".

It's possible - the blog author notes that this happened to him - when installing the latest Java update for OS X 10.{?} it will change the frameworks dir and rename the "1.5.0" folder to "1.5.0 1", installing the symlink to "CurrentJDK" in "1.5.0"'s place. If this happens, just jettison the new "1.5.0" and rename "1.5.0 1" back to "1.5.0".