rsclib 0.65__py3-none-any.whl → 0.67__py3-none-any.whl

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.
rsclib/hexdump.py CHANGED
@@ -73,19 +73,29 @@ def unhexdump (iterable, file = None):
73
73
  continue
74
74
  if end:
75
75
  raise ValueError ("Unknown hexdump format")
76
- x = line.split (' ', 2)
76
+ x = line.split (' ', 1)
77
77
  if len (x) == 1:
78
78
  h = x [0]
79
79
  else:
80
80
  h = x [1]
81
- assert len (x [0]) <= 8
81
+ assert len (x [0]) <= 10
82
82
  # Probably last line with only an address
83
83
  if ' ' not in h and len (x) == 1 and len (h) > 2:
84
84
  end = True
85
85
  continue
86
- h = h.strip ().split (' ')
86
+ # Determine format: split the thing and throw away last element
87
+ # if that is longer than 2.
88
+ h = h.strip ().split ()
87
89
  if len (h [0]) > 2:
88
90
  h = h [1:]
91
+ if len (h) > 16:
92
+ h = h [:16]
93
+ if len (h [-1]) != 2:
94
+ h = h [:-1]
95
+ assert len (h [0]) == 2
96
+ assert len (h [-1]) == 2
97
+ assert len (h) <= 16
98
+
89
99
  if sys.version_info [0] < 3:
90
100
  method = lambda x: b''.join (chr (c) for c in x)
91
101
  else:
rsclib/iter_recipes.py CHANGED
@@ -182,3 +182,20 @@ except ImportError :
182
182
  for j in range (i+1, r) :
183
183
  indices [j] = indices [j-1] + 1
184
184
  yield tuple (pool [i] for i in indices)
185
+
186
+ try :
187
+ from itertools import batched
188
+ except ImportError :
189
+ def batched (x, n):
190
+ b = []
191
+ x = iter (x)
192
+ while True:
193
+ for k in range (n):
194
+ try:
195
+ b.append (next (x))
196
+ except StopIteration:
197
+ if b:
198
+ yield b
199
+ return
200
+ yield b
201
+ b = []
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/python
2
- # -*- coding: iso-8859-1 -*-
3
- # Copyright (C) 2007-17 Dr. Ralf Schlatterbeck Open Source Consulting.
2
+ # Copyright (C) 2007-24 Dr. Ralf Schlatterbeck Open Source Consulting.
4
3
  # Reichergasse 131, A-3411 Weidling.
5
4
  # Web: http://www.runtux.com Email: office@runtux.com
6
5
  # All rights reserved
@@ -152,10 +151,7 @@ class Rational (autosuper) :
152
151
  other = self.__class__ (other)
153
152
  p = self.p * other.q
154
153
  q = self.q * other.p
155
- if q < 0 :
156
- p = -p
157
- q = -q
158
- return self.__class__ (p, q)
154
+ return p // q
159
155
  # end def __floordiv__
160
156
 
161
157
  def __rfloordiv__ (self, other) :
@@ -164,6 +160,23 @@ class Rational (autosuper) :
164
160
  return other // self
165
161
  # end def __rfloordiv__
166
162
 
163
+ def __truediv__ (self, other) :
164
+ if not isinstance (other, self.__class__) :
165
+ other = self.__class__ (other)
166
+ p = self.p * other.q
167
+ q = self.q * other.p
168
+ if q < 0 :
169
+ p = -p
170
+ q = -q
171
+ return self.__class__ (p, q)
172
+ # end def __truediv__
173
+
174
+ def __rtruediv__ (self, other) :
175
+ if not isinstance (other, self.__class__) :
176
+ other = self.__class__ (other)
177
+ return other / self
178
+ # end def __rtruediv__
179
+
167
180
  def __int__ (self) :
168
181
  return int (self.p // self.q)
169
182
  # end def __int__
@@ -193,10 +206,6 @@ class Rational (autosuper) :
193
206
  return self + other
194
207
  # end def __radd__
195
208
 
196
- def __rdiv__ (self, other) :
197
- return self.__class__ (other) // self
198
- # end def __rdiv__
199
-
200
209
  def __repr__ (self) :
201
210
  if self.q == 1 :
202
211
  return "%d" % self.p
rsclib/stateparser.py CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/python3
2
- # Copyright (C) 2007-21 Dr. Ralf Schlatterbeck Open Source Consulting.
2
+ # Copyright (C) 2007-24 Dr. Ralf Schlatterbeck Open Source Consulting.
3
3
  # Reichergasse 131, A-3411 Weidling.
4
4
  # Web: http://www.runtux.com Email: office@runtux.com
5
5
  # All rights reserved
@@ -25,21 +25,21 @@ import re
25
25
  import sys
26
26
  from rsclib.autosuper import autosuper
27
27
  from rsclib.base_pickler import Base_Pickler
28
- from rsclib.pycompat import string_types
28
+ from rsclib.pycompat import string_types, text_type
29
29
 
30
30
  pattern_type = type (re.compile (''))
31
31
 
32
- class Parse_Error (ValueError) : pass
32
+ class Parse_Error (ValueError): pass
33
33
 
34
- class Debug (autosuper) :
35
- def debug (self, level, * msg) :
36
- if self.verbose >= level :
37
- print (' '.join (unicode (x) for x in msg), file = sys.stderr)
34
+ class Debug (autosuper):
35
+ def debug (self, level, * msg):
36
+ if self.debug_level >= level:
37
+ print (' '.join (text_type (x) for x in msg), file = sys.stderr)
38
38
  sys.stderr.flush ()
39
39
  # end def debug
40
40
  # end class Debug
41
41
 
42
- class Transition (Debug) :
42
+ class Transition (Debug):
43
43
  """ Represents one line in a state-change diagram. If matched,
44
44
  applies the defined action (if any) and returns new state.
45
45
 
@@ -50,23 +50,23 @@ class Transition (Debug) :
50
50
  Action methods get the matched line, and in case of a regex the
51
51
  matched groups as parameter.
52
52
  """
53
- def __init__ (self, pattern, state, new_state, action, **kw) :
54
- self.pattern = pattern
55
- self.state = state
56
- self.new_state = new_state
57
- self.verbose = kw.get ('verbose', 0)
58
- self.act_name = action
59
- if action :
53
+ def __init__ (self, pattern, state, new_state, action, **kw):
54
+ self.pattern = pattern
55
+ self.state = state
56
+ self.new_state = new_state
57
+ self.debug_level = kw.get ('debug_level', 0)
58
+ self.act_name = action
59
+ if action:
60
60
  action = getattr (self.state.parser, action)
61
- self.action = action
61
+ self.action = action
62
62
  self.__super.__init__ (**kw)
63
63
  # end def __init__
64
64
 
65
- def _transition (self, match = None) :
65
+ def _transition (self, match = None):
66
66
  new = None
67
67
  pstate = self.state.parser.state
68
68
  line = self.state.parser.line
69
- if self.action :
69
+ if self.action:
70
70
  new = self.action (self.state, self.new_state, match)
71
71
  new = new or self.new_state
72
72
  self.debug \
@@ -77,15 +77,15 @@ class Transition (Debug) :
77
77
  return new
78
78
  # end def _transition
79
79
 
80
- def match (self) :
80
+ def match (self):
81
81
  line = self.state.parser.line
82
- if self.pattern is None or line == self.pattern :
82
+ if self.pattern is None or line == self.pattern:
83
83
  self.debug \
84
84
  (2, "match: %s (act = %s)" % (self.pattern, self.act_name))
85
85
  return self._transition ()
86
- if isinstance (self.pattern, pattern_type) :
86
+ if isinstance (self.pattern, pattern_type):
87
87
  m = self.pattern.search (line)
88
- if m :
88
+ if m:
89
89
  self.debug (2, "match: <regex> (act = %s)" % self.act_name)
90
90
  return self._transition (m)
91
91
  self.debug \
@@ -98,34 +98,34 @@ class Transition (Debug) :
98
98
 
99
99
  # end class Transition
100
100
 
101
- class State (Debug) :
101
+ class State (Debug):
102
102
  """ Represents a single state of the parser """
103
103
 
104
- def __init__ (self, parser, name, **kw) :
104
+ def __init__ (self, parser, name, **kw):
105
105
  self.name = name
106
106
  self.parser = parser
107
107
  self.transitions = []
108
- self.verbose = kw.get ('verbose', 0)
108
+ self.debug_level = kw.get ('debug_level', 0)
109
109
  self.__super.__init__ (**kw)
110
110
  # end def __init__
111
111
 
112
- def append (self, transition) :
112
+ def append (self, transition):
113
113
  self.transitions.append (transition)
114
114
  # end def append
115
115
 
116
- def match (self) :
117
- for t in self.transitions :
116
+ def match (self):
117
+ for t in self.transitions:
118
118
  state = t.match ()
119
- if state :
119
+ if state:
120
120
  return state
121
- else :
121
+ else:
122
122
  raise Parse_Error \
123
123
  ("%s: %s" % (self.parser.lineno, self.parser.line))
124
124
  # end def match
125
125
 
126
126
  # end class State
127
127
 
128
- class Parser (Debug, Base_Pickler) :
128
+ class Parser (Debug, Base_Pickler):
129
129
  """ Simple state-machine parser.
130
130
  To use, define a subclass with the necessary actions. An action
131
131
  method gets the line matched and an optional match object.
@@ -134,55 +134,55 @@ class Parser (Debug, Base_Pickler) :
134
134
  pickle_exceptions = dict.fromkeys (('stack', 'state', 'states'))
135
135
  encoding = 'latin1'
136
136
 
137
- def __init__ (self, matrix = None, **kw) :
138
- self.verbose = kw.get ('verbose', 0)
139
- self.state = None
140
- self.states = {}
137
+ def __init__ (self, matrix = None, **kw):
138
+ self.debug_level = kw.get ('debug_level', 0)
139
+ self.state = None
140
+ self.states = {}
141
141
  matrix = matrix or self.matrix
142
142
  self.__super.__init__ (**kw)
143
- for line in matrix :
143
+ for line in matrix:
144
144
  self.add_transition (* line)
145
145
  self.stack = []
146
146
  # end def __init__
147
147
 
148
- def add_transition (self, statename, pattern, newname, action) :
149
- if statename not in self.states :
148
+ def add_transition (self, statename, pattern, newname, action):
149
+ if statename not in self.states:
150
150
  self.states [statename] = \
151
- State (self, statename, verbose = self.verbose)
151
+ State (self, statename, debug_level = self.debug_level)
152
152
  state = self.states [statename]
153
153
  new = None
154
- if newname :
155
- if newname not in self.states :
154
+ if newname:
155
+ if newname not in self.states:
156
156
  self.states [newname] = \
157
- State (self, newname, verbose = self.verbose)
157
+ State (self, newname, debug_level = self.debug_level)
158
158
  new = self.states [newname]
159
- if not self.state :
159
+ if not self.state:
160
160
  self.state = state
161
161
  t = Transition \
162
- (pattern, state, new, action, verbose = self.verbose)
162
+ (pattern, state, new, action, debug_level = self.debug_level)
163
163
  state.append (t)
164
164
  # end def add_transition
165
165
 
166
- def parse (self, file) :
167
- for n, line in enumerate (file) :
168
- if self.encoding :
166
+ def parse (self, file):
167
+ for n, line in enumerate (file):
168
+ if self.encoding:
169
169
  line = line.decode (self.encoding)
170
170
  self.line = line.rstrip ()
171
171
  self.lineno = n + 1
172
- try :
172
+ try:
173
173
  self.state = self.state.match ()
174
- except Exception as cause :
174
+ except Exception as cause:
175
175
  #raise Parse_Error (self.lineno, cause)
176
176
  raise
177
177
  # end def parse
178
178
 
179
- def push (self, state, new_state = None, match = None) :
179
+ def push (self, state, new_state = None, match = None):
180
180
  self.stack.append (state)
181
181
  stack = [s.name for s in self.stack]
182
182
  self.debug (3, "push: %s, stack: %s" % (new_state.name, stack))
183
183
  # end def push
184
184
 
185
- def pop (self, state = None, new_state = None, match = None) :
185
+ def pop (self, state = None, new_state = None, match = None):
186
186
  self.debug \
187
187
  ( 3
188
188
  , "before pop: %s" % self.state.name
@@ -201,7 +201,7 @@ class Parser (Debug, Base_Pickler) :
201
201
  return state
202
202
  # end def pop
203
203
 
204
- def endpop (self, state = None, new_state = None, match = None) :
204
+ def endpop (self, state = None, new_state = None, match = None):
205
205
  """ Pop stack *after* handling the current line
206
206
  """
207
207
  return self.stack.pop ()