Skip to content

Instantly share code, notes, and snippets.

@keisuke-umezawa
Last active July 10, 2020 08:44
Show Gist options
  • Save keisuke-umezawa/0bced8f655d6e963084669dea9c14cec to your computer and use it in GitHub Desktop.
Save keisuke-umezawa/0bced8f655d6e963084669dea9c14cec to your computer and use it in GitHub Desktop.
Un-readable code
import datetime
ymdstr = datetime.date.today().strftime("%y-%m-%d")
def get_user_info(): pass
def get_client_data(): pass
def get_customer_record(): pass
import time
# What is the number 86400 for again?
time.sleep(86400)
import re
address = "One Infinite Loop, Cupertino 95014"
city_zip_code_regex = r"^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$"
matches = re.match(city_zip_code_regex, address)
if matches:
print(f"{matches[1]}: {matches[2]}")
seq = ("Austin", "New York", "San Francisco")
for item in seq:
#do_stuff()
#do_some_other_stuff()
# Wait, what's `item` again?
print(item)
class Car:
car_make: str
car_model: str
car_color: str
from typing import List
class Client:
active: bool
def email(client: Client) -> None:
pass
def email_clients(clients: List[Client]) -> None:
"""Filter active clients and send them an email.
"""
for client in clients:
if client.active:
email(client)
class Email:
def handle(self) -> None:
pass
message = Email()
# What is this supposed to do again?
message.handle()
# type: ignore
def parse_better_js_alternative(code: str) -> None:
regexes = [
# ...
]
statements = code.split('\n')
tokens = []
for regex in regexes:
for statement in statements:
pass
ast = []
for token in tokens:
pass
for node in ast:
pass
from typing import Text
from tempfile import gettempdir
from pathlib import Path
def create_file(name: Text, temp: bool) -> None:
if temp:
(Path(gettempdir()) / name).touch()
else:
Path(name).touch()
# type: ignore
# This is a module-level name.
# It"s good practice to define these as immutable values, such as a string.
# However...
fullname = "Ryan McDermott"
def split_into_first_and_last_name() -> None:
# The use of the global keyword here is changing the meaning of the
# the following line. This function is now mutating the module-level
# state and introducing a side-effect!
global fullname
fullname = fullname.split()
split_into_first_and_last_name()
# MyPy will spot the problem, complaining about 'Incompatible types in
# assignment: (expression has type "List[str]", variable has type "str")'
print(fullname) # ["Ryan", "McDermott"]
# OK. It worked the first time, but what will happen if we call the
# function again?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment