diff options
author | Chris Ellison <cellison@cse.ucdavis.edu> | 2011-08-29 15:13:55 -0700 |
---|---|---|
committer | Chris Ellison <cellison@cse.ucdavis.edu> | 2011-08-29 15:13:55 -0700 |
commit | b8defe81042cb21c9367014747add47e651329f6 (patch) | |
tree | dde5a32112c7afe37ca047f105b7c0fdbc4f4445 | |
parent | 8753d123c46d26242e6400f88f0f4fee814bc0d3 (diff) | |
download | networkx-b8defe81042cb21c9367014747add47e651329f6.tar.gz |
Refactor external submodule.
-rw-r--r-- | networkx/external/__init__.py | 6 | ||||
-rw-r--r-- | networkx/external/decorator/__init__.py | 11 | ||||
-rw-r--r-- | networkx/external/decorator/_decorator.py (renamed from networkx/external/decorator.py) | 19 | ||||
-rw-r--r-- | networkx/external/decorator/_decorator3.py (renamed from networkx/external/decorator3.py) | 19 | ||||
-rw-r--r-- | networkx/readwrite/tests/test_graphml.py | 25 | ||||
-rw-r--r-- | networkx/utils/decorators.py | 9 | ||||
-rw-r--r-- | networkx/utils/misc.py | 15 |
7 files changed, 52 insertions, 52 deletions
diff --git a/networkx/external/__init__.py b/networkx/external/__init__.py index cea1cfd9..e69de29b 100644 --- a/networkx/external/__init__.py +++ b/networkx/external/__init__.py @@ -1,6 +0,0 @@ -import sys - -if sys.version >= '3': - import networkx.external.decorator3 -else: - import networkx.external.decorator diff --git a/networkx/external/decorator/__init__.py b/networkx/external/decorator/__init__.py new file mode 100644 index 00000000..2c288345 --- /dev/null +++ b/networkx/external/decorator/__init__.py @@ -0,0 +1,11 @@ +""" + Hack for including decorator-3.3.1 in NetworkX. +""" + +import sys + +if sys.version >= '3': + from ._decorator3 import * + _decorator = _decorator3 +else: + from ._decorator import * diff --git a/networkx/external/decorator.py b/networkx/external/decorator/_decorator.py index 67e22214..02698c50 100644 --- a/networkx/external/decorator.py +++ b/networkx/external/decorator/_decorator.py @@ -3,12 +3,12 @@ ## Copyright (c) 2005-2011, Michele Simionato ## All rights reserved. ## -## Redistributions of source code must retain the above copyright +## Redistributions of source code must retain the above copyright ## notice, this list of conditions and the following disclaimer. ## Redistributions in bytecode form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in ## the documentation and/or other materials provided with the -## distribution. +## distribution. ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT @@ -41,7 +41,7 @@ except ImportError: # for Python version < 2.5 "A simple replacement of functools.partial" def __init__(self, func, *args, **kw): self.func = func - self.args = args + self.args = args self.keywords = kw def __call__(self, *otherargs, **otherkw): kw = self.keywords.copy() @@ -81,7 +81,7 @@ class FunctionMaker(object): # func can be a class or a callable, but not an instance method self.name = func.__name__ if self.name == '<lambda>': # small hack for lambda functions - self.name = '_lambda_' + self.name = '_lambda_' self.doc = func.__doc__ self.module = func.__module__ if inspect.isfunction(func): @@ -127,6 +127,7 @@ class FunctionMaker(object): func.__doc__ = getattr(self, 'doc', None) func.__dict__ = getattr(self, 'dict', {}) func.func_defaults = getattr(self, 'defaults', ()) + func.__kwdefaults__ = getattr(self, 'kwonlydefaults', None) callermodule = sys._getframe(3).f_globals.get('__name__', '?') func.__module__ = getattr(self, 'module', callermodule) func.__dict__.update(kw) @@ -139,7 +140,7 @@ class FunctionMaker(object): if mo is None: raise SyntaxError('not a valid function template\n%s' % src) name = mo.group(1) # extract the function name - names = set([name] + [arg.strip(' *') for arg in + names = set([name] + [arg.strip(' *') for arg in self.shortsignature.split(',')]) for n in names: if n in ('_func_', '_call_'): @@ -171,7 +172,7 @@ class FunctionMaker(object): """ if isinstance(obj, str): # "name(signature)" name, rest = obj.strip().split('(', 1) - signature = rest[:-1] #strip a right parens + signature = rest[:-1] #strip a right parens func = None else: # a function name = None @@ -179,9 +180,9 @@ class FunctionMaker(object): func = obj self = cls(func, name, signature, defaults, doc, module) ibody = '\n'.join(' ' + line for line in body.splitlines()) - return self.make('def %(name)s(%(signature)s):\n' + ibody, + return self.make('def %(name)s(%(signature)s):\n' + ibody, evaldict, addsource, **attrs) - + def decorator(caller, func=None): """ decorator(caller) converts a caller function into a decorator; @@ -203,7 +204,7 @@ def decorator(caller, func=None): evaldict['_call_'] = caller evaldict['decorator'] = decorator return FunctionMaker.create( - '%s(%s)' % (caller.__name__, first), + '%s(%s)' % (caller.__name__, first), 'return decorator(_call_, %s)' % first, evaldict, undecorated=caller, __wrapped__=caller, doc=caller.__doc__, module=caller.__module__) diff --git a/networkx/external/decorator3.py b/networkx/external/decorator/_decorator3.py index 8902792a..a388cc3c 100644 --- a/networkx/external/decorator3.py +++ b/networkx/external/decorator/_decorator3.py @@ -3,12 +3,12 @@ ## Copyright (c) 2005-2011, Michele Simionato ## All rights reserved. ## -## Redistributions of source code must retain the above copyright +## Redistributions of source code must retain the above copyright ## notice, this list of conditions and the following disclaimer. ## Redistributions in bytecode form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in ## the documentation and/or other materials provided with the -## distribution. +## distribution. ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT @@ -41,7 +41,7 @@ except ImportError: # for Python version < 2.5 "A simple replacement of functools.partial" def __init__(self, func, *args, **kw): self.func = func - self.args = args + self.args = args self.keywords = kw def __call__(self, *otherargs, **otherkw): kw = self.keywords.copy() @@ -81,7 +81,7 @@ class FunctionMaker(object): # func can be a class or a callable, but not an instance method self.name = func.__name__ if self.name == '<lambda>': # small hack for lambda functions - self.name = '_lambda_' + self.name = '_lambda_' self.doc = func.__doc__ self.module = func.__module__ if inspect.isfunction(func): @@ -127,6 +127,7 @@ class FunctionMaker(object): func.__doc__ = getattr(self, 'doc', None) func.__dict__ = getattr(self, 'dict', {}) func.__defaults__ = getattr(self, 'defaults', ()) + func.__kwdefaults__ = getattr(self, 'kwonlydefaults', None) callermodule = sys._getframe(3).f_globals.get('__name__', '?') func.__module__ = getattr(self, 'module', callermodule) func.__dict__.update(kw) @@ -139,7 +140,7 @@ class FunctionMaker(object): if mo is None: raise SyntaxError('not a valid function template\n%s' % src) name = mo.group(1) # extract the function name - names = set([name] + [arg.strip(' *') for arg in + names = set([name] + [arg.strip(' *') for arg in self.shortsignature.split(',')]) for n in names: if n in ('_func_', '_call_'): @@ -171,7 +172,7 @@ class FunctionMaker(object): """ if isinstance(obj, str): # "name(signature)" name, rest = obj.strip().split('(', 1) - signature = rest[:-1] #strip a right parens + signature = rest[:-1] #strip a right parens func = None else: # a function name = None @@ -179,9 +180,9 @@ class FunctionMaker(object): func = obj self = cls(func, name, signature, defaults, doc, module) ibody = '\n'.join(' ' + line for line in body.splitlines()) - return self.make('def %(name)s(%(signature)s):\n' + ibody, + return self.make('def %(name)s(%(signature)s):\n' + ibody, evaldict, addsource, **attrs) - + def decorator(caller, func=None): """ decorator(caller) converts a caller function into a decorator; @@ -203,7 +204,7 @@ def decorator(caller, func=None): evaldict['_call_'] = caller evaldict['decorator'] = decorator return FunctionMaker.create( - '%s(%s)' % (caller.__name__, first), + '%s(%s)' % (caller.__name__, first), 'return decorator(_call_, %s)' % first, evaldict, undecorated=caller, __wrapped__=caller, doc=caller.__doc__, module=caller.__module__) diff --git a/networkx/readwrite/tests/test_graphml.py b/networkx/readwrite/tests/test_graphml.py index 7882c7ad..bfd88682 100644 --- a/networkx/readwrite/tests/test_graphml.py +++ b/networkx/readwrite/tests/test_graphml.py @@ -58,15 +58,15 @@ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdr ('n8','n7'), ('n8','n9'), ]) - + self.simple_directed_fh = \ io.BytesIO(self.simple_directed_data.encode('UTF-8')) self.attribute_data="""<?xml version="1.0" encoding="UTF-8"?> -<graphml xmlns="http://graphml.graphdrawing.org/xmlns" +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <key id="d0" for="node" attr.name="color" attr.type="string"> <default>yellow</default> @@ -199,7 +199,7 @@ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdr </graphml>""" fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError,nx.read_graphml,fh) - + def test_undirected_edge_in_directed(self): s="""<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" @@ -217,9 +217,9 @@ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdr def test_key_error(self): s="""<?xml version="1.0" encoding="UTF-8"?> -<graphml xmlns="http://graphml.graphdrawing.org/xmlns" +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <key id="d0" for="node" attr.name="color" attr.type="string"> <default>yellow</default> @@ -244,9 +244,9 @@ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdr def test_hyperedge_error(self): s="""<?xml version="1.0" encoding="UTF-8"?> -<graphml xmlns="http://graphml.graphdrawing.org/xmlns" +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <key id="d0" for="node" attr.name="color" attr.type="string"> <default>yellow</default> @@ -272,7 +272,7 @@ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdr assert_raises(nx.NetworkXError,nx.read_graphml,fh) # remove test until we get the "name" issue sorted - # https://networkx.lanl.gov/trac/ticket/544 + # https://networkx.lanl.gov/trac/ticket/544 def test_default_attribute(self): G=nx.Graph() G.add_node(1,label=1,color='green') @@ -297,7 +297,7 @@ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdr def test_multigraph_to_graph(self): # test converting multigraph to graph if no parallel edges are found pass - + def test_yfiles_extension(self): data="""<?xml version="1.0" encoding="UTF-8" standalone="no"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd"> @@ -379,9 +379,9 @@ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdr def test_bool(self): s="""<?xml version="1.0" encoding="UTF-8"?> -<graphml xmlns="http://graphml.graphdrawing.org/xmlns" +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <key id="d0" for="node" attr.name="test" attr.type="boolean"> <default>false</default> @@ -407,6 +407,5 @@ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdr """ fh = io.BytesIO(s.encode('UTF-8')) G=nx.read_graphml(fh) - print G.node assert_equal(G.node['n0']['test'],True) assert_equal(G.node['n2']['test'],False) diff --git a/networkx/utils/decorators.py b/networkx/utils/decorators.py index 94fb8b77..5d6781ad 100644 --- a/networkx/utils/decorators.py +++ b/networkx/utils/decorators.py @@ -1,14 +1,11 @@ import sys -if sys.version >= '3': - from networkx.external.decorator3 import decorator -else: - from networkx.external.decorator import decorator -from networkx.utils import is_string_like from collections import defaultdict from os.path import splitext -import networkx as nx +import networkx as nx +from networkx.external.decorator import decorator +from networkx.utils import is_string_like def require(*packages): """Decorator to check whether specific packages can be imported. diff --git a/networkx/utils/misc.py b/networkx/utils/misc.py index d48dbabd..83d127c5 100644 --- a/networkx/utils/misc.py +++ b/networkx/utils/misc.py @@ -8,7 +8,7 @@ can be accessed, for example, as >>> networkx.utils.is_string_like('spam') True """ -# Copyright (C) 2004-2011 by +# Copyright (C) 2004-2011 by # Aric Hagberg <hagberg@lanl.gov> # Dan Schult <dschult@colgate.edu> # Pieter Swart <swart@lanl.gov> @@ -19,10 +19,7 @@ import subprocess import uuid import networkx as nx -if sys.version >= '3': - from networkx.external.decorator3 import decorator -else: - from networkx.external.decorator import decorator +from networkx.external.decorator import decorator __author__ = '\n'.join(['Aric Hagberg (hagberg@lanl.gov)', 'Dan Schult(dschult@colgate.edu)', @@ -84,7 +81,7 @@ def get_file_handle(path, mode='r'): import bz2 fh = bz2.BZ2File(path,mode=mode) else: - fh = open(path,mode = mode) + fh = open(path,mode = mode) elif hasattr(path, 'read'): fh = path else: @@ -98,7 +95,7 @@ def make_str(t): def cumulative_sum(numbers): """Yield cumulative sum of numbers. - + >>> import networkx.utils as utils >>> list(utils.cumulative_sum([1,2,3,4])) [1, 3, 6, 10] @@ -107,11 +104,11 @@ def cumulative_sum(numbers): for n in numbers: csum += n yield csum - + def generate_unique_node(): """ Generate a unique node label.""" return str(uuid.uuid1()) - + def default_opener(filename): """Opens `filename` using system's default program. |