Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pykat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sebastian Steinlechner
pykat
Commits
755a17bf
Commit
755a17bf
authored
10 years ago
by
Daniel Brown
Browse files
Options
Downloads
Patches
Plain Diff
fixing more deepcopying issues, probably still some left with detectors
parent
f98056f0
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
pykat/components.py
+26
-8
26 additions, 8 deletions
pykat/components.py
pykat/finesse.py
+29
-9
29 additions, 9 deletions
pykat/finesse.py
pykat/node_network.py
+51
-7
51 additions, 7 deletions
pykat/node_network.py
test/test_deepcopying_references.py
+46
-0
46 additions, 0 deletions
test/test_deepcopying_references.py
with
152 additions
and
24 deletions
pykat/components.py
+
26
−
8
View file @
755a17bf
...
...
@@ -84,6 +84,8 @@ class NodeGaussSetter(object):
def
qy
(
self
,
value
):
self
.
__node
().
setGauss
(
self
.
__comp
(),
self
.
qx
,
complex
(
value
))
id___
=
0
class
Component
(
object
):
__metaclass__
=
abc
.
ABCMeta
...
...
@@ -91,7 +93,13 @@ class Component(object):
# This creates an instance specific class for the component
# this enables us to add properties to instances rather than
# all classes
return
object
.
__new__
(
type
(
cls
.
__name__
,
(
cls
,),
{}),
*
args
,
**
kwargs
)
global
id___
id___
+=
1
cnew_name
=
str
(
"
%s.%s_%i
"
%
(
cls
.
__module__
,
cls
.
__name__
,
id___
))
cnew
=
type
(
cnew_name
,
(
cls
,),
{})
return
object
.
__new__
(
cnew
,
*
args
,
**
kwargs
)
def
__init__
(
self
,
name
=
None
):
...
...
@@ -116,10 +124,11 @@ class Component(object):
When deep copying a kat object we need to take into account
the instance specific properties.
"""
result
=
self
.
__class__
.
__new__
(
self
.
__class__
)
result
.
__dict__
=
copy
.
deepcopy
(
self
.
__dict__
,
memo
)
result
.
__update_node_setters
# Here we create a copy of this object based of the base class
# of this one, otherwise we're making a copy of a copy of a copy...
result
=
self
.
__class__
.
__new__
(
self
.
__class__
.
__base__
)
result
.
__dict__
=
copy
.
deepcopy
(
self
.
__dict__
,
memo
)
return
result
...
...
@@ -167,9 +176,15 @@ class Component(object):
# now we have a list of which to remove
for
key
in
key_rm
:
ns
=
self
.
__dict__
[
key
]
delattr
(
self
,
'
__nodesetter_
'
+
ns
.
node
.
name
)
delattr
(
self
.
__class__
,
ns
.
node
.
name
)
name
=
str
(
ns
.
node
.
name
)
if
'
__nodesetter_
'
+
name
in
self
.
__dict__
:
delattr
(
self
,
'
__nodesetter_
'
+
name
)
if
name
in
self
.
__class__
.
__dict__
:
delattr
(
self
.
__class__
,
name
)
# Now re-add them pointing to the recent nodes
for
node
in
self
.
nodes
:
if
type
(
node
)
!=
pykat
.
node_network
.
DumpNode
:
ns
=
NodeGaussSetter
(
self
,
node
)
...
...
@@ -180,9 +195,12 @@ class Component(object):
if
not
isinstance
(
ns
,
NodeGaussSetter
):
raise
exceptions
.
ValueError
(
"
Argument is not of type NodeGaussSetter
"
)
name
=
ns
.
node
.
name
name
=
str
(
ns
.
node
.
name
)
fget
=
lambda
self
:
self
.
__get_node_setter
(
name
)
if
name
==
"
nITM1
"
:
print
(
self
.
__class__
)
setattr
(
self
.
__class__
,
name
,
property
(
fget
))
setattr
(
self
,
'
__nodesetter_
'
+
name
,
ns
)
...
...
This diff is collapsed.
Click to expand it.
pykat/finesse.py
+
29
−
9
View file @
755a17bf
...
...
@@ -417,6 +417,8 @@ class Block:
Constant
=
namedtuple
(
'
Constant
'
,
'
name, value, usedBy
'
)
id___
=
0
class
kat
(
object
):
def
__new__
(
cls
,
*
args
,
**
kwargs
):
...
...
@@ -428,7 +430,10 @@ class kat(object):
# kat objects share the same class definition they also have the
# same properties regardless of whether they have the actual
# object added to it. So we create an instance specific class.
return
object
.
__new__
(
type
(
pykat
.
finesse
.
kat
.
__name__
,
(
pykat
.
finesse
.
kat
,),
{}),
*
args
,
**
kwargs
)
global
id___
id___
+=
1
cnew
=
type
(
pykat
.
finesse
.
kat
.
__name__
+
str
(
"
_
"
)
+
str
(
id___
),
(
pykat
.
finesse
.
kat
,),
{})
return
object
.
__new__
(
cnew
,
*
args
,
**
kwargs
)
def
__init__
(
self
,
kat_file
=
None
,
kat_code
=
None
,
katdir
=
""
,
katname
=
""
,
tempdir
=
None
,
tempname
=
None
):
self
.
scene
=
None
# scene object for GUI
...
...
@@ -477,12 +482,27 @@ class kat(object):
self
.
loadKatFile
(
kat_file
)
def
__deepcopy__
(
self
,
memo
):
cls
=
self
.
__class__
result
=
cls
.
__new__
(
cls
)
"""
When deep copying a kat object we need to take into account
the instance specific properties. This is because when
the kat object adds new components it also adds properties for
each of these. There properties are unique to each kat object,
but properties are part of the class definition. Thus if two
kat objects share the same class definition they also have the
same properties regardless of whether they have the actual
object added to it. So we create an instance specific class.
"""
result
=
self
.
__class__
.
__new__
(
self
.
__class__
)
memo
[
id
(
self
)]
=
result
result
.
__dict__
=
copy
.
deepcopy
(
self
.
__dict__
,
memo
)
# Find all properties in class we are copying
# and deep copy these to the new class instance
for
x
in
self
.
__class__
.
__dict__
.
items
():
if
isinstance
(
x
[
1
],
property
):
setattr
(
result
.
__class__
,
x
[
0
],
x
[
1
])
for
k
,
v
in
self
.
__dict__
.
items
():
setattr
(
result
,
k
,
copy
.
deepcopy
(
v
,
memo
))
result
.
nodes
.
_NodeNetwork__update_nodes_properties
()
# Update any weakrefs
for
c
in
result
.
components
:
...
...
This diff is collapsed.
Click to expand it.
pykat/node_network.py
+
51
−
7
View file @
755a17bf
...
...
@@ -22,13 +22,21 @@ from pykat.detectors import BaseDetector as Detector
from
pykat.optics.gaussian_beams
import
beam_param
from
copy
import
deepcopy
id___
=
0
class
NodeNetwork
(
object
):
def
__new__
(
cls
,
*
args
,
**
kwargs
):
# This creates an instance specific class for the component
# this enables us to add properties to instances rather than
# all classes
return
object
.
__new__
(
type
(
cls
.
__name__
,
(
cls
,),
{}),
*
args
,
**
kwargs
)
global
id___
id___
+=
1
cnew_name
=
str
(
"
%s.%s_%i
"
%
(
cls
.
__module__
,
cls
.
__name__
,
id___
))
cnew
=
type
(
cnew_name
,
(
cls
,),
{})
return
object
.
__new__
(
cnew
,
*
args
,
**
kwargs
)
def
__init__
(
self
,
kat
):
self
.
__nodes
=
{}
...
...
@@ -38,6 +46,19 @@ class NodeNetwork(object):
self
.
__componentCallback
=
{}
self
.
__node_id
=
1
def
__deepcopy__
(
self
,
memo
):
"""
When deep copying a kat object we need to take into account
the instance specific properties.
"""
# Here we create a copy of this object based of the base class
# of this one, otherwise we're making a copy of a copy of a copy...
result
=
self
.
__class__
.
__new__
(
self
.
__class__
.
__base__
)
result
.
__dict__
=
deepcopy
(
self
.
__dict__
,
memo
)
return
result
@property
def
kat
(
self
):
return
self
.
__kat
...
...
@@ -152,6 +173,28 @@ class NodeNetwork(object):
if
do_callback
:
self
.
__componentCallback
[
comp
.
id
]()
def
__update_nodes_properties
(
self
):
# check if any node setters have already been added. If so we
# need to remove them. This function should get called if the nodes
# are updated, either by some function call or the GUI
key_rm
=
[
k
for
k
in
self
.
__dict__
if
k
.
startswith
(
"
__node_
"
,
0
,
7
)]
# now we have a list of which to remove
for
key
in
key_rm
:
ns
=
self
.
__dict__
[
key
]
name
=
str
(
ns
.
name
)
if
'
__node_
'
+
name
in
self
.
__dict__
:
delattr
(
self
,
'
__node_
'
+
name
)
if
name
in
self
.
__class__
.
__dict__
:
delattr
(
self
.
__class__
,
name
)
# Now re-add them pointing to the recent nodes
for
node
in
self
.
__nodes
:
if
not
self
.
__nodes
[
node
].
isDump
:
self
.
__add_node_attr
(
self
.
__nodes
[
node
])
def
createNode
(
self
,
node_name
):
"""
This creates a new node object. It won
'
t be connected to anything or added to a
...
...
@@ -285,6 +328,7 @@ class NodeNetwork(object):
kat
.
nodes
.
replaceNode
(
kat
.
bs1
,
"
n1
"
,
kat
.
nodes
.
createNode
(
"
test1
"
))
name
=
node
.
name
delattr
(
self
,
'
__node_
'
+
name
)
delattr
(
self
.
__class__
,
name
)
...
...
This diff is collapsed.
Click to expand it.
test/test_deepcopying_references.py
0 → 100644
+
46
−
0
View file @
755a17bf
"""
Test file to ensure that references between deep copied objects are handled properly
"""
import
pykat
from
copy
import
deepcopy
kat0
=
pykat
.
finesse
.
kat
()
kat0
.
parseCommands
(
"
m m1 1 0 0 n0 n1
"
)
kat1
=
deepcopy
(
kat0
)
assert
(
kat0
!=
kat1
)
assert
(
kat0
.
__class__
!=
kat1
.
__class__
)
assert
(
kat0
.
m1
!=
kat1
.
m1
)
assert
(
kat0
.
m1
.
__class__
!=
kat1
.
m1
.
__class__
)
assert
(
kat0
.
m1
.
n0
!=
kat1
.
m1
.
n0
)
assert
(
kat0
.
m1
.
n0
.
node
!=
kat1
.
m1
.
n0
.
node
)
assert
(
kat0
.
nodes
.
n0
!=
kat1
.
nodes
.
n0
)
new
=
kat1
.
nodes
.
createNode
(
"
n4
"
)
kat1
.
nodes
.
replaceNode
(
kat1
.
m1
,
kat1
.
m1
.
n0
,
new
)
assert
(
not
hasattr
(
kat1
.
nodes
,
"
n0
"
))
assert
(
hasattr
(
kat1
.
nodes
,
"
n4
"
))
assert
(
hasattr
(
kat0
.
nodes
,
"
n0
"
))
assert
(
not
hasattr
(
kat0
.
nodes
,
"
n4
"
))
assert
(
hasattr
(
kat1
.
m1
,
"
n4
"
))
assert
(
not
hasattr
(
kat1
.
m1
,
"
n0
"
))
assert
(
hasattr
(
kat0
.
m1
,
"
n0
"
))
assert
(
not
hasattr
(
kat0
.
m1
,
"
n4
"
))
assert
(
kat0
.
nodes
.
n0
==
kat0
.
m1
.
n0
.
node
)
assert
(
kat0
.
nodes
.
n1
==
kat0
.
m1
.
n1
.
node
)
assert
(
kat1
.
nodes
.
n4
==
kat1
.
m1
.
n4
.
node
)
assert
(
kat1
.
nodes
.
n1
==
kat1
.
m1
.
n1
.
node
)
print
(
"
PASSED
"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment