Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F11709312
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/_modules/network_utils.py b/_modules/network_utils.py
index 1a16853..1ffb73e 100644
--- a/_modules/network_utils.py
+++ b/_modules/network_utils.py
@@ -1,46 +1,46 @@
# -*- coding: utf-8 -*-
# -------------------------------------------------------------
# Salt — Network execution module
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# License: BSD-2-Clause
# -------------------------------------------------------------
import re
# -------------------------------------------------------------
# CIDR netmask and prefixes
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def is_valid_netmask(netmask):
# "255.255.255.240" → "11111111111111111111111111110000"
- bits = ''.join([format(int(octet), 'b') for octet in netmask.split(".")])
+ bits = "".join([format(int(octet), "b") for octet in netmask.split(".")])
# A netmask is valid if the suite of bits:
# - starts by contiguous 1, e.g. here 1111111111111111111111111111
# - ends by contiguous 0, e.g. here 0000
#
# Also, as 0.0.0.0 is invalid, netmask must starts by 1.
return re.compile("^1+0*$").match(bits) is not None
def netmask_to_cidr_prefix(netmask):
"""
Convert a netmask like 255.255.255.240 into a CIDR prefix like 24.
This can be useful for RHEL network scripts requiring PREFIX information.
"""
if not is_valid_netmask(netmask):
raise ValueError("Netmask is invalid.")
# The CIDR prefix is the count of 1 bits in each octet.
# e.g. 255.255.255.240 can be split in octets [255, 255, 255, 240],
# then becomes ['0b11111111', '0b11111111', '0b11111111', '0b11110000'].
#
# There is 24 "1" in this expression, that's 24 is our CIDR prefix.
return sum([bin(int(octet)).count("1") for octet in netmask.split(".")])
diff --git a/_tests/modules/test_network.py b/_tests/modules/test_network.py
index b39ebd6..b05ed6e 100755
--- a/_tests/modules/test_network.py
+++ b/_tests/modules/test_network.py
@@ -1,54 +1,53 @@
#!/usr/bin/env python3
from importlib.machinery import SourceFileLoader
from unittest_data_provider import data_provider
import unittest
salt_test_case = SourceFileLoader("salt_test_case", "salt_test_case.py").load_module()
network = SourceFileLoader("network", "../_modules/network_utils.py").load_module()
class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase):
cidr_prefixes = lambda: (
("255.255.255.255", 32),
("255.255.255.254", 31),
("255.255.255.252", 30),
("255.255.255.240", 28),
("255.255.255.224", 27),
("255.255.255.0", 24),
("255.252.0.0", 14),
)
valid_netmasks = lambda: (
("255.255.255.255",),
("255.255.255.254",),
("255.255.255.252",),
("255.255.255.240",),
)
invalid_netmasks = lambda: (
# In binary, it's not a suite of 1 then a suite of 0
("255.255.255.209",),
-
# By definition, netmask MUST be strictly greater than 0
("0.0.0.0",),
)
@data_provider(cidr_prefixes)
def test_netmask_to_cidr_prefix(self, netmask, expected_prefix):
actual_prefix = network.netmask_to_cidr_prefix(netmask)
self.assertTrue(actual_prefix == expected_prefix)
@data_provider(valid_netmasks)
def test_is_valid_netmask(self, netmask):
self.assertTrue(network.is_valid_netmask(netmask))
@data_provider(invalid_netmasks)
def test_is_valid_netmask_when_it_is_not(self, netmask):
self.assertFalse(network.is_valid_netmask(netmask))
if __name__ == "__main__":
unittest.main()
diff --git a/roles/devserver/userland-home/files/dereckson/bin/http2pls b/roles/devserver/userland-home/files/dereckson/bin/http2pls
index c989fdf..b325b9d 100755
--- a/roles/devserver/userland-home/files/dereckson/bin/http2pls
+++ b/roles/devserver/userland-home/files/dereckson/bin/http2pls
@@ -1,69 +1,73 @@
#!/usr/bin/env python3
from bs4 import BeautifulSoup
from urllib import request, parse
from os import path
from sys import argv, exit, stderr
def get_url(base_url, tag):
- relative_url = tag.get('href')
+ relative_url = tag.get("href")
return parse.urljoin(base_url, relative_url)
def is_media_extension(extension):
valid_extensions = [
# Videos
- ".avi", ".flv", ".mp4", ".wmv", ".mkv",
+ ".avi",
+ ".flv",
+ ".mp4",
+ ".wmv",
+ ".mkv",
]
return extension in valid_extensions
def is_media_link(url):
extension = path.splitext(url)[1]
return is_media_extension(extension)
def print_pls(url):
links = get_media_links(url)
if not links:
return
print("[playlist]")
i = 0
for link in links:
i += 1
- print('File{:d}={:s}'.format(i, link))
+ print("File{:d}={:s}".format(i, link))
print("")
print("NumberOfEntries={:d}".format(i))
print("Version=2")
def get_media_links(url):
return [link for link in get_links(url) if is_media_link(link)]
def get_links(url):
try:
with request.urlopen(url) as response:
body = response.read()
- soup = BeautifulSoup(body, 'html.parser')
- return [get_url(url, link) for link in soup.find_all('a')]
+ soup = BeautifulSoup(body, "html.parser")
+ return [get_url(url, link) for link in soup.find_all("a")]
except ValueError:
print("No valid URL: " + url, file=stderr)
return []
def run():
if len(argv) < 2:
exit(1)
print_pls(argv[1])
if __name__ == "__main__":
run()
diff --git a/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution b/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution
index 369309e..01cdf13 100755
--- a/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution
+++ b/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution
@@ -1,58 +1,61 @@
#!/usr/bin/env python3
import math
import sys
def binomial_coefficient(n, k):
f = math.factorial
return f(n) / f(k) / f(n - k)
def compute(population_size, success_states_count, draws_count, observed_successes):
N = population_size
K = success_states_count
n = draws_count
k = observed_successes
bc = binomial_coefficient
return bc(K, k) * bc(N - K, n - k) / bc(N, n)
def usage():
- print(f"Usage: {sys.argv[0]} <population size> <number of success states> <number of draws> <number of observed successes>", file=sys.stderr)
+ print(
+ f"Usage: {sys.argv[0]} <population size> <number of success states> <number of draws> <number of observed successes>",
+ file=sys.stderr,
+ )
def help():
usage()
print()
print("Example:")
print()
print("In a Magic the Gathering deck of 60 cards, you've put 23 lands.")
print("To compute the probability to get 3 lands in your opening hand (7 cards):")
print(f" {sys.argv[0]} 60 23 7 3")
def has_help_arg():
help_args = ["-h", "--help", "/?", "/help"]
return any([help_arg in sys.argv[1:] for help_arg in help_args])
if __name__ == "__main__":
if has_help_arg():
help()
sys.exit(0)
if len(sys.argv) != 5:
usage()
sys.exit(1)
try:
numbers = [float(arg) for arg in sys.argv[1:]]
probability = compute(*numbers)
print(probability)
except ValueError as e:
print(e)
usage()
sys.exit(2)
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Sep 15, 09:26 (8 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2983970
Default Alt Text
(7 KB)
Attached To
Mode
rOPS Nasqueron Operations
Attached
Detach File
Event Timeline
Log In to Comment