psphere: how to make thread safe
Hi,
psphere: Python interface for the VMware vSphere Web Services SDK
I already developed an app using https://bitbucket.org/jkinred/psphere. But
getting lot of errors since psphere is not thread safe (I think). So i wrote
couple of scripts to test it (See attached files) and found that caching
mechanism used by psphere is not thread safe. Could someone please give me some
suggestion to make it thread safe.
===Test Code
import psphere
from psphere.client import Client
from psphere.managedobjects import HostSystem, VirtualMachine, ComputeResource
client = Client("192.168.0.114", "root", "vmware1") ##vCenter
print "\nSucessfully connected to vCenter.\n"
from threading import Thread
def myfunc(i):
host1 = HostSystem.get(client, name="192.168.0.134")
host2 = HostSystem.get(client, name="192.168.0.113")
print "i--",i
while True:
#host1.update(properties=["config", "vm"])
#host2.update(properties=["config", "vm"])
c = type(host1.config.network)
v = type(host2.config.network)
for vm in host1.vm:
k = vm.config.template
for vm in host2.vm:
p = vm.config.template
for i in range(10):
t = Thread(target=myfunc, args=(i,))
t.start()
"""
OUTPUT
===
Sucessfully connected to vCenter.
i-- 1
i-- 3
i-- 5
i-- 0
i-- 4
i-- 2i-- 7
i--
9
i-- 6
i-- 8
Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "vcenter_test1.py", line 19, in myfunc
k = vm.config.template
File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/__init__.py", line 79, in
__get__
value = self.fget(inst)
File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/managedobjects.py", line
1236, in config
return self._get_dataobject("config", False)
File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/__init__.py", line 116, in
_get_dataobject
return self._cache[name][0]
KeyError: 'config'
Exception in thread Thread-6:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "vcenter_test1.py", line 17, in myfunc
v = type(host2.config.network)
AttributeError: VirtualMachineConfigInfo instance has no attribute 'network'
"""
--
http://mail.python.org/mailman/listinfo/python-list
Unpaking Tuple
Hi, I am using python 2.6. I need a way to make following code working without any ValueError . >>> a, b, c, d = (1,2,3,4) >>> a, b, c, d = (1,2,3). Note: Number of values in the tuple will change dynamically. I know in python 3, you can do `a, b, c, *d = (1, 2, 3)` and then d will contain any elements that didn't fit into a,b,c. Regards, Saju -- http://mail.python.org/mailman/listinfo/python-list
I am facing an issue while decoding json string using json.loads
I am facing an issue while decoding json string using json.loads(jstring). Its
working, if i do json.dumps(eval(jstring)) before json.loads(jstring). I could
not figure out the issue. I want to avoide use of "eval" here.
## Failing without json.dumps(eval(jstring))
def format_json_string(self, jstring):
"""
"""
from pprint import pprint
print "===string before urllib.unquote===", pprint(jstring)
jstring = urllib.unquote(jstring)##For revert encode change applied in
the javascript
print "===string after urllib.unquote===", pprint(jstring)
jstring = json.loads(jstring)
print "===string after json.loads(jstring) ===", pprint(jstring)
===string from client
(javascript)u'{"selected_objects":{"datacenter-2":{"name":"Data
!@#$%25^&*()_ {}[]|%5c%2f.,?><:\\"`Center8(Data !@#$%^&*()_
{}[]|/.,?><:\\"`Center8)","type":"Datacenter","moid":"datacenter-2","actual_name":"Data
!@#$%25^&*()_ {}[]|%5c%2f.,?><:\\"`Center8","server_pools":[{"name":"cluster
~!@#$%25^&*()_ {}|\\":?><,.%2f;\'%5c][=-`(cluster ~!@#$%^&*()_
{}|\\":?><,./;\'][=-`)","type":"ClusterComputeResource","moid":"domain-c401","actual_name":"cluster
~!@#$%25^&*()_
{}|\\":?><,.%2f;\'%5c][=-`","hosts":[{"name":"192.168.1.13","type":"HostSystem","moid":"host-27","actual_name":"192.168.1.13"}]}]}}}'
None
===string after
urllib.unquoteu'{"selected_objects":{"datacenter-2":{"name":"Data
!@#$%^&*()_ {}[]|\\/.,?><:\\"`Center8(Data !@#$%^&*()_
{}[]|/.,?><:\\"`Center8)","type":"Datacenter","moid":"datacenter-2","actual_name":"Data
!@#$%^&*()_ {}[]|\\/.,?><:\\"`Center8","server_pools":[{"name":"cluster
~!@#$%^&*()_ {}|\\":?><,./;\'\\][=-`(cluster ~!@#$%^&*()_
{}|\\":?><,./;\'][=-`)","type":"ClusterComputeResource","moid":"domain-c401","actual_name":"cluster
~!@#$%^&*()_
{}|\\":?><,./;\'\\][=-`","hosts":[{"name":"192.168.1.13","type":"HostSystem","moid":"host-27","actual_name":"192.168.1.13"}]}]}}}'
None
Traceback (most recent call last):
File
"/home/saju/cvt/tk/src/cct/web/cct/cct/controllers/VcenterController.py", line
69, in import_managed_objects_from_vcenter
self.vcenter_service.validate_vcenter(session['auth'], vcenter_id, context)
File "/home/saju/cvt/tk/src/cct/web/cct/cct/viewModel/VcenterService.py",
line 360, in validate_vcenter
context = self.format_json_string(context)
File "/home/saju/cvt/tk/src/cct/web/cct/cct/viewModel/VcenterService.py",
line 747, in format_json_string
jstring = json.loads(jstring)
File
"/home/saju/cms/cct-enterprise/tg2env/local/lib/python2.7/site-packages/simplejson-2.5.0-py2.7-linux-x86_64.egg/simplejson/__init__.py",
line 451, in loads
return _default_decoder.decode(s)
File
"/home/saju/cms/cct-enterprise/tg2env/local/lib/python2.7/site-packages/simplejson-2.5.0-py2.7-linux-x86_64.egg/simplejson/decoder.py",
line 402, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File
"/home/saju/cms/cct-enterprise/tg2env/local/lib/python2.7/site-packages/simplejson-2.5.0-py2.7-linux-x86_64.egg/simplejson/decoder.py",
line 418, in raw_decode
obj, end = self.scan_once(s, idx)
JSONDecodeError: Invalid \escape: line 1 column 289 (char 289)
### Working with json.dumps(eval(jstring)) #
But, here i have to do json.dumps(eval(jstring)) before json.loads(). How to do
it without eval ???
def format_json_string(self, jstring):
"""
"""
from pprint import pprint
print "===string from client (javascript)===", pprint(jstring)
jstring = urllib.unquote(jstring)##For revert encode change applied in
the javascript
print "===string after urllib.unquote===", pprint(jstring)
jstring = json.dumps(eval(jstring))
print "===string after json.dumps(eval(jstring) ===", pprint(jstring)
jstring = json.loads(jstring)
print "===string after json.loads(jstring) ===", pprint(jstring)
===string from client
(javascript)u'{"selected_objects":{"datacenter-2":{"name":"Data
!@#$%25^&*()_ {}[]|%5c%2f.,?><:\\"`Center8(Data !@#$%^&*()_
{}[]|/.,?><:\\"`Center8)","type":"Datacenter","moid":"datacenter-2","actual_name":"Data
!@#$%25^&*()_ {}[]|%5c%2f.,?><:\\"`Center8","server_pools":[{"name":"cluster
~!@#$%25^&*()_ {}|\\":?><,.%2f;\'%5c][=-`(cluster ~!@#$%^&*()_
{}|\\":?><,./;\'][=-`)","type":"ClusterComputeResource","moid":"domain-c401","actual_name":"cluster
~!@#$%25^&*()_
{}|\\":?><,.%2f;\'%5c][=-`","hosts":[{"name":"192.168.1.13","type":"HostSystem","moid":"host-27","actual_name":"192.168.1.13"}]}]}}}'
None
===string after
urllib.unquote===u'{"selected_objects":{"datacenter-2":{"name":"Data
!@#$%^&*()_ {}[]|\\/.,?><:\\"`Center8(Data !@#$%^&*()_
{}[]|/.,?><:\\"`Center8)","type":"Datacenter","moid":"datacenter-2","actual_name":"Data
!@#$%^&*()_ {}[]|\\/.,?><:\\"`Center8","server_pools":[{"name":"cluster
~!@#$%^&*()_ {}|\\":?><,./;\'\\][=-`(clu
Re: I am facing an issue while decoding json string using json.loads
Hi, Fixed: = urllib.unquote is messing up the JSON. Reverse the order of unquote, and loads, i.e., do a json.loads on the original string, and then urllib.unquote the components that need unquote. Thanks, -- http://mail.python.org/mailman/listinfo/python-list
python: HTTP connections through a proxy server requiring authentication
Hi,
I followed http://dabase.com/blog/Minimal_squid3_proxy_configuration/ to setup
proxy server.
I tested my proxy server with firefox with IP:127.0.0.1 and Port:3128 and it
working (asking for proxy username and password).
But, i could not make http connection through proxy server requiring
authentication using following python code..
## Python code ##
import urllib2
proxy =
urllib2.ProxyHandler({'http':'http://saju:123@saju-Inspiron-N5010:3128'})
opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://python.org')
return_str = conn.read()
print "===return_st", return_str
ERROR
Traceback (most recent call last):
File "my_proxy.py", line 6, in
conn = urllib2.urlopen('http://python.org')
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 407, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 520, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 445, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 528, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required
## Proxy Server Settings ##
sudo vim /etc/squid3/squid.conf
---
auth_param digest program /usr/lib/squid3/digest_pw_auth -c
/etc/squid3/passwords
auth_param digest realm saju-Inspiron-N5010
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
http_port 3128
Setting up user
--
htdigest -c /etc/squid3/passwords saju-Inspiron-N5010 saju
==
--
http://mail.python.org/mailman/listinfo/python-list
Re: python: HTTP connections through a proxy server requiring authentication
Hi, /etc/squid3/squid.conf --- saju@saju-Inspiron-N5010:~$ cat squid.conf | grep ^[^#] auth_param digest program /usr/lib/squid3/digest_pw_auth -c /etc/squid3/passwords auth_param digest realm saju-Inspiron-N5010 acl manager proto cache_object acl localhost src 127.0.0.1/32 ::1 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1 acl authenticated proxy_auth REQUIRED acl SSL_ports port 443 acl Safe_ports port 80# http acl Safe_ports port 21# ftp acl Safe_ports port 443# https acl Safe_ports port 70# gopher acl Safe_ports port 210# wais acl Safe_ports port 1025-65535# unregistered ports acl Safe_ports port 280# http-mgmt acl Safe_ports port 488# gss-http acl Safe_ports port 591# filemaker acl Safe_ports port 777# multiling http acl CONNECT method CONNECT http_access allow manager localhost http_access deny manager http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access allow authenticated http_access deny all http_port 3128 coredump_dir /var/spool/squid3 refresh_pattern ^ftp:144020%10080 refresh_pattern ^gopher:14400%1440 refresh_pattern -i (/cgi-bin/|\?) 00%0 refresh_pattern (Release|Packages(.gz)*)$ 0 20% 2880 refresh_pattern .020%4320 saju@saju-Inspiron-N5010:~$ Thanks, -- http://mail.python.org/mailman/listinfo/python-list
Re: Guide to: Learning Python Decorators
Hi, Thanks for replay, I am looking for PDF version of same book. Please share if you can. http://www.amazon.com/gp/product/B006ZHJSIM/ref=as_li_tf_tl?ie=UTF8&tag=8012-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=B006ZHJSIM -- http://mail.python.org/mailman/listinfo/python-list
log and figure out what bits are slow and optimize them.
Hi, I want to log time taken to complete database requests inside a method/ function using decorator . is it possible I think, i have to inject log code inside the method/fuctions or modify it. I wrote a decorator to log taken by a method/function to complete it execution and its working well. My requirement : log everything and figure out what bits are slow and optimize them. What are your suggestions ?? -- http://mail.python.org/mailman/listinfo/python-list
Re: log and figure out what bits are slow and optimize them.
Hi,
Yes i saw profile module,
I think i have to do function call via
cProfile.run('foo()')
I know, we can debug this way.
But, i need a fixed logging system and want to use it in production.
I think, we can't permanently include profile's debugging code
in source code,
will cause any performance issue ??
--
http://mail.python.org/mailman/listinfo/python-list
ldap proxy user bind
I have developed a LDAP auth system using python-ldap module. Using that i can validate username and password, fetch user and groups info from LDAP directory. Now i want to implement ldap proxy user bind to the ldap server. I googled and find this http://ldapwiki.willeke.com/wiki/LDAPProxyUser But i don't have any idea about how implement it usng python-ldap. My existing LDAP settings at client side ldap_enabled = True ldap_host = your_ldap_server ldap_port = 389 ldap_basedn = o=My_omain ldap_user_key = cn ldap_group_key = groupMembership ldap_email_key = mail ldap_user_search = ou=Users ldap_group_search = ou=Groups ldap_group_objectclass = groupOfNames I want to add following 2 new flags ldap_proxy_user = ldap_proxy ldap_proxy_pwd = secret I don't know how this ldapproxy system would works. Could you please point me to an python article/example ?? -- http://mail.python.org/mailman/listinfo/python-list
Re: log and figure out what bits are slow and optimize them.
I decided to create a decorator like. import cProfile def debug_time(method): def timed(*args, **kw): prof = cProfile.Profile() prof.enable(subcalls=False, builtins=False) result = prof.runcall(method, *args, **kw) #prof.print_stats() msg = "\n\n\n\n###" msg += "\n\nURL : %s" %(tg.request.url) msg += "\nMethod: %r" %(method.__name__) print "--ddd", type(prof.getstats()) msg += "\n\nStatus : %s" %(prof.print_stats()) msg += "\n\n###" print msg LOGGER.info(msg) return result return timed Ref : http://stackoverflow.com/questions/5375624/a-decorator-that-profiles-a-method-call-and-logs-the-profiling-result I want to log it in existing log file in my project, so i tried prof.print_stats() and prof.getstats(). prof.getstats() will need extra loop for fetch data. prof.print_stats() will log library calls also. Please suggest a better way to log profiler output. -- http://mail.python.org/mailman/listinfo/python-list
Re: ldap proxy user bind
Hi Michael Torrie, Thanks to reply Why we need Twisted here, i did not get it. My understanding is that if ldap_proxy_user = ldap_proxy ldap_proxy_pwd = secret ( set more privileges to this user at ldap server side, for get other users infos) are configured at server side, then allow clients to login using username only, this time use ldap_proxy_user and ldap_proxy_pwd for login to ldap server, user validation and get user infos. Is it possible and any drawback I think this is what client need. -- http://mail.python.org/mailman/listinfo/python-list
RuntimeWarning: Unable to load template engine entry point
Hi, I am using Ubuntu 12.04 precise Python 2.7 turbogears 2.0.3 Getting following errors when doing turbogears setup. Have any way to fix this without upgrade to turbogears 2.0.4. Using /home/saju/cmt-enterprise/tg2env/lib/python2.7/site-packages/ BytecodeAssembler-0.3-py2.7.egg Searching for SymbolType==1.0 Best match: SymbolType 1.0 Processing SymbolType-1.0-py2.7.egg SymbolType 1.0 is already the active version in easy-install.pth Using /home/saju/cmt-enterprise/tg2env/lib/python2.7/site-packages/ SymbolType-1.0-py2.7.egg Finished processing dependencies for cmt==3.1dev-r5535 Setting up cmt with : /home/saju/cmt-enterprise/tg2env /home/saju/cmt-enterprise/tg2env/local/lib/python2.7/site-packages/ Pylons-0.9.7-py2.7.egg/pylons/configuration.py:26: RuntimeWarning: Unable to load template engine entry point: 'json = turbojson.jsonsupport:JsonSupport': Traceback (most recent call last): File "/home/saju/cmt-enterprise/tg2env/local/lib/python2.7/site- packages/Pylons-0.9.7-py2.7.egg/pylons/templating.py", line 610, in Engine = entry_point.load() File "/home/saju/cmt-enterprise/tg2env/local/lib/python2.7/site- packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 1954, in load entry = __import__(self.module_name, globals(),globals(), ['__name__']) File "build/bdist.linux-x86_64/egg/turbojson/__init__.py", line 1, in File "build/bdist.linux-x86_64/egg/turbojson/jsonsupport.py", line 3, in File "build/bdist.linux-x86_64/egg/turbojson/jsonify.py", line 6, in File "build/bdist.linux-x86_64/egg/peak/rules/__init__.py", line 3, in File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 729, in when(implies, (bool, bool))(lambda c1, c2: c2 or not c1) File "build/bdist.linux-x86_64/egg/peak/util/decorators.py", line 642, in do_decorate frame, getattr(f,'__name__',None), f, frame.f_locals File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 197, in callback register_for_class(None) File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 190, in register_for_class rules.add(parse_rule(engine, pred, context, cls)) File "", line 14, in add return __func(self, rule) File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 265, in add self._notify(added=actiondefs) File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 286, in _notify listener.actions_changed(added, removed) File "", line 14, in actions_changed return __func(self, added, removed) File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 433, in actions_changed self._add_method(rule.predicate, rule) File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 518, in _add_method if key!=signature and implies(key, signature): File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 591, in implies def implies(s1,s2): File "", line 14, in _regenerate return __func(self) File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 392, in _regenerate func.func_code = self.engine._generate_code() File "build/bdist.linux-x86_64/egg/peak/rules/core.py", line 553, in _generate_code c.return_(call_thru(self.function, target)) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 731, in return_ return self(ob, Code.RETURN_VALUE) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 720, in __call__ last = ob(self) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 103, in __call__ return func(*(self[1:]+(code,))) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 176, in Call code(func, *args) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 720, in __call__ last = ob(self) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 103, in __call__ return func(*(self[1:]+(code,))) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 176, in Call code(func, *args) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 727, in __call__ last = f(self, ob) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 871, in gen_tuple code(*ob) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 720, in __call__ last = ob(self) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 103, in __call__ return func(*(self[1:]+(code,))) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 237, in Suite code(*body) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 720, in __call__ last = ob(self) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 103, in __call__ return func(*(self[1:]+(code,))) File "build/bdist.linux-x86_64/egg/peak/util/assembler.py", line 221, in TryExcept next_test.JUMP_IF_FALSE, Code.POP_TOP, # remove condition AttributeError: 'Label' object has no attribute 'JUMP_IF_FALSE' import pylons.templating -- http://mail.python.org/mailman/listinfo/p
Re: RuntimeWarning: Unable to load template engine entry point
Issue get solved by updating following packages $ easy_install -U DecoratorTools $ easy_install -U turbojson -- http://mail.python.org/mailman/listinfo/python-list
LDAP: How get all users belongs to a group.
Hi, How get all users belongs to a group using python ldap module. -- http://mail.python.org/mailman/listinfo/python-list
Re: LDAP: How get all users belongs to a group.
Hi, Thanks for reply. dn: cn=My-Group-1, ou=Groups, o=CUST equivalentToMe: cn=TDS7034,ou=Internal PCA,o=CUST objectClass: groupOfNames < objectClass: top objectClass: swarePointers ACL: 2#entry#[Root]#member cn: My-Group-1 member: cn=AJP2203,ou=Internal PCA,o=CUST member: cn=AZE9632,ou=Internal PCA,o=CUST member: cn=BTC4979,ou=Internal PCA,o=CUST * I have group definition in LDAP server as above. * How fetch all members from this perticular group 'My-Group-1' using python-ldap module. * I tried, but i don't know how do it. * I want to get those 3 members from group 'My-Group-' -- http://mail.python.org/mailman/listinfo/python-list
Re: LDAP: How get all users belongs to a group.
I am using Openldap (openldap 2.3.43-12.el5_5.2 and openldap.i386 0:2.3.43_2.2.29-12.el5_6.7) -- http://mail.python.org/mailman/listinfo/python-list
Re: LDAP: How get all users belongs to a group.
--- User cn=AJP2203,ou=Internal PCA,o=CUST has group memberships to the following Groups: groupMembership: cn=My-Group-1,ou=Groups,o=CUST groupMembership: cn=My-Group-2,u=Groups,o=CUST groupMembership: cn=My-Group-3,ou=Groups,o=CUST --- Group dn: cn=My-Group-1, ou=Groups, o=CUST equivalentToMe: cn=TDS7034,ou=Internal PCA,o=CUST objectClass: groupOfNames < objectClass: top objectClass: swarePointers ACL: 2#entry#[Root]#member cn: My-Group-1 member: cn=AJP2203,ou=Internal PCA,o=CUST member: cn=AZE9632,ou=Internal PCA,o=CUST member: cn=BTC4979,ou=Internal PCA,o=CUST - * We will get groups of a member from member record, using key 'groupMembership'. * But i want to get members belogs to a particular group Eg:'My- Group-1' * Have any method in python-ldap model for this ??? -- http://mail.python.org/mailman/listinfo/python-list
Re: LDAP: How get all users belongs to a group.
results = ldapClient.search_s("cn=My-Group-1,ou=Groups,o=CUST",
ldap.SCOPE_BASE)
Is this method work for all types of groups (groupOfNames,
posixGroup) ???
have to implement user search/fetch separately for each groups ???
--
http://mail.python.org/mailman/listinfo/python-list
Change Location in the google search page
Hi,
I want to open Google search page and Change the Location link in the
left hand nav of Google) from the users current location to a
different location, then return the results of the updated search.
I tried to change google search location through
http://www.google.co.in/preferences?hl=en#loc , but getting error
mechanize._response.httperror_seek_wrapper: HTTP Error 404: Not
Found
Here is the code i tried
= 1
import re
import cookielib
from mechanize import Browser
import mechanize
br1= Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br1.set_cookiejar(cj)
br1.set_handle_robots(False)
br1.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-
US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
P_URL = "http://www.google.co.in/preferences?hl=en#loc";
s_page = br1.open(P_URL)
loc_form = br1.select_form(name="langform")
br1.form['luul'] = u'bangaluru' # <-- New location
resp = br1.submit()
print "-resp-",resp.read()
Output
===
$ python a11.py
Traceback (most recent call last):
File "a11.py", line 94, in ?
resp = br1.submit()
File "build/bdist.linux-i686/egg/mechanize/_mechanize.py", line 541,
in submit
File "build/bdist.linux-i686/egg/mechanize/_mechanize.py", line 203,
in open
File "build/bdist.linux-i686/egg/mechanize/_mechanize.py", line 255,
in _mech_open
mechanize._response.httperror_seek_wrapper: HTTP Error 404: Not Found
= 2
Tried this also, but getting blank google search page.
import re
from mechanize import Browser
LOGIN_URL = "http://www.google.co.in/search?q=new+york
+locksmith&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-
US:official&client=firefox-a"
br = Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686;
en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9
Firefox/3.0.1')]
s_page = br.open(LOGIN_URL)
loc_form = br.select_form(nr=1)
loc_text_fld = br.form.find_control(id='lc-input')
loc_text_fld.__dict__['_value'] = 'kerala' #Change the Location
resp = br.submit()
print "\n\n--resp--", resp.read() #Returning blank
google search page.
--
http://mail.python.org/mailman/listinfo/python-list
selenium pyvirtualdisplay script on remote server
Hi Friends,
Here the isuue is i can't find the "li" element. that is because that
element is out of display, so i adjust scroll bar or do focus around
that area to get that element via find_element_by_id("loc_opt")
I already tested with scroll bar and focus and its working fine in my
laptop.
But when i run this script on Remote Server, it can't find that
element.??
Note: Here i am using pyvirtualdisplay, Xvfb and Xephyr, because
server don't have Xserver.
Its also working fine with pyvirtualdisplay in my laptop. but the
issue is in Remote Server.
Has anyone faced this problem before ?
Please suggest a solution.
class Search:
def __init__(self):
"""
"""
self.display = Display(visible=0, size=(800, 600))
self.display.start()
self.url ='http://www.google.com'
self.search_url = None
self.driver = webdriver().Firefox()
def search(self, search_query, search_location=None):
"""
"""
if search_query:
self.search_url = "%s/search?q=%s" %(self.url,
search_query)
print "\nURL : ", self.search_url
self.driver.get(self.search_url)
self.submit_search()
#self.driver.execute_script("window.scrollBy(0,200)")
self.driver.execute_script("document.getElementById('tbpi').focus();")
more_search_tools_link =
self.driver.find_element_by_id("tbpi")
more_search_tools_link.click()
self.driver.execute_script("window.scrollBy(0,200)")
loc_li = self.driver.find_element_by_id("loc_opt")
--
http://mail.python.org/mailman/listinfo/python-list
Documentation using Sphinx
Hi, I am trying source code documentation using Sphinx. Here i have to copy paste all modules in to *.rst file, that is painful. Have any way to create documentation (doc for all modules, classes and methods in the project directory) from project folder quickly. I also plannig to add a code browsing facility in the document, for example: code should display if we click on a method/class name in the doc, does sphinx has this feature??? or what tool i have to choose for that ???. -- http://mail.python.org/mailman/listinfo/python-list
Secure LDAP Configuration
I want to create an LDAP database for my company with following
settings.
Only the ldap user belongs to my company can search and view ldap
entries
I want to protect ldap user belongs to my company
One ldap user can't search and view others details
Only allow ldap users to authenticate with there username and
password
I need an LDAP administrator for my company.Only he can add and
delete users from LDAP.
For these how configure /etc/openldap/slapd.conf
I need to add group and role infornations to ldap directory.Can i use
existing attributes like 'ou' for these or need to add new attribute.
Here is the LDAP entry i configured.
dn: uid=user6,dc=localhost,dc=localdomain
objectclass: top
objectclass: person
objectclass: inetorgperson
ou: [('userGroup111','userr'),('adminGroup','admin'),
('Server111','operator')]
cn: user6
sn: My company
uid: user6
userPassword: 123456
ou: [('userGroup111','userr'),('adminGroup','admin'),
('Server111','operator')] <-newly added group and role pair.Is it
correct way
Have any other way to do it.
--
http://mail.python.org/mailman/listinfo/python-list
list of tuples with dynamic change in position
I have a list of tuples l = [(('s','a'),(5,9)), (('u','w'),(9,2)),
(('y','x'),(3,0))]
some functions using this list and fetch data using index l[0][1], l[1]
[1]
I need to change position of each values in the list and that dont
affect fuctions which are using this list.
I must have to use list of tuples.
Have any way to do it using list of tuples.
--
http://mail.python.org/mailman/listinfo/python-list
Re: list of tuples with dynamic change in position
I have a list of tuples l = [(('s','a'),(5,9)), (('u','w'),(9,2)),
(('y','x'),(3,0))] and postion of values in the tuple change
dynamicaly. I need a way to access correct value even if change in
position.
--
http://mail.python.org/mailman/listinfo/python-list
Re: list of tuples with dynamic change in position
More details I have a list of tuples l = [((cpu_util,mem_util),(disk_util)), ((cpu_util,mem_util),(disk_util))] ie, l = [((30,50),(70)), ((50,20),(20))] l.sort(key=lambda x:(-x[0][0], x[1][0])) # sorting cpu_util asc and disk_util desc suppose i changed order that is l = [((mem_util,cpu_util), (disk_util)), ((mem_util,cpu_util),(disk_util))] So i have to change the sort code to l.sort(key=lambda x:(-x[0][1], x[1][0])) # sorting cpu_util asc and disk_util desc I want to use same (common) sort code, that must work even if i changed tuple order. -- http://mail.python.org/mailman/listinfo/python-list
Re: list of tuples with dynamic change in position
On Sep 7, 1:16 pm, Ulrich Eckhardt wrote: > sajuptpm wrote: > > I have a list of tuples l = [((cpu_util,mem_util),(disk_util)), > > ((cpu_util,mem_util),(disk_util))] > > ie, l = [((30,50),(70)), ((50,20),(20))] > > > l.sort(key=lambda x:(-x[0][0], x[1][0])) > > # sorting cpu_util asc and disk_util desc > > One thing here: Without knowing what special meaning the indices have, this > code is impossible to understand. In my opinion, that code should rather be > > l.sort(key=lambda x: (-x.cpu_util, x.disk_util)) > > You could then save the comment, which btw may or may not describe the > actual code. > > > suppose i changed order that is l = [((mem_util,cpu_util), > > (disk_util)), ((mem_util,cpu_util),(disk_util))] > > So i have to change the sort code to l.sort(key=lambda x:(-x[0][1], > > x[1][0])) # sorting cpu_util asc and disk_util desc > > > I want to use same (common) sort code, that must work even if i > > changed tuple order. > > You don't have to use a lambda function. You can as well define the sorting > order in the same place of your code where you define the order inside the > elements, that way you would only have to adjust the sorting in one place. > > Otherwise, you will have to embed the order of the tuple inside the tuple or > the containing list. One approach would be to have a dictionary (which has > no order), or you use a separate type instead: > > class record(object): > pass > > r = record() > r.cpu_util = 12 > r.disk_util = 42 > l.append(r) > > However, this basically all boils down to either "don't use the sorting > (dict)" or "keep records sorted", so it doesn't really answer your > question. However, I'm afraid there is no answer to your question as it > stands. Maybe I could help you better if you explained why the order > changes etc, because normally I don't deal with data that has such a > volatile meaning, I rather normalize it on input and then use it in the > same way afterwards. > > Uli > > -- > Sator Laser GmbH > Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [((cpu_util,mem_util),(disk_util)),((cpu_util,mem_util),(disk_util))] i want to find the loaded machine based on cpu and mem and desk utilization by changing this order. I created a UI using that i can change the order of item in the tuple. But the problem is asc and desc sorting -- http://mail.python.org/mailman/listinfo/python-list
another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
I have a list of tuples. l = [((30,50),(70)), ((50,20),(20))] for i in range(10): k = ((i+30,i+50),(i+70))#suppose creating new tuple in each iteration using some random value and in sert it into list. flag=True for i, v in enumerate(l): if v >= k: l.insert(i,k) flag = False break if flag: l.append(k) This code will give a list of tuples sorted in asc order. I need to change this code to sort this list by k[0][0] ascending and k[0][1] descending. What are the modifications needed in this code. I dont want to user sort() method of list. i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in another way .I want to know what the modification needed in the 'if' check to sort this list of tuples in k[0][0] ascending and k[0][1] descending. -- http://mail.python.org/mailman/listinfo/python-list
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
On Sep 7, 7:03 pm, Peter Otten <[email protected]> wrote: > sajuptpm wrote: > > i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in > > another way .I want to know what the modification needed in the 'if' > > check to sort this list of tuples in k[0][0] ascending and k[0][1] > > descending. > > It seems you are not getting any closer to your goal. Perhaps it would help > if you could explain that goal clearly rather than describing the means you > are employing to achieve it. > > > I have a list of tuples. > > > l = [((30,50),(70)), ((50,20),(20))] > > By the way, (42) is not a tuple, it's an integer. To turn it into a 1-tuple > you have to add a ',': > > >>> (42) > 42 > >>> (42,) > (42,) > >>> 42, > > (42,) > > Peter I have a list of tuples. l = [((30,50),(70,)), ((50,20),(20,))] for i in range(10): k = ((i+30,i+50),(i+70))#suppose creating new tuple in each iteration using some random value and in sert it into list. flag=True for i, v in enumerate(l): if v >= k: l.insert(i,k) flag = False break if flag: l.append(k) This code will give a list of tuples sorted in asc order. I need to change this code to sort this list by k[0][0] ascending and k[0][1] descending. What are the modifications needed in this code. I dont want to user sort() method of list. i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in another way .I want to know what the modification needed in the 'if' check to sort this list of tuples in k[0][0] ascending and k[0][1] descending. -- http://mail.python.org/mailman/listinfo/python-list
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
Detailed Description - l1 = [] l2 = [ ((3,8),(1,2)), ((1,3),(1,7)), ((7,0),(1,8)), ((4,2),(1,2)), ((2,9),(9,1)) ] I need to take each item from l2 and insert into l1 with first element(column)(3,1,7,4,2) sorted in ascending order and second element(column)(8,3,0,2,9) sorted in descending order. #SORTING for k in l2: flag=True for i, v in enumerate(l1): if v <= k: l1.insert(i,k) flag = False break if flag: l1.append(k) for a in l1: print a output --- ((7, 0), (1, 8)) ((4, 2), (1, 2)) ((3, 8), (1, 2)) ((2, 9), (9, 1)) ((1, 3), (1, 7)) This will not give l1 with first element(column)(3,1,7,4,2) sorted in ascending order and second element(column)(8,3,0,2,9) sorted in descending order. -- I added a -ve signe to all first elements l2 = [ ((-3,8),(1,2)), ((-1,3),(1,7)), ((-7,0),(1,8)), ((-4,2),(1,2)), ((-2,9),(9,1)) ] #SORTING for k in l2: flag=True for i, v in enumerate(l1): if v <= k: l1.insert(i,k) flag = False break if flag: l1.append(k) for a in l1: print a output --- ((-1, 3), (1, 7)) ((-2, 9), (9, 1)) ((-3, 8), (1, 2)) ((-4, 2), (1, 2)) ((-7, 0), (1, 8)) Now output is similar to first elements asc and second elements desc.But the problem is the -ve sign, i dont need that. Have any other method to do it?? -- http://mail.python.org/mailman/listinfo/python-list
