Lädt...


🔧 CodeSOD: Mailing it In


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: thedailywtf.com

Dan B is working on software which interacts with a bank. We'll get the REAL WTF out of the way right at the top: "The bank has requested that we send them an email containing the total of all the transactions…"

Yes, core financial business functions being handled over email. I imagine some readers are probably thinking about drinking something stronger than coffee at the thought of it. A lot of readers, on the other hand, are already onto something stronger than coffee and going, "Oh, yeah, seen that before. Hell, I'm pretty sure that EDI explicitly supports email as a delivery mechanism."

Let's dig into Dan's more specific challenge. The program he was supporting needed to take an input from their backend- a mainframe style, flat-file with fixed-width fields, and convert it into the format the bank wanted. Their input file tracked the value of each transaction as a zero-padded number in cents. E.g., a ten dollar transaction would be "0001000". The bank wanted a roll-up of all the line items, where the currency was represented as dollars and cents, e.g. "10.00". The email alert that needed to go out simply needed to be a summary of the file processed.

Now, there's an obvious way to do this, even when we're worried about things like floating point rounding errors. There's also a wrong way to do this. Let's look at that one.

def create_message_body_from_file(path):
    def to_dec(s): return Decimal('{}.{}'.format(s[:-2], s[len(s)-2:]))
    with path.open() as f:
        # 28:39 contains the transaction amount
        trans_amount_lines = [line[28:39] for line in f][2:-2]
    decimal_converted_lines = list()
    for line in trans_amount_lines:
        s = line.lstrip('0')
        decimal_converted_lines.append(to_dec(s))
    summed_lines = sum(decimal_converted_lines)
    todays_date = date.today()
    body_format = {
                    'file_name': path.name,
                    'summed_lines': str(summed_lines),
                    'todays_date': str(todays_date)
                  }
    body = '''
The ACH file has been successfully transmitted the file {file_name} with a
total ACH amount of {summed_lines} to the bank on {todays_date}.
'''.format(**body_format)
    return body

We start with the to_dec inner function. Here, we take a string and split it up, then reformat it and then convert it to a decimal type. Because string munging is always the best way to separate dollars and cents.

Then we pull out the field we care about- all the characters from 28:39 in every line. For every line in the file, we make a list of those fields, and then we use [2:-2] to skip the first two and last two lines in the file (headers and footers, I assume?).

Once we've got the lines parsed out, we then strip the leading zeroes, then run it through the to_dec function. Which, it's worth noting, converts the string to a numeric value- thus stripping the leading zeroes without doing string munging.

Finally, we sum it up, build our format, and output our body.

In total, we iterate across every row three times (once to read from the file, once to convert to decimal, once more to sum). For each row, generate a new string once when we strip and once again when we convert to decimal. Then two more, just for fun, when we create our body_format and format the string.

As Dan puts it: "While not incorrect, this is just… convoluted, hard to read, and a memory hog." The only good news is that "we're not processing too many transaction, yet…"

Ah, the best news for any business with bad code: "at least we're not generating a lot of sales". I'm sure this will be fine.

[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!
...

🔧 CodeSOD: Mailing it In


📈 31.12 Punkte
🔧 Programmierung

📰 Alternative zu GMX und Co.: Anbieter stellt Mailing-App mit 20 GB Speicher vor


📈 17.57 Punkte
📰 IT Nachrichten

🐧 Founder of Open Source ESR banned from OSI Mailing list


📈 17.57 Punkte
🐧 Linux Tipps

📰 Event Organizer Suffers Data Breach After Hacker Steals Mailing Lists


📈 17.57 Punkte
📰 IT Security

📰 Ethereum-Mailing-Liste missbraucht


📈 17.57 Punkte
📰 IT Security Nachrichten

🔧 Anzeige | Print-Mailing: Wie du deine Kampagnen richtig pimpst


📈 17.57 Punkte
🔧 Programmierung

💾 PHPTPoint Mailing Server Using File Handling 1.0 Arbitrary File Read


📈 17.57 Punkte
💾 IT Security Tools

📰 Alternative zu GMX und Co.: Anbieter stellt Mailing-App mit 20 GB Speicher vor


📈 17.57 Punkte
📰 IT Nachrichten

📰 FowardingMe Remains One of Canada Leading Packaging and Mailing Services


📈 17.57 Punkte
📰 IT Security Nachrichten

🪟 Check Point sichert Mailing in Office 365 ab


📈 17.57 Punkte
🪟 Windows Tipps

🕵️ Hackers compromised Ethereum mailing list and launched a crypto draining attack


📈 17.57 Punkte
🕵️ Hacking

📰 Phishing Victims are Urged to Send their Mailing Address to Hackers Posing as Walmart


📈 17.57 Punkte
📰 IT Security Nachrichten

🪟 Google stampft Mailing-Lösung Inbox ein


📈 17.57 Punkte
🪟 Windows Tipps

🐧 curl will no longer send "pre-notifications" for security vulnerabilities to the distros mailing list


📈 17.57 Punkte
🐧 Linux Tipps

🕵️ UK Consumer Database for Telemarketing, Email Marketing, Mailing Marketing


📈 17.57 Punkte
🕵️ Hacking

🕵️ Epoch Web Mailing List bis 0.31 Cross Site Scripting [CVE-2016-1211]


📈 17.57 Punkte
🕵️ Sicherheitslücken

📰 Ethereum mailing list breach exposes 35,000 to crypto draining attack


📈 17.57 Punkte
📰 IT Security Nachrichten

🔧 Anzeige | Fakten, News und Tipps rund um das Thema Print-Mailing


📈 17.57 Punkte
🔧 Programmierung

📰 What are some useful mailing lists/feeds to be subscribed to.


📈 17.57 Punkte
📰 IT Security Nachrichten

🐧 Installing Listmonk - Self-hosted Newsletter and Mailing List Manager


📈 17.57 Punkte
🐧 Linux Tipps

📰 NBA Notifying Individuals of Data Breach at Mailing Services Provider


📈 17.57 Punkte
📰 IT Security Nachrichten

🕵️ Mailing List Manager Pro 3.0 admin/users edit sql injection


📈 17.57 Punkte
🕵️ Sicherheitslücken

🪟 Eclipso Mailing-Dienst im Test


📈 17.57 Punkte
🪟 Windows Tipps

🔧 Anzeige | Print-Mailing-Automation: So springen deine Kunden nicht ab


📈 17.57 Punkte
🔧 Programmierung

🐧 public-inbox - an "archives first" approach to mailing lists


📈 17.57 Punkte
🐧 Linux Tipps

🔧 8 Best Mass Mailing Service Providers To Hit Their Inbox in 2025


📈 17.57 Punkte
🔧 Programmierung

🍏 Why Is Mailing Labels Always Print Out Last Name Then First Name?


📈 17.57 Punkte
🍏 iOS / Mac OS

🕵️ Global Shipping and mailing services firm Pitney Bowes hit by ransomware attack


📈 17.57 Punkte
🕵️ Hacking

📰 Event Organizer Suffers Data Breach After Hacker Steals Mailing Lists


📈 17.57 Punkte
📰 IT Security

🔧 Setting Up Listmonk: An Open-Source Newsletter Mailing System


📈 17.57 Punkte
🔧 Programmierung

🔧 Anzeige | Print-Mailing-Automation: So springen deine Kunden nicht ab


📈 17.57 Punkte
🔧 Programmierung

🕵️ E-Mailing Private HTTPS Keys


📈 17.57 Punkte
🕵️ Reverse Engineering

🔧 How to Easily Create Free Ebooks to Use as a Mailing List Subscription Incentive 


📈 17.57 Punkte
🔧 Programmierung

matomo