Skip to content

Tag: python

How do I change a raster’s geotransform with GDAL?

I’m trying to use GDAL to set the geotransform of a new raster that I’ve created by following the steps outlined in the GDAL API Tutorial. The dataset.SetGeoTransform() documentation says that this should set the affine transformation coefficients (which, according to the dataset.GetGeoTransform()…

Setting a default value in sqlalchemy

I would like to set a column default value that is based on another table in my SQLAlchemy model. Currently I have this: What I need is (roughly) this: How can I implement this in SQLAlchemy? Answer The documentation gives the following possibilities for default: A scalar, Python callable, or ClauseElement re…

best way to preserve numpy arrays on disk

I am looking for a fast way to preserve large numpy arrays. I want to save them to the disk in a binary format, then read them back into memory relatively fastly. cPickle is not fast enough, unfortunately. I found numpy.savez and numpy.load. But the weird thing is, numpy.load loads a npy file into “memo…

Convert an IP string to a number and vice versa

How would I use python to convert an IP address that comes as a str to a decimal number and vice versa? For example, for the IP 186.99.109.000 <type’str’>, I would like to have a decimal or binary form that is easy to store in a database, and then retrieve it. Answer converting an IP string …

Printing Lists as Tabular Data

I am quite new to Python and I am now struggling with formatting my data nicely for printed output. I have one list that is used for two headings, and a matrix that should be the contents of the table. Like so: Note that the heading names are not necessarily the same lengths. The data entries are all integers…

Count occurrences of a couple of specific words

I have a list of words, lets say: [“foo”, “bar”, “baz”] and a large string in which these words may occur. I now use for every word in the list the “string”.count(“word”) method. This works OK, but seems rather inefficient. For every extra word added…