Skip to content

Instantly share code, notes, and snippets.

@pydemo
pydemo / hive-honey.py
Created September 7, 2018 17:34
Extract data from Hive
from __future__ import with_statement
"""
set PROXY_HOST=your_bastion_host
set SERVICE_USER=your_func_user
set LINUX_USER=your_SOID
set LINUX_PWD=your_pwd
"""
import click
import paramiko
import time, sys, os
@pydemo
pydemo / pbrun.sh
Created September 7, 2018 17:35
pbrun automation
#!/usr/bin/expect -f
set timeout 300
set usr [lindex $argv 0];
set pwd [lindex $argv 1];
set query_file [lindex $argv 2];
spawn -noecho pbrun $usr &
expect -re "Password:"
send "$pwd\r"
sleep 1
@pydemo
pydemo / pyqsort.py
Created September 7, 2018 19:43
Cython quicksort
%%cython
cdef extern from "stdlib.h":
void *malloc(size_t size)
void free(void *ptr)
void qsort(void *array, size_t count, size_t size, int (*compare)(const void *, const void *))
cdef int int_compare(const void *a, const void *b):
cdef int ia, ib
@pydemo
pydemo / dynamic_array_cython.py
Last active July 3, 2022 21:45
Dynamic array allocation in Cython
from libc.stdlib cimport malloc
def dynamic(size_t N, size_t M):
cdef long *arr = <long*>malloc(N * M * sizeof(long))
#Using typed memoryview cast
def dynamic(size_t N, size_t M):
cdef long *arr = <long*>malloc(N * M * sizeof(long))
@pydemo
pydemo / arr_base.py
Created September 11, 2018 17:46
cleanup of C arrays in Cython
cdef class _finalizer:
cdef void *_data
def __dealloc__(self):
print "_finalizer.__dealloc__"
if self._data is not NULL:
free(self._data)
cdef void set_base(cnp.ndarray arr, void *carr):
cdef _finalizer f = _finalizer()
@pydemo
pydemo / SIMD-cython.md
Created September 11, 2018 19:04
SIMD-enabled functions in Cython

The fastest C implementation makes use of SIMD intrinsics to parallelize the core computation. There is nothing preventing us from accessing the same operations from our Cython code as well. Doing so requires that we declare the platform-specific SIMD-enabled functions to Cython and integrate them into the spectral_norm.pyx code.

@pydemo
pydemo / cython-memoryviews.py
Created September 11, 2018 20:17
cython-memoryviews example
import numpy as np
cimport numpy as np
cimport cython
from libc.stdio cimport FILE, fopen, fwrite, fscanf, fclose, fprintf, fseek, ftell, SEEK_END, rewind, fread
from numpy cimport float64_t
from libc.stdlib cimport malloc, free
cdef extern from "math.h":
double exp(double) nogil
double log(double) nogil
@pydemo
pydemo / cython-readfile.py
Created September 11, 2018 20:25
read file in Cython
from libc.stdio cimport *
cdef extern from "stdio.h":
#FILE * fopen ( const char * filename, const char * mode )
FILE *fopen(const char *, const char *)
#int fclose ( FILE * stream )
int fclose(FILE *)
#ssize_t getline(char **lineptr, size_t *n, FILE *stream);
ssize_t getline(char **, size_t *, FILE *)
@pydemo
pydemo / fprintf.py
Created September 11, 2018 20:34
fprintf example in Cython
%load_ext cython
%%cython
from libc.stdio cimport FILE, stdout, fprintf
fprintf(stdout, "%s\n", <char *>"Hello, world!")
%%cython
from libc.stdio cimport FILE, stdout, fprintf
fprintf(stdout, "%d\n", <int>123)
@pydemo
pydemo / openmp.py
Last active September 12, 2018 14:54
Parallel OpenMP Cython example
# distutils: extra_compile_args = -openmp
# distutils: extra_link_args = -openmp
from cython cimport boundscheck, wraparound
from cython.parallel cimport prange, threadid
from libc.stdio cimport stdout, fprintf
import numpy as np
cdef inline double norm2(double complex z) nogil: