Skip to main content

Posts

Showing posts with the label simple

Tmux quick start guide

Tmux is a handy terminal manager that allows you to switch between terminal sessions easily. Without losing history or windows upon ssh disconnects or similar. It is like screen , just better. (First of all because of using client-server based technology... ) Here is my minimal keyboard shortcuts guide that allows you to start using Tmux in a blink of an eye. Endless advanced commands and hotkey combinations you could always find by entering "man tmux" in a terminal. Tmux is installed quite easily in most of common linux based systems. Just type: Ubuntu: $ sudo apt - get install tmux CentOS: $ sudo yum install tmux This allows you to start using by starting it with $ tmux a | | tmux new This command first tries to attach to existing running tmux instance and creates new in case it is not found. Ctrl+b d - Will allow you to disconnect at any time. (This is also a way it is happening when you loose ssh session. How to connect - look earlier) Each sess...

Python Generators explained simply.

I'm often confused by recent obsession of generators in python. I'll try to explain them as simply as possible. Say you have never used iterators but coded in python. BUT I'm sure you have used dictionaries ( dict ) and have met requirement to iterate through it's keys and/or values. TADA! You have used generators already ;). So generator is a  function  in python. Except for it uses a keyword  yield  in it's code. Thus making it the iterator. So you could call this function in a sequence like you would probably do iterating over a dictionary already. E.g.: # typical iteration through dictionary key: value set for key, value in dict .iteritems(): # do someth # Usage of your own iterator for item in iterator_functuon(): # do something with function generated output So this function returns an iterator generator like the  dict()  type has by default. And has a  next()  function, like usual iterators have. So in attempt t...

Raspberry Pi first steps and basic network configuration on a Mac

Here are my first steps. And I hope you will find something useful here, while configuring your Pi... First of all. Mine have been bought on ebay, from resellers. And were delivered a while ago. Main purpose of this purchase war to attach a headless server to my router. I have 2 external HDD's and would like to have torrents, Time Mashine fro my macs and so on. So buying a handheld computer like this would be a bargain for me. As for built in decisions like some kinds of NAT devices and different routers with external HDD features... They are either cost a lot or lack some kinds of desired functionality. So the goal is to make some kind http/api manageable server in my local network with Time Mashine and file storage/backup. Just for fun. And to have only laptop on my work table. SO back to the Pi. If you are buying "device only" configuration, like I did. First of all you'll need some different kind of things many computer fans usual...

Python: simple recipe to measure your function's execution time

We always write something unusual while doing basic things. Our own bicycles and crutches to work out some unusual situation. Here is another recipe to do a thing like so. I have tried several libraries and readymade decisions. But it assumes you have them installed. And you often try things in console, don't you? Anyway the recipe is simple and quite straightforward. import datetime # Getting first timestamp t1 = datetime . datetime . now ( ) # Your function e.g.: data = [ g . name for g in request . user . groups . all ( ) ] # Second timestamp t2 = datetime . datetime . now ( ) print "Execution time: %s" % ( t1 - t2 ) It is rude and quite simple but may often suit you well to measure execution time in a simple and straightforward manner. Also nice idea to write down this function into logs. It may be handy on refactoring of your core app for e.g.; import datetime import logging log = logging . getLogger ( 'mylogger' ) # Getting ...

Users, Groups and their Permissions in Django + Recipes

Django supports security models and methods out of the box. They are Group and Permission objects. Permission is m2m related to internal Django User. I helps you relay on request.user later in your code. You often come to situations where you may need a view to be accessed only by certain group of users.  For example you have the app that has two groups of users. One can search and another one can Index files. Simplest approach is to use Groups here.  In fact you may use permissions in case your app will have several unique users that might do some stuff. In general best approach is to use Group to specify type of users and Permission to specify the role of users in this group. So if you will have Group called 'search' and it will have permission with name, say 'search stuff'. So when you will call: def my_view (request): # ... my view actions ... user_permissions = request.user.user_permissions.all() for p in user...

Python sort() patterns

Python has a lot of sorting patterns. Let's make a short a list. 1. Sorting list by it's element. Simple case. Should simply do: > > > exmpl_list = [ 'a' , 'c' , 'B' , 'd' ] > > > exmpl_list . sort ( ) [ 'a' , 'B' , 'c' , 'd' ] However this example does not take locale into account and works only for ASCII characters. 2. Sorting list of sub elements. exmpl_list = [ { 'name' : 'Homer' , 'age' : 39 } , { 'name' : 'Bart' , 'age' : 10 } ] # Sorting by 'name' newlist = sorted ( exmpl_list , key = lambda k : k [ 'name' ] ) # Better way to use itemgetter(): from operator import itemgetter newlist = sorted ( exmpl_list , key = itemgetter ( 'name' ) ) Note that it is equivalent to: exmpl_list . sort ( key = lambda k : k [ 'name' ] ) # OR: exmpl_list . sort ( key = ite...