I recently became the maintainter of PyPDF2 – a library which is pretty old and still has some code that deals with Python versions before 2.4. While I want to drop support for 3.5 and older soon, I see some parts where I’m uncertain why they were written as they are. One example is this: What is in the code:
Tag: python-2.x
Testing the same function with different parameters
I am trying to test a function multiple times using different parameters. The return value should be True. What’s the best way to go about doing this in pytest? I want to avoid multiple functions acting as assert True wrappers e.g Answer Use @pytest.mark.parametrize()
Constructing Bech32 addresses
I’m trying to follow the steps here https://en.bitcoin.it/wiki/Bech32 to generate a valid bech32 address. I am getting stuck on the first step: Having a compressed public key (0x02 or 0x03 followed by 32 byte X coordinate): 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 Perform SHA-256 hashing on the public key: 0f715baf5d4c2ed329785cef29e562f73488c8a2bb9dbc5700b361d54b9b0554 Here is one of the things I tried: Note: I’m limited to python 2.7.18 I’m
Calling function through string
I have a Python main program that imports another module (called actions) with multiple functions. The main program should run some things, get a string (i.e. goto(114)) and then run actions.goto(114), in which 114 is the argument to the function goto(x) in actions. I’ve tried the obvious which was just trying to run the string but that did not work.
Run interactive Bash with popen and a dedicated TTY Python
I need to run an interactive Bash instance in a separated process in Python with it’s own dedicated TTY (I can’t use pexpect). I used this code snippet I commonly see used in similar programs: But when I run it I get the following output: Strace of the run shows some errors: The code snippet seems pretty straightforward, is Bash
Python2: Should I use Pickle or cPickle?
Python 2 has both the pickle and cPickle modules for serialization. cPickle has an obvious advantage over pickle: speed. What, if any, advantage does pickle have over cPickle? Answer The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes. This process is also called serializing” the object. The byte stream representing the object
Generator as function argument
Can anyone explain why passing a generator as the only positional argument to a function seems to have special rules? If we have: This works, as expected. This does not work, as expected. This works, as expected This works, but I don’t understand why. Shouldn’t it fail in the same way as 2) Answer Both 3. and 4. should be
Why doesn’t the Python 2D array index order matter when used with colon ( : )
Creating a 2D array such as and indexing using the colon operator like this works as expected. It returns all of row 2. However, if I want to retrieve all of column 2, I would instinctively do But this also returns What is the reasoning behind this? I would intuitively think that this returns the column 2 of each row.
Python Requests: Hook or no Hook?
I ‘.get’ a request and process the response like: After reading the package’s docs, I saw that there is a hook functionality which would permit me to do: My questions: When should I use hooks? Or, why should I use hooks? I wish to initiate an object (a parser) after a request’s response is returned using the requests resp.text procedure.
How to pass arguments to the metaclass from the class definition?
I’m trying to dynamically generate classes in python 2.7, and am wondering if you can easily pass arguments to the metaclass from the class object. I’ve read this post, which is awesome, but doesn’t quite answer the question. at the moment I am doing: but this requires me to do the following Is there a cleaner way of doing this?