-
python onexit
Was looking for python's equivalent to onexit and found this:
import atexit atexit.register(function_that_will_be_called_before_this_program_exits)
-
web.py: error: No socket could be created
This error message most often occurrs when trying to run web.py on an invalid address and port. An example of an invalid address would be example.net, instead of a proper ip address like 98.228.37.242.
Other Causes:
Was just trying to get a simple web.py test running:
import web urls = ( '/x', 'X', ) class X : def GET(self) : return 'x' app = web.application(urls, globals()) app.run()
but I kept getting the error:
error: No socket could be createdThe problem was that due to the way web.py loading works, you need to have the creation and running of the app only occur if this is the main entry point of the program. Otherwise, the class loader in web.py will reload this file again later, and wind up spawning a new server when its intent was simply to load a class:
if __name__ == '__main__' : app = web.application(urls, globals()) app.run()
-
python random timezone
import random import pytz pytz.timezone(random.choice(pytz.all_timezones))
-
python: parsing the output of datetime.isoformat()
I couldn't find this explicitly anywhere, so here it is. A function to parse a string which was generated by datetime.isoformat()
import datetime def parse_iso_datetime(str) : # parse a datetime generated by datetime.isoformat() try : return datetime.datetime.strptime(str, "%Y-%m-%dT%H:%M:%S") except ValueError : return datetime.datetime.strptime(str, "%Y-%m-%dT%H:%M:%S.%f") # and a simple test case def test() : d = datetime.datetime.now() assert parse_iso_datetime(d.isoformat()) == d d = datetime.datetime.now().replace(microsecond = 0) assert parse_iso_datetime(d.isoformat()) == d
-
MySQLdb: TypeError: %d format: a number is required, not str
I was getting this error message from MySQLdb:
TypeError: %d format: a number is required, not strThe error message was the result of this code:
assert type(gateway_id) == int sql = """ SELECT meter_id FROM mtus WHERE gateway_id = %d AND name = '%s' """ conn = mysql_connect() cursor = conn.cursor() cursor.execute(sql, (gateway_id, name)) ret = cursor.fetchone() cursor.close()
It turns out, execute converts all the arguments to SQL literal values. reference All %Xs should be %s and there shouldn't be any quotes around them. MySQLdb takes care of the string escaping too.
Other Causes:
For those of you arriving here from a search and are not having problems with MySQL, the reason for this error is that a string was passed into a format where a number was expected:
# throws the TypeError: message = "%d seconds until done" % "three" # works: message = "%d seconds until done" % 3
-
Rails Headers
You can access header values from rails from the request.env hash. request.env contains a lot of other non HTTP header values. The header name is a bit transformed too:
- prepended with HTTP_
- converted to uppercase
- dashes converted to undersocres
- ... more?
Example:
curl -H 'x-custom-value: foo' http://example.com/can be accessed in rails with:
request.env['HTTP_X_CUSTOM_VALUE']
-
ruby rescue error message example
begin raise "this is an error message" rescue Exception => e # prints "this is an error message" puts e.message # or if you want the entire error message with stack trace and all: puts "failed sending weekly power usage to house: #{$!}" end
-
mongorestore: ERROR: root directory must be a dump of a single collection when specifying a collection name with –collection
mongorestore wants the specific collection's filename rather than the entire database's dump folder:
dwiel@dwiel$ mongodump -d db -c variables -o ../backups/1
connected to: 127.0.0.1
DATABASE: db to ../backups/1/db
db.variables to ../backups/1/db/variables.bson
3 objects
dwiel@dwiel$ mongorestore -d db -c variables --drop ../backups/1
ERROR: root directory must be a dump of a single collection
when specifying a collection name with --collection
usage: ........
dwiel@dwiel$ mongorestore -d db -c variables --drop ../backups/1/db/variables.bson
connected to: 127.0.0.1
../backups/1/db/variables.bson
going into namespace [db.variables]
dropping
3 objectsquite obvious in retrospect given the error message, but the internet didn't know the answer yet and there isn't much documentation about mongorestore.
-
MySQL Permission Errors After Moving Datadir
I wanted to make space on my root partition and so moved my mysql data dir to /home/mysql in /etc/mysql/my.conf and received the following errors:
dwiel@dwiel:~$ sudo mysqld
091111 20:39:16 [Warning] Can't create test file /home/mysql/dwiel.lower-test
091111 20:39:16 [Warning] Can't create test file /home/mysql/dwiel.lower-test
091111 20:39:16 [Note] Plugin 'FEDERATED' is disabled.
mysqld: Can't find file: './mysql/plugin.frm' (errno: 13)
091111 20:39:16 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
091111 20:39:16 InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name ./ibdata1
InnoDB: File operation call: 'open'.
InnoDB: Cannot continue operation.
The problem was with apparmor. It was restricting mysql from reading and writing to /home/mysql. To correct this I edited the file /etc/apparmor.d/usr.sbin.mysqld and added:
/home/mysql r,
/home/mysql** rwk,
to the end of the file. Then restarted apparmor:
sudo /etc/init.d/apparmor restart
and then restarted apache with no problem
-
Build tolua++ files with makefile
Here is how you can have your makefile build your tolua++ .cpp and .h files for you. It should work for plain tolua also.
TOLUA = tolua++5.1 tolua_%.cpp tolua_%.h : %.pkg $(TOLUA) -o $(@:%.h=%.cpp) -H $(@:%.cpp=%.h) $<
this will generate tolua_file.cpp and tolua_file.h files from corresponding file.pkg files anytime they the .cpp or .h file is depended on somewhere else in the file. In my case I just added tolua_file.o to my list of objects. Here is the full makefile for the project which required this - for reference:
# LINUX LIBLUA=lua5.1 # MAC OSX #LIBLUA=lua # LDFLAGS=-arch x86_64 OBJS = swarm.o group.o scene.o vmath.o tolua_group.o tolua_swarm.o tolua_vmath.o CXX = g++ CXXFLAGS = -Wall -c -O2 `sdl-config --cflags` LDFLAGS = -Wall `sdl-config --libs` INCLUDES = -I./include -I/usr/include/lua5.1 -I/opt/local/include LIBS = -L./lib -lANN -lGL -lGLU -llo -ltolua++5.1 -l$(LIBLUA) TOLUA = tolua++5.1 tolua_%.cpp tolua_%.h : %.pkg $(TOLUA) -o $(@:%.h=%.cpp) -H $(@:%.cpp=%.h) $< %.o: %.cpp $(CXX) $(INCLUDES) $(CXXFLAGS) -c $< -o $@ # the executable swarm: $(OBJS) $(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)
