Skip to main content

MAC address manipulation library

Project description

l2address

Micro library I'm planing to develop by using TDD approach, just to try it.

Example usage

import l2address

mac = l2address.mac_address(0xff00ff)
print(mac)
# 00:00:00:ff:00:ff
mac += 0xff00
print(mac)
# 00:00:00:ff:ff:ff
mac -= l2address.mac_address('00:00:00:ff:ff:f0')
print(mac)
# 00:00:00:00:00:0f
print(mac == l2address.mac_address(0xf))
# True
print(l2address.mac_address('ffffffffffff') + 1)
# 00:00:00:00:00:00
print(l2address.mac_address(0xff, l2address.PeriodFormatter(4)))
# 0000.0000.00ff

Features

At the current moment supports:

  • Create MAC address from int
  • Create MAC address from str
  • Sum and Substitution of MAC addresses (and with ints)
  • Convert MAC to string
  • Different formatting types (00:00:00:00:00:00, 0000.0000.0000, 00-00-00-00-00-00, and so on)
  • Validation of format when parsing from string. There are predefined formats, but can be extended, see: Extending Formats

Extending Formats

import l2address
from l2address.utils import per_join

# Just inherit l2address.Formatter and use new formatter


class MyMacStarFormatter(l2address.Formatter):
    def format(self, value, max_value):
        return per_join(self._to_clean_str(value, max_value), '*', 2)

    def _get_validator_regexp(self, _str, max_value):
        return r'^([\da-fA-F][\da-fA-F]\*?){6}'


try:
    l2address.mac_address('AB*ab*bc*de*ff*ff')
except ValueError:
    print('Failed as expected')
    # Failed as expected

mac = l2address.mac_address('AB*ab*bc*de*ff*ff', MyMacStarFormatter())
print(mac)
# ab*ab*bc*de*ff*ff
print(mac.to_str(l2address.ColonFormatter()))
# ab:ab:bc:de:ff:ff
print(l2address.mac_address(0xff).to_str(MyMacStarFormatter()))
# 00*00*00*00*00*ff

Plans

  • Multiplication by int
  • Different lengths of MAC address (there are 64 bit MAC addesses I believe)

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

l2address-1.0.1.tar.gz (3.4 kB view details)

Uploaded Source

File details

Details for the file l2address-1.0.1.tar.gz.

File metadata

  • Download URL: l2address-1.0.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.3

File hashes

Hashes for l2address-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5351cbef6aec849331b05d34b1ca1eb01092706f1aaf561247a3f3c918f78eb4
MD5 dcc55a0150c070d40d66c6a4e93e10dd
BLAKE2b-256 392a666283759443e4844c425035974d14b042d012afb07699ecf5b2c41cfce9

See more details on using hashes here.

Supported by

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