Posts Tagged ‘python’

Definition: unions in C and Python

Unions are basically the same as structs except that in a union all variables have the same memory location. This makes it easy to store the same value in different datatypes.

c-programming-tutorial

Definition: struct

In both C and Python a struct, or structure, is simply a group of variables of homogenous or heterogeneous datatypes which can be accessed using dot notation (e.g. group.var, indoctrinator.victim, ford.mustang, etc).

 

Examples

In C:


struct conspiracy
 {
 int corrupt_police
 int corrupt_medics
};

In Python:


class conspiracy(Structure):
    _fields_ = [
 ("corrupt_police", c_int),
 ("corrupt_medics", c_int),
]

 

 

 

python-logo

C-Python-ctypes Data Type Mapping Table

data type map c python ctypes

Quick-Tip: 3 Ways to Load Ctypes in Python

  1. cdll()
  2. windll()
  3. oledll()

python-300x187

cdecl and stdcall Calling Conventions, Stack Clearing and the EAX Register

Two key calling conventions are:

1. cdecl
2. stdcall

In cdecl parameters are pushed from right to left and the caller of the function is responsible for clearing the arguments from the stack. Used by most C systems on the x86 architecture.
Example of a cdecl call in C:
 int python_rocks(reason_one, reason_two, reason_three); 
In x86 Assembly:

push reason_three
push reason_two
push reason_one
call python_rocks
add esp, 12

The last line above increments the stack pointer 12 bytes (there are 3 parameters to the function and each stack parameter is 4 bytes and thus 12 bytes) which essentially clears those parameters.
cdecl
Example of a stdcall call in C:

int my_socks(color_one, color_two, color_three);

In x86 Assembly:

push color_three
push color_two
push color_one
call my_socks
The order of the parameters in stdcall is the same but the stack clearing is not done by the caller, but by the my_socks function.
For both stdcall and cdecl calling conventions it’s important to note that return values are stored in the EAX register.

Python: Using a List of Lists in a Function

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for lst in lists:
    for numbers in lst:
results.append(numbers)
return results
print flatten(n)
WordPress keeps trying to remove my indentations, which is a pretty huge problem. Lines 3 and 4 should be more indented than 1 an 2. Line 5 has an additional indentation and line 6 has one more . Line 7 is less indented by 2 and line 8 returns to the original level of indendation.

Python: How to Merge/Join 2 Lists

Just add them…….

m = [1, 2, 3]
n = [4, 5, 6]

def join_lists(x, y):
return x+y

print join_lists(m, n)
# You want this to print [1, 2, 3, 4, 5, 6]

Python: use ”.join(list) to concatenate a list of strings

 n = [“Michael”, “Lieberman”]
# Add your function here
def join_strings(words):
result = “”
for  i in words:
result = ”.join(words)
print result
return result

‘ ‘.join(list) if you want spaces

Python: List appends and other list functions

list.append(x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].

list.extend(L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.

list.insert(ix)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, anda.insert(len(a), x) is equivalent to a.append(x).

list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.

list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

list.index(x)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.

list.count(x)
Return the number of times x appears in the list.

list.sort()
Sort the items of the list, in place.

list.reverse()
Reverse the elements of the list, in place.

An example that uses most of the list methods:

>>>

>>> a = [66.25, 333, 333, 1, 1234.5]>>> print a.count(333), a.count(66.25), a.count('x')2 1 0>>> a.insert(2, -1)>>> a.append(333)>>> a[66.25, 333, -1, 333, 1, 1234.5, 333]>>> a.index(333)1>>> a.remove(333)>>> a[66.25, -1, 333, 1, 1234.5, 333]>>> a.reverse()>>> a[333, 1234.5, 1, 333, -1, 66.25]>>> a.sort()>>> a[-1, 1, 66.25, 333, 333, 1234.5]

Python: Add each element of a list

n = [3, 5, 7]

def total(numbers):
result = 0
for i in range(len(numbers)):
for i in numbers:
result = sum(numbers)
print result
return result