Python in Functional Style: How to add 2 lists of integers without using loops

Usually you’d add a list of integers this way:

[pastacode lang=”python” manual=”a%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ab%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ac%20%3D%20%5B%5D%0Afor%20i%20in%20range(len(a))%3A%0A%20c.append(a%5Bi%5D%20%2B%20b%5Bi%5D)” message=”” highlight=”” provider=”manual”/]

You can do it functionally without any loops in different ways:



Using map and a lambda that adds them up

[pastacode lang=”python” manual=”c%20%3D%20list(map(lambda%20x%2Cy%3A%20x%2By%2C%20a%2C%20b))” message=”” highlight=”” provider=”manual”/]

or you can import the add operator as a named function

[pastacode lang=”python” manual=”from%20operator%20import%20add%0Ac%20%3D%20list(map(add%2C%20a%2C%20b))” message=”” highlight=”” provider=”manual”/]


Ever zipped two lists into a list of tuples?

There’s another more convoluted way if you want to play with “zip”.

Close End 10cm 80cm 5# 10pcs white Metal Zipper for Sewing ...
Imagine a jacket zipper and the teeth on each side of the zipper is one element on each one of the list.


When you zip the lists a and b, you end up with a list of tuples of matching elements from the given lists.

[pastacode lang=”python” manual=”%3E%3E%3E%20list(zip(a%2Cb))%0A%5B(2%2C%202)%2C%20(2%2C%202)%2C%20(2%2C%202)%2C%20(2%2C%202)%5D” message=”” highlight=”” provider=”manual”/]

you could now map a function to add the elements within each tuple on that list.


[pastacode lang=”python” manual=”%3E%3E%3E%20list(map(lambda%20tup%3A%20tup%5B0%5D%2Btup%5B1%5D%2C%20zip(a%2Cb)))%0A%5B4%2C%204%2C%204%2C%204%5D” message=”” highlight=”” provider=”manual”/]


Notice how we don’t convert to list after zip, we can work directly with the zip iterator, we only convert to list with the final map iterator.

Python 2 & 3 Note:

In Python 2 it’s not necessary to use list(), the map() and zip() methods return lists there. But stay away from Python 2, a lot of projects are now discontinuing support.

[CODING/SOLVED] gradle build (android) breaks after upgrading a dependency with NullPointerException thrown at ProgramClass.constantPoolEntryAccept

You’ve just upgraded one of your Android project’s dependencies and when you ./gradlew assembleRelease the build process breaks.

You invoke it again with --stacktrace to find the following exception:

 

[pastacode lang=”java” manual=”java.lang.NullPointerException%0Aat%20proguard.classfile.ProgramClass.constantPoolEntryAccept(ProgramClass.java%3A537)%0Aat%20proguard.shrink.UsageMarker.markConstant(UsageMarker.java%3A1246)%0Aat%20proguard.shrink.UsageMarker.visitRequiresInfo(UsageMarker.java%3A1040)%0Aat%20proguard.classfile.attribute.module.ModuleAttribute.requiresAccept(ModuleAttribute.java%3A138)%0Aat%20proguard.shrink.UsageMarker.visitModuleAttribute(UsageMarker.java%3A739)%0Aat%20proguard.classfile.attribute.module.ModuleAttribute.accept(ModuleAttribute.java%3A99)%0Aat%20proguard.classfile.ProgramClass.attributesAccept(ProgramClass.java%3A619)%0Aat%20proguard.shrink.UsageMarker.markProgramClassBody(UsageMarker.java%3A124)%0Aat%20proguard.shrink.UsageMarker.visitProgramClass(UsageMarker.java%3A94)%0Aat%20proguard.classfile.visitor.MultiClassVisitor.visitProgramClass(MultiClassVisitor.java%3A67)%0Aat%20proguard.classfile.visitor.MultiClassVisitor.visitProgramClass(MultiClassVisitor.java%3A67)%0Aat%20proguard.classfile.visitor.ClassNameFilter.visitProgramClass(ClassNameFilter.java%3A128)%0Aat%20proguard.classfile.ProgramClass.accept(ProgramClass.java%3A430)%0Aat%20proguard.classfile.ClassPool.classesAccept(ClassPool.java%3A124)%0Aat%20proguard.classfile.visitor.AllClassVisitor.visitClassPool(AllClassVisitor.java%3A45)%0Aat%20proguard.classfile.visitor.MultiClassPoolVisitor.visitClassPool(MultiClassPoolVisitor.java%3A85)%0Aat%20proguard.classfile.ClassPool.accept(ClassPool.java%3A110)%0Aat%20proguard.shrink.Shrinker.execute(Shrinker.java%3A90)%0Aat%20proguard.ProGuard.shrink(ProGuard.java%3A381)%0Aat%20proguard.ProGuard.execute(ProGuard.java%3A145)%0Aat%20proguard.ProGuard.main(ProGuard.java%3A572)” message=”” highlight=”” provider=”manual”/]

This is a ProGuard bug, which my friend, has been solved by the ProGuard team ages ago, and your build environment is using an old ProGuard version.

Add this to your build.gradle to force it to use the latest version (as of today it’s 6.2.2, check the latest version here)

[pastacode lang=”java” manual=”buildscript%20%7B%0A%20%20%20%20…%0A%20%20%20%20dependencies%20%7B%0A%20%20%20%20%20%20%20%20…%0A%20%20%20%20%20%20%20%20classpath%20’net.sf.proguard%3Aproguard-gradle%3A6.2.2’%0A%20%20%7D%0A%7D%0A%7D” message=”force a newer proguard version for your android build” highlight=”1,3,5″ provider=”manual”/]

nginx server configuration for a wordpress instance served from a URL’s subdirectory

You want to serve a wordpress instance on a website’s domain url but not at the path’s root, you want it under a sub-directory, for example “blog”, the same as this blog:

https://www.gubatron.com/blog 

Here’s how my NGINX’s server block for ‘www.gubatron.com’ looks like at the moment (https/ssl hasn’t been configured yet)

[pastacode lang=”javascript” path_id=”fc6f967d7689931c81e9e9691b714f29″ file=”” highlight=”” lines=”” provider=”gist”/]

Here is the equivalent in lighttpd, too bad lighttpd has no plans for HTTP2, it’s much friendlier and flexible to configure than nginx in my humble opinion.

[pastacode lang=”javascript” path_id=”d6e2f73026cf61ac74e1d45ce8a401b5″ file=”” highlight=”” lines=”” provider=”gist”/]

I used to host this website and wordpress on lighttpd, lighttpd’s config file is very powerful, it’s all based on matching server variables and applying rules, I will miss it dearly, things like having a compressed file cache and it’s flexibility, but I have to move on to nginx if I want to use http2, the lighttpd has no plans for http2 support and it’s just much faster and efficient than http 1.1.

gradle/groovy: A simple way to check if a gradle task name has been invoked (e.g. “assembleRelease” for Android developers)

If you google for this question, you’ll find a bunch of crap answers about creating tasks and checking the task graph, bullshit.

All you need to do is check if a parameter has been passed to gradle.

Keep it simple and stupid:

[pastacode lang=”java” manual=”boolean%20isAssembleRelease%20%3D%20gradle.startParameter.taskNames.contains(%22assembleRelease%22)” message=”” highlight=”” provider=”manual”/]

(If you’re working with an android project, you can define that variable before the android { section starts)

Then, if you need to do something different somewhere else down in your script, say, ask for a key alias and key password to sign your release (because they invoked ./gradlew assembleRelease you do:

[pastacode lang=”java” manual=”signingConfigs%20%7B%0A%20%20%20%20%20%20%20%20release%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(isAssembleRelease)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20your%20code%20here%20to%20ask%20for%20the%20key%20alias%20and%20password%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Solving “Received fatal alert: handshake_failure” error when performing HTTPS connections on a custom made JRE with jlink

TL; Tell me already what to do:
Add the jdk.crypto.cryptoki module to the list of --add-modules parameter to your jlink command invocation


If you’re reading this you’re one of the few developers out there that wanted to distribute a java 9+ app (using either jdk 9, jdk 10, jdk 11 or jdk 12, as of this writing) with a smaller version of the jdk, to build your custom jre, you used the jlink tool.

When you run your app using the full JRE that comes with the OpenJDK, your app is working fine when it comes to making https requests, but when you run your app using your custom jre you get the following error when opening https connections

[pastacode lang=”bash” manual=”Received%20fatal%20alert%3A%20handshake_failure” message=”” highlight=”” provider=”manual”/]

This issue occurs because your JRE is missing lots of Cipher Suites that come with the full JDK.

With your JDK, you can try to check the list of supported ciphers with this one liner using the jrunscript tool:

[pastacode lang=”bash” manual=”jrunscript%20-e%20%22print(java.util.Arrays.toString(javax.net.ssl.SSLServerSocketFactory.getDefault().getSupportedCipherSuites()))%22%0A” message=”” highlight=”” provider=”manual”/]

however that might not work for your custom JRE if you haven’t included the scripting module, so here’s a Java program I made that prints all the available Ciphers of your JRE

[pastacode lang=”java” manual=”public%20class%20PrintCiphers%20%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09var%20sslSocketFactory%20%3D%20javax.net.ssl.SSLServerSocketFactory.getDefault()%3B%0A%09System.out.println(%22SSLServerSocketFactory%20-%3E%20%22%20%2B%20sslSocketFactory.getClass().getName())%3B%0A%09try%20%7B%0A%20%20%09%20%20%20%20var%20getSupportedCipherSuitesMethod%20%3D%20sslSocketFactory.getClass().getMethod(%22getSupportedCipherSuites%22)%3B%0A%09%20%20%20%20String%5B%5D%20ciphers%20%3D%20(String%5B%5D)%20getSupportedCipherSuitesMethod.invoke(sslSocketFactory)%3B%0A%09%20%20%20%20int%20i%3D1%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(String%20c%20%3A%20ciphers)%20%7B%0A%09%09System.out.println(i%2B%2B%20%2B%20%22%20%22%20%2B%20c)%3B%0A%09%20%20%20%20%7D%0A%09%7D%20catch(Throwable%20t)%20%7B%0A%09%20%20%20%20t.printStackTrace()%3B%0A%09%7D%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

If you run PrintCiphers on your OpenJDK’s full JRE, you will see almost 50 Cipher Suites available:

[pastacode lang=”bash” manual=”%24%20java%20PrintCiphers%3B%0A1%20TLS_AES_128_GCM_SHA256%0A2%20TLS_AES_256_GCM_SHA384%0A3%20TLS_CHACHA20_POLY1305_SHA256%0A4%20TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384%0A5%20TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256%0A6%20TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256%0A7%20TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384%0A8%20TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256%0A9%20TLS_RSA_WITH_AES_256_GCM_SHA384%0A10%20TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384%0A11%20TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384%0A12%20TLS_DHE_RSA_WITH_AES_256_GCM_SHA384%0A13%20TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256%0A14%20TLS_DHE_DSS_WITH_AES_256_GCM_SHA384%0A15%20TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256%0A16%20TLS_RSA_WITH_AES_128_GCM_SHA256%0A17%20TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256%0A18%20TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256%0A19%20TLS_DHE_RSA_WITH_AES_128_GCM_SHA256%0A20%20TLS_DHE_DSS_WITH_AES_128_GCM_SHA256%0A21%20TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384%0A22%20TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384%0A23%20TLS_RSA_WITH_AES_256_CBC_SHA256%0A24%20TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384%0A25%20TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384%0A26%20TLS_DHE_RSA_WITH_AES_256_CBC_SHA256%0A27%20TLS_DHE_DSS_WITH_AES_256_CBC_SHA256%0A28%20TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA%0A29%20TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA%0A30%20TLS_RSA_WITH_AES_256_CBC_SHA%0A31%20TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA%0A32%20TLS_ECDH_RSA_WITH_AES_256_CBC_SHA%0A33%20TLS_DHE_RSA_WITH_AES_256_CBC_SHA%0A34%20TLS_DHE_DSS_WITH_AES_256_CBC_SHA%0A35%20TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256%0A36%20TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256%0A37%20TLS_RSA_WITH_AES_128_CBC_SHA256%0A38%20TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256%0A39%20TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256%0A40%20TLS_DHE_RSA_WITH_AES_128_CBC_SHA256%0A41%20TLS_DHE_DSS_WITH_AES_128_CBC_SHA256%0A42%20TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA%0A43%20TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA%0A44%20TLS_RSA_WITH_AES_128_CBC_SHA%0A45%20TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA%0A46%20TLS_ECDH_RSA_WITH_AES_128_CBC_SHA%0A47%20TLS_DHE_RSA_WITH_AES_128_CBC_SHA%0A48%20TLS_DHE_DSS_WITH_AES_128_CBC_SHA%0A49%20TLS_EMPTY_RENEGOTIATION_INFO_SCSV%0A” message=”” highlight=”” provider=”manual”/]

but if you use your custom JRE to run PrintCiphers you will see only 23 Cipher Suites available:

[pastacode lang=”bash” manual=”%24%20jre%2Fbin%2Fjava%20PrintCiphers%0A1%20TLS_AES_128_GCM_SHA256%0A2%20TLS_AES_256_GCM_SHA384%0A3%20TLS_CHACHA20_POLY1305_SHA256%0A4%20TLS_RSA_WITH_AES_256_GCM_SHA384%0A5%20TLS_DHE_RSA_WITH_AES_256_GCM_SHA384%0A6%20TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256%0A7%20TLS_DHE_DSS_WITH_AES_256_GCM_SHA384%0A8%20TLS_RSA_WITH_AES_128_GCM_SHA256%0A9%20TLS_DHE_RSA_WITH_AES_128_GCM_SHA256%0A10%20TLS_DHE_DSS_WITH_AES_128_GCM_SHA256%0A11%20TLS_RSA_WITH_AES_256_CBC_SHA256%0A12%20TLS_DHE_RSA_WITH_AES_256_CBC_SHA256%0A13%20TLS_DHE_DSS_WITH_AES_256_CBC_SHA256%0A14%20TLS_RSA_WITH_AES_256_CBC_SHA%0A15%20TLS_DHE_RSA_WITH_AES_256_CBC_SHA%0A16%20TLS_DHE_DSS_WITH_AES_256_CBC_SHA%0A17%20TLS_RSA_WITH_AES_128_CBC_SHA256%0A18%20TLS_DHE_RSA_WITH_AES_128_CBC_SHA256%0A19%20TLS_DHE_DSS_WITH_AES_128_CBC_SHA256%0A20%20TLS_RSA_WITH_AES_128_CBC_SHA%0A21%20TLS_DHE_RSA_WITH_AES_128_CBC_SHA%0A22%20TLS_DHE_DSS_WITH_AES_128_CBC_SHA%0A23%20TLS_EMPTY_RENEGOTIATION_INFO_SCSV%0A” message=”” highlight=”” provider=”manual”/]

To solve the problem you must add the jdk.crypto.cryptoki module to the list of --add-modules parameter to your jlink command invocation, next time your run PrintCiphers you should see the full list of Cipher Suites and your SSL handshake issues should be gone.

Introducing Yuca: A light-weight, in-memory, fast and simple to use search engine library.

https://github.com/gubatron/yuca

If your app can’t handle or doesn’t really need installing a full featured and heavy search engine like Lucene, nor you want to depend on a SQL database for indexing and doing simple search based strings you can use Yuca to index documents under any number of arbitrary keys which can be grouped under tags.

The shared library currently weighs ~170kb without any packing optimizations, we hope to reduce the size further in the near future.

Today, Wed May 9th the library is only available as a C++ shared or static library, the goal is to have bindings for popular programming languages, the first being Java since I need to use it on Android apps I’m developing.

If you feel like you need something like this and you’re not coding in C++, please create an issue on the github repository asking for the language bindings that you need and I’ll try to prioritize your request for the next set of language bindings.

How to run your Kotlin gradle built app from the command line

So you build your Kotlin app, you went through the trouble of creating a build.gradle script that you build with

gradle assemble

this outputs a a “build/libs/kotlin.jar” .jar file, but you have no clue how to run your Kotlin code from the command line.

Doing it by hand with “java -cp ” is too much work, and there is a way to do

gradle run

or even

gradle -b /home/myuser/mykotlinapp/build.gradle run

in case you need to run your Kotlin script from a cronjob.

Make sure you have the following inside your build.gradle script in order to make the “run” task available


apply plugin: 'application'

// DO notice the "Kt" suffix on the class name below, if you don't use the Kt generated class you will get errors
mainClassName = 'com.myapp.MyKotlinAppKt'

// optional: add one string per argument you want as the default JVM args
applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1g"]

What if I don’t want to use gradle, and just java

ok… ok…
java -cp $KOTLIN_LIB/kotlin-runtime.jar:build/libs/kotlin.jar: com.myapp.MyKotlinAppKt

[Monero-Development] Installing dependencies on Mac

March 12th 2017.

In order to build Monero on MacOSX with cmake, I had to install the following dependencies via homebrew

brew install libunwind-headers --force
brew link libunwind-headers --force
brew install miniupnpc
brew install ldns
brew link ldns --force
brew install expat
brew link expat --force
brew install doxygen

[CODE] Detecting if a VPN connection is active programmatically (Jan/2017)

This method of programatic detection works as of January 24, 2017 with the latest versions of Express VPN and PIA (Private Internet Access)

On Mac OSX/
This works for Mac OSX 10.2.2.

The trick is to request your ip routing table and examine through which network interface your default traffic is going through.
(To do this programmatically you will have to parse the output with your favorite programming language)

This is how it looks for both ExpressVPN and PIA when the VPN is active:

To request your routing table you can do this on the command line:
netstat -nr

Notice the line starting with “0/1”, it’s going through that tunnel interface. (In Linux it would show 0.0.0.0 instead of 0/1)

VPN ON output in Mac

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
0/1                10.81.10.5         UGSc            5        0   utun1
...

VPN ON output in Linux

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.31.10.5      128.0.0.0       UG        0 0          0 tun0
...

When you turn VPN off this is how it looks:

VPN OFF in Mac

Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.1.1        UGSc           66        0     en0
127                127.0.0.1          UCS             2        4     lo0

VPN OFF in Linux

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.16.245.2    0.0.0.0         UG        0 0          0 eth0

So a quick way to determine if the VPN is on or off in Mac or Linux, is to filter-out what you care for using grep.
If you have any output it’s on, if not it’s off

netstat -nr | egrep "^0" | grep "tun"

(we filter for “tun” and not utun1, as in linux vpn network interfaces start with “tun”)

Parse the output of that command and you will have your VPN status. No output means VPN is disconnected. Some output means the VPN is connected.

On Windows
Do a nestat -nr and look for 128.0.0.1, if you find it, VPN is on.