Handy Snippets | Tech
by Saumya Shah
Python general
- Error catching
Pythonassert <expression> , "<message>" # OR if not <expression>: raise AssertionError("<message>") # some exception types that can be used: # AssertionError, IndexError, RunTimeError, TypeError, ValueError, ZeroDivisionErrore # OR use try except try: print("Hello") except: print("Something went wrong") # if error in try else: print("Nothing went wrong") # if no error in try # try except with finally try: print(x) except: print("Something went wrong") finally: print("The 'try except' is finished") # finally block is executed regardless of errors or no errors in try
Linux and Git Commands
- Sometimes you may want to keep a watch on something:
watch -n <number_of_seconds> <command>
- Reduce brightness below minimum (replace 2070)
cat /sys/class/backlight/intel_backlight/brightness sudo su -c "echo 2070 >/sys/class/backlight/intel_backlight/brightness"
- Mute an application :
pacmd list-sink-inputs pacmd set-sink-input-mute <index> true(/false)
- Setting upstream: sometimes you may want a local branch to track another remote branch
# check which branch tracks what git branch -vv # set upstream git branch -u <remote>/<branch> # or If you just wish to push once to another remote branch git push <remote> <local_branch>:<remote_name>
- sshfs: One of the most handy commands when working on remote server
sshfs USER@IP:/$PATH $LOCAL_PATH
- scp: copy recursively all files and folders
scp -rp sourcedirectory user@dest:/path # -r for recursive and -p to preserve modification times and modes
- Screen: another handy function when working remotely, to prevent creating multiple sessions locally
# make screen screen -S myscreen # list all screen -ls # attach to a session screen -r <name> # detach screen -d <name> # detach C-a d # kill C-a k
Miscellaneous
- Auto reload modules in Jupyter
# add to beginning of notebook %load_ext autoreload %autoreload 2
- Jekyll to build site locally
sudo apt-get install ruby-full build-essential zlib1g-dev gem install jekyll bundler bundle install bundle exec jekyll serve --watch --trace
- Run Jupyter Nodebook on remote server via ssh
# open it on remote sasha@remote $ jupyter notebook --no-browser --port=8887 # Tunnel through ssh on local ssh -N -L localhost:8888:localhost:8887 sasha@remote
- Download all google search images with filters: Fatkun batch extension
https://chrome.google.com/webstore/detail/fatkun-batch-download-ima/nnjjahlikiabnchcpehcpkdeckfgnohf?hl=en
- ROS Basics:
- https://tarangshah.com/blog/2017-04-01/ros-basics-1-nodes-topics-services-actions/
- https://www.youtube.com/watch?v=DBFYZRMLr70&list=PLK0b4e05LnzZWg_7QrIQWyvSPX2WN2ncc
References:
- https://arnavdhamija.com/computer-tips/
… to be continued