Thread Synchronization in Python using Lock

python-logo

Python provides a synchronization mechanism through Lock objects in its threading package. To learn more about locks or thread synchronization in general, have a look at here. In this post, I will demonstrate how we can make synchronize access to a shared piece of data such as a list in Python. Let’s start with the code first. import threading myList = [] myListLock = threading.Lock() def addToList(item): myListLock.acquire() myList.append(item) myListLock.release() def removeFromList(item): myListLock.acquire() myList.remove(item) myListLock.release() def getItemAt(pos): toret = ” myListLock.acquire() toret = myList[pos] myListLock.release() return toret This demonstrates a very simplified implementation of how we can establish synchronized access …

[ Continue Reading... ]

How to Display Html in TextView in Android

android_logo

TextViews in Android are much powerful than they initially seem to be. The best part with this UI element is that it can render basic html tags such as links and even styling attributes, which is similar to what a WebView is usually capable of. Here’s an example. In one of my apps, I needed to display two lines of text with three words of different colors. The line should re-size and wrap itself automatically, so it was not possible to have separate TextViews with different colors and juxtaposing them side by side. Why? Because android does not provide anything …

[ Continue Reading... ]

Extract Email Addresses Using Regular Expression in Python

python-logo

In an earlier post, I have demonstrated how we can extract links or anchor tags from html documents in python. The intent here is similar, but this time, we are going to pull out email addresses (an definitely not send them spam emails, please don’t). Python is awesome with regular expressions, and it is one of the prime reasons why python is so popular for crawling or scrapping jobs. Regular expression for emails r”’([\w\-\.+]+@\w[\w\-]+\.+[\w\-]+)”’ This is definitely not the definitive version for emails, and there could be some cases that this does not cover. However, this has served me humbly …

[ Continue Reading... ]

Extract Href Links (Urls) From HTML Using Regular Expression in Python

python-logo

Regular expressions with python is one of the nicest things you would find in python. In this post, I will demonstrate how we can extract links or anchor elements from a html document. This is particularly useful if you need to pull out the links from a web page. Here’s the regular expression for extracting whatever is there in an anchor element. As you would probably know, having the whole expression inside parentheses will return whatever matches the expression inside. If confused, experiment with the expression below by removing starting and ending parentheses for a pair. Regular expression for href …

[ Continue Reading... ]

Restore Windows Vista or 7 from .wim file

windows

This technique has been applied to restore Windows 7 to the factory settings found in the Recovery partition in a dell inspiron laptop. The partition is usually hidden. To make it visible, right click on My Computer and choose Manage. From the list of drives that appears, right click on the drive labeled Recovery and choose Mount. Note the drive letter assigned to the partition. Also collect a Windows 7 DVD because the recovery process needs to be carried out from the software in the DVD, not from hard-disk. So, we are wiping away whatever junk is there in C:. Lets say the drive …

[ Continue Reading... ]

Inflating and Encapsulating Layout XML into View Object in Android

android_logo

There are two different ways that layout files in xml can be inflated into the Activity class. The most straightforward method, usually seen in introductory materials demonstrating android Activity, is to use the the xml in setContentView function for the activity. For example, here’s how the HomeActivity will inflate home.xml into itself using setContentView. public class HomeActivity extends Activity{     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.home); } } Well, now that the xml has been inflated, what’s next? The view elements such as TextView, ImageView, etc. will now need to be queried from …

[ Continue Reading... ]

Convert Underscored Names to Camel Case in Python

python-logo

Variables in C, or names database columns, are typically written as words separated by underscores. For example, the column containing employee’s id would be usually named as ‘employee_id’. I was working on a generator in python that creates a model class in java from database table specification. As such, field names such as ‘employee_id’ were converted to employeeId, and table names such as employee_salary converted to EmployeeSalary. Here’s how we can do that.   Generating class names from table names: tableName = employee_salary javaClassName = [x.title() for x in fieldName.split('_')] Generating member variable names from field names: fieldName = ‘employee_something_id’ …

[ Continue Reading... ]

Android Device Detection Error ‘???????????? no permissions’

android_logo

I have suffered quite a few times in this pain. And then I found these three commands that saved my world! First, connect your device with the USB, and give the following commands. (In Ubuntu) sudo adb stop-server sudo adb start-server sudo adb devices

[ Continue Reading... ]

Parsing JSON in Android

android_logo

Web-services and APIs, for instance the Graph API of Facebook, depend heavily on JSON and XML to expose their functionalities. In this post, I will demonstrate how we can parse JSON data from a web-service (simply said, a url) into a  Java object. Android already provides necessary classes to work with JSON, and its as simple as it can be. See the sample usage below. First, import the following library classes. import org.json.JSONException; import org.json.JSONObject; Here’s a sample JSON we will parse. {   ”data” : {     ”id”: “1234″,     ”category” : “my_category”,     ”name” : “my_name”   } } And here’s the code …

[ Continue Reading... ]

Get GPS Location Coordinates in Android

android_logo

Android provides api to retrieve GPS location information. Based on our application, we will either call the service once to fetch the current coordinates, or set up a recurring event that updates us with the latest gps location coordinates at regular intervals. I have recently built an app that required the current location to be posted on a web server only when a user starts the applciation. So it didn’t matter where he moved to later on, I just needed to report his initial location he was when he started the app. Its a very simple task, hence the code …

[ Continue Reading... ]
Page 1 of 212