gdesk.utils.names module

Provide OrderedStruct and DictStruct classes.

Also, provide two utility functions: current_code_path() and force2bytes().

class gdesk.utils.names.DictStruct(dictionary=None)

Bases: object

Object that mimics a classic structure type for holding data.

You can consider it to be a dict with all the key/value pairs as object attributes, making it easier to do auto-completion on.

Regular dict methods such as keys(), values() and get() are all present.

Usage: .. code :: Python

from iskutil.names import DictStruct d = DictStruct(x=50) d.a = ‘new attribute a on first level’ d.b = 508 print(d.a, d.b)

class gdesk.utils.names.OrderedStruct(*args, **argv)

Bases: object

Object that mimics a classic structure type for holding data and retains the order of the elements.

You can think of this as an ordered DictStruct, but the name is ‘OrderedStruct’ because that is shorter ;-)

All the methods of the dict class are supported, but they will return their values NOT ordered. Instead, call _keys(), _values(), _items() and friends prepended with an underscore.

Usage: .. code :: Python

os = OrderedStruct(a=20) os.b = ‘new attribute b’ [key_value for key_value in os] >>> [(‘a’, 20), (‘b’, ‘new attribute b’)]

gdesk.utils.names.addprop(inst, name, getter, setter=None, doc=None)
gdesk.utils.names.current_code_path()

Return the path of the caller’s source code file.

If the caller is the shell (interactive scripting), None is returned.

Returns

Path object or None.

Example:

# works only when used from a .py file, not when used # in a shell (on standard input): from iskutil.names import current_code_path folder = current_code_path().parent

# in a shell this is what happens: current_code_path() is None >>> True

gdesk.utils.names.find_names(obj)
gdesk.utils.names.force2bytes(s)

Convert the given string to bytes by encoding it.

If the given argument is not a string (already bytes?), then return it verbatim.

Parameters

s – Data to encode in bytes, or any other object.

Returns

Bytes-encoded variant of the string, or the given argument verbatim.

gdesk.utils.names.fromfilename(fileName)

parse a filename to a valid identifier extension is removed