Skip to content

Instantly share code, notes, and snippets.

View heykarimoff's full-sized avatar
🎯
Focusing

Mukhammad Karimov heykarimoff

🎯
Focusing
View GitHub Profile
@heykarimoff
heykarimoff / recover_bucket.py
Created October 24, 2019 04:52
How to Recover Deleted Files in AWS S3 Bucket
#!/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 November 17, 2018 09:58
Python context manager examples
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 May 20, 2022 19:56
django model filefield random upload 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)
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
"""
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);