Package schrodinger :: Package application :: Package desmond :: Module util
[hide private]
[frames] | no frames]

Module util

Utility functions and classes that may make your coding life easier and cleaner.

The functions and classes fall into the following categories.

Debug and testing:

GUI programming:

Maestro programming:

File manipulation:

String manipulation:

Misc:

Copyright Schrodinger, LLC. All rights reserved.

Classes [hide private]
  TestSuite
A small and tight class designed for unit tests.
  Counter
This class was originally designed for the convenience of gridding widgets.
  InfAutofill
  EntryRestrictor
Similar to EntryFieldRestrictor, but for the 'Entry' widget instead of the 'EntryField' widget.
Functions [hide private]
 
busy_icon_exec(master, command)
Executes the 'command', waiting until it finishes and returning what is returned from the command.
 
parse_geometry(geometry)
Parses a standard X window geometry string (e.g., '35x70+0-0') to a list of integers (e.g., [35, 70, 0, 0] for the previous string).
 
resize_window(win)
Resizes the width of a given application window 'win' to its requested size.
 
resize_window2(win, should_reset_minsize=False)
Resizes a given application window 'win' to its requested size.
 
is_parent(parent, child)
Returns True if 'parent' is the parent widget of the 'child', or False otherwise.
 
get_entry_id(entry_name)
Returns the ID of an entry whose entry name being 'entry_name', or None if there is no such an entry.
 
remove_file(basename, prefix=None, suffix=None)
Deletes an existing file whose name is 'fname'.
 
write_n_ct(fname, struc)
Writes a list of CTs to a file with the name as given by 'fname'.
 
chdir(dir_name)
Changes the current directory to the one of the name 'dir_name'.
 
parent_dir(dir_name, up=1)
Returns the parent directory name.
 
relpath(xpath, refpath=None)
Given two paths ('xpath' and 'refpath'), returns the relative path of 'xpath' with respect to 'refpath'.
 
symlink(src_fname, des_fname)
Creates a symbolic link on the current directory to a file as given by 'src_fname'.
 
is_subdir(xpath, refpath=None)
Given two paths ('xpath' and 'refpath'), returns True if 'xpath' is a direct or indirect subdirectory of 'refpath'.
 
append_comment(fname, comment)
Appends a string 'comment' to a file 'fname'.
 
random_string(n, char_pool='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
Returns a random string with 'n' chars.
 
getlogin()
Returns the login name if it can be found, or otherwise 'unidentified_user'.
 
get_cmd_string(cmd)
Converts a command ('cmd') given as a list of strings to a single string representing the command in shells.
 
_strip_bracket(s)
 
expand_macro(s, macro_dict)
Replaces the macros in the string 's' using the values given by the macro dictionary 'macro_dict'.
 
print_if(condition, content)
Prints the string 'content' if 'condition' evalutes True.
 
time_duration(start_time, end_time, scale=1)
Given the start time and the end time, returns a string that says the duration between the two time points in the format of 'xh y' z"', where 'x', 'y', and 'z' are hours, minutes, and seconds, respectively.
 
get_product_info(product_name)
Returns a tuple with the following elements:
 
is_powerof2(x)
Returns True if 'x' is a power of 2, or False otherwise.
 
html_embed_image(url)
 
unique(li)
Returns a list containing unique elements from li and at the same time preserving the order of the elements as in li.
 
get_indices(slice_list, length)
Returns unique indices from a list of slice objects.
 
parse_slice(s, inclusive=False)
Parses slice expression and returns slice object.
Variables [hide private]
  _version = '$Revision: 1.42 $'
  MMSHARE_DIR = '/nfs/builds/objects/NB/2015-3/Linux-x86_64/buil...
  MMSHARE_DATA_DESMOND_DIR = '/nfs/builds/objects/NB/2015-3/Linu...
  MMSHARE_DATA_MCPRO_DIR = '/nfs/builds/objects/NB/2015-3/Linux-...
  __has_maestro = False
hash(x)
  mae = None
hash(x)
  __package__ = 'schrodinger.application.desmond'
Function Details [hide private]

busy_icon_exec(master, command)

 

Executes the 'command', waiting until it finishes and returning what is returned from the command.

During the execution process, the icon is automatically changed to the 'watch' type of icon and recover to the previous icon when the command is finished.

'command' can be either of:

  1. a string, in this case the string must be executable under a subprocess. The string normally contains the command and all necessary options, and there must be one or more spaces separating the command and options.
  2. a python function with no argument.

parse_geometry(geometry)

 

Parses a standard X window geometry string (e.g., '35x70+0-0') to a list of integers (e.g., [35, 70, 0, 0] for the previous string).

The list of integers are in the order: width, height, horizontal offset, vertical offset.

resize_window(win)

 

Resizes the width of a given application window 'win' to its requested size. The width of the window is not affected.

resize_window2(win, should_reset_minsize=False)

 

Resizes a given application window 'win' to its requested size. Both the height and width may be changed.

get_entry_id(entry_name)

 

Returns the ID of an entry whose entry name being 'entry_name', or None if there is no such an entry.

Note that if there are more than one entries have the same name of 'entry_name', the entry id of the first one will be returned.

remove_file(basename, prefix=None, suffix=None)

 

Deletes an existing file whose name is 'fname'. No effect if the file does not exist.

write_n_ct(fname, struc)

 

Writes a list of CTs to a file with the name as given by 'fname'.

The CTs in the output file are in the same order as in the list. The list can contain None elements, which will be ignored. This function has no effect if the 'struc' is an empty list or contains only Nones.

chdir(dir_name)

 

Changes the current directory to the one of the name 'dir_name'. If 'dir_name' is '..', then it will change to the parent directory, and this is is done in a portable way.

parent_dir(dir_name, up=1)

 

Returns the parent directory name.

Parameters:
  • up - This should be a non-negative integer value indicating the parent along the path. Default value is 1, indicating the immediate parent. Value 2, for example, indicates the parent of the immediate parent directory.

relpath(xpath, refpath=None)

 

Given two paths ('xpath' and 'refpath'), returns the relative path of 'xpath' with respect to 'refpath'.

Both 'xpath' and 'refpath' can be relative or absolute paths, and 'refpath' defaults to the current directory if it is not provided.

symlink(src_fname, des_fname)

 

Creates a symbolic link on the current directory to a file as given by 'src_fname'.

This differs from 'os.symlink' in that this function creates a symbolic link using a relative path. Also if there is already a file with the same name as 'des_fname', this function will try to delete the file and then create symbolic link. An exception will be raised, if this attemp fails.

Parameters:
  • src_fname - The name of the file to link to.
  • def_fname - The name to use for the symbolic link.

is_subdir(xpath, refpath=None)

 

Given two paths ('xpath' and 'refpath'), returns True if 'xpath' is a direct or indirect subdirectory of 'refpath'. Also returns True if 'xpath' and 'refpath' are the same.

Both 'xpath' and 'refpath' can be relative or absolute path, and 'refpath' defaults to the current directory if it is not provided.

append_comment(fname, comment)

 

Appends a string 'comment' to a file 'fname'. A char '#' will be automatically added to the head of the string.

Parameters:
  • comment - A string or a list of strings. If it is a list of strings, each string will be appended as a separate comment.

random_string(n, char_pool='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')

 

Returns a random string with 'n' chars. The 'n' chars will be taken from a pool of chars as given by 'char_pool'.

getlogin()

 

Returns the login name if it can be found, or otherwise 'unidentified_user'.

Note: 'os.getlogin' seems to have a bad dependency on the terminal in use, and as such exception can be thrown when the terminal does not provide the requested login information. This function is to solve this problem by trying three different ways to get the login name, and if all ways failed it will return a valid string that tells the login name can not be identified.

get_cmd_string(cmd)

 

Converts a command ('cmd') given as a list of strings to a single string representing the command in shells.

Quotes are properly added in the returned string to make it work in shells.

NOTE: This function is not needed. Use subprocess.list2cmdline() instead

expand_macro(s, macro_dict)

 

Replaces the macros in the string 's' using the values given by the macro dictionary 'macro_dict'. The expanded string will be returned.

Macro conventions:

  • All macros should start with a single '$', followed by capital letters, e.g., "$JOBNAME", "$USERNAME".
  • Optional macros should be bracketed by '[]', e.g., "myjob[_lambda$LAMBDANO]", where "$LAMBDANO" is an optional macro.
  • If optional macros are not expanded, the bracketed part of the string will be discarded.
  • Macro values should not contain the following chars: '[', ']', and '$'.

time_duration(start_time, end_time, scale=1)

 

Given the start time and the end time, returns a string that says the duration between the two time points in the format of 'xh y' z"', where 'x', 'y', and 'z' are hours, minutes, and seconds, respectively.

Parameters:
  • start_time - A time in seconds since the Epoch (i.e. a value as returned by the 'time.time()' function).
  • end_time - A time in seconds since the Epoch (i.e. a value as returned by the 'time.time()' function).

get_product_info(product_name)

 

Returns a tuple with the following elements:

  • 0 - the exec dir
  • 1 - the lib dir
  • 2 - the version number
  • 3 - the platform

All elements are strings.

get_indices(slice_list, length)

 

Returns unique indices from a list of slice objects. index that is invalid will be excluded.

parse_slice(s, inclusive=False)

 

Parses slice expression and returns slice object. Raise ValueError if invalid slice expression is passed to this function. By default, the slice expression is identical to python's which is half open. If inclusive is set to true, the slice expression will become full close one. For instance, "1:3" <==> [1, 2, 3].


Variables Details [hide private]

MMSHARE_DIR

Value:
'/nfs/builds/objects/NB/2015-3/Linux-x86_64/build-145/mmshare-v3.1'

MMSHARE_DATA_DESMOND_DIR

Value:
'/nfs/builds/objects/NB/2015-3/Linux-x86_64/build-145/mmshare-v3.1/dat\
a/desmond'

MMSHARE_DATA_MCPRO_DIR

Value:
'/nfs/builds/objects/NB/2015-3/Linux-x86_64/build-145/mmshare-v3.1/dat\
a/mcpro'