literals

Python type hints: How to use Literal with strings to conform with mypy?

Python type hints: How to use Literal with strings to conform with mypy? Question: I want to restrict the possible input arguments by using typing.Literal. The following code works just fine, however, mypy is complaining. from typing import Literal def literal_func(string_input: Literal["best", "worst"]) -> int: if string_input == "best": return 1 elif string_input == "worst": …

Total answers: 1

Get dummies for column of literals, with some values without quotation marks

Get dummies for column of literals, with some values without quotation marks Question: I have a .csv file downloaded from an SQL database, where column value is a list of strings. However some values come without quotation marks around them: user_id B value 0 a1 3 {no_quotations} #no quotation mark ardoun this word 1 a2 …

Total answers: 1

Determining if object is of typing.Literal type

Determining if object is of typing.Literal type Question: I need to check if object is descendant of typing.Literal, I have annotation like this: GameState: Literal[‘start’, ‘stop’] And I need to check GameState annotation type: def parse_values(ann) if isinstance(ann, str): # do sth if isinstance(ann, int): # do sth if isinstance(ann, Literal): # do sth But …

Total answers: 3

Do Python dict literals and dict(list of pairs) keep their key order?

Do Python dict literals and dict(list of pairs) keep their key order? Question: Do dict literals keep the order of their keys, in Python 3.7+? For example, is it guaranteed that {1: "one", 2: "two"} will always have its keys ordered this way (1, then 2) when iterating over it? (There is a thread in …

Total answers: 1

Getting the literal out of a python Literal type, at runtime?

Getting the literal out of a python Literal type, at runtime? Question: How can I get the literal value out of a Literal[] from typing? from typing import Literal, Union Add = Literal[‘add’] Multiply = Literal[‘mul’] Action = Union[Add,Multiply] def do(a: Action): if a == Add: print(“Adding!”) elif a == Multiply: print(“Multiplying!”) else: raise ValueError …

Total answers: 2

Passing Array to Spark Lit function

Passing Array to Spark Lit function Question: Let’s say I have a numpy array a that contains the numbers 1-10: [1 2 3 4 5 6 7 8 9 10] I also have a Spark dataframe to which I want to add my numpy array a. I figure that a column of literals will do …

Total answers: 2

Python's Passing by References

Python's Passing by References Question: Hello I am trying to understand how Python’s pass by reference works. I have an example: >>>a = 1 >>>b = 1 >>>id(a);id(b) 140522779858088 140522779858088 This makes perfect sense since a and b are both referencing the same value that they would have the identity. What I dont quite understand …

Total answers: 3

Type hint for a function that returns only a specific set of values

Type hint for a function that returns only a specific set of values Question: I have a function that can only return a, b or c, all of them are of type T. I want to include this fact in the signature because of the special meaning they carry in the context of the function. …

Total answers: 3

How to use digit separators for Python integer literals?

How to use digit separators for Python integer literals? Question: Is there any way to group digits in a Python code to increase code legibility? I’ve tried ‘ and _ which are digit separators of some other languages, but no avail. A weird operator which concatenates its left hand side with its right hand side …

Total answers: 4