Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Do the whole thing in one commit XD
Browse files Browse the repository at this point in the history
  • Loading branch information
JavaCode7 committed Mar 10, 2021
1 parent c0bacca commit dd00da6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,47 @@
# pyPtr
Pointers for Python

## Usage
To create a new `Pointer`:
```python
import ptr

num: int = 10
num_pointer: ptr.Pointer = ptr.Pointer(num)
```

To get the address and value that the pointer points to:
```python
num_pointer_address: int = num_pointer.getAddress()
num_pointer_value: int = num_pointer.getVal()
```

To perform calculations:
```python
# += and -=
num_pointer.increment()
num_pointer_value_after_plus_equals: int = num_pointer.getVal()

num_pointer.decrement()
num_pointer_value_after_minus_equals: int = num_pointer.getVal()


# + and -
num_pointer.add(10)
num_pointer_value_after_plus: int = num_pointer.getVal()

num_pointer.sub(15)
num_pointer_value_after_minus: int = num_pointer.getVal()


# ==, !=, <, >, <= and >=
other: int = 15
other_pointer: ptr.Pointer = ptr.Pointer(other)

are_addresses_equal: bool = num_pointer.eqeq(other_pointer)
are_addresses_equal: bool = num_pointer.ne(other_pointer)
are_addresses_equal: bool = num_pointer.gt(other_pointer)
are_addresses_equal: bool = num_pointer.lt(other_pointer)
are_addresses_equal: bool = num_pointer.lte(other_pointer)
are_addresses_equal: bool = num_pointer.gte(other_pointer)
```
47 changes: 47 additions & 0 deletions ptr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import sys
import ctypes

class Pointer:
def __init__(self, var: any):
self.pointsToVal = var
self.pointsToAddress = id(self.pointsToVal)

def getAddress(self):
return self.pointsToAddress

def getVal(self):
return self.pointsToVal

def increment(self):
self.pointsToAddress += sys.getsizeof(self.pointsToVal)
self.pointsToVal = ctypes.cast(self.pointsToAddress, ctypes.py_object).value

def decrement(self):
self.pointsToAddress -= sys.getsizeof(self.pointsToVal)
self.pointsToVal = ctypes.cast(self.pointsToAddress, ctypes.py_object).value

def add(self, val: int):
self.pointsToAddress += val * sys.getsizeof(self.pointsToVal)
self.pointsToVal = ctypes.cast(self.pointsToAddress, ctypes.py_object).value

def sub(self, val: int):
self.pointsToAddress -= val * sys.getsizeof(self.pointsToVal)
self.pointsToVal = ctypes.cast(self.pointsToAddress, ctypes.py_object).value

def eqeq(self, other):
return self.pointsToAddress == other.pointsToAddress

def ne(self, other):
return self.pointsToAddress != other.pointsToAddress

def gt(self, other):
return self.pointsToAddress > other.pointsToAddress

def lt(self, other):
return self.pointsToAddress < other.pointsToAddress

def lte(self, other):
return self.pointsToAddress <= other.pointsToAddress

def gte(self, other):
return self.pointsToAddress >= other.pointsToAddress

0 comments on commit dd00da6

Please sign in to comment.