def get_dictionary(dictionary_string, state_name, value_name, required_states): """ This high level function returns a mapping from states to floating point values. Each required state is expected to have exactly one floating point value. @param dictionary_string: a string of lines of text that each look something like 'A : 0.245' @param state_name: something like 'amino acid' @param value_name: something like 'energy' @param required_states: a set of required states @return: a dictionary mapping each state to a value """ # convert the multi-line string to a list of non-empty lines lines = smallutil.get_stripped_lines(StringIO(dictionary_string)) # make sure that at least one line is non-empty if not lines: raise HandlingError('no %s to %s mapping was specified' % (key_name, value_name)) d = {} for line in lines: state, value_string = get_state_value_pair(line) if state not in required_states: raise HandlingError('invalid %s: %s' % (state_name, state)) try: value = float(value_string) except ValueError: raise HandlingError( 'this %s could not be interpreted as a number: %s' % (value_name, value_string)) d[state] = value # assert that each state was assigned a value if len(d) < len(required_states): raise HandlingError('one or more %s was not assigned a %s' % (state_name, value_name)) return d
def docstring_to_title(docstring): """ @param docstring: something like __doc__ @return: the first line of the docstring as a title, or None """ # get lines of text without whitespace between lines lines = smallutil.get_stripped_lines(StringIO(docstring)) if lines: return lines[0] else: return None
def docstring_to_html(docstring): """ Convert the docstring to an html header. @param docstring: something like __doc__ """ # get lines of text without whitespace between lines lines = smallutil.get_stripped_lines(StringIO(docstring)) # no docstring if not lines: return '' # a single line docstring if len(lines) == 1: return lines[0] # multiple lines # the first line will be separated by an empty line arr = [] arr.append(lines[0]) arr.append('<br/><br/>') arr.extend(lines[1:]) return '\n'.join(arr)
def index(self): out = StringIO() self._init_form() form_html = Form.get_html_string(self.form_objects) print >> out, '<html>' print >> out, '<head>' title = SnippetUtil.docstring_to_title(self.module.__doc__) if title: print >> out, '<title>' print >> out, title print >> out, '</title>' print >> out, FormHeaderJs.get_header_script_text( self.form_objects, self.form_presets) print >> out, '</head>' print >> out, '<body>' lines = smallutil.get_stripped_lines(StringIO(self.module.__doc__)) if lines: print >> out, lines[0] else: print >> out, gray_span('untitled') print >> out, '<br/>' print >> out, '<code>', if self.source_link: relative_link = '../' + self.source_link print >> out, '<a href="%s">source code</a>' % relative_link else: print >> out, gray_span('source code') print >> out, '</code>' print >> out, '<br/><br/>' print >> out if len(lines) > 1: print >> out, '<!-- long description -->' for line in lines[1:]: print >> out, line print >> out, '<br/><br/>' else: print >> out, '<!-- no long description available -->' print >> out print >> out, '<!-- main form -->' print >> out, '<div style="float: left;">' print >> out, '<form id="mainform" action="process" method="post">' if form_html: print >> out, form_html print >> out, '<br/><br/>' print >> out, '<input type="submit" name="submit" value="view"/>' print >> out, '<input type="submit" name="submit" value="download"/>' print >> out, '<br/>' print >> out, '</form>' print >> out, '</div>' print >> out if self.form_presets: print >> out, '<!-- preset configurations -->' print >> out, '<div style="float: left;">' print >> out, '<fieldset>' print >> out, '<legend>preset configurations</legend>' button_tags = [] default_preset = Form.get_default_preset(self.form_objects) for i, preset in enumerate([default_preset] + self.form_presets): tag = get_preset_button_tag(i, preset.description) button_tags.append(tag) print >> out, '<br/>\n'.join(button_tags) print >> out, '</fieldset>' print >> out, '</div>' print >> out print >> out, '</body>' print >> out, '</html>' return out.getvalue().rstrip()