Site Sponsors:
German Notation & Problematic File Names 
For years I have been popularizing the idea of using "German Notation." Formerly known as "Nazi Notation," far from conjuring-up the image of unpleasantness, I use the term whimsically; When one hears it, my hope is that we think more of "Sergent Schultz" (I know nothing!:) or "Colonel Klink" - rather than Adolph Hitler?


Ironically, in as much as standards like PEP-8 remind many software developers of the more dark-size of what being a "Nazi" was all about, the idea is that there can be far better ways to name things like variables and member functions?

Indeed, rather than blindly following the edicts of any dictator - benevolent or not - when using such a typeless language as Python, perhaps one might like to use Hungarian Notation? --Surely prefixing simple characters like a lower-case 's' for string, 'i' for integer ... or merely dropping vowels & double consonants ... can create far more succinctly-informative mnemonics?

Oh well - rather than memorize any type of rigidly defined view over how people should all be writing code the same way, by way of introducing a nice way to use keywords and built-ins as variable names, note our scattering of a simple "z" prefix around in the following code:

import os

count = 0
zRoot = "/"
print("Parsing", zRoot)
for zPath, zDirs, zFiles in os.walk(zRoot):
for zFile in zFiles:
file = os.path.join(zPath, zFile)
nodes = os.path.split(file)
for node in nodes:
if is_good_node_name(node) is False:
count += 1
print(file)
break


print("Found", count, "potential problems.")


Honoring the freedom to do what - IMHO - makes for a far better single-word mnemonic experience, allow me to complete the above example by presenting the balance of the code:

bad_chars = bytearray(":?*<>|\\/\"", "utf8")


def is_clascii(str_data):
for ss, ch in enumerate(str_data):
code = ord(ch)
if code < 32:
return False
if code > 126:
return False
return True


def is_good_node_name(str_data):
global bad_chars
str_data2 = bytearray(str_data, "utf8")
for ss, ch in enumerate(str_data2):
for ref in bad_chars:
if ref == ch:
return False
return is_clascii(str_data)


Designed to rummage around zRoot so as to find zFile that might create a problem for your cross-platform file-transferal activities (Linux? Windows? OS/X? AIX?,) my hope is that you will find the above Ptyhon3 example to be of use in planning escape attempts for our comrades in-arms in a 'Stalag near you!


Enjoy the Journey.

-Rn


[ view entry ] ( 437 views )   |  permalink  |  related link

<<First <Back | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | Next> Last>>