Python One Line Script

Hey, read this code:

1from typing import Callable, TypedDict, Any
2from datetime import datetime as Datetime
3
4Job = TypedDict("Job", {
5    "year": str, 
6    "month": str, 
7    "date": str, 
8    "hour": str, 
9    "minute": str, 
10    "weekday": str
11})
12
13get_dates: Callable[[Job], Any] = lambda job: out if ((_ := True) and (now := Datetime.now()) and (day := now.day) and (month := now.month) and (year := now.year) and (hour := now.hour) and (minute := now.minute) and (weekday := now.weekday()) and (job_year := year) and (job_month := job["month"] or month) and (job_date := job["date"] or day) and (job_hour := job["hour"] or hour) and (job_minute := job["minute"] or minute) and (job_day := job["weekday"]) and ((job_day := None) if (job_day == "" or job_day == "*") else (job_day := day) if (int(job_day) == weekday) else _) and ((job_date := day + (int(0 if job_day is None else job_day) - weekday)) if (int(0 if job_day is None else job_day) > weekday) else (job_date := day + (weekday - int(0 if job_day is None else job_day) + 1)) if (int(0 if job_day is None else job_day) < weekday) else _) and (((job_day := day) if (weekday >= int(job_day.split('-')[0]) and weekday <= int(job_day.split('-')[1])) else _) if (job_day and type(job_day) == str and '-' in job_day) else _) and ((job_day := day + 1) if (int(job_day) == weekday and job_hour > hour) else _) and ((job_month := int(num) for num in job_month.split(',') if num >= month) if (job_month and type(job_month) == str and ',' in job_month) else _) and ((job_month := int(job_month.split('/')[1])) if (job_month and type(job_month) == str and '/' in job_month) else _) and (out := job_month)) else f"Debug: {job_day} ({type(job_day)})"
14
15print(
16    get_dates({
17        "date": '30', 
18        "hour": "11", 
19        "minute": "59", 
20        "month": "4/6", 
21        "weekday": "2", 
22        "year": "*2"
23    })
24)

Here's how I did this:

1from typing import Callable, Dict, NoReturn
2
3def panic() -> NoReturn:
4    raise Exception("Error")
5
6sanitize: Callable[[str], str | None] = lambda string: out if (
7    (out := string.strip())
8    and
9    (parts := out.split("-"))
10    and
11    (parts := [
12        capital for part in parts if (capital := part.capitalize())
13    ])
14    and
15    (out := " ".join(parts))
16
17) else panic();

Then:

1MatchHowToParse: Dict[str, Callable[[str], str]] = {
2    "foo": lambda string: out if (
3        (out := string.strip())
4        and
5        (parts := out.split("-"))
6        and
7        (parts := [
8            for_out for part in parts if (
9                (
10                    (capital := part.capitalize())
11                    and
12                    (foo := capital.removesuffix("r"))
13                    and
14                    (for_out := foo)
15                ) 
16                    if (
17                        len(part) == 3 
18                    )
19                    else (
20                        for_out := part
21                    ) 
22                        if (
23                            len(part) == 4
24                        )
25                    else (
26                        for_out := part.upper()
27                    )
28            )
29        ])
30        and
31        (out := " ".join(parts))
32    ) else "None",
33    "bar": lambda string: out if (
34        (out := string.strip())
35        and
36        (parts := out.split("-"))
37        and
38        (parts := [
39            capital for part in parts if (capital := part.capitalize())
40        ])
41        and
42        (out := " ".join(parts))
43    ) else "None",
44}
45
46
47print(
48    (
49        MatchHowToParse["foo"]("foo-bar-bazzy-boor")
50    )
51)

I then attempted to convert a serious library, a basic crontab parser, into this single-line style:

1from typing import Callable, TypedDict, Any
2from datetime import datetime as Datetime
3
4Job = TypedDict("Job", {
5    "year": str,
6    "month": str,
7    "date": str,
8    "hour": str,
9    "minute": str,
10    "weekday": str
11})
12
13get_dates: Callable[[Job], Any] = lambda job: out if (
14    (_ := True)
15    and
16    (now := Datetime.now())
17    and 
18        (day := now.day)
19        and
20        (month := now.month)
21        and
22        (year := now.year)
23        and
24        (hour := now.hour) 
25        and
26        (minute := now.minute)
27        and
28        (weekday := now.weekday()) 
29    
30    and 
31        (job_year := year)
32        and
33        (job_month := job["month"] or month)
34        and
35        (job_date := job["date"] or day)
36        and
37        (job_hour := job["hour"] or hour) 
38        and
39        (job_minute := job["minute"] or minute)
40        and
41        (job_day := job["weekday"]) 
42        
43    # Day of the week
44    and (
45        (job_day := None)
46            if (job_day == "" or job_day == "*")
47        else (job_day := day) 
48            if (int(job_day) == weekday)
49            else _
50    )
51    and (
52        (job_date := day + (int(0 if job_day is None else job_day) - weekday))
53            if (int(0 if job_day is None else job_day) > weekday)
54        else (job_date := day + (weekday - int(0 if job_day is None else job_day) + 1))
55            if (int(0 if job_day is None else job_day) < weekday)
56            else _
57    )
58    and (
59        (
60            (job_day := day                                                                 ) 
61                if (weekday >= int(job_day.split('-')[0]) and weekday <= int(job_day.split('-')[1])) 
62                else _
63        ) if (job_day and type(job_day) == str and '-' in job_day)
64            else _
65    )
66    and (
67        (job_day := day + 1                                                                 )
68            if (int(job_day) == weekday and job_hour > hour)
69            else _
70    )
71    # Month
72    and (
73        (job_month := int(num) for num in job_month.split(',') if num >= month      )
74            if (job_month and type(job_month) == str and ',' in job_month)
75            else _ 
76    )
77    and (
78        (job_month := int(job_month.split('/')[1])                                  )
79            if (job_month and type(job_month) == str and '/' in job_month)
80            else _
81    )
82
83    # ...
84
85    and 
86    (out := job_month)
87
88) else f"Debug: {job_day} ({type(job_day)})"
89# Because the whole thing runs by 'and' connecting truthy clauses, 
90# this else block is equivalent to an erroneous early return
91
92print( 
93    get_dates({
94        "date": '30',
95        "hour": "11",
96        "minute": "59",
97        "month": "4/6",
98        "weekday": "2",
99        "year": "*2",
100    })
101)

But unfortunately, I was using an example repo to create this, because I wanted a basic example of a useful and complete, couple hundred line codebase. I encountered that the hardest part of converting into this nonsense was...

Determining the types of unlabeled JavaScript.

This runs, exactly as expected, up until the point that I stopped, and contains for loops with conditional control flow inside. This means you could make an indefinitely long cursed script like this. If you choose to not even declare functions, but just include their bodies instead, you could basically make a whole library in it.