Regex 🤖, Django Architeture ⚙️, Lessons learned 📝, using exceljs 📊

By Prajwal Haniya

Techletter #82 | July 20, 2024

Pattern matching with Regular expression

Regular expressions allow you to specify a pattern of text to search for. Tech writer Cory Doctorow argues that even before teaching programming, we should be teaching regular expressions:

“Knowing [regular expressions] can mean the difference between solving a problem in 3 steps and solving it in 3,000 steps. When you’re a nerd, you forget that the problems you solve with a couple keystrokes can take other people days of tedious, error-prone work to slog through.”

Regular expressions, called regexes for short, are descriptions of a pattern of text. For example, a \d in a regex stands for a digit character— that is, any single numeral 0 to 9.

Regular expressions can be much more sophisticated. For example, adding a 3 in curly brackets ({3}) after a pattern is like saying, “Match this pattern three times.”

All the regex functions in Python are in the re module

So basically you can follow the below steps for using regexes in Python

  1. Import the regex module with import re.
  2. Create a Regex object with the re.compile() function. (Remember to use a raw string.)
  3. Pass the string you want to search into the Regex object’s search() method. This returns a Match object.
  4. Call the Match object’s group() method to return a string of the actual matched text.
import re

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('My number is 415-555-4242.')
print('Phone number found: ' + mo.group())

Grouping with parenthesis

import re

phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('My number is 415-555-4242.')

print(mo.group(1)) # 415
print(mo.group(2)) # 555-4242
print(mo.group(0)) # 415-555-4242
print(mo.groups()) # ('415', '555-4242')

Django Architecture

There are four main components to consider: URLs, views, models, and templates.

Now, let’s see the interaction with the Django

Below is the first step when you interact with a Django backend app

WEB BROWSER → | DJANGO | → URL Dispatcher → View → Model → Database

Similarly, the response return flow is as below

Database → Model → View + Template → | Django | → Web Browser

Django’s approach is sometimes called Model-View-Template (MVT) but is more accurately a 4-part pattern incorporating URL configuration, Model-View-Template-URL (MVTU).


Lessons learned in 35 years of making software

The software we build is valuable. It builds the value of the company. When you hold it until it’s perfect, or everything you think it needs to be, you are holding back on building the company’s value. Find the fastest, shortest path to getting the smallest increment of the thing that will work into the customer’s hands. You can keep making it better from there.

Never be invisible. Showcase your work.

Chase adventures and not salaries.

The software we are building right now will one day be decommissioned and not be used anymore, probably before your career is over.

Original link


How to use exceljs?

const Excel = require('exceljs');

const workbook = new Excel.Workbook();
const worksheet = workbook.addWorksheet("My Sheet");

worksheet.columns = [
  { header: 'Id', key: 'id', width: 10 },
  { header: 'Name', key: 'name', width: 32 },
  { header: 'D.O.B.', key: 'dob', width: 15 }
];

function writeToSheet() {
   worksheet.addRow({id: 1, name: 'John Doe', dob: new Date(1970, 1, 1)});
   worksheet.addRow({id: 2, name: 'Jane Doe', dob: new Date(1965, 1, 7)});

   await workbook.xlsx.writeFile('output.xlsx');
}

writeToSheet();