Skip to content

Commit

Permalink
Merge pull request #2 from panyanyany/dev
Browse files Browse the repository at this point in the history
basically done
  • Loading branch information
panyanyany authored Aug 26, 2016
2 parents 518c405 + fd0ec71 commit af465a5
Show file tree
Hide file tree
Showing 21 changed files with 2,184 additions and 543 deletions.
1 change: 0 additions & 1 deletion README.md

This file was deleted.

178 changes: 178 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
beeprint
===
make your debug printing more friendly

Features
===
- print dict elegantly
- auto wrap text, including English and Chinese
- outstanding mark to class and instance
- compatible with py2 py3

Contents
===
- [Examples](#examples)
- [Installation](#installation)
- [Settings](#settings)

Examples
===

Import beeprint as pp
---
```
from beeprint.printer import beeprint as pp
```

Short List
---

```
In [2]: alist = [1, 2, 3, 4, 5, 6]
In [3]: pp(alist)
[1, 2, 3, 4, 5, 6]
```

Complicated List
---
```
In [4]: clist = [1, [2], {'key': 'val'}]
In [5]: pp(clist)
[
1,
[2],
{
'key': 'val',
},
]
```

Class Instance
---
```
In [6]: class NormalClassNewStyle(object):
...: def mth():pass
...: static_props = 1
...: lists = []
...: dicts = {}
...: tupl = (1,2)
...:
In [7]: obj = NormalClassNewStyle()
In [8]: pp(obj)
instance(NormalClassNewStyle):
dicts: {
},
lists: [],
static_props: 1,
tupl: (1, 2)
```

Long Text
---
```
In [27]: long_text_en = "The separation of the sky and the earth took eighteen thousand years-the yang which was light and pure rose to become the sky, and the yin which was heavy and murky(朦胧的) sank to form the earth. Between them was Pangu, who went through nine changes every day, his wisdom greater than that of the sky and his ability greater than that of the earth. Every day the sky rose ten feet higher, the earth became ten feet thicker, and Pangu grew ten feet taller."
In [28]: pp(long_text_en)
'The separation of the sky and the earth took eighteen thousand years-the yang which was light and pure rose to
become the sky, and the yin which was heavy and murky(朦胧的) sank to form the earth. Between them was Pangu,
who went through nine changes every day, his wisdom greater than that of the sky and his ability greater than that
of the earth. Every day the sky rose ten feet higher, the earth became ten feet thicker, and Pangu grew ten feet
taller.'
In [30]: long_text_cn = "据民间神话传说古时盘古生在黑暗团中,他不能忍受黑暗,用神斧劈向四方,逐渐使天空高远,大地辽阔。他为不使天地会重新合并,继续施展法术。每当盘古的身体长高一尺,天空就随之增高一尺,经过1.8万多年的努力,盘古变成 一位顶天立地的巨人,而天空也升得高不可及,大地也变得厚实无比。盘古生前完成开天辟地的伟大业绩,死后永远留给后人无穷 无尽的宝藏,成为中华民族崇拜的英雄。""
In [31]: pp(long_text_cn)
'据民间神话传说古时盘古生在黑暗团中,他不能忍受黑暗,用神斧劈向四方,逐渐使天空高远,大地辽阔。他为不使天地会重新合
并,继续施展法术。每当盘古的身体长高一尺,天空就随之增高一尺,经过1.8万多年的努力,盘古变成一位顶天立地的巨人,而
天空也升得高不可及,大地也变得厚实无比。盘古生前完成开天辟地的伟大业绩,死后永远留给后人无穷无尽的宝藏,成为中华民
族崇拜的英雄。'
```

Long Text in Dict
---
```
In [33]: d = {'en': long_text_en, 'cn': long_text_cn}
In [34]: pp(d)
{
'en': 'The separation of the sky and the earth took eighteen thousand years-the yang which was light and pure
rose to become the sky, and the yin which was heavy and murky(朦胧的) sank to form the earth. Between
them was Pangu, who went through nine changes every day, his wisdom greater than that of the sky and his
ability greater than that of the earth. Every day the sky rose ten feet higher, the earth became ten feet
thicker, and Pangu grew ten feet taller.',
'cn': '据民间神话传说古时盘古生在黑暗团中,他不能忍受黑暗,用神斧劈向四方,逐渐使天空高远,大地辽阔。他为不使天地
会重新合并,继续施展法术。每当盘古的身体长高一尺,天空就随之增高一尺,经过1.8万多年的努力,盘古变成一位顶
天立地的巨人,而天空也升得高不可及,大地也变得厚实无比。盘古生前完成开天辟地的伟大业绩,死后永远留给后人无
穷无尽的宝藏,成为中华民族崇拜的英雄。',
}
```

Complicated data
---
```
[
1,
1.1,
'literal',
'unicode',
'literal中文',
'unicode中文',
[1, 2, 3, 4, 5, 6],
[
1,
[2],
{
'key': 'val',
},
],
(1, 2),
function(EmptyFunc),
class(EmptyClassOldStyle),
class(EmptyClassNewStyle),
class(NormalClassOldStyle):
static_props: 1
class(NormalClassNewStyle):
dicts: {
},
lists: [],
static_props: 1,
tupl: (1, 2)
instance(NormalClassOldStyle):
static_props: 1
instance(NormalClassNewStyle):
dicts: {
},
lists: [],
static_props: 1,
tupl: (1, 2)
method(mth),
method(mth),
{
'key': []
},
{
'key': instance(NormalClassNewStyle):
dicts: {
},
lists: [],
static_props: 1,
tupl: (1, 2)
},
]
```

Installation
===
```shell
pip install beeprint
```

Settings
===

> more on [settings.py](./beeprint/settings.py)
53 changes: 0 additions & 53 deletions README.txt

This file was deleted.

1 change: 1 addition & 0 deletions README.txt
21 changes: 21 additions & 0 deletions beeprint/block_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding:utf-8 -*-
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import division
import inspect
import sys
import types

from . import constants as C
from .helper import pstr, typeval


def pair_block_key(position, key):
if position & C._AS_CLASS_ELEMENT_:
# class method name or attribute name no need to add u or b prefix
key = pstr(key)
else:
key = typeval(None, key)

return key
15 changes: 8 additions & 7 deletions beeprint/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
# compares to _AS_ELEMENT_, a value is a component of an element
# it has not leading spaces except a span between a key
# it belongs to a key
_AS_VALUE_ = 2
_AS_VALUE_ = 1 << 1
# when display, these elements need comma between each others
# it must has a parent block
# eg: [1, 2], {'key1': 'val1', 'key2': 'val2'}, (1, 2)
_AS_LIST_ELEMENT_ = \
_AS_TUPLE_ELEMENT_ = 4
_AS_TUPLE_ELEMENT_ = 1 << 2

_AS_DICT_ELEMENT_ = 8
_AS_CLASS_ELEMENT_ = 16
_AS_DICT_ELEMENT_ = 1 << 3
_AS_CLASS_ELEMENT_ = 1 << 4

# string type
_ST_LITERAL_ = 1 # string literal depends on script's coding
Expand All @@ -39,6 +39,7 @@
_DL_STATEMENT = 3

# long string
_LS_WRAP_BY_NONE = 0
_LS_WRAP_BY_TERMINAL = 1
_LS_WRAP_BY_80_COLUMN = 2
_TEXT_WRAP_BY_NONE = 0
_TEXT_WRAP_BY_TERMINAL = 1
# accompany with S.text_wrap_width argument
_TEXT_WRAP_BY_WIDTH = 2
4 changes: 3 additions & 1 deletion beeprint/debug_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ def add_leading(depth, text):
def debug(level, depth, text):
if S.debug_level >= level:
frame_list = inspect.stack()
frame_obj = frame_list[1][0]
class_name = frame_obj.f_locals['self'].__class__.__name__
caller_name = frame_list[1][3]
depth = len(frame_list) - 4
if level == C._DL_FUNC_:
depth -= 1
text = caller_name + ': ' + text
text = class_name + '.' + caller_name + ': ' + text
text = add_leading(depth, text)
print(text)
Loading

0 comments on commit af465a5

Please sign in to comment.