Skip to content

Commit

Permalink
Fixed up tests and OrderedDiskDict.
Browse files Browse the repository at this point in the history
First test of Travis-CI integration includes every possible Python
allowed, to see how many I can target. Then I'll cut down to a
reasonable amount.
  • Loading branch information
cdusold committed Aug 18, 2017
1 parent 70f053b commit c87c7d1
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 46 deletions.
21 changes: 21 additions & 0 deletions .travis.yml
@@ -0,0 +1,21 @@
language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
- "3.4"
- "3.5"
- "3.5-dev" # 3.5 development branch
- "3.6"
- "3.6-dev" # 3.6 development branch
- "3.7-dev" # 3.7 development branch
- "nightly" # currently points to 3.7-dev
# PyPy versions
- "pypy" # PyPy2 2.5.0
- "pypy3" # Pypy3 2.4.0
- "pypy-5.3.1"
install:
- pip install .
- pip install -r requirements.txt
script: pytest
21 changes: 11 additions & 10 deletions pyspeedup/algorithms/Primes.py
Expand Up @@ -5,7 +5,10 @@
and uses concurrent branching to split primality tests and prime factorizations and
speed up the whole process.
"""
import cPickle
try:
import cPickle as pickle
except:
import pickle
from os.path import join
from threading import Thread
from time import sleep
Expand All @@ -20,8 +23,8 @@
global q
global thread
global file_location
D = DiskDict()
F = OrderedDiskDict()
D = DiskDict("seive")
F = OrderedDiskDict("factors")
c = 3
p = 3
q = 9
Expand All @@ -42,14 +45,12 @@ def load_primes(location):
global file_location
stop_seive()
del D
D = DiskDict() #New seive object.
D.link_to_disk("seive",file_location=location, size_limit = 65536, max_pages = 32) #Load or create persistance.
D = DiskDict("seive", file_location=location, size_limit = 65536, max_pages = 32) #New seive object.
del F
F = OrderedDiskDict() #A new factor list object.
F.link_to_disk("factors",file_location=location, size_limit = 65536, max_pages = 32) #Load or create persistance.
F = OrderedDiskDict("factors",file_location=location, size_limit = 65536, max_pages = 32) #A new factor list object.
try:
with open(D._file_base+"current",'rb') as f:
c,p = cPickle.load(f)
c,p = pickle.load(f)
except:
c,p = 3,3
file_location = location
Expand Down Expand Up @@ -107,7 +108,7 @@ def _prime_seive():
c += 2 #We skip all even numbers since 2 is hard coded.
sleep(0)
with open(D._file_base+"current",'wb') as f:
cPickle.dump((c,p), f)
pickle.dump((c,p), f)

def nextPrime(p):
"""
Expand Down Expand Up @@ -212,7 +213,7 @@ def get_factorization(q):
from os.path import expanduser
load_primes("D:/.pyspeedup")
start_seive()
while len(F)<1000000:
while len(F)<1000000:
sleep(1)
stop_seive()
print(c)
Expand Down
2 changes: 1 addition & 1 deletion pyspeedup/algorithms/__init__.py
Expand Up @@ -21,4 +21,4 @@ class which I intend to build.
from pyspeedup.algorithms._indexCalculus import rowReduce
from pyspeedup.algorithms._squares import isSquare
from pyspeedup.algorithms._factor import factor
import Primes as Primes
from pyspeedup.algorithms import Primes
7 changes: 5 additions & 2 deletions pyspeedup/memory/_diskdict.py
@@ -1,5 +1,8 @@
from collections import MutableMapping
import pickle
try:
import cPickle as pickle
except:
import pickle
from os.path import expanduser,join
from os import remove,makedirs
from glob import glob
Expand Down Expand Up @@ -235,7 +238,7 @@ def _load_page_from_disk(self,number):
def __str__(self):
return "Dictionary with values stored to "+self._file_base
def __repr__(self):
return "DiskDict().link_to_disk('',"+str(self.size_limit)+','+str(self.max_pages)+','+self._file_base+')'
return "DiskDict('',"+str(self.size_limit)+','+str(self.max_pages)+','+self._file_base+')'
def __contains__(self, item):
i,k = self._finditem(item)
return k in self.pages[i]
Expand Down
5 changes: 4 additions & 1 deletion pyspeedup/memory/_disklist.py
@@ -1,5 +1,8 @@
from collections import MutableSequence
import pickle
try:
import cPickle
except:
import pickle
from os.path import expanduser,join
from os import remove, makedirs
from glob import glob
Expand Down
60 changes: 29 additions & 31 deletions pyspeedup/memory/_ordereddiskdict.py
@@ -1,5 +1,8 @@
from collections import MutableMapping
import cPickle
try:
import cPickle as pickle
except:
import pickle
from os.path import expanduser,join
from os import remove,makedirs
from glob import glob
Expand All @@ -23,31 +26,28 @@ class OrderedDiskDict(MutableMapping):
This is accomplished through a rudimentary (for now) hashing scheme to page the
dictionary into parts.
"""
def __init__(self, *args, **kwargs):
self.pages=_page()
self.pages[0]=_page(*args, **kwargs)
self._length = len(self.pages[0])
self._total={0}
self._queue=[0]
self._file_base = None
def link_to_disk(self, file_basename, size_limit = 1024, max_pages = 16, file_location = join(expanduser("~"),"PySpeedup")):
if len(self)>0:
raise Exception("Linking to disk should happen before any data is written.")
if self._file_base:
raise Exception("Can't link to two file names or locations at the same time.")
try:
os.makedirs(file_location)
except:
pass
self._file_base = join(file_location,file_basename)
self.size_limit = size_limit
def __init__(self, file_basename, size_limit = 1024, max_pages = 16, file_location = join(expanduser("~"),"PySpeedup")):
if max_pages < 1:
raise ValueError("There must be allowed at least one page in RAM.")
self.max_pages = max_pages
if size_limit < 1:
raise ValueError("There must be allowed at least one item per page.")
self.size_limit = size_limit
if file_location:
try:
makedirs(file_location)
except OSError as e:
if e.errno != 17:
raise
pass
self._file_base = join(file_location,file_basename)
self.pages = _page()
self._length = 0
self._total = set()
self._queue = []
try:
with open(self._file_base+'Len', 'rb') as f:
self._length = cPickle.load(f)
self._total.remove(0)
self._queue=[]
del self.pages[0]
self.pages.currentDepth,self._length = pickle.load(f)
for f in glob(self._file_base+'*'):
try:
self._total.add(int(f[len(self._file_base):]))
Expand All @@ -56,7 +56,6 @@ def link_to_disk(self, file_basename, size_limit = 1024, max_pages = 16, file_lo
except:
pass
atexit.register(_exitgracefully,self)
return self
def _guarantee_page(self,k):
"""
Pulls up the page in question.
Expand Down Expand Up @@ -143,14 +142,14 @@ def __del__(self):
for key in self.pages.keys():
self._save_page_to_disk(key)
def _save_page_to_disk(self,number):
import cPickle
import pickle
with open(self._file_base+'Len', 'wb') as f:
cPickle.dump(self._length,f)
pickle.dump(self._length,f)
if self._file_base:
if number in self.pages:
if len(self.pages[number])>0:
with open(self._file_base+str(number),'wb') as f:
cPickle.dump(self.pages[number],f)
pickle.dump(self.pages[number],f)
else:
self._total.remove(number)
del self.pages[number]
Expand All @@ -161,7 +160,7 @@ def _save_page_to_disk(self,number):
def _load_page_from_disk(self,number):
if self._file_base:
with open(self._file_base+str(number),'rb') as f:
self.pages[number] = cPickle.load(f)
self.pages[number] = pickle.load(f)
self._queue.append(number)
remove(self._file_base+str(number))
def __str__(self):
Expand All @@ -178,12 +177,11 @@ def __exit__(self, exception_type, exception_val, trace):


if __name__ == '__main__':
d = DiskDict()
d.link_to_disk('testDiskDict',2,2)
d = DiskDict('testDiskDict',2,2)
for i in range(16):
d[i/10.]=i
print(d.pages)
d.max_pages=16
for i in range(16):
d[i/10.]=i
print(d.pages)
print(d.pages)
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -10,7 +10,7 @@ def read(fname):

setup(
name = "PySpeedup",
version = "0.1.2.2",
version = "0.1.3.0",
author = "Chris Dusold",
author_email = "PySpeedup@ChrisDusold.com",
description = ("A multiprocess framework for efficient calculations."),
Expand Down
5 changes: 5 additions & 0 deletions source/index.rst
Expand Up @@ -80,6 +80,11 @@ Disk Based Dictionary

.. autoclass:: pyspeedup.memory.DiskDict

Ordered Access Disk Based Dictionary
---------------------

.. autoclass:: pyspeedup.memory.OrderedDiskDict

Disk Based List
---------------

Expand Down
File renamed without changes.

0 comments on commit c87c7d1

Please sign in to comment.