`tiny_malloc_from_free_list` made my pointer `NULL`? - python
I am working on code that includes bllipparser Python module, among other things. Feeding it the same dataset, it will intermittently crash (maybe once in three to ten runs). Going through lldb, I found that the public field weights of RerankerModel (source), that is apparently only set once (in the constructor), randomly becomes NULL (I only have one RerankerModel for the duration of my run, so there should be exactly one weights, that persists unchanged throughout). So I set up an ambush (I mean, a watchpoint: I stopped the code in the constructor and watchpoint set expression -w write -- &weights), and apparently the culprit that nulls the pointer is tiny_malloc_from_free_list from libsystem_malloc.dylib. Here's the relevant top of the backtrace:
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 4
* frame #0: 0x00007fff61caf22a libsystem_malloc.dylib`tiny_malloc_from_free_list + 151
frame #1: 0x00007fff61cae3bf libsystem_malloc.dylib`szone_malloc_should_clear + 422
frame #2: 0x00007fff61cae1bd libsystem_malloc.dylib`malloc_zone_malloc + 103
frame #3: 0x00007fff61cad4c7 libsystem_malloc.dylib`malloc + 24
frame #4: 0x00007fff5faac628 libc++abi.dylib`operator new(unsigned long) + 40
frame #5: 0x00000001133c904c _CharniakParser.cpython-36m-darwin.so`std::__1::__split_buffer<short, std::__1::allocator<short>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<short>&) [inlined] std::__1::__allocate(__size=4) at new:226
frame #6: 0x00000001133c9040 _CharniakParser.cpython-36m-darwin.so`std::__1::__split_buffer<short, std::__1::allocator<short>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<short>&) [inlined] std::__1::allocator<short>::allocate(this=0x0000000135316448, __n=2, (null)=0x0000000000000000) at memory:1747
frame #7: 0x00000001133c8f44 _CharniakParser.cpython-36m-darwin.so`std::__1::__split_buffer<short, std::__1::allocator<short>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<short>&) [inlined] std::__1::allocator_traits<std::__1::allocator<short> >::allocate(__a=0x0000000135316448, __n=2) at memory:1502
frame #8: 0x00000001133c8f16 _CharniakParser.cpython-36m-darwin.so`std::__1::__split_buffer<short, std::__1::allocator<short>&>::__split_buffer(this=0x00007ffeefbf3b48, __cap=2, __start=1, __a=0x0000000135316448) at __split_buffer:311
frame #9: 0x00000001133c878d _CharniakParser.cpython-36m-darwin.so`std::__1::__split_buffer<short, std::__1::allocator<short>&>::__split_buffer(this=0x00007ffeefbf3b48, __cap=2, __start=1, __a=0x0000000135316448) at __split_buffer:310
frame #10: 0x00000001133c869b _CharniakParser.cpython-36m-darwin.so`void std::__1::vector<short, std::__1::allocator<short> >::__push_back_slow_path<short const>(this=0x0000000135316438 size=1, __x=0x00007ffeefbf3caa) at vector:1567
frame #11: 0x00000001133c4446 _CharniakParser.cpython-36m-darwin.so`Val::extendTrees(Bst&, int) [inlined] std::__1::vector<short, std::__1::allocator<short> >::push_back(this=0x0000000135316438 size=1, __x=0x00007ffeefbf3caa) at vector:1588
I am very much not an expert on C++, but... How is the allocator NULLing the pointer? How does the allocator even know where the pointer is? Why is the allocator NULLing the pointer? I mean, I can see how I might run out of memory, what I was doing wasn't exactly memory-light, but I'd much sooner expect allocator to fail to allocate than to randomly deallocate something - and I had no idea it could NULL a pointer. Can anyone explain to me exactly how this works, why it happened, how it happened, why is it always the same pointer in a code that has lots and lots of other juicy pointers, and what I can do to make it not happen?
Addendum if needed: Here's where the actual NULLing takes place, the code of tiny_malloc_from_free_list, if anyone can make sense of it...
libsystem_malloc.dylib`tiny_malloc_from_free_list:
0x7fff61caf193 <+0>: pushq %rbp
0x7fff61caf194 <+1>: movq %rsp, %rbp
0x7fff61caf197 <+4>: pushq %r15
0x7fff61caf199 <+6>: pushq %r14
0x7fff61caf19b <+8>: pushq %r13
0x7fff61caf19d <+10>: pushq %r12
0x7fff61caf19f <+12>: pushq %rbx
0x7fff61caf1a0 <+13>: pushq %rax
0x7fff61caf1a1 <+14>: movl %edx, %r15d
0x7fff61caf1a4 <+17>: movq %rsi, %r14
0x7fff61caf1a7 <+20>: movq %rdi, %r12
0x7fff61caf1aa <+23>: leal -0x1(%r15), %ecx
0x7fff61caf1ae <+27>: movq 0x18(%r14,%rcx,8), %r13
0x7fff61caf1b3 <+32>: testq %r13, %r13
0x7fff61caf1b6 <+35>: je 0x7fff61caf22f ; <+156>
0x7fff61caf1b8 <+37>: movq 0x8(%r13), %rdx
0x7fff61caf1bc <+41>: movq %rdx, %rax
0x7fff61caf1bf <+44>: shlq $0x4, %rax
0x7fff61caf1c3 <+48>: shrq $0x3c, %rdx
0x7fff61caf1c7 <+52>: movq 0x278(%r12), %rsi
0x7fff61caf1cf <+60>: xorq %rax, %rsi
0x7fff61caf1d2 <+63>: movq %rsi, %rdi
0x7fff61caf1d5 <+66>: shrq $0x8, %rdi
0x7fff61caf1d9 <+70>: addl %esi, %edi
0x7fff61caf1db <+72>: movq %rsi, %rbx
0x7fff61caf1de <+75>: shrq $0x10, %rbx
0x7fff61caf1e2 <+79>: addl %edi, %ebx
0x7fff61caf1e4 <+81>: movq %rsi, %rdi
0x7fff61caf1e7 <+84>: shrq $0x18, %rdi
0x7fff61caf1eb <+88>: addl %ebx, %edi
0x7fff61caf1ed <+90>: movq %rsi, %rbx
0x7fff61caf1f0 <+93>: shrq $0x20, %rbx
0x7fff61caf1f4 <+97>: addl %edi, %ebx
0x7fff61caf1f6 <+99>: movq %rsi, %rdi
0x7fff61caf1f9 <+102>: shrq $0x28, %rdi
0x7fff61caf1fd <+106>: addl %ebx, %edi
0x7fff61caf1ff <+108>: movq %rsi, %rbx
0x7fff61caf202 <+111>: shrq $0x30, %rbx
0x7fff61caf206 <+115>: addl %edi, %ebx
0x7fff61caf208 <+117>: shrq $0x38, %rsi
0x7fff61caf20c <+121>: addl %ebx, %esi
0x7fff61caf20e <+123>: andl $0xf, %esi
0x7fff61caf211 <+126>: cmpq %rsi, %rdx
0x7fff61caf214 <+129>: jne 0x7fff61caf602 ; <+1135>
0x7fff61caf21a <+135>: testq %rax, %rax
0x7fff61caf21d <+138>: je 0x7fff61caf2de ; <+331>
0x7fff61caf223 <+144>: movq (%r13), %rdx
0x7fff61caf227 <+148>: movq %rdx, (%rax)
-> 0x7fff61caf22a <+151>: jmp 0x7fff61caf2f2 ; <+351>
0x7fff61caf22f <+156>: movq $-0x1, %rax
0x7fff61caf236 <+163>: shlq %cl, %rax
0x7fff61caf239 <+166>: andq 0x818(%r14), %rax
0x7fff61caf240 <+173>: je 0x7fff61caf4f9 ; <+870>
0x7fff61caf246 <+179>: bsfq %rax, %rcx
0x7fff61caf24a <+183>: cmpq $0x3f, %rcx
0x7fff61caf24e <+187>: je 0x7fff61caf39d ; <+522>
0x7fff61caf254 <+193>: movq 0x18(%r14,%rcx,8), %r13
0x7fff61caf259 <+198>: testq %r13, %r13
0x7fff61caf25c <+201>: je 0x7fff61caf39d ; <+522>
0x7fff61caf262 <+207>: movq 0x8(%r13), %rdx
0x7fff61caf266 <+211>: movq %rdx, %rax
0x7fff61caf269 <+214>: shlq $0x4, %rax
0x7fff61caf26d <+218>: shrq $0x3c, %rdx
0x7fff61caf271 <+222>: movq 0x278(%r12), %rsi
0x7fff61caf279 <+230>: xorq %rax, %rsi
0x7fff61caf27c <+233>: movq %rsi, %rdi
0x7fff61caf27f <+236>: shrq $0x8, %rdi
0x7fff61caf283 <+240>: addl %esi, %edi
0x7fff61caf285 <+242>: movq %rsi, %rbx
0x7fff61caf288 <+245>: shrq $0x10, %rbx
0x7fff61caf28c <+249>: addl %edi, %ebx
0x7fff61caf28e <+251>: movq %rsi, %rdi
0x7fff61caf291 <+254>: shrq $0x18, %rdi
0x7fff61caf295 <+258>: addl %ebx, %edi
0x7fff61caf297 <+260>: movq %rsi, %rbx
0x7fff61caf29a <+263>: shrq $0x20, %rbx
0x7fff61caf29e <+267>: addl %edi, %ebx
0x7fff61caf2a0 <+269>: movq %rsi, %rdi
0x7fff61caf2a3 <+272>: shrq $0x28, %rdi
0x7fff61caf2a7 <+276>: addl %ebx, %edi
0x7fff61caf2a9 <+278>: movq %rsi, %rbx
0x7fff61caf2ac <+281>: shrq $0x30, %rbx
0x7fff61caf2b0 <+285>: addl %edi, %ebx
0x7fff61caf2b2 <+287>: shrq $0x38, %rsi
0x7fff61caf2b6 <+291>: addl %ebx, %esi
0x7fff61caf2b8 <+293>: andl $0xf, %esi
0x7fff61caf2bb <+296>: cmpq %rsi, %rdx
0x7fff61caf2be <+299>: jne 0x7fff61caf602 ; <+1135>
0x7fff61caf2c4 <+305>: movq %rax, 0x18(%r14,%rcx,8)
0x7fff61caf2c9 <+310>: testq %rax, %rax
0x7fff61caf2cc <+313>: je 0x7fff61caf59f ; <+1036>
0x7fff61caf2d2 <+319>: movq (%r13), %rcx
0x7fff61caf2d6 <+323>: movq %rcx, (%rax)
0x7fff61caf2d9 <+326>: jmp 0x7fff61caf5b2 ; <+1055>
0x7fff61caf2de <+331>: movl $0xfffffffe, %edx ; imm = 0xFFFFFFFE
0x7fff61caf2e3 <+336>: roll %cl, %edx
0x7fff61caf2e5 <+338>: movl %ecx, %esi
0x7fff61caf2e7 <+340>: shrl $0x5, %esi
0x7fff61caf2ea <+343>: andl %edx, 0x818(%r14,%rsi,4)
0x7fff61caf2f2 <+351>: movq %rax, 0x18(%r14,%rcx,8)
0x7fff61caf2f7 <+356>: incl 0x850(%r14)
0x7fff61caf2fe <+363>: movzwl %r15w, %esi
0x7fff61caf302 <+367>: movl %esi, %ecx
0x7fff61caf304 <+369>: shll $0x4, %ecx
0x7fff61caf307 <+372>: addq %rcx, 0x858(%r14)
0x7fff61caf30e <+379>: movq %r13, %rax
0x7fff61caf311 <+382>: andq $-0x100000, %rax ; imm = 0xFFF00000
0x7fff61caf317 <+388>: addl 0xfc098(%rax), %ecx
0x7fff61caf31d <+394>: movl %ecx, 0xfc098(%rax)
0x7fff61caf323 <+400>: cmpl $0xbd060, %ecx ; imm = 0xBD060
0x7fff61caf329 <+406>: jb 0x7fff61caf335 ; <+418>
0x7fff61caf32b <+408>: movl $0x0, 0xfc090(%rax)
0x7fff61caf335 <+418>: cmpl $0x2, %esi
0x7fff61caf338 <+421>: jb 0x7fff61caf344 ; <+433>
0x7fff61caf33a <+423>: movq %r13, %rdi
0x7fff61caf33d <+426>: callq 0x7fff61cc5eaa ; set_tiny_meta_header_in_use
0x7fff61caf342 <+431>: jmp 0x7fff61caf38b ; <+504>
0x7fff61caf344 <+433>: movq %r13, %rcx
0x7fff61caf347 <+436>: shrq $0x4, %rcx
0x7fff61caf34b <+440>: movl %r13d, %edx
0x7fff61caf34e <+443>: shrl $0x8, %edx
0x7fff61caf351 <+446>: andl $0xffe, %edx ; imm = 0xFFE
0x7fff61caf357 <+452>: movl $0x1, %esi
0x7fff61caf35c <+457>: movl $0x1, %edi
0x7fff61caf361 <+462>: shll %cl, %edi
0x7fff61caf363 <+464>: orl %edi, 0xfc0a0(%rax,%rdx,4)
0x7fff61caf36a <+471>: orl $0x1, %edx
0x7fff61caf36d <+474>: orl %edi, 0xfc0a0(%rax,%rdx,4)
0x7fff61caf374 <+481>: leal 0x1(%rcx), %ecx
0x7fff61caf377 <+484>: movl %ecx, %edx
0x7fff61caf379 <+486>: shrl $0x4, %edx
0x7fff61caf37c <+489>: andl $0xffe, %edx ; imm = 0xFFE
0x7fff61caf382 <+495>: shll %cl, %esi
0x7fff61caf384 <+497>: orl %esi, 0xfc0a0(%rax,%rdx,4)
0x7fff61caf38b <+504>: movq %r13, %rax
0x7fff61caf38e <+507>: addq $0x8, %rsp
0x7fff61caf392 <+511>: popq %rbx
0x7fff61caf393 <+512>: popq %r12
0x7fff61caf395 <+514>: popq %r13
0x7fff61caf397 <+516>: popq %r14
0x7fff61caf399 <+518>: popq %r15
0x7fff61caf39b <+520>: popq %rbp
0x7fff61caf39c <+521>: retq
0x7fff61caf39d <+522>: movq 0x210(%r14), %r13
0x7fff61caf3a4 <+529>: testq %r13, %r13
0x7fff61caf3a7 <+532>: je 0x7fff61caf4f9 ; <+870>
0x7fff61caf3ad <+538>: movq %r13, %rdi
0x7fff61caf3b0 <+541>: callq 0x7fff61cb05ab ; get_tiny_free_size
0x7fff61caf3b5 <+546>: movq 0x8(%r13), %rcx
0x7fff61caf3b9 <+550>: movq %rcx, %r8
0x7fff61caf3bc <+553>: shlq $0x4, %r8
0x7fff61caf3c0 <+557>: shrq $0x3c, %rcx
0x7fff61caf3c4 <+561>: movq 0x278(%r12), %rsi
0x7fff61caf3cc <+569>: movq %r8, %rdi
0x7fff61caf3cf <+572>: xorq %rsi, %rdi
0x7fff61caf3d2 <+575>: movq %rdi, %rbx
0x7fff61caf3d5 <+578>: shrq $0x8, %rbx
0x7fff61caf3d9 <+582>: addl %edi, %ebx
0x7fff61caf3db <+584>: movq %rdi, %rdx
0x7fff61caf3de <+587>: shrq $0x10, %rdx
0x7fff61caf3e2 <+591>: addl %ebx, %edx
0x7fff61caf3e4 <+593>: movq %rdi, %rbx
0x7fff61caf3e7 <+596>: shrq $0x18, %rbx
0x7fff61caf3eb <+600>: addl %edx, %ebx
0x7fff61caf3ed <+602>: movq %rdi, %rdx
0x7fff61caf3f0 <+605>: shrq $0x20, %rdx
0x7fff61caf3f4 <+609>: addl %ebx, %edx
0x7fff61caf3f6 <+611>: movq %rdi, %rbx
0x7fff61caf3f9 <+614>: shrq $0x28, %rbx
0x7fff61caf3fd <+618>: addl %edx, %ebx
0x7fff61caf3ff <+620>: movq %rdi, %rdx
0x7fff61caf402 <+623>: shrq $0x30, %rdx
0x7fff61caf406 <+627>: addl %ebx, %edx
0x7fff61caf408 <+629>: shrq $0x38, %rdi
0x7fff61caf40c <+633>: addl %edx, %edi
0x7fff61caf40e <+635>: andl $0xf, %edi
0x7fff61caf411 <+638>: cmpq %rdi, %rcx
0x7fff61caf414 <+641>: jne 0x7fff61caf602 ; <+1135>
0x7fff61caf41a <+647>: movzwl %ax, %edi
0x7fff61caf41d <+650>: subl %r15d, %edi
0x7fff61caf420 <+653>: cmpl $0x40, %edi
0x7fff61caf423 <+656>: jl 0x7fff61caf58a ; <+1015>
0x7fff61caf429 <+662>: movl %r15d, %eax
0x7fff61caf42c <+665>: shll $0x4, %eax
0x7fff61caf42f <+668>: addq %r13, %rax
0x7fff61caf432 <+671>: movq %rax, 0x210(%r14)
0x7fff61caf439 <+678>: movq %rax, %rcx
0x7fff61caf43c <+681>: shrq $0x4, %rcx
0x7fff61caf440 <+685>: testq %r8, %r8
0x7fff61caf443 <+688>: je 0x7fff61caf48e ; <+763>
0x7fff61caf445 <+690>: xorq %rax, %rsi
0x7fff61caf448 <+693>: movq %rsi, %rdx
0x7fff61caf44b <+696>: shrq $0x8, %rdx
0x7fff61caf44f <+700>: addl %esi, %edx
0x7fff61caf451 <+702>: movq %rsi, %rbx
0x7fff61caf454 <+705>: shrq $0x10, %rbx
0x7fff61caf458 <+709>: addl %edx, %ebx
0x7fff61caf45a <+711>: movq %rsi, %rdx
0x7fff61caf45d <+714>: shrq $0x18, %rdx
0x7fff61caf461 <+718>: addl %ebx, %edx
0x7fff61caf463 <+720>: movq %rsi, %rbx
0x7fff61caf466 <+723>: shrq $0x20, %rbx
0x7fff61caf46a <+727>: addl %edx, %ebx
0x7fff61caf46c <+729>: movq %rsi, %rdx
0x7fff61caf46f <+732>: shrq $0x28, %rdx
0x7fff61caf473 <+736>: addl %ebx, %edx
0x7fff61caf475 <+738>: movq %rsi, %rbx
0x7fff61caf478 <+741>: shrq $0x30, %rbx
0x7fff61caf47c <+745>: addl %edx, %ebx
0x7fff61caf47e <+747>: shrq $0x38, %rsi
0x7fff61caf482 <+751>: addl %ebx, %esi
0x7fff61caf484 <+753>: shlq $0x3c, %rsi
0x7fff61caf488 <+757>: orq %rcx, %rsi
0x7fff61caf48b <+760>: movq %rsi, (%r8)
0x7fff61caf48e <+763>: movq (%r13), %rdx
0x7fff61caf492 <+767>: movq %rdx, (%rax)
0x7fff61caf495 <+770>: movq 0x8(%r13), %rdx
0x7fff61caf499 <+774>: movq %rdx, 0x8(%rax)
0x7fff61caf49d <+778>: movq %rax, %rdx
0x7fff61caf4a0 <+781>: andq $-0x100000, %rdx ; imm = 0xFFF00000
0x7fff61caf4a7 <+788>: movl %eax, %esi
0x7fff61caf4a9 <+790>: shrl $0x8, %esi
0x7fff61caf4ac <+793>: andl $0xffe, %esi ; imm = 0xFFE
0x7fff61caf4b2 <+799>: andl $0x1f, %ecx
0x7fff61caf4b5 <+802>: movl $0x1, %ebx
0x7fff61caf4ba <+807>: shll %cl, %ebx
0x7fff61caf4bc <+809>: orl %ebx, 0xfc0a0(%rdx,%rsi,4)
0x7fff61caf4c3 <+816>: movl $0xfffffffe, %ebx ; imm = 0xFFFFFFFE
0x7fff61caf4c8 <+821>: roll %cl, %ebx
0x7fff61caf4ca <+823>: orl $0x1, %esi
0x7fff61caf4cd <+826>: andl %ebx, 0xfc0a0(%rdx,%rsi,4)
0x7fff61caf4d4 <+833>: movzwl %di, %ecx
0x7fff61caf4d7 <+836>: cmpl $0x2, %ecx
0x7fff61caf4da <+839>: jb 0x7fff61caf5ee ; <+1115>
0x7fff61caf4e0 <+845>: movl %edi, %ecx
0x7fff61caf4e2 <+847>: shll $0x4, %ecx
0x7fff61caf4e5 <+850>: andl $0xffff0, %ecx ; imm = 0xFFFF0
0x7fff61caf4eb <+856>: movw %di, -0x2(%rax,%rcx)
0x7fff61caf4f0 <+861>: movw %di, 0x10(%rax)
0x7fff61caf4f4 <+865>: jmp 0x7fff61caf2f7 ; <+356>
0x7fff61caf4f9 <+870>: movq 0x838(%r14), %rcx
0x7fff61caf500 <+877>: movl %r15d, %eax
0x7fff61caf503 <+880>: shll $0x4, %eax
0x7fff61caf506 <+883>: movq %rcx, %rdx
0x7fff61caf509 <+886>: subq %rax, %rdx
0x7fff61caf50c <+889>: jae 0x7fff61caf516 ; <+899>
0x7fff61caf50e <+891>: xorl %r13d, %r13d
0x7fff61caf511 <+894>: jmp 0x7fff61caf38b ; <+504>
0x7fff61caf516 <+899>: movl $0xfc080, %r13d ; imm = 0xFC080
0x7fff61caf51c <+905>: subq %rcx, %r13
0x7fff61caf51f <+908>: addq 0x848(%r14), %r13
0x7fff61caf526 <+915>: movq %rdx, 0x838(%r14)
0x7fff61caf52d <+922>: testq %rdx, %rdx
0x7fff61caf530 <+925>: je 0x7fff61caf2f7 ; <+356>
0x7fff61caf536 <+931>: addq %r13, %rax
0x7fff61caf539 <+934>: movq %rax, %rdx
0x7fff61caf53c <+937>: andq $-0x100000, %rdx ; imm = 0xFFF00000
0x7fff61caf543 <+944>: movq %rax, %rcx
0x7fff61caf546 <+947>: shrq $0x4, %rcx
0x7fff61caf54a <+951>: shrl $0x8, %eax
0x7fff61caf54d <+954>: andl $0xffe, %eax ; imm = 0xFFE
0x7fff61caf552 <+959>: movl $0x1, %esi
0x7fff61caf557 <+964>: movl $0x1, %edi
0x7fff61caf55c <+969>: shll %cl, %edi
0x7fff61caf55e <+971>: orl %edi, 0xfc0a0(%rdx,%rax,4)
0x7fff61caf565 <+978>: orl $0x1, %eax
0x7fff61caf568 <+981>: orl %edi, 0xfc0a0(%rdx,%rax,4)
0x7fff61caf56f <+988>: leal 0x1(%rcx), %ecx
0x7fff61caf572 <+991>: movl %ecx, %eax
0x7fff61caf574 <+993>: shrl $0x4, %eax
0x7fff61caf577 <+996>: andl $0xffe, %eax ; imm = 0xFFE
0x7fff61caf57c <+1001>: shll %cl, %esi
0x7fff61caf57e <+1003>: orl %esi, 0xfc0a0(%rdx,%rax,4)
0x7fff61caf585 <+1010>: jmp 0x7fff61caf2f7 ; <+356>
0x7fff61caf58a <+1015>: testq %r8, %r8
0x7fff61caf58d <+1018>: je 0x7fff61caf596 ; <+1027>
0x7fff61caf58f <+1020>: movq (%r13), %rcx
0x7fff61caf593 <+1024>: movq %rcx, (%r8)
0x7fff61caf596 <+1027>: movq %r8, 0x210(%r14)
0x7fff61caf59d <+1034>: jmp 0x7fff61caf5ba ; <+1063>
0x7fff61caf59f <+1036>: movl $0xfffffffe, %eax ; imm = 0xFFFFFFFE
0x7fff61caf5a4 <+1041>: roll %cl, %eax
0x7fff61caf5a6 <+1043>: shrq $0x5, %rcx
0x7fff61caf5aa <+1047>: andl %eax, 0x818(%r14,%rcx,4)
0x7fff61caf5b2 <+1055>: movq %r13, %rdi
0x7fff61caf5b5 <+1058>: callq 0x7fff61cb05ab ; get_tiny_free_size
0x7fff61caf5ba <+1063>: leal -0x1(%rax), %ecx
0x7fff61caf5bd <+1066>: cmpw %r15w, %cx
0x7fff61caf5c1 <+1070>: jae 0x7fff61caf5cc ; <+1081>
0x7fff61caf5c3 <+1072>: movw %ax, %r15w
0x7fff61caf5c7 <+1076>: jmp 0x7fff61caf2f7 ; <+356>
0x7fff61caf5cc <+1081>: subl %r15d, %eax
0x7fff61caf5cf <+1084>: movl %r15d, %ecx
0x7fff61caf5d2 <+1087>: shll $0x4, %ecx
0x7fff61caf5d5 <+1090>: movq %r13, %rdx
0x7fff61caf5d8 <+1093>: addq %rcx, %rdx
0x7fff61caf5db <+1096>: movzwl %ax, %ecx
0x7fff61caf5de <+1099>: movq %r12, %rdi
0x7fff61caf5e1 <+1102>: movq %r14, %rsi
0x7fff61caf5e4 <+1105>: callq 0x7fff61cafe1b ; tiny_free_list_add_ptr
0x7fff61caf5e9 <+1110>: jmp 0x7fff61caf2f7 ; <+356>
0x7fff61caf5ee <+1115>: testw %di, %di
0x7fff61caf5f1 <+1118>: jne 0x7fff61caf2f7 ; <+356>
0x7fff61caf5f7 <+1124>: movw $0x0, 0x10(%rax)
0x7fff61caf5fd <+1130>: jmp 0x7fff61caf2f7 ; <+356>
0x7fff61caf602 <+1135>: addq $0x8, %r13
0x7fff61caf606 <+1139>: movl 0x26c(%r12), %edi
0x7fff61caf60e <+1147>: movq %r13, %rsi
0x7fff61caf611 <+1150>: callq 0x7fff61cc5505 ; free_list_checksum_botch.352
0x7fff61caf616 <+1155>: ud2
0x7fff61caf618 <+1157>: nop
The offender is 0x7fff61caf227 <+148>: movq %rdx, (%rax), where rax is the address of my weights pointer that gets nulled, and rdx is 0.
That should be:
An escape sequence \0 is present in the dataset. The size of the string is calculated with python:
>>> len("a\0aa")
4
Then, the string is passed to c++ (CharniakParser) and we loop through it to parse it:
string a = "a\0aa";
const char* b = a.c_str();
cout << a.size() << endl; // size == 1, \0 is the end of a string
for(size_t i=0; i<4; i++)// 4 is the string size calculated with python
{
const char* c = &b[i];
do_something_with(c); // c is corrupt after i == 0
}
Because you are close to be out of memory, you corrupted pointer c shall points to a not null address (the assumption of not always the same pointer nullified), the object pointed is removed by an operation, and so in an other portion of your code, you have a pointer pointing to that object that is removed --> this is your bug
I realize that my explanation is far-fetched, but we haven't that much datas.
Related
How can I disassemble a runtime code using python?
Through capstone library i can easily disassemble a PE static code with all the instructions but i need to disassemble a runtime code of a PE file is this possible? import pefile import binascii from capstone import * pe = pefile.PE("/home/kali/Desktop/Debug Py/m.exe") entrypoint = pe.OPTIONAL_HEADER.AddressOfEntryPoint entrypoint_address = entrypoint+pe.OPTIONAL_HEADER.ImageBase binary_code = pe.get_memory_mapped_image()[entrypoint:entrypoint+100] disassembler = Cs(CS_ARCH_X86, CS_MODE_32) for i in disassembler.disasm(binary_code, entrypoint_address): b = binascii.hexlify(i.bytes) print("0x%x:\t%s\t\t%s\t%s" %(i.address,b,i.mnemonic, i.op_str)) 0x4014e0: b'83ec0c' sub esp, 0xc 0x4014e3: b'c7053810490000000000' mov dword ptr [0x491038], 0 0x4014ed: b'e82ec00000' call 0x40d520 0x4014f2: b'83c40c' add esp, 0xc 0x4014f5: b'e986fcffff' jmp 0x401180 0x4014fa: b'90' nop 0x4014fb: b'90' nop 0x4014fc: b'90' nop 0x4014fd: b'90' nop 0x4014fe: b'90' nop 0x4014ff: b'90' nop 0x401500: b'55' push ebp 0x401501: b'89e5' mov ebp, esp 0x401503: b'83e4f0' and esp, 0xfffffff0 0x401506: b'83ec10' sub esp, 0x10 0x401509: b'e8f2bf0000' call 0x40d500 0x40150e: b'c744240400804800' mov dword ptr [esp + 4], 0x488000 0x401516: b'c7042440714800' mov dword ptr [esp], 0x487140 0x40151d: b'e87e9d0700' call 0x47b2a0 0x401522: b'b800000000' mov eax, 0 0x401527: b'c9' leave 0x401528: b'c3' ret 0x401529: b'55' push ebp 0x40152a: b'89e5' mov ebp, esp 0x40152c: b'83ec08' sub esp, 8 0x40152f: b'b920104900' mov ecx, 0x491020 0x401534: b'e8d7230700' call 0x473910 0x401539: b'c9' leave 0x40153a: b'c3' ret 0x40153b: b'55' push ebp 0x40153c: b'89e5' mov ebp, esp 0x40153e: b'83ec18' sub esp, 0x18` This is a Static Code for the PE
GeoJson object (layer) not being added to Folium map
Have looked at previous similar questions and implemented suggestions but I am still having trouble getting a layer to appear on a Folium map. Basically I have used Here's isoline API to create an array of GeoJson data objects (these are bascially areas reacjable within certain drivetimes). I can render these drivetimes on a Here map but would like to use Folium as I think it's easier to set layer colors etc. So the GeoJson objects (drivetimes) are stored in an array called "values". My code is as follows: # Create the map m = folium.Map(location=[latitude, longitude], zoom_start=13) # Add a marker to the map folium.Marker( [latitude, longitude] ).add_to(m) # Add the layer folium.GeoJson(data=value[0], name="geojson").add_to(m) folium.LayerControl().add_to(m) m An example of one of the drivetimes is as follows, any help would be appreciated: {"data": {"features": [{"geometry": {"coordinates": [[[-6.643639, 53.382568], [-6.635742, 53.382568], [-6.633682, 53.381882], [-6.632309, 53.380508], [-6.630249, 53.379822], [-6.622009, 53.379822], [-6.619949, 53.379135], [-6.618576, 53.377762], [-6.616516, 53.377075], [-6.611023, 53.377075], [-6.608963, 53.376389], [-6.60759, 53.375015], [-6.60553, 53.374329], [-6.600037, 53.374329], [-6.597977, 53.375015], [-6.596603, 53.376389], [-6.594543, 53.377075], [-6.586304, 53.377075], [-6.584244, 53.376389], [-6.58287, 53.372269], [-6.580811, 53.371582], [-6.564331, 53.371582], [-6.562271, 53.372269], [-6.560898, 53.373642], [-6.558838, 53.374329], [-6.556091, 53.374329], [-6.554031, 53.373642], [-6.552658, 53.372269], [-6.550598, 53.371582], [-6.548538, 53.372269], [-6.547852, 53.374329], [-6.547852, 53.377075], [-6.547165, 53.379135], [-6.544418, 53.381882], [-6.542358, 53.382568], [-6.540298, 53.381882], [-6.538925, 53.380508], [-6.537552, 53.380508], [-6.536179, 53.381882], [-6.534119, 53.382568], [-6.525879, 53.382568], [-6.523819, 53.381882], [-6.522446, 53.377762], [-6.520386, 53.377075], [-6.514893, 53.377075], [-6.512833, 53.376389], [-6.511459, 53.375015], [-6.509399, 53.374329], [-6.50116, 53.374329], [-6.4991, 53.373642], [-6.497726, 53.372269], [-6.495667, 53.371582], [-6.493607, 53.372269], [-6.493607, 53.373642], [-6.497726, 53.377762], [-6.498413, 53.379822], [-6.497726, 53.381882], [-6.49498, 53.384628], [-6.49292, 53.385315], [-6.490173, 53.385315], [-6.488113, 53.384628], [-6.48674, 53.383255], [-6.48468, 53.382568], [-6.479187, 53.382568], [-6.477127, 53.381882], [-6.475754, 53.380508], [-6.471634, 53.379135], [-6.470261, 53.377762], [-6.468201, 53.377075], [-6.465454, 53.377075], [-6.463394, 53.377762], [-6.463394, 53.379135], [-6.466141, 53.381882], [-6.470261, 53.383255], [-6.473007, 53.386002], [-6.473694, 53.388062], [-6.473694, 53.396301], [-6.47438, 53.398361], [-6.4785, 53.399734], [-6.479187, 53.401794], [-6.479187, 53.404541], [-6.479874, 53.406601], [-6.481934, 53.407288], [-6.483994, 53.406601], [-6.485367, 53.402481], [-6.487427, 53.401794], [-6.495667, 53.401794], [-6.497726, 53.402481], [-6.4991, 53.406601], [-6.500473, 53.406601], [-6.501846, 53.405228], [-6.503906, 53.404541], [-6.514893, 53.404541], [-6.516953, 53.405228], [-6.517639, 53.407288], [-6.517639, 53.410034], [-6.516953, 53.412094], [-6.514893, 53.412781], [-6.512146, 53.412781], [-6.510086, 53.413467], [-6.508713, 53.414841], [-6.504593, 53.416214], [-6.50322, 53.417587], [-6.4991, 53.418961], [-6.498413, 53.421021], [-6.4991, 53.42308], [-6.50116, 53.423767], [-6.517639, 53.423767], [-6.519699, 53.424454], [-6.521072, 53.428574], [-6.525192, 53.429947], [-6.526566, 53.43132], [-6.530685, 53.432693], [-6.533432, 53.43544], [-6.534119, 53.4375], [-6.533432, 53.43956], [-6.530685, 53.442307], [-6.528625, 53.442993], [-6.525879, 53.442993], [-6.523819, 53.442307], [-6.522446, 53.440933], [-6.518326, 53.43956], [-6.516953, 53.438187], [-6.514893, 53.4375], [-6.512146, 53.4375], [-6.510086, 53.436813], [-6.508713, 53.43544], [-6.506653, 53.434753], [-6.50116, 53.434753], [-6.4991, 53.434067], [-6.497726, 53.432693], [-6.493607, 53.43132], [-6.492233, 53.429947], [-6.49086, 53.429947], [-6.489487, 53.43132], [-6.485367, 53.432693], [-6.48468, 53.434753], [-6.485367, 53.436813], [-6.489487, 53.438187], [-6.490173, 53.440247], [-6.489487, 53.442307], [-6.488113, 53.44368], [-6.488113, 53.445053], [-6.49086, 53.4478], [-6.49292, 53.448486], [-6.498413, 53.448486], [-6.500473, 53.4478], [-6.501846, 53.446426], [-6.503906, 53.44574], [-6.509399, 53.44574], [-6.511459, 53.446426], [-6.512146, 53.448486], [-6.512146, 53.451233], [-6.512833, 53.453293], [-6.514893, 53.453979], [-6.517639, 53.453979], [-6.519699, 53.454666], [-6.521072, 53.456039], [-6.525192, 53.457413], [-6.527939, 53.460159], [-6.528625, 53.462219], [-6.527939, 53.464279], [-6.525192, 53.467026], [-6.523132, 53.467712], [-6.521072, 53.467026], [-6.519699, 53.465652], [-6.517639, 53.464966], [-6.515579, 53.465652], [-6.515579, 53.467026], [-6.536179, 53.487625], [-6.536865, 53.489685], [-6.536865, 53.492432], [-6.536179, 53.494492], [-6.534119, 53.495178], [-6.528625, 53.495178], [-6.526566, 53.494492], [-6.511459, 53.479385], [-6.509399, 53.478699], [-6.506653, 53.478699], [-6.504593, 53.479385], [-6.50322, 53.480759], [-6.50116, 53.481445], [-6.4991, 53.480759], [-6.497726, 53.479385], [-6.493607, 53.478012], [-6.481247, 53.465652], [-6.479874, 53.465652], [-6.4785, 53.469772], [-6.477127, 53.471146], [-6.477127, 53.472519], [-6.4785, 53.473892], [-6.479187, 53.475952], [-6.4785, 53.478012], [-6.475754, 53.480759], [-6.473694, 53.481445], [-6.470947, 53.481445], [-6.468887, 53.480759], [-6.466141, 53.478012], [-6.465454, 53.475952], [-6.465454, 53.470459], [-6.464767, 53.468399], [-6.462021, 53.465652], [-6.457901, 53.464279], [-6.457214, 53.462219], [-6.457214, 53.459473], [-6.457901, 53.457413], [-6.459961, 53.456726], [-6.465454, 53.456726], [-6.467514, 53.456039], [-6.470261, 53.453293], [-6.470261, 53.45192], [-6.467514, 53.449173], [-6.463394, 53.4478], [-6.462021, 53.446426], [-6.459961, 53.44574], [-6.448975, 53.44574], [-6.446915, 53.446426], [-6.445541, 53.450546], [-6.443481, 53.451233], [-6.437988, 53.451233], [-6.435928, 53.450546], [-6.434555, 53.449173], [-6.430435, 53.4478], [-6.429749, 53.44574], [-6.430435, 53.44368], [-6.431808, 53.442307], [-6.431808, 53.440933], [-6.430435, 53.43956], [-6.429749, 53.4375], [-6.430435, 53.43544], [-6.434555, 53.434067], [-6.435928, 53.432693], [-6.437988, 53.432007], [-6.440735, 53.432007], [-6.442795, 53.432693], [-6.444168, 53.434067], [-6.445541, 53.434067], [-6.446915, 53.429947], [-6.451035, 53.428574], [-6.451721, 53.426514], [-6.451035, 53.424454], [-6.446915, 53.42308], [-6.445541, 53.421707], [-6.444168, 53.421707], [-6.437302, 53.428574], [-6.433182, 53.429947], [-6.431808, 53.43132], [-6.429749, 53.432007], [-6.424255, 53.432007], [-6.422195, 53.43132], [-6.418076, 53.4272], [-6.416016, 53.426514], [-6.405029, 53.426514], [-6.402969, 53.425827], [-6.401596, 53.424454], [-6.397476, 53.42308], [-6.396103, 53.421707], [-6.394043, 53.421021], [-6.391983, 53.421707], [-6.387863, 53.425827], [-6.383743, 53.4272], [-6.383057, 53.42926], [-6.383743, 53.43132], [-6.385803, 53.432007], [-6.391296, 53.432007], [-6.393356, 53.432693], [-6.39473, 53.434067], [-6.39679, 53.434753], [-6.399536, 53.434753], [-6.401596, 53.43544], [-6.402283, 53.4375], [-6.402283, 53.440247], [-6.401596, 53.442307], [-6.399536, 53.442993], [-6.383057, 53.442993], [-6.380997, 53.44368], [-6.376877, 53.4478], [-6.374817, 53.448486], [-6.369324, 53.448486], [-6.367264, 53.4478], [-6.365891, 53.446426], [-6.363831, 53.44574], [-6.361771, 53.446426], [-6.361084, 53.448486], [-6.361084, 53.451233], [-6.361771, 53.453293], [-6.363831, 53.453979], [-6.369324, 53.453979], [-6.371384, 53.454666], [-6.37207, 53.456726], [-6.37207, 53.459473], [-6.371384, 53.461533], [-6.37001, 53.462906], [-6.37001, 53.464279], [-6.371384, 53.465652], [-6.372757, 53.469772], [-6.383743, 53.480759], [-6.385803, 53.481445], [-6.38855, 53.481445], [-6.39061, 53.482132], [-6.391983, 53.483505], [-6.396103, 53.484879], [-6.397476, 53.486252], [-6.399536, 53.486938], [-6.410522, 53.486938], [-6.412582, 53.487625], [-6.413956, 53.488998], [-6.418076, 53.490372], [-6.423569, 53.495865], [-6.424255, 53.497925], [-6.424255, 53.500671], [-6.424942, 53.502731], [-6.426315, 53.504105], [-6.427002, 53.506165], [-6.427002, 53.514404], [-6.426315, 53.516464], [-6.424255, 53.517151], [-6.418762, 53.517151], [-6.416702, 53.516464], [-6.413956, 53.513718], [-6.413269, 53.511658], [-6.413269, 53.506165], [-6.412582, 53.504105], [-6.411209, 53.502731], [-6.409836, 53.498611], [-6.408463, 53.498611], [-6.407089, 53.499985], [-6.405029, 53.500671], [-6.399536, 53.500671], [-6.397476, 53.501358], [-6.39679, 53.503418], [-6.39679, 53.506165], [-6.396103, 53.508224], [-6.394043, 53.508911], [-6.38855, 53.508911], [-6.38649, 53.508224], [-6.379623, 53.501358], [-6.377563, 53.500671], [-6.37207, 53.500671], [-6.37001, 53.499985], [-6.368637, 53.498611], [-6.364517, 53.497238], [-6.363831, 53.495178], [-6.363831, 53.492432], [-6.364517, 53.490372], [-6.368637, 53.488998], [-6.368637, 53.487625], [-6.367264, 53.486252], [-6.365891, 53.482132], [-6.363144, 53.479385], [-6.361084, 53.478699], [-6.352844, 53.478699], [-6.350784, 53.478012], [-6.348038, 53.475266], [-6.348038, 53.473892], [-6.349411, 53.472519], [-6.350098, 53.470459], [-6.349411, 53.468399], [-6.348038, 53.468399], [-6.346664, 53.469772], [-6.344604, 53.470459], [-6.339111, 53.470459], [-6.337051, 53.469772], [-6.331558, 53.464279], [-6.330872, 53.462219], [-6.331558, 53.460159], [-6.335678, 53.458786], [-6.335678, 53.457413], [-6.332932, 53.454666], [-6.330872, 53.453979], [-6.319885, 53.453979], [-6.317825, 53.454666], [-6.317139, 53.456726], [-6.317139, 53.459473], [-6.316452, 53.461533], [-6.314392, 53.462219], [-6.303406, 53.462219], [-6.301346, 53.462906], [-6.299973, 53.464279], [-6.297913, 53.464966], [-6.295166, 53.464966], [-6.293106, 53.464279], [-6.290359, 53.461533], [-6.288986, 53.457413], [-6.286926, 53.456726], [-6.27594, 53.456726], [-6.27388, 53.456039], [-6.271133, 53.453293], [-6.26976, 53.449173], [-6.268387, 53.449173], [-6.267014, 53.450546], [-6.264954, 53.451233], [-6.256714, 53.451233], [-6.254654, 53.450546], [-6.253281, 53.448486], [-6.251907, 53.447113], [-6.250534, 53.4478], [-6.249161, 53.449173], [-6.247787, 53.453293], [-6.245728, 53.453979], [-6.234741, 53.453979], [-6.232681, 53.454666], [-6.228561, 53.458786], [-6.226501, 53.459473], [-6.221008, 53.459473], [-6.218948, 53.460159], [-6.218948, 53.461533], [-6.220322, 53.462906], [-6.221008, 53.464966], [-6.221008, 53.475952], [-6.220322, 53.478012], [-6.218262, 53.478699], [-6.215515, 53.478699], [-6.213455, 53.479385], [-6.212769, 53.481445], [-6.212769, 53.489685], [-6.212082, 53.491745], [-6.207962, 53.495865], [-6.207275, 53.497925], [-6.207275, 53.508911], [-6.206589, 53.510971], [-6.204529, 53.511658], [-6.199036, 53.511658], [-6.196976, 53.510971], [-6.194229, 53.508224], [-6.193542, 53.506165], [-6.193542, 53.495178], [-6.194229, 53.493118], [-6.195602, 53.491745], [-6.195602, 53.490372], [-6.194229, 53.488998], [-6.193542, 53.486938], [-6.193542, 53.484192], [-6.192856, 53.482132], [-6.191483, 53.480759], [-6.190796, 53.478699], [-6.191483, 53.476639], [-6.195602, 53.472519], [-6.196289, 53.470459], [-6.196289, 53.451233], [-6.195602, 53.449173], [-6.191483, 53.4478], [-6.188736, 53.445053], [-6.188049, 53.442993], [-6.188736, 53.440933], [-6.191483, 53.438187], [-6.193542, 53.4375], [-6.199036, 53.4375], [-6.201096, 53.436813], [-6.206589, 53.43132], [-6.206589, 53.429947], [-6.204529, 53.42926], [-6.193542, 53.42926], [-6.191483, 53.428574], [-6.190109, 53.4272], [-6.188049, 53.426514], [-6.185303, 53.426514], [-6.183243, 53.425827], [-6.182556, 53.423767], [-6.183243, 53.421707], [-6.183929, 53.420334], [-6.182556, 53.418961], [-6.17981, 53.418274], [-6.174316, 53.418274], [-6.172256, 53.417587], [-6.170883, 53.413467], [-6.166763, 53.412094], [-6.164017, 53.409348], [-6.162643, 53.405228], [-6.158524, 53.403854], [-6.155777, 53.401108], [-6.15509, 53.399048], [-6.155777, 53.396988], [-6.158524, 53.394241], [-6.162643, 53.392868], [-6.16333, 53.390808], [-6.16333, 53.388062], [-6.162643, 53.386002], [-6.160583, 53.385315], [-6.157837, 53.385315], [-6.155777, 53.386002], [-6.154404, 53.387375], [-6.152344, 53.388062], [-6.144104, 53.388062], [-6.142044, 53.387375], [-6.141357, 53.385315], [-6.141357, 53.382568], [-6.142044, 53.380508], [-6.144791, 53.377762], [-6.146851, 53.377075], [-6.149597, 53.377075], [-6.151657, 53.376389], [-6.152344, 53.374329], [-6.152344, 53.371582], [-6.15303, 53.369522], [-6.15509, 53.368835], [-6.157837, 53.368835], [-6.159897, 53.368149], [-6.16127, 53.366776], [-6.16539, 53.365402], [-6.168137, 53.362656], [-6.168137, 53.361282], [-6.166763, 53.359909], [-6.166077, 53.357849], [-6.166077, 53.355103], [-6.166763, 53.353043], [-6.168823, 53.352356], [-6.177063, 53.352356], [-6.179123, 53.353043], [-6.180496, 53.354416], [-6.182556, 53.355103], [-6.185303, 53.355103], [-6.187363, 53.354416], [-6.188049, 53.352356], [-6.188049, 53.346863], [-6.188736, 53.344803], [-6.190109, 53.34343], [-6.190796, 53.34137], [-6.190796, 53.338623], [-6.191483, 53.336563], [-6.193542, 53.335876], [-6.201782, 53.335876], [-6.203842, 53.33519], [-6.203842, 53.333817], [-6.202469, 53.332443], [-6.201096, 53.328323], [-6.199722, 53.32695], [-6.199036, 53.32489], [-6.199036, 53.319397], [-6.198349, 53.317337], [-6.195602, 53.31459], [-6.191483, 53.313217], [-6.190109, 53.311844], [-6.185989, 53.310471], [-6.185303, 53.308411], [-6.185303, 53.305664], [-6.185989, 53.303604], [-6.188049, 53.302917], [-6.190796, 53.302917], [-6.192856, 53.302231], [-6.194229, 53.298111], [-6.195602, 53.296738], [-6.196976, 53.292618], [-6.199036, 53.291931], [-6.207275, 53.291931], [-6.209335, 53.292618], [-6.210709, 53.293991], [-6.212769, 53.294678], [-6.221008, 53.294678], [-6.223068, 53.293991], [-6.224442, 53.289871], [-6.232681, 53.281631], [-6.236801, 53.280258], [-6.237488, 53.278198], [-6.236801, 53.276138], [-6.234741, 53.275452], [-6.210022, 53.275452], [-6.207962, 53.274765], [-6.206589, 53.273392], [-6.202469, 53.272018], [-6.201782, 53.269958], [-6.201782, 53.267212], [-6.202469, 53.265152], [-6.204529, 53.264465], [-6.210022, 53.264465], [-6.212082, 53.265152], [-6.213455, 53.266525], [-6.215515, 53.267212], [-6.237488, 53.267212], [-6.239548, 53.266525], [-6.240921, 53.265152], [-6.242981, 53.264465], [-6.245728, 53.264465], [-6.247787, 53.263779], [-6.249161, 53.262405], [-6.251221, 53.261719], [-6.25946, 53.261719], [-6.26152, 53.262405], [-6.262894, 53.263779], [-6.264954, 53.264465], [-6.270447, 53.264465], [-6.272507, 53.263779], [-6.27388, 53.262405], [-6.27594, 53.261719], [-6.289673, 53.261719], [-6.291733, 53.262405], [-6.293106, 53.263779], [-6.295166, 53.264465], [-6.300659, 53.264465], [-6.302719, 53.265152], [-6.304092, 53.266525], [-6.306152, 53.267212], [-6.314392, 53.267212], [-6.316452, 53.266525], [-6.316452, 53.265152], [-6.312332, 53.263779], [-6.311646, 53.261719], [-6.312332, 53.259659], [-6.315079, 53.256912], [-6.317139, 53.256226], [-6.322632, 53.256226], [-6.324692, 53.256912], [-6.328812, 53.261032], [-6.332932, 53.262405], [-6.334305, 53.263779], [-6.336365, 53.264465], [-6.347351, 53.264465], [-6.349411, 53.265152], [-6.350784, 53.266525], [-6.352158, 53.266525], [-6.353531, 53.265152], [-6.355591, 53.264465], [-6.363831, 53.264465], [-6.365891, 53.265152], [-6.366577, 53.267212], [-6.366577, 53.269958], [-6.367264, 53.272018], [-6.369324, 53.272705], [-6.377563, 53.272705], [-6.379623, 53.273392], [-6.380997, 53.277512], [-6.383057, 53.278198], [-6.385117, 53.277512], [-6.38649, 53.276138], [-6.38855, 53.275452], [-6.405029, 53.275452], [-6.407089, 53.276138], [-6.408463, 53.276825], [-6.409836, 53.275452], [-6.411209, 53.273392], [-6.413269, 53.272705], [-6.421509, 53.272705], [-6.423569, 53.273392], [-6.424942, 53.274765], [-6.426315, 53.274765], [-6.427689, 53.270645], [-6.429749, 53.269958], [-6.440735, 53.269958], [-6.442795, 53.269272], [-6.444168, 53.267899], [-6.446228, 53.267212], [-6.451721, 53.267212], [-6.453781, 53.267899], [-6.457901, 53.272018], [-6.459961, 53.273392], [-6.462708, 53.274765], [-6.465454, 53.275452], [-6.468201, 53.275452], [-6.470261, 53.274765], [-6.471634, 53.273392], [-6.475754, 53.272018], [-6.477127, 53.270645], [-6.4785, 53.270645], [-6.479874, 53.272018], [-6.481934, 53.272705], [-6.498413, 53.272705], [-6.500473, 53.272018], [-6.501846, 53.270645], [-6.503906, 53.269958], [-6.506653, 53.269958], [-6.508713, 53.269272], [-6.510086, 53.267899], [-6.514206, 53.266525], [-6.515579, 53.265152], [-6.517639, 53.264465], [-6.523132, 53.264465], [-6.525192, 53.265152], [-6.525879, 53.267212], [-6.525879, 53.269958], [-6.525192, 53.272018], [-6.521072, 53.273392], [-6.519699, 53.274765], [-6.515579, 53.276138], [-6.514206, 53.277512], [-6.510086, 53.278885], [-6.508713, 53.280258], [-6.506653, 53.280945], [-6.498413, 53.280945], [-6.496353, 53.281631], [-6.49498, 53.283005], [-6.49292, 53.283691], [-6.487427, 53.283691], [-6.485367, 53.284378], [-6.483994, 53.285751], [-6.481934, 53.286438], [-6.47644, 53.286438], [-6.47438, 53.287125], [-6.473694, 53.289185], [-6.473694, 53.291931], [-6.473007, 53.293991], [-6.470947, 53.294678], [-6.465454, 53.294678], [-6.463394, 53.293991], [-6.462021, 53.292618], [-6.459961, 53.291931], [-6.454468, 53.291931], [-6.452408, 53.292618], [-6.451035, 53.293991], [-6.446915, 53.295364], [-6.445541, 53.299484], [-6.444168, 53.300858], [-6.444168, 53.302231], [-6.446228, 53.302917], [-6.448975, 53.302917], [-6.451035, 53.303604], [-6.452408, 53.307724], [-6.454468, 53.308411], [-6.459961, 53.308411], [-6.462021, 53.307724], [-6.463394, 53.306351], [-6.465454, 53.305664], [-6.473694, 53.305664], [-6.475754, 53.306351], [-6.477127, 53.310471], [-6.479187, 53.311157], [-6.481247, 53.310471], [-6.48262, 53.309097], [-6.48468, 53.308411], [-6.490173, 53.308411], [-6.492233, 53.309097], [-6.49498, 53.311844], [-6.495667, 53.313904], [-6.49498, 53.315964], [-6.49086, 53.317337], [-6.48674, 53.321457], [-6.48468, 53.322144], [-6.473694, 53.322144], [-6.471634, 53.32283], [-6.467514, 53.32695], [-6.463394, 53.328323], [-6.463394, 53.329697], [-6.467514, 53.33107], [-6.468887, 53.332443], [-6.470947, 53.33313], [-6.47644, 53.33313], [-6.4785, 53.333817], [-6.479874, 53.33519], [-6.483994, 53.336563], [-6.485367, 53.337936], [-6.48674, 53.337936], [-6.49086, 53.333817], [-6.49498, 53.332443], [-6.495667, 53.330383], [-6.495667, 53.327637], [-6.496353, 53.325577], [-6.4991, 53.32283], [-6.50116, 53.322144], [-6.514893, 53.322144], [-6.516953, 53.321457], [-6.518326, 53.320084], [-6.520386, 53.319397], [-6.523132, 53.319397], [-6.525192, 53.320084], [-6.527939, 53.32283], [-6.528625, 53.32489], [-6.527939, 53.32695], [-6.523819, 53.33107], [-6.523132, 53.33313], [-6.523819, 53.33519], [-6.525879, 53.335876], [-6.527939, 53.33519], [-6.529312, 53.333817], [-6.531372, 53.33313], [-6.542358, 53.33313], [-6.544418, 53.333817], [-6.545105, 53.335876], [-6.545105, 53.338623], [-6.545792, 53.340683], [-6.548538, 53.34343], [-6.550598, 53.344116], [-6.553345, 53.344116], [-6.555405, 53.344803], [-6.556778, 53.348923], [-6.560898, 53.353043], [-6.562271, 53.357162], [-6.564331, 53.357849], [-6.569824, 53.357849], [-6.571884, 53.358536], [-6.573257, 53.359909], [-6.575317, 53.360596], [-6.583557, 53.360596], [-6.585617, 53.359909], [-6.58699, 53.355789], [-6.59111, 53.351669], [-6.592484, 53.347549], [-6.59523, 53.344803], [-6.59729, 53.344116], [-6.600037, 53.344116], [-6.602097, 53.344803], [-6.604843, 53.347549], [-6.60553, 53.349609], [-6.60553, 53.352356], [-6.604843, 53.354416], [-6.600723, 53.358536], [-6.600037, 53.360596], [-6.600723, 53.362656], [-6.60347, 53.365402], [-6.60553, 53.366089], [-6.61377, 53.366089], [-6.615829, 53.366776], [-6.617203, 53.368149], [-6.619263, 53.368835], [-6.624756, 53.368835], [-6.626816, 53.369522], [-6.628189, 53.370895], [-6.630249, 53.371582], [-6.635742, 53.371582], [-6.637802, 53.372269], [-6.639175, 53.373642], [-6.641235, 53.374329], [-6.643982, 53.374329], [-6.646042, 53.375015], [-6.646729, 53.377075], [-6.646729, 53.379822], [-6.646385, 53.381882], [-6.645355, 53.382568], [-6.643639, 53.382568]]], "type": "Polygon"}, "properties": {"range": {"type": "time", "value": 1500}}, "type": "Feature"}], "type": "FeatureCollection"}, "type": "GeoJSON"}
To specify polygons without changing the data in your question, you need to specify geometry values in dictionary form. import folium latitude = 53.372 longitude = -6.49 # Create the map m = folium.Map(location=[latitude, longitude], zoom_start=10) # Add a marker to the map folium.Marker( [latitude, longitude] ).add_to(m) # Add the layer folium.GeoJson(data=value['data']['features'][0]['geometry'], name="geojson").add_to(m) folium.LayerControl().add_to(m) m
I get a message "local variable 'cache' referenced before assignment
I'm trying to make a Morse encrypter, and I don't see why my code isn't working. I have a working one which I made using a tutorial, but this one and that one mainly have the samr guts. My code: cheat_sheet = { 'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-'} cache = 'beans' def morse_encrypter (placeholder): for letter in placeholder: cache += cheat_sheet [letter] return cache b = input ('bruh') def DO_THE_THING(): placeholder = b the_answer = morse_encrypter(placeholder.upper()) print (the_answer) DO_THE_THING ()
The problem is your code references the current value of cache via the += operation but it's not defined. Variables used in functions are local to them by default, so you could fix that by adding a cache = '' at the very beginning in the morse_encrypter(). However. the fact is you don't really need the variable at all if you instead used what is called a generator expression as shown below: def morse_encrypter(placeholder): return ''.join(cheat_sheet[letter] for letter in placeholder) There's additional information on generator expressions and related list comprehensions here in the documentation if you're interested.
Use "global cache" in morse_encrypter() This should work- cheat_sheet = { 'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-'} cache = 'beans' def morse_encrypter (placeholder): global cache for letter in placeholder: cache += cheat_sheet [letter] return cache b = input ('bruh') def DO_THE_THING(): placeholder = b the_answer = morse_encrypter(placeholder.upper()) print (the_answer) DO_THE_THING () Or you can initiate a local instance of 'cache', depending on your program requirements.
Put cache = '' in the method morse_encrypted
How do I split a text which contains figures and letters in a specific way?
I tried to find a solution online. But I wasn't successful. I would like to split a text like this: that's the original: '16.11., ARD, 20.15 Uhr: „Mein Schwiegervater, der Camper“ Vor einigen Jahren hat die ARD-Tochter 17.11., ARD, 20.15 Uhr: „Tatort: Die Pfalz von oben“ Natürlich knüpft der Jubiläums-„Tatort“ mit 18.11., ZDF, 20.15 Uhr: „Stumme Schreie“ „Erschütternd“ ist als Begriff fast noch zu' This is, what I need: ['16.11., ARD, 20.15 Uhr: „Mein Schwiegervater, der Camper“ Vor einigen Jahren hat die ARD-Tochter', '17.11., ARD, 20.15 Uhr: „Tatort: Die Pfalz von oben“ Natürlich knüpft der Jubiläums-„Tatort“ mit', '18.11., ZDF, 20.15 Uhr: „Stumme Schreie“ „Erschütternd“ ist als Begriff fast noch zu'] This option would please me as well: ['16.11., ARD, 20.15 Uhr'], ['„Mein Schwiegervater, der Camper“ Vor einigen Jahren hat die ARD-Tochter'], ['17.11., ARD, 20.15 Uhr'] ['Tatort: Die Pfalz von oben“ Natürlich knüpft der Jubiläums-„Tatort“ mit'], ['18.11., ZDF, 20.15 Uhr'], ['„Stumme Schreie“ „Erschütternd“ ist als Begriff fast noch zu'] How can I get there? I tried the split method and different other possibilities, but it didn't work out... regular expressions split might be an option, but I fail because there are two numbers in each line. Thank you in advance, Viktor
First requirement: import pprint text = """'16.11., ARD, 20.15 Uhr: „Mein Schwiegervater, der Camper“ Vor einigen Jahren hat die ARD-Tochter 17.11., ARD, 20.15 Uhr: „Tatort: Die Pfalz von oben“ Natürlich knüpft der Jubiläums-„Tatort“ mit 18.11., ZDF, 20.15 Uhr: „Stumme Schreie“ „Erschütternd“ ist als Begriff fast noch zu'""" pprint.pprint(text.split('\n\n')) Output: ['16.11., ARD, 20.15 Uhr: „Mein Schwiegervater, der Camper“\n' 'Vor einigen Jahren hat die ARD-Tochter ', '17.11., ARD, 20.15 Uhr: „Tatort: Die Pfalz von oben“\n' 'Natürlich knüpft der Jubiläums-„Tatort“ mit', '18.11., ZDF, 20.15 Uhr: „Stumme Schreie“\n' '„Erschütternd“ ist als Begriff fast noch zu'] Second requirement: lst = text.split('\n\n') lst2 = [[elem] for elem in sum((i.split(': ',1) for i in lst), [])] print(lst2) Output: [['16.11., ARD, 20.15 Uhr'], ['„Mein Schwiegervater, der Camper“\nVor einigen Jahren hat die ARD-Tochter '], ['17.11., ARD, 20.15 Uhr'], ['„Tatort: Die Pfalz von oben“\nNatürlich knüpft der Jubiläums-„Tatort“ mit'], ['18.11., ZDF, 20.15 Uhr'], ['„Stumme Schreie“\n„Erschütternd“ ist als Begriff fast noch zu']]
you can split the string on two new lines: s = open('test', 'r').read() s.split('\n\n') Out[38]: ["'16.11., ARD, 20.15 Uhr: „Mein Schwiegervater, der Camper“\nVor einigen Jahren hat die ARD-Tochter", '17.11., ARD, 20.15 Uhr: „Tatort: Die Pfalz von oben“\nNatürlich knüpft der Jubiläums-„Tatort“ mit', "18.11., ZDF, 20.15 Uhr: „Stumme Schreie“\n„Erschütternd“ ist als Begriff fast noch zu'"] Then you can split more on single new lines('\n') or colons(':') if you need to.
Python Owlready2 Ontology Creation and Reasoning, Cannot save function error, while saving equivalent_to
I try to create some semantic rules and want to save them using owlready2 for python. I want to reason over timerange related errors, so i created a error class and a timerangeWithError class. Now i want to check if the incoming error is a TimeRangeRelatedError, so i try to use the equivalent_to-method to set up some rule, that the hermit reasoner can distinguish between an Error and a TimeRelatedError. Nevertheless, the compiler raise the an error saying i can't store a function: File "C:\Users\-name-\AppData\Local\Programs\Python\Python36\lib\site-packages\owlready2\base.py", line 49, in to_literal if datatype is None: raise ValueError("Cannot store literal '%s' of type '%s'!" % (o, type(o))) ValueError: Cannot store literal '<function TimeRangeWithError.is_in_timerange at 0x000002797417B6A8>' of type '<class 'function'>'! I don't know any other way to check if the timespan is in a specific range. I think i modelled something wrong within my ontology description. Could you help me to build an ontology that is capable of checking if the value is inbetween dates? from owlready2 import * onto = get_ontology("http://test.org/rules.owl") with onto: class Error(Thing): pass class appeared_at(Error >> datetime.datetime): pass class TimeRangeWithError(Thing): def is_in_timerange(self): return self.timerange_start_at <= self.error_at and self.timerange_end_at >= self.error_at class timerange_start_at(TimeRangeWithError >> datetime.datetime): pass class timerange_end_at(TimeRangeWithError >> datetime.datetime): pass class error_at(TimeRangeWithError >> datetime.datetime): pass class TimeRangeRelatedError(Error): equivalent_to = [Error & TimeRangeWithError.is_in_timerange] onto.save() Edit:// After AWSK comment i tried to set error_at with a constraintedDatatype (should be throw an exception after reasoning if the datetime is out of bounds and in the end i have a method to check if a date is in range), but it also fails with an error: from owlready2 import * onto = get_ontology("http://test.org/onto") with onto: class Error(Thing):pass class appeared_at(Error >> datetime.datetime): pass class TimeRangeWithError(Thing):pass class timerange_start_at(TimeRangeWithError >> datetime.datetime, FunctionalProperty): pass class timerange_end_at(TimeRangeWithError >> datetime.datetime, FunctionalProperty): pass class error_at(TimeRangeWithError >> ConstrainedDatatype(int, min_inclusive = timerange_start_at, max_inclusive = timerange_end_at), FunctionalProperty): pass class is_during_timerange(TimeRangeWithError >> bool, FunctionalProperty) : pass class TimeRangeRelatedError(Error): equivalent_to = [Error & is_during_timerange] rule = Imp() rule.set_as_rule("""TimeRangeWithError(?d), timerange_start_at(?d, ?s), timerange_end_at(?d, ?e), error_at(?d, ?b), lessThanOrEqual(?b, ?s, ?r1), greaterThanOrEqual(?b, ?e, ?r2), equal(?r1, ?r2, ?r4) -> is_during_timerange(?d, ?r4)""") error = Error() timeRange = TimeRangeWithError() timeRange.shift_start_at = datetime.datetime.now() timeRange.shift_end_at = datetime.datetime.now() timeRange.error_at = datetime.datetime.now() onto.save() Error message: File "C:\Users\----\AppData\Local\Programs\Python\Python36\lib\site-packages\owlready2\triplelite.py", line 1180, in _set_data_triple_raw_spod self.execute("INSERT INTO datas VALUES (?, ?, ?, ?, ?)", (self.c, s, p, o, d)) sqlite3.InterfaceError: Error binding parameter 3 - probably unsupported type. After some work i got the following, but reasoning within pellet failed: from datetime import timedelta from owlready2 import * onto = get_ontology("http://test.org/onto.owl") with onto: class Error(Thing):pass class timerange_start_at(Error >> datetime.datetime, FunctionalProperty): python_name = "start" class timerange_end_at(Error >> datetime.datetime, FunctionalProperty): python_name = "end" class error_at(Error >> datetime.datetime, FunctionalProperty): python_name = "appeared" class is_after_start(Error >> bool, FunctionalProperty) : pass class is_before_end(Error >> bool, FunctionalProperty): pass class TimeRangeRelatedError(Error): equivalent_to = [Error & is_after_start & is_before_end] rule1 = Imp() rule1.set_as_rule("""Error(?d), timerange_start_at(?d, ?s), error_at(?d, ?b), greaterThan(?s, ?b) -> is_after_start(?d)""") rule2 = Imp() rule2.set_as_rule("""Error(?d), timerange_end_at(?d, ?e), error_at(?d, ?b), lessThan(?e, ?b) -> is_before_end(?d)""") current = datetime.datetime.now() start = current - timedelta(days=1) end = current + timedelta(days=1) error = Error(start = start, end = end, appeared = current) close_world(Error) print("Error old Classes:", error.__class__) onto.save(file="C:/Users/---/PycharmProjects/owlreasoner/rules.owl") sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True) print("Error new Classes:", error.__class__) With the following Pellet Exception, i think pellet cannot find ?d Variable (which is i thought the instance for the one error i defined upfront): File "C:/Users/---/PycharmProjects/owlreasoner/__main__.py", line 41, in <module> sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True) File "C:\Users\---\AppData\Local\Programs\Python\Python36\lib\site-packages\owlready2\reasoning.py", line 244, in sync_reasoner_pellet raise OwlReadyJavaError("Java error message is:\n%s" % (e.stderr or b"").decode("utf8")) owlready2.base.OwlReadyJavaError: Java error message is: [main] WARN org.apache.jena.riot.RDFLanguages - java-jsonld classes not on the classpath - JSON-LD input-output not available. [main] WARN org.apache.jena.riot.RDFLanguages - Minimum jarfiles are jsonld-java, jackson-core, jackson-annotations [main] WARN org.apache.jena.riot.RDFLanguages - If using a Jena distribution, put all jars in the lib/ directory on the classpath [main] WARN org.semanticweb.owlapi.util.SAXParsers - http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit not supported by parser type org.apache.xerces.jaxp.SAXParserImpl [main] WARN org.semanticweb.owlapi.util.SAXParsers - entityExpansionLimit not supported by parser type org.apache.xerces.jaxp.SAXParserImpl [main] WARN org.semanticweb.owlapi.util.SAXParsers - http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit not supported by parser type org.apache.xerces.jaxp.SAXParserImpl [main] WARN org.semanticweb.owlapi.util.SAXParsers - entityExpansionLimit not supported by parser type org.apache.xerces.jaxp.SAXParserImpl Could not translate SWRL Atom D-Object Use -v for detail. Here is my generated OWL File, maybe something gets generated in a wrong way (i am just a beginner in this domain, sorry in advanced): <?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xml:base="http://test.org/onto.owl" xmlns="http://test.org/onto.owl#" xmlns:swrl="http://www.w3.org/2003/11/swrl#" xmlns:owlr="http://www.lesfleursdunormal.fr/static/_downloads/owlready_ontology.owl#"> <owl:Ontology rdf:about="http://test.org/onto.owl"/> <owl:Class rdf:about="#Error"> <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/> <rdfs:subClassOf> <owl:Class> <owl:oneOf> <rdf:Description> <rdf:first rdf:resource="#error1"/> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </owl:oneOf> </owl:Class> </rdfs:subClassOf> </owl:Class> <owl:DatatypeProperty rdf:about="#timerange_start_at"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/> <owlr:python_name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">start</owlr:python_name> </owl:DatatypeProperty> <owl:DatatypeProperty rdf:about="#timerange_end_at"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/> <owlr:python_name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">end</owlr:python_name> </owl:DatatypeProperty> <owl:DatatypeProperty rdf:about="#error_at"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/> <owlr:python_name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">appeared</owlr:python_name> </owl:DatatypeProperty> <owl:DatatypeProperty rdf:about="#is_after_start"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#boolean"/> </owl:DatatypeProperty> <owl:DatatypeProperty rdf:about="#is_before_end"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#boolean"/> </owl:DatatypeProperty> <owl:Class rdf:about="#TimeRangeRelatedError"> <rdfs:subClassOf rdf:resource="#Error"/> <owl:equivalentClass> <owl:Class> <owl:intersectionOf rdf:parseType="Collection"> <rdf:Description rdf:about="#Error"/> <rdf:Description rdf:about="#is_after_start"/> <rdf:Description rdf:about="#is_before_end"/> </owl:intersectionOf> </owl:Class> </owl:equivalentClass> </owl:Class> <swrl:Variable rdf:about="urn:swrl#d"/> <swrl:Variable rdf:about="urn:swrl#s"/> <swrl:Variable rdf:about="urn:swrl#b"/> <swrl:Variable rdf:about="urn:swrl#e"/> <Error rdf:about="#error1"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/> <timerange_start_at rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2019-07-25T12:44:10.526106</timerange_start_at> <timerange_end_at rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2019-07-27T12:44:10.526106</timerange_end_at> <error_at rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2019-07-26T12:44:10.526106</error_at> </Error> <swrl:Imp> <swrl:body> <rdf:Description> <rdf:first> <swrl:ClassAtom> <swrl:classPredicate rdf:resource="#Error"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> </swrl:ClassAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#timerange_end_at"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:resource="urn:swrl#e"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#error_at"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:resource="urn:swrl#b"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:BuiltinAtom> <swrl:builtin rdf:resource="http://www.w3.org/2003/11/swrlb#lessThan"/> <swrl:arguments> <rdf:Description> <rdf:first rdf:resource="urn:swrl#e"/> <rdf:rest> <rdf:Description> <rdf:first rdf:resource="urn:swrl#b"/> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </rdf:rest> </rdf:Description> </swrl:arguments> </swrl:BuiltinAtom> </rdf:first> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </rdf:rest> </rdf:Description> </rdf:rest> </rdf:Description> </rdf:rest> </rdf:Description> </swrl:body> <swrl:head> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#is_before_end"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </swrl:head> </swrl:Imp> <swrl:Imp> <swrl:body> <rdf:Description> <rdf:first> <swrl:ClassAtom> <swrl:classPredicate rdf:resource="#Error"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> </swrl:ClassAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#timerange_start_at"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:resource="urn:swrl#s"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#error_at"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:resource="urn:swrl#b"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:BuiltinAtom> <swrl:builtin rdf:resource="http://www.w3.org/2003/11/swrlb#greaterThan"/> <swrl:arguments> <rdf:Description> <rdf:first rdf:resource="urn:swrl#s"/> <rdf:rest> <rdf:Description> <rdf:first rdf:resource="urn:swrl#b"/> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </rdf:rest> </rdf:Description> </swrl:arguments> </swrl:BuiltinAtom> </rdf:first> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </rdf:rest> </rdf:Description> </rdf:rest> </rdf:Description> </rdf:rest> </rdf:Description> </swrl:body> <swrl:head> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#is_after_start"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </swrl:head> </swrl:Imp> </rdf:RDF> Edit3:// Idk how to return the return value of lessThan, so i presume that the rule will only fire if lessEqual is true. Here is my new Code: from datetime import timedelta from owlready2 import * onto = get_ontology("http://test.org/onto.owl") with onto: class Error(Thing): def who(self): print("Error") class timerange_start_at(Error >> datetime.datetime, FunctionalProperty): python_name = "start" class timerange_end_at(Error >> datetime.datetime, FunctionalProperty): python_name = "end" class error_at(Error >> datetime.datetime, FunctionalProperty): python_name = "appeared" class is_after_start(Error >> bool, FunctionalProperty) : pass class is_before_end(Error >> bool, FunctionalProperty): pass class TimeRangeRelatedError(Error): equivalent_to = [Error & is_after_start.value(True) & is_before_end.value(True)] def who(self): print("TimeRange") rule1 = Imp() rule1.set_as_rule("""Error(?d), timerange_start_at(?d, ?s), error_at(?d, ?b), greaterThan(?s, ?b) -> is_after_start(?d, true)""") rule2 = Imp() rule2.set_as_rule("""Error(?d), timerange_end_at(?d, ?e), error_at(?d, ?b), lessThan(?e, ?b) -> is_before_end(?d, true)""") current = datetime.datetime.now() start = current - timedelta(days=1) end = current + timedelta(days=1) error = Error(start = start, end = end, appeared = current) close_world(Error) error.who() onto.save(file="C:/Users/---/PycharmProjects/owlreasoner/rules.owl") sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True) time.sleep(5) onto.save(file="C:/Users/---/PycharmProjects/owlreasoner/rules2.owl") error.who() And here is my new rdf file: <?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xml:base="http://test.org/onto.owl" xmlns="http://test.org/onto.owl#" xmlns:swrl="http://www.w3.org/2003/11/swrl#" xmlns:owlr="http://www.lesfleursdunormal.fr/static/_downloads/owlready_ontology.owl#"> <owl:Ontology rdf:about="http://test.org/onto.owl"/> <owl:Class rdf:about="#Error"> <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/> <rdfs:subClassOf> <owl:Class> <owl:oneOf> <rdf:Description> <rdf:first rdf:resource="#error1"/> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </owl:oneOf> </owl:Class> </rdfs:subClassOf> </owl:Class> <owl:DatatypeProperty rdf:about="#timerange_start_at"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/> <owlr:python_name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">start</owlr:python_name> </owl:DatatypeProperty> <owl:DatatypeProperty rdf:about="#timerange_end_at"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/> <owlr:python_name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">end</owlr:python_name> </owl:DatatypeProperty> <owl:DatatypeProperty rdf:about="#error_at"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/> <owlr:python_name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">appeared</owlr:python_name> </owl:DatatypeProperty> <owl:DatatypeProperty rdf:about="#is_after_start"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#boolean"/> </owl:DatatypeProperty> <owl:DatatypeProperty rdf:about="#is_before_end"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> <rdfs:domain rdf:resource="#Error"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#boolean"/> </owl:DatatypeProperty> <owl:Class rdf:about="#TimeRangeRelatedError"> <rdfs:subClassOf rdf:resource="#Error"/> <owl:equivalentClass> <owl:Class> <owl:intersectionOf rdf:parseType="Collection"> <rdf:Description rdf:about="#Error"/> <owl:Restriction> <owl:onProperty rdf:resource="#is_after_start"/> <owl:hasValue rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</owl:hasValue> </owl:Restriction> <owl:Restriction> <owl:onProperty rdf:resource="#is_before_end"/> <owl:hasValue rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</owl:hasValue> </owl:Restriction> </owl:intersectionOf> </owl:Class> </owl:equivalentClass> </owl:Class> <swrl:Variable rdf:about="urn:swrl#d"/> <swrl:Variable rdf:about="urn:swrl#s"/> <swrl:Variable rdf:about="urn:swrl#b"/> <swrl:Variable rdf:about="urn:swrl#e"/> <Error rdf:about="#error1"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/> <timerange_start_at rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2019-07-25T14:23:29.941201</timerange_start_at> <timerange_end_at rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2019-07-27T14:23:29.941201</timerange_end_at> <error_at rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2019-07-26T14:23:29.941201</error_at> </Error> <swrl:Imp> <swrl:body> <rdf:Description> <rdf:first> <swrl:ClassAtom> <swrl:classPredicate rdf:resource="#Error"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> </swrl:ClassAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#timerange_end_at"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:resource="urn:swrl#e"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#error_at"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:resource="urn:swrl#b"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:BuiltinAtom> <swrl:builtin rdf:resource="http://www.w3.org/2003/11/swrlb#lessThan"/> <swrl:arguments> <rdf:Description> <rdf:first rdf:resource="urn:swrl#e"/> <rdf:rest> <rdf:Description> <rdf:first rdf:resource="urn:swrl#b"/> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </rdf:rest> </rdf:Description> </swrl:arguments> </swrl:BuiltinAtom> </rdf:first> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </rdf:rest> </rdf:Description> </rdf:rest> </rdf:Description> </rdf:rest> </rdf:Description> </swrl:body> <swrl:head> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#is_before_end"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</swrl:argument2> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </swrl:head> </swrl:Imp> <swrl:Imp> <swrl:body> <rdf:Description> <rdf:first> <swrl:ClassAtom> <swrl:classPredicate rdf:resource="#Error"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> </swrl:ClassAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#timerange_start_at"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:resource="urn:swrl#s"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#error_at"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:resource="urn:swrl#b"/> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest> <rdf:Description> <rdf:first> <swrl:BuiltinAtom> <swrl:builtin rdf:resource="http://www.w3.org/2003/11/swrlb#greaterThan"/> <swrl:arguments> <rdf:Description> <rdf:first rdf:resource="urn:swrl#s"/> <rdf:rest> <rdf:Description> <rdf:first rdf:resource="urn:swrl#b"/> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </rdf:rest> </rdf:Description> </swrl:arguments> </swrl:BuiltinAtom> </rdf:first> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </rdf:rest> </rdf:Description> </rdf:rest> </rdf:Description> </rdf:rest> </rdf:Description> </swrl:body> <swrl:head> <rdf:Description> <rdf:first> <swrl:DatavaluedPropertyAtom> <swrl:propertyPredicate rdf:resource="#is_after_start"/> <swrl:argument1 rdf:resource="urn:swrl#d"/> <swrl:argument2 rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</swrl:argument2> </swrl:DatavaluedPropertyAtom> </rdf:first> <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/> </rdf:Description> </swrl:head> </swrl:Imp> </rdf:RDF>
I got it working finally! Thank you so much #AKSW! My assumption that the rule will only be called if lessThan or greaterThan is true, was correct. Finally you have to return a boolean, which you don't have to Flag with ^^xsd:boolean, just return true, the library do the Rest for us. And thanks for the Hint with the classes, you have to use .value(True) to compare it with the returned true (notice case-insentivity). But the python True will be mapped onto "true"^^xsd:boolean. Here is my final code, thanks again AKSW!: from datetime import timedelta from owlready2 import * onto = get_ontology("http://test.org/onto.owl") with onto: class Error(Thing): def who(self): print("Error") class timerange_start_at(Error >> datetime.datetime, FunctionalProperty): python_name = "start" class timerange_end_at(Error >> datetime.datetime, FunctionalProperty): python_name = "end" class error_at(Error >> datetime.datetime, FunctionalProperty): python_name = "appeared" class is_after_start(Error >> bool, FunctionalProperty) : pass class is_before_end(Error >> bool, FunctionalProperty): pass class TimeRangeRelatedError(Error): equivalent_to = [Error & is_after_start.value(True) & is_before_end.value(True)] def who(self): print("TimeRange") rule1 = Imp() rule1.set_as_rule("""Error(?d), timerange_start_at(?d, ?s), error_at(?d, ?b), greaterThan(?b, ?s) -> is_after_start(?d, true)""") rule2 = Imp() rule2.set_as_rule("""Error(?d), timerange_end_at(?d, ?e), error_at(?d, ?b), lessThan(?b, ?e) -> is_before_end(?d, true)""") current = datetime.datetime.now() start = current - timedelta(days=1) end = current + timedelta(days=1) error = Error(start = start, end = end, appeared = current) close_world(Error) error.who() onto.save(file="C:/Users/---/PycharmProjects/owlreasoner/rules.owl") sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True) onto.save(file="C:/Users/---/PycharmProjects/owlreasoner/rules2.owl") error.who() Output is: * Owlready2 * Warning: optimized Cython parser module 'owlready2_optimized' is not available, defaulting to slower Python implementation Error * Owlready2 * Running Pellet... ........ * Owlready2 * Pellet took 3.5119872093200684 seconds * Owlready * Equivalenting: onto.Error onto.TimeRangeRelatedError * Owlready * Equivalenting: onto.TimeRangeRelatedError onto.Error * Owlready * Reparenting onto.error1: {onto.Error} => {onto.TimeRangeRelatedError} * Owlready * Adding relation onto.error1 is_after_start true * Owlready * Adding relation onto.error1 is_before_end true TimeRange * Owlready * (NB: only changes on entities loaded in Python are shown, other changes are done but not listed)