I am doing a small script in python, but since I am quite new I got stuck in one part: I need to get timing and text from a .srt file. For example, from I need to get: 00:00:01,000 –> 00:00:04,074 and Subtitles downloaded from www.OpenSubtitles.org. I have already managed to make the regex for timing…
Tag: python
How to limit choices of ForeignKey choices for Django raw_id_field
How do you limit what choices are shown for ForeignKey fields in Django’s admin when they’re displayed using the raw_id_fields option? When rendered as a select box, it’s simple to define a custom ModelForm to set that field’s queryset value with the choices to want. However, this quer…
Do attribute names consume memory on instance basis in python
Considering I have millions of objects with 3 __slots__ Is it more memory efficient to have short slot names like x vs. long like would_you_like_fries_with_that_cheeseburger? Or are the names allocated only once per class (opposed to once per instance?) Answer Names for slots only take memory per class, not p…
Check if a value exists in pandas dataframe index
I am sure there is an obvious way to do this but cant think of anything slick right now. Basically instead of raising exception I would like to get True or False to see if a value exists in pandas df index. What I have working now is the following Answer This should do the trick
How to do/workaround a conditional join in python Pandas?
I am trying to calculate time-based aggregations in Pandas based on date values stored in a separate tables. The top of the first table table_a looks like this: Here is the code to create the table: The second table, table_b, looks like this: and the code to create it is: I want to be able to get the sum of
BFS algorithm in Python
Guys! Can someone help me with this bfs code? I can’t understand how to solve this queue line. Answer To extend your queue with all nodes not yet seen on the path, use set operations: or use a generator expression: Lists don’t support subtraction. You don’t really need to filter the nodes, h…
Tracking how many elements processed in generator
I have a problem in which I process documents from files using python generators. The number of files I need to process are not known in advance. Each file contain records which consumes considerable amount of memory. Due to that, generators are used to process records. Here is the summary of the code I am wo…
Randomly extract x items from a list using python
Starting with two lists such as: I want to have the user input how many items they want to extract, as a percentage of the overall list length, and the same indices from each list to be randomly extracted. For example say I wanted 50% the output would be I have achieved this using the following code: But I wa…
How to correctly parse a tiled map in a text file?
I’m making a RPG with Python and pygame for a school project. In order to create the few maps, I have chosen the Tile Mapping techniques I have seen in some tutorials, using a *.txt file. However, I have to cut some sprites (trees, houses, …) into several pieces. The problem is that I’m runn…
Add a sequential counter column on groups to a pandas dataframe
I feel like there is a better way than this: To achieve this: Is there a way to do it that avoids the callback? Answer use cumcount(), see docs here If you want orderings starting at 1