You are here

python

Paramiko's SFTPFile.truncate()

Paramiko is the go-to module for utilizing SSH/SFTP in Python. One one the best features of Paramiko is being able to being able to SFTPClient.open() a remote file and simply use it like you would use a local file. SFTPClient's open() returns an SFTPFile which is a file-like object that implements theoretically the same behavior as Python's native file object.

But the catch here is file-like. It is like a file, except when it is not like a file.

Interrogating the Infallible Secretary

Numerous applications in GNOME exhibit magically wonderful behavior, like they remember everything and know what you want. One example of such an application is the excellent PDF reader [Evince](http://projects.gnome.org/evince/; every time I open a PDF it opens to the same page as the last time I looked at that document.

Encoding sambaNTPassword With Python

Samba's sambaNTPassword attribute, which mimics the corresponding NT / Active Directory attribute, has a value that must be a hex encoded MD4 hash of the user's password with a UTF-16 encoding. Fortunately generating such a string is a Python one-liner.

import hashlib

password = 'fred123'
nt_password = hashlib.new('md4', password.encode('utf-16le')).digest().encode('hex').upper()

Note that Samba wants all the alpha characters in the string as upper-case.The result will always be 32 characters long.

Counting weekdays between dates

Need a method to accurately count the number of weekdays between two days? [The key here is "accurately", it is a bit harder than it seems at first]. In Python there are several ways to do this, but most involve some iteration or list comprehension. In my opinion, if you have to do that, you are probably violating the Python idiom of "use the batteries".

If a record exists

A common action when synchronizing data between some source and a database is to check if such-and-such record already exists and needs to be updated or if a new record needs to be created. The SQLAlchemy's one() method [of the query object] provides an easy way to check to see if such-and-such record exists; but it doesn't return either an ORM object or None - if no record is found it raises an exception. This is surprising at first as x=do;if-not-x is possibly the most common of all Python constructs.

A JSONDateTime TypeDecorator

JSON doesn't provide a date or date-time construct; therefore every application is burdened with implementing a solution of it's own for receiving date-time information over the wire. On common issue receiving JSON and serializing that data into some type of database - but the database knows the value is a date-time and you want to be able to perform date-time like operations on the value (locally).

Discovering DLL Version With pefile

A Microsoft KB article claimed that if a specific DLL was at least a certain version that a bug reported by one of my users would be resolved. But the user was using their computer and I dislike interrupting people's work (I know how annoying it is when someone interrupts me). No problem; I can just grab the named DLL off their machine over the network and copy it to my home directory. But I'm not running Windows and all file tells me is that the DLL is a 32-bit PE file.

Querying Connectivity

You're application almost always needs to know if there is a working network connection. This is typically handled by placing the connection attempt in a try...catch block. That works, but can be slow, and it means the UI can't really adapt to the level of current connectivity. A much better solution is to query the NetworkManager [used by every mainstream distribution] via the System D-Bus for the current connectivity.

Compression & Decompress Of A Stream

So far in Python I had not found a good method / module for performing compression and decompression of data as streams; most tools required files to be compressed which has some obvious limitations. But then I saw a mention of pyLZMA roll by. It supports compression and decompression of streams using the Lempel–Ziv–Markov chain algorithm.

Tags: 

Printing Via LPR

If you have a Python app, or almost any kind of app, the accepted manner for printing is to use some kind of subprocess to invoke some command-line utility to submit the print job. Of course this requires that the underlying subsystems are aware of printers [and thus run a printer subsystem]. It also assumes the name of the command-line utility, the permissions are adequate to execute it, and all manner of other things. To put it simply: this is terrible! Why does my web server, workflow server, etc... need to run a print service?

Pages

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer