Skip to content

Commit dd88986

Browse files
committed
update mdf
1 parent 298a73e commit dd88986

File tree

4 files changed

+73
-57
lines changed

4 files changed

+73
-57
lines changed

decorator.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ __Result:__
197197
>> type(say_miau) : <class '__main__.CountCalls'>
198198
>> say_miau.__name__ : say_miau
199199
>> say_miau.__doc__ : docstring: print the vocalization of a Felis Catus, also known as cat
200-
>> say_miau.__wrapped__ : <function say_miau at 0x7fe31470a280>
200+
>> say_miau.__wrapped__ : <function say_miau at 0x7fe50c802310>
201201
```
202202

203203
Attention!
@@ -350,7 +350,7 @@ for idx in range(1, 4):
350350
__Result:__
351351

352352
```
353-
>> LimitCalls function: <function square_me at 0x7fe31470f670> max_hits: 3 log_calls: False
353+
>> LimitCalls function: <function square_me at 0x7fe50c804790> max_hits: 3 log_calls: False
354354
>> square_me type: <class '__main__._LimitCalls'>
355355
>> idx: 1
356356
>> call # 1 returns: 4
@@ -380,7 +380,7 @@ __Result:__
380380

381381
```
382382
>> LimitCalls function: None max_hits: 4 log_calls: True
383-
>> wrapper function: <function cube_me at 0x7fe31470fc10>
383+
>> wrapper function: <function cube_me at 0x7fe50c804d30>
384384
```
385385

386386
cube\_me is a variable of type \_LimitCalls
@@ -457,7 +457,7 @@ __Result:__
457457
>> wrapper function: <class '__main__.Foo'>
458458
>> Calling: Foo #call: 1 positional-arguments: keyword-arguments:
459459
>> inside Foo.__init__
460-
>> Return from: Foo #call: 1 return-value: <__main__.Foo object at 0x7fe314713100>
460+
>> Return from: Foo #call: 1 return-value: <__main__.Foo object at 0x7fe50c809160>
461461
>> do_something in Foo
462462
```
463463

@@ -627,8 +627,8 @@ for idx in range(1, 5):
627627
__Result:__
628628

629629
```
630-
>> LimitCalls2 _func: <function dec_three_from_me at 0x7fe314714af0> max_hits: 3 Log_calls: False
631-
>> LimitCalls in nested forward_func_call. func: <function dec_three_from_me at 0x7fe314714af0>
630+
>> LimitCalls2 _func: <function dec_three_from_me at 0x7fe50c80ac10> max_hits: 3 Log_calls: False
631+
>> LimitCalls in nested forward_func_call. func: <function dec_three_from_me at 0x7fe50c80ac10>
632632
>> type(dec_three_from_me) : <class 'function'>
633633
>> dec_three_from_me.__name__ : dec_three_from_me
634634
>> dec_three_from_me.__doc__ : None
@@ -673,7 +673,7 @@ __Result:__
673673

674674
```
675675
>> LimitCalls2 _func: None max_hits: 2 Log_calls: True
676-
>> LimitCalls in nested forward_func_call. func: <function dec_me at 0x7fe31471b160>
676+
>> LimitCalls in nested forward_func_call. func: <function dec_me at 0x7fe50c807280>
677677
>> idx: 1
678678
>> Calling: dec_me #call: 1 positional-arguments: 1 keyword-arguments:
679679
>> Return from: dec_me #call: 1 return-value: 0
@@ -714,7 +714,7 @@ __Result:__
714714
>> LimitCalls in nested forward_func_call. func: <class '__main__.Foo3'>
715715
>> Calling: Foo3 #call: 1 positional-arguments: keyword-arguments:
716716
>> inside Foo3.__init__
717-
>> Return from: Foo3 #call: 1 return-value: <__main__.Foo3 object at 0x7fe314719310>
717+
>> Return from: Foo3 #call: 1 return-value: <__main__.Foo3 object at 0x7fe50c8102b0>
718718
>> do_something in Foo3
719719
```
720720

@@ -745,9 +745,9 @@ __Result:__
745745

746746
```
747747
>> LimitCalls2 _func: None max_hits: 3 Log_calls: True
748-
>> LimitCalls in nested forward_func_call. func: <function Foo4.do_something at 0x7fe31471bee0>
748+
>> LimitCalls in nested forward_func_call. func: <function Foo4.do_something at 0x7fe50c816040>
749749
>> inside Foo4.__init__
750-
>> Calling: do_something #call: 1 positional-arguments: <__main__.Foo4 object at 0x7fe3147198b0> keyword-arguments:
750+
>> Calling: do_something #call: 1 positional-arguments: <__main__.Foo4 object at 0x7fe50c810850> keyword-arguments:
751751
>> do_something in Foo4
752752
>> Return from: do_something #call: 1 return-value: None
753753
```
@@ -801,8 +801,8 @@ __Result:__
801801

802802
```
803803
>> absolute of a number: 3
804-
>> random number between 0 and 1 0.007452044931850588
805-
>> random number between 0 and 1 0.9842930421729486
804+
>> random number between 0 and 1 0.25110412114316527
805+
>> random number between 0 and 1 0.28173332825716046
806806
```
807807

808808
A method that is declared with the @classmthod decorator, here the first parameter is the class object. Note that a method like this doesn't have a self parameter.
@@ -840,7 +840,7 @@ print("color red: ", colour_red , "red:", colour_red.red , "green:", colour_red.
840840
__Result:__
841841

842842
```
843-
>> color red: <__main__.Colour object at 0x7fe31472bfa0> red: 255 green: 0 blue: 0
843+
>> color red: <__main__.Colour object at 0x7fe50c823e50> red: 255 green: 0 blue: 0
844844
```
845845

846846
At first it doesn't make an awfull lot of sense, but lets derive the ColourWithAlphaChannel class from Colour.
@@ -874,7 +874,7 @@ print("color red: ", colour_red , "red:", colour_red.red , "green:", colour_red.
874874
__Result:__
875875

876876
```
877-
>> color red: <__main__.ColourWithAlphaChannel object at 0x7fe314713bb0> red: 255 green: 0 blue: 0 alpha: 1.0
877+
>> color red: <__main__.ColourWithAlphaChannel object at 0x7fe50c8107c0> red: 255 green: 0 blue: 0 alpha: 1.0
878878
```
879879

880880
Other examples of alternate constructors in the standard library:

gen-iterator.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ __Result:__
304304
```
305305
>> type(range_iter): <class 'range_iterator'>
306306
>> dir(range_iter): ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__']
307-
>> id(range_iter): 140423685359392 id(range_iter2): 140423685359488
307+
>> id(range_iter): 140222239865952 id(range_iter2): 140222239865904
308308
```
309309

310310
Returning a separate range\_iter object on each call to \_\_iter\_\_ makes sense:
@@ -407,7 +407,7 @@ print("type(no_gen_ret_val):", type(no_gen_ret_val))
407407
__Result:__
408408

409409
```
410-
>> type(not_a_generator): <function not_a_generator at 0x7fb6f20071f0>
410+
>> type(not_a_generator): <function not_a_generator at 0x7f8808f07310>
411411
>> type(no_gen_ret_val): <class 'int'>
412412
```
413413

@@ -724,10 +724,10 @@ print("inspect.getgeneratorstate(fib_ben):", inspect.getgeneratorstate(fib_gen))
724724
__Result:__
725725

726726
```
727-
>> caller of generator operating system thread_id: 4629142976
727+
>> caller of generator operating system thread_id: 4449541568
728728
>> inspect.getgeneratorstate(fib_gen): GEN_CREATED
729-
>> (generator) fib_generator operating system thread_id: 4629142976
730-
>> (generator) type(fib_gen.gi_frame): <class 'frame'> fib_gen.gi_frame: <frame at 0x7fb6efe11900, file '<string>', line 10, code fib_generator>
729+
>> (generator) fib_generator operating system thread_id: 4449541568
730+
>> (generator) type(fib_gen.gi_frame): <class 'frame'> fib_gen.gi_frame: <frame at 0x7f8808b11900, file '<string>', line 10, code fib_generator>
731731
>> (generator) fib_gen.gi_frame.f_locals: {'a': 0, 'b': 1}
732732
>> fibonacci number: 1
733733
>> (generator) fib_gen.gi_frame.f_locals: {'a': 1, 'b': 1}
@@ -884,10 +884,17 @@ __Result:__
884884

885885
```
886886
>> task_name: ' Task-1 ' tasks created
887-
>> task_name: ' find random bigger than 100 ' task returns: 194
887+
>> task_name: ' find random bigger than 100 ' sleep...
888888
>> task_name: ' find random bigger than 1000 ' sleep...
889-
>> task_name: ' find random bigger than 1000 ' task returns: 1019
890-
>> task_name: ' Task-1 ' asynccio.gather finished return value from cothreads: [194, 1019]
889+
>> task_name: ' find random bigger than 100 ' sleep...
890+
>> task_name: ' find random bigger than 1000 ' sleep...
891+
>> task_name: ' find random bigger than 100 ' sleep...
892+
>> task_name: ' find random bigger than 1000 ' sleep...
893+
>> task_name: ' find random bigger than 100 ' task returns: 133
894+
>> task_name: ' find random bigger than 1000 ' sleep...
895+
>> task_name: ' find random bigger than 1000 ' sleep...
896+
>> task_name: ' find random bigger than 1000 ' task returns: 1015
897+
>> task_name: ' Task-1 ' asynccio.gather finished return value from cothreads: [133, 1015]
891898
```
892899

893900

@@ -1077,12 +1084,12 @@ __Result:__
10771084
>> task_name: ' server task ' calling server.serve_forever()
10781085
>> task_name: ' client task ' after asyncio.sleep(1)
10791086
>> <class '__main__.TimeClientHandler'> request sent
1080-
>> <class '__main__.TimeServerProtocol'> Connection from peername: ('127.0.0.1', 63103)
10811087
>> task_name: ' client task ' enter await on_con_lost
1088+
>> <class '__main__.TimeServerProtocol'> Connection from peername: ('127.0.0.1', 61357)
10821089
>> <class '__main__.TimeServerProtocol'> Data received: type(message) <class 'str'> repr(message): 'local' eof-message
1083-
>> <class '__main__.TimeServerProtocol'> Send: Saturday 01/01/2022 07:01:07 +0200 + nanosec: 1097179725
1090+
>> <class '__main__.TimeServerProtocol'> Send: Tuesday 11/01/2022 08:16:41 +0200 + nanosec: 1097472243
10841091
>> <class '__main__.TimeServerProtocol'> Close the server socket
1085-
>> <class '__main__.TimeClientHandler'> Data received: Saturday 01/01/2022 07:01:07 +0200 + nanosec: 1097179725
1092+
>> <class '__main__.TimeClientHandler'> Data received: Tuesday 11/01/2022 08:16:41 +0200 + nanosec: 1097472243
10861093
>> <class '__main__.TimeClientHandler'> client connection lost
10871094
>> task_name: ' client task ' after await on_con_lost
10881095
>> task_name: ' client task ' closing client transport

mdpyformat/mdf.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@ def print_md(*args):
1818
paragraph = re.sub(r"^\s+","", paragraph)
1919
print(paragraph)
2020

21+
def _quote_string(str):
22+
return str.replace("<","&lt;").replace(">","&gt;")
23+
2124
def print_quoted(*args):
2225
"""show arguments as quoted text in markdown"""
23-
print("```\n" + '\n'.join(map(str, args)) + "\n```" )
26+
msg = '\n'.join(map(str, args))
27+
print("```\n" + msg + "\n```" )
28+
29+
def print_quoted_pre(*args):
30+
"""show arguments as quoted text in markdown"""
31+
msg = '\n'.join(map(str, args))
32+
print("<pre>\n" + _quote_string(msg) + "\n</pre>" )
33+
2434

2535
def print_code(*args,lang="python"):
2636
"""show arguments as quoted text in markdown"""
2737
print(f"```{lang}\n" + '\n'.join(map(str, args)) + "\n```" )
2838

29-
3039
def run_and_quote(file_name, command="python3", line_prefix="> ", exit_on_error=True):
3140
"""show contents of file name, run the file name with command and show results"""
3241
print("")
@@ -46,7 +55,7 @@ def run_and_quote(file_name, command="python3", line_prefix="> ", exit_on_error=
4655
if sline != "":
4756
print("")
4857
print("__Result:__")
49-
print_code( '\n'.join( map( lambda line : line_prefix + line, sline.split("\n") ) ) )
58+
print_quoted_pre( '\n'.join( map( lambda line : line_prefix + line, sline.split("\n") ) ) )
5059
print("")
5160

5261
if exit_on_error and cmd.exit_code != 0:

0 commit comments

Comments
 (0)