Two friendly websites for hackers and terminal lovers

Kevin Tewouda
7 min readNov 11, 2022

--

Ultimate cheatsheet and weather information right in your terminal!

An image of of a linux terminal
Photo by Sai Kiran Anagani on Unsplash

In this article, I want to present two websites I discovered recently written by the same author. They are all written in python of course. 😁

Cheat sheet

The first one is cht.sh. It is the ultimate cheat sheet for hackers.

screenshot of curl cht.sh on my terminal

It can be used:

  • To learn or remember how a feature of a programming language works. It supports 56 languages at the moment I’m writing this article.
  • To have an overview of what a programming language can be able to do.
  • To request code samples on StackOverflow by formulating simple requests in any spoken language (French, English, Russian, etc…)
  • As a man page for more than 1000 commands.

Some examples

Get the cheat sheet of a Linux command. Example with crontab.

> curl cht.sh/crontab
cheat:crontab
---
tags: [ job, scheduler, periodic ]
---
# set a shell
SHELL=/bin/bash

# set a PATH
PATH=/usr/bin:/usr/sbin:/usr/local/bin

# incorrect way of seeting PATH
PATH=$PATH:/do/not/do/this

# crontab format
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +- day of week (0 - 7) (where sunday is 0 and 7)
| | | +--- month (1 - 12)
| | +----- day (1 - 31)
| +------- hour (0 - 23)
+--------- minute (0 - 59)

# example entries
# every 15 min
*/15 * * * * /home/user/command.sh
...

Get the list of cheatsheets available for python (you can change python with your favorite programming language).

> curl cht.sh/python/:list
1_Inheritance
1line
2_Multiple_Inheritance
:learn
:list
Advanced
Classes
Comments
Control_Flow_and_Iterables
Functions
Modules
Primitive_Datatypes_and_Operators
Variables_and_Collections
doc
func
hello
lambda
list_comprehension
loops
recursion
rosetta/

To get the cheatsheet for python classes, we can then use:

> curl cht.sh/python/classes
# One of the big advantages of using OOP is **extensibility**.
#
# Let's say you'd written an application that processes lots of data in
# the form of points. Now your customer adds to the specification that
# as well as the x and y coordinate of each point, your app needs to
# know what colour each point is.
#
# If you'd written your code to store each point as a tuple, `(x, y)`,
# you might add the colour as a third value: `(x, y, colour)`. But now
# you have to go through all of your code to find the places where it's
# broken because you changed the data format. If you'd used a class, you
# could simply define a new class that inherits from `Point` and adds
# the necessary capabilities:

class ColouredPoint(Point):
""" Class for points which are coloured, based on Point """

def __init__(self, initX, initY, initCol):
Point.__init__(self, initX, initY)
self.colour = initCol

p = ColouredPoint(3, 3, "green")
q = ColouredPoint(6, 7, "red")
r = Point(8, 4)

print(p.distanceFromPoint(q))
print(p.colour)
print(p.distanceFromPoint(r))

# All your code that worked with the `Point` class will still work with
# the new class, and you can do this even if you didn't write, or can't
# change, the definition of the `Point` class.
#
# [nekomatic] [so/q/36640673] [cc by-sa 3.0]

If we don’t want all the comments, just the code, we can use the suffix \?Qfor our query.

> curl cht.sh/python/classes\?Q

class ColouredPoint(Point):
""" Class for points which are coloured, based on Point """

def __init__(self, initX, initY, initCol):
Point.__init__(self, initX, initY)
self.colour = initCol

p = ColouredPoint(3, 3, "green")
q = ColouredPoint(6, 7, "red")
r = Point(8, 4)

print(p.distanceFromPoint(q))
print(p.colour)
print(p.distanceFromPoint(r))

The special :learncheatsheet gives a condensed tutorial on the programming language.

> curl cht.sh/python/:learn
# Single line comments start with a number symbol.

""" Multiline strings can be written
using three "s, and are often used
as documentation.
"""

####################################################
## 1. Primitive Datatypes and Operators
####################################################

# You have numbers
3 # => 3

# Math is what you would expect
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7.0
...

The special rosetta/opens doors to many other cheatsheets.

> curl cht.sh/python/rosetta/:list
00DESCRIPTION
100-doors
24-game
24-game-Solve
9-billion-names-of-God-the-integer
99-Bottles-of-Beer
A+B
ABC-Problem
AKS-test-for-primes
Abstract-type
...

> curl cht.sh/python/rosetta/Draw-a-clock
import time

def chunks(l, n=5):
return [l[i:i+n] for i in range(0, len(l), n)]

def binary(n, digits=8):
n=int(n)
return '{0:0{1}b}'.format(n, digits)

def secs(n):
n=int(n)
h='x' * n
return "|".join(chunks(h))

def bin_bit(h):
h=h.replace("1","x")
h=h.replace("0"," ")
return "|".join(list(h))


x=str(time.ctime()).split()
y=x[3].split(":")

s=y[-1]
y=map(binary,y[:-1])

print bin_bit(y[0])
print
print bin_bit(y[1])
print
print secs(s)

Obviously, a cheatsheet can’t contain everything. In these cases, you will have to write queries that will be sent to StackOverflow. Let’s look at an example of how to iterate a list in python (ok we have this in cheatsheets but it is just for the example).

# note that we use the "+" sign to concatenate our query
> curl cht.sh/python/iterate+a+list
# Try this,
#
# `x in mylist` is better and more readable than `x in mylist[:]` and
# your `len(x)` should be equal to `3`.

>>> mylist = [[1,2,3],[4,5,6,7],[8,9,10]]
>>> for x in mylist:
... if len(x)==3:
... print x
...
[1, 2, 3]
[8, 9, 10]

# or if you need more pythonic use [list-comprehensions][1]

>>> [x for x in mylist if len(x)==3]
[[1, 2, 3], [8, 9, 10]]
>>>

# [1]: http://docs.python.org/tutorial/datastructures.htmllist-
# comprehensions
#
# [RanRag] [so/q/9138112] [cc by-sa 3.0]

Note on the last line of this query, you have:

Again, if you don’t want the comments, just the snippets, you can use\?Qoption.

When querying on StackOverflow, we are not limited to programming languages, we can also query some frameworks. Let’s look at an example with AWS SDK.

> curl cht.sh/aws/bucket
/*
* I'd recommend using [boto][1]. Then it's a quick [couple of lines of
* python][2]:
*
* <!-- language: python -->
*/

from boto.s3.connection import S3Connection

conn = S3Connection('access-key','secret-access-key')
bucket = conn.get_bucket('bucket')
for key in bucket.list():
print key.name.encode('utf-8')

/* Save this as list.py, open a terminal, and then run: */

$ python list.py > results.txt

/*
* [1]: https://github.com/boto/boto
* [2]: http://code.google.com/p/boto/wiki/GetAllKeys
*
* [Zachary Ozer] [so/q/3337912] [cc by-sa 3.0]
*/

If you are not satisfied with an answer, you can iterate over responses. For that, repeat the question with an additional parameter /1,/2, etc…

> curl cht.sh/aws/bucket/1
/*
* While @Rigerta 's answer will work, I think it's worthy to explain why
* and how you can make your policy work
*
* If you notice, in your policy you're specifying that only that user
* will be able to access all objects in your bucket
*
* ````
* "Resource": "arn:aws:s3:::NAMEOFBUCKET/*"
...

There is a help command if you want to know more about website usage.

> curl cht.sh/:help
Usage:

$ curl cheat.sh/TOPIC show cheat sheet on the TOPIC
$ curl cheat.sh/TOPIC/SUB show cheat sheet on the SUB topic in TOPIC
$ curl cheat.sh/~KEYWORD search cheat sheets for KEYWORD

Options:

?OPTIONS

q quiet mode, don't show github/twitter buttons
T text only, no ANSI sequences
style=STYLE color style

c do not comment text, do not shift code (QUERY+ only)
C do not comment text, shift code (QUERY+ only)
Q code only, don't show text (QUERY+ only)
...

You can also install it locally and there are plugins for many editors. For more information, look at the readme of the project.

Weather

The second one is not useful as the first one but it is pretty cool. Who has never dreamed of having the weather displayed in his terminal? 😂

The website is located on wttr.in. By default when you called it with curl you will get the weather of your city today, tomorrow, and the next day.

Screenshot of the weather on my terminal

You can change the city by adding a path with the name of the city, for example, /paris and also change the display language of the information using the query parameter lang.

Screenshot of Paris weather with the info displayed in french

You can show the moon phase, and take a screenshot of the output among other things. For more information, use the help command.

> curl wttr.in/:help
Usage:

$ curl wttr.in # current location
$ curl wttr.in/muc # weather in the Munich airport

Supported location types:

/paris # city name
/~Eiffel+tower # any location (+ for spaces)
/Москва # Unicode name of any location in any language
/muc # airport code (3 letters)
/@stackoverflow.com # domain name
/94107 # area codes
/-78.46,106.79 # GPS coordinates

Moon phase information:

/moon # Moon phase (add ,+US or ,+France for these cities)
/moon@2016-10-25 # Moon phase for the date (@2016-10-25)

This is all for this article, hope you enjoyed it. Take care of yourself and see you next time! 😁

If you like my article and want to continue learning with me, don’t hesitate to follow me here and subscribe to my newsletter on substack 😉

--

--

Kevin Tewouda
Kevin Tewouda

Written by Kevin Tewouda

Déserteur camerounais résidant désormais en France. Passionné de programmation, sport, de cinéma et mangas. J’écris en français et en anglais dû à mes origines.

No responses yet