Trey Hunner / Python & Django Trainer / @treyhunner
βReadability is the ease with which a reader can understand a written text.β —Wikipedia
employee_hours = (schedule.earliest_hour for employee in
self.public_employees for schedule in
employee.schedules)
return min(h for h in employee_hours if h is not None)
employee_hours = (
schedule.earliest_hour
for employee in self.public_employees
for schedule in employee.schedules
)
return min(
hour
for hour in employee_hours
if hour is not None
)
def is_valid_uuid(uuid):
"""Return True if given variable is a valid UUID."""
return bool(re.search(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}'
r'-[0-9a-f]{4}-[0-9a-f]{12}$',
uuid, re.IGNORECASE))
def is_valid_uuid(uuid):
"""Return True if given variable is a valid UUID."""
UUID_RE = re.compile(r'''
^
[0-9a-f] {8} # 8 hex digits
-
[0-9a-f] {4} # 4 hex digits
-
[0-9a-f] {4} # 4 hex digits
-
[0-9a-f] {4} # 4 hex digits
-
[0-9a-f] {12} # 12 hex digits
$
''')
return bool(UUID_RE.search(uuid, re.IGNORECASE | re.VERBOSE))
default_appointment = models.ForeignKey(
'AppointmentType', null=True, on_delete=models.SET_NULL,
related_name='+')
default_appointment = models.ForeignKey('AppointmentType',
null=True,
on_delete=models.SET_NULL,
related_name='+')
default_appointment = models.ForeignKey(
'AppointmentType',
null=True,
on_delete=models.SET_NULL,
related_name='+')
default_appointment = models.ForeignKey(
othermodel='AppointmentType', null=True,
on_delete=models.SET_NULL, related_name='+')
default_appointment = models.ForeignKey(othermodel='AppointmentType',
null=True,
on_delete=models.SET_NULL,
related_name='+')
default_appointment = models.ForeignKey(
othermodel='AppointmentType',
null=True,
on_delete=models.SET_NULL,
related_name='+')
default_appointment = models.ForeignKey(
othermodel='AppointmentType',
null=True,
on_delete=models.SET_NULL,
related_name='+')
default_appointment = models.ForeignKey(
othermodel='AppointmentType',
null=True,
on_delete=models.SET_NULL,
related_name='+'
)
default_appointment = models.ForeignKey(
othermodel='AppointmentType',
null=True,
on_delete=models.SET_NULL,
related_name='+',
)
sc = {}
for i in csv_data:
sc[i[0]] = i[1]
state_capitals = {}
for i in capitals_csv_data:
state_capitals[i[0]] = i[1]
state_capitals = {}
for s, c, *_ in capitals_csv_data:
state_capitals[s] = c
state_capitals = {}
for state, capital, *_ in capitals_csv_data:
state_capitals[state] = capital
def detect_anagrams(word, candidates):
anagrams = []
for candidate in candidates:
if (sorted(word.upper()) == sorted(candidate.upper())
and word.upper() != candidate.upper()):
anagrams.append(candidate)
def detect_anagrams(word, candidates):
anagrams = []
for candidate in candidates:
if is_anagram(word, candidate):
anagrams.append(candidate)
def is_anagram(word1, word2):
return (sorted(word1.upper()) == sorted(word2.upper())
and word1.upper() != word2.upper())
def is_anagram(word1, word2):
word1, word2 = word1.upper(), word2.upper()
return sorted(word1) == sorted(word2) and word1 != word2
def is_anagram(word1, word2):
word1, word2 = word1.upper(), word2.upper()
return (
sorted(word1) == sorted(word2) # words have same letters
and word1 != word2 # words are not the same
)
def is_anagram(word1, word2):
word1, word2 = word1.upper(), word2.upper()
are_different_words = (word1 != word2)
have_same_letters = (sorted(word1) == sorted(word2))
return have_same_letters and are_different_words
def detect_anagrams(word, candidates):
anagrams = []
for candidate in candidates:
if is_anagram(word, candidate):
anagrams.append(candidate)
def is_anagram(word1, word2):
word1, word2 = word1.upper(), word2.upper()
are_different_words = word1 != word2
have_same_letters = sorted(word1) == sorted(word2)
return have_same_letters and are_different_words
def update_appointment_types(self):
"""Delete/make appt. types and set default appt. type"""
self.appt_types.exclude(specialty=self.specialty).delete()
new_types = self.specialty.appt_types.exclude(agent=self)
self.appt_types.bulk_create(
AppointmentType(agent=self, appointment_type=type_)
for type_ in new_types
)
old_default_id = self.default_appt_id
self.default_appt_type = self.specialty.default_appt_type
if self.default_appt_type.id != old_default_id:
self.save(update_fields=['default_appt_type'])
def update_appointment_types(self):
"""Delete/make appt. types and set default appt. type"""
self.appt_types.exclude(specialty=self.specialty).delete()
new_types = self.specialty.appt_types.exclude(agent=self)
self.appt_types.bulk_create(
AppointmentType(agent=self, appointment_type=type_)
for type_ in new_types
)
old_default_id = self.default_appt_id
self.default_appt_type = self.specialty.default_appt_type
if self.default_appt_type.id != old_default_id:
self.save(update_fields=['default_appt_type'])
def update_appointment_types(self):
"""Delete/make appt. types and set default appt. type"""
# Delete appointment types for specialty besides current one
self.appt_types.exclude(specialty=self.specialty).delete()
# Create new appointment types based on specialty (if needed)
new_types = self.specialty.appt_types.exclude(agent=self)
self.appt_types.bulk_create(
AppointmentType(agent=self, appointment_type=type_)
for type_ in new_types
)
# Set default appointment type based on specialty
old_default_id = self.default_appt_id
self.default_appt_type = self.specialty.default_appt_type
if self.default_appt_type.id != old_default_id:
self.save(update_fields=['default_appt_type'])
def update_appointment_types(self):
"""Delete/make appt. types and set default appt. type"""
# Delete appointment types for specialty besides current one
self.appt_types.exclude(specialty=self.specialty).delete()
# Create new appointment types based on specialty (if needed)
new_types = self.specialty.appt_types.exclude(agent=self)
self.appt_types.bulk_create(
AppointmentType(agent=self, appointment_type=type_)
for type_ in new_types
)
# Set default appointment type based on specialty
old_default_id = self.default_appt_id
self.default_appt_type = self.specialty.default_appt_type
if self.default_appt_type.id != old_default_id:
self.save(update_fields=['default_appt_type'])
def update_appointment_types(self):
"""Delete/make appt. types and set default appt. type"""
# Delete appointment types for specialty besides current one
self.appt_types.exclude(specialty=self.specialty).delete()
# Create new appointment types based on specialty (if needed)
new_types = self.specialty.appt_types.exclude(agent=self)
self.appt_types.bulk_create(
AppointmentType(agent=self, appointment_type=type_)
for type_ in new_types
)
# Set default appointment type based on specialty
old_default_id = self.default_appt_id
self.default_appt_type = self.specialty.default_appt_type
if self.default_appt_type.id != old_default_id:
self.save(update_fields=['default_appt_type'])
def _delete_stale_appointment_types(self):
"""Delete appointment types for specialties besides ours"""
self.appt_types.exclude(specialty=self.specialty).delete()
def _create_new_appointment_types(self):
"""Create new appointment types based on specialty if needed"""
new_types = self.specialty.appt_types.exclude(agent=self)
self.appt_types.bulk_create(
AppointmentType(agent=self, appointment_type=type_)
for type_ in new_types
)
def _update_default_appointment_type(self):
"""Set default appointment type based on specialty"""
old_default_id = self.default_appt_id
self.default_appt_type = self.specialty.default_appt_type
if self.default_appt_type.id != old_default_id:
self.save(update_fields=['default_appt_type'])
def update_appointment_types(self):
"""Delete/make appt. types and set default appt. type"""
self._delete_stale_appointment_types()
self._create_new_appointment_types()
self._update_default_appointment_type()
db = DBConnection("mydb")
try:
records = db.query_all()
finally:
db.close()
class connect:
def __init__(self, path):
self.connection = DBConnection(path)
def __enter__(self):
return self.connection
def __exit__(self):
self.close()
with connect("mydb") as db:
db.query_all()
from contextlib import closing
with closing(DBConnection("mydb")) as db:
db.query_all()
employees = []
for calendar, availabilities in calendar_availabilities:
if availabilities:
employees.append(calendar.employee)
employees = []
for calendar, availabilities in calendar_availabilities:
if availabilities:
employees.append(calendar.employee)
employees = [
calendar.employee
for (calendar, availabilities) in
calendar_availabilities
if availabilities
]
class ShoppingCart:
def contains(self, product):
"""Return True if cart contains the product."""
def add(self, product, quantity):
"""Add the quantity of a product to the cart."""
def remove(self, product):
"""Completely remove a product from the cart."""
def set(self, product, quantity):
"""Set the quantity of a product in the cart."""
@property
def count(self):
"""Return product count in cart, ignoring quantities."""
@property
def is_empty(self):
"""Return True if cart is empty."""
Before | After |
---|---|
cart.contains(item) | item in cart |
cart.set(item, q) | cart[item] = q |
cart.add(item, q) | cart[item] += q |
cart.remove(item) | del cart[item] |
cart.count | len(cart) |
cart.is_empty | not cart |
class ShoppingCart:
def __contains__(self, product):
"""Return True if cart contains the product."""
def __setitem__(self, product, quantity):
"""Set the quantity of a product in the cart."""
def __delitem__(self, product):
"""Completely remove a product from the cart."""
def __len__(self):
"""Return product count in cart, ignoring quantities."""
def __bool__(self):
"""Return True if cart is non-empty."""
collections.UserList
: make custom listcollections.UserDict
: make custom dictionarycollections.UserString
: make custom string
def get_connection(host, username, password):
"""Initialize IMAP server and login"""
server = IMAP4_SSL(host)
server.login(username, password)
server.select("inbox")
return server
def close_connection(server):
server.close()
server.logout()
def get_message_uids(server):
"""Return unique identifiers for each message"""
return server.uid("search", None, "ALL")[1][0].split()
def get_message(server, uid):
"""Get email message identified by given UID"""
result, data = server.uid("fetch", uid, "(RFC822)")
(_, message_text), _ = data
message = Parser().parsestr(message_text)
return message
def get_connection(host, username, password):
"""Initialize IMAP server and login"""
server = IMAP4_SSL(host)
server.login(username, password)
server.select("inbox")
return server
def close_connection(server):
server.close()
server.logout()
def get_message_uids(server):
"""Return unique identifiers for each message"""
return server.uid("search", None, "ALL")[1][0].split()
def get_message(server, uid):
"""Get email message identified by given UID"""
result, data = server.uid("fetch", uid, "(RFC822)")
(_, message_text), _ = data
message = Parser().parsestr(message_text)
return message
class IMAPChecker:
def __init__(self, host):
"""Initialize IMAP email server with given host"""
self.server = IMAP4_SSL(host)
def authenticate(self, username, password):
"""Authenticate with email server"""
self.server.login(username, password)
self.server.select("inbox")
def quit(self):
self.server.close()
self.server.logout()
def get_message_uids(self):
"""Return unique identifiers for each message"""
return self.server.uid("search", None, "ALL")[1][0].split()
def get_message(self, uid):
"""Get email message identified by given UID"""
result, data = self.server.uid("fetch", uid, "(RFC822)")
(_, message_text), _ = data
message = Parser().parsestr(message_text)
return message
@treyhunner
http://WeeklyPython.Chat
Python & Django Trainer for hire