Site Sponsors:
ASCII Strings in Python 3 
Since Python decided to convert to Unicode, the default string encoding is no longer ASCII.

In as much as the vast majority of the world is still working with ASCII text however, lots of people are being frustrated.

In an effort to stem the frustration, allow me to share an example. Written in Python3, feel free to use this little ditty to inspire-power your own (whatever) to ASCII filtering & conversions:


def mk_printable(str_data):
result = bytearray(str_data, "utf-8")
for ss, code in enumerate(result):
if code < 32:
result[ss] = 0x20
continue
if code > 126:
result[ss] = 0x20
continue
return str(result, "utf-8")


In addition to converting a "whatever" string to UTF-8, note that the above routine will also map non-ascii encodings to an ASCII 'SPACE' character (0x20 in the above.)

We also leave the final conversion to UTF8 to Python's str() built-in function.


Enjoy the journey!

-Rn



[ add comment ] ( 313 views )   |  permalink
Staples Asks: "Do you have a surprising tip for improving productivity?" 
I wish I was the first one to note it, but it was Thomas Monson who brought the idea that "when performance is measured, performance improves" into my bailiwick.

Yet to force people to be more obsessed with recording what they do is one very sure way to demoralize & intimidate just about everyone!

But to answer the question of how to improve productivity, on a PERSONAL level I have discovered that privately micro-blogging about what I do every day is the best way to ensure that I am always productive. Over time, voluntarily choosing to record what I do not only encourages me to do many more things, but in the technical world using tools like the "About Time" software allows me to diagnose though technical problems, as well as to generate the related reporting.

You can read more about "About Time" on the download page.

[ add comment ] ( 387 views )   |  permalink  |  related link
Fiark: Unicode & Valid File / Folder Names 
Sitting down to update Fiark this weekend, once again I came across the notion of detecting invalid directory & filenames. With a heavy sigh, I realize the advent of Unicode meant that there would be far, far more to check for than the usual "{}", "]", "/", "\", "&", "*" and others from our ASCII / UTF8 past.

If you have run across a similar problem, then you probably need no further explanation of the problem domain. Submitted for your fair use enjoyment therefore, here is what will be coming soon to a Fiark near you:


package com.soft9000.file;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author profnagy
*/
public class LiveNodeName {

private String nodePathName = "";
private boolean isDirectory = false;

public LiveNodeName() {

}

public LiveNodeName(String nodePathName, boolean isDirectory) {
this.nodePathName = nodePathName;
this.isDirectory = isDirectory;

}

public String getName() {
return this.nodePathName;
}

public boolean isDirectory() {
return this.isDirectory;
}

public boolean isNull() {
return nodePathName.isEmpty();
}

public void asNull() {
this.nodePathName = "";
}

public static List<LiveNodeName> GetNodes(final LiveNodeName ref) {
String zName = ref.getName();
if (com.soft9000.Text.Contains(zName, "\\".getBytes())) {
zName = com.soft9000.Text.ReplaceAll(zName, "\\", "/"); // No RE's
}
while (zName.startsWith(".")) {
zName = zName.substring(1);
}
while (zName.startsWith("/")) {
zName = zName.substring(1);
}
String[] array = com.soft9000.Text.Split(zName, '/');
List<LiveNodeName> results = new ArrayList<LiveNodeName>();
for (String leaf : array) {
results.add(new LiveNodeName(leaf, true));
}
if (results.size() > 0 && ref.isDirectory == false) {
results.get(results.size() - 1).isDirectory = false;
}
return results;
}

/**
* With the advent of Unicode, the range of valid / invalid file names has
* become truly infinite! Rather than crafting a plethora of OS-centric RE
* tests, surely the best strategy is to simply let the operating system
* itself tell us the final score?
*
* @param ref a LiveNodeName
* @return True if the node can be hosted on this O.S!
*/
public static boolean CanCreate(final LiveNodeName nodei) {
if (nodei == null || nodei.isNull()) {
return false;
}
List<LiveNodeName> array = LiveNodeName.GetNodes(nodei);
try {
for (LiveNodeName oneSeg : array) {
if (oneSeg.isNull()) {
return false;
}
if (oneSeg.isDirectory) {
Directory dir = new Directory(oneSeg.getName());
if (dir.exists()) {
return true;
}
if (dir.mkdir() == true) {
return dir.delete();
}
} else {
File file = new File(oneSeg.nodePathName);
if (file.exists()) {
return true;
}
File pFile = file.getParentFile();
if (pFile == null) {
if (file.createNewFile() == true) {
file.delete();
return true;
}
}
boolean bShoulRemove = false;
Directory dir = new Directory(pFile);
if (dir.exists() == false) {
if (dir.mkdir() == false) {
return false;
}
bShoulRemove = true;
}
if (file.createNewFile() == true) {
if (file.delete() == false) {
return false;
}
if (bShoulRemove) {
return dir.delete();
} else {
return true;
}
}
}
}
} catch (Exception ex) {
}

return false;
}

/**
* Check a list of LiveNodeName, assigning any error-nodes to the result
* list.
*
* @param input List for LiveNodeName to test.
* @param results List of invalid LiveNodeName (rejects.)
* @return True of all NodeNames are okay - false if there were rejects.
*/
public static boolean CanCreate(final List<LiveNodeName> input, List<LiveNodeName> results) {
boolean br = true;
for (LiveNodeName ref : input) {
if (CanCreate(ref) == false) {
results.add(ref);
br = false;
}
}
return br;
}

/**
* For my problem domain (ONLY :)
* @param foo
*/
public static void main(String... foo) {
LiveNodeName node = new LiveNodeName("/foo/bar", true);
if (CanCreate(node) == false) {
System.out.println("Error 1000001!");
}
node = new LiveNodeName("foo", true);
if (CanCreate(node) == false) {
System.out.println("Error 1000002!");
}
node = new LiveNodeName("foo", false);
if (CanCreate(node) == false) {
System.out.println("Error 1000003!");
}
node = new LiveNodeName("/foo/bar.txt", false);
if (CanCreate(node) == false) {
System.out.println("Error 1000004!");
}
node = new LiveNodeName("/foo/\\bar.txt", false);
if (CanCreate(node) == false) {
System.out.println("Error 1000005!");
}
}
}


Enjoy the journey!

-Randall


[ add comment ] ( 316 views )   |  permalink  |  related link

<<First <Back | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | Next> Last>>