Skip to content

Commit e379ea8

Browse files
app for Python re(gex)? exercises
1 parent ad7c446 commit e379ea8

9 files changed

+2198
-1
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Sundeep Agarwal
3+
Copyright (c) 2023 Sundeep Agarwal
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

PyRegexExercises/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Python re(gex)? exercises
2+
3+
This TUI application is intended to help you practice Python regular expressions. There are more than 100 exercises covering both the builtin `re` and third-party `regex` module.
4+
5+
# Installation
6+
7+
To be added after releasing a package.
8+
9+
Here's a sample screenshot:
10+
11+
<p align="center"><img src="./pyregex_exercises.png" alt="Sample Python regex exercise" /></p>
12+
13+
# Guide
14+
15+
See [app_guide.md](./app_guide.md)
16+
17+
# Ebook
18+
19+
See my [Understanding Python re(gex)?](https://github.com/learnbyexample/py_regular_expressions) ebook to learn regular expressions with hundreds of examples and exercises.
20+
21+
# License
22+
23+
Code snippets are licensed under [MIT LICENSE](../LICENSE)
24+
25+
Exercise questions and associated files (like `questions.json`) are licensed under [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/)
26+

PyRegexExercises/app_guide.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
### Questions
2+
3+
There are more than 100 exercises covering both the builtin `re` and third-party `regex` module. The initial questions cover the very basics of using `re` module functions. After that, you'll be required to use more and more regular expression features, roughly in the following order:
4+
5+
* Anchors
6+
* Alternation and Grouping
7+
* Escaping metacharacters
8+
* Dot metacharacter and Quantifiers
9+
* Working with matched portions
10+
* Character class
11+
* Groupings and backreferences
12+
* Lookarounds
13+
* regex module
14+
15+
There are two types of exercises:
16+
17+
* search related functions like `search()`, `fullmatch()`, etc
18+
* other functions like `sub()`, `split()`, `findall()`, `finditer()`, etc
19+
20+
### Solution Input box
21+
22+
You can type the solution in the input box and press the **Enter** key to execute. For example, `re.search(r'cat', ip)` to check if `cat` is present in the input strings. `ip` is the variable that'll have the sample input (string or list of strings) for all the exercises.
23+
24+
The input box will accept a single valid Python expression. Some of the exercises will require you to use list comprehensions.
25+
26+
Some of the error types are caught. In such cases, the background color of the input box will change to *red* and the error message will be displayed below the box. Other errors might result in the app crashing.
27+
28+
The input box will turn *green* if the entered code solves all the sample input strings. A reference solution box will appear below the table in such cases. You can press **Ctrl+s** to show the solution box whenever you want.
29+
30+
For search related functions, the table below the input box will have a set of strings that should match and another set of strings that *shouldn't* match. These strings will be individually highlighted in *green* if the condition is satisfied and *red* otherwise.
31+
32+
For some exercises, you might need to view the representation of sample strings instead of how they are displayed on the screen. For example, to spot characters like tabs, newlines, backspaces, etc. You can switch between these views using the **str** and **repr** radio buttons. Lists and dicts won't be affected by this option. You can also use the **Ctrl+r** shortcut to toggle these radio buttons.
33+
34+
For other functions, the left column of the table will have a set of input with corresponding output in the right column. Each row will be highlighted in *green* or *red* based on the entered code satisfying the condition or not. You can switch between **expected** and **actual** radio buttons to view the expected and actual output in the right column. When you choose the **actual** option, the strings will be highlighted in *orange* if the output doesn't match the expected result. You can also use the **Ctrl+b** shortcut to toggle these radio buttons.
35+
36+
Use appropriate anchors based on whether the term **string** or **line** is used in the question description.
37+
38+
> **Warning:** There is no safeguard against the commands you have typed. They are treated as if you executed them from a Python program.
39+
40+
### Shortcuts
41+
42+
You can either click the buttons using mouse or press the key combinations listed below:
43+
44+
* **F1** view this guide
45+
* **F2** view exercises
46+
* **Ctrl+n** go the next question
47+
* **Ctrl+p** go the previous question
48+
* **Ctrl+s** show solution box
49+
* **Ctrl+r** toggle between **str** and **repr**
50+
* **Ctrl+b** toggle between **expected** and **actual**
51+
* **Ctrl+t** toggle the theme between **light** and **dark** modes
52+
* **Ctrl+c** or **Ctrl+q** quit the application
53+
54+
Shortcuts for navigating and editing in the solution box are listed below:
55+
56+
* Use mouse click to position the cursor anywhere you like
57+
* **** move left by one character
58+
* **** move right by one character
59+
* **Home** or **Ctrl+a** move to the start of the line
60+
* **End** or **Ctrl+e** move to the end of the line
61+
* **Ctrl+←** move to the start of the current/previous word
62+
* **Ctrl+→** move to the start of the next word
63+
* **Ctrl+w** delete till the start of the current/previous word
64+
* **Ctrl+f** delete till the start of the next word
65+
* **Ctrl+u** delete till the start of the line
66+
* **Ctrl+k** delete till the end of the line
67+
* **Backspace** or **Ctrl+h** delete character to the left of the cursor
68+
* **Delete** or **Ctrl+d** delete character under the cursor
69+
* **Enter** submit the code for execution
70+
71+
### User progress
72+
73+
Solutions you have typed are automatically saved in `user_progress.json` in the same directory as the script. This happens only when you press **Enter** to execute — navigating to another question and closing the app won't trigger the save logic.
74+
75+
Radio button and theme choices are also saved.
76+
77+
If you close the application and open it again, the first unsolved question will be displayed (i.e. already solved questions are skipped).
78+
79+
If you use **Ctrl+s**, the solution *won't* be saved — you'll have to type and execute the code to be considered for saving the changes.
80+
81+
Once you have solved a question, only a different correct solution can override the previously saved code.
82+
83+
### Understanding Python re(gex)?
84+
85+
The exercise questions in this app have been adapted from my ebook: [https://github.com/learnbyexample/py_regular_expressions](https://github.com/learnbyexample/py_regular_expressions)
86+
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.container {
2+
height: auto;
3+
align: center middle;
4+
}
5+
6+
#code {
7+
height: auto;
8+
border: solid green;
9+
}
10+
11+
#input {
12+
padding: 0 1 0 1;
13+
margin: 0 1 0 1;
14+
border: none;
15+
background: silver;
16+
}
17+
18+
.radio {
19+
layout: horizontal;
20+
border: none;
21+
width: 50%;
22+
margin: 0 2 0 2;
23+
}
24+
25+
#question {
26+
width: 100%;
27+
margin: 0 1 1 1;
28+
}
29+
30+
#solution {
31+
width: 100%;
32+
margin: 0 1 1 1;
33+
}
34+
35+
#columns {
36+
height: auto;
37+
align: center middle;
38+
border: solid brown;
39+
}
40+
41+
.list_title {
42+
text-align: center;
43+
color: gray;
44+
}
45+
46+
.list_item {
47+
border-top: solid gray;
48+
padding: 0 1 0 1;
49+
}
50+
51+
.list {
52+
height: auto;
53+
margin: 0 1 0 1;
54+
width: 50%;
55+
color: black;
56+
}
57+
58+
.table {
59+
height: auto;
60+
align: center middle;
61+
}
62+
63+
.error {
64+
min-height: 0;
65+
height: auto;
66+
width: 100%;
67+
color: red;
68+
}
69+
70+
Button:focus {
71+
text-style: bold;
72+
}
73+
74+
.buttons {
75+
max-height: 1;
76+
border: none;
77+
width: 50%;
78+
}
23 KB
Loading

0 commit comments

Comments
 (0)