Skip to content

Instantly share code, notes, and snippets.

View ftfarias's full-sized avatar
🎯
Focusing

Felipe Farias ftfarias

🎯
Focusing
  • Data Lead at Alice
  • São Paulo, Brazil
View GitHub Profile
import re
DOUBLE_SPACES_REMOVER = re.compile(r'[ ]+')
def remove_double_spaces_re(text):
return DOUBLE_SPACES_REMOVER.sub(' ',text)
%timeit remove_double_spaces_re('ads sadf scbvcxb ret h fdgh jj gh erty ')
4.3 µs ± 62.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
############################
@ftfarias
ftfarias / README.md
Created January 24, 2017 23:10
Circular imports in Python 2 and Python 3: when are they fatal? When do they work?

When are Python circular imports fatal?

In your Python package, you have:

  • an __init__.py that designates this as a Python package
  • a module_a.py, containing a function action_a() that references an attribute (like a function or variable) in module_b.py, and
  • a module_b.py, containing a function action_b() that references an attribute (like a function or variable) in module_a.py.

This situation can introduce a circular import error: module_a attempts to import module_b, but can't, because module_b needs to import module_a, which is in the process of being interpreted.

But, sometimes Python is magic, and code that looks like it should cause this circular import error works just fine!

import re
DATA = "Hey, you - what are you doing here!?"
print re.findall(r"[\w']+", DATA)
# Prints ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here']
>>> re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split('(\W+)', 'Words, words, words.')
# from tqdm import tqdm
import csv
with open('source.csv', 'r', encoding='utf-8', errors='replace') as input_file:
# protects from "null" bytes
input_file = (l.replace('\0' ,'') for l in input_file)
input_csv = csv.reader(input_file, delimiter=';', quotechar='"')
# remove header if necessary
header = next(input_csv)