Skip to content

Instantly share code, notes, and snippets.

View akkefa's full-sized avatar
💻
Grit

Ikram Ali akkefa

💻
Grit
View GitHub Profile
@akkefa
akkefa / binary_tensor.py
Created December 20, 2022 19:20
Threshold a tensor into binary values using pytorch (torch.where)
import torch
# Create a tensor with values between 0 and 1
tensor = torch.rand(3, 3)
# Set a threshold value
threshold = 0.5
# Use torch.where to threshold the tensor
binary_tensor = torch.where(tensor > threshold, torch.tensor(1), torch.tensor(0))
@akkefa
akkefa / gist:64ddc3fc8d74406f814ca348be4a8725
Created December 20, 2022 07:04
Pytorch Dataloader Example
import torch
from torch.utils.data import DataLoader, TensorDataset
# Define the dataset
X = torch.Tensor([[1, 2], [3, 4], [5, 6], [7, 8]])
y = torch.Tensor([0, 1, 0, 1])
dataset = TensorDataset(X, y)
# Create the dataloader
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
@akkefa
akkefa / Automate your Python project with Makefile
Created June 17, 2022 18:17
Automate your Python project with Makefile
coverage: ## Run tests with coverage
coverage erase
coverage run --include=podsearch/* -m pytest -ra
coverage report -m
deps: ## Install dependencies
pip install black coverage flake8 mypy pylint pytest tox
lint: ## Lint and static-check
flake8 podsearch
@akkefa
akkefa / Linux disk Management commands
Last active April 29, 2022 12:30
All commands related to disk management
## Check space on specfic directory
du -h --max-depth=0 /u/iali
du -h --max-depth=1 /u/iali
@akkefa
akkefa / tmux-cheatsheet.markdown
Created January 30, 2019 13:10 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@akkefa
akkefa / python Rot13 cipher
Created June 10, 2018 16:00
Rot13 is a cipher: it rotates characters forward 13 places. This makes text unreadable. But as a cipher, it is reversible by applying the translation a second time. We write rot13 with maketrans and translate.
# Create translation table.
trans = str.maketrans("abcdefghijklmnopqrstuvwxyz",
"nopqrstuvwxyzabcdefghijklm")
# Apply rot13 translation.
print("gandalf".translate(trans))
print("gandalf".translate(trans).translate(trans))
@akkefa
akkefa / pytest.sh
Created May 23, 2018 10:03 — forked from amatellanes/pytest.sh
Useful py.test commands.
py.test test_sample.py --collect-only # collects information test suite
py.test test_sample.py -v # outputs verbose messages
py.test -q test_sample.py # omit filename output
python -m pytest -q test_sample.py # calling pytest through python
py.test --markers # show available markers
@akkefa
akkefa / scp file tranfer
Created May 10, 2018 11:46
Transferring files over SSH
# copy from local machine to remote machine
scp localfile user@host:/path/to/whereyouwant/thefile
# copy from remote machine to local machine
scp user@host:/path/to/remotefile localfile
@akkefa
akkefa / Python unicode Handling
Last active April 21, 2018 10:03
Python unicode Handling
# Encode: unicode code point to bytes
>>> s = u'Café'
>>> type(s.encode('utf-8'))
<class 'bytes'>
Decode: bytes to unicode code point
>>> s = bytes('Café', encoding='utf-8')
>>> s.decode('utf-8')
'Café'
Get unicode code point
@akkefa
akkefa / json-utf8.py
Created April 10, 2018 06:44
Python 3: Save unicode(Urdu) string to file not using '\uXXXX' but using UTF-8.
# -*- coding: utf-8 -*-
import json
from codecs import open
o = { 'text': 'پنجاب' }
with open('foo.json', 'w', encoding= 'utf-8') as fp:
json.dump(o, fp, ensure_ascii= False)