Created
February 29, 2020 09:19
-
-
Save Riizade/0db1866f0c78bb92a2b28d7125e79fdf to your computer and use it in GitHub Desktop.
Does some basic analysis of an r/socialism survey on reddit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from typing import Dict, Iterable, List, Tuple | |
import csv | |
def rows_from_csv(filename: Path) -> Iterable[Dict[str, str]]: | |
rows = [] | |
with open(filename, newline='') as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in reader: | |
rows.append(row) | |
return rows | |
# counts the number of rows that contain the given key/value pairs as conditions | |
def count_fields(rows: Iterable[Dict[str, str]], conditions: List[Tuple[str, str]]) -> int: | |
count = 0 | |
for row in rows: | |
valid = True | |
for condition in conditions: | |
key = condition[0] | |
val = condition[1] | |
if row[key].lower() != val.lower(): | |
valid = False | |
break | |
if valid: | |
count += 1 | |
return count | |
# counts the number of rows that contain the given key/value pairs as conditions | |
def count_value_contains(rows: Iterable[Dict[str, str]], conditions: List[Tuple[str, str]]) -> int: | |
count = 0 | |
for row in rows: | |
valid = True | |
for condition in conditions: | |
key = condition[0] | |
val = condition[1] | |
if val.lower() not in row[key].lower(): | |
valid = False | |
if valid: | |
count += 1 | |
return count | |
def percentage_unemployed(rows: Iterable[Dict[str, str]]) -> float: | |
total = count_fields(rows, {}) | |
# count rows that contain the string 'unemployed' | |
unemployed = count_value_contains(rows, [ | |
('Which best describes your employment status?', 'unemployed') | |
]) | |
student_unemployed = count_value_contains(rows, [ | |
('Which best describes your employment status?', 'unemployed'), | |
('Which best describes your employment status?', 'student') | |
]) | |
under_18_unemployed = count_value_contains(rows, [ | |
('Which best describes your employment status?', 'unemployed'), | |
('How old are you?', '17 or younger') | |
]) | |
under_18_and_student_and_unemployed = count_value_contains(rows, [ | |
('Which best describes your employment status?', 'unemployed'), | |
('Which best describes your employment status?', 'student'), | |
('How old are you?', '17 or younger') | |
]) | |
lives_with_family = count_value_contains(rows, [ | |
("What is your living situation?", "stay with family/parents") | |
]) | |
student_lives_with_family = count_value_contains(rows, [ | |
("What is your living situation?", "stay with family/parents"), | |
('Which best describes your employment status?', 'student') | |
]) | |
under_18_lives_with_family = count_value_contains(rows, [ | |
("What is your living situation?", "stay with family/parents"), | |
('How old are you?', '17 or younger') | |
]) | |
under_18_and_student_and_lives_with_family = count_value_contains(rows, [ | |
("What is your living situation?", "stay with family/parents"), | |
('How old are you?', '17 or younger'), | |
('Which best describes your employment status?', 'student') | |
]) | |
non_minor_unemployed = unemployed - under_18_unemployed | |
non_student_unemployed = unemployed - student_unemployed | |
non_minor_non_student_unemployed = unemployed - (under_18_unemployed + student_unemployed - under_18_and_student_and_unemployed) | |
non_minor_lives_with_family = lives_with_family - under_18_lives_with_family | |
non_student_lives_with_family = lives_with_family - student_lives_with_family | |
non_minor_non_student_lives_with_family = lives_with_family - (under_18_lives_with_family + student_lives_with_family - under_18_and_student_and_lives_with_family) | |
doesnt_trust_state = (count_value_contains(rows, [ | |
("Should free speech be a right?", "I do not trust the bourgeois state to define speech or enforce rights") | |
])) | |
yes_free_speech = (count_value_contains(rows, [ | |
("Should free speech be a right?", "yes, always.") | |
])) | |
yes_free_speech_with_restrictions = (count_value_contains(rows, [ | |
("Should free speech be a right?", "Yes, but restricted") | |
])) | |
no_free_speech = (count_fields(rows, [ | |
("Should free speech be a right?", "No") | |
])) | |
total_free_speech_or_with_restrictions = yes_free_speech + yes_free_speech_with_restrictions | |
free_speech_or_unclear = total_free_speech_or_with_restrictions + doesnt_trust_state | |
not_no_free_speech = total - no_free_speech | |
print_percentage("unemployed", unemployed, total) | |
print_percentage("student_unemployed", student_unemployed, total) | |
print_percentage("under_18_unemployed", under_18_unemployed, total) | |
print_percentage("under_18_and_student_and_unemployed", under_18_and_student_and_unemployed, total) | |
print_percentage("lives_with_family", lives_with_family, total) | |
print_percentage("student_lives_with_family", student_lives_with_family, total) | |
print_percentage("under_18_lives_with_family", under_18_lives_with_family, total) | |
print_percentage("under_18_and_student_and_lives_with_family", under_18_and_student_and_lives_with_family, total) | |
print_percentage("non_minor_unemployed", non_minor_unemployed, total) | |
print_percentage("non_student_unemployed", non_student_unemployed, total) | |
print_percentage("non_minor_non_student_unemployed", non_minor_non_student_unemployed, total) | |
print_percentage("non_minor_lives_with_family", non_minor_lives_with_family, total) | |
print_percentage("non_student_lives_with_family", non_student_lives_with_family, total) | |
print_percentage("non_minor_non_student_lives_with_family", non_minor_non_student_lives_with_family, total) | |
print_percentage("doesnt_trust_state", doesnt_trust_state, total) | |
print_percentage("yes_free_speech", yes_free_speech, total) | |
print_percentage("yes_free_speech_with_restrictions", yes_free_speech_with_restrictions, total) | |
print_percentage("no_free_speech", no_free_speech, total) | |
print_percentage("total_free_speech_or_with_restrictions", total_free_speech_or_with_restrictions, total) | |
print_percentage("free_speech_or_unclear", free_speech_or_unclear, total) | |
print_percentage("not_no_free_speech", not_no_free_speech, total) | |
def print_percentage(message: str, count: int, total: int) -> None: | |
percentage = (float(count) / float(total)) * 100.0 | |
justified = str.rjust(f"{message}:", 50) | |
print(f"{justified} {count} ({str(round(percentage, 2))}%)") | |
def main(): | |
rows = rows_from_csv(Path('survey.csv')) | |
percentage_unemployed(rows) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment