Skip to content

Instantly share code, notes, and snippets.

Avatar

Erik Madsen beatmadsen

View GitHub Profile
@beatmadsen
beatmadsen / xlswithxmlparser.py
Last active Aug 15, 2020
python 3 parse badly encoded excel sheet
View xlswithxmlparser.py
# Solution inspired by https://stackoverflow.com/a/33504236/2020801
from xml.sax import ContentHandler, parseString
class ExcelHandler(ContentHandler):
def __init__(self):
self.chars = []
self.cells = []
self.rows = []
self.table = []
self.sheet_name = None
@beatmadsen
beatmadsen / event-loop-future-example.scala
Created Jan 23, 2020
An example of an asynchronous service using futures to be run on an event loop
View event-loop-future-example.scala
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class FileApp(val fileCounter: FileCounter) extends Service {
private def countWordsV1(number: Int): Future[Int] = fileCounter.countWordsInFile(s"${number}a")
.flatMap(c1 => fileCounter.countWordsInFile(s"${number}b").map(c2 => c1 + c2))
private def countWordsV2(number: Int): Future[Int] = {
@beatmadsen
beatmadsen / fiber-example.rb
Created Jan 23, 2020
An example of how fibers work
View fiber-example.rb
f = Fiber.new do
a = 42
loop do
Fiber.yield(a)
a += 1
end
end
f.resume # => 42
f.resume # => 43
@beatmadsen
beatmadsen / two_methods_instead_of_boolean.rb
Last active Oct 26, 2018
Two methods instead of boolean parameter
View two_methods_instead_of_boolean.rb
# calling code:
age = user.belongs_to_system_a? ?
find_age_from_system_a(name: user.name) :
find_age_from_system_b(name: user.name)
# methods
def find_age_from_system_a(name:)
payload = build_payload(name)
response = call_system_a(payload)
@beatmadsen
beatmadsen / bad_reuse_with_boolean.rb
Last active Oct 26, 2018
Bad reuse with boolean parameter
View bad_reuse_with_boolean.rb
# calling code:
age = find_age(name: user.name, divert: !user.belongs_to_system_a?)
# methods
def find_age(name:, divert:)
payload = { name: name, size: 42 }
response = divert ? call_system_b(payload) : call_system_a(payload)
You can’t perform that action at this time.