I am teaching a colleague Python and I think he should do some exercises.
Is there any online available other than python challenge? I feel that python challenge is puzzles, not exercises.
Hi there,
I'm relatively new to python but have a bit of experience using IDL. I was wondering if anyone knows if there are equivalent commands in python for IDL's stop and .reset commands.
If I'm running some IDL script I wrote that I put a stop command in, essentially what it does is stop the script there and give me access to the command line in the middle of the script. So I have access to all the functions and variables that I defined before the stop command, which I find really useful for debugging.
The .reset command I find extremely useful too. What it does is reset the the IDL environment (clears all variables, functions, etc.). It's as if I closed that session and opened a new one, but without having to exit and restart IDL. I find that if I'm trying to debug a script I wrote it's useful sometimes to start from scratch and not have to reset IDL (or python now). It would be useful also in python to be able to un-import any modules I had previously imported.
Any help with these issues would be greatly appreciated.
Cheers
Related
Python Drop into REPL
Is it possible to go into ipython from code?
For what I've read I need Python-Dev, how do I install it on OSX?
I think the problem I have, is, my Xcode was not properly installed, and I don't have the paths where I should.
This previous question:
http://stackoverflow.com/questions/2685887/where-is-gcc-on-osx-i-have-installed-xcode-already
Was about I couldn't find gcc, now I can't find Python.h
Should I just link my /Developer directory to somewhere else in /usr/ ???
This is my output:
$ sudo easy_install mercurial
Password:
Searching for mercurial
Reading http://pypi.python.org/simple/mercurial/
Reading http://www.selenic.com/mercurial
Best match: mercurial 1.5.1
Downloading http://mercurial.selenic.com/release/mercurial-1.5.1.tar.gz
Processing mercurial-1.5.1.tar.gz
Running mercurial-1.5.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-_7RaTq/mercurial-1.5.1/egg-dist-tmp-l7JP3u
mercurial/base85.c:12:20: error: Python.h: No such file or directory
...
Thanks in advance.
Sequel to what's missing in Python: I hear that Java's standard library is larger than Python's so I'm curious, what's in Python standard library that's missing in Java's.
I came across the Pythonwith statement for the first time today. I've been using Python lightly for several months and didn't even of its existence! Given its somewhat obscure status, I thought it would be worth asking:
What is the Pythonwith statement
designed to be used for?
What do
you use it for?
Are their any
gotchas I need to be aware of, or
common anti-patterns associated with
its use?
I'm thinking about rewriting an MS Access db I wrote years ago into a stand-alone Python app.
Other than a college class called "Intro to C++" (console only, with OOP concepts) and the Access db itself, I have no experience, so I need to ask if the basis for my decision to go withPython is correct. Is it true that:
1) Python is relatively easy to pick up?
2) Python apps can run as "portable" (ie from one directory, no registry needs, nothing needs to be pre-installed on a Windows PC) as long as I use an embedded db?
Hi guys,
I have an python error AttributeError: 'module' object has no attribute 'initialize'
I am running Python 2.6.2 on Solaris 10 UNIX and recently installed the pythonldap 2.3.9. The script is very basic, only has these 2 lines. Can anyone tell me why?? Traceback error below.
#!/usr/local/bin/python
import ldap, sys
con = ldap.initialize('ldap://localhost')
Traceback (most recent call last):
File "./myldap.py", line 5, in
con = ldap.initialize('ldap://localhost')
AttributeError: 'module' object has no attribute 'initialize'
Regards,
Jenny
hey,
I have a firmware for an USB module I can already control by visual C. Now I want to port this to python. for this I need the octopus library which is written in c. I found a file called octopus_wrap which was created by SWIG!
then I found a makefile which says:
python2.5:
swig -python -outdir ./ ../octopus.i
gcc -fPIC -c ../../liboctopus/src/octopus.c
gcc -fPIC -c ../octopus_wrap.c -I /usr/include/python2.5
gcc -fPIC -shared octopus_wrap.o octopus.o /usr/lib/libusb.so -o _octopus.so
python2.4:
swig -python -outdir ./ ../octopus.i
gcc -fPIC -c ../../liboctopus/src/octopus.c
gcc -fPIC -c ../octopus_wrap.c -I /usr/include/python2.4
gcc -fPIC -shared octopus_wrap.o octopus.o /usr/lib/libusb.so -o _octopus.so
win:
gcc -fPIC -c ../../liboctopus/src/octopus.c -I /c/Programme/libusb-win32-device-bin-0.1.10.1/include
gcc -fPIC -c octopus_wrap.c -I /c/Python25/libs -lpython25 -I/c/Python25/include -I /c/Programme/libusb-win32-device-bin-0.1.10.1/include
gcc -fPIC -shared *.o -o _octopus.pyd -L/c/Python25/libs -lpython25 -lusb -L/c/Programme/libusb-win32-device-bin-0.1.10.1/lib/gcc
clean:
rm -f octopus* _octopus*
install_python2.4:
cp _octopus.so /usr/local/lib/python2.4/site-packages/
cp octopus.py /usr/local/lib/python2.4/site-packages/
install_python2.5:
cp _octopus.so /usr/local/lib/python2.5/site-packages/
cp octopus.py /usr/local/lib/python2.5/site-packages/
I dont know how to handle this but as far as I can see octopus.py and _octopus.so are the resulting output files which are relevant to python right?
luckily someone already did that and so I put those 2 files to my "python26/lib" folder (hope it doesnt matter if it´s python 2.5 or 2.6?!)
So when working with the USB device the octopus.py is the library to work with!
Importing this file makes several problems:
>>>
Traceback (most recent call last):
File "C:\Users\ameise\My Dropbox\µC\AVR\OCTOPUS\octopususb-0.5\demos\python \blink_status.py", line 8, in <module>
from octopus import *
File "C:\Python26\lib\octopus.py", line 7, in <module>
import _octopus
ImportError: DLL load failed: module not found.
and here´s the related line 7 :
import _octopus
So there´s a problem considering the .so file!
What could be my next step?
I know that´s a lot of confusing stuff but I hope anyone of you could bring some light in my mind!
thy in advance
I'm replicating a small piece of Sugarscape agent simulation model in Python 3. I found the performance of my code is ~3 times slower than that of NetLogo. Is it likely the problem with my code, or can it be the inherent limitation of Python?
Obviously, this is just a fragment of the code, but that's where Python spends two-thirds of the run-time. I hope if I wrote something really inefficient it might show up in this fragment:
UP = (0, -1)
RIGHT = (1, 0)
DOWN = (0, 1)
LEFT = (-1, 0)
all_directions = [UP, DOWN, RIGHT, LEFT]
# point is just a tuple (x, y)
def look_around(self):
max_sugar_point = self.point
max_sugar = self.world.sugar_map[self.point].level
min_range = 0
random.shuffle(self.all_directions)
for r in range(1, self.vision+1):
for d in self.all_directions:
p = ((self.point[0] + r * d[0]) % self.world.surface.length,
(self.point[1] + r * d[1]) % self.world.surface.height)
if self.world.occupied(p): # checks if p is in a lookup table (dict)
continue
if self.world.sugar_map[p].level > max_sugar:
max_sugar = self.world.sugar_map[p].level
max_sugar_point = p
if max_sugar_point is not self.point:
self.move(max_sugar_point)
Roughly equivalent code in NetLogo (this fragment does a bit more than the Python function above):
; -- The SugarScape growth and motion procedures. --
to M ; Motion rule (page 25)
locals [ps p v d]
set ps (patches at-points neighborhood) with [count turtles-here = 0]
if (count ps > 0) [
set v psugar-of max-one-of ps [psugar] ; v is max sugar w/in vision
set ps ps with [psugar = v] ; ps is legal sites w/ v sugar
set d distance min-one-of ps [distance myself] ; d is min dist from me to ps agents
set p random-one-of ps with [distance myself = d] ; p is one of the min dist patches
if (psugar >= v and includeMyPatch?) [set p patch-here]
setxy pxcor-of p pycor-of p ; jump to p
set sugar sugar + psugar-of p ; consume its sugar
ask p [setpsugar 0] ; .. setting its sugar to 0
]
set sugar sugar - metabolism ; eat sugar (metabolism)
set age age + 1
end
On my computer, the Python code takes 15.5 sec to run 1000 steps; on the same laptop, the NetLogo simulation running in Java inside the browser finishes 1000 steps in less than 6 sec.
EDIT: Just checked Repast, using Java implementation. And it's also about the same as NetLogo at 5.4 sec. Recent comparisons between Java and Python suggest no advantage to Java, so I guess it's just my code that's to blame?
EDIT: I understand MASON is supposed to be even faster than Repast, and yet it still runs Java in the end.
The C function myfunc operates on a larger chunk of data. The results are returned in chunks to a callback function:
int myfunc(const char *data, int (*callback)(char *result, void *userdata), void *userdata);
Using ctypes, it's no big deal to call myfunc from Python code, and to have the results being returned to a Python callback function. This callback work fine.
myfunc = mylib.myfunc
myfunc.restype = c_int
myfuncFUNCTYPE = CFUNCTYPE(STRING, c_void_p)
myfunc.argtypes = [POINTER(c_char), callbackFUNCTYPE, c_void_p]
def mycb(result, userdata):
print result
return True
input="A large chunk of data."
myfunc(input, myfuncFUNCTYPE(mycb), 0)
But, is there any way to give a Python object (say a list) as userdata to the callback function? In order to store away the result chunks, I'd like to do e.g.:
def mycb(result, userdata):
userdata.append(result)
userdata=[]
But I have no idea how to cast the Python list to a c_void_p, so that it can be used in the call to myfunc.
My current workaround is to implement a linked list as a ctypes structure, which is quite cumbersome.
hi there,
I found this post: http://stackoverflow.com/questions/118143/python-regex-vs-php-regex but I actually did not get if Python's REGEX syntax matches PHP's REGEX syntax.
I started to convert some of my old PHP code to python (due to g's appengine etc.), and now I would like to know whether the regex is 100% convertable, by simple copy & paste.
regards,
Unless I'm mistaken, creating a function in python works like this
def my_func(param1, param2):
/*stuff*/
However, you don't actually give the types of those parameters. Also, if I remember, python is a strongly typed language, as such, it seams like python shouldn't let you pass in a parameter of a different type then the function creator expected. However, how does python know that the user of the function is passing in the proper types? Or will the program just die if it's the wrong type, assuming the function actually uses the parameter? Or do you have to specify the type/I'm missing something?
Thank you.
I am shortly going to buy a Wacom Bamboo Pen&Touch, and I'd like to be able to access it from Python, preferably using PyQt. I've found a Python module for accessing it under windows (cgkit/wintab) and I've seen Python code for accessing it using GTK (GnomeTabletApps).
Is there an equivalent for Mac? The Cocoa documentation provided by Wacom is way beyond me.
Quite-probably a silly question, as I don't know much about Java/Jython/JRuby/bytecode, but..
I stumbled across _why's unholy again today.. It allows you to output Python bytecode from Ruby code.. Basically allowing them to produce the same bytecode..
Jython outputs Java bytecode, as does JRuby.. Since these both compile to the same bytecode, does this mean you could potentially use any Python library from Ruby, and Ruby libraries from Python?
i'm writing a small python utility which will be consumed by moderately non-technical users and which needs to interface w/ some protobufs.
ideally, i would like the only prerequisites to using this on a local machine to be:
have python installed
* have an SVN checkout of the repository
* run a simple bash script to build the local proto .py definitions
* run "python myutility"
i'm running into trouble around importing descriptor_pb2.py, tho.
i've seen Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers? ,
but would like to avoid adding the additional prerequisite of having run the proto SDK installer.
i've modified the bash script to also generate descriptor_pb2.py in the local heirarchy,
which works for the first level of imports from my other _pb2.py files, but it looks like descriptor_pb2.py itself tries to import descriptor_pb2 can't find it:
$ python myutility.py
Traceback (most recent call last):
File "myutility.py", line 4, in <module>
import protos.myProto_pb2
File "/myPath/protos/myProto_pb2.py", line 8, in <module>
from google.protobuf import descriptor_pb2
File "/myPath/google/protobuf/descriptor_pb2.py", line 8, in <module>
from google.protobuf import descriptor_pb2
ImportError: cannot import name descriptor_pb2
my local folder looks like:
* myutility.py
* google/
* protobuf/
* descriptor.py
* descriptor_pb2.py
* protos
* myProto_ob2.py
also, i'm a python n00b, so it's possible i'm overlooking something obvious.
tia,
orion
Would it be possible to integrate Python (and/or Perl) and Ruby with some degree of transparency? I've looked at http://www.goto.info.waseda.ac.jp/~fukusima/ruby/python/doc/ and http://code.google.com/p/ruby-perl/ , but they both seem rather outdated.
Perhaps this is not an appropriate approach, but would it be possible to generate a Ruby interface for Python's C API?
Hello, I am unsure whether or not the following code is a blocking operation in python:
import httplib
import urllib
def do_request(server, port, timeout, remote_url):
conn = httplib.HTTPConnection(server, port, timeout=timeout)
conn.request("POST", remote_url, urllib.urlencode(query_dictionary, True))
conn.close()
return True
do_request("http://www.example.org", 80, 30, "foo/bar")
print "hi!"
And if it is, how would one go about creating a non-blocking asynchronous http request in python?
Thanks from a python noob.
According to the Python 2.6.5 docs [1], the bsddb module has been deprecated for removal in Python 3.0.
What module/wrapper should I be using instead?
[1] http://docs.python.org/library/bsddb.html
I don't intend to simply waste your time, but: has it occurred to you too, while using Python's with statement that it really is contrary to the 5th line of "The Zen of Python" that goes "Flat is better than nested"? Can any enlightened Python guru share me some of their insights on this?
(I always find that one more level of indentation pops up in my code every time I use with instead of f.close()... and it's not like I'm not gonna use try: ... finally: ... anyways and thus the benefits of with still elude me, even as I grow to like and understand Python more and more...)
Hi, I am learning python for a while, now I like to learn python for web, using python as a back-end of website. where and how do I start learning this:
Example usages are : connecting database and retrieve and store information from forms
Good Day,
I am using Python 2.6 and am trying to run a simple random number generator program (random.py):
import random
for i in range(5):
# random float: 0.0 <= number < 1.0
print random.random(),
# random float: 10 <= number < 20
print random.uniform(10, 20),
# random integer: 100 <= number <= 1000
print random.randint(100, 1000),
# random integer: even numbers in 100 <= number < 1000
print random.randrange(100, 1000, 2)
I'm now receiving the following error:
C:\Users\Developer\Documents\PythonDemo>python random.py
Traceback (most recent call last):
File "random.py", line 3, in <module>
import random
File "C:\Users\Developer\Documents\PythonDemo\random.py", line 8, in <module>
print random.random(),
TypeError: 'module' object is not callable
C:\Users\Developer\Documents\PythonDemo>
I've looked at the Python docs and this version of Python supports random. Is there something else I'm missing?
TIA,
coson
How can I make the following one liner print every file through Python?
python -c "import sys;print '>>',sys.argv[1:]" | dir *.*
Specifically would like to know how to pipe into a python -c.
DOS or Cygwin responses accepted.
hey guys, im trying to run a python script after every 5 minutes using cron, inside the script is a django import
import django
when running the crontab i get mailed the following error
ImportError: No module named django
this is what the crontab file looks like:
[email protected]
*/5 * * * * /usr/bin/python /Users/paulo/Desktop/ashtanga/ping/sender.py
do anyone of you know whats causing this ?
btw i do have django insalled version 1.2, python 2.6, and MacOX 10.6
I have a little piece of python code in the server script for my website which looks a little bit like this:
console.append([str(x) for x in data])
console.append(str(max(data)))
quite simple, you might think, however the result it's outputting is this:
['3', '12', '3']
3
for some reason python thinks 3 is the max of [3,12,3]!
So am I doing something wrong? Or this is misbehaviour on the part of python?