Changeset 1087


Ignore:
Timestamp:
02/19/21 03:14:41 (3 years ago)
Author:
Maciej Komosinski
Message:

Cosmetic

Location:
framspy
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • framspy/FramsticksCLI.py

    r1079 r1087  
    1919
    2020        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::
    2222                FramsticksCLI.py -h"""
    2323
  • framspy/FramsticksLib.py

    r1084 r1087  
    88
    99class FramsticksLib:
    10         """Communicates directly with Framsticks DLL/SO.
     10        """Communicates directly with Framsticks library (.dll or .so).
    1111        You can perform basic operations like mutation, crossover, and evaluation of genotypes.
    1212        This way you can perform evolution controlled by python as well as access and manipulate genotypes.
     
    1414        or access and control the simulation and simulated creatures step by step.
    1515
    16         You need to provide one or two parameters when you run this class: the path to Framsticks CLI where .dll/.so resides
    17         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::
    1818                FramsticksLib.py -h"""
    1919
     
    2626
    2727        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
    2932
    3033                print('Available objects:', dir(frams))
  • framspy/frams-test.py

    r1085 r1087  
    22import frams
    33
    4 frams.init(*(sys.argv[1:])) #pass whatever args we have, init() is the right place to deal with different scenrios:
     4frams.init(*(sys.argv[1:]))  # pass whatever args we have, init() is the right place to deal with different scenarios:
    55# 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
    89
    910print('Available objects:', dir(frams))
  • framspy/frams.py

    r1085 r1087  
    2323
    2424        _reInsideParens = re.compile('\((.*)\)')
    25         _reservedWords = ['import']  # this list is scanned during every attribute access, only add what is really clashing with framsticks properties
     25        _reservedWords = ['import']  # this list is scanned during every attribute access, only add what is really clashing with Framsticks properties
    2626        _reservedXWords = ['x' + word for word in _reservedWords]
    2727        _encoding = 'utf-8'
     
    252252        Initializes the connection to Framsticks dll/so.
    253253
    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.
    258258
    259259        """
    260260        # goals:
    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=[]
     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 = []
    266266        for a in args:
    267                 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==None:
    274                         lib_path=a
     267                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
    275275                else:
    276276                        initargs.append(a)
    277         if lib_path==None:
    278                 #todo: use env 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='.'
     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 = '.'
    281281
    282282        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 dll
    284         abs_data=os.path.abspath('data') #use absolute path for -d and -D so python is free to cd anywhere without confusing the frams
     283        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
    285285        # 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_data
    288         if frams_D==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')
    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
    294294        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 './'
    297297        c_api = ctypes.CDLL(lib_name)
    298298        os.chdir(original_dir)
    299        
     299
    300300        c_api.init.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)]
    301301        c_api.init.restype = None
Note: See TracChangeset for help on using the changeset viewer.