2026-xx-xx_this-is-python.html


001| #:name::This is Python! (part ?)
002| #:link::
003| #:date::2026-0X-XX
004| #:note:: Lambda Functions, Standard Library, Decorators, Strings, ...
005| #:code::
006| #:code::
007| #:code::
008| #:code::
009| #:code::
010| #:code::
011| #:code::
012| #:code::
013| #:code::
014| #:code::
015| #:code::
016| #:code::
017| #:code::
018| #:code::
019| #:code::
020| #:code::
021| #:code::
022| #:code::
023| #:code::
024| #:code::
025| #:code::
026| #:name:: LAMBDA FUNCTIONS
027| #:code::
028| # Lambda expressions...
029| 
030| ListSize = 16
031| 
032| Reds = [3, 7, 13]
033| 
034| Item = lambda n: 'red' if n in Reds else 'black'
035| 
036| 
037| # Start with an empty list...
038| colors = []
039| 
040| # Create list of colors long...
041| for ix in range(ListSize):
042| 
043|     # Get the color for this index...
044|     color = Item(ix)
045| 
046|     # Append it to the list...
047|     colors.append(color)
048| 
049| 
050| # Print the list we made...
051| for ix,color in enumerate(colors):
052|     print(f'{ix:2d}: {color}')
053| 
054| print()
055| 
056| #:code::
057| # Lambda Function Factory...
058| 
059| # Define a lambda function that returns a lambda function...
060| multiply_by = lambda factor: lambda n: factor*n
061| 
062| # Get some "multiplier" functions...
063| multiply_by_5 = multiply_by(5)
064| multiply_by_8 = multiply_by(8)
065| 
066| print(f'')
067| print()
068| 
069| #:code::
070| #:code::
071| #:code::
072| #:code::
073| #:code::
074| #:code::
075| #:code::
076| #:name:: DECORATORS
077| #:code::
078| import time
079| import random
080| 
081| def timer (function):
082|     ts = []
083| 
084|     def inner (*args):
085|         t0 = time.time()
086|         rv = function(*args)
087|         tt = time.time() - t0
088|         ts.append(tt)
089|         return rv
090| 
091|     inner.times = ts
092|     #setattr(inner, 'times', ts)
093|     return inner
094| 
095| 
096| @timer
097| def my_function (a, b, c=0):
098|     ...
099|     ...
100|     time.sleep(1.5 + (random.random() * 5))
101|     ...
102|     ...
103|     return 42
104| 
105| @timer
106| def my_other_function ():
107|     ...
108|     return
109| 
110| 
111| if __name__ == '__main__':
112|     print()
113| 
114|     for ix in range(8):
115|         print(ix, '...', sep='', end=' ')
116|         my_function("foobar", 42)
117|     print()
118| 
119|     my_other_function()
120|     print()
121| 
122|     print(f'Total Time: {sum(my_function.times)}')
123|     print(f'Max Time: {max(my_function.times)}')
124|     print(f'Min Time: {min(my_function.times)}')
125|     print()
126| 
127|     for ix,t in enumerate(my_function.times):
128|         print(f'[{ix:2d}] {t}')
129|     print()
130| 
131|     print(f'Other Times: {my_other_function.times}')
132|     print()
133| 
134| #:code::
135| #:code::
136| #:code::
137| #:code::
138| #:code::
139| #:name:: RANDOM MODULE
140| #:code::
141| from random import choice, shuffle
142| 
143| # Here are the people...
144| people = ['Alex', "Blair", 'Chris', "Drew", 'Ed', 'Fred']
145| 
146| # Only so many booths available...
147| booths = ['101', '201', '301a','301b', '401']
148| 
149| # Randomize booth order...
150| shuffle(booths)
151| 
152| # One lucky lotto ticket out of #-of-people...
153| tickets = []
154| for n in range(len(people)):
155|     a = choice("ABCD")
156|     b = choice("LMNOP")
157|     ticket = f'LX{a}{b}{n:03d}'
158|     tickets.append(ticket)
159| 
160| # Randomize ticket order...
161| shuffle(tickets)
162| 
163| # Create the group...
164| group = []
165| for member in zip(people, booths, tickets):
166|     group.append(member)
167|     print(member)
168| print()
169| 
170| #:code::
171| from random import choice, shuffle
172| 
173| # Here are the people...
174| people = ['Alex', "Blair", 'Chris', "Drew", 'Ed', 'Fred']
175| 
176| # Only so many booths available...
177| booths = ['101', '201', '301a','301b', '401']
178| 
179| # Ensure booths list is long enough...
180| while len(booths) < len(people):
181|     booths.append('--')
182| 
183| # Randomize booth order...
184| shuffle(booths)
185| 
186| # One lucky lotto ticket out of #-of-people...
187| tickets = []
188| for n in range(len(people)):
189|     ticket = f'LX{choice("ABCD")}{choice("LMNOP")}{n:03d}'
190|     tickets.append(ticket)
191| 
192| # Randomize ticket order...
193| shuffle(tickets)
194| 
195| # Create the group...
196| group = []
197| for member in zip(people, booths, tickets):
198|     group.append(member)
199| 
200| # Print the group...
201| for name,booth,ticket in group:
202|     lotto = ' Winner!' if ticket.endswith('000') else ''
203|     print(f'{name:7s} Booth: {booth:5s}{lotto}')
204| print()
205| 
206| #:code::
207| from random import choice, shuffle
208| 
209| # Here are the people...
210| people = ['Alex', "Blair", 'Chris', "Drew", 'Ed', 'Fred']
211| 
212| # Only so many booths available...
213| booths = ['101', '201', '301a','301b', '401']
214| 
215| # Ensure booths list is long enough...
216| booth_gap = len(people) - len(booths)
217| if 0 < booth_gap:
218|     blanks = ['--'] * booth_gap
219|     booths.extend(blanks)
220| 
221| # Randomize booth order...
222| shuffle(booths)
223| 
224| # One lucky lotto ticket out of #-of-people...
225| X1 = "ABCDEFG"
226| X2 = "LMNOPRS"
227| ticket = lambda n: f'LX{choice(X1)}{choice(X2)}{n:03d}'
228| 
229| tickets = [ticket(n) for n in range(len(people))]
230| 
231| # Randomize ticket order...
232| shuffle(tickets)
233| 
234| # Create the group...
235| group = list(zip(people, booths, tickets))
236| 
237| # Print the group...
238| fmt = '{0:7s} Booth: {1:5s} Ticket: {2}{3}'
239| 
240| for ix,member in enumerate(group):
241|     name, booth, ticket = member
242|     lotto = ' Winner!' if ticket.endswith('000') else ''
243|     print(fmt.format(name,booth,ticket,lotto))
244| print()
245| 
246| #:code::
247| #:code::
248| #:code::
249| #:code::
250| #:code::
251| #:name:: UTF-8 HANDLING
252| #:code::
253| def utf8_errors (s_in, codepage='cp1250', encoding='utf-8'):
254|     '''For working with UTF-8 issues.'''
255|     bs = s_in.encode(encoding)
256|     s_out = bs.decode(codepage)
257|     print()
258|     print(s_in)
259|     print()
260|     print(' '.join(['%02x'%b for b in bs]))
261|     print()
262|     print(s_out)
263|     print()
264|     return (s_out, bs, s_in)
265| 
266| def demo_utf8_errors ():
267|     # Should only be opened in IDLE (gvim will wreck Unicode strings).
268|     s1 = r'I didn’t imagine that! Or think to ask, “When was that?”'
269|     utf8_errors(s1)
270| 
271| #:code::
272| #:code::
273| #:code::
274| #:code::
275| #:code::
276| if __name__ == '__main__':
277|     print()
278|     print()
279| 
280| #:end:code::
281|