Skip to content

Instantly share code, notes, and snippets.

@dolang
Last active July 6, 2018 12:19
Show Gist options
  • Save dolang/87b37438475e224bfb2ee2dc270fa44e to your computer and use it in GitHub Desktop.
Save dolang/87b37438475e224bfb2ee2dc270fa44e to your computer and use it in GitHub Desktop.
Cython oddity
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from libc.stdint cimport uint8_t # note: it's *cimport* not *import*
from random import randint
cpdef char* random_chars(int length=100):
cdef:
char[100] r_chars
uint8_t a_char
if length > 100:
length = 100
print("Warning: max supported length is 100")
for i in range(length):
a_char = randint(1, 255) # don't want any \x00 bytes here
r_chars[i] = a_char
return r_chars
"""
Main module. Requires py3.5+ and Cython
"""
import pyximport; pyximport.install()
import coding_test
def main():
r_chars = coding_test.random_chars(15) # strange: play around with the length and go below 14
print('as string:')
print('len:', len(r_chars), ' chars:', r_chars)
print('as hex:')
print('len:', len(r_chars.hex()), ' chars:', r_chars.hex())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment