Skip to content

Instantly share code, notes, and snippets.

@hartwork
Last active December 30, 2021 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartwork/ccad94d00c4193e05ab57de70021e6ee to your computer and use it in GitHub Desktop.
Save hartwork/ccad94d00c4193e05ab57de70021e6ee to your computer and use it in GitHub Desktop.
Creates an XML file with a given number of prefixed XML attributes on a single XML tag.
# Copyright (c) 2021 Sebastian Pipping <sebastian@pipping.org>
# Licensed under the Apache license version 2.0
#
# Creates an XML file with a given number of prefixed XML attributes
# on a single XML tag.
# Needs Python >=3.6 and PyPI package "base58"
#
# 2021-12-31 00:47 UTC+1
import argparse
import math
import base58
_BATCH_SIZE = 2**13
def _to_name(i: int) -> str:
bites = []
while i >= 256:
bite = i % 256
i //= 256
bites.append(bite)
bites.append(i)
return base58.b58encode(bytes(bites))
def _validate_attributes_count(candidate: str) -> int:
res = int(candidate) # may throw
if res <= 0 or res % _BATCH_SIZE != 0:
raise ValueError(f'not a multiple of batch size {_BATCH_SIZE}')
return res
_validate_attributes_count.__name__ = 'attributes count' # for argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('attributes_count', metavar='COUNT',
type=_validate_attributes_count,
help=('number of XML attributes to write'
' excluding the namespace declaration'
', must be a multiple of 2^13'
'; e.g. try $((2**15)) in Bash'))
parser.add_argument('output_filename', metavar='FILENAME',
help='filename of the file to write to')
config = parser.parse_args()
runs_count = config.attributes_count // _BATCH_SIZE
with open(config.output_filename, 'bw') as f:
f.write(b'<r xmlns:a="http://whatever.invalid/"')
for run in range(runs_count):
f.write(b'\n')
print(f'Run {run+1} of {runs_count}'
f': {run * _BATCH_SIZE + 1} to {(run + 1) * _BATCH_SIZE}')
f.write(b'\n'.join(
b'a:a' + _to_name(run * _BATCH_SIZE + i) + b'=""'
for i in range(_BATCH_SIZE)))
f.flush()
f.write(b'\n/>')
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment