pydu/docs/index.rst

226 lines
5.2 KiB
ReStructuredText
Raw Normal View History

2017-10-06 15:59:14 +00:00
.. pydu documentation master file, created by
2017-10-06 15:45:29 +00:00
sphinx-quickstart on Fri Oct 6 23:05:59 2017.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
2017-10-06 15:59:14 +00:00
pydu documentation
2017-10-06 15:45:29 +00:00
===================
About
-----
2017-10-10 17:07:18 +00:00
**pydu** (python data structures and utils) is a library for Python 2 and 3.
It is collected from open source projects and created by contributors.
2017-10-06 15:45:29 +00:00
It is with Python versions from **2.7 to 3.6**.
2017-10-06 15:59:14 +00:00
The pydu documentation you're reading is distributed as a single HTML page.
2017-10-06 15:45:29 +00:00
2017-10-28 15:03:14 +00:00
Content
2017-10-06 15:45:29 +00:00
===============
2017-10-29 13:21:18 +00:00
Archive
-------
.. function:: pydu.archive.extract(path, to_path='', ext='')
Unpack the tar or zip file at the specified path or file to the directory
specified by ``to_path``. It supports many extensions, like ``.tar``,
``.tar.bz2``, ``.tar.gz``, ``.tgz``, ``.tz2``, ``.zip``. If the file name of
given ``path`` doesn't contain file extension, the ``ext`` parameter can be
specified one of supported extensions to indicate file type.
>>> from pydu.archive import extract
>>> extract('foobar.tgz', '/tmp')
>>> extract('foobar', '/tmp', ext='.tgz')
>>> extract('foobar', '/tmp')
Traceback (most recent call last):
... AttributeError: pydu.archive.UnrecognizedArchiveFormat: Path not a recognized archive format: foobar
2017-10-06 15:45:29 +00:00
Dict
----
2017-10-27 12:15:19 +00:00
.. class:: pydu.dict.AttrDict(seq=None, **kwargs)
2017-10-06 15:45:29 +00:00
2017-10-26 14:56:58 +00:00
A AttrDict object is like a dictionary except ``obj.foo`` can be used
in addition to ``obj['foo']``.
2017-10-06 15:45:29 +00:00
2017-10-27 12:15:19 +00:00
>>> from pydu.dict import AttrDict
2017-10-06 15:59:14 +00:00
>>> o = AttrDict(a=1)
o.a
1
>>> o['a']
1
>>> o.a = 2
>>> o['a']
2
>>> del o.a
>>> o.a
Traceback (most recent call last):
2017-10-27 12:15:19 +00:00
... AttributeError: 'a'
2017-10-06 15:59:14 +00:00
2017-10-27 12:15:19 +00:00
.. class:: pydu.dict.CaseInsensitiveDict(data=None, **kwargs)
2017-10-06 15:45:29 +00:00
A case-insensitive ``dict``-like object.
Implements all methods and operations of ``collections.MutableMapping``
as well as dict's ``copy``. Also provides ``lower_items``.
All keys are expected to be strings. The structure remembers the
case of the last key to be set, and ``iter(instance)``, ``keys()``,
``items()``, ``iterkeys()``, and ``iteritems()`` will contain
2017-10-12 15:42:02 +00:00
case-sensitive keys.
2017-10-06 15:59:14 +00:00
2017-10-27 12:15:19 +00:00
>>> from pydu.dict import CaseInsensitiveDict
2017-10-06 15:59:14 +00:00
>>> cid = CaseInsensitiveDict()
>>> cid['Accept'] = 'application/json'
>>> cid['aCCEPT'] == 'application/json'
True
>>> list(cid) == ['Accept']
True
2017-10-06 15:45:29 +00:00
2017-10-27 12:15:19 +00:00
.. class:: pydu.dict.LookupDict(name=None)
2017-10-06 15:45:29 +00:00
Dictionary lookup object.
2017-10-27 12:15:19 +00:00
>>> from pydu.dict import LookupDict
2017-10-06 15:59:14 +00:00
>>> d = LookupDict()
>>> d['key']
None
>>> d['key'] = 1
>>> d['key']
1
2017-10-27 12:15:19 +00:00
.. function:: pydu.dict.attrify(obj)
2017-10-12 15:42:02 +00:00
2017-10-27 12:15:19 +00:00
Attrify obj into ``AttriDict`` or ``list of AttriDict`` if the obj is list.
2017-10-12 15:42:02 +00:00
2017-10-27 12:15:19 +00:00
>>> from pydu.dict import attrify
2017-10-12 15:42:02 +00:00
>>> attrd = attrify({
'a': [1, 2, {'b': 'b'}],
'c': 'c',
})
>>> attrd
<AttrDict {'a': [1, 2, <AttrDict {'b': 'b'}>], 'c': 'c'}>
>>> attrd.a
1
>>> attrd.a[2].b
b
>>> attrd.c
c
2017-10-28 15:03:14 +00:00
Set
----
.. class:: pydu.set.OrderedSet(iterable=None)
A set which keeps the ordering of the inserted items.
>>> from pydu.set import OrderedSet
>>> s = OrderedSet([1, 3, 1, 2])
>>> list(s)
[1, 3, 2]
>>> s.discard(3)
>>> list(s)
[1, 2]
2017-10-09 14:56:02 +00:00
String
------
2017-10-06 15:59:14 +00:00
2017-10-27 12:15:19 +00:00
.. function:: pydu.string.safeunicode(obj, encoding='utf-8')
2017-10-14 15:46:43 +00:00
Converts any given object to unicode string.
2017-10-27 12:15:19 +00:00
>>> from pydu.string import safeunicode
2017-10-14 15:46:43 +00:00
>>> safeunicode('hello')
u'hello'
>>> safeunicode(2)
u'2'
>>> safeunicode('\xe4\xb8\xad\xe6\x96\x87')
u'中文'
2017-10-27 12:15:19 +00:00
.. function:: pydu.string.lstrips(text, remove)
2017-10-06 15:59:14 +00:00
2017-10-26 14:56:58 +00:00
Removes the string ``remove`` from the left of ``text``.
2017-10-06 15:59:14 +00:00
2017-10-27 12:15:19 +00:00
>>> from pydu.string import lstrips
2017-10-09 14:56:02 +00:00
>>> lstrips('foobar', 'foo')
'bar'
>>> lstrips('FOOBARBAZ', ['FOO', 'BAR'])
'BAZ'
>>> lstrips('FOOBARBAZ', ['BAR', 'FOO'])
'BARBAZ'
2017-10-06 15:59:14 +00:00
2017-10-27 12:15:19 +00:00
.. function:: pydu.string.rstrips(text, remove)
2017-10-06 15:45:29 +00:00
2017-10-26 14:56:58 +00:00
Removes the string ``remove`` from the right of ``text``.
2017-10-06 15:45:29 +00:00
2017-10-27 12:15:19 +00:00
>>> from pydu.string import rstrips
2017-10-09 14:56:02 +00:00
>>> rstrips('foobar', 'bar')
'foo'
2017-10-27 12:15:19 +00:00
.. function:: pydu.string.strips(text, remove)
2017-10-09 14:56:02 +00:00
2017-10-26 14:56:58 +00:00
Removes the string ``remove`` from the both sides of ``text``.
2017-10-09 14:56:02 +00:00
2017-10-27 12:15:19 +00:00
>>> from pydu.string import strips
2017-10-09 14:56:02 +00:00
>>> strips('foobarfoo', 'foo')
'bar'
2017-10-25 14:33:27 +00:00
Miscellanea
-----------
2017-10-27 12:15:19 +00:00
.. function:: pydu.unix_timeout(seconds)
2017-10-26 14:54:11 +00:00
2017-10-26 14:56:58 +00:00
This func decorates any func which may be hang for a while. The param ``seconds``
should be integer. ``unix_timeout`` can only be used on ``unix-like`` system.
2017-10-26 14:54:11 +00:00
In `test.py`, you may write like below:
.. code-block:: python
import time
2017-10-29 13:23:08 +00:00
from pydu.misc import unix_timeout
2017-10-26 14:54:11 +00:00
@unix_timeout(1)
def f():
time.sleep(1.01)
f()
2017-10-26 14:56:58 +00:00
Ant run `test.py`, will see ``TimeoutError``.
2017-10-26 14:54:11 +00:00
2017-10-27 12:15:19 +00:00
.. function:: pydu.trace(obj)
2017-10-25 14:33:27 +00:00
2017-10-26 14:56:58 +00:00
Tracing every statement and line number for running program, like ``bash -x``.
2017-10-25 14:33:27 +00:00
In `test.py`, you may write like below:
.. code-block:: python
2017-10-27 12:15:19 +00:00
from pydu import trace
2017-10-25 14:33:27 +00:00
@trace
def f():
print(1)
a = 1 + 5
b = [a]
print(2)
f()
Ant run `test.py`, will see below output from console:
.. code-block:: console
test.py(4): print(1)
1
test.py(5): a = 1 + 5
test.py(6): b = [a]
test.py(7): print(2)
2