python parallell namspace issue - python

I have a problem with the namespace in a simple Python Program: can anyone point me in the right direction
import numpy as np
import simple_sim
from IPython.parallel import Client
prescale_steps = np.linspace(0.5, 1.5, 101)
val = []
c = Client()
dview = c[:]
dview.execute('import simple_sim')
dview.execute('from numpy import *')
dview['prescale_steps'] = prescale_steps
dview['val'] = val
detuning_steps = np.linspace(-11,11,101)
def fid(det):
for p in prescale_steps:
tlist, ret = simple_sim.simple_simulation(pulse_file='/home/andreas/Dropbox/puls25p8gn15map.mat', pulse_length=0.5, gamma=0, detuning=det, prescale=p)
val.append(np.array([d,p,ret[-1]]))
return val
lview = c.load_balanced_view()
res = lview.map(fid, detuning_steps)
a = res.get()
a = np.asarray(a)
always raises the Error: global name 'simple_sim' is not defined, although it should be defined shouldn't it?

Make sure that simple_sim is in the path for your ipython engines, not just your ipython shell.
I.e. if simple_sim.py in ~/mydir/, you need to run ipcluster start --n=4 in ~/mydir/ or have ~/mydir in your $PYTHON_PATH for the shell running ipcluster.

Not 100% sure, but it could be that simple_sim isin't in the site packages or in the same folder as dview. In the other words dview cant find your simple_sim module and therefore it produces error. However if that happens not to be case, I'm not sure what produces that error.

Related

Can't get correct path to roaming, when it is running as windows service

I want to get path to roaming, which should end up like this in my case:
A:\Users\Mitja\AppData\Roaming
But when program is ran as windows service all I get is:
C:\Windows\System32\config\systemprofile\AppData\Roaming
I tried multiple libraries, but all did the same. Does anyone maybe know how would I get the path?
I already tried all of these:
roaming_folder = os.getenv('APPDATA')
roaming_folder = os.path.expanduser('~\\AppData\\Roaming')
roaming_folder = os.environ['APPDATA']
def get_appdata():
CSIDL_APPDATA = 26
SHGFP_TYPE_CURRENT = 0
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_APPDATA, None, SHGFP_TYPE_CURRENT, buf)
return buf.value
roaming_folder = get_appdata()
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
roaming_folder = shell.SpecialFolders("AppData")

Multiprocessing With r2pipe

I'm having issues with using r2pipe, Radare2's API, with the multiprocessing Pool.map function in python. The problem I am facing is the application hangs on pool.join().
My hope was to use multithreading via the multiprocessing.dummy class in order to evaluate functions quickly through r2pipe. I have tried passing my r2pipe object as a namespace using the Manager class. I have attempted using events as well, but none of these seem to work.
class Test:
def __init__(self, filename=None):
if filename:
self.r2 = r2pipe.open(filename)
else:
self.r2 = r2pipe.open()
self.r2.cmd('aaa')
def t_func(self, args):
f = args[0]
r2_ns = args[1]
print('afbj # {}'.format(f['name']))
try:
bb = r2_ns.cmdj('afbj # {}'.format(f['name']))
if bb:
return bb[0]['addr']
else:
return None
except Exception as e:
print(e)
return None
def thread(self):
funcs = self.r2.cmdj('aflj')
mgr = ThreadMgr()
ns = mgr.Namespace()
ns.r2 = self.r2
pool = ThreadPool(2)
results = pool.map(self.t_func, product(funcs, [ns.r2]))
pool.close()
pool.join()
print(list(results))
This is the class I am using. I make a call to the Test.thread function in my main function.
I expect the application to print out the command it is about to run in r2pipe afbj # entry0, etc. Then to print out the list of results containing the first basic block address [40000, 50000, ...].
The application does print out the command about to run, but then hangs before printing out the results.
ENVIRONMENT
radare2: radare2 4.2.0-git 23712 # linux-x86-64 git.4.1.1-97-g5a48a4017
commit: 5a48a401787c0eab31ecfb48bebf7cdfccb66e9b build: 2020-01-09__21:44:51
r2pipe: 1.4.2
python: Python 3.6.9 (default, Nov 7 2019, 10:44:02)
system: Ubuntu 18.04.3 LTS
SOLUTION
This may be due to passing the same instance of r2pipe.open() to every call of t_func in the pool. One solution is to move the following lines of code into t_func:
r2 = r2pipe.open('filename')
r2.cmd('aaa')
This works, however its terribly slow to reanalyze for each thread/process.
Also, it is often faster to allow radare2 to do as much of the work as possible and limit the number of commands we need to send using r2pipe.
This problem is solved by using the command: afbj ##f
afbj # List basic blocks of given function and show results in json
##f # Execute the command for each function
EXAMPLE
Longer Example
import r2pipe
R2: r2pipe.open_sync = r2pipe.open('/bin/ls')
R2.cmd("aaaa")
FUNCS: list = R2.cmd('afbj ##f').split("\n")[:-1]
RESULTS: list = []
for func in FUNCS:
basic_block_info: list = eval(func)
first_block: dict = basic_block_info[0]
address_first_block: int = first_block['addr']
RESULTS.append(hex(address_first_block))
print(RESULTS)
'''
['0x4a56', '0x1636c', '0x3758', '0x15690', '0x15420', '0x154f0', '0x15420',
'0x154f0', '0x3780', '0x3790', '0x37a0', '0x37b0', '0x37c0', '0x37d0', '0x0',
...,
'0x3e90', '0x6210', '0x62f0', '0x8f60', '0x99e0', '0xa860', '0xc640', '0x3e70',
'0xd200', '0xd220', '0x133a0', '0x14480', '0x144e0', '0x145e0', '0x14840', '0x15cf0']
'''
Shorter Example
import r2pipe
R2 = r2pipe.open('/bin/ls')
R2.cmd("aaaa")
print([hex(eval(func)[0]['addr']) for func in R2.cmd('afbj ##f').split("\n")[:-1]])

SyntaxWarning: import * only allowed at module level

I am trying to make someone else program work. I have no experience of Python. I would appreciate if someone could help me here. I get the following error with python 2.6:
WSHSP.py:598: SyntaxWarning: import * only allowed at module level
def drawComposition(self, solution, goalService):
WSHSP.py:598: SyntaxWarning: import * only allowed at module level
def drawComposition(self, solution, goalService):
C:\WSPR\WebServicePath.py:3: DeprecationWarning: the sets module is deprecated
from sets import Set
here is the code:
def SMxmlPrint(self, solution, goalService, node_case):
parent = node_case
OPEN = []
CLOSE =[]
OPEN = solution
itr = 1
state = set(goalService.inputList)
for t in goalService.inputList:
if self.typeTable.has_key(t):
state |= set(self.typeTable[t])
while True:
for ws in OPEN:
if set(self.webServiceList[ws].inputList).issubset(state):
CLOSE.append(ws)
parent = self.appendChildNode(parent, str(itr), ws)
itr +=1
for ws in CLOSE:
state = state.union(self.webServiceList[ws].outputList)
for t in self.webServiceList[ws].outputList:
if self.typeTable.has_key(t):
state |= set(self.typeTable[t])
OPEN = list ( Set(solution).difference(Set(CLOSE)) )
if len(OPEN) is 0:
break
def drawComposition(self, solution, goalService):
try:
from pylab import *
except:
print ("pylab not found: see https://networkx.lanl.gov/Drawing.html for info")
raise
from networkx import *
I don't think it's a good idea to ignore warnings, but if you simply must get it out of your sight, you can use the -W flag on the command line:
python -W ignore your_script_name.py

Ipython customize prompt to display cell run time

i am wondering how to configure Ipython so that it adds the run time of the last command in milliseconds/seconds to the right command prompt. This could be done in ZSH/Bash shells as illustrated here https://coderwall.com/p/kmchbw
How should I go about doing this?
This is a code snippet that times each statement and prints it right adjusted before the next prompt, and also makes the value accessible by name 'texc'.
# Assumes from __future__ import print_function
from time import time
import blessings # Not a necessary requirement
class ExecTimer(object):
def __init__(self, ip):
self.shell = ip
self.t_pre = time()
self.texc = 0
self.prev_texc = 0
self.term = blessings.Terminal()
def pre_execute(self):
self.t_pre = time()
def post_execute(self):
self.prev_texc = self.texc
self.texc = round(time() - self.t_pre, 4)
print(self.term.bold_blue(
'{} s'.format(self.texc).rjust(self.term.width - 1)
))
# Only add or update user namespace var if it is safe to do so
if 'texc' not in self.shell.user_ns or \
self.shell.user_ns['texc'] == self.prev_texc:
self.shell.push({'texc': self.texc})
else:
pass
def register(self):
self.shell.events.register('pre_execute', self.pre_execute)
self.shell.events.register('post_execute', self.post_execute)
ExecTimer(get_ipython()).register()
To print it above the in-prompt instead, remove the print, and in ipython_config.py set:
c.PromptManager.in_template = '{texc} s\nIn[\\#]: '
or in the same file (startup.py) use
get_ipython().run_line_magic(
'config',
r"PromptManager.in_template = '{texc} s\nIn[\\#]: '"
)
For those who are interested, please refer to this issue opened in Github.
https://github.com/ipython/ipython/issues/5237

Python IRC bot system uptime

I'm trying to show system uptime in my irc bot. The script I'm using is:
#linux
import os, sys
from datetime import timedelta
from util import hook
import subprocess
import datetime
#hook.command
def uptime_linux(inp,say=None):
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
uptime_string = str(timedelta(seconds = uptime_seconds))
say(uptime_string)
# windows
def uptime():
"""Returns a datetime.timedelta instance representing the uptime in a Windows 2000/NT/XP machine"""
if not sys.platform.startswith('win'):
raise RuntimeError, "This function is to be used in windows only"
cmd = "net statistics server"
p = subprocess.Popen(cmd, shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(child_stdin, child_stdout) = (p.stdin, p.stdout)
lines = child_stdout.readlines()
child_stdin.close()
child_stdout.close()
lines = [line.strip() for line in lines if line.strip()]
date, time, ampm = lines[1].split()[2:5]
#print date, time, ampm
m, d, y = [int(v) for v in date.split('/')]
H, M = [int(v) for v in time.split(':')]
if ampm.lower() == 'pm':
H += 12
now = datetime.datetime.now()
then = datetime.datetime(y, m, d, H, M)
diff = now - then
return diff
#hook.command
def uptime_win(inp,say=None):
if __name__ == '__main__':
say(uptime())
It doesn't give me an error, but it doesn't show. I've looked at the code, I don't see why I'm not able to see it.Maybe it might something small but I don't see it :D. I have the needed modules included, and it still doesn't work :'(. Also I'd want to ask if any of you have easier method to get uptime for windows (I have for linux already).Thanks!
I don't see what's wrong right now, but in case it helps a bot I worked on did something similar, maybe you can take a look there:
uptime() at https://bazaar.launchpad.net/~p1tr-dev/p1tr/main/view/head:/plugins/info.py
using _get_output defined at https://bazaar.launchpad.net/~p1tr-dev/p1tr/main/view/head:/lib/plugin.py
I think that you are not in the main module so you have to remove if __name__ == '__main__':
Haven't tested on Windows as I don't have a Windows box handy, but using psutil (which is supposed to be cross platform)
>>> pid = psutil.Process(1) # get main process (kernel or close to it)
>>> pid
<psutil.Process(pid=1, name='init') at 31222480>
>>> pid.create_time # create_time is effectively system up time (or should be close to it)
1356597946.03

Categories

Resources