Archive for the ‘Python’ Category

Arrays in ctypes

In ctypes an array is defined by multiplying a type by the number of elements you want allocated within the array, for example:

In Python
class marijuana_amount(Union):
    _fields_ = [
    ("weed_long", c_long),
    ("weed_int", c_int),
    ("weed_char", c_char * 8),
    ]

c_languagepython-logo

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: while loop

The while loop is similar to an ifstatement: it executes the code inside of it if some condition is true. The difference is that the while loop will continue to execute as long as the condition is true. In other words, instead of executing if something is true, it executes while that thing is true.

BEWARE OF INFINITE LOOPS!
 

count = 0

if count < 5:

print “Hello, I am an if statement and count is”, count

while count < 10:

print “Hello, I am a while and count is”, count

count += 1

Hopefully the indentation is displayed correctly in your view of this post. The lines under **if** and **while** should be indented.

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