Saturday, September 18, 2021

Python: Easy help print

As a follow up of the "Bash: Easy help print", lets do the same thing. So I will just copy the whole text and replace what needs replacing...

Ok, lets create a single line of python that will handle the -h option and print something.

First, what to print? I usually add some comments on the start of the file, so lets print that. Example script:

#!/bin/env python3
#
#  This is the doc of the script
#

the line that we will add

code of script

Lets go step by step. We need to print the file it self:

h = open(args[0], 'r').read()
print(h)

Skip the first line (lets skip the read):

print(h[h.index('\n')+1:])	

Go until you find the first empty line:

print(h[h.index('\n')+1:h.index('\n\n')]

Remove the # fro the start of the line:

.replace('#','')

Finally, we check if the -h is the first argument, print the thing and exit:

if '-h' in args: h = open(args[0], 'r').read(); print(h[h.index('\n')+1:h.index('\n\n')].replace('#','')); exit()



done_

Wednesday, June 16, 2021

Javascript: Self call re-call interval set

 So we have the requestAnimationFrame, you pass a function and it called after a very specific while (irrelevant to the following).

Now lets see the basic structure:

function loop() {
    update()
    requestAnimationFrame(loop)
}
loop()

Ok now lets wrap the function and call it without calling it by name:

(function loop() {
    update()
    requestAnimationFrame(loop)
})()

Is there any way to remove the name of function and still pass it? There is a magical object called arguments which appears out of nothing every time a function is executed. And the member that we need is arguments.callee which is self reference to the function being executed. So we get rid of the name:

(function () {
    update()
    requestAnimationFrame(arguments.callee)
})()

And of course to maximize the coolness we put in a single line:

(function() { update(); requestAnimationFrame(arguments.callee) })()

Ok, this very cool, but is it worth it? Let's analyze what this code succeeds to do:

1. Confuses the next reader of the code.
2. Confuses also the 2nd reader.
3. Confuses also the 3rd reader.
...
N. Confuses also the N-th reader.

So you have to choose what is cooler: confusing no people or confusing up to infinite people? Comment below, like and subscribe.



done_



Techniques: Build-time pre-package capture

 Usually when things/files are packaged from simple files to single monstrosity, you no longer have the ability to ls/dir to lookup what things/files exist.

An example is a Java jar, where to list for example all the packaged files you either cannot or you need to open a zip stream and figure out yourself.

Also in Java and other languages with limited introspection you cannot for example list all the classes that implement the interface X.

So:

Create a script than generates all the information you want.

Put that script in the build/compile process.

Include the file in the project.

Write some code to parse the file.


Example of listing all the inheritance properties by finding all the extends keyword:

grep -hr extends | sed -E 's/.*class (\w+) extends (\w+) .*/\2 -> \1/'
Parent -> Child1
Parent -> Child2
...

No need to say again and again "This is not supported, I cannot do it", supported yourself, you are a damn programmer! 



done_ 

Friday, June 11, 2021

Html: Input tag drop-down list

The classic way to have a drop-down list in html is the select tag.

However there are two main problems/inconvenient. The select tag will only allow to select one item and you cannot type for a not included option or just to filter the list options. Also sometimes just have all the input fields under the same tag name <input> is just niter.

An other way is the <datalist>, that for some reason I learned about it lately (sad).

The classic example:

<input list="hashes">
<datalist id="hashes">
  <option value="sha1">
  <option value="sha256">
  <option value="sha512">
</datalist>


done_ 

Friday, March 5, 2021

Email: Why are you tracking me in "secret"

So Aege@n (airline) has been spamming me with emails after a took a single flight with them.

One day they actually sent an email with the subject that made me believe that it was interested in it, so I opened it, turns out it was a click bait, but:

I had 22 (unread) emails from them in a span of one year and after I opened that click bait I got 7 in the following month. So they knew, I opened it ... But how did they know?

There was no "Return receipt" request, so they have done something sneaky.

Lets look at the email.. ok, its just html

Lets load the images.. ok, some images from http://static.cdn... were loaded.

Wait what is that? All images are from a cdn except one:

https://news.aegeanair.com/pub/as?_ri_=X0Gzc2X%3DAQpglLjHJlTQGmafAoKGsenIJ7EBMdskH5TyfUpyNwKkXSeDbmwPrIMgdr9IPNDpYNzfLdOGMwzcGVXHkMX%3Dw&_ei_=EolaGGF4SNMvxFF7KucKuWPnzljXYMJZUMmgkkIvbZe171NV_sKNU-8XjLVsBtAWB1ASqm8piDcfWdATMJx3XCt5nps.

Lets open that image... ok its a 1x1 gif image.

A 1x1 pixels gif image.

A 1 pixel by 1 pixel gif image.

A 1 pixel width by 1 pixel height gif image.

A 1 pixel width by 1 pixel height graphics interchange format image.

So they masked a http request to there sever with a unique identifier as an image download request.

Is this ok? Hello?

...

I guess I manually clicked "Load all images" (even 1x1 gifs), so it must be ok... 

Wait, so this is why the there is the load all images button... cool.



done_

Wednesday, February 24, 2021

Python: Easy line plots

Need to just plot a file with numbers? Stop using matl@b or excl.

All we need is a couple lines of Python code:

import matplotlib.pyplot as p

p.plot(values)
p.show()

And for the values (if you have the number with new-line separating them) with either a file as an argument of just the stdin couple more lines:

import sys

stream = sys.stdin
if len(sys.argv) > 1:
    stream = open(sys.argv[1], 'r')

values = [float(line) for line in stream]



done_

Sunday, February 7, 2021

Bash: Easy help print

Ok, lets create a single line of bash that will handle the -h option and print something.

First, what to print? I usually add some comments on the start of the file, so lets print that. Example script:

#!/bin/bash
#
#  This is the doc of the script
#

the line that we will add

code of script

Lets go step by step. We need to print the file it self:

cat "$0"

Skip the first line (lets switch to sed):

sed -n '2,$ p' "$0"

Go until you find the first empty line:

sed -n '2,/^$/ p' "$0"

Remove the # fro the start of the line:

sed '2,/^$/ s/#// p' "$0"

Finally, we check if the -h is the first argument, print the thing and exit:

[ "$1" = '-h' ] && sed -n '2,/^$/ s/#// p' "$0" && exit



done_

Thursday, February 4, 2021

Android: Send preset SMS with with one tap

For all of you that need to sent a simple preset SMS without copy pasting all the time (covid-era).

For some reason on the play store most of the app that provide this kind of operation instead of sending the SMS they just open the messages app...

Ok, lets do it:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phone_number", null, "message", null, null);

And because this android we need to haggle with the permissions:

<uses-permission android:name="android.permission.SEND_SMS" />
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) 
		!= PackageManager.PERMISSION_GRANTED) {
	ActivityCompat.requestPermissions(this, new String[] {
    		Manifest.permission.SEND_SMS}, 0);
}

And we are done, that's it.



done_