Changeset 8017:1b07b29cf53e in orange


Ignore:
Timestamp:
06/14/11 14:11:53 (3 years ago)
Author:
miha <miha.stajdohar@…>
Branch:
default
Convert:
ce9e5167d1723b4fa0c64982f794c5247171ca91
Message:

Copied three helper functions from the NetworkX utils.py that does not exist in all NetworkX versions (especially some linux versions :)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/network/readwrite.py

    r8004 r8017  
    2323import networkx as nx 
    2424import networkx.readwrite as rw 
    25  
    26 from networkx.utils import _get_fh, make_str, is_string_like 
    2725 
    2826import Orange 
     
    4947    tmp = tmp.replace('nx', 'Orange.network.nx') 
    5048    myclass.__doc__ += tmp  
     49 
     50def _is_string_like(obj): # from John Hunter, types-free version 
     51    """Check if obj is string.""" 
     52    try: 
     53        obj + '' 
     54    except (TypeError, ValueError): 
     55        return False 
     56    return True 
     57 
     58def _get_fh(path, mode='r'): 
     59    """ Return a file handle for given path. 
     60 
     61    Path can be a string or a file handle. 
     62 
     63    Attempt to uncompress/compress files ending in '.gz' and '.bz2'. 
     64 
     65    """ 
     66    if _is_string_like(path): 
     67        if path.endswith('.gz'): 
     68            import gzip 
     69            fh = gzip.open(path,mode=mode) 
     70        elif path.endswith('.bz2'): 
     71            import bz2 
     72            fh = bz2.BZ2File(path,mode=mode) 
     73        else: 
     74            fh = open(path,mode = mode)            
     75    elif hasattr(path, 'read'): 
     76        fh = path 
     77    else: 
     78        raise ValueError('path must be a string or file handle') 
     79    return fh 
     80 
     81def _make_str(t): 
     82    """Return the string representation of t.""" 
     83    if _is_string_like(t): return t 
     84    return str(t) 
    5185 
    5286def read(path, encoding='UTF-8'): 
     
    234268        nodenumber[n]=id 
    235269        shape=na.get('shape','ellipse') 
    236         s = ' '.join(map(make_str,(id,n,x,y,shape))) 
     270        s = ' '.join(map(_make_str,(id,n,x,y,shape))) 
    237271        for k,v in na.items(): 
    238272            if k != 'x' and k != 'y': 
     
    248282        d=edgedata.copy() 
    249283        value=d.pop('weight',1.0) # use 1 as default edge value 
    250         s = ' '.join(map(make_str,(nodenumber[u],nodenumber[v],value))) 
     284        s = ' '.join(map(_make_str,(nodenumber[u],nodenumber[v],value))) 
    251285        for k,v in d.items(): 
    252             if not is_string_like(v): 
     286            if not _is_string_like(v): 
    253287                v = repr(v) 
    254288            # add quotes to any values with a blank space 
Note: See TracChangeset for help on using the changeset viewer.