Tuesday, December 31, 2013

How I combined simple #Python list comprehension and JSON parsing

There are lots of very interesting things to discover in Python - especially if you've been working with Java or any other programming language for some time. For example this simple piece of code demonstrates how you can quite easily convert a list of of Strings of integers separated by comma - into a list of integers. What I particularly like about this small code is that it uses what Python developers refer to list comprehension.
string1 = "020783, 503553, 555204"
mylist = [int(n) for n in string1.split(',')]
>>>>>> mylist
>>> [020783, 503553, 555204]
Now, that piece of code was all I needed to get the API application I was working on to generate a nested list of integers. As it turns out, Python makes things really easy.
def parse_points(self, json_response):
        try:
            result = json.loads(json_response)
            if result.has_key('points'):
                the_points = result['points']['code']
                mylist = [int(n) for n in the_points.split(',')]
                return mylist 
            else:
                return result
        except ValueError, e:
            raise PlottingError('JSON Parsing Error: ' + e)
.... Further down the code, that returned value is eventually used to as an array of list:
     "waypoints":[020783,
                  503553,
                  555204]
This would not make much sense to a lot of people out there - that is deliberate because I don't want to show the code in full. Protecting the real code is vital here. However, the aim for this small code is to demonstrate how to parse a list of integers in string separated by comma. With time I will post a full blog with a complete working code on how this works.

Monday, December 02, 2013

Python 3 Object Oriented Programming - The Review

Prior to reading Python 3 Object Oriented Programming I was already familiar with advanced OOP concepts and Python 2 in particular; as well as with Java and other languages. So, I expected the book to enforce my thinking and help me to understand new features provided by Python 3 as compared to version 2. I think the book managed to do this in an excellent manner.

I think the approach used by the book is well suited for a wide range of readers. It explains enough theory and provides useful examples that help to understand how to apply OOP in practice. People new to Python and/or OOP have a lot to gain from reading Python 3 Object Oriented Programming. More experienced users of the language may find the book ideal as reference material.

It's important to note that the book focuses on OOP particularly in the context of Python. Don't expect any history lessons or theory on various OOP approaches (prototypes vs. classes, ie.) beyond the one (class based approach) used in Python. Despite this the book provides excellent value. I do recommend checking out several other languages (Java, Lua, JavaScript, Smalltalk) and paradigms (AOP, traits) for further inspiration.

You can get a copy here >>

Tuesday, January 08, 2013

Gray Hat Python for Windows

In my career, Python was the first programming language that I have learned. I then learned programming in Java and C++. I looked back to Python for it's simplicity and versatility. That is why I decided to pick up Gray Hat Python for Windows to learn what Python can do in hacking and reverse engineering since that is something I am interested in.

The author's writing style is clear, direct, and mature. The format of the book is clean and well structured which makes it easier to maneuver. The problem I had with the book was that the code in the book is unreliable. The code in the book and the code in the website do not match. Also, the book is Windows oriented which may be a disappointment for some people.

Overall, there was an abundance of valuable information in the book that grabbed my attention such as fuzzing and the debugging. The book covers a lot of subjects such as debugging, hooking, code injection, and more. The author does a very good job with consolidating the information in the book.
Related Posts with Thumbnails