This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JavaCode7
committed
Mar 10, 2021
1 parent
c0bacca
commit dd00da6
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |