Skip to main content

A LinkedTuple is a read-only structure of linked (nested) tuples much like lists in functional languages

Project description

Version License:MIT

A LinkedTuple is a read-only structure of linked (nested) tuples much like lists in functional languages.

Instances have a head element that can be anything and a tail element that can be either another LinkedTuple or None.

Being a nested container of elements, a LinkedTuple is less efficient than a list or a plain tuple, but its main advantage is that it allows branching. That is, a tail can be shared by more than one LinkedTuple

Install using pip

pip install linkedtuple

Class

  • LinkedTuple(head[,tail])

    Returns a new LinkedTuple instance with the provided head and possibly a tail

Properties

  • head

    read only head

  • tail

    read only tail

Methods

  • make(iterable) [classmethod]

    Returns a new LinkedTuple chain with the elements taken from the iterable

  • plus(element)

    Returns a new LinkedTuple with element as head and the current ListTuple as tail

  • reversed()

    Returns a new LinkedTuple with the items in reverse order

Examples

We can create a LinkedTuple with a single element

>>> from linkedtuple import LinkedTuple

>>> LinkedTuple('x')
LinkedTuple(head='x', tail=None)

Or from an iterable.

>>> ltup = LinkedTuple.make([1,2,3,4])

>>> ltup
LinkedTuple(head=4, tail=LinkedTuple(head=3, tail=LinkedTuple(head=2, tail=LinkedTuple(head=1, tail=None))))

Note the elements are present in reverse order.

So far, we've shown LinkedTuples as represented when calling repr() on them. When casting to string we get a more compact representation.

>>> repr(ltup)
'LinkedTuple(head=4, tail=LinkedTuple(head=3, tail=LinkedTuple(head=2, tail=LinkedTuple(head=1, tail=None))))'

>>> str(ltup)
'(4,(3,(2,(1,None))))'

LinkedTuples have a few available read-only properties, the most important ones are head and tail.

>>> ltup.head
4

>>> ltup.tail
LinkedTuple(head=3, tail=LinkedTuple(head=2, tail=LinkedTuple(head=1, tail=None)))

Passing an iterable as a single element to LinkedTuple won't expand the iterable and will result in a LinkedTuple with the iterable as head and no tail.

>>> LinkedTuple([1,2,3,4])
LinkedTuple(head=[1, 2, 3, 4], tail=None)

We can create a new LinkedTuple from an existing one plus a new element.

>>> str(ltup.plus('X'))
'(X,(4,(3,(2,(1,None)))))'

The previous one doesn't (and can't) change. LinkedTuples are immutable.

>>> str(ltup)
'(4,(3,(2,(1,None))))'

>>> ltup.head = 8
Traceback (most recent call last):
	File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

We can ask for the number of elements in a LinkedTuple, however this is slow because all the chain has to be traversed to count them.

>>> print(len(ltup))
4

LinkedTuples are iterables themselves, so we can do things like the following:

>>> list(ltup)
[4, 3, 2, 1]

>>> for item in ltup:
...     print(item)
... 
4
3
2
1

We can generate a new LinkedTuple from another LinkedTuple with the elements reversed

>>> str(ltup)
'(4,(3,(2,(1,None))))'

>>> str(ltup.reversed())
'(1,(2,(3,(4,None))))'

Contact

  • Me on GitHub Gato-X
  • Project page on GitHub
  • If you find any issues, please let me know here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linkedtuple-1.0.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

linkedtuple-1.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file linkedtuple-1.0.0.tar.gz.

File metadata

  • Download URL: linkedtuple-1.0.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.21.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.3

File hashes

Hashes for linkedtuple-1.0.0.tar.gz
Algorithm Hash digest
SHA256 eb28702c9f0d92641d922c88f59c415ca061da75f1f1912e1dd92d15ade71a7b
MD5 0b1a826341be96acc805aaef6b00ae5e
BLAKE2b-256 d6bbcc5b0a0ae8d6963f0fd27360821927bccbac7357eab3efc44bd3106c626d

See more details on using hashes here.

File details

Details for the file linkedtuple-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: linkedtuple-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.21.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.3

File hashes

Hashes for linkedtuple-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58995fce36a7d0a21cc6d9531dad98e04f59ed9d1f74689d38fb7a183f840cf8
MD5 e381336c61f24a69e9a10088473cc6a8
BLAKE2b-256 fe883e7ce69023168e01a1fc44ef9100af1a56a22fcfe4a2c5d60f3b0d85f49a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page