New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable passing a prefix for IP addresses #75
Conversation
We may need a prefix for ipv6 too. With that said I'm in favor of changing the argument name to A |
@elyezer I can rename it, but I am not entirely familiar with ipv6, so someone else will probably have to do it 馃槂 |
@mfalesni the ipv6 generation is pretty straight forward, and expecting a list you can decrease the number of random generation parts then join all together, the same approach for ipv4 you've done. |
Ok, will take a look on ipv6 too. |
What I thought would be something like: max_prefix_len = 7 if not ipv6 else 3 - int(ip3)
prefix = [str(field) for field in prefix_ipv4 or []]
prefix_len = len(prefix)
if prefix_len > max_prefix_len:
raise ValueError('Prefix {} would lead to no randomness at all'.format(repr(prefix)))
if ipv6:
ipaddr = u':'.join('{0:x}'.format(
random.randint(0, 2**16 - 1)
) for i in range(8 - prefix_len))
if prefix is not None:
ipaddr = '{}:{}'.format(':'.join(prefix), ipaddr)
else:
rng = 3 if ip3 else 4
ipaddr = u".".join([str(random.randrange(0, 255, 1)) for _ in range(rng - prefix_len)])
if prefix is not None:
ipaddr = '{}.{}'.format('.'.join(prefix), ipaddr)
if ip3:
ipaddr = ipaddr + u".0" We can improve the prefix logic in order to check after generating the random part. |
@elyezer Just pushed something. |
"""Generates a random IP address. | ||
|
||
You can also specify an IP address prefix if you are interested eg. in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove the eg.
here and add a ,
right before the etc
as such: generation, etc.
.
APT |
:returns: An IP address. | ||
:rtype: str | ||
:raises: ``ValueError`` if ``prefix`` is too long either to fit into | ||
the IP address or disabling randomness at all. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My previous comment may have gotten squashed, but: what does disabling randomness at all
means?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
:raises: ``ValueError`` if ``prefix`` defines a full IP addess or has no elements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@omaciel If you specify a prefix with length of 4, then there is no randomness involved. Maybe the phrase should be written differently, I'll gladly accept an advice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ichimonji10 the :raises:
cannot be so simply written because these limits also apply for ipv6 which has 8 fields therefore max. 7 fields of prefix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mfalesni I've updated the suggested docstring. Have a second look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ichimonji10 Why no elements? If the prefix is empty, then nothing special happens ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mfalesni The code will not be orthogonal if you allow an empty prefix to be specified. A user can specify an emtpy prefix either by passing in None
or by passing in an empty iterable such as []
or ()
. Non-orthogonal code is harder to understand and more complex.
Now, if you do want to allow empty prefixes, that's fine. It makes a lot of sense, actually. But in that case, disallow None
and instead write this:
def gen_ipaddr(ip3=False, ipv6=False, prefix=()):
If you do that, then there is only one way to specify an empty prefix: by passing in an empty iterable.
@Ichimonji10 @elyezer @omaciel Updated according to the comments. |
ACK |
1 similar comment
ACK |
Enable passing a prefix for IP addresses
Thank you @mfalesni
|
I hope tests say all, intended to replace https://github.com/RedHatQE/cfme_tests/blob/master/utils/randomness.py#L5
btw. what's wrong on map(str, prefix_ipv4 or []) ? Shorter than the list comprehension 馃槃