Posts

Showing posts with the label python

Python multiprocessing: Pass the shared resource along to children

Update 11 Aug '20: Turned out I misunderstood how multiprocessing module works! A Process can inherit resources created in the parent process scope but that does not include globals; ie if used_ids and used_ids_lock were defined in the block marked by if __name__ == '__main__' it would work (though not recommended). Python's documentation briefly touches upon this subject (see "Explicitly pass resources to child processes" section"). So, one correct way of structuring the code is: #!/usr/bin/env python ################################################################################ # simple tool to generate n random numbers. # # usage: # main.py N_RECORDS CONCURRENCY_STRATEGY:"t"|"p" CONCURRENCY_FACTOR:int # # N_RECORDS is an integer # CONCURRENCY_STRATEGY can only be either 't' or 'p' # CONCURRENCY_FACTOR is an integer (preferrably less than or equal to the no of # cores on the machine.) # # Examp...

How To Enable Auto-Completion in Python Shell

Learn how to use TAB to auto-complete expressions in plain vannila Python shell. If you need to have auto completion in Python shell (almost the same way as iPython), it's very easy. Create a file named .pyreadline with the following content and put it in your home directory. #!python import rlcompleter, readline readline.parse_and_bind('tab: complete') Export the file as PYTHONSTARTUP variable; for example append it to .bashrc . echo "export PYTHONSTARTUP=~/.pyreadline" >> ~/.bashrc

Tornado Web Framework - Flash Messages

Implement flash messages in Tornado web framework. Problem Suppose you have a URL for editing an object under /OBJECT-ID/edit . Normally when user fills in the information on that page and presses the submit button, the request is sent to /OBJECT-ID/update which persists the data. But if the the data provided by user is not valid, you need to redirect user the to /OBJECT-ID/edit along with the provided data and validation errors, so s/he can correct it. The clean and preferred way to do this is by using flash cookies. Code All the source code provided in this post, is licensed under Apache License 2.0. import re import pickle from tornado.escape import to_unicode from tornado import web, escape class Flash(object): """ A flash message along with optional (form) data. """ def __init__(self, message, data=None): """ 'message': A string. 'data': Can be anything. ""...

Amailyser: The E-mail Analyser

Introducing a small software to analyse a mailbox. Recently Ed Daniel (a friend of mine) needed to produce a time analysis report out of his e-mails and asked if I can help him.  Well, obviously I said yes. I thought to myself, he needs charts and graphs and data querying from many aspects that I may not know.  So to me the best option is to load his e-mail important fields into an RDBMS (such as SQLite or PostgreSQL).  Later he can do whatever he wants with the data using reporting tools such as JasperReports and produce nice charts and graphs as he requires. That was the motivation to write Amailyser (github repo) . The language is Python 2.x; I could have done it in Perl but I'm learning Python and it seems like a wonderful practice to me. For DB interaction I used SQLAlchemy a nice ORM mapper for Python. To read the e-mail messages I used Python standard library mailbox. After downloading and extracting, you can run the Amailyzer (after you have modified con...