Skip to content

Tag: python

How to declare a dictionary with inline function

Do I have to formally define a function before I can use it as an element of a dictionary? I would rather define the function inline. I just tried to type out what I want to do, but the whitespace policies of python syntax make it very hard to define an inline func within a dict. Is there any way

Ray and square/rectangle intersection in 3D

Hei. Are making a game and are looking for a ray intersection onto a square or a rectangle only in 3D space. Have search the web and found many solutions but nothing i can understand have a line and line segment intersection script in 2D but i cant figure out have to make it 3D. It is not important from

How to convert a binary (string) into a float value?

I want to convert a binary number into a float number. Here’s an example of a possibility: gives me the correct output: Unfortunately, I am working with binary strings, i.e., I need something like float(‘-0b1110’). However, this doesn’t work: I tried to use binascii.a2b_qp(string[, hea…

Retain all entries except for one key python

I have a python dictionary. Just to give out context, I am trying to write my own simple cross validation unit. So basically what I want is to get all the values except for the given keys. And depending on the input, it returns all the values from a dictionary except to those what has been given. So if the

detecting bind mounts on linux

I am looking for a way to determine if a given path is a bind mount point (on linux). The standard techniques for detecting regular mount points don’t seem to work. Even the mountpoint command fails to detect bind mounts. Answer I’m not sure there is supposed to be a way to do that (except perhaps…

Controlling Yaml Serialization Order in Python

How do you control how the order in which PyYaml outputs key/value pairs when serializing a Python dictionary? I’m using Yaml as a simple serialization format in a Python script. My Yaml serialized objects represent a sort of “document”, so for maximum user-friendliness, I’d like my ob…

How can I control what scalar form PyYAML uses for my data?

I’ve got an object with a short string attribute, and a long multi-line string attribute. I want to write the short string as a YAML quoted scalar, and the multi-line string as a literal scalar: I’d like the YAML to look like this: How can I instruct PyYAML to do this? If I call yaml.dump(my_obj),…