- 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)
This commit is contained in:
parent
043ad86808
commit
402a05f86d
5 changed files with 32 additions and 4 deletions
|
@ -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))
|
||||
|
||||
|
|
16
core/file.py
16
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
|
||||
|
|
14
core/misc.py
14
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='')
|
||||
|
|
|
@ -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))
|
||||
|
||||
|
|
|
@ -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]))
|
||||
|
||||
# --------------------------------------------------
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue