The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,837 other followers

Archive for the ‘Python’ Category

More great free Python books

Posted by jpluimers on 2020/02/20

A while ago I wrote about Until 20171201 you can get free access to “Automate the Boring Stuff with Python”?.

That book is still free. And these great books are too:

Via: [WayBack] Cracking Codes with Python by @AlSweigart teaches complete beginners how to program in Python. The book features the source code to several ciphers and … – ThisIsWhyICode – Google+


Posted in Development, Python, Scripting, Software Development | Leave a Comment »

Python – list transformation; string formatting – Stack Overflow

Posted by jpluimers on 2020/01/08

Sometimes simple examples are the best: [WayBack] Python – list transformation – Stack Overflow.

Interactive example (note you can run and save at in either [WayBack] – Python 3 or [WayBack] – Python 2; you can run but not save it at [WayBack] Welcome to interactive Python shell):

# Links the documentation are Python 2, though everything works in Python 3 as well.

x = [1,2,3,4,5,11]
print("x: ", repr(x))

y = ['01','02','03','04','05','11']
print("y: ", repr(y))

# List comprehension
# ... using `str.format()` (Python >= 2.6): and
y = ["{0:0>2}".format(v) for v in x]
print("y: ", repr(y))

# ... using the traditional `%` formatting operator (Python < 2.6): y = ["%02d" % v for v in x] print("y: ", repr(y)) # ... using the format()` function (Python >= 2.6): and
# this omits the "{0:...}" ceremony from the positional #0 parameter
y = [format(v, "0>2") for v in x]
print("y: ", repr(y))

# Note that for new style format strings, the positional argument (to specify argument #0) is optional (Python >= 2.7)
y = ["{:0>2}".format(v) for v in x]
print("y: ", repr(y))

# Using `lambda`
# ... Python < 3 return a list y = map(lambda v: "%02d" %v, x) print("y: ", repr(y)) # ... Python >= 3 return a map object to iterate over
y = list(map(lambda v: "%02d" %v, x))
print("y: ", repr(y))


Python 3 Python 2
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
x:  [1, 2, 3, 4, 5, 11]
y:  ['01', '02', '03', '04', '05', '11']
y:  ['01', '02', '03', '04', '05', '11']
y:  ['01', '02', '03', '04', '05', '11']
y:  ['01', '02', '03', '04', '05', '11']
y:  ['01', '02', '03', '04', '05', '11']
y:  <map object at 0x7fe1218200b8>
y:  ['01', '02', '03', '04', '05', '11']
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
('x: ', '[1, 2, 3, 4, 5, 11]')
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")


Read the rest of this entry »

Posted in Conference Topics, Conferences, Development, Event, Python, Scripting, Software Development | Leave a Comment »

python – Why does “return list.sort()” return None, not the list? – Stack Overflow

Posted by jpluimers on 2020/01/02

list.sort sorts the list in place, i.e. it doesn’t return a new list. Just write

return newList

The above answer is goden as performing return list.sort() bytes me often, because Python is usually an environment using the functional approach.

Answer from [WayBack] python – Why does “return list.sort()” return None, not the list? – Stack Overflow

Background information:


Posted in Development, Python, Scripting, Software Development | Leave a Comment »

Using a main check __main__ to call a main function in Python

Posted by jpluimers on 2020/01/01

[WayBack] __main__ — Top-level script environment — Python 3 documentation recommends code like this:

if __name__ == "__main__":
    # execute only if run as a script

This has many cool possibilities, including these that I like most as a beginning Python developer:

  • having your def main(): function in a separate source file
  • allowing to return prematurely from your main function (you cannot do this from the main block)
  • allowing a file to distinguish if it is being loaded as a module, or as a main program



Posted in Conference Topics, Conferences, Development, Event, Python, Scripting, Software Development | Leave a Comment »

Delphi, decoding files to strings and finding line endings: some links, some history on Windows NT and UTF/UCS encodings

Posted by jpluimers on 2019/12/31

A while back there were a few G+ threads sprouted by David Heffernan on decoding big files into line-ending splitted strings:

Code comparison:


with open(filename, 'r', encoding='utf-16-le') as f:
  for line in f:


for Line in TLineReader.FromFile(filename, TEncoding.Unicode) do

This spurred some nice observations and unfounded statements on which encodings should be used, so I posted a bit of history that is included below.

Some tips and observations from the links:

  • Good old text files are not “good” with Unicode support, neither are TextFile Device Drivers; nobody has written a driver supporting a wide range of encodings as of yet.
  • Good old text files are slow as well, even with a changed SetTextBuffer
  • When using the TStreamReader, the decoding takes much more time than the actual reading, which means that [WayBack] Faster FileStream with TBufferedFileStream • DelphiABall does not help much
  • TStringList.LoadFromFile, though fast, is a memory allocation dork and has limits on string size
  • Delphi RTL code is not what it used to be: pre-Delphi Unicode RTL code is of far better quality than Delphi 2009 and up RTL code
  • Supporting various encodings is important
  • EBCDIC days: three kinds of spaces, two kinds of hyphens, multiple codepages
  • Strings are just that: strings. It’s about the encoding from/to the file that needs to be optimal.
  • When processing large files, caching only makes sense when the file fits in memory. Otherwise caching just adds overhead.
  • On Windows, if you read a big text file into memory, open the file in “sequential read” mode, to disable caching. Use the FILE_FLAG_SEQUENTIAL_SCAN flag under Windows, as stated at [WayBack] How do FILE_FLAG_SEQUENTIAL_SCAN and FILE_FLAG_RANDOM_ACCESS affect how the operating system treats my file? – The Old New Thing
  • Python string reading depends on the way you read files (ASCII or Unicode); see [WayBack] unicode – Python codecs line ending – Stack Overflow

Though TLineReader is not part of the RTL, I think it is from [WayBack] For-in Enumeration – ADUG.

Encodings in use

It doesn’t help that on the Windows Console, various encodings are used:

Good reading here is [WayBack] c++ – What unicode encoding (UTF-8, UTF-16, other) does Windows use for its Unicode data types? – Stack Overflow

Encoding history

+A. Bouchez I’m with +David Heffernan here:

At its release in 1993, Windows NT was very early in supporting Unicode. Development of Windows NT started in 1990 where they opted for UCS-2 having 2 bytes per character and had a non-required annex on UTF-1.

UTF-1 – that later evolved into UTF-8 – did not even exist at that time. Even UCS-2 was still young: it got designed in 1989. UTF-8 was outlined late 1992 and became a standard in 1993

Some references:


Read the rest of this entry »

Posted in Delphi, Development, Encoding, PowerShell, PowerShell, Python, Scripting, Software Development, The Old New Thing, Unicode, UTF-16, UTF-8, Windows Development | Leave a Comment »

%d bloggers like this: