- 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):
|
def __getattr__(self, str name):
|
||||||
if name in self:
|
if name in self:
|
||||||
return self[name]
|
return self[name]
|
||||||
|
elif name.replace('_', '-') in self:
|
||||||
|
return self[name.replace('_', '-')]
|
||||||
else:
|
else:
|
||||||
raise AttributeError("'ndict' object has no attribute '%s'" %(name))
|
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'):
|
with open(fname, 'a'):
|
||||||
os.utime(fname, times)
|
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
|
||||||
|
|
12
core/misc.py
12
core/misc.py
|
@ -1,6 +1,7 @@
|
||||||
#! /bin/env python3
|
#! /bin/env python3
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
import copy
|
||||||
import imp
|
import imp
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@ -48,17 +49,24 @@ 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.
|
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
|
tab_completion enables completion of object names using TAB, however note
|
||||||
that this will make pasting formatted snippets of code difficult
|
that this will make pasting formatted snippets of code difficult
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import code, readline, rlcompleter
|
import code, readline, rlcompleter
|
||||||
|
|
||||||
|
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')
|
readline.parse_and_bind('tab: complete')
|
||||||
|
|
||||||
c = code.InteractiveConsole(environment)
|
c = code.InteractiveConsole(environment)
|
||||||
|
|
|
@ -21,6 +21,8 @@ class ndict(dict):
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
if name in self:
|
if name in self:
|
||||||
return self[name]
|
return self[name]
|
||||||
|
elif name.replace('_', '-') in self:
|
||||||
|
return self[name.replace('_', '-')]
|
||||||
else:
|
else:
|
||||||
raise AttributeError("'ndict' object has no attribute '%s'" %(name))
|
raise AttributeError("'ndict' object has no attribute '%s'" %(name))
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ def lexical_join(words, oxford=False):
|
||||||
elif l == 2:
|
elif l == 2:
|
||||||
return '%s and %s' %(str(words[0]), str(words[1]))
|
return '%s and %s' %(str(words[0]), str(words[1]))
|
||||||
else:
|
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