Skip to content

Commit

Permalink
Merge pull request #19 from benmaier/dev
Browse files Browse the repository at this point in the history
Fix Python distributions on travis, header-less csv files, and increase the version number
  • Loading branch information
benmaier authored May 19, 2021
2 parents c85325a + e759a35 commit d5bd6e0
Show file tree
Hide file tree
Showing 22 changed files with 411 additions and 998 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: python
dist: trusty
dist: bionic

script:
- python setup.py test
Expand All @@ -18,7 +18,8 @@ branches:

python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6-dev"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
31 changes: 15 additions & 16 deletions binpacking/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,41 @@

def load_csv(filepath,weight_column,has_header=False,delim=',',quotechar='"'):

weight_col_is_str = isinstance(weight_column,basestring)

if weight_col_is_str and not has_header:
raise Exception("weight key "+weight_column+" useless, since given csv has no header")

with open(filepath) as csvfile:
reader = csv.reader(csvfile, delimiter=delim, quotechar=quotechar)

data = []
row_count = 0
header = None

for row in reader:

if row_count>0:
row[weight_column] = float(row[weight_column])
data.append(row)
elif has_header:
if has_header and row_count == 0:
header = row
if isinstance(weight_column,basestring):
if has_header:
if weight_column in header:
weight_column = header.index(weight_column)
else:
raise Exception("weight key "+weight_column+" not found in header")
if weight_column in header:
weight_column = header.index(weight_column)
else:
raise Exception("weight key "+weight_column+" not found in header")
else:
if isinstance(weight_column,basestring):
raise Exception("weight key "+weight_column+" useless, since given csv has no header")
row[weight_column] = float(row[weight_column])
data.append(row)

row_count += 1


return data,weight_column,header
return data, weight_column, header


def print_binsizes(bins,weight_column):
print("=== distributed items to bins with sizes ===")
formatstr = "%0" + str(len(str(len(bins)-1))) + "d"
for ib,b in enumerate(bins):
print(formatstr % ib, sum([t[weight_column] for t in b]))


def save_csvs(bins,filepath,header,delim=',',quotechar='"'):
filename, file_extension = os.path.splitext(filepath)
Expand All @@ -52,7 +51,7 @@ def save_csvs(bins,filepath,header,delim=',',quotechar='"'):

for ib,b in enumerate(bins):
current_path = filename + "_" + formatstr % ib + file_extension
with open(current_path,"w") as csvfile:
with open(current_path,"w") as csvfile:
writer = csv.writer(csvfile, delimiter=delim, quotechar=quotechar, quoting=csv.QUOTE_MINIMAL)
if header is not None:
writer.writerow(header)
Expand Down
102 changes: 5 additions & 97 deletions examples/hamlet_word_count_00.csv
Original file line number Diff line number Diff line change
@@ -1,97 +1,5 @@
the,1206.0
we,151.0
from,107.0
let,92.0
would,78.0
give,65.0
an,59.0
one,55.0
ros,53.0
horatio,43.0
take,38.0
polonius,35.0
within,32.0
nothing,30.0
been,28.0
away,26.0
comes,24.0
about,22.0
done,21.0
means,21.0
keep,19.0
form,18.0
once,17.0
is't,16.0
thoughts,16.0
etexts,15.0
grief,14.0
welcome,14.0
duty,13.0
grace,13.0
awhile,12.0
thousand,12.0
neither,11.0
through,11.0
villain,11.0
full,11.0
passion,11.0
let's,10.0
arms,9.0
behind,9.0
falls,9.0
aside,9.0
shows,9.0
understand,8.0
says,8.0
wrong,8.0
hope,8.0
return,8.0
brother's,7.0
beseech,7.0
shot,7.0
loves,7.0
fool,7.0
sorrow,6.0
cornelius,6.0
ambition,6.0
double,6.0
cold,6.0
gifts,6.0
danish,6.0
ii,6.0
struck,6.0
stir,5.0
statement,5.0
souls,5.0
among,5.0
man's,5.0
present,5.0
liberty,5.0
blessing,5.0
marriage,5.0
flesh,5.0
burial,5.0
prologue,5.0
safety,5.0
flourish,5.0
servant,5.0
request,4.0
breathe,4.0
favour,4.0
forget,4.0
reach,4.0
hecuba,4.0
hence,4.0
twice,4.0
legal,4.0
rose,4.0
b',4.0
didst,4.0
past,4.0
possible,4.0
certain,4.0
william,4.0
lives,4.0
hundred,4.0
hall,4.0
ye,4.0
word,count
to,797.0
of,732.0
in,444.0
pray,27.0
103 changes: 5 additions & 98 deletions examples/hamlet_word_count_01.csv
Original file line number Diff line number Diff line change
@@ -1,98 +1,5 @@
and,1005.0
he,212.0
are,141.0
o,104.0
at,97.0
there,74.0
enter,66.0
should,59.0
am,55.0
man,51.0
had,44.0
own,39.0
guil,35.0
mine,32.0
again,30.0
pray,27.0
any,26.0
nay,25.0
call,22.0
players,21.0
son,21.0
hand,19.0
thing,18.0
ear,17.0
question,16.0
majesty,16.0
between,15.0
o'er,14.0
another,14.0
spirit,13.0
gentleman,13.0
hands,12.0
second,12.0
men,11.0
name,11.0
takes,11.0
fire,11.0
fine,10.0
peace,10.0
doubt,10.0
fall,9.0
strange,9.0
note,9.0
offence,9.0
back,8.0
wife,8.0
to't,8.0
shame,8.0
sit,8.0
public,7.0
already,7.0
woman,7.0
next,7.0
husband,7.0
reynaldo,6.0
information,6.0
re-enter,6.0
feed,6.0
wouldst,6.0
dare,6.0
beauty,6.0
prince,6.0
confess,6.0
house,5.0
sudden,5.0
slain,5.0
withal,5.0
'gainst,5.0
perhaps,5.0
property,5.0
author,5.0
cry,5.0
mass,5.0
player,5.0
damages,5.0
university,5.0
received,5.0
drown'd,5.0
expense,4.0
remembrance,4.0
poison'd,4.0
armed,4.0
spade,4.0
turn'd,4.0
wild,4.0
habit,4.0
begun,4.0
fort,4.0
honour'd,4.0
length,4.0
four,4.0
so',4.0
forgot,4.0
wi',4.0
sorry,4.0
flowers,4.0
lights,4.0
unnatural,4.0
word,count
you,592.0
a,563.0
i,541.0
his,297.0
106 changes: 7 additions & 99 deletions examples/hamlet_word_count_02.csv
Original file line number Diff line number Diff line change
@@ -1,99 +1,7 @@
to,797.0
for,269.0
so,197.0
if,131.0
hamlet,102.0
pol,86.0
well,71.0
very,65.0
i'll,56.0
say,53.0
see,48.0
can,39.0
nor,38.0
etext,34.0
night,31.0
both,29.0
sweet,26.0
great,26.0
set,24.0
i',22.0
ber,21.0
words,20.0
use,20.0
little,18.0
please,17.0
youth,16.0
last,16.0
live,15.0
mark,14.0
work,14.0
kind,13.0
we'll,13.0
help,12.0
drink,12.0
revenge,11.0
he's,11.0
bed,11.0
fortune,11.0
deed,10.0
aught,10.0
phrase,10.0
swear,9.0
walk,9.0
bernardo,9.0
letters,9.0
fran,8.0
list,8.0
power,8.0
law,8.0
looks,8.0
needs,7.0
rank,7.0
dies,7.0
service,7.0
water,6.0
fault,6.0
others,6.0
shape,6.0
choose,6.0
health,6.0
copy,6.0
buried,6.0
hot,6.0
bad,6.0
heavy,6.0
sing,5.0
knew,5.0
france,5.0
ten,5.0
slave,5.0
patience,5.0
bodies,5.0
spirits,5.0
fortune's,5.0
million,5.0
wit,5.0
beard,5.0
terms,5.0
brains,5.0
freely,4.0
pate,4.0
tale,4.0
pleasure,4.0
mock,4.0
fast,4.0
cries,4.0
wear,4.0
motion,4.0
stars,4.0
stronger,4.0
modesty,4.0
fingers,4.0
sigh,4.0
thrice,4.0
thence,4.0
reads,4.0
months,4.0
directly,4.0
ass,4.0
word,count
my,513.0
it,428.0
that,391.0
is,359.0
with,274.0
ay,35.0
Loading

0 comments on commit d5bd6e0

Please sign in to comment.