Pro tip to renaming a key in a python dictionary:
mapping = { 'old_key_name': 'new_key_name' }
for key, value in my_dict.iteritems():
if key in mapping:
my_dict[mapping[key]] = value
del my_dict[key]
YATB – Yet Another Technology Blog.
Pro tip to renaming a key in a python dictionary:
mapping = { 'old_key_name': 'new_key_name' }
for key, value in my_dict.iteritems():
if key in mapping:
my_dict[mapping[key]] = value
del my_dict[key]
Posted in Python.
– February 1, 2013
– January 28, 2013
If you’re having a problem getting Flask-admin to play ball with apache2 (mod_wsgi) in a virtualenv then this might be of help.
Its an easy fix to an annoying problem.
The Problem:
You installed all flask-env into your virtualenv, but it browsing to it just returns a 404.
Chances are you have something like this in your app.py if you’ve been following the flask-admin quick start guide.
if __name__ == '__main__':
admin = admin.Admin(name='admin')
admin.add_view(MyAdminView(name='Hello 1', endpoint='test1', category='One'))
admin.add_view(AnotherAdminView(name='Hello 2', endpoint='test2',category='Two'))
admin.init_app(app)
app.run()
The solution:
admin = admin.Admin(name='admin')
admin.add_view(MyAdminView(name='Hello 1', endpoint='test1', category='One'))
admin.add_view(AnotherAdminView(name='Hello 2', endpoint='test2',category='Two'))
admin.init_app(app)
if __name__ == '__main__':
app.run()
Posted in Python, Technology.
– November 15, 2012
Here is a really quick way to have an Amazon Elastic IP address assigned to a specific machine instance when it boots. In this case, I am using Ubuntu.
1. Obtain an elastic IP address from within your management console. Under Network & Security – > Elastic IPs -> Allocate New address
2. Login to the target system and sudo to root.
3. Install Amazon’s EC2 Tools (instructions here)
3. cd /etc/init.d/
4. vim ec2-elastic-ip and put the following script in place, tailoring it to your particular setup.
#!/bin/sh
export EC2_HOME=/opt/aws/apitools/ec2
export JAVA_HOME=/usr/lib/jvm/jre
export PATH=$PATH:$EC2_HOME/bin
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
USERDATA=$(curl -s http://169.254.169.254/latest/user-data)
ELASTIC_IP=$(echo $USERDATA | awk 'BEGIN{RS="|";FS="="} /elastic-ip/ {print $2}')
if [ -n "${ELASTIC_IP}" ]
then
ec2-associate-address -K /home/ec2-user/pk.pem -C /home/ec2-user/cert.pem -i $INSTANCE_ID $ELASTIC_IP
else
echo "No Elastic IP passed."
fi
5. Save out the file and run
update-rc.d ec2-elastic-ip defaults
Job done.
Posted in Technology.
– October 8, 2012
The Link List 21st May 2012
Topics Covered:
Application performance, distributed systems design, MongoDb, Javascript Raphael, Careers, Coding for scale, scalability bottlenecks.
1. http://www.bluebox.net/news/2012/04/distributed-systems-design-part-1/
2. http://www.techspot.co.in/2012/01/application-performance-and.html
3. http://hacks.mozilla.org/2012/05/the-web-developer-toolbox-raphael/
4. http://kathycassidy.com/2012/05/15/why-my-six-year-olds-have-digital-portfolios/
5. http://stackoverflow.com/a/1619677/1242999
6. http://horicky.blogspot.com/2012/04/mongodb-architecture.html
7. http://highscalability.com/blog/2012/5/16/big-list-of-20-common-bottlenecks.html
8. http://www.rackspace.com/knowledge_center/databases/how-to-write-code-that-scales
Happy Learning!
Posted in Technology.
– May 21, 2012
This is simply a great presentation on the use of Redis and Python.
Sides of the talk can be found here
I seriously recommend that you subscribe to this guys blog. He is one smart man who is sharing his tech!
– May 3, 2012
Alternate PHP Cache – A summary
**APC is not a replacement for Memcached.***
*/ APC is native to PHP so not external deps need to be staistifed
*/ APC is perfect for small apps under load.
*/ APC is limited by PHP’s memory limit
*/ APC should be used when speed is important – user facing scripts for e.g.
The process of serving a PHP script is as follows:
PHP Script -> Parsed -> Compiled -> Excuted -> Output
This is not ideal in high traffic scenarios as this process is repeated.
APC solves this resource problem by caching the previously compiled code for excution.
ACP Process
PHP Script -> converted to opcodes -> excuted -> output.
This process removes the Parse and Compile steps from the normal flow for PHP scripts.
Limitations
Default size is 30mb – this can be modified.
Only caches to the host machine and therefore cannot be shared between local PHP servers.
Storing Data
Done via apc_add or apc_store functions
This means the values of a variable can be APC’d
Do need to keep in mind the limitations of this as memory is limited.
Example:
$apcTest = ‘This is a storable item’;
// apc_store is key value – so generate a good random key.
$key = md5($apcTest);
apc_store($key, $apcTest);
// Retrieving by key
echo apc_fetch($key);
The conventional method with user caches is first to check if the value exists in the cache before
fetching it from source.
Compared with fetching data from a mysql store takes X ms and initially this same data takes
slightly longer (approx X + 2 ms) the first time its cached. After that, the performance can be
up to 50x. Yay!
APC typically caches compiled files whenever they are executed.
Pre-compilation before execution can be triggered with the apc_compile_file() method.
This is generally used to warm up the cache on some systems.
TTL
Normal process is to flush the entire cache when it becomes full.
However, you can set a TTL on opcode caches with the apc.ttl configuration directive.
This can even be customised on specific keys if needs be.
Deleting Files:
Done with apc_delete($filename);
This can be costly as it might trigger a re-hashing of internal hash tables.
Best to work using auto expiry basis.
Tools
VLD (Vulcan Logic Dumper/Disassembler) allows to check out the opcodes generated by compilation
and execution of PHP scripts
Posted in PHP.
– March 1, 2012