Skip to content

Commit

Permalink
prepare for distributing
Browse files Browse the repository at this point in the history
  • Loading branch information
panyanyany committed Jun 16, 2016
1 parent 76c26fc commit a77d51d
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 57 deletions.
1 change: 1 addition & 0 deletions README.md
45 changes: 43 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
beeprint: Beautiful Print
beeprint: Beautifully Print
===
pprint is good, but not clean. So beeprint do it.

Features
===
- print dict elegantly
- format of sequential type is controllable
- outstanding mark to class and instance
- compatible with py2 py3 in same output

Examples
===

Plain Variables
Complicated data
---
```
[
1,
1.1,
's',
'us',
'a中文',
'a中文',
[1],
(1, 2),
function(EmptyFunc),
class(EmptyClassOldStyle),
class(EmptyClassNewStyle),
class(NormalClassOldStyle):
static_props: 1,
class(NormalClassNewStyle):
dicts: {
},
lists: [],
static_props: 1,
instance(NormalClassOldStyle):
static_props: 1,
instance(NormalClassNewStyle):
dicts: {
},
lists: [],
static_props: 1,
method(mth),
method(mth),
{
'key': [],
'key2': {
},
},
]
```

8 changes: 4 additions & 4 deletions beeprint/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
_AS_CLASS_ELEMENT_ = 16

# string type
ST_LITERAL = 1 # string literal depends on script's coding
ST_UNICODE = 2
ST_BYTES = 4
ST_UNDEFINED = 0
_ST_LITERAL_ = 1 # string literal depends on script's coding
_ST_UNICODE_ = 2
_ST_BYTES_ = 4
_ST_UNDEFINED_ = 0

# debug level
_DL_MODULE_ = 1
Expand Down
24 changes: 12 additions & 12 deletions beeprint/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def dict_key_filter(obj, name, val):

def _b(s):
if S.write_to_buffer_when_execute:
S.bufferHandle.write(s)
S.bufferHandle.flush()
S.buffer_handler.write(s)
S.buffer_handler.flush()
return s

def pstr(s):
Expand Down Expand Up @@ -151,7 +151,7 @@ def build_single_block(obj, leadCnt=0, position=C._AS_ELEMENT_):

tail = tail_symbol(position)

if S.maxDeep < leadCnt:
if S.max_depth < leadCnt:
if S.newline or position & C._AS_ELEMENT_:
ret = pstr(leadCnt * S.leading) + pstr("<OUT OF RANGE>\n")
else:
Expand Down Expand Up @@ -199,7 +199,7 @@ def build_pair_block(name, val, leadCnt=0, position=C._AS_ELEMENT_):
else:
name = typeval(name)
ret += _b(S.leading * leadCnt + name + ':')
if is_extendable(val) and S.maxDeep > leadCnt:
if is_extendable(val) and S.max_depth > leadCnt:
# value need to be dispalyed on new line
# including:
# class type & class instance
Expand All @@ -217,7 +217,7 @@ def build_pair_block(name, val, leadCnt=0, position=C._AS_ELEMENT_):

ret += build_single_block(val, leadCnt, position)
else:
if S.maxDeep <= leadCnt:
if S.max_depth <= leadCnt:
ret += _b(pstr(" <OUT OF RANGE>%s\n" % tail))
else:
ret += _b(pstr(" ") + typeval(val) + pstr(tail + '\n'))
Expand Down Expand Up @@ -394,12 +394,12 @@ def typeval(v):
if S.united_str_coding_representation:
st = string_type(v)
ret = u''
if st & C.ST_UNICODE != 0:
if st & C._ST_UNICODE_ != 0:
if S.str_display_not_prefix_u:
ret = u"'" + v + u"'"
else:
ret = u"u'" + v + u"'"
elif st & C.ST_BYTES != 0:
elif st & C._ST_BYTES_ != 0:
# in py3, printed string will enclose with b''
# ret = pstr(v)
if S.str_display_not_prefix_b:
Expand Down Expand Up @@ -432,20 +432,20 @@ def string_type(s):
# a literal string is str (i.e: coding encoded, eg: utf8)
# a u-prefixed string is unicode
if isinstance(s, unicode):
return C.ST_UNICODE
return C._ST_UNICODE_
elif isinstance(s, str): # same as isinstance(v, bytes)
return C.ST_LITERAL | C.ST_BYTES
return C._ST_LITERAL_ | C._ST_BYTES_
else:
# in py3,
# a literal string is str (i.e: unicode encoded)
# a u-prefixed string is str
# a utf8 string is bytes
if isinstance(s, bytes):
return C.ST_BYTES
return C._ST_BYTES_
elif isinstance(s, str):
return C.ST_LITERAL | C.ST_UNICODE
return C._ST_LITERAL_ | C._ST_UNICODE_

return C.ST_UNDEFINED
return C._ST_UNDEFINED_


def is_newline_obj(o):
Expand Down
17 changes: 13 additions & 4 deletions beeprint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@
from . import constants as C
from . import utils

outfile = sys.stdout
# >> coding
encoding = 'utf-8'
maxDeep = 5

# >> representation
max_depth = 5
leading = u' '
newline = False
write_to_buffer_when_execute = False
bufferHandle = sys.stdout
tuple_in_line = True
list_in_line = True

# >> buffer
buffer_handler = sys.stdout
# use buffer_handler.flush() every print
write_to_buffer_when_execute = False

# >> class controll
# 过滤以 x 开头的属性
prop_leading_filters = ["__", "func_"]
# 根据类型过滤对象的属性
prop_filters = [utils.is_pan_function, 'im_func', 'im_self', 'im_class']

# >> 优先策略
# to raise exception or not when errors happened
# _PS_CONTENT_FIRST will keep content printing despite of any error
priority_strategy = C._PS_CONTENT_FIRST

# debug = False
Expand Down
14 changes: 7 additions & 7 deletions tests/data/tests_complicate_data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
'a中文',
[1],
(1, 2),
function(f),
class(CE),
class(CE2),
class(c):
function(EmptyFunc),
class(EmptyClassOldStyle),
class(EmptyClassNewStyle),
class(NormalClassOldStyle):
static_props: 1,
class(c2):
class(NormalClassNewStyle):
dicts: {
},
lists: [],
static_props: 1,
instance(c):
instance(NormalClassOldStyle):
static_props: 1,
instance(c2):
instance(NormalClassNewStyle):
dicts: {
},
lists: [],
Expand Down
14 changes: 7 additions & 7 deletions tests/data/tests_complicate_data2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
'a中文',
[1],
(1, 2),
function(f),
class(CE),
class(CE2),
class(c):
function(EmptyFunc),
class(EmptyClassOldStyle),
class(EmptyClassNewStyle),
class(NormalClassOldStyle):
static_props: 1,
class(c2):
class(NormalClassNewStyle):
dicts: {
},
lists: [],
static_props: 1,
instance(c):
instance(NormalClassOldStyle):
static_props: 1,
instance(c2):
instance(NormalClassNewStyle):
dicts: {
},
lists: [],
Expand Down
32 changes: 16 additions & 16 deletions tests/definition.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# -*- coding:utf-8 -*-

def f(): pass
def EmptyFunc(): pass

class CE: pass
class CE2(object): pass
class EmptyClassOldStyle: pass
class EmptyClassNewStyle(object): pass

class c:
class NormalClassOldStyle:
def mth():pass
static_props = 1

class c2(object):
class NormalClassNewStyle(object):
def mth():pass
static_props = 1
lists = []
dicts = {}

ic = c()
ic2 = c2()
inst_of_normal_class_old_style = NormalClassOldStyle()
inst_of_normal_class_new_style = NormalClassNewStyle()

values = [
1,
Expand All @@ -27,15 +27,15 @@ def mth():pass
u"a中文",
[1],
(1,2),
f,
CE,
CE2,
c,
c2,
ic,
ic2,
ic.mth,
ic2.mth,
EmptyFunc,
EmptyClassOldStyle,
EmptyClassNewStyle,
NormalClassOldStyle,
NormalClassNewStyle,
inst_of_normal_class_old_style,
inst_of_normal_class_new_style,
inst_of_normal_class_old_style.mth,
inst_of_normal_class_new_style.mth,
{
'key': [],
u'key2': {},
Expand Down
10 changes: 5 additions & 5 deletions tests/t.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

try:
from .definition import values
from .definition import ic, ic2, c, c2, f
from .definition import inst_of_normal_class_old_style, inst_of_normal_class_new_style, NormalClassOldStyle, NormalClassNewStyle, EmptyFunc
except:
from definition import values
from definition import ic, ic2, c, c2, f
from definition import inst_of_normal_class_old_style, inst_of_normal_class_new_style, NormalClassOldStyle, NormalClassNewStyle, EmptyFunc


# >> utilities
Expand Down Expand Up @@ -70,12 +70,12 @@ def inst_test():
else:
print('%40s: %s' % (v, isinstance(v, object)))

same_attrs = detect_same_attrs(ic, ic2)
same_attrs = detect_same_attrs(inst_of_normal_class_old_style, inst_of_normal_class_new_style)
for attr, v in same_attrs:
print('%40s: %s' % (attr, v))

def builtin_test():
for v in [f, c.mth, c2.mth, ic.mth, ic2.mth]:
for v in [EmptyFunc, NormalClassOldStyle.mth, NormalClassNewStyle.mth, inst_of_normal_class_old_style.mth, inst_of_normal_class_new_style.mth]:
# print('%40s: %s' % (v, isinstance(v, types.MethodType))) py2 all true
# print('%40s: %s' % (v, inspect.ismethod(v))) py2 all true
# print('%40s: %s' % (v, inspect.isbuiltin(v))) py2 all false
Expand All @@ -95,7 +95,7 @@ def main():
# S.str_display_not_prefix_b = False

pp(values)
# pp([ic.mth, ic2.mth])
# pp([inst_of_normal_class_old_style.mth, inst_of_normal_class_new_style.mth])
return

for i in range(1, len(sys.argv)):
Expand Down

0 comments on commit a77d51d

Please sign in to comment.