Sorry, I know this may be an odd questions, but I made this basic JavaScript program, and I'm not exactly sure what I should look up to know how to translate it to a python program with the same functionality. I know how the code works, but I don't know what the technical term for it is so I am struggling to find out how to do it in python. Any help would be greatly appreciated. Thanks!
var myArray = [];
var myCache = {
add: function a(x){
myArray.shift();
myArray.push(x);
},
access: function b(z){
var zLocation = myArray.indexOf(z);
var temp1 = myArray.slice(0,zLocation);
var temp2 = myArray.slice(zLocation+1, myArray.length);
temp1 = temp1.concat(temp2);
temp1.push(z);
myArray = temp1;
},
print: function c(){
console.log("Current array is: " + myArray);
},
size: function d(y){
yArray.length = y;
}
};
myCache.add(7);
I don't know how to add the add, access, print, and size functionality to something I create in Python. Thanks!
2 Answers 2
As python is an object oriented language, the way to get an object is basically to create a class which acts as the blueprint/prototype for different instantiations of that object. So your code translated to python could look more or less like this:
(all of you python pros, please forgive me, I'm not =D)
class MyCache:
def __init__(self, *args, **kwargs):
# in python the most similar to a javascript array is a list
# to make it a bit more readable `myArray` is called `_cache` here
self._cache = []
def add(self, x):
# do not try to pop() from an empty list, will throw an error
if self._cache:
self._cache.pop(0)
self._cache.append(x)
def access(self, z):
# index() is a bit whimpy in python...
try:
i = self._cache.index(z);
# I think we don't need the .slice() stuff here,
# can just .pop() at the index in python
self._cache.pop(i)
self._cache.append(z)
except ValueError as err:
print(err)
def size(self, y):
# not sure what you want to do here, initialize the cache with
# an array/list of a specific length?
self._cache = [None] * y
# print is a reserved word in python...
def status(self):
print("Current array is: ")
print(self._cache)
# instantiate the class (e.g. get a cache object)
# and do some stuff with it..
cache = MyCache()
cache.add(7)
cache.status()
cache.add(10)
cache.status()
cache.add(3)
cache.status()
cache.access(3)
cache.status()
not sure if this is actually doing what you're expecting it to do, you'll always have only 1 value in the cache because the add() method will always remove one...so the access method kind of doesn't make sense...but maybe it was just your simplified example code?
1 Comment
Just use Js2PY. It's written in Python so of course you'd have to have Python but other than that your good. If you want to go the long way then learn Python which would be better because once you have your code in Python you know how to change it.
3 Comments
js2py.translate_file('example.js', 'example.py')
myCache, an object, with functions as values. You can make a class in python with methods that do this. Read about classes and methods and it should become clear how to do this.