static

Python class static methods

Python class static methods Question: I want to create a kind of utility class which contains only static methods which are callable by the name class prefix. Looks like I’m doing something wrong 🙂 Here is my small class: class FileUtility(): @staticmethod def GetFileSize(self, fullName): fileSize = os.path.getsize(fullName) return fileSize @staticmethod def GetFilePath(self, fullName): filePath …

Total answers: 7

Python static setters and getters?

Python static setters and getters? Question: I have put a Jira client for my django application together and now need to make it static but I can’t figure how to convert @property and @?.setter into static fields: Say I have a class: class nothing(object): auth_token = None counter = 0 @property def a(self): self.log(‘getter’) if …

Total answers: 3

Proper way to handle static files and templates for Django on Heroku

Proper way to handle static files and templates for Django on Heroku Question: I’m moving over my django app to Heroku, and I was wondering what the proper way to handle static files is. Do I just push them via git to Heroku? Or should I be storing them on SW3 or something? Also, what …

Total answers: 2

Static classes in Python

Static classes in Python Question: I once read (I think on a page from Microsoft) that it’s a good way to use static classes, when you don’t NEED two or more instances of a class. I’m writing a program in Python. Is it a bad style, if I use @classmethod for every method of a …

Total answers: 5

How to serve static files from a different directory than the static path?

How to serve static files from a different directory than the static path? Question: I am trying this: favicon_path = ‘/path/to/favicon.ico’ settings = {‘debug’: True, ‘static_path’: os.path.join(PATH, ‘static’)} handlers = [(r’/’, WebHandler), (r’/favicon.ico’, tornado.web.StaticFileHandler, {‘path’: favicon_path})] application = tornado.web.Application(handlers, **settings) application.listen(port) tornado.ioloop.IOLoop.instance().start() But it keeps serving the favicon.ico that I have in my static_path (I …

Total answers: 3

How to statically link a library when compiling a python module extension

How to statically link a library when compiling a python module extension Question: I would like to modify a setup.py file such that the command “python setup.py build” compiles a C-based extension module that is statically (rather than dynamically) linked to a library. The extension is currently dynamically linked to a number of libraries. I …

Total answers: 3

Static files in Flask – robot.txt, sitemap.xml (mod_wsgi)

Static files in Flask – robot.txt, sitemap.xml (mod_wsgi) Question: Is there any clever solution to store static files in Flask’s application root directory. robots.txt and sitemap.xml are expected to be found in /, so my idea was to create routes for them: @app.route(‘/sitemap.xml’, methods=[‘GET’]) def sitemap(): response = make_response(open(‘sitemap.xml’).read()) response.headers[“Content-type”] = “text/plain” return response There …

Total answers: 10

Instance variables vs. class variables in Python

Instance variables vs. class variables in Python Question: I have Python classes, of which I need only one instance at runtime, so it would be sufficient to have the attributes only once per class and not per instance. If there would be more than one instance (which won’t happen), all instance should have the same …

Total answers: 4

Overriding a static method in python

Overriding a static method in python Question: Referring to the first answer about python’s bound and unbound methods here, I have a question: class Test: def method_one(self): print “Called method_one” @staticmethod def method_two(): print “Called method_two” @staticmethod def method_three(): Test.method_two() class T2(Test): @staticmethod def method_two(): print “T2” a_test = Test() a_test.method_one() a_test.method_two() a_test.method_three() b_test = …

Total answers: 3

What is the Python equivalent of static variables inside a function?

What is the Python equivalent of static variables inside a function? Question: What is the idiomatic Python equivalent of this C/C++ code? void foo() { static int counter = 0; counter++; printf(“counter is %dn”, counter); } specifically, how does one implement the static member at the function level, as opposed to the class level? And …

Total answers: 30