Categories
python TILs

secrets.randbelow(x)

TIL (Today I learned) about the python programming language module secrets that can be used to generate random numbers. You can generate random integer below a certain upper bound, say we can generate a number below 200 as follows:

import secrets
print(secrets.randbelow(200))

So how did I come across this? I was looking at how the Hushline (https://hushline.app), a whistleblower platform that provides secure and anonymous tip lines, was handling authentication. They use the secrets module to generate two random numbers for the CAPTCHA math question to verify if a human and not a bot is trying to authenticate. You can look at the code here https://github.com/scidsg/hushline/blob/main/hushline/routes/auth.py#L62. They use the secrets library to generate two random numbers below 10 then add 1 to the numbers that will be displayed to the human to do the math.

Categories
macOS TILs

networkquality

Today I learned that MacOS has an inbuilt command networkquality that can use can you to test your internet speeds without using speedtest.net or fast.com. You type it in the command line and while running it will display the Downlink, Uplink speeds:

$ networkquality
Downlink: 3.958 Mbps, 37 RPM - Uplink: 8.765 Mbps, 37 RPM

You can stop it using Ctrl+C but if you let it run for a few seconds it will end with a summary as shown below

==== SUMMARY ====
Uplink capacity: 7.997 Mbps
Downlink capacity: 5.332 Mbps
Responsiveness: Low (1.071 seconds | 56 RPM)
Idle Latency: 104.417 milliseconds | 576 RPM

I’m thinking this might be a way that maintains privacy without having to visit some ads serving speed testing website.

Categories
macOS TILs

remove exif data on MacOS

I was look at how to remove exif data from photos I had copied to my Mac. I used AppleScript to achieve this:

— Select images to remove EXIF data
set imageFiles to choose file with prompt "Select images to remove EXIF data" of type {"public.image"} with multiple selections allowed
— Loop through each selected image
repeat with imageFile in imageFiles
set imagePath to POSIX path of imageFile
— Create a backup copy (optional)
do shell script "cp " & quoted form of imagePath & " " & quoted form of imagePath & ".backup"
— Remove metadata using sips
do shell script "sips -s formatOptions none " & quoted form of imagePath
end repeat
display dialog "EXIF data removed from selected images." buttons {"OK"} default button "OK"

To use this:

  1. Open Script Editor on your Mac (found in Applications > Utilities).
  2. Paste the script
  3. Hit the run button

This script lets you select one or more images, uses the sips command with the -s formatOptions none option to remove the metadata. It makes a backup copy of the image with .backup extension.

Categories
macOS TILs

Console.app

Today while reading this blog, https://objective-see.org/blog/blog_0x7B.html, by the amazing Patrick Wardle, I learned about the MacOS Console.app

Console app allows you to see reports generated on you mac and iPhone. These reports include: crash, spin, log, diagnostic, mac analytics and system log reports. See screenshot below with the example in the blog post mentioned above. Here there is code and screenshot of the crash report via the Console app:

This is what you see on the terminal when you run the compile and run ./a.out

Console app has a lot of details well summarised plus the full report in json format

You can also see reports from your iPhone. See below screenshot of wifid process on the iPhone

These reports are important when you want to understand what is really going on on your devices. I suggest reading the blog post I linked at the beginning https://objective-see.org/blog/blog_0x7B.html

Categories
data TILs

Collaborative Filtering

This is a technique used in recommendations systems to predict preferences of a user by collecting preferences from many users.

There are 2 types:

  1. user based collaborative filtering – finds users who are similar to the target user based on their past interactions (e.g., ratings or purchases) and recommends items that these similar users liked
  2. item based collaborative filtering – looks at the similarity between items based on users’ past behavior. It recommends items that are similar to what the target user has liked in the past

Here is code. Its scrollable 🙂

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

You can find the Jupyter notebook here https://github.com/bmwasaru/collaborative_filtering/blob/main/collaborative_filtering.ipynb

Leave me a comment, I like feedback 🙂

Categories
git TILs

[rejected] main -> main (fetch first)

So I created a repository on Github and added a README file during the creation and I needed to push my local changes to the GitHub remote repository, I encountered this error because the history on the main branch are different between the local and remote repo

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/<my_username>/<repo_name>.git'

To solve this I tried:

$ git pull origin main --allow-unrelated-histories

Then I encountered this warning or was it an error (its yellow in colour):

hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
I had to run the following:
$ git config pull.rebase false
$ git pull origin main --allow-unrelated-histories
Categories
TILs

kill a process using port_number

I was working on putting together a web server then I realised I couldn’t use port 8888 because of some reason. To solve this I had to find what was using the port

$ lsof -i:8888

COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Python  6556 zero    7u  IPv4  0x8593e686724202a      0t0  TCP localhost:ddi-tcp-1 (LISTEN)
Python  6556 zero   10u  IPv6 0x8d6c72b61b67f0b6      0t0  TCP localhost:ddi-tcp-1 (LISTEN)

lsof will (list of open files) with the process that opened them with this syntax:

lsof -i:[port_number]

Now that I know the process id (PID) I can kill it using the syntax

kill [PID]

In this case its:

$ kill 6556