Linked Questions
90 questions linked to/from "Ask forgiveness not permission" - explain
-1
votes
1
answer
66
views
Separating the lists where after conversion all values are numbers [duplicate]
To separate only the lists that converted become all numbers, I do it like this:
a = ['start','nan','1.004569','0.8554','nan']
b = ['0.464','5','4.8784','2.411474','0.44']
c = ['start','start','start',...
340
votes
25
answers
387k
views
How to read a single character from the user?
Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like getch()). I know there's a function in Windows for ...
453
votes
10
answers
289k
views
How to overload __init__ method based on argument type?
Let's say I have a class that has a member called data which is a list.
I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with ...
67
votes
7
answers
103k
views
Minimum bit length needed for a positive integer in Python
1 = 0b1 -> 1
5 = 0b101 -> 3
10 = 0b1010 -> 4
100 = 0b1100100 -> 7
1000 = 0b1111101000 -> 10
...
How can I get the bit length of an integer, i.e. the number of bits that are necessary to ...
63
votes
11
answers
120k
views
os.mkdir(path) returns OSError when directory does not exist
I am calling os.mkdir to create a folder with a certain set of generated data. However, even though the path I specified has not been created, the os.mkdir(path) raises an OSError that the path ...
30
votes
5
answers
42k
views
How to check if a byte array is a valid image?
I know there is no .Net function that exists for checking, but is there an algorithm or easy and effective way of checking if a byte is a valid image before I use the byte array. I need this because I'...
20
votes
7
answers
17k
views
How do I check if a value matches a type in python?
Let's say I have a python function whose single argument is a non-trivial type:
from typing import List, Dict
ArgType = List[Dict[str, int]] # this could be any non-trivial type
def myfun(a: ArgType) ...
25
votes
2
answers
29k
views
Pickle - Load variable if exists or create and save it
Is there a better way to load a variable with pickle if it already exists or create it and dump it if it doesn't?
if os.path.isfile("var.pickle"):
foo = pickle.load( open( "var.pickle", "rb" ) )
...
8
votes
6
answers
12k
views
Pythonic way to delete variable from self if it exists in a list
I have a list of strings ['foo1', 'foo2', ...] that represent variables that I want to delete from self if they are part of self. What is a Pythonic and compact way to do this?
My first attempt is
...
13
votes
4
answers
20k
views
Pythonic way of write if open is successful
I am looking for a pythonic way to create and write to file if opening it was successful or else return an error if not (e.g. permission denied).
I was reading here What's the pythonic way of ...
18
votes
4
answers
11k
views
Type hint for an exhaustive dictionary with Enum/Literal keys
I'm working on code bases with extensive type hints, checked by mypy. There's several instances where we have a mapping from an enum.Enum or other small finite set of statically known values (typing....
11
votes
2
answers
32k
views
How to json.load(filename) on an empty file? [duplicate]
I have a file that may or may not be empty. The goal is to read from the file an json object, append to it, and then write it back to the file. However, on the case that the file is empty, then json....
13
votes
2
answers
8k
views
Equivalent of Python's KeyError exception in JavaScript?
I am trying to access a certain member in a JavaScript object. In order to do this, I need to try out a couple of key values.
For example, Object['text/html'] which will give me an export link for a ...
5
votes
6
answers
13k
views
Python : check if the nested dictionary exist
I have several nested dictionaries within lists, and I need to verify if a specific path exist e.g.
dict1['layer1']['layer2'][0]['layer3']
How can I check with an IF statement if the path is valid?
...
4
votes
3
answers
8k
views
Python 3.6 Split() Not enough values to unpack workaround? [duplicate]
I keep getting an error in Python when I try to split a single word. From what I read, this is because the default split() command looks for whitespace. The problem is, I want the second assigned ...