text
stringlengths
0
828
value = value[:-1]
i.is_secret = True
return (value, i)
return None"
1118,"def extract_csv(zip_path, destination):
""""""
Extract the first CSV file found in the given ``zip_path`` ZIP file to the
``destination`` file. Raises :class:`LookupError` if no CSV file can be
found in the ZIP.
""""""
with zipfile.ZipFile(zip_path) as zf:
member_to_unzip = None
for member in zf.namelist():
if member.endswith('.csv'):
member_to_unzip = member
break
if not member_to_unzip:
raise LookupError(
""Couldn't find any CSV file in the archive""
)
with zf.open(member_to_unzip) as zfp, \
open(destination, 'wb') as dfp:
dfp.write(zfp.read())"
1119,"def download(self, overwrite=True):
""""""
Download the zipcodes CSV file. If ``overwrite`` is set to False, the
file won't be downloaded if it already exists.
""""""
if overwrite or not os.path.exists(self.file_path):
_, f = tempfile.mkstemp()
try:
urlretrieve(self.DOWNLOAD_URL, f)
extract_csv(f, self.file_path)
finally:
os.remove(f)"
1120,"def get_locations(self):
""""""
Return the zipcodes mapping as a list of ``{zipcode: location}`` dicts.
The zipcodes file will be downloaded if necessary.
""""""
if not self.zipcode_mapping:
self.download(overwrite=False)
zipcode_mapping = {}
with UnicodeReader(self.file_path, delimiter=';', encoding='latin1') as csv_reader:
# Skip header
next(csv_reader)
for line in csv_reader:
zipcode_mapping[int(line[1])] = Location(
official_name=line[0],
canton=line[5],
municipality=line[3]
)
self.zipcode_mapping = zipcode_mapping
return self.zipcode_mapping"
1121,"def get_zipcodes_for_canton(self, canton):
""""""
Return the list of zipcodes for the given canton code.
""""""
zipcodes = [
zipcode for zipcode, location in self.get_locations().items()
if location.canton == canton
]
return zipcodes"
1122,"def get_cantons(self):
""""""
Return the list of unique cantons, sorted by name.
""""""
return sorted(list(set([
location.canton for location in self.get_locations().values()
])))"
1123,"def get_municipalities(self):
""""""
Return the list of unique municipalities, sorted by name.
""""""
return sorted(list(set([
location.municipality for location in self.get_locations().values()
])))"
1124,"def term_vector(self, params):
'''
params are either True/False, 'with_offsets', 'with_positions', 'with_positions_offsets'
'''
if params == True:
self[self.field]['term_vector'] = 'yes'
elif params == False:
self[self.field]['term_vector'] = 'no'
else:
self[self.field]['term_vector'] = params
return self"
1125,"def _get_formula_class(self, formula):
""""""
get a formula class object if it exists, else
create one, add it to the dict, and pass return it.
""""""
# recursive import otherwise
from sprinter.formula.base import FormulaBase