Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
snipper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
tuhe
snipper
Commits
1006ff96
Commit
1006ff96
authored
2 years ago
by
tuhe
Browse files
Options
Downloads
Plain Diff
updates
parents
39111374
1c982319
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/snipper/block_parsing.py
+4
-0
4 additions, 0 deletions
src/snipper/block_parsing.py
src/snipper/fix_bf.py
+93
-6
93 additions, 6 deletions
src/snipper/fix_bf.py
src/snipper/test.py
+1
-0
1 addition, 0 deletions
src/snipper/test.py
with
98 additions
and
6 deletions
src/snipper/block_parsing.py
+
4
−
0
View file @
1006ff96
...
...
@@ -83,6 +83,10 @@ def block_split(lines, tag):
post
=
line
[
nx_tag
:]
else
:
post
=
''
if
"
"
in
arg1
:
# remove block tags.
arg1
=
arg1
[
arg1
.
find
(
"
"
):].
strip
()
else
:
arg1
=
""
return
arg1
,
post
contents
[
'
arg1
'
],
contents
[
'
post1
'
]
=
argpost
(
lines
[
i
],
j
)
...
...
This diff is collapsed.
Click to expand it.
src/snipper/fix_bf.py
+
93
−
6
View file @
1006ff96
import
functools
import
hashlib
from
snipper.legacy
import
gcoms
from
snipper.block_parsing
import
indent
from
snipper.block_parsing
import
block_split
,
block_join
def
fix_f
(
lines
,
debug
,
keep
=
False
):
lines2
=
[]
i
=
0
...
...
@@ -63,7 +63,11 @@ def fix_f(lines, debug, keep=False):
return
lines2
# stats = {'n': 0}
def
_block_fun
(
lines
,
start_extra
,
end_extra
,
keep
=
False
,
silent
=
False
):
def
_block_fun
(
lines
,
start_extra
,
end_extra
,
keep
=
False
,
permute
=
False
,
questionmarks
=
false
,
halfquestionmarks
=
False
,
silent
=
False
):
methods
=
{
'
remove
'
:
0
}
# if method not in ['remove', 'permute', 'questionmark', 'halfquestionmark']:
# assert False
id
=
indent
(
lines
[
0
])
if
not
keep
:
lines
=
lines
[
1
:]
if
len
(
lines
[
0
].
strip
())
==
0
else
lines
...
...
@@ -72,6 +76,8 @@ def _block_fun(lines, start_extra, end_extra, keep=False, silent=False):
ee
=
end_extra
.
strip
()
if
len
(
ee
)
>=
2
and
ee
[
0
]
==
'"'
:
ee
=
ee
[
1
:
-
1
]
if
len
(
ee
)
==
0
:
ee
=
"
Insert your solution and remove this error.
"
start_extra
=
start_extra
.
strip
()
if
keep
:
l2
=
[
'
GARBAGE
'
]
*
cc
...
...
@@ -80,8 +86,28 @@ def _block_fun(lines, start_extra, end_extra, keep=False, silent=False):
l2
=
[]
cc
=
0
else
:
l2
=
([
id
+
start_extra
]
if
len
(
start_extra
)
>
0
else
[])
+
[
id
+
f
"
# TODO:
{
cc
}
lines missing.
"
,
id
+
f
'
raise NotImplementedError(
"
{
ee
}
"
)
'
]
# Ok we got so far. Now decide on randomization strategies and so on.
insert_lines
=
False
msg
=
[]
if
permute
:
msg
=
[
id
+
"
# TODO: The following lines have been permuted. Can you put them back in order?
"
]
# Permute all lines.
lines
=
f1
(
lines
)
insert_lines
=
True
pass
if
questionmarks
:
lines
=
f2
(
lines
)
insert_lines
=
True
elif
halfquestionmarks
:
lines
=
f3
(
lines
)
insert_lines
=
True
if
not
insert_lines
:
lines
=
[
id
+
f
"
# TODO:
{
cc
}
lines missing.
"
]
else
:
lines
=
msg
+
lines
lines
+=
[
id
+
f
'
raise NotImplementedError(
"
{
ee
}
"
)
'
]
l2
=
([
id
+
start_extra
]
if
len
(
start_extra
)
>
0
else
[])
+
lines
# [id + f"# TODO: {cc} lines missing.",
# id + f'raise NotImplementedError("{ee}")']
return
l2
,
cc
def
fix_b
(
lines
,
keep
=
False
):
...
...
@@ -93,9 +119,70 @@ def fix_b(lines, keep=False):
break
args
=
{
k
:
v
for
k
,
v
in
b
[
'
start_tag_args
'
].
items
()
if
len
(
k
)
>
0
}
cutout
+=
b
[
'
block
'
]
b
[
'
block
'
],
dn
=
_block_fun
(
b
[
'
block
'
],
start_extra
=
b
[
'
arg1
'
],
end_extra
=
b
[
'
arg2
'
],
**
args
,
keep
=
keep
)
# method = b['start_tag_args'].get('', 'remove')
b
[
'
block
'
],
dn
=
_block_fun
(
b
[
'
block
'
],
start_extra
=
b
[
'
arg1
'
],
end_extra
=
b
[
'
arg1
'
],
**
args
,
keep
=
keep
)
lines
=
block_join
(
b
)
n
+=
dn
# lines2, _, _, cutout = block_process(lines, tag="#!b", block_fun=functools.partial(block_fun, stats=stats))
return
lines
,
n
,
cutout
\ No newline at end of file
return
lines
,
n
,
cutout
import
textwrap
import
numpy
as
np
def
wspace
(
l
):
whitespace
=
"
"
*
(
len
(
l
)
-
len
(
l
.
lstrip
()))
return
whitespace
def
cmnt
(
lines
):
whitespace
=
"
"
*
(
len
(
lines
[
0
])
-
len
(
lines
[
0
].
lstrip
()))
lines
=
textwrap
.
dedent
(
"
\n
"
.
join
(
lines
)).
splitlines
()
lines
=
[
"
#
"
+
l
for
l
in
lines
]
return
lines
,
whitespace
# Example 1: Simply permute the lines
def
f1
(
lines
,
seed
=
None
):
# Hash the seed.
if
seed
==
None
:
ss
=
""
.
join
([
l
.
strip
()
for
l
in
lines
])
seed
=
int
(
hashlib
.
sha1
(
ss
.
encode
(
"
utf-8
"
)).
hexdigest
(),
16
)
%
(
10
**
8
)
# seed = abs(hash("".join([l.strip() for l in lines]))) % (10 ** 8)
permutation
=
np
.
random
.
RandomState
(
seed
=
seed
).
permutation
(
len
(
lines
))
# print(seed)
# print(lines)
# print(permutation)
lines
,
whitespace
=
cmnt
(
lines
)
lines
=
[
lines
[
i
]
for
i
in
permutation
]
lines
=
textwrap
.
indent
(
"
\n
"
.
join
(
lines
),
whitespace
).
splitlines
()
return
lines
# obscure(blk, f1, 'cs101_output/obscure_1.py')
# Example 2: Try to preserve keywords and special syntax symbols
def
f2
(
lines
):
lines
,
whitespace
=
cmnt
(
lines
)
kp
=
"""
#
'"
[](){},.+-012345679:=
"""
l2
=
[]
for
line
in
lines
:
line2
=
[]
for
w
in
line
.
split
(
'
'
):
if
w
in
[
''
,
'
return
'
,
'
if
'
,
'
else
'
'
=
'
,
'
#
'
,
"
for
"
,
"
in
"
]:
line2
.
append
(
w
)
else
:
w2
=
""
.
join
(
[
(
t
if
t
in
kp
else
'
?
'
)
for
t
in
w
]
)
line2
.
append
(
w2
)
l2
.
append
(
"
"
.
join
(
line2
))
lines
=
l2
lines
=
textwrap
.
indent
(
"
\n
"
.
join
(
lines
),
whitespace
).
splitlines
()
return
lines
# obscure(blk, f2, 'cs101_output/obscure_2.py')
# Example 3: keep half of the lines
def
f3
(
lines
):
lines
=
[
(
l
.
strip
(),
len
(
l
.
strip
()),
wspace
(
l
))
for
l
in
lines
]
lines
=
[
wp
+
l
[:
k
//
2
]
+
"
?
"
*
(
k
-
k
//
2
)
for
l
,
k
,
wp
in
lines
]
lines
,
whitespace
=
cmnt
(
lines
)
lines
=
textwrap
.
indent
(
"
\n
"
.
join
(
lines
),
whitespace
).
splitlines
()
return
lines
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/snipper/test.py
0 → 100644
+
1
−
0
View file @
1006ff96
import
d4rl
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