Exemplo n.º 1
0
	def _dorequest(self, rf, wf):
		rp = pickle.Unpickler(rf)
		try:
			request = rp.load()
		except EOFError:
			return 0
		if self._verbose > 1: print "Got request: %s" % repr(request)
		try:
			methodname, args, id = request
			if '.' in methodname:
				reply = (None, self._special(methodname, args), id)
			elif methodname[0] == '_':
				raise NameError, "illegal method name %s" % repr(methodname)
			else:
				method = getattr(self, methodname)
				reply = (None, apply(method, args), id)
		except:
			reply = (sys.exc_type, sys.exc_value, id)
		if id < 0 and reply[:2] == (None, None):
			if self._verbose > 1: print "Suppress reply"
			return 1
		if self._verbose > 1: print "Send reply: %s" % repr(reply)
		wp = pickle.Pickler(wf)
		wp.dump(reply)
		return 1
def dwg_file_collector(bldgs_dict, location=os.getcwd()):
    """
    ...
    Args:
    bldgs_dict (func) = A call to the bldgs_dict function.
    location (str) = A string representation of the directory location.

    Returns:
    dwg_bldg_code (dict) = A dictionary that contains  a list of every dwg
    per building folder.
    dwg_bldg_number (dict) = A dictionary that contains  a list of every dwg
    per building folder.
    in the subfolders.

    Examples:
    >>> dwg_file_collector(bldgs_dict('...\qryAllBldgs.xlsx'),
                           '...\CAD-to-esri-3D-Network\\floorplans')
    Executing bldgs_dict...
    CU Boulder buildings: {u'131': u'ATHN', u'133': u'TB33', ...}
    Executing dwg_file_collector...
    16 dwgs were found in: .../CAD-to-esri-3D-Network/floorplans/
    Buildings numbers dictionary: {'338': ['S-338-01-DWG-BAS.dwg',...}
    Buildings codes dictionary: {u'ADEN': ['S-339-01-DWG-BAS.dwg',...}
    """
    # getting the name of the function programatically.
    print ('Executing {}... '.format(inspect.currentframe().f_code.co_name))
    original_workspace = os.getcwd()
    # making the path compatible with python.
    location = location.replace('\\', '/') + '/'
    os.chdir(location)
    folders = [p.replace('\\', '') for p in glob.glob('*/')]
    # so the number of dwgs that were found can be reported.
    dwg_files = []
    dwg_bldg_number = {}
    dwg_bldg_code = {}
    for folder in folders:
        folder_path = ''.join([location, folder])
        os.chdir(folder_path)
        folder_dwg_files = glob.glob('*.dwg')
        # our current dwg naming convention is as follows:
        # 'bldg_number-floor_number-DWG-drawing_type (i.e.'325-01-DWG-BAS.dwg')
        # removes 'ROOF' files from the floorplans' list.
        for i, dwg in enumerate(folder_dwg_files):
            if dwg[-7:] == 'BAS.dwg' and 'ROOF' not in dwg:
                folder_dwg_files[i] = '/'.join([folder_path, dwg])
            else:
                folder_dwg_files.remove(dwg)
        # dict where the buildings' numbers are the keys.
        dwg_bldg_number[folder] = folder_dwg_files
        # dict where the buildings' codes are the keys.
        dwg_bldg_code[bldgs_dict[folder]] = folder_dwg_files
        dwg_files += folder_dwg_files
    os.chdir(original_workspace)
    print ('{} dwgs were found in: {} '.format(
        (len(dwg_files)), location))
    print('Buildings numbers dictionary: {}'.format(
        reprlib.repr(dwg_bldg_number)))
    print('Buildings codes dictionary: {}'.format(
        reprlib.repr(dwg_bldg_code)))
    return dwg_bldg_number, dwg_bldg_code
Exemplo n.º 3
0
 def _dorequest(self, rf, wf):
     rp = pickle.Unpickler(rf)
     try:
         request = rp.load()
     except EOFError:
         return 0
     if self._verbose > 1: print "Got request: %s" % repr(request)
     try:
         methodname, args, id = request
         if '.' in methodname:
             reply = (None, self._special(methodname, args), id)
         elif methodname[0] == '_':
             raise NameError, "illegal method name %s" % repr(methodname)
         else:
             method = getattr(self, methodname)
             reply = (None, apply(method, args), id)
     except:
         reply = (sys.exc_type, sys.exc_value, id)
     if id < 0 and reply[:2] == (None, None):
         if self._verbose > 1: print "Suppress reply"
         return 1
     if self._verbose > 1: print "Send reply: %s" % repr(reply)
     wp = pickle.Pickler(wf)
     wp.dump(reply)
     return 1
Exemplo n.º 4
0
 def format_stack_entry(self, frame_lineno, lprefix=': '):
     import linecache, repr
     frame, lineno = frame_lineno
     filename = self.canonic(frame.f_code.co_filename)
     s = '%s(%r)' % (filename, lineno)
     if frame.f_code.co_name:
         s = s + frame.f_code.co_name
     else:
         s = s + '<lambda>'
     if '__args__' in frame.f_locals:
         args = frame.f_locals['__args__']
     else:
         args = None
     if args:
         s = s + repr.repr(args)
     else:
         s = s + '()'
     if '__return__' in frame.f_locals:
         rv = frame.f_locals['__return__']
         s = s + '->'
         s = s + repr.repr(rv)
     line = linecache.getline(filename, lineno, frame.f_globals)
     if line:
         s = s + lprefix + line.strip()
     return s
Exemplo n.º 5
0
    def format_stack_entry(self, frame_lineno, lprefix=": "):
        import linecache, repr

        frame, lineno = frame_lineno
        filename = self.canonic(frame.f_code.co_filename)
        s = "%s(%r)" % (filename, lineno)
        if frame.f_code.co_name:
            s = s + frame.f_code.co_name
        else:
            s = s + "<lambda>"
        if "__args__" in frame.f_locals:
            args = frame.f_locals["__args__"]
        else:
            args = None
        if args:
            s = s + repr.repr(args)
        else:
            s = s + "()"
        if "__return__" in frame.f_locals:
            rv = frame.f_locals["__return__"]
            s = s + "->"
            s = s + repr.repr(rv)
        line = linecache.getline(filename, lineno)
        if line:
            s = s + lprefix + line.strip()
        return s
Exemplo n.º 6
0
def format_stack_entry(self, frame_lineno, lprefix=': '):
    import linecache, repr
    frame, lineno = frame_lineno

    filename = self.canonic(frame.f_code.co_filename)
    ## doctest hack
    if filename.startswith('<doctest'):
        lineno = frame.f_back.f_locals['example'].lineno + frame.f_back.f_locals['test'].lineno + 1
        filename = frame.f_back.f_locals['test'].filename
        s = 'doctest @ %s(%r)' % (filename, lineno)
    else:
        s = '%s(%r)' % (filename, lineno)

    if frame.f_code.co_name:
        s = s + frame.f_code.co_name
    else:
        s = s + "<lambda>"
    if '__args__' in frame.f_locals:
        args = frame.f_locals['__args__']
    else:
        args = None
    if args:
        s = s + repr.repr(args)
    else:
        s = s + '()'
    if '__return__' in frame.f_locals:
        rv = frame.f_locals['__return__']
        s = s + '->'
        s = s + repr.repr(rv)

    line = linecache.getline(filename, lineno)
    if line: s = s + lprefix + line.strip()
    return s
Exemplo n.º 7
0
 def format_stack_entry(self, frame_lineno, lprefix = ': '):
     import linecache, repr
     frame, lineno = frame_lineno
     filename = self.canonic(frame.f_code.co_filename)
     s = '%s(%r)' % (filename, lineno)
     if frame.f_code.co_name:
         s = s + frame.f_code.co_name
     else:
         s = s + '<lambda>'
     if '__args__' in frame.f_locals:
         args = frame.f_locals['__args__']
     else:
         args = None
     if args:
         s = s + repr.repr(args)
     else:
         s = s + '()'
     if '__return__' in frame.f_locals:
         rv = frame.f_locals['__return__']
         s = s + '->'
         s = s + repr.repr(rv)
     line = linecache.getline(filename, lineno, frame.f_globals)
     if line:
         s = s + lprefix + line.strip()
     return s
Exemplo n.º 8
0
 def format_stack_entry(self, frame_lineno, prefix=""):
     import linecache, repr, string
     frame, lineno = frame_lineno
     filename = frame.f_code.co_filename
     s = ' at ' + filename + ':' + `lineno`
     if frame.f_code.co_name:
         f = frame.f_code.co_name
     else:
         f = "<lambda>"
     if frame.f_locals.has_key('__args__'):
         args = frame.f_locals['__args__']
     else:
         args = None
     if args:
         a = '(' + repr.repr(args) + ')'
     else:
         a = '()'
     first_line = prefix + f + a + s + '\n'
     # Don't want the ?() at <string>:  line printed out; confuses ddd
     if first_line[:15] == '?() at <string>':
         return 'Issue "continue" command'
     second_line = `lineno` + '    ' 
     line = linecache.getline(filename, lineno)
     if line: second_line = second_line + string.strip(line)
     result = first_line + second_line
     if frame.f_locals.has_key('__return__'):
         rv = frame.f_locals['__return__']
         third_line = 'Value returned is $1 = ' + repr.repr(rv)
         result = result + '\n' + third_line
     return result
Exemplo n.º 9
0
 def format_stack_entry(self, frame_lineno, lprefix=': '):
     import repr
     frame, lineno = frame_lineno
     filename = canonic(frame.f_code.co_filename)
     s = '%s(%r)' % (filename, lineno)
     if frame.f_code.co_name:
         s += frame.f_code.co_name
     else:
         s += "<lambda>"
     locals = self.get_locals(frame)
     if '__args__' in locals:
         args = locals['__args__']
     else:
         args = None
     if args:
         s += repr.repr(args)
     else:
         s += '()'
     if '__return__' in locals:
         rv = locals['__return__']
         s += '->'
         s += repr.repr(rv)
     line = linecache.getline(filename, lineno)
     if line:
         s += lprefix + line.strip()
     return s
Exemplo n.º 10
0
 def draw(self, detail):
     import linecache, string
     d = self.win.begindrawing()
     try:
         h, v = 0, 0
         for f, lineno in self.stack:
             fn = f.f_code.co_filename
             if f is self.curframe:
                 s = '> '
             else:
                 s = '  '
             s = s + fn + '(' + ` lineno ` + ')'
             s = s + f.f_code.co_name
             if f.f_locals.has_key('__args__'):
                 args = f.f_locals['__args__']
                 if args is not None:
                     s = s + repr.repr(args)
             if f.f_locals.has_key('__return__'):
                 rv = f.f_locals['__return__']
                 s = s + '->'
                 s = s + repr.repr(rv)
             line = linecache.getline(fn, lineno)
             if line: s = s + ': ' + string.strip(line)
             d.text((h, v), s)
             v = v + d.lineheight()
     finally:
         d.close()
Exemplo n.º 11
0
	def draw(self, detail):
		import linecache, codehack, string
		d = self.win.begindrawing()
		try:
			h, v = 0, 0
			for f, lineno in self.stack:
				fn = f.f_code.co_filename
				if f is self.curframe:
					s = '> '
				else:
					s = '  '
				s = s + fn + '(' + `lineno` + ')'
				s = s + codehack.getcodename(f.f_code)
				if f.f_locals.has_key('__args__'):
					args = f.f_locals['__args__']
					if args is not None:
						s = s + repr.repr(args)
				if f.f_locals.has_key('__return__'):
					rv = f.f_locals['__return__']
					s = s + '->'
					s = s + repr.repr(rv)
				line = linecache.getline(fn, lineno)
				if line: s = s + ': ' + string.strip(line)
				d.text((h, v), s)
				v = v + d.lineheight()
		finally:
			d.close()
Exemplo n.º 12
0
 def format_stack_entry(self, frame_lineno, lprefix=': '):
     import linecache, repr
     frame, lineno = frame_lineno
     b_cls = '<module>';
     if 'self' in frame.f_locals:
         b_cls = frame.f_locals['self'].__class__.__name__
     elif 'cls' in frame.f_locals:
         b_cls = frame.f_locals['cls'].__name__
     filename = self.canonic(frame.f_code.co_filename)
     s = '%s(%r) %s:' % (filename, lineno, b_cls)
     if frame.f_code.co_name:
         s = s + frame.f_code.co_name
     else:
         s = s + "<lambda>"
     if '__args__' in frame.f_locals:
         args = frame.f_locals['__args__']
     else:
         args = None
     if args:
         s = s + repr.repr(args)
     else:
         s = s + '()'
     if '__return__' in frame.f_locals:
         rv = frame.f_locals['__return__']
         s = s + '->'
         s = s + repr.repr(rv)
     line = linecache.getline(filename, lineno, frame.f_globals)
     if line: s = s + lprefix + line.strip()
     return s
Exemplo n.º 13
0
 def format_stack_entry(self, frame_lineno, lprefix=': '):
     import linecache, repr
     frame, lineno = frame_lineno
     filename = self.pdb.canonic(frame.f_code.co_filename)
     L = [filename, lineno]
     if frame.f_code.co_name:
          L.append(frame.f_code.co_name)
     else:
         L.append("<lambda>")
     if '__args__' in frame.f_locals:
         L.append(repr.repr(frame.f_locals['__args__']))
     else:
         L.append([])
     if '__return__' in frame.f_locals:
         rv = frame.f_locals['__return__']
         L.append(repr.repr(rv))
     else:
         L.append(None)
     line = linecache.getline(filename, lineno)
     if line:
         L.append(line.strip())
     else:
         L.append('')
     L.append(self.format_namespace(frame.f_locals))
     L.append(self.format_namespace(frame.f_globals))
     return L
Exemplo n.º 14
0
 def format_stack_entry(self, frame_lineno, lprefix=': '):
     import linecache, repr
     frame, lineno = frame_lineno
     filename = self.pdb.canonic(frame.f_code.co_filename)
     L = [filename, lineno]
     if frame.f_code.co_name:
          L.append(frame.f_code.co_name)
     else:
         L.append("<lambda>")
     if '__args__' in frame.f_locals:
         L.append(repr.repr(frame.f_locals['__args__']))
     else:
         L.append([])
     if '__return__' in frame.f_locals:
         rv = frame.f_locals['__return__']
         L.append(repr.repr(rv))
     else:
         L.append(None)
     line = linecache.getline(filename, lineno)
     if line:
         L.append(line.strip())
     else:
         L.append('')
     L.append(self.format_namespace(frame.f_locals))
     L.append(self.format_namespace(frame.f_globals))
     return L
Exemplo n.º 15
0
    def format_stack_entry(self, frame_lineno, lprefix=': ', context=3):
        import linecache, repr

        ret = []

        Colors = self.color_scheme_table.active_colors
        ColorsNormal = Colors.Normal
        tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
        tpl_call = '%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
        tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
        tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
                                            ColorsNormal)

        frame, lineno = frame_lineno

        return_value = ''
        if '__return__' in frame.f_locals:
            rv = frame.f_locals['__return__']
            #return_value += '->'
            return_value += repr.repr(rv) + '\n'
        ret.append(return_value)

        #s = filename + '(' + `lineno` + ')'
        filename = self.canonic(frame.f_code.co_filename)
        link = tpl_link % filename

        if frame.f_code.co_name:
            func = frame.f_code.co_name
        else:
            func = "<lambda>"

        call = ''
        if func != '?':
            if '__args__' in frame.f_locals:
                args = repr.repr(frame.f_locals['__args__'])
            else:
                args = '()'
            call = tpl_call % (func, args)

        # The level info should be generated in the same format pdb uses, to
        # avoid breaking the pdbtrack functionality of python-mode in *emacs.
        ret.append('> %s(%s)%s\n' % (link, lineno, call))

        start = lineno - 1 - context // 2
        lines = linecache.getlines(filename)
        start = max(start, 0)
        start = min(start, len(lines) - context)
        lines = lines[start:start + context]

        for i, line in enumerate(lines):
            show_arrow = (start + 1 + i == lineno)
            ret.append(
                self.__format_line(tpl_line_em,
                                   filename,
                                   start + 1 + i,
                                   line,
                                   arrow=show_arrow))

        return ''.join(ret)
Exemplo n.º 16
0
 def __repr__(self):
     return "%s(%s, %s, %s, %s)" % (
         self.__class__.__name__,
         repr(self.type),
         self.raw_url,
         self.proxied_url,
         repr(self.resource_type),
     )
Exemplo n.º 17
0
    def format_stack_entry(self, frame_lineno, lprefix=": ", context=3):
        import linecache, repr

        ret = []

        Colors = self.color_scheme_table.active_colors
        ColorsNormal = Colors.Normal
        tpl_link = "%s%%s%s" % (Colors.filenameEm, ColorsNormal)
        tpl_call = "%s%%s%s%%s%s" % (Colors.vName, Colors.valEm, ColorsNormal)
        tpl_line = "%%s%s%%s %s%%s" % (Colors.lineno, ColorsNormal)
        tpl_line_em = "%%s%s%%s %s%%s%s" % (Colors.linenoEm, Colors.line, ColorsNormal)

        frame, lineno = frame_lineno

        return_value = ""
        if "__return__" in frame.f_locals:
            rv = frame.f_locals["__return__"]
            # return_value += '->'
            return_value += repr.repr(rv) + "\n"
        ret.append(return_value)

        # s = filename + '(' + `lineno` + ')'
        filename = self.canonic(frame.f_code.co_filename)
        link = tpl_link % filename

        if frame.f_code.co_name:
            func = frame.f_code.co_name
        else:
            func = "<lambda>"

        call = ""
        if func != "?":
            if "__args__" in frame.f_locals:
                args = repr.repr(frame.f_locals["__args__"])
            else:
                args = "()"
            call = tpl_call % (func, args)

        # The level info should be generated in the same format pdb uses, to
        # avoid breaking the pdbtrack functionality of python-mode in *emacs.
        if frame is self.curframe:
            ret.append("> ")
        else:
            ret.append("  ")
        ret.append("%s(%s)%s\n" % (link, lineno, call))

        start = lineno - 1 - context // 2
        lines = linecache.getlines(filename)
        start = max(start, 0)
        start = min(start, len(lines) - context)
        lines = lines[start : start + context]

        for i, line in enumerate(lines):
            show_arrow = start + 1 + i == lineno
            linetpl = (frame is self.curframe or show_arrow) and tpl_line_em or tpl_line
            ret.append(self.__format_line(linetpl, filename, start + 1 + i, line, arrow=show_arrow))

        return "".join(ret)
Exemplo n.º 18
0
 def __call__(self, test):
     old = self.old or test.init_old(test.cloned_library)
     legacy = self.legacy or test.init_legacy(test.cloned_library)
     oldres = getattr(old, self.func_name)(*self.args, **self.kwargs)
     newres = getattr(legacy, self.func_name)(*self.args, **self.kwargs)
     test.assertEqual(oldres, newres, 'Equivalence test for %s with args: %s and kwargs: %s failed' % (
         self.func_name, repr(self.args), repr(self.kwargs)))
     self.retval = newres
     return newres
Exemplo n.º 19
0
 def __call__(self, test):
     old = self.old or test.init_old(test.cloned_library)
     legacy = self.legacy or test.init_legacy(test.cloned_library)
     oldres = getattr(old, self.func_name)(*self.args, **self.kwargs)
     newres = getattr(legacy, self.func_name)(*self.args, **self.kwargs)
     test.assertEqual(oldres, newres, 'Equivalence test for %s with args: %s and kwargs: %s failed' % (
         self.func_name, repr(self.args), repr(self.kwargs)))
     self.retval = newres
     return newres
Exemplo n.º 20
0
    def format_stack_entry(self, frame_lineno, lprefix=': ', context=3):
        import linecache, repr

        ret = ""

        Colors = self.color_scheme_table.active_colors
        ColorsNormal = Colors.Normal
        tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
        tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
        tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
        tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
                                            ColorsNormal)

        frame, lineno = frame_lineno

        return_value = ''
        if '__return__' in frame.f_locals:
            rv = frame.f_locals['__return__']
            #return_value += '->'
            return_value += repr.repr(rv) + '\n'
        ret += return_value

        #s = filename + '(' + `lineno` + ')'
        filename = self.canonic(frame.f_code.co_filename)
        link = tpl_link % filename

        if frame.f_code.co_name:
            func = frame.f_code.co_name
        else:
            func = "<lambda>"

        call = ''
        if func != '?':
            if '__args__' in frame.f_locals:
                args = repr.repr(frame.f_locals['__args__'])
            else:
                args = '()'
            call = tpl_call % (func, args)

        level = '%s %s\n' % (link, call)
        ret += level

        start = lineno - 1 - context//2
        lines = linecache.getlines(filename)
        start = max(start, 0)
        start = min(start, len(lines) - context)
        lines = lines[start : start + context]

        for i in range(len(lines)):
            line = lines[i]
            if start + 1 + i == lineno:
                ret += self.__format_line(tpl_line_em, filename, start + 1 + i, line, arrow = True)
            else:
                ret += self.__format_line(tpl_line, filename, start + 1 + i, line, arrow = False)

        return ret
Exemplo n.º 21
0
def test():
    helper.dividing_with_title(' start ')

    # repr
    import repr
    print repr.repr(set('abcdedabc'))
    print repr.repr(dict({'name': 'wjh', 'age': 11}))

    # pprint
    import pprint
    t = [[[['black', 'cyan'], 'white', ['green', 'red']],
          [['magenta', 'yellow'], 'blue']]]
    pprint.pprint(t, None, 1, 80)

    # textwrap
    import textwrap
    doc = """The wrap() method is just like fill() except that it returns
    a list of strings instead of one big string with newlines to separate
    the wrapped lines."""
    print textwrap.fill(doc, 50)

    # locale
    import locale
    locale.setlocale(locale.LC_ALL, 'English_United States.1252')
    conv = locale.localeconv()
    x = 1234.6
    print locale.format("%d", x, grouping=True)
    print locale.format_string(
        "%s%.*f", (conv['currency_symbol'], conv['frac_digits'], x),
        grouping=True)

    # Template
    from string import Template
    t = Template('${village}folk send $$10 to $cause.')
    print t.substitute(village='Nottingham', cause='the ditch fund')

    d = dict(name='wjh', age=20)
    t = Template('name: $name and age: $age')
    print t.substitute(d)
    print t.safe_substitute(d)

    import time, os.path
    photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
    # fmt = raw_input('Enter rename style (%d-date %n-seqnum %f-format):  ')
    # print fmt

    # struct
    import struct
    data = open(helper.getfile('test.txt'), 'rb')
    print data.readline()
    data.close()
Exemplo n.º 22
0
def Test_1():
    import repr
    print repr.repr(set('supercalifragilisticexpialidocious'))

    import pprint
    t = [[[['black', 'cyan'], 'white', ['green', 'red']],
          [['magenta', 'yellow'], 'blue']]]
    pprint.pprint(t)

    import textwrap
    doc = """ The wrap() method is just like fill() except that it returns
    a list of strings instead of one big string with newlines to separate
    the wrapped lines."""

    print textwrap.fill(doc, width=40)
Exemplo n.º 23
0
 def _serve(self):
     if self._verbose: print "Wait for connection ..."
     conn, address = self._socket.accept()
     if self._verbose: print "Accepted connection from %s" % repr(address)
     if not self._verify(conn, address):
         print "*** Connection from %s refused" % repr(address)
         conn.close()
         return
     rf = conn.makefile('r')
     wf = conn.makefile('w')
     ok = 1
     while ok:
         wf.flush()
         if self._verbose > 1: print "Wait for next request ..."
         ok = self._dorequest(rf, wf)
Exemplo n.º 24
0
	def _serve(self):
		if self._verbose: print "Wait for connection ..."
		conn, address = self._socket.accept()
		if self._verbose: print "Accepted connection from %s" % repr(address)
		if not self._verify(conn, address):
			print "*** Connection from %s refused" % repr(address)
			conn.close()
			return
		rf = conn.makefile('r')
		wf = conn.makefile('w')
		ok = 1
		while ok:
			wf.flush()
			if self._verbose > 1: print "Wait for next request ..."
			ok = self._dorequest(rf, wf)
Exemplo n.º 25
0
def test():
    helper.dividing_with_title(' start ')

    # repr
    import repr
    print repr.repr(set('abcdedabc'))
    print repr.repr(dict({'name' : 'wjh', 'age' : 11}))

    # pprint
    import pprint
    t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta',  'yellow'], 'blue']]]
    pprint.pprint(t,None,1,80)

    # textwrap
    import textwrap
    doc = """The wrap() method is just like fill() except that it returns
    a list of strings instead of one big string with newlines to separate
    the wrapped lines."""
    print textwrap.fill(doc,50)

    # locale
    import locale
    locale.setlocale(locale.LC_ALL,'English_United States.1252')
    conv=locale.localeconv()
    x = 1234.6
    print locale.format("%d", x, grouping=True)
    print locale.format_string("%s%.*f", (conv['currency_symbol'],      conv['frac_digits'], x), grouping=True)

    # Template
    from string import Template
    t = Template('${village}folk send $$10 to $cause.')
    print t.substitute(village='Nottingham', cause='the ditch fund')

    d = dict(name='wjh',age=20)
    t = Template('name: $name and age: $age')
    print t.substitute(d)
    print t.safe_substitute(d)

    import time, os.path
    photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
    # fmt = raw_input('Enter rename style (%d-date %n-seqnum %f-format):  ')
    # print fmt

    # struct
    import struct
    data = open(helper.getfile('test.txt'), 'rb')
    print data.readline()
    data.close()
Exemplo n.º 26
0
 def read_chunk_length(self, inheaders, line_buf, buf, bytes_read, event):
     line = self.readline(line_buf)
     if line is None:
         return
     bytes_read[0] += len(line)
     try:
         chunk_size = int(line.strip(), 16)
     except Exception:
         return self.simple_response(
             httplib.BAD_REQUEST,
             '%s is not a valid chunk size' % reprlib.repr(line.strip()))
     if bytes_read[0] + chunk_size + 2 > self.max_request_body_size:
         return self.simple_response(
             httplib.REQUEST_ENTITY_TOO_LARGE,
             'Chunked request is larger than %d bytes' %
             self.max_request_body_size)
     if chunk_size == 0:
         self.set_state(READ,
                        self.read_chunk_separator,
                        inheaders,
                        Accumulator(),
                        buf,
                        bytes_read,
                        last=True)
     else:
         self.set_state(READ, self.read_chunk, inheaders, buf, chunk_size,
                        buf.tell() + chunk_size, bytes_read)
Exemplo n.º 27
0
 def store(self, obj):
     if self.on_store:
         self.on_store(obj)
     storage = self.storage_for(obj)
     if storage is None:
         raise RuntimeError("No suitable storage for object: %s" % reprlib.repr(obj))
     storage.store(obj)
def bldgs_dict(qryAllBldgs_location='qryAllBldgs.xlsx'):
    """
    This function reads the 'qryAllBldgs.xlsx' file and returns
    a python dictionary in which the keys are the buildings' numbers
    and the values are the buildings' codes (i.e. {325 : 'MUEN'} ).
    This allows to rename files either based on building number or
    building code. The 'qryAllBldgs.xlsx' file was exported from
    the 'LiveCASP_SpaceDatabase.mdb' access file
    ('\\Cotterpin\CASPData\Extract\LiveDatabase').
    Args:
    qryAllBldgs_location(string) = The location of the 'qryAllBldgs.xlsx' file.

    Returns:
    bldgs (dict) = A python dictionary in which the keys are the buildings'
    numbers and the values are the buildings' codes.

    Examples:
    >>> bldgs_dict()
    Executing bldgs_dict...
    CU Boulder buildings: {u'131': u'ATHN', u'133': u'TB33',...}
    """
    # getting the name of the function programatically.
    print ('Executing {}... '.format(inspect.currentframe().f_code.co_name))
    bldgs = pd.read_excel(
        qryAllBldgs_location, header=0, index_col=0).to_dict()
    # getting building codes.
    bldgs = bldgs['BuildingCode']
    print('CU Boulder buildings: {}'.format(reprlib.repr(bldgs)))
    return bldgs
Exemplo n.º 29
0
Arquivo: mfs.py Projeto: slogen/mypy
def _tracefunc_top(frame, event, arg):
    """helper function for tracing execution when debugging"""
    import repr as reprlib

    loc = frame.f_locals
    code = frame.f_code
    self = loc.get('self', None)
    do_trace = (
        True
        #and self is not None 
        #and self.__class__.__module__ in '__main__'
        and not code.co_name.startswith("_"))
    if event == 'exception':
        print 'exception: File "%s", line %s, in %s' % (
            code.co_filename, frame.f_lineno, code.co_name)
        if isinstance(arg[0], (GeneratorExit, )):
            return None
        traceback.print_exception(*arg)
    if do_trace:
        if event == 'return':
            print 'return: %s  File "%s", line %i, in %s' % (
                arg, code.co_filename, frame.f_lineno,code.co_name)
            return _tracefunc_top
        elif event == 'call':
            print 'call:  File "%s", line %i, in %s\n%s' % (
                code.co_filename, frame.f_lineno, code.co_name, 
                reprlib.repr(loc))
            return _tracefunc_top
Exemplo n.º 30
0
 def parse_part():
     line = buf.readline()
     if not line:
         raise ValueError('Premature end of message')
     if not line.startswith(b'--' + sep):
         raise ValueError('Malformed start of multipart message: %s' %
                          reprlib.repr(line))
     if line.endswith(b'--'):
         return None
     headers = read_headers(buf.readline)
     cr = headers.get('Content-Range')
     if not cr:
         raise ValueError('Missing Content-Range header in sub-part')
     if not cr.startswith('bytes '):
         raise ValueError(
             'Malformed Content-Range header in sub-part, no prefix')
     try:
         start, stop = map(
             lambda x: int(x.strip()),
             cr.partition(' ')[-1].partition('/')[0].partition('-')[::2])
     except Exception:
         raise ValueError(
             'Malformed Content-Range header in sub-part, failed to parse byte range'
         )
     content_length = stop - start + 1
     ret = buf.read(content_length)
     if len(ret) != content_length:
         raise ValueError(
             'Malformed sub-part, length of body not equal to length specified in Content-Range'
         )
     buf.readline()
     return (start, ret)
Exemplo n.º 31
0
 def _special(self, methodname, args):
     if methodname == '.methods':
         if not hasattr(self, '_methods'):
             self._methods = tuple(self._listmethods())
         return self._methods
     raise NameError, "unrecognized special method name %s" % repr(
         methodname)
Exemplo n.º 32
0
 def frameAsBag(self,frame,lineno):
     filename = self.canonic(frame.f_code.co_filename)
     result=Bag(lineno=lineno,filename=filename,functionName=frame.f_code.co_name or '"<lambda>"')
     result['locals']=Bag(dict(frame.f_locals))
     if '__return__' in frame.f_locals:
         rv = frame.f_locals['__return__']
         result['returnValue']=repr.repr(rv)
     return result
Exemplo n.º 33
0
	def format_stack_entry(self, frame_lineno):
		import codehack, linecache, repr, string
		frame, lineno = frame_lineno
		filename = frame.f_code.co_filename
		s = filename + '(' + `lineno` + ')'
		s = s + codehack.getcodename(frame.f_code)
		if frame.f_locals.has_key('__args__'):
			args = frame.f_locals['__args__']
			if args is not None:
				s = s + repr.repr(args)
		if frame.f_locals.has_key('__return__'):
			rv = frame.f_locals['__return__']
			s = s + '->'
			s = s + repr.repr(rv)
		line = linecache.getline(filename, lineno)
		if line: s = s + ': ' + string.strip(line)
		return s
Exemplo n.º 34
0
 def test_new_trait_type(self):
     tt1 = self.conn._get_or_create_trait_type("foo", 0)
     self.assertTrue(tt1.id >= 0)
     tt2 = self.conn._get_or_create_trait_type("blah", 0)
     self.assertNotEqual(tt1.id, tt2.id)
     self.assertNotEqual(tt1.desc, tt2.desc)
     # Test the method __repr__ returns a string
     self.assertTrue(repr.repr(tt2))
Exemplo n.º 35
0
 def test_new_unique(self):
     u1 = self.conn._get_or_create_unique_name("foo")
     self.assertTrue(u1.id >= 0)
     u2 = self.conn._get_or_create_unique_name("blah")
     self.assertNotEqual(u1.id, u2.id)
     self.assertNotEqual(u1.key, u2.key)
     # Test the method __repr__ returns a string
     self.assertTrue(repr.repr(u2))
Exemplo n.º 36
0
 def test_event_type_unique(self):
     et1 = self.conn._get_or_create_event_type("foo")
     self.assertTrue(et1.id >= 0)
     et2 = self.conn._get_or_create_event_type("blah")
     self.assertNotEqual(et1.id, et2.id)
     self.assertNotEqual(et1.desc, et2.desc)
     # Test the method __repr__ returns a string
     self.assertTrue(repr.repr(et2))
Exemplo n.º 37
0
 def test_new_unique(self):
     u1 = self.conn._get_or_create_unique_name("foo")
     self.assertTrue(u1.id >= 0)
     u2 = self.conn._get_or_create_unique_name("blah")
     self.assertNotEqual(u1.id, u2.id)
     self.assertNotEqual(u1.key, u2.key)
     # Test the method __repr__ returns a string
     self.assertTrue(repr.repr(u2))
Exemplo n.º 38
0
 def test_event_type_unique(self):
     et1 = self.event_conn._get_or_create_event_type("foo")
     self.assertTrue(et1.id >= 0)
     et2 = self.event_conn._get_or_create_event_type("blah")
     self.assertNotEqual(et1.id, et2.id)
     self.assertNotEqual(et1.desc, et2.desc)
     # Test the method __repr__ returns a string
     self.assertTrue(repr.repr(et2))
Exemplo n.º 39
0
 def test_new_trait_type(self):
     tt1 = self.event_conn._get_or_create_trait_type("foo", 0)
     self.assertTrue(tt1.id >= 0)
     tt2 = self.event_conn._get_or_create_trait_type("blah", 0)
     self.assertNotEqual(tt1.id, tt2.id)
     self.assertNotEqual(tt1.desc, tt2.desc)
     # Test the method __repr__ returns a string
     self.assertTrue(repr.repr(tt2))
Exemplo n.º 40
0
 def resultfn(self, txn, results):
     """
     Retrieve the channel and the Client's txn information using
     our Map/Reduce Transaction's txn, and send the results back to
     the Client.
     """
     client = self.inflight.pop(txn, None)
     if client is not None:
         # Got results (they may be empty, but this is a valid result)
         logging.warning("%s Map/Reduce txn %s: results via %s: %s" % (
                 self.name(), txn, client['chan'].name(),
                 repr.repr(results)))
         client['chan'].send_command( client['command'] + "done", data=results,
                                      txn=client['txn'] )
     else:
         logging.error("%s unable to identify txn %s results in %s: %s" % (
                 self.name(), txn, repr.repr(self.inflight),
                 repr.repr(results)))
 def test_trait_different_data_type(self):
     tt1 = self.conn._get_or_create_trait_type("foo", 0)
     self.assertTrue(tt1.id >= 0)
     tt2 = self.conn._get_or_create_trait_type("foo", 1)
     self.assertNotEqual(tt1.id, tt2.id)
     self.assertEqual(tt2.desc, tt1.desc)
     self.assertNotEqual(tt1.data_type, tt2.data_type)
     # Test the method __repr__ returns a string
     self.assertTrue(repr.repr(tt2))
Exemplo n.º 42
0
def prints(*args, **kwargs):
    '''
    Print unicode arguments safely by encoding them to preferred_encoding
    Has the same signature as the print function from Python 3, except for the
    additional keyword argument safe_encode, which if set to True will cause the
    function to use repr when encoding fails.
    '''
    file = kwargs.get('file', sys.stdout)
    sep = kwargs.get('sep', ' ')
    end = kwargs.get('end', '\n')
    enc = preferred_encoding
    safe_encode = kwargs.get('safe_encode', False)
    if 'CALIBRE_WORKER' in os.environ:
        enc = 'utf-8'
    for i, arg in enumerate(args):
        if isinstance(arg, unicode):
            if iswindows:
                from calibre.utils.terminal import Detect
                cs = Detect(file)
                if cs.is_console:
                    cs.write_unicode_text(arg)
                    if i != len(args) - 1:
                        file.write(bytes(sep))
                    continue
            try:
                arg = arg.encode(enc)
            except UnicodeEncodeError:
                try:
                    arg = arg.encode('utf-8')
                except:
                    if not safe_encode:
                        raise
                    arg = repr(arg)
        if not isinstance(arg, str):
            try:
                arg = str(arg)
            except ValueError:
                arg = unicode(arg)
            if isinstance(arg, unicode):
                try:
                    arg = arg.encode(enc)
                except UnicodeEncodeError:
                    try:
                        arg = arg.encode('utf-8')
                    except:
                        if not safe_encode:
                            raise
                        arg = repr(arg)

        try:
            file.write(arg)
        except:
            import repr as reprlib
            file.write(reprlib.repr(arg))
        if i != len(args) - 1:
            file.write(bytes(sep))
    file.write(bytes(end))
Exemplo n.º 43
0
def client(credentials):
    logging.debug("  socket_map at client startup: %s" %
                  (repr.repr(asyncore.socket_map)))
    c = mincemeat.Client()
    c.conn(**credentials)
    # Client communications with Server done; either server completed
    # success, or exited without completing our authentication.
    if not c.authenticated():
        raise Exception("No server authenticated!")
Exemplo n.º 44
0
 def test_trait_different_data_type(self):
     tt1 = self.event_conn._get_or_create_trait_type("foo", 0)
     self.assertTrue(tt1.id >= 0)
     tt2 = self.event_conn._get_or_create_trait_type("foo", 1)
     self.assertNotEqual(tt1.id, tt2.id)
     self.assertEqual(tt2.desc, tt1.desc)
     self.assertNotEqual(tt1.data_type, tt2.data_type)
     # Test the method __repr__ returns a string
     self.assertTrue(repr.repr(tt2))
Exemplo n.º 45
0
 def user_return(self, frame, return_value):
     # called when a return trap is set here
     frame.f_locals['__return__'] = return_value
     if frame.f_code.co_name:
         func = frame.f_code.co_name
     else:
         func = "<lambda>"
     self.set_status(func + " rerturned " + repr.repr(return_value))
     self.interaction(frame, None)
Exemplo n.º 46
0
 def user_return(self, frame, return_value):
     # called when a return trap is set here
     frame.f_locals['__return__'] = return_value
     if frame.f_code.co_name:
         func = frame.f_code.co_name
     else:
         func = "<lambda>"
     self.set_status(func + " returned " + repr.repr(return_value))
     self.interaction(frame, None)
Exemplo n.º 47
0
def client( credentials ):
    logging.debug( "  socket_map at client startup: %s" % (
            repr.repr( asyncore.socket_map )))
    c = mincemeat.Client()
    c.conn( **credentials )
    # Client communications with Server done; either server completed
    # success, or exited without completing our authentication.
    if not c.authenticated():
        raise Exception( "No server authenticated!" )
Exemplo n.º 48
0
def prints(*args, **kwargs):
    '''
    Print unicode arguments safely by encoding them to preferred_encoding
    Has the same signature as the print function from Python 3, except for the
    additional keyword argument safe_encode, which if set to True will cause the
    function to use repr when encoding fails.
    '''
    file = kwargs.get('file', sys.stdout)
    sep  = kwargs.get('sep', ' ')
    end  = kwargs.get('end', '\n')
    enc = preferred_encoding
    safe_encode = kwargs.get('safe_encode', False)
    if 'CALIBRE_WORKER' in os.environ:
        enc = 'utf-8'
    for i, arg in enumerate(args):
        if isinstance(arg, unicode):
            if iswindows:
                from calibre.utils.terminal import Detect
                cs = Detect(file)
                if cs.is_console:
                    cs.write_unicode_text(arg)
                    if i != len(args)-1:
                        file.write(bytes(sep))
                    continue
            try:
                arg = arg.encode(enc)
            except UnicodeEncodeError:
                try:
                    arg = arg.encode('utf-8')
                except:
                    if not safe_encode:
                        raise
                    arg = repr(arg)
        if not isinstance(arg, str):
            try:
                arg = str(arg)
            except ValueError:
                arg = unicode(arg)
            if isinstance(arg, unicode):
                try:
                    arg = arg.encode(enc)
                except UnicodeEncodeError:
                    try:
                        arg = arg.encode('utf-8')
                    except:
                        if not safe_encode:
                            raise
                        arg = repr(arg)

        try:
            file.write(arg)
        except:
            import repr as reprlib
            file.write(reprlib.repr(arg))
        if i != len(args)-1:
            file.write(bytes(sep))
    file.write(bytes(end))
Exemplo n.º 49
0
 def resultfn(self, txn, results):
     """
     Retrieve the channel and the Client's txn information using
     our Map/Reduce Transaction's txn, and send the results back to
     the Client.
     """
     client = self.inflight.pop(txn, None)
     if client is not None:
         # Got results (they may be empty, but this is a valid result)
         logging.warning(
             "%s Map/Reduce txn %s: results via %s: %s" %
             (self.name(), txn, client['chan'].name(), repr.repr(results)))
         client['chan'].send_command(client['command'] + "done",
                                     data=results,
                                     txn=client['txn'])
     else:
         logging.error("%s unable to identify txn %s results in %s: %s" %
                       (self.name(), txn, repr.repr(
                           self.inflight), repr.repr(results)))
Exemplo n.º 50
0
 def update_var_listing(self):
     self.vardisp.freeze()
     self.vardisp.clear()
     locals = self.curframe.f_locals
     self.vardisp.varnames = locals.keys()
     self.vardisp.varnames.sort()
     for var in self.vardisp.varnames:
         row = [var, type(locals[var]).__name__, repr.repr(locals[var])]
         self.vardisp.append(row)
     self.vardisp.thaw()
Exemplo n.º 51
0
 def run(self):
     try:
         self.connect()
         while True:
             func, args, kwargs = self.requests.get()
             if func == self.CLOSE:
                 self.conn.close()
                 break
             if func == 'dump':
                 try:
                     ok, res = True, tuple(self.conn.iterdump())
                 except Exception as err:
                     ok, res = False, (err, traceback.format_exc())
             elif func == 'create_dynamic_filter':
                 try:
                     f = DynamicFilter(args[0])
                     self.conn.create_function(args[0], 1, f)
                     ok, res = True, f
                 except Exception as err:
                     ok, res = False, (err, traceback.format_exc())
             else:
                 bfunc = getattr(self.conn, func)
                 try:
                     for i in range(3):
                         try:
                             ok, res = True, bfunc(*args, **kwargs)
                             break
                         except OperationalError as err:
                             # Retry if unable to open db file
                             e = str(err)
                             if 'unable to open' not in e or i == 2:
                                 if 'unable to open' in e:
                                     prints(
                                         'Unable to open database for func',
                                         func, reprlib.repr(args),
                                         reprlib.repr(kwargs))
                                 raise
                         time.sleep(0.5)
                 except Exception as err:
                     ok, res = False, (err, traceback.format_exc())
             self.results.put((ok, res))
     except Exception as err:
         self.unhandled_error = (err, traceback.format_exc())
Exemplo n.º 52
0
 def run(self):
     try:
         self.connect()
         while True:
             func, args, kwargs = self.requests.get()
             if func == self.CLOSE:
                 self.conn.close()
                 break
             if func == 'dump':
                 try:
                     ok, res = True, tuple(self.conn.iterdump())
                 except Exception as err:
                     ok, res = False, (err, traceback.format_exc())
             elif func == 'create_dynamic_filter':
                 try:
                     f = DynamicFilter(args[0])
                     self.conn.create_function(args[0], 1, f)
                     ok, res = True, f
                 except Exception as err:
                     ok, res = False, (err, traceback.format_exc())
             else:
                 bfunc = getattr(self.conn, func)
                 try:
                     for i in range(3):
                         try:
                             ok, res = True, bfunc(*args, **kwargs)
                             break
                         except OperationalError as err:
                             # Retry if unable to open db file
                             e = str(err)
                             if 'unable to open' not in e or i == 2:
                                 if 'unable to open' in e:
                                     prints('Unable to open database for func',
                                         func, reprlib.repr(args),
                                         reprlib.repr(kwargs))
                                 raise
                         time.sleep(0.5)
                 except Exception as err:
                     ok, res = False, (err, traceback.format_exc())
             self.results.put((ok, res))
     except Exception as err:
         self.unhandled_error = (err, traceback.format_exc())
Exemplo n.º 53
0
 def update_var_listing(self):
     model = self.vardisp.get_model()
     model.clear()
     locals = self.curframe.f_locals
     self.vardisp.varnames = locals.keys()
     self.vardisp.varnames.sort()
     for var in self.vardisp.varnames:
         row = [var, type(locals[var]).__name__, repr.repr(locals[var])]
         model.append(row)
     self.vardisp.get_selection().select_path(0)
     return
Exemplo n.º 54
0
	def refreshframe(self):
		import repr
		del self.displaylist[3:]
		self.re_eval()
		names = self.dict.keys()
		for key, label in ('__args__', 'Args: '), \
				  ('__return__', 'Return: '):
			if self.dict.has_key(key):
				names.remove(key)
				value = self.dict[key]
				label = label + repr.repr(value)
			self.displaylist.append(label)
		names.sort()
		for name in names:
			value = self.dict[name]
			line = name + ' = ' + repr.repr(value)
			self.displaylist.append(line)
		self.win.setdocsize(0, \
			stdwin.lineheight() * len(self.displaylist))
		self.refreshall() # XXX Be more subtle later
Exemplo n.º 55
0
def _prepare_path(path):
    if not isinstance(path, unicode):
        try:
            path = json.loads(b'"%s"' % path)
        except ValueError:
            raise PointerDecodeException(u"Could not decode string path %s" %
                                         repr.repr(path))
    if path.startswith(u'#'):
        # TODO: does unquote have a problem with unicode objects??
        path = urllib.unquote(path[1:])

    return [unescape(part) for part in path.split(u'/')]