STRling 0.0.2__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of STRling might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: STRling
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Description-Content-Type: text/markdown
5
5
 
6
6
  # STRling
@@ -38,28 +38,34 @@ pip install STRling
38
38
  Here's a quick example to demonstrate how easy it is to get started with STRling:
39
39
 
40
40
  ```python
41
- from STRling import match, group, lib, test
42
41
  import re
42
+ from STRling import digit, lit, group, merge, or_
43
43
 
44
- # Define a group of 3 digits followed by a hyphen
45
- num3hyp = group('group3', lib.num(3), lib.lit('-'))
44
+ # Define a named capture group for phone number
45
+ first_group = group('first', digit()(3))
46
+ second_group = group('second', digit()(3))
47
+ third_group = group('third', digit()(4))
46
48
 
47
- # Define a group of 4 digits
48
- num4 = group('group4', lib.num(4))
49
+ # Create a non-capturing group for phone number pattern
50
+ phone_num = merge(first_group, lit('-')(), second_group, lit('-')(), third_group)
49
51
 
50
- # Create a custom number group
51
- custom = group('custom', num3hyp(2), num4())
52
+ # Create a phone number group of 10 digits or the phone number pattern
53
+ custom = digit()(10)
54
+ phone_number = group('phone_number', or_(custom, phone_num))
52
55
 
53
- # Define a group of 10 digits
54
- num10 = group('group10', lib.num(10))
55
-
56
- # Create a phone number group using either custom or num10
57
- phone_number = group('phone_number', lib.or_(custom, num10))
56
+ # Print the pattern value
57
+ print(str(phone_number))
58
+ # Output: (?P<phone_number>\d{10}|(?:(?P<first>\d{3})-(?P<second>\d{3})-(?P<third>\d{4})))
58
59
 
59
60
  # Compile the pattern and match against a string
60
- compiled_pattern = re.compile(phone_number)
61
+ compiled_pattern = re.compile(str(phone_number))
61
62
  input_string = "123-456-7890"
62
63
  match = compiled_pattern.match(input_string)
64
+
65
+ # Check the match and print the captured groups
66
+ if match:
67
+ print(match.groupdict())
68
+ # Output: {'phone_number': '123-456-7890', 'first': '123', 'second': '456', 'third': '7890'}
63
69
  ```
64
70
 
65
71
  ### License
@@ -33,28 +33,34 @@ pip install STRling
33
33
  Here's a quick example to demonstrate how easy it is to get started with STRling:
34
34
 
35
35
  ```python
36
- from STRling import match, group, lib, test
37
36
  import re
37
+ from STRling import digit, lit, group, merge, or_
38
38
 
39
- # Define a group of 3 digits followed by a hyphen
40
- num3hyp = group('group3', lib.num(3), lib.lit('-'))
39
+ # Define a named capture group for phone number
40
+ first_group = group('first', digit()(3))
41
+ second_group = group('second', digit()(3))
42
+ third_group = group('third', digit()(4))
41
43
 
42
- # Define a group of 4 digits
43
- num4 = group('group4', lib.num(4))
44
+ # Create a non-capturing group for phone number pattern
45
+ phone_num = merge(first_group, lit('-')(), second_group, lit('-')(), third_group)
44
46
 
45
- # Create a custom number group
46
- custom = group('custom', num3hyp(2), num4())
47
+ # Create a phone number group of 10 digits or the phone number pattern
48
+ custom = digit()(10)
49
+ phone_number = group('phone_number', or_(custom, phone_num))
47
50
 
48
- # Define a group of 10 digits
49
- num10 = group('group10', lib.num(10))
50
-
51
- # Create a phone number group using either custom or num10
52
- phone_number = group('phone_number', lib.or_(custom, num10))
51
+ # Print the pattern value
52
+ print(str(phone_number))
53
+ # Output: (?P<phone_number>\d{10}|(?:(?P<first>\d{3})-(?P<second>\d{3})-(?P<third>\d{4})))
53
54
 
54
55
  # Compile the pattern and match against a string
55
- compiled_pattern = re.compile(phone_number)
56
+ compiled_pattern = re.compile(str(phone_number))
56
57
  input_string = "123-456-7890"
57
58
  match = compiled_pattern.match(input_string)
59
+
60
+ # Check the match and print the captured groups
61
+ if match:
62
+ print(match.groupdict())
63
+ # Output: {'phone_number': '123-456-7890', 'first': '123', 'second': '456', 'third': '7890'}
58
64
  ```
59
65
 
60
66
  ### License
@@ -0,0 +1,2 @@
1
+ from . import lib
2
+ from .lib import group, merge
@@ -1,18 +1,9 @@
1
1
  import re
2
2
 
3
3
 
4
- # Decorators
5
- def repetition(func):
6
- def wrapper(*args, min=None, max=None, **kwargs):
7
- pattern = func(*args, **kwargs)
8
- pattern += repeat(min, max)
9
- return pattern
10
- return wrapper
11
-
12
-
13
4
  # Range syntax
14
5
  def repeat(min=None, max=None):
15
- """Create a range for the preceding symbol"""
6
+ """Create a range for the preceding symbol."""
16
7
  if min is not None and max is not None:
17
8
  if min > max:
18
9
  raise ValueError('min cannot be greater than max')
@@ -22,22 +13,52 @@ def repeat(min=None, max=None):
22
13
  return ''
23
14
 
24
15
 
16
+ # Pattern class
17
+ class Pattern:
18
+ def __init__(self, pattern):
19
+ self.pattern = pattern
20
+
21
+ def __call__(self, min=None, max=None):
22
+ return self.pattern + repeat(min, max)
23
+
24
+ def __str__(self):
25
+ return self.pattern
26
+
27
+
28
+ # Decorators
29
+ def repetition(func):
30
+ def wrapper(*args, **kwargs):
31
+ pattern = func(*args, **kwargs)
32
+ return Pattern(pattern)
33
+ return wrapper
34
+
35
+
25
36
  # Literals
26
37
  @repetition
27
38
  def lit(text):
28
- """Match the literal string `text`, optionally specifying repeat."""
39
+ """Match the literal string `text`."""
29
40
  return re.escape(text)
30
41
 
31
42
  @repetition
32
- def digit(min=None, max=None):
33
- """Match a digit, optionally specifying repeat."""
43
+ def digit():
44
+ """Match a digit."""
34
45
  return r'\d'
35
46
 
36
47
  @repetition
37
- def letter(min=None, max=None):
48
+ def letter():
38
49
  """Match a letter."""
39
50
  return r'[A-Za-z]'
40
51
 
52
+ @repetition
53
+ def upper():
54
+ """Match a letter."""
55
+ return r'[A-Z]'
56
+
57
+ @repetition
58
+ def lower():
59
+ """Match a letter."""
60
+ return r'[a-z]'
61
+
41
62
  @repetition
42
63
  def between(start_char, end_char):
43
64
  """Match any character between `start_char` and `end_char`."""
@@ -84,53 +105,53 @@ def carriage():
84
105
  # Conditionals
85
106
  def or_(*patterns):
86
107
  """Match any of the given patterns."""
87
- joined = '|'.join(patterns)
88
- return f'({joined})'
108
+ joined = '|'.join(str(p) for p in patterns)
109
+ return Pattern(f'({joined})')
89
110
 
90
111
  def may(pattern):
91
112
  """Match zero or one occurrence of the pattern."""
92
- return f'{pattern}?'
113
+ return Pattern(f'{pattern}?')
93
114
 
94
115
 
95
116
  # Anchors
96
117
  def start():
97
118
  """Match the start of the string."""
98
- return r'^'
119
+ return Pattern(r'^')
99
120
 
100
121
  def end():
101
122
  """Match the end of the string."""
102
- return r'$'
123
+ return Pattern(r'$')
103
124
 
104
125
  def bound(text):
105
126
  """Match the exact word `text`."""
106
- return r'\b'
127
+ return Pattern(r'\b')
107
128
 
108
129
 
109
130
  # Lookarounds
110
131
  def ahead(pattern):
111
132
  """Match if the pattern matches ahead."""
112
- return f'(?={pattern})'
133
+ return Pattern(f'(?={pattern})')
113
134
 
114
135
  def behind(pattern):
115
136
  """Match if the pattern matches behind."""
116
- return f'(?<={pattern})'
137
+ return Pattern(f'(?<={pattern})')
117
138
 
118
139
  def notahead(pattern):
119
140
  """Match if the pattern does not match ahead."""
120
- return f'(?!{pattern})'
141
+ return Pattern(f'(?!{pattern})')
121
142
 
122
143
  def notbehind(pattern):
123
144
  """Match if the pattern does not match behind."""
124
- return f'(?<!{pattern})'
145
+ return Pattern(f'(?<!{pattern})')
125
146
 
126
147
 
127
148
  # Grouping
128
149
  def group(name, *patterns):
129
150
  """Create a named capture group with the given patterns."""
130
- joined = ''.join(patterns)
131
- return f'(?P<{name}>{joined})'
151
+ joined = ''.join(str(p) for p in patterns)
152
+ return Pattern(f'(?P<{name}>{joined})')
132
153
 
133
154
  def merge(*patterns):
134
155
  """Create a non-capturing group with the given patterns."""
135
- joined = ''.join(patterns)
136
- return f'(?:{joined})'
156
+ joined = ''.join(str(p) for p in patterns)
157
+ return Pattern(f'(?:{joined})')
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: STRling
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Description-Content-Type: text/markdown
5
5
 
6
6
  # STRling
@@ -38,28 +38,34 @@ pip install STRling
38
38
  Here's a quick example to demonstrate how easy it is to get started with STRling:
39
39
 
40
40
  ```python
41
- from STRling import match, group, lib, test
42
41
  import re
42
+ from STRling import digit, lit, group, merge, or_
43
43
 
44
- # Define a group of 3 digits followed by a hyphen
45
- num3hyp = group('group3', lib.num(3), lib.lit('-'))
44
+ # Define a named capture group for phone number
45
+ first_group = group('first', digit()(3))
46
+ second_group = group('second', digit()(3))
47
+ third_group = group('third', digit()(4))
46
48
 
47
- # Define a group of 4 digits
48
- num4 = group('group4', lib.num(4))
49
+ # Create a non-capturing group for phone number pattern
50
+ phone_num = merge(first_group, lit('-')(), second_group, lit('-')(), third_group)
49
51
 
50
- # Create a custom number group
51
- custom = group('custom', num3hyp(2), num4())
52
+ # Create a phone number group of 10 digits or the phone number pattern
53
+ custom = digit()(10)
54
+ phone_number = group('phone_number', or_(custom, phone_num))
52
55
 
53
- # Define a group of 10 digits
54
- num10 = group('group10', lib.num(10))
55
-
56
- # Create a phone number group using either custom or num10
57
- phone_number = group('phone_number', lib.or_(custom, num10))
56
+ # Print the pattern value
57
+ print(str(phone_number))
58
+ # Output: (?P<phone_number>\d{10}|(?:(?P<first>\d{3})-(?P<second>\d{3})-(?P<third>\d{4})))
58
59
 
59
60
  # Compile the pattern and match against a string
60
- compiled_pattern = re.compile(phone_number)
61
+ compiled_pattern = re.compile(str(phone_number))
61
62
  input_string = "123-456-7890"
62
63
  match = compiled_pattern.match(input_string)
64
+
65
+ # Check the match and print the captured groups
66
+ if match:
67
+ print(match.groupdict())
68
+ # Output: {'phone_number': '123-456-7890', 'first': '123', 'second': '456', 'third': '7890'}
63
69
  ```
64
70
 
65
71
  ### License
@@ -9,7 +9,7 @@ with open('README.md', 'r') as f:
9
9
 
10
10
  setup(
11
11
  name='STRling',
12
- version='0.0.2',
12
+ version='0.0.3',
13
13
  packages=find_packages(),
14
14
  install_requires=[
15
15
  # Add dependencies here.
@@ -1 +0,0 @@
1
- from . import lib
File without changes