INSTRUCTION
stringlengths
1
8.43k
RESPONSE
stringlengths
75
104k
Return dct [ key ] and delete dct [ key ].
def popkey(dct,key,default=NotGiven): """Return dct[key] and delete dct[key]. If default is given, return it if dct[key] doesn't exist, otherwise raise KeyError. """ try: val = dct[key] except KeyError: if default is NotGiven: raise else: return default else: del dct[key] return val
Show all figures as SVG/ PNG payloads sent to the IPython clients.
def show(close=None): """Show all figures as SVG/PNG payloads sent to the IPython clients. Parameters ---------- close : bool, optional If true, a ``plt.close('all')`` call is automatically issued after sending all the figures. If this is set, the figures will entirely removed from the internal list of figures. """ if close is None: close = InlineBackend.instance().close_figures try: for figure_manager in Gcf.get_all_fig_managers(): send_figure(figure_manager.canvas.figure) finally: show._to_draw = [] if close: matplotlib.pyplot.close('all')
Is called after every pylab drawing command
def draw_if_interactive(): """ Is called after every pylab drawing command """ # signal that the current active figure should be sent at the end of # execution. Also sets the _draw_called flag, signaling that there will be # something to send. At the end of the code execution, a separate call to # flush_figures() will act upon these values fig = Gcf.get_active().canvas.figure # Hack: matplotlib FigureManager objects in interacive backends (at least # in some of them) monkeypatch the figure object and add a .show() method # to it. This applies the same monkeypatch in order to support user code # that might expect `.show()` to be part of the official API of figure # objects. # For further reference: # https://github.com/ipython/ipython/issues/1612 # https://github.com/matplotlib/matplotlib/issues/835 if not hasattr(fig, 'show'): # Queue up `fig` for display fig.show = lambda *a: send_figure(fig) # If matplotlib was manually set to non-interactive mode, this function # should be a no-op (otherwise we'll generate duplicate plots, since a user # who set ioff() manually expects to make separate draw/show calls). if not matplotlib.is_interactive(): return # ensure current figure will be drawn, and each subsequent call # of draw_if_interactive() moves the active figure to ensure it is # drawn last try: show._to_draw.remove(fig) except ValueError: # ensure it only appears in the draw list once pass # Queue up the figure for drawing in next show() call show._to_draw.append(fig) show._draw_called = True
Send all figures that changed
def flush_figures(): """Send all figures that changed This is meant to be called automatically and will call show() if, during prior code execution, there had been any calls to draw_if_interactive. This function is meant to be used as a post_execute callback in IPython, so user-caused errors are handled with showtraceback() instead of being allowed to raise. If this function is not called from within IPython, then these exceptions will raise. """ if not show._draw_called: return if InlineBackend.instance().close_figures: # ignore the tracking, just draw and close all figures try: return show(True) except Exception as e: # safely show traceback if in IPython, else raise try: get_ipython except NameError: raise e else: get_ipython().showtraceback() return try: # exclude any figures that were closed: active = set([fm.canvas.figure for fm in Gcf.get_all_fig_managers()]) for fig in [ fig for fig in show._to_draw if fig in active ]: try: send_figure(fig) except Exception as e: # safely show traceback if in IPython, else raise try: get_ipython except NameError: raise e else: get_ipython().showtraceback() break finally: # clear flags for next round show._to_draw = [] show._draw_called = False
Draw the given figure and send it as a PNG payload.
def send_figure(fig): """Draw the given figure and send it as a PNG payload. """ fmt = InlineBackend.instance().figure_format data = print_figure(fig, fmt) # print_figure will return None if there's nothing to draw: if data is None: return mimetypes = { 'png' : 'image/png', 'svg' : 'image/svg+xml' } mime = mimetypes[fmt] # flush text streams before sending figures, helps a little with output # synchronization in the console (though it's a bandaid, not a real sln) sys.stdout.flush(); sys.stderr.flush() publish_display_data( 'IPython.zmq.pylab.backend_inline.send_figure', {mime : data} )
Load an IPython extension by its module name.
def load_extension(self, module_str): """Load an IPython extension by its module name. If :func:`load_ipython_extension` returns anything, this function will return that object. """ from IPython.utils.syspathcontext import prepended_to_syspath if module_str not in sys.modules: with prepended_to_syspath(self.ipython_extension_dir): __import__(module_str) mod = sys.modules[module_str] return self._call_load_ipython_extension(mod)
Unload an IPython extension by its module name.
def unload_extension(self, module_str): """Unload an IPython extension by its module name. This function looks up the extension's name in ``sys.modules`` and simply calls ``mod.unload_ipython_extension(self)``. """ if module_str in sys.modules: mod = sys.modules[module_str] self._call_unload_ipython_extension(mod)
Download and install an IPython extension. If filename is given the file will be so named ( inside the extension directory ). Otherwise the name from the URL will be used. The file must have a. py or. zip extension ; otherwise a ValueError will be raised. Returns the full path to the installed file.
def install_extension(self, url, filename=None): """Download and install an IPython extension. If filename is given, the file will be so named (inside the extension directory). Otherwise, the name from the URL will be used. The file must have a .py or .zip extension; otherwise, a ValueError will be raised. Returns the full path to the installed file. """ # Ensure the extension directory exists if not os.path.isdir(self.ipython_extension_dir): os.makedirs(self.ipython_extension_dir, mode = 0777) if os.path.isfile(url): src_filename = os.path.basename(url) copy = copyfile else: src_filename = urlparse(url).path.split('/')[-1] copy = urlretrieve if filename is None: filename = src_filename if os.path.splitext(filename)[1] not in ('.py', '.zip'): raise ValueError("The file must have a .py or .zip extension", filename) filename = os.path.join(self.ipython_extension_dir, filename) copy(url, filename) return filename
Find any svn: externals directories
def externals_finder(dirname, filename): """Find any 'svn:externals' directories""" found = False f = open(filename,'rt') for line in iter(f.readline, ''): # can't use direct iter! parts = line.split() if len(parts)==2: kind,length = parts data = f.read(int(length)) if kind=='K' and data=='svn:externals': found = True elif kind=='V' and found: f.close() break else: f.close() return for line in data.splitlines(): parts = line.split() if parts: yield joinpath(dirname, parts[0])
Generate a list of n random ports near the given port.
def random_ports(port, n): """Generate a list of n random ports near the given port. The first 5 ports will be sequential, and the remaining n-5 will be randomly selected in the range [port-2*n, port+2*n]. """ for i in range(min(5, n)): yield port + i for i in range(n-5): yield port + random.randint(-2*n, 2*n)
initialize tornado webapp and httpserver
def init_webapp(self): """initialize tornado webapp and httpserver""" self.web_app = NotebookWebApplication( self, self.kernel_manager, self.notebook_manager, self.cluster_manager, self.log, self.base_project_url, self.webapp_settings ) if self.certfile: ssl_options = dict(certfile=self.certfile) if self.keyfile: ssl_options['keyfile'] = self.keyfile else: ssl_options = None self.web_app.password = self.password self.http_server = httpserver.HTTPServer(self.web_app, ssl_options=ssl_options) if ssl_options is None and not self.ip and not (self.read_only and not self.password): self.log.critical('WARNING: the notebook server is listening on all IP addresses ' 'but not using any encryption or authentication. This is highly ' 'insecure and not recommended.') success = None for port in random_ports(self.port, self.port_retries+1): try: self.http_server.listen(port, self.ip) except socket.error, e: if e.errno != errno.EADDRINUSE: raise self.log.info('The port %i is already in use, trying another random port.' % port) else: self.port = port success = True break if not success: self.log.critical('ERROR: the notebook server could not be started because ' 'no available port could be found.') self.exit(1)
SIGINT handler spawns confirmation dialog
def _handle_sigint(self, sig, frame): """SIGINT handler spawns confirmation dialog""" # register more forceful signal handler for ^C^C case signal.signal(signal.SIGINT, self._signal_stop) # request confirmation dialog in bg thread, to avoid # blocking the App thread = threading.Thread(target=self._confirm_exit) thread.daemon = True thread.start()
confirm shutdown on ^C A second ^C or answering y within 5s will cause shutdown otherwise original SIGINT handler will be restored. This doesn t work on Windows.
def _confirm_exit(self): """confirm shutdown on ^C A second ^C, or answering 'y' within 5s will cause shutdown, otherwise original SIGINT handler will be restored. This doesn't work on Windows. """ # FIXME: remove this delay when pyzmq dependency is >= 2.1.11 time.sleep(0.1) sys.stdout.write("Shutdown Notebook Server (y/[n])? ") sys.stdout.flush() r,w,x = select.select([sys.stdin], [], [], 5) if r: line = sys.stdin.readline() if line.lower().startswith('y'): self.log.critical("Shutdown confirmed") ioloop.IOLoop.instance().stop() return else: print "No answer for 5s:", print "resuming operation..." # no answer, or answer is no: # set it back to original SIGINT handler # use IOLoop.add_callback because signal.signal must be called # from main thread ioloop.IOLoop.instance().add_callback(self._restore_sigint_handler)
shutdown all kernels The kernels will shutdown themselves when this process no longer exists but explicit shutdown allows the KernelManagers to cleanup the connection files.
def cleanup_kernels(self): """shutdown all kernels The kernels will shutdown themselves when this process no longer exists, but explicit shutdown allows the KernelManagers to cleanup the connection files. """ self.log.info('Shutting down kernels') km = self.kernel_manager # copy list, since shutdown_kernel deletes keys for kid in list(km.kernel_ids): km.shutdown_kernel(kid)
Price European and Asian options using a Monte Carlo method.
def price_options(S=100.0, K=100.0, sigma=0.25, r=0.05, days=260, paths=10000): """ Price European and Asian options using a Monte Carlo method. Parameters ---------- S : float The initial price of the stock. K : float The strike price of the option. sigma : float The volatility of the stock. r : float The risk free interest rate. days : int The number of days until the option expires. paths : int The number of Monte Carlo paths used to price the option. Returns ------- A tuple of (E. call, E. put, A. call, A. put) option prices. """ import numpy as np from math import exp,sqrt h = 1.0/days const1 = exp((r-0.5*sigma**2)*h) const2 = sigma*sqrt(h) stock_price = S*np.ones(paths, dtype='float64') stock_price_sum = np.zeros(paths, dtype='float64') for j in range(days): growth_factor = const1*np.exp(const2*np.random.standard_normal(paths)) stock_price = stock_price*growth_factor stock_price_sum = stock_price_sum + stock_price stock_price_avg = stock_price_sum/days zeros = np.zeros(paths, dtype='float64') r_factor = exp(-r*h*days) euro_put = r_factor*np.mean(np.maximum(zeros, K-stock_price)) asian_put = r_factor*np.mean(np.maximum(zeros, K-stock_price_avg)) euro_call = r_factor*np.mean(np.maximum(zeros, stock_price-K)) asian_call = r_factor*np.mean(np.maximum(zeros, stock_price_avg-K)) return (euro_call, euro_put, asian_call, asian_put)
Replace in text all occurences of any key in the given dictionary by its corresponding value. Returns the new string.
def multiple_replace(dict, text): """ Replace in 'text' all occurences of any key in the given dictionary by its corresponding value. Returns the new string.""" # Function by Xavier Defrang, originally found at: # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330 # Create a regular expression from the dictionary keys regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys()))) # For each match, look-up corresponding value in dictionary return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
Return the last depth elements of the current working directory.
def cwd_filt(depth): """Return the last depth elements of the current working directory. $HOME is always replaced with '~'. If depth==0, the full path is returned.""" cwd = os.getcwdu().replace(HOME,"~") out = os.sep.join(cwd.split(os.sep)[-depth:]) return out or os.sep
Return the last depth elements of the current working directory.
def cwd_filt2(depth): """Return the last depth elements of the current working directory. $HOME is always replaced with '~'. If depth==0, the full path is returned.""" full_cwd = os.getcwdu() cwd = full_cwd.replace(HOME,"~").split(os.sep) if '~' in cwd and len(cwd) == depth+1: depth += 1 drivepart = '' if sys.platform == 'win32' and len(cwd) > depth: drivepart = os.path.splitdrive(full_cwd)[0] out = drivepart + '/'.join(cwd[-depth:]) return out or os.sep
This is called when a prompt template is updated. It processes abbreviations used in the prompt template ( like \ # ) and calculates how many invisible characters ( ANSI colour escapes ) the resulting prompt contains. It is also called for each prompt on changing the colour scheme. In both cases traitlets should take care of calling this automatically.
def update_prompt(self, name, new_template=None): """This is called when a prompt template is updated. It processes abbreviations used in the prompt template (like \#) and calculates how many invisible characters (ANSI colour escapes) the resulting prompt contains. It is also called for each prompt on changing the colour scheme. In both cases, traitlets should take care of calling this automatically. """ if new_template is not None: self.templates[name] = multiple_replace(prompt_abbreviations, new_template) # We count invisible characters (colour escapes) on the last line of the # prompt, to calculate the width for lining up subsequent prompts. invis_chars = _lenlastline(self._render(name, color=True)) - \ _lenlastline(self._render(name, color=False)) self.invisible_chars[name] = invis_chars
Render but don t justify or update the width or txtwidth attributes.
def _render(self, name, color=True, **kwargs): """Render but don't justify, or update the width or txtwidth attributes. """ if name == 'rewrite': return self._render_rewrite(color=color) if color: scheme = self.color_scheme_table.active_colors if name=='out': colors = color_lists['normal'] colors.number, colors.prompt, colors.normal = \ scheme.out_number, scheme.out_prompt, scheme.normal else: colors = color_lists['inp'] colors.number, colors.prompt, colors.normal = \ scheme.in_number, scheme.in_prompt, scheme.in_normal if name=='in2': colors.prompt = scheme.in_prompt2 else: # No color colors = color_lists['nocolor'] colors.number, colors.prompt, colors.normal = '', '', '' count = self.shell.execution_count # Shorthand # Build the dictionary to be passed to string formatting fmtargs = dict(color=colors, count=count, dots="."*len(str(count)), width=self.width, txtwidth=self.txtwidth ) fmtargs.update(self.lazy_evaluate_fields) fmtargs.update(kwargs) # Prepare the prompt prompt = colors.prompt + self.templates[name] + colors.normal # Fill in required fields return self._formatter.format(prompt, **fmtargs)
Render the --- > rewrite prompt.
def _render_rewrite(self, color=True): """Render the ---> rewrite prompt.""" if color: scheme = self.color_scheme_table.active_colors # We need a non-input version of these escapes color_prompt = scheme.in_prompt.replace("\001","").replace("\002","") color_normal = scheme.normal else: color_prompt, color_normal = '', '' return color_prompt + "-> ".rjust(self.txtwidth, "-") + color_normal
Render the selected prompt. Parameters ---------- name: str Which prompt to render. One of in in2 out rewrite color: bool If True ( default ) include ANSI escape sequences for a coloured prompt. just: bool If True justify the prompt to the width of the last prompt. The default is stored in self. justify. ** kwargs: Additional arguments will be passed to the string formatting operation so they can override the values that would otherwise fill in the template. Returns ------- A string containing the rendered prompt.
def render(self, name, color=True, just=None, **kwargs): """ Render the selected prompt. Parameters ---------- name : str Which prompt to render. One of 'in', 'in2', 'out', 'rewrite' color : bool If True (default), include ANSI escape sequences for a coloured prompt. just : bool If True, justify the prompt to the width of the last prompt. The default is stored in self.justify. **kwargs : Additional arguments will be passed to the string formatting operation, so they can override the values that would otherwise fill in the template. Returns ------- A string containing the rendered prompt. """ res = self._render(name, color=color, **kwargs) # Handle justification of prompt invis_chars = self.invisible_chars[name] if color else 0 self.txtwidth = _lenlastline(res) - invis_chars just = self.justify if (just is None) else just # If the prompt spans more than one line, don't try to justify it: if just and name != 'in' and ('\n' not in res) and ('\r' not in res): res = res.rjust(self.width + invis_chars) self.width = _lenlastline(res) - invis_chars return res
Generates a JSON config file including the selection of random ports. Parameters ----------
def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0, ip=LOCALHOST, key=b''): """Generates a JSON config file, including the selection of random ports. Parameters ---------- fname : unicode The path to the file to write shell_port : int, optional The port to use for ROUTER channel. iopub_port : int, optional The port to use for the SUB channel. stdin_port : int, optional The port to use for the REQ (raw input) channel. hb_port : int, optional The port to use for the hearbeat REP channel. ip : str, optional The ip address the kernel will bind to. key : str, optional The Session key used for HMAC authentication. """ # default to temporary connector file if not fname: fname = tempfile.mktemp('.json') # Find open ports as necessary. ports = [] ports_needed = int(shell_port <= 0) + int(iopub_port <= 0) + \ int(stdin_port <= 0) + int(hb_port <= 0) for i in xrange(ports_needed): sock = socket.socket() sock.bind(('', 0)) ports.append(sock) for i, sock in enumerate(ports): port = sock.getsockname()[1] sock.close() ports[i] = port if shell_port <= 0: shell_port = ports.pop(0) if iopub_port <= 0: iopub_port = ports.pop(0) if stdin_port <= 0: stdin_port = ports.pop(0) if hb_port <= 0: hb_port = ports.pop(0) cfg = dict( shell_port=shell_port, iopub_port=iopub_port, stdin_port=stdin_port, hb_port=hb_port, ) cfg['ip'] = ip cfg['key'] = bytes_to_str(key) with open(fname, 'w') as f: f.write(json.dumps(cfg, indent=2)) return fname, cfg
Launches a localhost kernel binding to the specified ports.
def base_launch_kernel(code, fname, stdin=None, stdout=None, stderr=None, executable=None, independent=False, extra_arguments=[], cwd=None): """ Launches a localhost kernel, binding to the specified ports. Parameters ---------- code : str, A string of Python code that imports and executes a kernel entry point. stdin, stdout, stderr : optional (default None) Standards streams, as defined in subprocess.Popen. fname : unicode, optional The JSON connector file, containing ip/port/hmac key information. key : str, optional The Session key used for HMAC authentication. executable : str, optional (default sys.executable) The Python executable to use for the kernel process. independent : bool, optional (default False) If set, the kernel process is guaranteed to survive if this process dies. If not set, an effort is made to ensure that the kernel is killed when this process dies. Note that in this case it is still good practice to kill kernels manually before exiting. extra_arguments : list, optional A list of extra arguments to pass when executing the launch code. cwd : path, optional The working dir of the kernel process (default: cwd of this process). Returns ------- A tuple of form: (kernel_process, shell_port, iopub_port, stdin_port, hb_port) where kernel_process is a Popen object and the ports are integers. """ # Build the kernel launch command. if executable is None: executable = sys.executable arguments = [ executable, '-c', code, '-f', fname ] arguments.extend(extra_arguments) # Popen will fail (sometimes with a deadlock) if stdin, stdout, and stderr # are invalid. Unfortunately, there is in general no way to detect whether # they are valid. The following two blocks redirect them to (temporary) # pipes in certain important cases. # If this process has been backgrounded, our stdin is invalid. Since there # is no compelling reason for the kernel to inherit our stdin anyway, we'll # place this one safe and always redirect. redirect_in = True _stdin = PIPE if stdin is None else stdin # If this process in running on pythonw, we know that stdin, stdout, and # stderr are all invalid. redirect_out = sys.executable.endswith('pythonw.exe') if redirect_out: _stdout = PIPE if stdout is None else stdout _stderr = PIPE if stderr is None else stderr else: _stdout, _stderr = stdout, stderr # Spawn a kernel. if sys.platform == 'win32': # Create a Win32 event for interrupting the kernel. interrupt_event = ParentPollerWindows.create_interrupt_event() arguments += [ '--interrupt=%i'%interrupt_event ] # If the kernel is running on pythonw and stdout/stderr are not been # re-directed, it will crash when more than 4KB of data is written to # stdout or stderr. This is a bug that has been with Python for a very # long time; see http://bugs.python.org/issue706263. # A cleaner solution to this problem would be to pass os.devnull to # Popen directly. Unfortunately, that does not work. if executable.endswith('pythonw.exe'): if stdout is None: arguments.append('--no-stdout') if stderr is None: arguments.append('--no-stderr') # Launch the kernel process. if independent: proc = Popen(arguments, creationflags=512, # CREATE_NEW_PROCESS_GROUP stdin=_stdin, stdout=_stdout, stderr=_stderr) else: try: from _winapi import DuplicateHandle, GetCurrentProcess, \ DUPLICATE_SAME_ACCESS except: from _subprocess import DuplicateHandle, GetCurrentProcess, \ DUPLICATE_SAME_ACCESS pid = GetCurrentProcess() handle = DuplicateHandle(pid, pid, pid, 0, True, # Inheritable by new processes. DUPLICATE_SAME_ACCESS) proc = Popen(arguments + ['--parent=%i'%int(handle)], stdin=_stdin, stdout=_stdout, stderr=_stderr) # Attach the interrupt event to the Popen objet so it can be used later. proc.win32_interrupt_event = interrupt_event else: if independent: proc = Popen(arguments, preexec_fn=lambda: os.setsid(), stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd) else: proc = Popen(arguments + ['--parent=1'], stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd) # Clean up pipes created to work around Popen bug. if redirect_in: if stdin is None: proc.stdin.close() if redirect_out: if stdout is None: proc.stdout.close() if stderr is None: proc.stderr.close() return proc
This is the actual zest. releaser entry point
def create_zipfile(context): """This is the actual zest.releaser entry point Relevant items in the context dict: name Name of the project being released tagdir Directory where the tag checkout is placed (*if* a tag checkout has been made) version Version we're releasing workingdir Original working directory """ if not prerequisites_ok(): return # Create a zipfile. subprocess.call(['make', 'zip']) for zipfile in glob.glob('*.zip'): first_part = zipfile.split('.')[0] new_name = "%s.%s.zip" % (first_part, context['version']) target = os.path.join(context['workingdir'], new_name) shutil.copy(zipfile, target) print("Copied %s to %s" % (zipfile, target))
Fix the version in metadata. txt
def fix_version(context): """Fix the version in metadata.txt Relevant context dict item for both prerelease and postrelease: ``new_version``. """ if not prerequisites_ok(): return lines = codecs.open('metadata.txt', 'rU', 'utf-8').readlines() for index, line in enumerate(lines): if line.startswith('version'): new_line = 'version=%s\n' % context['new_version'] lines[index] = new_line time.sleep(1) codecs.open('metadata.txt', 'w', 'utf-8').writelines(lines)
return whether an object is mappable or not.
def mappable(obj): """return whether an object is mappable or not.""" if isinstance(obj, (tuple,list)): return True for m in arrayModules: if isinstance(obj,m['type']): return True return False
Returns the pth partition of q partitions of seq.
def getPartition(self, seq, p, q): """Returns the pth partition of q partitions of seq.""" # Test for error conditions here if p<0 or p>=q: print "No partition exists." return remainder = len(seq)%q basesize = len(seq)//q hi = [] lo = [] for n in range(q): if n < remainder: lo.append(n * (basesize + 1)) hi.append(lo[-1] + basesize + 1) else: lo.append(n*basesize + remainder) hi.append(lo[-1] + basesize) try: result = seq[lo[p]:hi[p]] except TypeError: # some objects (iterators) can't be sliced, # use islice: result = list(islice(seq, lo[p], hi[p])) return result
Patch pexpect to prevent unhandled exceptions at VM teardown.
def pexpect_monkeypatch(): """Patch pexpect to prevent unhandled exceptions at VM teardown. Calling this function will monkeypatch the pexpect.spawn class and modify its __del__ method to make it more robust in the face of failures that can occur if it is called when the Python VM is shutting down. Since Python may fire __del__ methods arbitrarily late, it's possible for them to execute during the teardown of the Python VM itself. At this point, various builtin modules have been reset to None. Thus, the call to self.close() will trigger an exception because it tries to call os.close(), and os is now None. """ if pexpect.__version__[:3] >= '2.2': # No need to patch, fix is already the upstream version. return def __del__(self): """This makes sure that no system resources are left open. Python only garbage collects Python objects. OS file descriptors are not Python objects, so they must be handled explicitly. If the child file descriptor was opened outside of this class (passed to the constructor) then this does not close it. """ if not self.closed: try: self.close() except AttributeError: pass pexpect.spawn.__del__ = __del__
Run as a command - line script.
def main(): """Run as a command-line script.""" parser = optparse.OptionParser(usage=MAIN_USAGE) newopt = parser.add_option newopt('--ipython',action='store_const',dest='mode',const='ipython', help='IPython interactive runner (default).') newopt('--python',action='store_const',dest='mode',const='python', help='Python interactive runner.') newopt('--sage',action='store_const',dest='mode',const='sage', help='SAGE interactive runner.') opts,args = parser.parse_args() runners = dict(ipython=IPythonRunner, python=PythonRunner, sage=SAGERunner) try: ext = os.path.splitext(args[0])[-1] except IndexError: ext = '' modes = {'.ipy':'ipython', '.py':'python', '.sage':'sage'} mode = modes.get(ext,"ipython") if opts.mode: mode = opts.mode runners[mode]().main(args)
Run the given file interactively.
def run_file(self,fname,interact=False,get_output=False): """Run the given file interactively. Inputs: -fname: name of the file to execute. See the run_source docstring for the meaning of the optional arguments.""" fobj = open(fname,'r') try: out = self.run_source(fobj,interact,get_output) finally: fobj.close() if get_output: return out
Run the given source code interactively.
def run_source(self,source,interact=False,get_output=False): """Run the given source code interactively. Inputs: - source: a string of code to be executed, or an open file object we can iterate over. Optional inputs: - interact(False): if true, start to interact with the running program at the end of the script. Otherwise, just exit. - get_output(False): if true, capture the output of the child process (filtering the input commands out) and return it as a string. Returns: A string containing the process output, but only if requested. """ # if the source is a string, chop it up in lines so we can iterate # over it just as if it were an open file. if isinstance(source, basestring): source = source.splitlines(True) if self.echo: # normalize all strings we write to use the native OS line # separators. linesep = os.linesep stdwrite = self.out.write write = lambda s: stdwrite(s.replace('\r\n',linesep)) else: # Quiet mode, all writes are no-ops write = lambda s: None c = self.child prompts = c.compile_pattern_list(self.prompts) prompt_idx = c.expect_list(prompts) # Flag whether the script ends normally or not, to know whether we can # do anything further with the underlying process. end_normal = True # If the output was requested, store it in a list for return at the end if get_output: output = [] store_output = output.append for cmd in source: # skip blank lines for all matches to the 'main' prompt, while the # secondary prompts do not if prompt_idx==0 and \ (cmd.isspace() or cmd.lstrip().startswith('#')): write(cmd) continue # write('AFTER: '+c.after) # dbg write(c.after) c.send(cmd) try: prompt_idx = c.expect_list(prompts) except pexpect.EOF: # this will happen if the child dies unexpectedly write(c.before) end_normal = False break write(c.before) # With an echoing process, the output we get in c.before contains # the command sent, a newline, and then the actual process output if get_output: store_output(c.before[len(cmd+'\n'):]) #write('CMD: <<%s>>' % cmd) # dbg #write('OUTPUT: <<%s>>' % output[-1]) # dbg self.out.flush() if end_normal: if interact: c.send('\n') print '<< Starting interactive mode >>', try: c.interact() except OSError: # This is what fires when the child stops. Simply print a # newline so the system prompt is aligned. The extra # space is there to make sure it gets printed, otherwise # OS buffering sometimes just suppresses it. write(' \n') self.out.flush() else: if interact: e="Further interaction is not possible: child process is dead." print >> sys.stderr, e # Leave the child ready for more input later on, otherwise select just # hangs on the second invocation. if c.isalive(): c.send('\n') # Return any requested output if get_output: return ''.join(output)
Run as a command - line script.
def main(self,argv=None): """Run as a command-line script.""" parser = optparse.OptionParser(usage=USAGE % self.__class__.__name__) newopt = parser.add_option newopt('-i','--interact',action='store_true',default=False, help='Interact with the program after the script is run.') opts,args = parser.parse_args(argv) if len(args) != 1: print >> sys.stderr,"You must supply exactly one file to run." sys.exit(1) self.run_file(args[0],opts.interact)
Generate a Cobertura - compatible XML report for morfs.
def report(self, morfs, outfile=None): """Generate a Cobertura-compatible XML report for `morfs`. `morfs` is a list of modules or filenames. `outfile` is a file object to write the XML to. """ # Initial setup. outfile = outfile or sys.stdout # Create the DOM that will store the data. impl = xml.dom.minidom.getDOMImplementation() docType = impl.createDocumentType( "coverage", None, "http://cobertura.sourceforge.net/xml/coverage-03.dtd" ) self.xml_out = impl.createDocument(None, "coverage", docType) # Write header stuff. xcoverage = self.xml_out.documentElement xcoverage.setAttribute("version", __version__) xcoverage.setAttribute("timestamp", str(int(time.time()*1000))) xcoverage.appendChild(self.xml_out.createComment( " Generated by coverage.py: %s " % __url__ )) xpackages = self.xml_out.createElement("packages") xcoverage.appendChild(xpackages) # Call xml_file for each file in the data. self.packages = {} self.report_files(self.xml_file, morfs) lnum_tot, lhits_tot = 0, 0 bnum_tot, bhits_tot = 0, 0 # Populate the XML DOM with the package info. for pkg_name in sorted(self.packages.keys()): pkg_data = self.packages[pkg_name] class_elts, lhits, lnum, bhits, bnum = pkg_data xpackage = self.xml_out.createElement("package") xpackages.appendChild(xpackage) xclasses = self.xml_out.createElement("classes") xpackage.appendChild(xclasses) for class_name in sorted(class_elts.keys()): xclasses.appendChild(class_elts[class_name]) xpackage.setAttribute("name", pkg_name.replace(os.sep, '.')) xpackage.setAttribute("line-rate", rate(lhits, lnum)) xpackage.setAttribute("branch-rate", rate(bhits, bnum)) xpackage.setAttribute("complexity", "0") lnum_tot += lnum lhits_tot += lhits bnum_tot += bnum bhits_tot += bhits xcoverage.setAttribute("line-rate", rate(lhits_tot, lnum_tot)) xcoverage.setAttribute("branch-rate", rate(bhits_tot, bnum_tot)) # Use the DOM to write the output file. outfile.write(self.xml_out.toprettyxml()) # Return the total percentage. denom = lnum_tot + bnum_tot if denom == 0: pct = 0.0 else: pct = 100.0 * (lhits_tot + bhits_tot) / denom return pct
Add to the XML report for a single file.
def xml_file(self, cu, analysis): """Add to the XML report for a single file.""" # Create the 'lines' and 'package' XML elements, which # are populated later. Note that a package == a directory. package_name = rpartition(cu.name, ".")[0] className = cu.name package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0]) xclass = self.xml_out.createElement("class") xclass.appendChild(self.xml_out.createElement("methods")) xlines = self.xml_out.createElement("lines") xclass.appendChild(xlines) xclass.setAttribute("name", className) filename = cu.file_locator.relative_filename(cu.filename) xclass.setAttribute("filename", filename.replace("\\", "/")) xclass.setAttribute("complexity", "0") branch_stats = analysis.branch_stats() # For each statement, create an XML 'line' element. for line in sorted(analysis.statements): xline = self.xml_out.createElement("line") xline.setAttribute("number", str(line)) # Q: can we get info about the number of times a statement is # executed? If so, that should be recorded here. xline.setAttribute("hits", str(int(line not in analysis.missing))) if self.arcs: if line in branch_stats: total, taken = branch_stats[line] xline.setAttribute("branch", "true") xline.setAttribute("condition-coverage", "%d%% (%d/%d)" % (100*taken/total, taken, total) ) xlines.appendChild(xline) class_lines = len(analysis.statements) class_hits = class_lines - len(analysis.missing) if self.arcs: class_branches = sum([t for t,k in branch_stats.values()]) missing_branches = sum([t-k for t,k in branch_stats.values()]) class_br_hits = class_branches - missing_branches else: class_branches = 0.0 class_br_hits = 0.0 # Finalize the statistics that are collected in the XML DOM. xclass.setAttribute("line-rate", rate(class_hits, class_lines)) xclass.setAttribute("branch-rate", rate(class_br_hits, class_branches)) package[0][className] = xclass package[1] += class_hits package[2] += class_lines package[3] += class_br_hits package[4] += class_branches
Compute the histogram of a remote array a. Parameters ---------- view IPython DirectView instance a: str String name of the remote array bins: int Number of histogram bins rng: ( float float ) Tuple of min max of the range to histogram normed: boolean Should the histogram counts be normalized to 1
def phistogram(view, a, bins=10, rng=None, normed=False): """Compute the histogram of a remote array a. Parameters ---------- view IPython DirectView instance a : str String name of the remote array bins : int Number of histogram bins rng : (float, float) Tuple of min, max of the range to histogram normed : boolean Should the histogram counts be normalized to 1 """ nengines = len(view.targets) # view.push(dict(bins=bins, rng=rng)) with view.sync_imports(): import numpy rets = view.apply_sync(lambda a, b, rng: numpy.histogram(a,b,rng), Reference(a), bins, rng) hists = [ r[0] for r in rets ] lower_edges = [ r[1] for r in rets ] # view.execute('hist, lower_edges = numpy.histogram(%s, bins, rng)' % a) lower_edges = view.pull('lower_edges', targets=0) hist_array = numpy.array(hists).reshape(nengines, -1) # hist_array.shape = (nengines,-1) total_hist = numpy.sum(hist_array, 0) if normed: total_hist = total_hist/numpy.sum(total_hist,dtype=float) return total_hist, lower_edges
This will download a segment of pi from super - computing. org if the file is not already present.
def fetch_pi_file(filename): """This will download a segment of pi from super-computing.org if the file is not already present. """ import os, urllib ftpdir="ftp://pi.super-computing.org/.2/pi200m/" if os.path.exists(filename): # we already have it return else: # download it urllib.urlretrieve(ftpdir+filename,filename)
Add up a list of freq counts to get the total counts.
def reduce_freqs(freqlist): """ Add up a list of freq counts to get the total counts. """ allfreqs = np.zeros_like(freqlist[0]) for f in freqlist: allfreqs += f return allfreqs
Read digits of pi from a file and compute the n digit frequencies.
def compute_n_digit_freqs(filename, n): """ Read digits of pi from a file and compute the n digit frequencies. """ d = txt_file_to_digits(filename) freqs = n_digit_freqs(d, n) return freqs
Yield the digits of pi read from a. txt file.
def txt_file_to_digits(filename, the_type=str): """ Yield the digits of pi read from a .txt file. """ with open(filename, 'r') as f: for line in f.readlines(): for c in line: if c != '\n' and c!= ' ': yield the_type(c)
Consume digits of pi and compute 1 digit freq. counts.
def one_digit_freqs(digits, normalize=False): """ Consume digits of pi and compute 1 digit freq. counts. """ freqs = np.zeros(10, dtype='i4') for d in digits: freqs[int(d)] += 1 if normalize: freqs = freqs/freqs.sum() return freqs
Consume digits of pi and compute 2 digits freq. counts.
def two_digit_freqs(digits, normalize=False): """ Consume digits of pi and compute 2 digits freq. counts. """ freqs = np.zeros(100, dtype='i4') last = digits.next() this = digits.next() for d in digits: index = int(last + this) freqs[index] += 1 last = this this = d if normalize: freqs = freqs/freqs.sum() return freqs
Consume digits of pi and compute n digits freq. counts.
def n_digit_freqs(digits, n, normalize=False): """ Consume digits of pi and compute n digits freq. counts. This should only be used for 1-6 digits. """ freqs = np.zeros(pow(10,n), dtype='i4') current = np.zeros(n, dtype=int) for i in range(n): current[i] = digits.next() for d in digits: index = int(''.join(map(str, current))) freqs[index] += 1 current[0:-1] = current[1:] current[-1] = d if normalize: freqs = freqs/freqs.sum() return freqs
Plot two digits frequency counts using matplotlib.
def plot_two_digit_freqs(f2): """ Plot two digits frequency counts using matplotlib. """ f2_copy = f2.copy() f2_copy.shape = (10,10) ax = plt.matshow(f2_copy) plt.colorbar() for i in range(10): for j in range(10): plt.text(i-0.2, j+0.2, str(j)+str(i)) plt.ylabel('First digit') plt.xlabel('Second digit') return ax
Plot one digit frequency counts using matplotlib.
def plot_one_digit_freqs(f1): """ Plot one digit frequency counts using matplotlib. """ ax = plt.plot(f1,'bo-') plt.title('Single digit counts in pi') plt.xlabel('Digit') plt.ylabel('Count') return ax
Extend a prefixed uri with the help of a specific dictionary of prefixes: param short: Prefixed uri to be extended: return:
def __extend_uri(self, short): """ Extend a prefixed uri with the help of a specific dictionary of prefixes :param short: Prefixed uri to be extended :return: """ if short == 'a': return RDF.type for prefix in sorted(self.__prefixes, key=lambda x: len(x), reverse=True): if short.startswith(prefix): return URIRef(short.replace(prefix + ':', self.__prefixes[prefix])) return short
Try to retrieve a model and return None if it is not found. Useful if you do not want to bother with the try/ except block.
def get_object_or_none(qs, *args, **kwargs): """ Try to retrieve a model, and return None if it is not found. Useful if you do not want to bother with the try/except block. """ try: return qs.get(*args, **kwargs) except models.ObjectDoesNotExist: return None
Extract a set of variables by name from another frame.
def extract_vars(*names,**kw): """Extract a set of variables by name from another frame. :Parameters: - `*names`: strings One or more variable names which will be extracted from the caller's frame. :Keywords: - `depth`: integer (0) How many frames in the stack to walk when looking for your variables. Examples: In [2]: def func(x): ...: y = 1 ...: print sorted(extract_vars('x','y').items()) ...: In [3]: func('hello') [('x', 'hello'), ('y', 1)] """ depth = kw.get('depth',0) callerNS = sys._getframe(depth+1).f_locals return dict((k,callerNS[k]) for k in names)
Extract a set of variables by name from another frame.
def extract_vars_above(*names): """Extract a set of variables by name from another frame. Similar to extractVars(), but with a specified depth of 1, so that names are exctracted exactly from above the caller. This is simply a convenience function so that the very common case (for us) of skipping exactly 1 frame doesn't have to construct a special dict for keyword passing.""" callerNS = sys._getframe(2).f_locals return dict((k,callerNS[k]) for k in names)
Print the value of an expression from the caller s frame.
def debugx(expr,pre_msg=''): """Print the value of an expression from the caller's frame. Takes an expression, evaluates it in the caller's frame and prints both the given expression and the resulting value (as well as a debug mark indicating the name of the calling function. The input must be of a form suitable for eval(). An optional message can be passed, which will be prepended to the printed expr->value pair.""" cf = sys._getframe(1) print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, eval(expr,cf.f_globals,cf.f_locals))
Returns ( module locals ) of the funciton depth frames away from the caller
def extract_module_locals(depth=0): """Returns (module, locals) of the funciton `depth` frames away from the caller""" f = sys._getframe(depth + 1) global_ns = f.f_globals module = sys.modules[global_ns['__name__']] return (module, f.f_locals)
User - friendly reverse. Pass arguments and keyword arguments to Django s reverse as args and kwargs arguments respectively.
def reverse(view, *args, **kwargs): ''' User-friendly reverse. Pass arguments and keyword arguments to Django's `reverse` as `args` and `kwargs` arguments, respectively. The special optional keyword argument `query` is a dictionary of query (or GET) parameters that can be appended to the `reverse`d URL. Example: reverse('products:category', categoryId = 5, query = {'page': 2}) is equivalent to django.core.urlresolvers.reverse('products:category', kwargs = {'categoryId': 5}) + '?page=2' ''' if 'query' in kwargs: query = kwargs.pop('query') else: query = None base = urlresolvers.reverse(view, args = args, kwargs = kwargs) if query: return '{}?{}'.format(base, django.utils.http.urlencode(query)) else: return base
prefix base - > true iff name prefix +. + base is private.
def is_private(prefix, base): """prefix, base -> true iff name prefix + "." + base is "private". Prefix may be an empty string, and base does not contain a period. Prefix is ignored (although functions you write conforming to this protocol may make use of it). Return true iff base begins with an (at least one) underscore, but does not both begin and end with (at least) two underscores. """ warnings.warn("is_private is deprecated; it wasn't useful; " "examine DocTestFinder.find() lists instead", DeprecationWarning, stacklevel=2) return base[:1] == "_" and not base[:2] == "__" == base[-2:]
Return the compiler - flags associated with the future features that have been imported into the given namespace ( globs ).
def _extract_future_flags(globs): """ Return the compiler-flags associated with the future features that have been imported into the given namespace (globs). """ flags = 0 for fname in __future__.all_feature_names: feature = globs.get(fname, None) if feature is getattr(__future__, fname): flags |= feature.compiler_flag return flags
Return the module specified by module. In particular: - If module is a module then return module. - If module is a string then import and return the module with that name. - If module is None then return the calling module. The calling module is assumed to be the module of the stack frame at the given depth in the call stack.
def _normalize_module(module, depth=2): """ Return the module specified by `module`. In particular: - If `module` is a module, then return module. - If `module` is a string, then import and return the module with that name. - If `module` is None, then return the calling module. The calling module is assumed to be the module of the stack frame at the given depth in the call stack. """ if inspect.ismodule(module): return module elif isinstance(module, (str, unicode)): return __import__(module, globals(), locals(), ["*"]) elif module is None: return sys.modules[sys._getframe(depth).f_globals['__name__']] else: raise TypeError("Expected a module, string, or None")
Return a string containing a traceback message for the given exc_info tuple ( as returned by sys. exc_info () ).
def _exception_traceback(exc_info): """ Return a string containing a traceback message for the given exc_info tuple (as returned by sys.exc_info()). """ # Get a traceback message. excout = StringIO() exc_type, exc_val, exc_tb = exc_info traceback.print_exception(exc_type, exc_val, exc_tb, file=excout) return excout.getvalue()
Test examples in the given object s docstring ( f ) using globs as globals. Optional argument name is used in failure messages. If the optional argument verbose is true then generate output even if there are no failures.
def run_docstring_examples(f, globs, verbose=False, name="NoName", compileflags=None, optionflags=0): """ Test examples in the given object's docstring (`f`), using `globs` as globals. Optional argument `name` is used in failure messages. If the optional argument `verbose` is true, then generate output even if there are no failures. `compileflags` gives the set of flags that should be used by the Python compiler when running the examples. If not specified, then it will default to the set of future-import flags that apply to `globs`. Optional keyword arg `optionflags` specifies options for the testing and output. See the documentation for `testmod` for more information. """ # Find, parse, and run all tests in the given module. finder = DocTestFinder(verbose=verbose, recurse=False) runner = DocTestRunner(verbose=verbose, optionflags=optionflags) for test in finder.find(f, name, globs=globs): runner.run(test, compileflags=compileflags)
A unittest suite for one or more doctest files.
def DocFileSuite(*paths, **kw): """A unittest suite for one or more doctest files. The path to each doctest file is given as a string; the interpretation of that string depends on the keyword argument "module_relative". A number of options may be provided as keyword arguments: module_relative If "module_relative" is True, then the given file paths are interpreted as os-independent module-relative paths. By default, these paths are relative to the calling module's directory; but if the "package" argument is specified, then they are relative to that package. To ensure os-independence, "filename" should use "/" characters to separate path segments, and may not be an absolute path (i.e., it may not begin with "/"). If "module_relative" is False, then the given file paths are interpreted as os-specific paths. These paths may be absolute or relative (to the current working directory). package A Python package or the name of a Python package whose directory should be used as the base directory for module relative paths. If "package" is not specified, then the calling module's directory is used as the base directory for module relative filenames. It is an error to specify "package" if "module_relative" is False. setUp A set-up function. This is called before running the tests in each file. The setUp function will be passed a DocTest object. The setUp function can access the test globals as the globs attribute of the test passed. tearDown A tear-down function. This is called after running the tests in each file. The tearDown function will be passed a DocTest object. The tearDown function can access the test globals as the globs attribute of the test passed. globs A dictionary containing initial global variables for the tests. optionflags A set of doctest option flags expressed as an integer. parser A DocTestParser (or subclass) that should be used to extract tests from the files. """ suite = unittest.TestSuite() # We do this here so that _normalize_module is called at the right # level. If it were called in DocFileTest, then this function # would be the caller and we might guess the package incorrectly. if kw.get('module_relative', True): kw['package'] = _normalize_module(kw.get('package')) for path in paths: suite.addTest(DocFileTest(path, **kw)) return suite
Debug a single doctest docstring in argument src
def debug_src(src, pm=False, globs=None): """Debug a single doctest docstring, in argument `src`'""" testsrc = script_from_examples(src) debug_script(testsrc, pm, globs)
Debug a test script. src is the script as a string.
def debug_script(src, pm=False, globs=None): "Debug a test script. `src` is the script, as a string." import pdb # Note that tempfile.NameTemporaryFile() cannot be used. As the # docs say, a file so created cannot be opened by name a second time # on modern Windows boxes, and execfile() needs to open it. srcfilename = tempfile.mktemp(".py", "doctestdebug") f = open(srcfilename, 'w') f.write(src) f.close() try: if globs: globs = globs.copy() else: globs = {} if pm: try: execfile(srcfilename, globs, globs) except: print sys.exc_info()[1] pdb.post_mortem(sys.exc_info()[2]) else: # Note that %r is vital here. '%s' instead can, e.g., cause # backslashes to get treated as metacharacters on Windows. pdb.run("execfile(%r)" % srcfilename, globs, globs) finally: os.remove(srcfilename)
Debug a single doctest docstring.
def debug(module, name, pm=False): """Debug a single doctest docstring. Provide the module (or dotted name of the module) containing the test to be debugged and the name (within the module) of the object with the docstring with tests to be debugged. """ module = _normalize_module(module) testsrc = testsource(module, name) debug_script(testsrc, pm, module.__dict__)
Return True iff the actual output from an example ( got ) matches the expected output ( want ). These strings are always considered to match if they are identical ; but depending on what option flags the test runner is using several non - exact match types are also possible. See the documentation for TestRunner for more information about option flags.
def check_output(self, want, got, optionflags): """ Return True iff the actual output from an example (`got`) matches the expected output (`want`). These strings are always considered to match if they are identical; but depending on what option flags the test runner is using, several non-exact match types are also possible. See the documentation for `TestRunner` for more information about option flags. """ # Handle the common case first, for efficiency: # if they're string-identical, always return true. if got == want: return True # The values True and False replaced 1 and 0 as the return # value for boolean comparisons in Python 2.3. if not (optionflags & DONT_ACCEPT_TRUE_FOR_1): if (got,want) == ("True\n", "1\n"): return True if (got,want) == ("False\n", "0\n"): return True # <BLANKLINE> can be used as a special sequence to signify a # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used. if not (optionflags & DONT_ACCEPT_BLANKLINE): # Replace <BLANKLINE> in want with a blank line. want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER), '', want) # If a line in got contains only spaces, then remove the # spaces. got = re.sub('(?m)^\s*?$', '', got) if got == want: return True # This flag causes doctest to ignore any differences in the # contents of whitespace strings. Note that this can be used # in conjunction with the ELLIPSIS flag. if optionflags & NORMALIZE_WHITESPACE: got = ' '.join(got.split()) want = ' '.join(want.split()) if got == want: return True # The ELLIPSIS flag says to let the sequence "..." in `want` # match any substring in `got`. if optionflags & ELLIPSIS: if _ellipsis_match(want, got): return True # We didn't find any match; return false. return False
Return a string describing the differences between the expected output for a given example ( example ) and the actual output ( got ). optionflags is the set of option flags used to compare want and got.
def output_difference(self, example, got, optionflags): """ Return a string describing the differences between the expected output for a given example (`example`) and the actual output (`got`). `optionflags` is the set of option flags used to compare `want` and `got`. """ want = example.want # If <BLANKLINE>s are being used, then replace blank lines # with <BLANKLINE> in the actual output string. if not (optionflags & DONT_ACCEPT_BLANKLINE): got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got) # Check if we should use diff. if self._do_a_fancy_diff(want, got, optionflags): # Split want & got into lines. want_lines = want.splitlines(True) # True == keep line ends got_lines = got.splitlines(True) # Use difflib to find their differences. if optionflags & REPORT_UDIFF: diff = difflib.unified_diff(want_lines, got_lines, n=2) diff = list(diff)[2:] # strip the diff header kind = 'unified diff with -expected +actual' elif optionflags & REPORT_CDIFF: diff = difflib.context_diff(want_lines, got_lines, n=2) diff = list(diff)[2:] # strip the diff header kind = 'context diff with expected followed by actual' elif optionflags & REPORT_NDIFF: engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK) diff = list(engine.compare(want_lines, got_lines)) kind = 'ndiff with -expected +actual' else: assert 0, 'Bad diff option' # Remove trailing whitespace on diff output. diff = [line.rstrip() + '\n' for line in diff] return 'Differences (%s):\n' % kind + _indent(''.join(diff)) # If we're not using diff, then simply list the expected # output followed by the actual output. if want and got: return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got)) elif want: return 'Expected:\n%sGot nothing\n' % _indent(want) elif got: return 'Expected nothing\nGot:\n%s' % _indent(got) else: return 'Expected nothing\nGot nothing\n'
hashed set
def hset(self, hashroot, key, value): """ hashed set """ hroot = self.root / hashroot if not hroot.isdir(): hroot.makedirs() hfile = hroot / gethashfile(key) d = self.get(hfile, {}) d.update( {key : value}) self[hfile] = d
Get all data contained in hashed category hashroot as dict
def hdict(self, hashroot): """ Get all data contained in hashed category 'hashroot' as dict """ hfiles = self.keys(hashroot + "/*") hfiles.sort() last = len(hfiles) and hfiles[-1] or '' if last.endswith('xx'): # print "using xx" hfiles = [last] + hfiles[:-1] all = {} for f in hfiles: # print "using",f try: all.update(self[f]) except KeyError: print "Corrupt",f,"deleted - hset is not threadsafe!" del self[f] self.uncache(f) return all
Compress category hashroot so hset is fast again
def hcompress(self, hashroot): """ Compress category 'hashroot', so hset is fast again hget will fail if fast_only is True for compressed items (that were hset before hcompress). """ hfiles = self.keys(hashroot + "/*") all = {} for f in hfiles: # print "using",f all.update(self[f]) self.uncache(f) self[hashroot + '/xx'] = all for f in hfiles: p = self.root / f if p.basename() == 'xx': continue p.remove()
All keys in DB or all keys matching a glob
def keys(self, globpat = None): """ All keys in DB, or all keys matching a glob""" if globpat is None: files = self.root.walkfiles() else: files = [Path(p) for p in glob.glob(self.root/globpat)] return [self._normalized(p) for p in files if p.isfile()]
Reimplemented to handle keyboard input and to auto - hide when the text edit loses focus.
def eventFilter(self, obj, event): """ Reimplemented to handle keyboard input and to auto-hide when the text edit loses focus. """ if obj == self._text_edit: etype = event.type() if etype in( QtCore.QEvent.KeyPress, QtCore.QEvent.FocusOut ): self.cancel_completion() return super(CompletionPlain, self).eventFilter(obj, event)
Shows the completion widget with items at the position specified by cursor.
def show_items(self, cursor, items): """ Shows the completion widget with 'items' at the position specified by 'cursor'. """ if not items : return self.cancel_completion() strng = text.columnize(items) self._console_widget._fill_temporary_buffer(cursor, strng, html=False)
returns whether this record should be printed
def allow(self, record): """returns whether this record should be printed""" if not self: # nothing to filter return True return self._allow(record) and not self._deny(record)
return the bool of whether record starts with any item in matchers
def _any_match(matchers, record): """return the bool of whether `record` starts with any item in `matchers`""" def record_matches_key(key): return record == key or record.startswith(key + '.') return anyp(bool, map(record_matches_key, matchers))
Register commandline options.
def options(self, parser, env): """Register commandline options. """ parser.add_option( "--nologcapture", action="store_false", default=not env.get(self.env_opt), dest="logcapture", help="Disable logging capture plugin. " "Logging configurtion will be left intact." " [NOSE_NOLOGCAPTURE]") parser.add_option( "--logging-format", action="store", dest="logcapture_format", default=env.get('NOSE_LOGFORMAT') or self.logformat, metavar="FORMAT", help="Specify custom format to print statements. " "Uses the same format as used by standard logging handlers." " [NOSE_LOGFORMAT]") parser.add_option( "--logging-datefmt", action="store", dest="logcapture_datefmt", default=env.get('NOSE_LOGDATEFMT') or self.logdatefmt, metavar="FORMAT", help="Specify custom date/time format to print statements. " "Uses the same format as used by standard logging handlers." " [NOSE_LOGDATEFMT]") parser.add_option( "--logging-filter", action="store", dest="logcapture_filters", default=env.get('NOSE_LOGFILTER'), metavar="FILTER", help="Specify which statements to filter in/out. " "By default, everything is captured. If the output is too" " verbose,\nuse this option to filter out needless output.\n" "Example: filter=foo will capture statements issued ONLY to\n" " foo or foo.what.ever.sub but not foobar or other logger.\n" "Specify multiple loggers with comma: filter=foo,bar,baz.\n" "If any logger name is prefixed with a minus, eg filter=-foo,\n" "it will be excluded rather than included. Default: " "exclude logging messages from nose itself (-nose)." " [NOSE_LOGFILTER]\n") parser.add_option( "--logging-clear-handlers", action="store_true", default=False, dest="logcapture_clear", help="Clear all other logging handlers") parser.add_option( "--logging-level", action="store", default='NOTSET', dest="logcapture_level", help="Set the log level to capture")
Configure plugin.
def configure(self, options, conf): """Configure plugin. """ self.conf = conf # Disable if explicitly disabled, or if logging is # configured via logging config file if not options.logcapture or conf.loggingConfig: self.enabled = False self.logformat = options.logcapture_format self.logdatefmt = options.logcapture_datefmt self.clear = options.logcapture_clear self.loglevel = options.logcapture_level if options.logcapture_filters: self.filters = options.logcapture_filters.split(',')
Add captured log messages to error output.
def formatError(self, test, err): """Add captured log messages to error output. """ # logic flow copied from Capture.formatError test.capturedLogging = records = self.formatLogRecords() if not records: return err ec, ev, tb = err return (ec, self.addCaptureToErr(ev, records), tb)
Call this to embed IPython at the current point in your program.
def embed(**kwargs): """Call this to embed IPython at the current point in your program. The first invocation of this will create an :class:`InteractiveShellEmbed` instance and then call it. Consecutive calls just call the already created instance. Here is a simple example:: from IPython import embed a = 10 b = 20 embed('First time') c = 30 d = 40 embed Full customization can be done by passing a :class:`Struct` in as the config argument. """ config = kwargs.get('config') header = kwargs.pop('header', u'') if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config global _embedded_shell if _embedded_shell is None: _embedded_shell = InteractiveShellEmbed(**kwargs) _embedded_shell(header=header, stack_depth=2)
Embeds IPython into a running python program.
def mainloop(self, local_ns=None, module=None, stack_depth=0, display_banner=None, global_ns=None): """Embeds IPython into a running python program. Input: - header: An optional header message can be specified. - local_ns, module: working local namespace (a dict) and module (a module or similar object). If given as None, they are automatically taken from the scope where the shell was called, so that program variables become visible. - stack_depth: specifies how many levels in the stack to go to looking for namespaces (when local_ns or module is None). This allows an intermediate caller to make sure that this function gets the namespace from the intended level in the stack. By default (0) it will get its locals and globals from the immediate caller. Warning: it's possible to use this in a program which is being run by IPython itself (via %run), but some funny things will happen (a few globals get overwritten). In the future this will be cleaned up, as there is no fundamental reason why it can't work perfectly.""" if (global_ns is not None) and (module is None): class DummyMod(object): """A dummy module object for embedded IPython.""" pass warnings.warn("global_ns is deprecated, use module instead.", DeprecationWarning) module = DummyMod() module.__dict__ = global_ns # Get locals and globals from caller if (local_ns is None or module is None) and self.default_user_namespaces: call_frame = sys._getframe(stack_depth).f_back if local_ns is None: local_ns = call_frame.f_locals if module is None: global_ns = call_frame.f_globals module = sys.modules[global_ns['__name__']] # Save original namespace and module so we can restore them after # embedding; otherwise the shell doesn't shut down correctly. orig_user_module = self.user_module orig_user_ns = self.user_ns # Update namespaces and fire up interpreter # The global one is easy, we can just throw it in if module is not None: self.user_module = module # But the user/local one is tricky: ipython needs it to store internal # data, but we also need the locals. We'll throw our hidden variables # like _ih and get_ipython() into the local namespace, but delete them # later. if local_ns is not None: self.user_ns = local_ns self.init_user_ns() # Patch for global embedding to make sure that things don't overwrite # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> # FIXME. Test this a bit more carefully (the if.. is new) # N.B. This can't now ever be called. Not sure what it was for. # And now, since it wasn't called in the previous version, I'm # commenting out these lines so they can't be called with my new changes # --TK, 2011-12-10 #if local_ns is None and module is None: # self.user_global_ns.update(__main__.__dict__) # make sure the tab-completer has the correct frame information, so it # actually completes using the frame's locals/globals self.set_completer_frame() with nested(self.builtin_trap, self.display_trap): self.interact(display_banner=display_banner) # now, purge out the local namespace of IPython's hidden variables. if local_ns is not None: for name in self.user_ns_hidden: local_ns.pop(name, None) # Restore original namespace so shell can shut down when we exit. self.user_module = orig_user_module self.user_ns = orig_user_ns
dir2 ( obj ) - > list of strings
def dir2(obj): """dir2(obj) -> list of strings Extended version of the Python builtin dir(), which does a few extra checks, and supports common objects with unusual internals that confuse dir(), such as Traits and PyCrust. This version is guaranteed to return only a list of true strings, whereas dir() returns anything that objects inject into themselves, even if they are later not really valid for attribute access (many extension libraries have such bugs). """ # Start building the attribute list via dir(), and then complete it # with a few extra special-purpose calls. words = set(dir(obj)) if hasattr(obj, '__class__'): #words.add('__class__') words |= set(get_class_members(obj.__class__)) # for objects with Enthought's traits, add trait_names() list # for PyCrust-style, add _getAttributeNames() magic method list for attr in ('trait_names', '_getAttributeNames'): if hasattr(obj, attr): try: func = getattr(obj, attr) if callable(func): words |= set(func()) except: # TypeError: obj is class not instance pass # filter out non-string attributes which may be stuffed by dir() calls # and poor coding in third-party modules words = [w for w in words if isinstance(w, basestring)] return sorted(words)
Get all po filenames from locale folder and return list of them. Assumes a directory structure: <locale_root >/ <lang >/ <po_files_path >/ <filename >.
def _get_all_po_filenames(locale_root, lang, po_files_path): """ Get all po filenames from locale folder and return list of them. Assumes a directory structure: <locale_root>/<lang>/<po_files_path>/<filename>. """ all_files = os.listdir(os.path.join(locale_root, lang, po_files_path)) return filter(lambda s: s.endswith('.po'), all_files)
Prepare new csv writers write title rows and return them.
def _get_new_csv_writers(trans_title, meta_title, trans_csv_path, meta_csv_path): """ Prepare new csv writers, write title rows and return them. """ trans_writer = UnicodeWriter(trans_csv_path) trans_writer.writerow(trans_title) meta_writer = UnicodeWriter(meta_csv_path) meta_writer.writerow(meta_title) return trans_writer, meta_writer
Prepare locale dirs for writing po files. Create new directories if they doesn t exist.
def _prepare_locale_dirs(languages, locale_root): """ Prepare locale dirs for writing po files. Create new directories if they doesn't exist. """ trans_languages = [] for i, t in enumerate(languages): lang = t.split(':')[0] trans_languages.append(lang) lang_path = os.path.join(locale_root, lang) if not os.path.exists(lang_path): os.makedirs(lang_path) return trans_languages
Prepare polib file object for writing/ reading from them. Create directories and write header if needed. For each language ensure there s a translation file named filename in the correct place. Assumes ( and creates ) a directory structure: <locale_root >/ <lang >/ <po_files_path >/ <filename >.
def _prepare_polib_files(files_dict, filename, languages, locale_root, po_files_path, header): """ Prepare polib file object for writing/reading from them. Create directories and write header if needed. For each language, ensure there's a translation file named "filename" in the correct place. Assumes (and creates) a directory structure: <locale_root>/<lang>/<po_files_path>/<filename>. """ files_dict[filename] = {} for lang in languages: file_path = os.path.join(locale_root, lang, po_files_path) if not os.path.exists(file_path): os.makedirs(file_path) if header is not None: _write_header(os.path.join(file_path, filename), lang, header) files_dict[filename][lang] = polib.pofile( os.path.join(file_path, filename), encoding="UTF-8")
Write msgstr for every language with all needed metadata and comment. Metadata are parser from string into dict so read them only from gdocs.
def _write_entries(po_files, languages, msgid, msgstrs, metadata, comment): """ Write msgstr for every language with all needed metadata and comment. Metadata are parser from string into dict, so read them only from gdocs. """ start = re.compile(r'^[\s]+') end = re.compile(r'[\s]+$') for i, lang in enumerate(languages): meta = ast.literal_eval(metadata) entry = polib.POEntry(**meta) entry.tcomment = comment entry.msgid = msgid if msgstrs[i]: start_ws = start.search(msgid) end_ws = end.search(msgid) entry.msgstr = str(start_ws.group() if start_ws else '') + \ unicode(msgstrs[i].strip()) + \ str(end_ws.group() if end_ws else '') else: entry.msgstr = '' po_files[lang].append(entry)
Write header into po file for specific lang. Metadata are read from settings file.
def _write_header(po_path, lang, header): """ Write header into po file for specific lang. Metadata are read from settings file. """ po_file = open(po_path, 'w') po_file.write(header + '\n') po_file.write( 'msgid ""' + '\nmsgstr ""' + '\n"MIME-Version: ' + settings.METADATA['MIME-Version'] + r'\n"' '\n"Content-Type: ' + settings.METADATA['Content-Type'] + r'\n"' '\n"Content-Transfer-Encoding: ' + settings.METADATA['Content-Transfer-Encoding'] + r'\n"' '\n"Language: ' + lang + r'\n"' + '\n') po_file.close()
Write new msgids which appeared in po files with empty msgstrs values and metadata. Look for all new msgids which are diffed with msgids list provided as an argument.
def _write_new_messages(po_file_path, trans_writer, meta_writer, msgids, msgstrs, languages): """ Write new msgids which appeared in po files with empty msgstrs values and metadata. Look for all new msgids which are diffed with msgids list provided as an argument. """ po_filename = os.path.basename(po_file_path) po_file = polib.pofile(po_file_path) new_trans = 0 for entry in po_file: if entry.msgid not in msgids: new_trans += 1 trans = [po_filename, entry.tcomment, entry.msgid, entry.msgstr] for lang in languages[1:]: trans.append(msgstrs[lang].get(entry.msgid, '')) meta = dict(entry.__dict__) meta.pop('msgid', None) meta.pop('msgstr', None) meta.pop('tcomment', None) trans_writer.writerow(trans) meta_writer.writerow([str(meta)]) return new_trans
Write new msgids which appeared in po files with empty msgstrs values and metadata. Look for all new msgids which are diffed with msgids list provided as an argument.
def _get_new_msgstrs(po_file_path, msgids): """ Write new msgids which appeared in po files with empty msgstrs values and metadata. Look for all new msgids which are diffed with msgids list provided as an argument. """ po_file = polib.pofile(po_file_path) msgstrs = {} for entry in po_file: if entry.msgid not in msgids: msgstrs[entry.msgid] = entry.msgstr return msgstrs
Converts po file to csv GDocs spreadsheet readable format. Merges them if some msgid aren t in the spreadsheet.: param languages: list of language codes: param locale_root: path to locale root folder containing directories with languages: param po_files_path: path from lang directory to po file: param local_trans_csv: path where local csv with translations will be created: param local_meta_csv: path where local csv with metadata will be created: param gdocs_trans_csv: path to gdoc csv with translations
def po_to_csv_merge(languages, locale_root, po_files_path, local_trans_csv, local_meta_csv, gdocs_trans_csv, gdocs_meta_csv): """ Converts po file to csv GDocs spreadsheet readable format. Merges them if some msgid aren't in the spreadsheet. :param languages: list of language codes :param locale_root: path to locale root folder containing directories with languages :param po_files_path: path from lang directory to po file :param local_trans_csv: path where local csv with translations will be created :param local_meta_csv: path where local csv with metadata will be created :param gdocs_trans_csv: path to gdoc csv with translations """ msgids = [] trans_reader = UnicodeReader(gdocs_trans_csv) meta_reader = UnicodeReader(gdocs_meta_csv) try: trans_title = trans_reader.next() meta_title = meta_reader.next() except StopIteration: trans_title = ['file', 'comment', 'msgid'] trans_title += map(lambda s: s + ':msgstr', languages) meta_title = ['metadata'] trans_writer, meta_writer = _get_new_csv_writers( trans_title, meta_title, local_trans_csv, local_meta_csv) for trans_row, meta_row in izip_longest(trans_reader, meta_reader): msgids.append(trans_row[2]) trans_writer.writerow(trans_row) meta_writer.writerow(meta_row if meta_row else [METADATA_EMPTY]) trans_reader.close() meta_reader.close() po_files = _get_all_po_filenames(locale_root, languages[0], po_files_path) new_trans = False for po_filename in po_files: new_msgstrs = {} for lang in languages[1:]: po_file_path = os.path.join(locale_root, lang, po_files_path, po_filename) if not os.path.exists(po_file_path): open(po_file_path, 'a').close() new_msgstrs[lang] = _get_new_msgstrs(po_file_path, msgids) if len(new_msgstrs[languages[1]].keys()) > 0: new_trans = True po_file_path = os.path.join(locale_root, languages[0], po_files_path, po_filename) _write_new_messages(po_file_path, trans_writer, meta_writer, msgids, new_msgstrs, languages) trans_writer.close() meta_writer.close() return new_trans
Converts GDocs spreadsheet generated csv file into po file.: param trans_csv_path: path to temporary file with translations: param meta_csv_path: path to temporary file with meta information: param locale_root: path to locale root folder containing directories with languages: param po_files_path: path from lang directory to po file
def csv_to_po(trans_csv_path, meta_csv_path, locale_root, po_files_path, header=None): """ Converts GDocs spreadsheet generated csv file into po file. :param trans_csv_path: path to temporary file with translations :param meta_csv_path: path to temporary file with meta information :param locale_root: path to locale root folder containing directories with languages :param po_files_path: path from lang directory to po file """ pattern = "^\w+.*po$" for root, dirs, files in os.walk(locale_root): for f in filter(lambda x: re.match(pattern, x), files): os.remove(os.path.join(root, f)) # read title row and prepare descriptors for po files in each lang trans_reader = UnicodeReader(trans_csv_path) meta_reader = UnicodeReader(meta_csv_path) try: title_row = trans_reader.next() except StopIteration: # empty file return trans_languages = _prepare_locale_dirs(title_row[3:], locale_root) po_files = {} meta_reader.next() # go through every row in downloaded csv file for trans_row, meta_row in izip_longest(trans_reader, meta_reader): filename = trans_row[0].rstrip() metadata = meta_row[0].rstrip() if meta_row else METADATA_EMPTY comment = trans_row[1] msgid = trans_row[2] if filename not in po_files: _prepare_polib_files(po_files, filename, trans_languages, locale_root, po_files_path, header) _write_entries(po_files[filename], trans_languages, msgid, trans_row[3:], metadata, comment) for filename in po_files: for lang in po_files[filename]: po_files[filename][lang].save() trans_reader.close() meta_reader.close()
method to subscribe a user to a service
def subscribe_user(self, user): """ method to subscribe a user to a service """ url = self.root_url + "subscribe_user" values = {} values["username"] = user return self._query(url, values)
method to send a message to a user
def send_notification(self, to=None, msg=None, label=None, title=None, uri=None): """ method to send a message to a user Parameters: to -> recipient msg -> message to send label -> application description title -> name of the notification event uri -> callback uri """ url = self.root_url + "send_notification" values = {} if to is not None: values["to"] = to if msg is not None: values["msg"] = msg if label is not None: values["label"] = label if title is not None: values["title"] = title if uri is not None: values["uri"] = uri return self._query(url, values)
method to send a message to a user
def send_message(self, to=None, msg=None): """ method to send a message to a user Parameters: to -> recipient msg -> message to send """ url = self.root_url + "send_message" values = {} if to is not None: values["to"] = to if msg is not None: values["msg"] = msg return self._query(url, values)
query method to do HTTP POST/ GET
def _query(self, url, data = None): """ query method to do HTTP POST/GET Parameters: url -> the url to POST/GET data -> header_data as a dict (only for POST) Returns: Parsed JSON data as dict or None on error """ auth = encodestring('%s:%s' % (self.user, self.secret)).replace('\n', '') if data is not None: # we have POST data if there is data values = urllib.urlencode(data) request = urllib2.Request(url, values) request.add_header("Authorization", "Basic %s" % auth) else: # do a GET otherwise request = urllib2.Request(url) request.add_header("Authorization", "Basic %s" % auth) try: response = urllib2.urlopen(request) except IOError, e: # no connection return {"status" : "error", "response_code" : e.code, "response_message" : e.msg } return json.loads(response.read())
function to init option parser
def init_parser(): """ function to init option parser """ usage = "usage: %prog -u user -s secret -n name [-l label] \ [-t title] [-c callback] [TEXT]" parser = OptionParser(usage, version="%prog " + notifo.__version__) parser.add_option("-u", "--user", action="store", dest="user", help="your notifo username") parser.add_option("-s", "--secret", action="store", dest="secret", help="your notifo API secret") parser.add_option("-n", "--name", action="store", dest="name", help="recipient for the notification") parser.add_option("-l", "--label", action="store", dest="label", help="label for the notification") parser.add_option("-t", "--title", action="store", dest="title", help="title of the notification") parser.add_option("-c", "--callback", action="store", dest="callback", help="callback URL to call") parser.add_option("-m", "--message", action="store_true", dest="message", default=False, help="send message instead of notification") (options, args) = parser.parse_args() return (parser, options, args)
main function
def main(): """ main function """ # get options and arguments (parser, options, args) = init_parser() # initialize result variable result = None # check for values which are always needed if not options.user: parser.error("No user given.") if not options.secret: parser.error("No API secret given.") if not options.name: parser.error("No recipient given.") # If there is no message, we probably want to subscribe a user if len(args) < 1: result = notifo.subscribe_user(options.user, options.secret, options.name) else: params = {} params["to"] = options.name m = '' for a in args: m = "%s %s" %(m, a) params["msg"] = m if options.message == True: result = notifo.send_message(options.user, options.secret, **params) else: if options.label: params["label"] = options.label if options.title: params["title"] = options.title if options.callback: params["uri"] = options.callback result = notifo.send_notification(options.user,options.secret, **params) if result is None: print "Something went wrong. Check parameters and try again."
The same as s. rsplit ( sep 1 ) but works in 2. 3
def rsplit1(s, sep): """The same as s.rsplit(sep, 1), but works in 2.3""" parts = s.split(sep) return sep.join(parts[:-1]), parts[-1]
Run a python module as though with python - m name args....
def run_python_module(modulename, args): """Run a python module, as though with ``python -m name args...``. `modulename` is the name of the module, possibly a dot-separated name. `args` is the argument array to present as sys.argv, including the first element naming the module being executed. """ openfile = None glo, loc = globals(), locals() try: try: # Search for the module - inside its parent package, if any - using # standard import mechanics. if '.' in modulename: packagename, name = rsplit1(modulename, '.') package = __import__(packagename, glo, loc, ['__path__']) searchpath = package.__path__ else: packagename, name = None, modulename searchpath = None # "top-level search" in imp.find_module() openfile, pathname, _ = imp.find_module(name, searchpath) # Complain if this is a magic non-file module. if openfile is None and pathname is None: raise NoSource( "module does not live in a file: %r" % modulename ) # If `modulename` is actually a package, not a mere module, then we # pretend to be Python 2.7 and try running its __main__.py script. if openfile is None: packagename = modulename name = '__main__' package = __import__(packagename, glo, loc, ['__path__']) searchpath = package.__path__ openfile, pathname, _ = imp.find_module(name, searchpath) except ImportError: _, err, _ = sys.exc_info() raise NoSource(str(err)) finally: if openfile: openfile.close() # Finally, hand the file off to run_python_file for execution. pathname = os.path.abspath(pathname) args[0] = pathname run_python_file(pathname, args, package=packagename)
Run a python file as if it were the main program on the command line.
def run_python_file(filename, args, package=None): """Run a python file as if it were the main program on the command line. `filename` is the path to the file to execute, it need not be a .py file. `args` is the argument array to present as sys.argv, including the first element naming the file being executed. `package` is the name of the enclosing package, if any. """ # Create a module to serve as __main__ old_main_mod = sys.modules['__main__'] main_mod = imp.new_module('__main__') sys.modules['__main__'] = main_mod main_mod.__file__ = filename if package: main_mod.__package__ = package main_mod.__builtins__ = BUILTINS # Set sys.argv properly. old_argv = sys.argv sys.argv = args try: # Make a code object somehow. if filename.endswith(".pyc") or filename.endswith(".pyo"): code = make_code_from_pyc(filename) else: code = make_code_from_py(filename) # Execute the code object. try: exec_code_object(code, main_mod.__dict__) except SystemExit: # The user called sys.exit(). Just pass it along to the upper # layers, where it will be handled. raise except: # Something went wrong while executing the user code. # Get the exc_info, and pack them into an exception that we can # throw up to the outer loop. We peel two layers off the traceback # so that the coverage.py code doesn't appear in the final printed # traceback. typ, err, tb = sys.exc_info() raise ExceptionDuringRun(typ, err, tb.tb_next.tb_next) finally: # Restore the old __main__ sys.modules['__main__'] = old_main_mod # Restore the old argv and path sys.argv = old_argv
Get source from filename and make a code object of it.
def make_code_from_py(filename): """Get source from `filename` and make a code object of it.""" # Open the source file. try: source_file = open_source(filename) except IOError: raise NoSource("No file to run: %r" % filename) try: source = source_file.read() finally: source_file.close() # We have the source. `compile` still needs the last line to be clean, # so make sure it is, then compile a code object from it. if not source or source[-1] != '\n': source += '\n' code = compile(source, filename, "exec") return code
Get a code object from a. pyc file.
def make_code_from_pyc(filename): """Get a code object from a .pyc file.""" try: fpyc = open(filename, "rb") except IOError: raise NoCode("No file to run: %r" % filename) try: # First four bytes are a version-specific magic number. It has to # match or we won't run the file. magic = fpyc.read(4) if magic != imp.get_magic(): raise NoCode("Bad magic number in .pyc file") # Skip the junk in the header that we don't need. fpyc.read(4) # Skip the moddate. if sys.version_info >= (3, 3): # 3.3 added another long to the header (size), skip it. fpyc.read(4) # The rest of the file is the code object we want. code = marshal.load(fpyc) finally: fpyc.close() return code
returnr a string for an html table
def html_tableify(item_matrix, select=None, header=None , footer=None) : """ returnr a string for an html table""" if not item_matrix : return '' html_cols = [] tds = lambda text : u'<td>'+text+u' </td>' trs = lambda text : u'<tr>'+text+u'</tr>' tds_items = [map(tds, row) for row in item_matrix] if select : row, col = select tds_items[row][col] = u'<td class="inverted">'\ +item_matrix[row][col]\ +u' </td>' #select the right item html_cols = map(trs, (u''.join(row) for row in tds_items)) head = '' foot = '' if header : head = (u'<tr>'\ +''.join((u'<td>'+header+u'</td>')*len(item_matrix[0]))\ +'</tr>') if footer : foot = (u'<tr>'\ +''.join((u'<td>'+footer+u'</td>')*len(item_matrix[0]))\ +'</tr>') html = (u'<table class="completion" style="white-space:pre">'+head+(u''.join(html_cols))+foot+u'</table>') return html
set current cursor position
def current(self, value): """set current cursor position""" current = min(max(self._min, value), self._max) self._current = current if current > self._stop : self._stop = current self._start = current-self._width elif current < self._start : self._start = current self._stop = current + self._width if abs(self._start - self._min) <= self._sticky_lenght : self._start = self._min if abs(self._stop - self._max) <= self._sticky_lenght : self._stop = self._max