Password-Manager with Salt and Pepper (commented version will follow soon)

from hashlib import pbkdf2_hmac

lower_case = list("abcdefghijklmnopqrstuvwxyz")
upper_case = list("ABCDEFGHJKLMNOPQRTUVWXYZ")
numbers = list("0123456789")
special_char = list("!§$%&/()=?[],.-")
password_chars = lower_case + upper_case + numbers + special_char
salt = "pepper"

def convert_bytes_to_password(hashed_bytes, length):
    number = int.from_bytes(hashed_bytes, byteorder="big")
    password = ""
    while number > 0 and len(password) < length:
        password = password + password_chars[number % len(password_chars)]
        number = number // len(password_chars)
    return password

master_password = input("Master Password: ")
domain = input("Website: ")

while len(domain) < 1:
    print("Enter a Website you want to generate the password for: ")
    domain = input("Website: ")

hash_string = domain + master_password

hashed_bytes = pbkdf2_hmac("sha512",
                            hash_string.encode("utf-8"),
                            salt.encode("utf-8"),
                            4096)

print("Passwort: " + convert_bytes_to_password(hashed_bytes, 10))

Day 69: Make Good Looking Tables With PrettyTable (Module)

 

Scripts running in the shell are pretty cool. But sometime you want to display tables that have no real format.

 

The module PrettyTable generates some – well pretty – tables with just a couple of lines.

 

The script takes a dictionary with the sizes of all planets in the solar system.

 

It exports it in a PrettyTable in descending order.

 

# PrettyTable module
# Create handsome tables in your shell
from prettytable import PrettyTable

planets = {"Mercury" : "4879",
        "Venus" : "12104",
        "Earth" : "12756",
        "Mars" : "6779",
        "Jupiter" : " 142984",
        "Saturn" : "120536",
        "Uranus" : "50724",
        "Neptune" : "49244"
        }

# create the table with two colums
size_table = PrettyTable(["Planet", "Diameter (km)"])

# go over every planet in dict and add a row for each
for item in planets:
    size_table.add_row([item, planets[str(item)]])

# sort planets from big to small
size_table.sort = True

print(size_table)

The output looks like this:

 

 

Day 58: Using Progress Bars In The Shell (Module)

 

The module ProgressBar generates timed progress bars in the terminal while you execute a program.

 

A progress bar is seldomly really necessary but sometimes it is just neat to display some progress for the user

 

You can find the documentation here

 

An easy example:

 

from time import sleep                          # to use the timer
from progressbar import ProgressBar             # progressbar module


print("Multiplying 987.000 * 1.210.100")        

num = 987000 * 1210100


bar = ProgressBar()                         
for i in bar(range(100)):
    sleep(0.01)
    bar.update(i)

print("Result: " + str(num))

And this is what it looks like in the terminal:

 

Day 57: Get Global Weather Data with OpenWeatherMap (Module)

 

OpenWeatherMap provides free weather data for every city worldwide.

 

#Takes the name of a city and delivers
# weather data via OpenWeatherMap

import pyowm               # OpenWeatherMap-module for python

owm = pyowm.OWM('a22ec5378101af24d94fb76fexxxxx')  # You MUST provide your own API key - this one will not work

city = input("Weather forecast: (Enter City): ")


# Search for current weather in city via OWM
observation = owm.weather_at_place(city)
w = observation.get_weather()

print(city.capitalize())
# extract and print the various data
get_str = str(w)
stat = get_str.find('status') + 7
print("Condition: " + get_str[stat:-1])

tempe = w.get_temperature('celsius')

print("Celsius: " + str(tempe['temp']))

print("Humidity: " + str(w.get_humidity()))

speed = w.get_wind()

print("Wind: " + str(speed['speed']) + " kph")