Saturday, September 7, 2024

Figuring out if that USB drive is legit

So you just bought USB drives off Temu or some other e-commerce site because it was cheap. You like what you see, 32GB for 99 bucks + shipping, I guess I'll buy five.

But are you really getting a deal?

Mayhaps. But unlike the common folk I don't take such things on face value and  see if we are really getting the real deal. Thankfully, Linux - in this case, PopOS - has the command line tools to check.

So get 1 USB drive to test and we install, F3

$ sudo apt install f3

F3 comes with a command line utility named f3probe, which is used to test USB flash drives for capacity.

But before we run f3probe, we need to figure what block device name the USB drive was given.

$ lsblk

 

As you can see, mine is sdb/sdb1

so

$ sudo f3probe --destructive --time-ops /dev/sdb

Wait for it. BTW, back up if you files in the USB before you do this.

And then be disappointed because your 99 bucks 32GB USB drive is "counterfeit". It's only an 8GB.

Oh well. I hope you didn't buy five. 

 

Ref

  • https://www.linuxbabe.com/command-line/f3-usb-capacity-fake-usb-test-linux

Monday, July 10, 2023

When you need to open 3 (or more) files in Python3

The problem is I needed to open 3 files:

1. CSV source file to read
2. CSV file to write results on after processing the first file
3. Another CSV file that has metadata resulting from processing the 1st CSV that's different from the 2nd

Solution

There are 2 practical ways to do it. There's a 3rd but it has a very specific use case.

As for Python3, you can basically do this using context managers via the 'with' keyword:


with open('a', 'r') as a_file, open('b', 'w') as b_file, open('c', 'wb') as c_file:    
    do_something()

I believe it this also exist in Python 2.7 but you might need to use the contextlib.nested() function

The other approach is when you need to process files sequentially rather than opening all of them at the same time. Useful if you have variable number of files:

for fname in filenames:
    with open(fname) as f:
        # Process f

You can also use this if you would rather keep the results in memory and only write it to the file when done. 

Reference: 

  • https://stackoverflow.com/questions/4617034/how-can-i-open-multiple-files-using-with-open-in-python

Thursday, March 30, 2023

Notes on Solving Simple Vehicle Routing with Python and Google Maps only

Simple Vehicle Routing is a problem related to finding the "optimum" path between a set of locations. The rub here is dealing with real-world addresses and road data (ie. traffic, condition, etc.). Google documentation (or propaganda) will say this is easy but that's slightly misleading.

What I've learned:

 1. Setting up to use the Google Maps APIs (yes APIs; we are going to use most of the it) needs a billing account or credit card. Although, it will not cost you anything because Google is giving you usd$200 month credit. So unless, you exceed 200 bucks worth of usage, it won't cost you anything BUT it's a good idea to set a limit just in case.

2. Jupyter Notebooks is handy to figure out and test stuff. It's just a little finicky getting it to run. For example:

  • I had to rollback ipywidgets to 7.7.2 from the latest 8.0.9 to get gmaps to work
  • I had to change python versions from 3.11 to 3.9 because scipy was being a bitch when installing ortools. Ortools has dependencys to scipy. Luckily I already use pyenv so this was relatively painless.
Jupyter Notebook running with gmaps, OR-Tools


Next step is figuring out how get what I've learned into a REST API for delivery/pickup app. I thinking, use Starlite + Rethinkdb.

Ref: 
  • https://woolpert.com/managing-simple-vrp-with-google-maps-platform/
  • https://developers.google.com/optimization/routing/vrp
  • https://jupyter-gmaps.readthedocs.io/en/latest/install.html
  • https://stackoverflow.com/questions/72371859/attributeerror-module-collections-has-no-attribute-iterable