Parts of this were shot on my floor of my building.
To find out more, visit https://developer.capitalone.com/community/?io=true
Rants on the daily grind of building software. This has been moved to https://slott56.github.io. Fix your bookmarks.
![]() | Data Science Renee (@BecomingDataSci) |
|
Thread. Coding is inherently frustrating. Expect that. But with puzzle-solving healthy attitude, keep going. Don't give up on yourself! twitter.com/IsabellaGhemen…
| |
"Do you ever get frustrated when coding? Or is it all inspirational?"
"go into obsessive mode until I fix the problem"This is fascinating. I've been coding for -- well -- a looooong time. I no longer recall a time when I struggled. These quotes provide some insight into the barrier that some people find between them and a finished project.
case when gender = 'M' then 1 else 0 end as gender_m, case when gender = 'F' then 1 else 0 end as gender_f,...
from pathlib import Path
import csv
source_path = Path("some_file.csv")
target_path = Path(source_path.stem + "_1").with_suffix('.csv')
def transform(row):
return row
with source_path.open() as source_file:
with target_path.open('w', newline='') as target_file:
reader = csv.DictReader(source_file)
columns = reader.fieldnames + ['gender_m', 'gender_f']
writer = csv.DictWriter(target_file, columns)
writer.writeheader()
for row in reader:
new_row = transform(row)
writer.writerow(new_row)
The goal is to be able put some meaningful transformation processing in place of the build new_row comment.def transform(row):
row['gender_m'] = 1 if row['gender'] == 'M' else 0
row['gender_f'] = 1 if row['gender'] == 'F' else 0
return row
def transform(row):
if row['gender'] == 'M':
row['gender_m'] = 1
else:
row['gender_m'] = 0
row['gender_f'] = 1 if row['gender'] == 'F' else 0
return row
for row in reader: new_row = transform(row) writer.writerow(new_row)
writer.writerows(transform(row) for row in reader)
writer.writerows(map(transform, reader))
class AlgorithmOne(DoAThing):
"""One way to do it."""
def doTheThing(self, arg1, arg2):
# Check some constraints, maybe...
if arg1 < arg2:
return Fraction(arg1, arg2)
else:
raise DoAThingError("Outside AlgorithmOne Constraints")
a2 = AlgorithmTwo() a1 = AlgorithmOne(a2) coc = UseCache(a1)
def builder(*classes):
previous = None
for class_ in classes:
next = class_(previous)
previous = next
return previous
"""Chain of command."""
from fractions import Fraction
import pickle
class DoAThingError(Exception):
pass
class DoAThing:
"""Abstract superclass."""
def __init__(self, fall_back=None):
self.fall_back = fall_back
def theThing(self, arg1, arg2):
try:
return self.doTheThing(arg1, arg2)
except DoAThingError:
if self.fall_back:
return self.fall_back.theThing(arg1, arg2)
def doTheThing(self, arg1, arg2):
raise DoAThingError("Not Implemented")
class UseCache(DoAThing):
"""Is the answer in cache? Cache is dynamic and grows quickly.
There's no LRU.
"""
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
self.cache = {}
def load(self, openFile):
self.cache = pickle.load(openFile)
def dump(self, openFile):
pickle.dump(self.cache, openFile)
def theThing(self, arg1, arg2):
if (arg1, arg2) not in self.cache:
self.cache[arg1, arg2] = self.fall_back.theThing(arg1, arg2)
return self.cache[arg1, arg2]
class AlgorithmOne(DoAThing):
"""One way to do it."""
def doTheThing(self, arg1, arg2):
# Check some constraints, maybe...
if arg1 < arg2:
return Fraction(arg1, arg2)
else:
raise DoAThingError("Outside AlgorithmOne Constraints")
class AlgorithmTwo(DoAThing):
"""Another way to do it."""
def doTheThing(self, arg1, arg2):
return arg1/arg2
a2 = AlgorithmTwo()
a1 = AlgorithmOne(a2)
coc = UseCache(a1)
print(coc.theThing(1,2))
print(coc.theThing(2,1))
print(coc.cache)