My algo: If I take first 2 numbers as 0, 1; the number that I find first in while loop will be an odd number and first of Fibonacci series. This way I calculate the even number and each time add the value of even to total. If value of even is greater than 4e6, I break from the infinite
Tag: python
custom dict that allows delete during iteration
UPDATED based on Lennart Regebro’s answer Suppose you iterate through a dictionary, and sometimes need to delete an element. The following is very efficient: The only overhead here is building the list of keys to remove; unless it grows large compared to the dictionary size, it’s not an issue. How…
How to find tag with particular text with Beautiful Soup?
How to find text I am looking for in the following HTML (line breaks marked with n)? The code below returns first found value, so I need to filter by “Fixed text:” somehow. UPDATE: If I use the following code: then it returns just Fixed text:, not the <strong>-highlighted text in that same e…
Ignoring library.zip in py2exe
I need to have an executable file (.exe) which takes some parameters from a file which can be configured by the user depending on his/her need. Since i have created scripts in python, i have used py2exe to create that executable. Though i could place a configurable file called settings.dat as data file and us…
how to set foreign key during form completion (python/django)
During form processing I’d like to be able to set a foreign key field on a model object without the user having to select the key from a dropdown. For instance: The user will navigate from a view showing an instance of ‘AAA’ to a create_object style view that will create an instance of ̵…
How to trace the path in a Breadth-First Search?
How do you trace the path of a Breadth-First Search, such that in the following example: If searching for key 11, return the shortest list connecting 1 to 11. Answer You should have look at http://en.wikipedia.org/wiki/Breadth-first_search first. Below is a quick implementation, in which I used a list of list…
Issue with virtualenv – cannot activate
I created a virtualenv around my project, but when I try to activate it I cannot. It might just be syntax or folder location, but I am stumped right now. You can see below, I create the virtualenv and call it venv. Everything looks good, then I try to activate it by running source venv/bin/activate I’m …
adding xml content to sphinx generated doc
So here is my problem. I’m trying to generate documentation for my project using sphinx and managed to get the basic working, like adding modules and images. So a basic index.rst would look like: ‘tvb’ here contains top level modules and links to sub-packages and so on. This seems to work fi…
How to access the keys or values of Python GDB Value
I have a struct in GDB and want to run a script which examines this struct. In Python GDB you can easily access the struct via Now I got this variable called mystruct which is a GDB.Value object. And I can access all the members of the struct by simply using this object as a dictionary (likemystruct[‘me…
Python : clear a log file
I develop a client-server application and I have log in the server, so I use the logging module. I would like to create a command in the server to clear the file. I have test with os.remove() but after, the log doesn’t work. Do you have an idea? Thanks. Answer It might be better to truncate the file ins…