- Timestamp:
- 02/19/21 03:14:41 (4 years ago)
- Location:
- framspy
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/FramsticksCLI.py
r1079 r1087 19 19 20 20 You need to provide one or two parameters when you run this class: the path to Framsticks CLI 21 and the name of the Framsticks CLI executable (if it is non-standard). See::21 and, optionally, the name of the Framsticks CLI executable (if it is non-standard). See:: 22 22 FramsticksCLI.py -h""" 23 23 -
framspy/FramsticksLib.py
r1084 r1087 8 8 9 9 class FramsticksLib: 10 """Communicates directly with Framsticks DLL/SO.10 """Communicates directly with Framsticks library (.dll or .so). 11 11 You can perform basic operations like mutation, crossover, and evaluation of genotypes. 12 12 This way you can perform evolution controlled by python as well as access and manipulate genotypes. … … 14 14 or access and control the simulation and simulated creatures step by step. 15 15 16 You need to provide one or two parameters when you run this class: the path to Framsticks CLIwhere .dll/.so resides17 and the name of the Framsticks dll/so (if it is non-standard). See::16 You need to provide one or two parameters when you run this class: the path to Framsticks where .dll/.so resides 17 and, optionally, the name of the Framsticks dll/so (if it is non-standard). See:: 18 18 FramsticksLib.py -h""" 19 19 … … 26 26 27 27 def __init__(self, frams_path, frams_lib_name): 28 frams.init(frams_path, frams_lib_name, "-Ddata") # "-D"+os.path.join(frams_path,"data")) # not possible (maybe python windows issue) because of the need for os.chdir(). So we assume "data" is where the dll/so is 28 if frams_lib_name is None: 29 frams.init(frams_path) # could add support for setting alternative directories using -D and -d 30 else: 31 frams.init(frams_path, "-L" + frams_lib_name) # could add support for setting alternative directories using -D and -d 29 32 30 33 print('Available objects:', dir(frams)) -
framspy/frams-test.py
r1085 r1087 2 2 import frams 3 3 4 frams.init(*(sys.argv[1:])) #pass whatever args we have, init() is the right place to deal with different scenrios:4 frams.init(*(sys.argv[1:])) # pass whatever args we have, init() is the right place to deal with different scenarios: 5 5 # frams.init() - should try to figure out everything (and might fail) 6 # frams.init('path/to/frams') - load the library from the specified dir and configure framsticks path as "data" inside the dir 7 # frams.init('path/to/frams','-d/tmp/workdir/data') - as above, but set the working (writable) directory somewhere else 6 # frams.init('path/to/lib') - load the library from the specified directory and configure Framsticks path as "data" inside this directory 7 # frams.init('path/to/lib','-d/tmp/workdir/data') - as above, but set the working (writable) directory somewhere else (see also -D) 8 # frams.init('path/to/lib','-Lframs-objects-alt.dll') - use specified library location and non-default file name 8 9 9 10 print('Available objects:', dir(frams)) -
framspy/frams.py
r1085 r1087 23 23 24 24 _reInsideParens = re.compile('\((.*)\)') 25 _reservedWords = ['import'] # this list is scanned during every attribute access, only add what is really clashing with framsticks properties25 _reservedWords = ['import'] # this list is scanned during every attribute access, only add what is really clashing with Framsticks properties 26 26 _reservedXWords = ['x' + word for word in _reservedWords] 27 27 _encoding = 'utf-8' … … 252 252 Initializes the connection to Framsticks dll/so. 253 253 254 Python programs do not have to know the framstics path but if they know, just pass the path as the first argument.255 Similarly '-dPATH' and '-DPATH' needed by framsticks are optional and derived from the first path, unless they are specified as args in init().256 '- lNAME' is the optional library name (full name including the file name extension), default is 'frams-objects.dll/.so' depending on the platform.257 All other arguments are passed to framsticks and not interpreted by this function.254 Python programs do not have to know the Framstics path but if they know, just pass the path as the first argument. 255 Similarly '-dPATH' and '-DPATH' needed by Framsticks are optional and derived from the first path, unless they are specified as args in init(). 256 '-LNAME' is the optional library name (full name including the file name extension), default is 'frams-objects.dll/.so' depending on the platform. 257 All other arguments are passed to Framsticks and not interpreted by this function. 258 258 259 259 """ 260 260 # goals: 261 frams_d =None262 frams_D =None263 lib_path =None264 lib_name ='frams-objects.so' if os.name == 'posix' else 'frams-objects.dll'265 initargs =[]261 frams_d = None 262 frams_D = None 263 lib_path = None 264 lib_name = 'frams-objects.so' if os.name == 'posix' else 'frams-objects.dll' 265 initargs = [] 266 266 for a in args: 267 if a[:2] =='-d':268 frams_d =a269 elif a[:2] =='-D':270 frams_D =a271 elif a[:2] =='-l':272 lib_name =a[2:]273 elif lib_path ==None:274 lib_path =a267 if a[:2] == '-d': 268 frams_d = a 269 elif a[:2] == '-D': 270 frams_D = a 271 elif a[:2] == '-L': 272 lib_name = a[2:] 273 elif lib_path is None: 274 lib_path = a 275 275 else: 276 276 initargs.append(a) 277 if lib_path ==None:278 # todo: use envvariable and/or the zip distribution we are in when the path is not specified in arg279 # for now just assume the current dir is framsticks280 lib_path ='.'277 if lib_path is None: 278 # TODO: use environment variable and/or the zip distribution we are in when the path is not specified in arg 279 # for now just assume the current dir is Framsticks 280 lib_path = '.' 281 281 282 282 original_dir = os.getcwd() 283 os.chdir(lib_path) # because under Windows, frams .dll requires other dll's which reside in the same directory, so we must change current dir for them to be found while loading the main dll284 abs_data =os.path.abspath('data') #use absolute path for -d and -D so python is free to cd anywhere without confusing the frams283 os.chdir(lib_path) # because under Windows, frams-objects.dll requires other dll's which reside in the same directory, so we must change current dir for them to be found while loading the main dll 284 abs_data = os.path.abspath('data') # use absolute path for -d and -D so python is free to cd anywhere without confusing Framsticks 285 285 # for the hypothetical case without lib_path the abs_data must be obtained from somewhere else 286 if frams_d ==None:287 frams_d ='-d'+abs_data288 if frams_D ==None:289 frams_D ='-D'+abs_data290 initargs.insert(0, frams_d)291 initargs.insert(0, frams_D)292 initargs.insert(0, 'dummy.exe')293 286 if frams_d is None: 287 frams_d = '-d' + abs_data 288 if frams_D is None: 289 frams_D = '-D' + abs_data 290 initargs.insert(0, frams_d) 291 initargs.insert(0, frams_D) 292 initargs.insert(0, 'dummy.exe') # as an offset, 0th arg is by convention app name 293 294 294 global c_api # access global variable 295 if lib_path !=None and os.name == 'posix':296 lib_name = './' +lib_name #currently we always have lib_path (even if it is incorrect) but hypothetically it could work with lib_path==None and load .so from some default system path without './'295 if lib_path is not None and os.name == 'posix': 296 lib_name = './' + lib_name # currently we always have lib_path (even if it is incorrect) but hypothetically it could work with lib_path==None and load .so from some default system path without './' 297 297 c_api = ctypes.CDLL(lib_name) 298 298 os.chdir(original_dir) 299 299 300 300 c_api.init.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] 301 301 c_api.init.restype = None
Note: See TracChangeset
for help on using the changeset viewer.