Multiple Event Loops in Different Threads
Just experimenting if I could run multiple event loops in multiple threads as we obviously can’t do multiple event loops in the same thread as each future can bind to only one loop and loop running methods are blocking.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import asyncio | |
from functools import partial | |
async def task(count, stime): | |
await asyncio.sleep(stime) | |
print ('%s: %s' % (threading.current_thread().name, count), flush=True) | |
def multi_event_loops(): | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
t1 = task(1, 6) | |
t2 = task(2, 3) | |
loop.run_until_complete(asyncio.gather(t1, t2)) | |
def creepy(): | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
t1 = task(10, 25) | |
loop.run_until_complete(t1) | |
def main(): | |
threading.Thread(target=creepy).start() | |
for _ in range(3): | |
threading.Thread(target=multi_event_loops).start() | |
main() | |
Hackerrank – HourRank5 – Hr City
nice problem, have to be extra careful when writing the recurrence relations.
dist[n]
denotes the sum of the distances between each pair of nodes in the nth step.
coun[n]
denotes the number of nodes in the nth step.
cdis[n]
denotes the distance from every node to the corner of the structure in the nth step
corn[n]
denotes the distance between north east corner to south west corner of the structure in the step
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = int(raw_input()) | |
d = map(int, raw_input().split()) | |
dist, coun, cdis, corn = [0]*(n + 1), [1]*(n + 1), [0]*(n + 1), [0]*(n + 1) | |
for x in xrange(1, n + 1): | |
dist[x] = (4 * dist[x – 1] + coun[x – 1] * (16 * coun[x – 1] * d[x – 1] + 12 * cdis[x – 1]) + 8 * cdis[x – 1] + 12 * coun[x – 1] * d[x – 1] + d[x – 1]) % 1000000007 | |
coun[x] = (4 * coun[x – 1] + 2) % 1000000007 | |
cdis[x] = (4 * cdis[x – 1] + coun[x – 1] * 8 * d[x – 1] + 3 * coun[x – 1] * corn[x – 1] + 3 * d[x – 1] + 2 * corn[x – 1]) % 1000000007 | |
corn[x] = (2 * corn[x – 1] + 3 * d[x – 1]) % 1000000007 | |
print dist[n] |
After installing Ubuntu 16.04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## ☐ ☑ ☒ | |
☐ enable partner repositories | |
☐ install y-ppa-manager | |
☐ % sudo add-apt-repository -y ppa:webupd8team/y-ppa-manager | |
☐ % sudo apt-get update | |
☐ % sudo apt-get install y-ppa-manager | |
☐ media | |
☐ install vlc | |
☐ % sudo add-apt-repository -y ppa:videolan/stable-daily | |
☐ % sudo apt-get update | |
☐ % sudo apt-get install vlc | |
☐ % sudo add-apt-repository ppa:mc3man/trusty-media | |
☐ % sudo apt-get install gimp gimp-data gimp-plugin-registry gimp-data-extras | |
☐ pycharm | |
☐ sublime-text-3 | |
☐ atom | |
☐ firefox-arc-theme | |
☐ gnome-arc-theme | |
☐ compile aria2c | |
☐ % firefox https://github.com/tatsuhiro-t/aria2/releases | |
☐ % cd /sources | |
☐ % take aria2c | |
☐ % # wget https://github.com/tatsuhiro-t/aria2/releases/download/release-X.XX.X/aria2-X.XX.X.tar.xz | |
☐ % tar -xvf aria2-*.tar.xz | |
☐ % sudo apt-get install libgnutls-dev nettle-dev libgmp-dev libssh2-1-dev libc-ares-dev libxml2-dev zlib1g-dev libsqlite3-dev pkg-config libgpg-error-dev libgcrypt-dev libssl-dev libexpat1-dev libxml2-dev libcppunit-dev autoconf automake autotools-dev autopoint libtool | |
☐ % autoreconf -i | |
☐ % ./configure –with-ca-bundle='/etc/ssl/certs/ca-certificates.crt' –without-gnutls –with-openssl –enable-libaria2 –prefix /opt/aria2c | |
☐ % make | |
☐ % sudo make install | |
☐ install cling binary | |
☐ Some Final Setup | |
☐ % sudo apt-get update | |
☐ % sudo apt-get upgrade | |
☐ % sudo apt-get install ubuntu-restricted-extras ubuntu-wallpapers* git rar flashplugin-installer unace unrar zip unzip p7zip-full p7zip-rar | |
☐ Gnome-Terminal Setup | |
☐ https://github.com/Mayccoll/Gogh # pick a theme from here | |
# CAUTION | |
sudo apt-get install lm-sensors xsensors fancontrol i8kutils | |
ps: i will update this from time to time |
wordpress_com_bot
wordpress_com_bot is a telegram bot that I made to write small blog posts from my telegram app itself. the android app is simply too slow, in fact I have to blame my ISP(aircel) for poor mobile connection.
links:
https://telegram.me/wordpress_com_bot
https://github.com/eightnoteight/tgbots
ps: this post is posted from wordpress_com_bot
openshift private pip repo
If you are working with openshift-flask, then you would surely notice that that your pip freeze to requirement.txt will fail in deploying your app. That is because most of the time your pip freeze contains the latest versions of the python modules, but if a repo is popular openshift’s private repo only contains the highly tested versions. So one solution is to check the available versions from the error messages when the app is deploed through git. Or you can use the requirements.txt --index-url
feature.
here’s my requirements.txt
--index-url https://pypi.python.org/simple/ | |
Flask==0.10.1 | |
itsdangerous==0.24 | |
Jinja2==2.8 | |
MarkupSafe==0.23 | |
psycopg2==2.6.1 | |
requests==2.9.1 | |
six==1.10.0 | |
SQLAlchemy==1.0.11 | |
SQLAlchemy-Utils==0.31.4 | |
telepot==4.1 | |
Werkzeug==0.11.3 | |
ujson==1.34 |
spoj EXPEDI
easy one.
#include <bits/stdc++.h> | |
#ifdef __mr__ | |
#include "prettyprint.hpp" | |
#else | |
#define endl '\n' | |
#endif | |
#define uint unsigned int | |
#define ulong unsigned long long | |
using namespace std; | |
template<class T> using maxheap=priority_queue<T, vector<T>, less<T>>; | |
template<class T> using minheap=priority_queue<T, vector<T>, greater<T>>; | |
int main(int argc, char const *argv[]) { | |
#ifndef __mr__ | |
ios::sync_with_stdio(0);cin.tie(0); | |
#endif | |
int testcases; | |
cin >> testcases; | |
while (testcases--) { | |
// efa: estimated fuel amount to reach the target; cfa: current fuel amount | |
// tfs: total fuel stops | |
uint n, efa, cfa, tfs = 0; | |
cin >> n; | |
vector<pair<uint, uint>> arr(n + 1); | |
for (uint i = 0; i < n; ++i) | |
cin >> arr[i].first >> arr[i].second; | |
cin >> efa >> cfa; | |
for (uint i = 0; i < n; ++i) | |
arr[i].first = efa - arr[i].first; | |
arr[n] = make_pair(efa, 0); | |
sort(arr.begin(), arr.end()); | |
maxheap<uint> memmax; | |
for (uint i = 0; i < arr.size(); ++i) { | |
if (cfa >= efa) | |
break; | |
while (!memmax.empty() && arr[i].first > cfa) { | |
cfa += memmax.top(); memmax.pop(); | |
tfs++; | |
} | |
if (arr[i].first > cfa) | |
efa = 0xffffffffu; | |
memmax.push(arr[i].second); | |
} | |
if (cfa < efa) | |
cout << -1 << endl; | |
else | |
cout << tfs << endl; | |
} | |
return 0; | |
} |
https://gist.github.com/73495213c7851e16991c
Spoj Solution Downloader
This script works for today’s SPOJ UI.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from getpass import getpass | |
from requests import Session | |
STATUS_PAGE = "http://www.spoj.com/status/{problem},{username}/" | |
MYACCOUNT_PAGE = "http://www.spoj.com/myaccount/" | |
SOLUTION_PAGE = "http://www.spoj.com/files/src/save/{sol_id}" | |
HOME_PAGE = "https://www.spoj.com/" | |
class Login(object): | |
def __init__(self, username, password): | |
self.session = Session() | |
self.username = username | |
self.password = password | |
def __enter__(self): | |
self.session.post(HOME_PAGE, data={ | |
'login_user': self.username, | |
'password': self.password | |
}) | |
return self.session | |
def __exit__(self, _1, _2, _3): | |
return False | |
class TakeDirectory(object): | |
def __init__(self, path): | |
self.oldloc = os.path.abspath(os.curdir) | |
self.newloc = path | |
def __enter__(self): | |
if not os.path.exists(self.newloc): | |
os.mkdir(self.newloc) | |
if not os.path.isdir(self.newloc): | |
raise RuntimeError | |
os.chdir(self.newloc) | |
def __exit__(self, _1, _2, _3): | |
os.chdir(self.oldloc) | |
return False | |
def getSolvedProblems(doc, username): | |
pattern = '/status/(.{3,8}),' + username + '/' | |
return re.findall(pattern, doc) | |
def getSolutions(doc): | |
pattern = '/files/src/([0-9]*?)/' | |
return re.findall(pattern, doc) | |
def download(): | |
username = raw_input("username: ") | |
password = getpass("password: ") | |
with Login(username, password) as session: | |
for prob_id in getSolvedProblems(session.get(MYACCOUNT_PAGE).content, username): | |
with TakeDirectory(os.path.abspath(prob_id)): | |
for sol_id in getSolutions(session.get( | |
STATUS_PAGE.format(problem=prob_id, username=username)).content): | |
with open(sol_id, 'w') as solution: | |
solution.write(session.get(SOLUTION_PAGE.format(sol_id=sol_id)).content) | |
if __name__ == '__main__': | |
download() |
A better mod_dirlisting for lighttpd
The default module is cool and fast, but i lacks some features like it doesn’t show the modes of the files, and not all tags are associated with class names which limits our usage in using an external css. So i made an attempt to add these to the default module.
as lighttpd runs based on a config file, you have to code the config file every-time or you can write a custom script with cmdline-args(for config) to do that for you, here’s mine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from subprocess import call | |
import tempfile | |
import os.path | |
import socket | |
import sys | |
import mimetypes | |
from netifaces import interfaces, ifaddresses, AF_INET | |
types_map = { | |
'.json': 'text/json', | |
'.cpp': 'text/c++', | |
'.c++': 'text/c++', | |
'.cc': 'text/c++', | |
'.cxx': 'text/c++', | |
} | |
types_map.update(mimetypes.types_map) | |
startport = 9009 if len(sys.argv) <= 1 else sys.argv[1] | |
for testport in range(startport, startport + 1000): | |
port = testport | |
if socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex(('0.0.0.0', testport)) != 0: | |
break | |
def ip4_addresses(): | |
ip_list = [] | |
for interface in interfaces(): | |
try: | |
for link in ifaddresses(interface)[AF_INET]: | |
try: | |
ip_list.append(link['addr']) | |
except: | |
pass | |
except: | |
pass | |
return ip_list | |
pwd = __import__('os').getcwd() | |
stylecss = open('/home/eightnoteight/.config/lighty/style.css', 'r').read() | |
config = [ | |
'server.modules = ( )', | |
'server.document-root = "{path}"'.format(path=pwd), | |
'server.port = {port}'.format(port=port), | |
'dir-listing.activate = "enable"', | |
'dir-listing.external-css = "/{csspath}"', | |
'server.indexfiles = ( "index.html" )', | |
'mimetype.assign = ( {} )'.format(', '.join([ | |
'"{}" => "{}"'.format(key, value) for key, value in types_map.items() | |
])), | |
'dir-listing.set-footer = " "', | |
'# dir-listing.external-css = "{path}"'.format(path='/tmp/tmp.css'), | |
'# debug.log-request-handling = "enable"', | |
'# debug.log-response-header = "enable"', | |
'# debug.log-request-header = "enable"', | |
'# server.kbytes-per-second = 10240', | |
'# connection.kbytes-per-second = 1024', | |
] | |
print('\n'.join( | |
['server starting at \033[91;1m{host}:{port}\033[0m'.format( | |
port=port, host=host) for host in ip4_addresses()])) | |
with tempfile.NamedTemporaryFile(prefix='lighty-', suffix=".css", dir=pwd, delete=True, mode='w+') as tempcssf: | |
print(stylecss, file=tempcssf, flush=True) | |
config[4] = config[4].format(csspath=os.path.split(tempcssf.name)[-1]) | |
with tempfile.NamedTemporaryFile(prefix='lighty-', delete=True, mode='w+') as tempf: | |
print('\n'.join(config), file=tempf, flush=True) | |
try: | |
call(["/home/eightnoteight/testbed/lighttpd-1.4.x/sbin/lighttpd", '-D', "-f", tempf.name, '-m', '/home/eightnoteight/testbed/lighttpd-1.4.x/lib']) | |
except KeyboardInterrupt as e: | |
raise e |
[link: https://gist.github.com/9b3e0a7daecdc0206305 ]
It starts a port scan from 9009 and searches for the available one to bind lighttpd, adds all known mimetypes(from python module) to the config, copies external css for mod_dirlisting. and of-course document root is current directory.
At first i tried to implement the features as mod_cgi – python using jinja which is obviously the easy way but unfortunately the mod_cgi – python is deadly slow, it decreased the speed from 360 requests / sec to 7 requests / sec. here’s my index.jinja2 and dir-generator.py.cgi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import stat | |
from os import environ, listdir | |
from jinja2 import Template | |
from datetime import datetime | |
from mimetypes import guess_type | |
def mylistdir(path): | |
items = listdir(path) | |
for item in items: | |
itemstat = os.lstat(os.path.abspath(path) + '/' + item) | |
modes = ['-', 'd'][stat.S_ISDIR(itemstat.st_mode)] + ''.join( | |
['———', 'rwxrwxrwx'][int(c)][i] for i, c in enumerate( | |
bin(stat.S_IMODE(itemstat.st_mode))[2:].zfill(9))) | |
lastmodified = str(datetime.fromtimestamp(int(itemstat.st_mtime))) | |
itemtype = [guess_type(item)[0] or '-', 'directoy'][stat.S_ISDIR(itemstat.st_mode)] | |
yield modes, item, lastmodified, itemstat.st_size, itemtype, environ["REQUEST_URI"] + item + '/' | |
cssstyle = open('/home/eightnoteight/.config/lighty/style.css', 'r').read() | |
print(Template(open('/home/eightnoteight/.config/lighty/index.jinja2', 'r').read()).render( | |
requri=environ["REQUEST_URI"], | |
cssstyle=cssstyle, | |
dirlist=mylistdir(environ["DOCUMENT_ROOT"] + environ["REQUEST_URI"]) | |
)) | |
print(environ["REQUEST_URI"], listdir()) | |
print() | |
print() | |
print() | |
print() | |
print(environ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Index of {{ requri }}</title> | |
<style type="text/css"> | |
{{ cssstyle }} | |
</style> | |
<style type="text/css"> | |
th, td { font: 90% monospace; text-align: left; line-height: 1.0;} | |
.table > thead > tr > th, | |
.table > tbody > tr > th, | |
.table > tfoot > tr > th, | |
.table > thead > tr > td, | |
.table > tbody > tr > td, | |
.table > tfoot > tr > td { | |
padding: 6px; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>Index of {{ requri }}</h2> | |
<div class="list"> | |
<table summary="Directory Listing" class="table"> | |
<thead> | |
<tr> | |
<th class="modes">Modes</th> | |
<th class="name">Name</th> | |
<th class="modified">Last Modified</th> | |
<th class="size" style="text-align: right;">Size</th> | |
<th class="type">Type</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for item in dirlist %} | |
<tr> | |
<td class="modes">{{ item[0] }}</a></td> | |
<td class="name"><a href="{{item[5]}}">{{ item[1] }}</a></td> | |
<td class="modified">{{ item[2] }}</td> | |
<td class="size" style="text-align: right;">{{ item[3] }}</td> | |
<td class="type">{{ item[4] }}</td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
</div> | |
<div class="foot"> | |
hosted by eightnoteight using lighttp | |
</div> | |
</body> | |
</html> |
[link: https://gist.github.com/38d382872086e6bf154f]
For adding class names to the tags, we have to edit mod_dirlisting.c, system stat call will provide the modes of file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setItemModes(const char* itemname, char* modes) { | |
struct stat info; | |
stat(itemname, &info); | |
mode_t mode = info.st_mode; | |
strcpy(modes, "———-"); | |
if ( S_ISDIR(mode) ) modes[0] = 'd'; | |
if ( S_ISCHR(mode) ) modes[0] = 'c'; | |
if ( S_ISBLK(mode) ) modes[0] = 'b'; | |
if ( mode & S_IRUSR ) modes[1] = 'r'; | |
if ( mode & S_IWUSR ) modes[2] = 'w'; | |
if ( mode & S_IXUSR ) modes[3] = 'x'; | |
if ( mode & S_IRGRP ) modes[4] = 'r'; | |
if ( mode & S_IWGRP ) modes[5] = 'w'; | |
if ( mode & S_IXGRP ) modes[6] = 'x'; | |
if ( mode & S_IROTH ) modes[7] = 'r'; | |
if ( mode & S_IWOTH ) modes[8] = 'w'; | |
if ( mode & S_IXOTH ) modes[9] = 'x'; | |
} | |
[link: https://gist.github.com/bab0ae824bd526f17f0e]
If you want to test it on your computer, here’s my mod_dirlisting.c, and its git diff(just in case)
[mod_dirlisting.c: https://gist.github.com/f5fcf309c62beaae841b]
[git diff src/mod_dirlisting.c: https://gist.github.com/553cfd249bffef44369c]
[style.css: https://gist.github.com/6609af61d4fa2f1682fc] (most of it is from bootstrap.css)
screenshots:
spoj main72 subset sum
the problem asks to compute sum of distinct subset sums of the given array of integers;
as the range of numbers in the array is 0 to 1000; and the size of arr is 100, we can assume that the number distinct values cannot be more than 1000*100, So complexity wise it is ok to compute all distinct subset sums. for which we can use a set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#ifdef __mr__ | |
#include "prettyprint.hpp" | |
#endif | |
#ifndef __mr__ | |
#define endl '\n' | |
#endif | |
#define ulong unsigned long | |
#define uint unsigned int | |
#define uset unordered_set | |
#define umap unordered_map | |
using namespace std; | |
namespace fast { | |
template<class InputIterator> | |
inline void fill(InputIterator first, InputIterator last, int v) { | |
memset(first, v, distance(first, last)*sizeof(*first)); | |
} | |
} | |
inline int totalsubsetsums(int* arr, int* tmp, int n) { | |
uset<int> subsetsums({0}); | |
for (int i = 0; i < n; ++i) { | |
int* tend = tmp; | |
for(int x: subsetsums) | |
*(tend++) = x + arr[i]; | |
subsetsums.insert(tmp, tend); | |
} | |
return accumulate(subsetsums.begin(), subsetsums.end(), 0); | |
} | |
int main(int argc, char const *argv[]) { | |
#ifndef __mr__ | |
ios::sync_with_stdio(0);cin.tie(0); | |
#endif | |
int t; | |
cin >> t; | |
int arr[101]; | |
int tmp[100001]; | |
while(t–) { | |
int n; | |
cin >> n; | |
for (int i = 0; i < n; ++i) | |
cin >> arr[i]; | |
cout << totalsubsetsums(arr, tmp, n) << endl; | |
} | |
return 0; | |
} |
https://gist.github.com/dfdad3667ba70be17e08
PS: i got tle for set but made it to ac by using unordered_set