I need my script to download a new file, if the old one is old enough. I set the maximum age of file in seconds. So that I would get back on track with my script writing I need example code, where file age is printed out in seconds. Answer This shows how to find a file’s (or directory’s) last
Tag: python
Importing modules inside python class
I’m currently writing a class that needs os, stat and some others. What’s the best way to import these modules in my class? I’m thinking about when others will use it, I want the ‘dependency’ modules to be already imported when the class is instantiated. Now I’m importing t…
Rolling or sliding window iterator?
I need a rolling window (aka sliding window) iterable over a sequence/iterator/generator. (Default Python iteration could be considered a special case, where the window length is 1.) I’m currently using the following code. How can I do it more elegantly and/or efficiently? For the specific case of windo…
Execute code when Django starts ONCE only?
I’m writing a Django Middleware class that I want to execute only once at startup, to initialise some other arbritary code. I’ve followed the very nice solution posted by sdolan here, but the “Hello” message is output to the terminal twice. E.g. and in my Django settings file, I’…
How to query database by id using SqlAlchemy?
I need to query a SQLAlchemy database by its id something similar to but for id. How do I do this? [Searching over Google and SO didn’t help] Answer Query has a get function that supports querying by the primary key of the table, which I assume that id is. For example, to query for an object with ID of
Can i code browser games in python in my website
I am not much into gaming but i am learning and doing some practicles in Artifical Intelligence algorithms. Now as i can develop full fledge application so it means even if learn various techniques , i won’t be having anything to show in interview. I have seen that all AI techniques / algorithms are usu…
Python argparse: Make at least one argument required
I’ve been using argparse for a Python program that can -process, -upload or both: The program is meaningless without at least one parameter. How can I configure argparse to force at least one parameter to be chosen? UPDATE: Following the comments: What’s the Pythonic way to parametrize a program w…
Flask-SQLalchemy update a row’s information
How can I update a row’s information? For example I’d like to alter the name column of the row that has the id 5. Answer Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then, db.session.c…
Stop parsing on first unknown argument
Using argparse, is it possible to stop parsing arguments at the first unknown argument? I’ve found 2 almost solutions; parse_known_args, but this allows for known parameters to be detected after the first unknown argument. nargs=argparse.REMAINDER, but this won’t stop parsing until the first non-o…
Paginator for inline models in django admin
I have this simple django model consisting of an sensor and values for the specific sensor. The number of values per Pyranometer is high (>30k). Is it somehow possible to paginate PyranometerValues by a specific day or generell apply a paginator to the admin inline view? Answer Have you checked raw_id_fiel…