Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Muhammad Karim heykarimoff

🎯
Focusing
View GitHub Profile
@heykarimoff
heykarimoff / recover_bucket.py
Created Oct 24, 2019
How to Recover Deleted Files in AWS S3 Bucket
View recover_bucket.py
#!/usr/bin/env python
from datetime import datetime, timezone
import boto3
# ######################################
#
# Empty Bucket of all delete markers from all objects.
#
# ######################################
@heykarimoff
heykarimoff / context_manager.py
Created Nov 17, 2018
Python context manager examples
View context_manager.py
import json
from contextlib import contextmanager
data = {'key': 1234}
with open('the_json_file.json', 'wt') as json_file:
json.dump(data, json_file)
with OpenFile('the_json_file.json', 'wt') as json_file:
json.dump(data, json_file)
@heykarimoff
heykarimoff / random_filefield_path
Last active Feb 18, 2018
django model filefield random upload path
View random_filefield_path
# helpers.py
@deconstructible
class RandomFileName(object):
def __init__(self, path):
self.path = os.path.join(path, "%s%s")
def __call__(self, _, filename):
# @note It's up to the validators to check if it's the correct file type in name or if one even exist.
extension = os.path.splitext(filename)[1]
return self.path % (uuid.uuid4(), extension)
View Django Ignore user language preference
class ForceDefaultLanguageMiddleware(object):
"""
Ignore Accept-Language HTTP headers
This will force the I18N machinery to always choose settings.LANGUAGE_CODE
as the default initial language, unless another one is set via sessions or cookies
Should be installed *before* any middleware that checks request.META['HTTP_ACCEPT_LANGUAGE'],
namely django.middleware.locale.LocaleMiddleware
"""
View soket.io in cunjunction with express
var express = require("express");
var http = require("http");
var socket = require("socket.io");
// express app
app = express();
// http server based on express app
server = http.createServer(app);
io = socket(server);
You can’t perform that action at this time.