Skip to content

Instantly share code, notes, and snippets.

View martin-martin's full-sized avatar
⛰️
hill view from the balcony

Martin Breuss martin-martin

⛰️
hill view from the balcony
View GitHub Profile
@martin-martin
martin-martin / file1.txt
Created May 7, 2020 14:09
Created via API
Demo
import random
num = random.randint(1, 10)
guess = None
while guess != num:
guess = input("guess a number between 1 and 10: ")
guess = int(guess)
@martin-martin
martin-martin / 09_04_files_exceptions.py
Created July 31, 2020 08:46
Reading book files and printing the first character
import os
warpeace=[]
with open ("books/war_and_peace.txt", "r") as store:
warpeace.append(store.readlines())
print(warpeace[0])
with open ("books/crime_and_punishment.txt", "w") as overwrite:
overwrite.write("")
for book in os.listdir(r"books/"):
with open (f"books/{book}", "r") as x:

Let's look at a new scenario. In the following code snippet, you will create a conditional statement that will check your age and let you know how you can participate in elections depending on that factor:

>>> age = 10
>>> if age < 18:
...     print('You can follow the elections on the news')
... elif age < 35:
...     print('You can vote in all elections')
... elif age >= 35:
... print('You can stand for any election')

You can combine logical operators in a conditional statement. Here's an example:

>>> color = 'white'
>>> age = 10
>>> if age <= 14 and (color == 'white' or color == 'green'):
...     print(f'This milk is {age} days old and looks {color}')
... else:
...     print(f'You should better not drink this {age} day old milk...')
... 

If you go to the command line in Mac OS X and enter:

defaults write com.apple.finder AppleShowAllFiles TRUE

All system files that are usually hidden (e.g. all .git folders) will then be shown by default in the Finder. Hide the system files by executing the same command, but set TRUE to FALSE. Finally, enter:

killall Finder
@martin-martin
martin-martin / tag.py
Created February 3, 2021 10:31
tag decorator
def tag(msg,tag):
def wrapping():
return f'<{tag}> {msg} </{tag}>'
return wrapping
string = input('Please, type in a sentence: ')
tg = input('Please, type in an HTML tag you want: ')
a = tag(string, tg)
print(a())
@martin-martin
martin-martin / venv.txt
Created March 5, 2021 11:24
File and folder contents of a Python virtual environment
.
├── bin
│   ├── Activate.ps1
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── dmypy
│   ├── easy_install
│   ├── easy_install-3.8
│   ├── mypy
@martin-martin
martin-martin / rescrape.py
Created March 29, 2021 13:37
Example scraper and scraper test solution
import requests
from bs4 import BeautifulSoup
BASE_URL = "https://codingnomads.github.io/recipes/"
def get_page_content(url):
"""Gets the response from a HTTP call to the URL."""
page = requests.get(url)
return page
import argparse
from pathlib import Path
import re
import sys
my_parser = argparse.ArgumentParser(description='List Markdown headings')
my_parser.add_argument('Path',
metavar='path',
type=str,