How do I get this simple pytest fixture to work correctly? [closed] - pytest

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I'm attempting to get a simple pytest fixture to work.
#pytest.fixture
def two_cities_wind():
return {'bristol': [7, 7, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 9, 9, 8, 8, 8, 8, 8, 8, 9, 8, 8, 8],
'bath': [14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 15, 15, 15]}
My test is
def test_city_with_high_wind_of_week():
windy_city = weather.city_with_high_wind_of_week(two_cities_wind)
assert windy_city == "bath"
I am expecting the fixture to provide the dict that should be iterable returned, but the test is throwing an exception.
windy_city = ""
wind_speed = 0
> for city in cities_weather:
E TypeError: 'function' object is not iterable
src/weather.py:52: TypeError
========================================================== short test summary info ===========================================================
FAILED tests/test_zweather.py::test_city_with_high_wind_of_week - TypeError: 'function' object is not iterable
======================================================== 1 failed, 10 passed in 0.49s ========================================================
The function under test is
def city_with_high_wind_of_week(cities_weather) -> str:
windy_city = ""
wind_speed = 0
for city in cities_weather:
highest_speed = cities_weather[city].sort()[-1]
if highest_speed > wind_speed:
windy_city = city
wind_speed = highest_speed
return windy_city
The way I am attempting this looks comparable with these examples
https://docs.pytest.org/en/latest/explanation/fixtures.html
https://levelup.gitconnected.com/how-to-make-your-pytest-tests-as-dry-as-bone-e9c1c35ddcb4

Answer supplied by #MrBeanBremen
I needed to pass the fixture to the test function.
The test should be called as follows:
def test_city_with_high_wind_of_week(two_cities_wind):
windy_city = weather.city_with_high_wind_of_week(two_cities_wind)
assert windy_city == "bath"

Related

Sort numbers inside string

I have a string that consists of few numbers. First number is the number of the row, remaining are numbers in this row.(Array but string, kind of). The problem is that remaining numbers are unsorted, and I want to find the clearest way of sorting them without creating new List and sorting everything there.
String unsorted = '9, 12, 14, 11, 2, 10';
print(unsorted.sort()); // '9, 2, 10, 11, 12, 14'
You cant really avoid converting to a list of numbers if you want to sort it.
void main() {
print(sortNumString('9, 12, 14, 11, 2, 10')); // 2, 9, 10, 11, 12, 14
}
String sortNumString(String numString, [String separator = ', ']) =>
(numString.split(separator).map(int.parse).toList()..sort())
.join(separator);
The .. means to return the previous thing, the list, since sort returns void.
I'm not exactly experienced when it comes to dart programming language but this is what i came up with.
void main() {
var unsorted = "9, 12, 14, 11, 2, 10";
var nums_int = unsorted.split(", ").map(int.parse).toList();
nums_int.sort();
for (var n in nums_int) {
stdout.write(n.toString() + ", ");
}
}
That should give the expected output: "2, 9, 10, 11, 12, 14"
Hope this helps.

Observable.switchMap does not interrupt Observable

I don't understand why switchMap doesn't work as expected.
At first case switchMap doesn't interrupt Observable but at second case it works as expected. Please, explain that behaviour.
val test = TestObserver<Int>()
Observable.fromArray(10, 20)
.switchMap({
// 1. switchMap does not interrupt generator
Observable.generate(
Callable{
generateSequence(it, { n -> if (n < it + 9) n + 1 else null}).iterator()
},
BiConsumer<Iterator<Int>, Emitter<Int>>(){ data, emitter ->
if (data.hasNext()) {
Thread.sleep(100)
emitter.onNext(data.next())
}else {
emitter.onComplete()
}
}
)
// 2. switchMap disposes observable
//Observable.just(it).delay(1, TimeUnit.SECONDS)
},2)
.subscribeWith(test)
test.await()
// 1. >> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
// But I expect [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
// 2. >> [20]
print(test.values())
What I want to do.
In reality my generator do hard work based on value from switchMap and I want interrupt this work if new value is appeared. At the beginning generator start transaction and at the end generator commits or cancels transaction.

Swap first and last columns in matrix

For example, we have a matrix.
1, 2, 3, 4, 5
6, 7, 8, 9, 10
11, 12, 13, 14, 15
16, 17, 18, 19, 20
21, 22, 23, 24, 25
Perhaps the simplest way to resolve the problem sounded in the title of the topic in Perl6 looks like
my #matrix = [1..5], [6..10], [11..15], [16..20], [21..25];
#matrix.map:{.[0,*-1] = .[*-1,0]};
Result
5, 2, 3, 4, 1
10, 7, 8, 9, 6
15, 12, 13, 14, 11
20, 17, 18, 19, 16
25, 22, 23, 24, 21
How to do the same is also beautiful in PowerShell?
Your code snippet translated to PowerShell would look like this:
$matrix = (1..5), (6..10), (11..15), (16..20), (21..25)
$matrix | ForEach-Object { $_[0], $_[-1] = $_[-1], $_[0] }

Powershell "Padding is invalid and cannot be removed" error

I'm trying to decrypt a string in Powershell and am getting this error. What could be wrong?
This exception can represent a lot of different things, and not all of them are related to the padding, so I have tried to catalogue all the different scenarios where this can occur.
If you know of another situation where this padding exception is thrown, please add it.
To start, this is an example of encryption/decryption that works as expected.
$testData = "Hi there! This is a test of a string during encryption"
$enc = [system.Text.Encoding]::UTF8
$data = $enc.getBytes($testData)
# Encrypt some data
$encryptAlgorithm = [System.Security.Cryptography.SymmetricAlgorithm] (New-Object System.Security.Cryptography.AesCryptoServiceProvider)
$encryptAlgorithm.Mode = [System.Security.Cryptography.CipherMode]::CBC
$encryptAlgorithm.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$encryptAlgorithm.KeySize = 128
$encryptAlgorithm.BlockSize = 128
$encryptAlgorithm.Key = #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
$encryptAlgorithm.IV = #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
$encryptor = [System.Security.Cryptography.ICryptoTransform]$encryptAlgorithm.CreateEncryptor()
$encryptorMemoryStream = new-Object IO.MemoryStream
$encryptorCryptoStream = new-Object Security.Cryptography.CryptoStream $encryptorMemoryStream,$encryptor,"Write"
$encryptorCryptoStream.Write($data, 0, $data.Length)
$encryptorCryptoStream.FlushFinalBlock();
$encryptedData = $encryptorMemoryStream.ToArray()
Write-Host $enc.GetString($encryptedData)
Write-Host $encryptedData.Length
# Decrypt some data
$descryptAlgorithm = [System.Security.Cryptography.SymmetricAlgorithm] (New-Object System.Security.Cryptography.AesCryptoServiceProvider)
$descryptAlgorithm.Mode = [System.Security.Cryptography.CipherMode]::CBC
$descryptAlgorithm.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$descryptAlgorithm.KeySize = 128
$descryptAlgorithm.BlockSize = 128
$descryptAlgorithm.Key = #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
$descryptAlgorithm.IV = #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
$decryptor = [System.Security.Cryptography.ICryptoTransform]$descryptAlgorithm.CreateDecryptor()
$dataToDecrypt = $encryptedData
$decryptorMemoryStream = new-Object IO.MemoryStream #(,$dataToDecrypt)
$decryptorCryptoStream = new-Object Security.Cryptography.CryptoStream $decryptorMemoryStream,$decryptor,"Read"
$streamReader = new-Object IO.StreamReader $decryptorCryptoStream
try
{
Write-Output $streamReader.ReadToEnd()
}
catch
{
$e = $_.Exception
$msg = $e.Message
while ($e.InnerException) {
$e = $e.InnerException
$msg += "`n" + $e.Message
}
$msg
}
Let's look at some examples that trigger the padding exception.
Failing to flush the final block
This blog post has a good write up of this scenario.
#$encryptorCryptoStream.FlushFinalBlock();
Invalid keys
I've changed the byte array used for the decryption key slightly here to simulate keys that don't match.
$descryptAlgorithm.Key = #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17)
RijndaelManaged with an empty input
$descryptAlgorithm = [System.Security.Cryptography.SymmetricAlgorithm] (New-Object System.Security.Cryptography.RijndaelManaged)
// ...
$dataToDecrypt = #()
Invalid padding
I've manually added some invalid padding to the end of the data to decrypt.
$dataToDecrypt = $encryptedData + #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
Different padding schemes
Interestingly only some padding scheme combinations result in the padding error. A lot will decrypt without error, even if the resulting sting is incorrect.
$descryptAlgorithm.Padding = [System.Security.Cryptography.PaddingMode]::ANSIX923
Different block sizes
$descryptAlgorithm.BlockSize = 64

ZXing library Reed Solomon example

I want to try the ReedSolomonDecoder from the ZXing library on the example given on page 10 of this paper
Basically, it encodes the message
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
using the generator polynomial
x^4 + 15x^3 + 3x^2 + x + 12
which results in
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12
I want to decode this in the following manner:
int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12};
GenericGF field = new GenericGF(?, 16, 1); // what integer should I use for primitive here?
ReedSolomonDecoder decoder = new ReedSolomonDecoder(field);
decoder.decode(data, 4);
I don't know how to create a GenericGF object from the given generator polynomial. I know that it expects a binary integer representation of the polynomial, but to do that, I would need the polynomial to be in an irreducible form, i.e. all the coefficients to be either 0 or 1. How can I achieve that from this given generator polynomial?
I'm pretty new to this as well but I think you would want to use
public static GenericGF AZTEC_PARAM = new GenericGF(0x13, 16, 1);