From 402a05f86d17e7cdb34067c57c0358f4e9e43653 Mon Sep 17 00:00:00 2001 From: Overwatch Date: Wed, 14 Jan 2015 13:14:34 +0100 Subject: [PATCH] - over.core.misc.console now accepts globals as well as locals to offer a complete environment - new over.core.file.count_lines - over.core.types.ndict can now export keys with dashes in them same way over.core.app.Main does it (by translating them to underscores) --- core/cython_types.pyx | 2 ++ core/file.py | 16 ++++++++++++++++ core/misc.py | 14 +++++++++++--- core/python_types.py | 2 ++ core/text.py | 2 +- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/cython_types.pyx b/core/cython_types.pyx index 0568f83..6c843ab 100644 --- a/core/cython_types.pyx +++ b/core/cython_types.pyx @@ -21,6 +21,8 @@ cdef class ndict(dict): def __getattr__(self, str name): if name in self: return self[name] + elif name.replace('_', '-') in self: + return self[name.replace('_', '-')] else: raise AttributeError("'ndict' object has no attribute '%s'" %(name)) diff --git a/core/file.py b/core/file.py index 0709140..9315913 100644 --- a/core/file.py +++ b/core/file.py @@ -85,3 +85,19 @@ def touch(fname, times=None): with open(fname, 'a'): os.utime(fname, times) + +# -------------------------------------------------- + +def count_lines(filename): + ''' + A reasonably fast and memory-lean line counter. + ''' + + lines = 0 + buffer = bytearray(2048) + + with open(filename, 'rb') as f: + while f.readinto(buffer): + lines += buffer.count(b'\n') + + return lines diff --git a/core/misc.py b/core/misc.py index b71a114..d370e4e 100644 --- a/core/misc.py +++ b/core/misc.py @@ -1,6 +1,7 @@ #! /bin/env python3 # encoding: utf-8 +import copy import imp import os @@ -48,18 +49,25 @@ def batch_gen(data, batch_size): # -------------------------------------------------- -def console(environment, tab_completion=False): +def console(locals, globals=None, tab_completion=False): ''' Opens up a Python console. - environment is a dictionary typically returned by locals() or similar tab_completion enables completion of object names using TAB, however note that this will make pasting formatted snippets of code difficult ''' import code, readline, rlcompleter - readline.parse_and_bind('tab: complete') + environment = copy.copy(locals) + + if globals: + for key, value in globals.items(): + if key not in environment: + environment[key] = value + + if tab_completion: + readline.parse_and_bind('tab: complete') c = code.InteractiveConsole(environment) c.interact(banner='') diff --git a/core/python_types.py b/core/python_types.py index 9815d8e..801e0f2 100644 --- a/core/python_types.py +++ b/core/python_types.py @@ -21,6 +21,8 @@ class ndict(dict): def __getattr__(self, name): if name in self: return self[name] + elif name.replace('_', '-') in self: + return self[name.replace('_', '-')] else: raise AttributeError("'ndict' object has no attribute '%s'" %(name)) diff --git a/core/text.py b/core/text.py index a357b37..0132b5c 100644 --- a/core/text.py +++ b/core/text.py @@ -37,7 +37,7 @@ def lexical_join(words, oxford=False): elif l == 2: return '%s and %s' %(str(words[0]), str(words[1])) else: - return '%s%s and %s' %(', '.join(str(w) for w in words[:-1]), ',' if oxford_comma else '', str(words[-1])) + return '%s%s and %s' %(', '.join(str(w) for w in words[:-1]), ',' if oxford else '', str(words[-1])) # --------------------------------------------------